2015-03-26 05:36:19 +08:00
|
|
|
extern crate nalgebra as na;
|
2015-02-17 20:32:54 +08:00
|
|
|
extern crate rand;
|
2014-09-29 01:20:22 +08:00
|
|
|
|
2015-02-17 20:32:54 +08:00
|
|
|
use rand::random;
|
2015-06-02 04:37:54 +08:00
|
|
|
use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot2, Rot3, Persp3, PerspMat3, Ortho3,
|
2015-08-07 20:44:25 +08:00
|
|
|
OrthoMat3, DMat, DVec, Row, Col, BaseFloat, Diag};
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2013-05-22 07:15:03 +08:00
|
|
|
macro_rules! test_inv_mat_impl(
|
2013-06-09 22:04:54 +08:00
|
|
|
($t: ty) => (
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2013-05-22 07:15:03 +08:00
|
|
|
let randmat : $t = random();
|
|
|
|
|
2014-09-20 05:53:36 +08:00
|
|
|
match na::inv(&randmat) {
|
|
|
|
None => { },
|
|
|
|
Some(i) => assert!(na::approx_eq(&(i * randmat), &na::one()))
|
|
|
|
}
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-05-22 07:15:03 +08:00
|
|
|
);
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2013-07-23 17:15:20 +08:00
|
|
|
macro_rules! test_transpose_mat_impl(
|
|
|
|
($t: ty) => (
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2013-07-23 17:15:20 +08:00
|
|
|
let randmat : $t = random();
|
|
|
|
|
2013-10-14 16:22:32 +08:00
|
|
|
assert!(na::transpose(&na::transpose(&randmat)) == randmat);
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
);
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2013-07-23 17:15:20 +08:00
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
macro_rules! test_qr_impl(
|
2014-05-12 20:06:25 +08:00
|
|
|
($t: ty) => (
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2014-05-12 20:06:25 +08:00
|
|
|
let randmat : $t = random();
|
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
let (q, r) = na::qr(&randmat);
|
2014-05-12 20:06:25 +08:00
|
|
|
let recomp = q * r;
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&randmat, &recomp));
|
|
|
|
}
|
|
|
|
);
|
2014-12-19 22:33:01 +08:00
|
|
|
);
|
2014-05-12 20:06:25 +08:00
|
|
|
|
2015-08-07 20:44:25 +08:00
|
|
|
macro_rules! test_cholesky_impl(
|
|
|
|
($t: ty) => (
|
|
|
|
for _ in (0usize .. 10000) {
|
|
|
|
|
|
|
|
// construct symmetric positive definite matrix
|
|
|
|
let mut randmat : $t = random();
|
|
|
|
let mut diagmat : $t = Diag::from_diag(&na::diag(&randmat));
|
|
|
|
|
|
|
|
diagmat = na::abs(&diagmat) + 1.0;
|
|
|
|
randmat = randmat * diagmat * na::transpose(&randmat);
|
|
|
|
|
|
|
|
let result = na::cholesky(&randmat);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(v) => {
|
|
|
|
let recomp = v * na::transpose(&v);
|
|
|
|
assert!(na::approx_eq(&randmat, &recomp));
|
|
|
|
},
|
|
|
|
Err(_) => assert!(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
2014-08-16 20:53:25 +08:00
|
|
|
// NOTE: deactivated untile we get a better convergence rate.
|
|
|
|
// macro_rules! test_eigen_qr_impl(
|
|
|
|
// ($t: ty) => {
|
2015-03-01 08:49:12 +08:00
|
|
|
// for _ in (0usize .. 10000) {
|
2014-08-16 20:53:25 +08:00
|
|
|
// let randmat : $t = random();
|
|
|
|
// // Make it symetric so that we can recompose the matrix to test at the end.
|
|
|
|
// let randmat = na::transpose(&randmat) * randmat;
|
|
|
|
//
|
|
|
|
// let (eigenvectors, eigenvalues) = na::eigen_qr(&randmat, &Float::epsilon(), 100);
|
|
|
|
//
|
|
|
|
// let diag: $t = Diag::from_diag(&eigenvalues);
|
|
|
|
//
|
|
|
|
// let recomp = eigenvectors * diag * na::transpose(&eigenvectors);
|
|
|
|
//
|
2015-01-10 05:52:44 +08:00
|
|
|
// println!("eigenvalues: {}", eigenvalues);
|
|
|
|
// println!(" mat: {}", randmat);
|
|
|
|
// println!("recomp: {}", recomp);
|
2014-08-16 20:53:25 +08:00
|
|
|
//
|
|
|
|
// assert!(na::approx_eq_eps(&randmat, &recomp, &1.0e-2));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// )
|
2014-08-16 18:16:26 +08:00
|
|
|
|
2013-07-23 17:15:20 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat1() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_transpose_mat_impl!(Mat1<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat2() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_transpose_mat_impl!(Mat2<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat3() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_transpose_mat_impl!(Mat3<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat4() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_transpose_mat_impl!(Mat4<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat5() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_transpose_mat_impl!(Mat5<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat6() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_transpose_mat_impl!(Mat6<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
2013-05-19 01:04:03 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat1() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_inv_mat_impl!(Mat1<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat2() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_inv_mat_impl!(Mat2<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat3() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_inv_mat_impl!(Mat3<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2013-07-01 05:19:36 +08:00
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat4() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_inv_mat_impl!(Mat4<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-01 05:19:36 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat5() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_inv_mat_impl!(Mat5<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-01 05:19:36 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat6() {
|
2013-10-18 04:40:44 +08:00
|
|
|
test_inv_mat_impl!(Mat6<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_rotation2() {
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2013-10-08 07:22:56 +08:00
|
|
|
let randmat: na::Rot2<f64> = na::one();
|
2015-01-25 01:26:36 +08:00
|
|
|
let ang = Vec1::new(na::abs(&random::<f64>()) % <f64 as BaseFloat>::pi());
|
2013-05-19 19:53:19 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&na::rotation(&na::append_rotation(&randmat, &ang)), &ang));
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
}
|
2013-06-29 23:19:21 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_index_mat2() {
|
2013-10-18 04:40:44 +08:00
|
|
|
let mat: Mat2<f64> = random();
|
2013-06-29 23:19:21 +08:00
|
|
|
|
2014-10-26 23:30:09 +08:00
|
|
|
assert!(mat[(0, 1)] == na::transpose(&mat)[(1, 0)]);
|
2013-06-29 23:19:21 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_rotation3() {
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10000) {
|
2013-10-18 04:40:44 +08:00
|
|
|
let randmat: Rot3<f64> = na::one();
|
|
|
|
let dir: Vec3<f64> = random();
|
2015-01-25 01:26:36 +08:00
|
|
|
let ang = na::normalize(&dir) * (na::abs(&random::<f64>()) % <f64 as BaseFloat>::pi());
|
2013-10-14 16:22:32 +08:00
|
|
|
let rot = na::append_rotation(&randmat, &ang);
|
2013-09-22 20:22:17 +08:00
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&(na::transpose(&rot) * rot), &na::one()));
|
2014-01-30 18:28:15 +08:00
|
|
|
}
|
2013-09-22 20:22:17 +08:00
|
|
|
}
|
|
|
|
|
2015-06-02 04:37:54 +08:00
|
|
|
#[test]
|
|
|
|
fn test_rot3_rotation_between() {
|
|
|
|
let r1: Rot3<f64> = random();
|
|
|
|
let r2: Rot3<f64> = random();
|
|
|
|
|
|
|
|
let delta = na::rotation_between(&r1, &r2);
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&(delta * r1), &r2))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rot3_angle_between() {
|
|
|
|
let r1: Rot3<f64> = random();
|
|
|
|
let r2: Rot3<f64> = random();
|
|
|
|
|
|
|
|
let delta = na::rotation_between(&r1, &r2);
|
|
|
|
let delta_angle = na::angle_between(&r1, &r2);
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&na::norm(&na::rotation(&delta)), &delta_angle))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rot2_rotation_between() {
|
|
|
|
let r1: Rot2<f64> = random();
|
|
|
|
let r2: Rot2<f64> = random();
|
|
|
|
|
|
|
|
let delta = na::rotation_between(&r1, &r2);
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&(delta * r1), &r2))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rot2_angle_between() {
|
|
|
|
let r1: Rot2<f64> = random();
|
|
|
|
let r2: Rot2<f64> = random();
|
|
|
|
|
|
|
|
let delta = na::rotation_between(&r1, &r2);
|
|
|
|
let delta_angle = na::angle_between(&r1, &r2);
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&na::norm(&na::rotation(&delta)), &delta_angle))
|
|
|
|
}
|
|
|
|
|
2013-09-22 20:22:17 +08:00
|
|
|
#[test]
|
|
|
|
fn test_mean_dmat() {
|
2013-10-18 04:40:44 +08:00
|
|
|
let mat = DMat::from_row_vec(
|
2013-09-22 20:22:17 +08:00
|
|
|
3,
|
|
|
|
3,
|
2014-11-20 16:56:26 +08:00
|
|
|
&[
|
2013-09-22 20:22:17 +08:00
|
|
|
1.0f64, 2.0, 3.0,
|
|
|
|
4.0f64, 5.0, 6.0,
|
|
|
|
7.0f64, 8.0, 9.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2014-11-20 16:56:26 +08:00
|
|
|
assert!(na::approx_eq(&na::mean(&mat), &DVec::from_slice(3, &[4.0f64, 5.0, 6.0])));
|
2013-09-22 20:22:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cov_dmat() {
|
2013-10-18 04:40:44 +08:00
|
|
|
let mat = DMat::from_row_vec(
|
2013-09-22 20:22:17 +08:00
|
|
|
5,
|
|
|
|
3,
|
2014-11-20 16:56:26 +08:00
|
|
|
&[
|
2014-07-05 16:24:43 +08:00
|
|
|
4.0f64, 2.0, 0.60,
|
|
|
|
4.2f64, 2.1, 0.59,
|
|
|
|
3.9f64, 2.0, 0.58,
|
|
|
|
4.3f64, 2.1, 0.62,
|
|
|
|
4.1f64, 2.2, 0.63
|
2013-09-22 20:22:17 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2013-10-18 04:40:44 +08:00
|
|
|
let expected = DMat::from_row_vec(
|
2013-09-22 20:22:17 +08:00
|
|
|
3,
|
|
|
|
3,
|
2014-11-20 16:56:26 +08:00
|
|
|
&[
|
2014-07-05 16:24:43 +08:00
|
|
|
0.025f64, 0.0075, 0.00175,
|
|
|
|
0.0075f64, 0.007, 0.00135,
|
|
|
|
0.00175f64, 0.00135, 0.00043
|
2013-09-22 20:22:17 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2014-01-10 03:48:30 +08:00
|
|
|
assert!(na::approx_eq(&na::cov(&mat), &expected));
|
2013-07-23 17:15:20 +08:00
|
|
|
}
|
2013-10-18 04:40:44 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transpose_dmat() {
|
|
|
|
let mat = DMat::from_row_vec(
|
|
|
|
8,
|
|
|
|
4,
|
2014-11-20 16:56:26 +08:00
|
|
|
&[
|
2015-01-10 05:52:44 +08:00
|
|
|
1u32,2, 3, 4,
|
2014-07-05 16:24:43 +08:00
|
|
|
5, 6, 7, 8,
|
|
|
|
9, 10, 11, 12,
|
|
|
|
13, 14, 15, 16,
|
|
|
|
17, 18, 19, 20,
|
|
|
|
21, 22, 23, 24,
|
|
|
|
25, 26, 27, 28,
|
|
|
|
29, 30, 31, 32
|
2013-10-18 04:40:44 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(na::transpose(&na::transpose(&mat)) == mat);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dmat_from_vec() {
|
|
|
|
let mat1 = DMat::from_row_vec(
|
|
|
|
8,
|
|
|
|
4,
|
2014-11-20 16:56:26 +08:00
|
|
|
&[
|
2014-07-05 16:24:43 +08:00
|
|
|
1i32, 2, 3, 4,
|
|
|
|
5, 6, 7, 8,
|
|
|
|
9, 10, 11, 12,
|
|
|
|
13, 14, 15, 16,
|
|
|
|
17, 18, 19, 20,
|
|
|
|
21, 22, 23, 24,
|
|
|
|
25, 26, 27, 28,
|
|
|
|
29, 30, 31, 32
|
2013-10-18 04:40:44 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let mat2 = DMat::from_col_vec(
|
|
|
|
8,
|
|
|
|
4,
|
2014-11-20 16:56:26 +08:00
|
|
|
&[
|
2014-07-05 16:24:43 +08:00
|
|
|
1i32, 5, 9, 13, 17, 21, 25, 29,
|
|
|
|
2i32, 6, 10, 14, 18, 22, 26, 30,
|
|
|
|
3i32, 7, 11, 15, 19, 23, 27, 31,
|
|
|
|
4i32, 8, 12, 16, 20, 24, 28, 32
|
2013-10-18 04:40:44 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2015-01-10 05:52:44 +08:00
|
|
|
println!("mat1: {:?}, mat2: {:?}", mat1, mat2);
|
2013-10-18 04:40:44 +08:00
|
|
|
|
|
|
|
assert!(mat1 == mat2);
|
|
|
|
}
|
2014-05-10 00:59:26 +08:00
|
|
|
|
2015-06-06 18:53:40 +08:00
|
|
|
#[test]
|
|
|
|
fn test_dmat_addition() {
|
|
|
|
let mat1 = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
1.0, 2.0,
|
|
|
|
3.0, 4.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let mat2 = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
10.0, 20.0,
|
|
|
|
30.0, 40.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
11.0, 22.0,
|
|
|
|
33.0, 44.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!((mat1 + mat2) == res);
|
2015-06-20 22:20:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dmat_multiplication() {
|
|
|
|
let mat1 = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
1.0, 2.0,
|
|
|
|
3.0, 4.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let mat2 = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
10.0, 20.0,
|
|
|
|
30.0, 40.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
70.0, 100.0,
|
|
|
|
150.0, 220.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!((mat1 * mat2) == res);
|
2015-07-12 14:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests multiplication of rectangular (non-square) matrices.
|
|
|
|
#[test]
|
|
|
|
fn test_dmat_multiplication_rect() {
|
|
|
|
let mat1 = DMat::from_row_vec(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
1.0, 2.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let mat2 = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
3.0, 4.0, 5.0,
|
|
|
|
6.0, 7.0, 8.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = DMat::from_row_vec(
|
|
|
|
1,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
15.0, 18.0, 21.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!((mat1.clone() * mat2.clone()) == res);
|
|
|
|
assert!((&mat1 * mat2.clone()) == res);
|
|
|
|
assert!((mat1.clone() * &mat2) == res);
|
|
|
|
assert!((&mat1 * &mat2) == res);
|
2015-06-06 18:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dmat_subtraction() {
|
|
|
|
let mat1 = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
1.0, 2.0,
|
|
|
|
3.0, 4.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let mat2 = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
10.0, 20.0,
|
|
|
|
30.0, 40.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let res = DMat::from_row_vec(
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
-09.0, -18.0,
|
|
|
|
-27.0, -36.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!((mat1 - mat2) == res);
|
|
|
|
}
|
|
|
|
|
2014-12-18 06:28:32 +08:00
|
|
|
/* FIXME: review qr decomposition to make it work with DMat.
|
2014-05-10 00:59:26 +08:00
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr() {
|
2015-03-01 08:49:12 +08:00
|
|
|
for _ in (0usize .. 10) {
|
2015-01-10 05:26:05 +08:00
|
|
|
let dim1: usize = random();
|
|
|
|
let dim2: usize = random();
|
2014-05-12 20:06:25 +08:00
|
|
|
let rows = min(40, max(dim1, dim2));
|
|
|
|
let cols = min(40, min(dim1, dim2));
|
|
|
|
let randmat: DMat<f64> = DMat::new_random(rows, cols);
|
2014-08-16 18:16:26 +08:00
|
|
|
let (q, r) = na::qr(&randmat);
|
2014-05-12 20:06:25 +08:00
|
|
|
let recomp = q * r;
|
|
|
|
|
|
|
|
assert!(na::approx_eq(&randmat, &recomp));
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 06:28:32 +08:00
|
|
|
*/
|
2014-05-12 20:06:25 +08:00
|
|
|
|
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat1() {
|
|
|
|
test_qr_impl!(Mat1<f64>);
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat2() {
|
|
|
|
test_qr_impl!(Mat2<f64>);
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
2014-05-10 00:59:26 +08:00
|
|
|
|
2014-05-12 20:06:25 +08:00
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat3() {
|
|
|
|
test_qr_impl!(Mat3<f64>);
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat4() {
|
|
|
|
test_qr_impl!(Mat4<f64>);
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
2014-05-10 00:59:26 +08:00
|
|
|
|
2014-05-12 20:06:25 +08:00
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat5() {
|
|
|
|
test_qr_impl!(Mat5<f64>);
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat6() {
|
|
|
|
test_qr_impl!(Mat6<f64>);
|
2014-05-10 00:59:26 +08:00
|
|
|
}
|
2014-07-27 08:03:37 +08:00
|
|
|
|
2014-10-13 02:21:06 +08:00
|
|
|
// NOTE: deactivated until we get a better convergence rate.
|
2014-08-16 18:16:26 +08:00
|
|
|
// #[test]
|
|
|
|
// fn test_eigen_qr_mat1() {
|
|
|
|
// test_eigen_qr_impl!(Mat1<f64>);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_eigen_qr_mat2() {
|
|
|
|
// test_eigen_qr_impl!(Mat2<f64>);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_eigen_qr_mat3() {
|
|
|
|
// test_eigen_qr_impl!(Mat3<f64>);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_eigen_qr_mat4() {
|
|
|
|
// test_eigen_qr_impl!(Mat4<f64>);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_eigen_qr_mat5() {
|
|
|
|
// test_eigen_qr_impl!(Mat5<f64>);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[test]
|
|
|
|
// fn test_eigen_qr_mat6() {
|
|
|
|
// test_eigen_qr_impl!(Mat6<f64>);
|
|
|
|
// }
|
2014-07-27 08:03:37 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_fn() {
|
2015-01-10 05:26:05 +08:00
|
|
|
let actual: DMat<usize> = DMat::from_fn(3, 4, |i, j| 10 * i + j);
|
|
|
|
let expected: DMat<usize> = DMat::from_row_vec(3, 4,
|
2014-11-20 16:56:26 +08:00
|
|
|
&[ 0_0, 0_1, 0_2, 0_3,
|
|
|
|
1_0, 1_1, 1_2, 1_3,
|
|
|
|
2_0, 2_1, 2_2, 2_3 ]);
|
2014-07-27 08:03:37 +08:00
|
|
|
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
2014-09-20 05:51:27 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_row_3() {
|
|
|
|
let mat = Mat3::new(0.0f32, 1.0, 2.0,
|
|
|
|
3.0, 4.0, 5.0,
|
|
|
|
6.0, 7.0, 8.0);
|
|
|
|
let second_row = mat.row(1);
|
|
|
|
let second_col = mat.col(1);
|
|
|
|
|
|
|
|
assert!(second_row == Vec3::new(3.0, 4.0, 5.0));
|
|
|
|
assert!(second_col == Vec3::new(1.0, 4.0, 7.0));
|
|
|
|
}
|
2014-10-13 02:21:06 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_persp() {
|
|
|
|
let mut p = Persp3::new(42.0f64, 0.5, 1.5, 10.0);
|
|
|
|
let mut pm = PerspMat3::new(42.0f64, 0.5, 1.5, 10.0);
|
|
|
|
assert!(p.to_mat() == pm.to_mat());
|
|
|
|
assert!(p.aspect() == 42.0);
|
|
|
|
assert!(p.fov() == 0.5);
|
|
|
|
assert!(p.znear() == 1.5);
|
|
|
|
assert!(p.zfar() == 10.0);
|
|
|
|
assert!(na::approx_eq(&pm.aspect(), &42.0));
|
|
|
|
assert!(na::approx_eq(&pm.fov(), &0.5));
|
|
|
|
assert!(na::approx_eq(&pm.znear(), &1.5));
|
|
|
|
assert!(na::approx_eq(&pm.zfar(), &10.0));
|
|
|
|
|
|
|
|
p.set_fov(0.1);
|
|
|
|
pm.set_fov(0.1);
|
|
|
|
assert!(na::approx_eq(&p.to_mat(), pm.as_mat()));
|
|
|
|
|
|
|
|
p.set_znear(24.0);
|
|
|
|
pm.set_znear(24.0);
|
|
|
|
assert!(na::approx_eq(&p.to_mat(), pm.as_mat()));
|
|
|
|
|
|
|
|
p.set_zfar(61.0);
|
|
|
|
pm.set_zfar(61.0);
|
|
|
|
assert!(na::approx_eq(&p.to_mat(), pm.as_mat()));
|
|
|
|
|
|
|
|
p.set_aspect(23.0);
|
|
|
|
pm.set_aspect(23.0);
|
|
|
|
assert!(na::approx_eq(&p.to_mat(), pm.as_mat()));
|
|
|
|
|
|
|
|
assert!(p.aspect() == 23.0);
|
|
|
|
assert!(p.fov() == 0.1);
|
|
|
|
assert!(p.znear() == 24.0);
|
|
|
|
assert!(p.zfar() == 61.0);
|
|
|
|
assert!(na::approx_eq(&pm.aspect(), &23.0));
|
|
|
|
assert!(na::approx_eq(&pm.fov(), &0.1));
|
|
|
|
assert!(na::approx_eq(&pm.znear(), &24.0));
|
|
|
|
assert!(na::approx_eq(&pm.zfar(), &61.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ortho() {
|
|
|
|
let mut p = Ortho3::new(42.0f64, 0.5, 1.5, 10.0);
|
|
|
|
let mut pm = OrthoMat3::new(42.0f64, 0.5, 1.5, 10.0);
|
|
|
|
assert!(p.to_mat() == pm.to_mat());
|
|
|
|
assert!(p.width() == 42.0);
|
|
|
|
assert!(p.height() == 0.5);
|
|
|
|
assert!(p.znear() == 1.5);
|
|
|
|
assert!(p.zfar() == 10.0);
|
|
|
|
assert!(na::approx_eq(&pm.width(), &42.0));
|
|
|
|
assert!(na::approx_eq(&pm.height(), &0.5));
|
|
|
|
assert!(na::approx_eq(&pm.znear(), &1.5));
|
|
|
|
assert!(na::approx_eq(&pm.zfar(), &10.0));
|
|
|
|
|
|
|
|
p.set_width(0.1);
|
|
|
|
pm.set_width(0.1);
|
|
|
|
assert!(na::approx_eq(&p.to_mat(), pm.as_mat()));
|
|
|
|
|
|
|
|
p.set_znear(24.0);
|
|
|
|
pm.set_znear(24.0);
|
|
|
|
assert!(na::approx_eq(&p.to_mat(), pm.as_mat()));
|
|
|
|
|
|
|
|
p.set_zfar(61.0);
|
|
|
|
pm.set_zfar(61.0);
|
|
|
|
assert!(na::approx_eq(&p.to_mat(), pm.as_mat()));
|
|
|
|
|
|
|
|
p.set_height(23.0);
|
|
|
|
pm.set_height(23.0);
|
|
|
|
assert!(na::approx_eq(&p.to_mat(), pm.as_mat()));
|
|
|
|
|
|
|
|
assert!(p.height() == 23.0);
|
|
|
|
assert!(p.width() == 0.1);
|
|
|
|
assert!(p.znear() == 24.0);
|
|
|
|
assert!(p.zfar() == 61.0);
|
|
|
|
assert!(na::approx_eq(&pm.height(), &23.0));
|
|
|
|
assert!(na::approx_eq(&pm.width(), &0.1));
|
|
|
|
assert!(na::approx_eq(&pm.znear(), &24.0));
|
|
|
|
assert!(na::approx_eq(&pm.zfar(), &61.0));
|
|
|
|
}
|
2015-08-07 20:44:25 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_const() {
|
|
|
|
|
|
|
|
let a : Mat3<f64> = Mat3::<f64>::new(1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 3.0);
|
|
|
|
let g : Mat3<f64> = Mat3::<f64>::new(1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0);
|
|
|
|
|
|
|
|
let result = na::cholesky(&a);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(v) => {
|
|
|
|
assert!(na::approx_eq(&v, &g));
|
|
|
|
|
|
|
|
let recomp = v * na::transpose(&v);
|
|
|
|
assert!(na::approx_eq(&recomp, &a));
|
|
|
|
},
|
|
|
|
|
|
|
|
Err(_) => assert!(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_not_spd() {
|
|
|
|
|
|
|
|
let a : Mat3<f64> = Mat3::<f64>::new(1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 1.0, 1.0, 1.0);
|
|
|
|
|
|
|
|
let result = na::cholesky(&a);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => assert!(false),
|
|
|
|
Err(_) => assert!(true),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 21:03:38 +08:00
|
|
|
#[test]
|
|
|
|
fn test_cholesky_not_symmetric() {
|
|
|
|
|
|
|
|
let a : Mat2<f64> = Mat2::<f64>::new(1.0, 1.0, -1.0, 1.0);
|
|
|
|
|
|
|
|
let result = na::cholesky(&a);
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => assert!(false),
|
|
|
|
Err(_) => assert!(true),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 20:44:25 +08:00
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat1() {
|
|
|
|
test_cholesky_impl!(Mat1<f64>);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat2() {
|
|
|
|
test_cholesky_impl!(Mat2<f64>);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat3() {
|
|
|
|
test_cholesky_impl!(Mat3<f64>);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat4() {
|
|
|
|
test_cholesky_impl!(Mat4<f64>);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat5() {
|
|
|
|
test_cholesky_impl!(Mat5<f64>);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat6() {
|
|
|
|
test_cholesky_impl!(Mat6<f64>);
|
|
|
|
}
|