Skip to content

Field trait #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/assert.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
//! Assertions for array

use std::iter::Sum;
use num_traits::Float;
use ndarray::*;

use super::types::*;
use super::vector::*;
use super::norm::*;

pub fn rclose<A, Tol>(test: A, truth: A, rtol: Tol) -> Result<Tol, Tol>
where A: LinalgScalar + Absolute<Output = Tol>,
Tol: Float
where A: Field + Absolute<Output = Tol>,
Tol: RealField
{
let dev = (test - truth).abs() / truth.abs();
if dev < rtol { Ok(dev) } else { Err(dev) }
}

pub fn aclose<A, Tol>(test: A, truth: A, atol: Tol) -> Result<Tol, Tol>
where A: LinalgScalar + Absolute<Output = Tol>,
Tol: Float
where A: Field + Absolute<Output = Tol>,
Tol: RealField
{
let dev = (test - truth).abs();
if dev < atol { Ok(dev) } else { Err(dev) }
}

/// check two arrays are close in maximum norm
pub fn close_max<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>, truth: &ArrayBase<S2, D>, atol: Tol) -> Result<Tol, Tol>
where A: LinalgScalar + Absolute<Output = Tol>,
Tol: Float + Sum,
where A: Field + Absolute<Output = Tol>,
Tol: RealField,
S1: Data<Elem = A>,
S2: Data<Elem = A>,
D: Dimension
Expand All @@ -37,8 +35,8 @@ pub fn close_max<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>, truth: &ArrayBase<S

/// check two arrays are close in L1 norm
pub fn close_l1<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>, truth: &ArrayBase<S2, D>, rtol: Tol) -> Result<Tol, Tol>
where A: LinalgScalar + Absolute<Output = Tol>,
Tol: Float + Sum,
where A: Field + Absolute<Output = Tol>,
Tol: RealField,
S1: Data<Elem = A>,
S2: Data<Elem = A>,
D: Dimension
Expand All @@ -49,8 +47,8 @@ pub fn close_l1<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>, truth: &ArrayBase<S2

/// check two arrays are close in L2 norm
pub fn close_l2<A, Tol, S1, S2, D>(test: &ArrayBase<S1, D>, truth: &ArrayBase<S2, D>, rtol: Tol) -> Result<Tol, Tol>
where A: LinalgScalar + Absolute<Output = Tol>,
Tol: Float + Sum,
where A: Field + Absolute<Output = Tol>,
Tol: RealField,
S1: Data<Elem = A>,
S2: Data<Elem = A>,
D: Dimension
Expand Down
8 changes: 6 additions & 2 deletions src/impl2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ pub use self::eigh::*;

use super::error::*;

pub trait LapackScalar: OperatorNorm_ + QR_ + SVD_ + Solve_ + Cholesky_ + Eigh_ {}
impl<A> LapackScalar for A where A: OperatorNorm_ + QR_ + SVD_ + Solve_ + Cholesky_ + Eigh_ {}
trait_alias!(LapackScalar: OperatorNorm_,
QR_,
SVD_,
Solve_,
Cholesky_,
Eigh_);

pub fn into_result<T>(info: i32, val: T) -> Result<T> {
if info == 0 {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extern crate enum_error_derive;
#[macro_use]
extern crate derive_new;

#[macro_use]
pub mod types;
pub mod error;
pub mod layout;
Expand All @@ -56,12 +57,12 @@ pub mod solve;
pub mod cholesky;
pub mod eigh;

pub mod vector;
pub mod matrix;
pub mod square;
pub mod triangular;

pub mod util;
pub mod generate;
pub mod assert;
pub mod norm;

pub mod prelude;
27 changes: 23 additions & 4 deletions src/vector.rs → src/norm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Define trait for vectors

use std::iter::Sum;
use std::ops::*;
use ndarray::*;
use num_traits::Float;

use super::types::*;

Expand All @@ -24,8 +23,8 @@ pub trait Norm {
}

impl<A, S, D, T> Norm for ArrayBase<S, D>
where A: LinalgScalar + Absolute<Output = T>,
T: Float + Sum,
where A: Field + Absolute<Output = T>,
T: RealField,
S: Data<Elem = A>,
D: Dimension
{
Expand All @@ -43,3 +42,23 @@ impl<A, S, D, T> Norm for ArrayBase<S, D>
})
}
}

pub enum NormalizeAxis {
Row = 0,
Column = 1,
}

/// normalize in L2 norm
pub fn normalize<A, S, T>(mut m: ArrayBase<S, Ix2>, axis: NormalizeAxis) -> (ArrayBase<S, Ix2>, Vec<T>)
where A: Field + Absolute<Output = T> + Div<T, Output = A>,
S: DataMut<Elem = A>,
T: RealField
{
let mut ms = Vec::new();
for mut v in m.axis_iter_mut(Axis(axis as usize)) {
let n = v.norm();
ms.push(n);
v.map_inplace(|x| *x = *x / n)
}
(m, ms)
}
3 changes: 1 addition & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub use vector::Norm;
pub use matrix::Matrix;
pub use square::SquareMatrix;
pub use triangular::*;
pub use util::*;
pub use norm::*;
pub use types::*;
pub use generate::*;
pub use assert::*;
Expand Down
54 changes: 49 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@

pub use num_complex::Complex32 as c32;
pub use num_complex::Complex64 as c64;
use num_complex::Complex;
use num_traits::Float;
use std::ops::*;
use std::fmt::Debug;
use std::iter::Sum;
use num_complex::Complex;
use num_traits::*;
use rand::Rng;
use rand::distributions::*;
use ndarray::LinalgScalar;

use super::impl2::LapackScalar;

pub use num_complex::Complex32 as c32;
pub use num_complex::Complex64 as c64;

macro_rules! trait_alias {
($name:ident: $($t:ident),*) => {

pub trait $name : $($t +)* {}

impl<T> $name for T where T: $($t +)* {}

}} // trait_alias!

trait_alias!(Field: LapackScalar,
LinalgScalar,
AssociatedReal,
AssociatedComplex,
Absolute,
SquareRoot,
Conjugate,
RandNormal,
Sum,
Debug);

trait_alias!(RealField: Field, Float);

pub trait AssociatedReal: Sized {
type Real: Float + Mul<Self, Output = Self>;
Expand All @@ -16,13 +44,17 @@ pub trait AssociatedComplex: Sized {

/// Field with norm
pub trait Absolute {
type Output: Float;
type Output: RealField;
fn squared(&self) -> Self::Output;
fn abs(&self) -> Self::Output {
self.squared().sqrt()
}
}

pub trait SquareRoot {
fn sqrt(&self) -> Self;
}

pub trait Conjugate: Copy {
fn conj(self) -> Self;
}
Expand Down Expand Up @@ -70,6 +102,18 @@ impl Absolute for $complex {
}
}

impl SquareRoot for $real {
fn sqrt(&self) -> Self {
Float::sqrt(*self)
}
}

impl SquareRoot for $complex {
fn sqrt(&self) -> Self {
Complex::sqrt(self)
}
}

impl Conjugate for $real {
fn conj(self) -> Self {
self
Expand Down
29 changes: 0 additions & 29 deletions src/util.rs

This file was deleted.