Skip to content

Revice test #51

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 3 commits into from
Jun 22, 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
38 changes: 12 additions & 26 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,26 @@ pub fn conjugate<A, Si, So>(a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
a
}

/// Random vector
pub fn random_vector<A, S>(n: usize) -> ArrayBase<S, Ix1>
pub fn random<A, S, Sh, D>(sh: Sh) -> ArrayBase<S, D>
where A: RandNormal,
S: DataOwned<Elem = A>
S: DataOwned<Elem = A>,
D: Dimension,
Sh: ShapeBuilder<Dim = D>
{
let mut rng = thread_rng();
let v: Vec<A> = (0..n).map(|_| A::randn(&mut rng)).collect();
ArrayBase::from_vec(v)
}

/// Random matrix
pub fn random_matrix<A, S>(n: usize, m: usize) -> ArrayBase<S, Ix2>
where A: RandNormal,
S: DataOwned<Elem = A>
{
let mut rng = thread_rng();
let v: Vec<A> = (0..n * m).map(|_| A::randn(&mut rng)).collect();
ArrayBase::from_shape_vec((n, m), v).unwrap()
}

/// Random square matrix
pub fn random_square<A, S>(n: usize) -> ArrayBase<S, Ix2>
where A: RandNormal,
S: DataOwned<Elem = A>
{
random_matrix(n, n)
ArrayBase::from_shape_fn(sh, |_| A::randn(&mut rng))
}

/// Random Hermite matrix
pub fn random_hermite<A, S>(n: usize) -> ArrayBase<S, Ix2>
pub fn random_hermite<A, S>(n: usize, c_order: bool) -> ArrayBase<S, Ix2>
where A: RandNormal + Conjugate + Add<Output = A>,
S: DataOwned<Elem = A> + DataMut
{
let mut a = random_square(n);
let mut a = if c_order {
random((n, n))
} else {
random((n, n).f())
};
for i in 0..n {
a[(i, i)] = a[(i, i)] + Conjugate::conj(a[(i, i)]);
for j in (i + 1)..n {
Expand All @@ -67,7 +53,7 @@ pub fn random_hpd<A, S>(n: usize) -> ArrayBase<S, Ix2>
where A: RandNormal + Conjugate + LinalgScalar,
S: DataOwned<Elem = A> + DataMut
{
let a: Array2<A> = random_square(n);
let a: Array2<A> = random((n, n));
let ah: Array2<A> = conjugate(&a);
replicate(&ah.dot(&a))
}
Expand Down
47 changes: 0 additions & 47 deletions tests/header.rs

This file was deleted.

60 changes: 28 additions & 32 deletions tests/inv.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
include!("header.rs");

macro_rules! impl_test{
($modname:ident, $clone:ident) => {
mod $modname {
use super::random_square;
use ndarray::*;
use ndarray_linalg::*;
#[test]
fn inv_random() {
let a = random_square(3);
let ai = a.$clone().inv().unwrap();
let id = Array::eye(3);
assert_close_l2!(&ai.dot(&a), &id, 1e-7);
}
extern crate ndarray;
#[macro_use]
extern crate ndarray_linalg;
extern crate num_traits;

#[test]
fn inv_random_t() {
let a = random_square(3).reversed_axes();
let ai = a.$clone().inv().unwrap();
let id = Array::eye(3);
assert_close_l2!(&ai.dot(&a), &id, 1e-7);
}
use ndarray::*;
use ndarray_linalg::*;

#[test]
#[should_panic]
fn inv_error() {
// do not have inverse
let a = Array::<f64, _>::zeros(9).into_shape((3, 3)).unwrap();
let a_inv = a.$clone().inv().unwrap();
println!("{:?}", a_inv);
}
#[test]
fn inv_random() {
let a: Array2<f64> = random((3, 3));
let ai: Array2<_> = (&a).inv().unwrap();
let id = Array::eye(3);
assert_close_l2!(&ai.dot(&a), &id, 1e-7);
}
}} // impl_test

impl_test!(owned, clone);
impl_test!(shared, to_shared);
#[test]
fn inv_random_t() {
let a: Array2<f64> = random((3, 3).f());
let ai: Array2<_> = (&a).inv().unwrap();
let id = Array::eye(3);
assert_close_l2!(&ai.dot(&a), &id, 1e-7);
}

#[test]
#[should_panic]
fn inv_error() {
// do not have inverse
let a = Array::<f64, _>::zeros(9).into_shape((3, 3)).unwrap();
let a_inv = a.inv().unwrap();
println!("{:?}", a_inv);
}
13 changes: 10 additions & 3 deletions tests/normalize.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
include!("header.rs");

extern crate ndarray;
#[macro_use]
extern crate ndarray_linalg;
extern crate num_traits;

use ndarray::*;
use ndarray_linalg::*;

#[test]
fn n_columns() {
let a = random_owned(3, 2, true);
let a: Array2<f64> = random((3, 2));
let (n, v) = normalize(a.clone(), NormalizeAxis::Column);
assert_close_l2!(&n.dot(&from_diag(&v)), &a, 1e-7);
}

#[test]
fn n_rows() {
let a = random_owned(3, 2, true);
let a: Array2<f64> = random((3, 2));
let (n, v) = normalize(a.clone(), NormalizeAxis::Row);
assert_close_l2!(&from_diag(&v).dot(&n), &a, 1e-7);
}
84 changes: 50 additions & 34 deletions tests/opnorm.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,57 @@
include!("header.rs");

macro_rules! impl_test {
($funcname:ident, $a:expr, $op1:expr, $opi:expr, $opf:expr) => {
#[test]
fn $funcname() {
let a = $a;
extern crate ndarray;
#[macro_use]
extern crate ndarray_linalg;
extern crate num_traits;

use ndarray::*;
use ndarray_linalg::*;
use num_traits::Float;

fn test(a: Array2<f64>, one: f64, inf: f64, fro: f64) {
println!("ONE = {:?}", a.opnorm_one());
println!("INF = {:?}", a.opnorm_inf());
println!("FRO = {:?}", a.opnorm_fro());
assert_rclose!(a.opnorm_fro().unwrap(), $opf, 1e-7; "Frobenius norm");
assert_rclose!(a.opnorm_one().unwrap(), $op1, 1e-7; "One norm");
assert_rclose!(a.opnorm_inf().unwrap(), $opi, 1e-7; "Infinity norm");
}
}} // impl_test

macro_rules! impl_test_opnorm {
($modname:ident, $array:ty, $range:path) => {
mod $modname {
use ndarray::*;
use ndarray_linalg::*;
use num_traits::Float;
fn gen(i: usize, j: usize, rev: bool) -> $array {
let n = (i * j + 1) as f64;
if rev {
$range(1., n, 1.).into_shape((j, i)).unwrap().reversed_axes()
} else {
$range(1., n, 1.).into_shape((i, j)).unwrap()
}
assert_rclose!(a.opnorm_one().unwrap(), one, 1e-7; "One norm");
assert_rclose!(a.opnorm_inf().unwrap(), inf, 1e-7; "Infinity norm");
assert_rclose!(a.opnorm_fro().unwrap(), fro, 1e-7; "Frobenius norm");
}

fn gen(i: usize, j: usize, rev: bool) -> Array2<f64> {
let n = (i * j + 1) as f64;
if rev {
Array::range(1., n, 1.).into_shape((j, i)).unwrap().reversed_axes()
} else {
Array::range(1., n, 1.).into_shape((i, j)).unwrap()
}
impl_test!(opnorm_square, gen(3, 3, false), 18.0, 24.0, 285.0.sqrt());
impl_test!(opnorm_square_t, gen(3, 3, true), 24.0, 18.0, 285.0.sqrt());
impl_test!(opnorm_3x4, gen(3, 4, false), 24.0, 42.0, 650.0.sqrt());
impl_test!(opnorm_4x3_t, gen(4, 3, true), 42.0, 24.0, 650.0.sqrt());
impl_test!(opnorm_3x4_t, gen(3, 4, true), 33.0, 30.0, 650.0.sqrt());
impl_test!(opnorm_4x3, gen(4, 3, false), 30.0, 33.0, 650.0.sqrt());
}
}} // impl_test_opnorm

impl_test_opnorm!(owned, Array<f64, Ix2>, Array::range);
impl_test_opnorm!(shared, RcArray<f64, Ix2>, RcArray::range);
#[test]
fn opnorm_square() {
test(gen(3, 3, false), 18.0, 24.0, 285.0.sqrt());
}

#[test]
fn opnorm_square_t() {
test(gen(3, 3, true), 24.0, 18.0, 285.0.sqrt());
}

#[test]
fn opnorm_3x4() {
test(gen(3, 4, false), 24.0, 42.0, 650.0.sqrt());
}

#[test]
fn opnorm_3x4_t() {
test(gen(3, 4, true), 33.0, 30.0, 650.0.sqrt());
}

#[test]
fn opnorm_4x3() {
test(gen(4, 3, false), 30.0, 33.0, 650.0.sqrt());
}

#[test]
fn opnorm_4x3_t() {
test(gen(4, 3, true), 42.0, 24.0, 650.0.sqrt());
}
70 changes: 45 additions & 25 deletions tests/qr.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
include!("header.rs");

macro_rules! impl_test {
($funcname:ident, $random:path, $n:expr, $m:expr, $t:expr) => {
#[test]
fn $funcname() {
use std::cmp::min;
use ndarray::*;
use ndarray_linalg::*;
let a = $random($n, $m, $t);
extern crate ndarray;
#[macro_use]
extern crate ndarray_linalg;

use std::cmp::min;
use ndarray::*;
use ndarray_linalg::*;

fn test(a: Array2<f64>, n: usize, m: usize) {
let ans = a.clone();
println!("a = \n{:?}", &a);
let (q, r) : (Array2<_>, Array2<_>) = a.qr().unwrap();
let (q, r): (Array2<_>, Array2<_>) = a.qr().unwrap();
println!("q = \n{:?}", &q);
println!("r = \n{:?}", &r);
assert_close_l2!(&q.t().dot(&q), &Array::eye(min($n, $m)), 1e-7);
assert_close_l2!(&q.t().dot(&q), &Array::eye(min(n, m)), 1e-7);
assert_close_l2!(&q.dot(&r), &ans, 1e-7);
assert_close_l2!(&drop_lower(r.clone()), &r, 1e-7);
}
}} // impl_test

macro_rules! impl_test_qr {
($modname:ident, $random:path) => {
mod $modname {
impl_test!(qr_square, $random, 3, 3, false);
impl_test!(qr_square_t, $random, 3, 3, true);
impl_test!(qr_3x4, $random, 3, 4, false);
impl_test!(qr_3x4_t, $random, 3, 4, true);
impl_test!(qr_4x3, $random, 4, 3, false);
impl_test!(qr_4x3_t, $random, 4, 3, true);

#[test]
fn qr_square() {
let a = random((3, 3));
test(a, 3, 3);
}

#[test]
fn qr_square_t() {
let a = random((3, 3).f());
test(a, 3, 3);
}
}} // impl_test_qr

impl_test_qr!(owned, super::random_owned);
impl_test_qr!(shared, super::random_shared);
#[test]
fn qr_3x4() {
let a = random((3, 4));
test(a, 3, 4);
}

#[test]
fn qr_3x4_t() {
let a = random((3, 4).f());
test(a, 3, 4);
}

#[test]
fn qr_4x3() {
let a = random((4, 3));
test(a, 4, 3);
}

#[test]
fn qr_4x3_t() {
let a = random((4, 3).f());
test(a, 4, 3);
}
Loading