2013-06-29 05:03:40 +08:00
|
|
|
use std::num::{Real, One, abs};
|
2013-06-28 00:16:07 +08:00
|
|
|
use std::rand::random;
|
2013-06-02 02:50:00 +08:00
|
|
|
use std::cmp::ApproxEq;
|
2013-10-06 22:54:09 +08:00
|
|
|
use na::*;
|
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) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
do 10000.times {
|
2013-05-22 07:15:03 +08:00
|
|
|
let randmat : $t = random();
|
|
|
|
|
2013-10-06 22:54:09 +08:00
|
|
|
assert!((randmat.inverted().unwrap() * randmat).approx_eq(&One::one()));
|
2013-05-22 07:15:03 +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) => (
|
2013-08-05 16:13:44 +08:00
|
|
|
do 10000.times {
|
2013-07-23 17:15:20 +08:00
|
|
|
let randmat : $t = random();
|
|
|
|
|
|
|
|
assert!(randmat.transposed().transposed().eq(&randmat));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
)
|
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat1() {
|
|
|
|
test_transpose_mat_impl!(Mat1<f64>);
|
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat2() {
|
|
|
|
test_transpose_mat_impl!(Mat2<f64>);
|
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat3() {
|
|
|
|
test_transpose_mat_impl!(Mat3<f64>);
|
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat4() {
|
|
|
|
test_transpose_mat_impl!(Mat4<f64>);
|
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat5() {
|
|
|
|
test_transpose_mat_impl!(Mat5<f64>);
|
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_transpose_mat6() {
|
|
|
|
test_transpose_mat_impl!(Mat6<f64>);
|
|
|
|
}
|
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() {
|
|
|
|
test_inv_mat_impl!(Mat1<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat2() {
|
|
|
|
test_inv_mat_impl!(Mat2<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat3() {
|
|
|
|
test_inv_mat_impl!(Mat3<f64>);
|
|
|
|
}
|
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() {
|
|
|
|
test_inv_mat_impl!(Mat4<f64>);
|
|
|
|
}
|
2013-07-01 05:19:36 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat5() {
|
|
|
|
test_inv_mat_impl!(Mat5<f64>);
|
|
|
|
}
|
2013-07-01 05:19:36 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_mat6() {
|
|
|
|
test_inv_mat_impl!(Mat6<f64>);
|
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_rotation2() {
|
2013-09-22 20:22:17 +08:00
|
|
|
do 10000.times {
|
2013-10-06 22:54:09 +08:00
|
|
|
let randmat: Rot2<f64> = One::one();
|
2013-09-22 20:22:17 +08:00
|
|
|
let ang = &Vec1::new(abs::<f64>(random()) % Real::pi());
|
2013-05-19 19:53:19 +08:00
|
|
|
|
2013-09-22 20:22:17 +08:00
|
|
|
assert!(randmat.rotated(ang).rotation().approx_eq(ang));
|
|
|
|
}
|
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-06-29 23:19:21 +08:00
|
|
|
let mat: Mat2<f64> = random();
|
|
|
|
|
|
|
|
assert!(mat.at((0, 1)) == mat.transposed().at((1, 0)));
|
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2013-08-05 16:13:44 +08:00
|
|
|
fn test_inv_rotation3() {
|
2013-09-22 20:22:17 +08:00
|
|
|
do 10000.times {
|
2013-10-06 22:54:09 +08:00
|
|
|
let randmat: Rot3<f64> = One::one();
|
2013-09-22 20:22:17 +08:00
|
|
|
let dir: Vec3<f64> = random();
|
|
|
|
let ang = &(dir.normalized() * (abs::<f64>(random()) % Real::pi()));
|
|
|
|
let rot = randmat.rotated(ang);
|
|
|
|
|
|
|
|
assert!((rot.transposed() * rot).approx_eq(&One::one()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mean_dmat() {
|
|
|
|
let mat = DMat::from_vec(
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
[
|
|
|
|
1.0f64, 2.0, 3.0,
|
|
|
|
4.0f64, 5.0, 6.0,
|
|
|
|
7.0f64, 8.0, 9.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(mat.mean().approx_eq(&DVec::from_vec(3, [4.0f64, 5.0, 6.0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cov_dmat() {
|
|
|
|
let mat = DMat::from_vec(
|
|
|
|
5,
|
|
|
|
3,
|
|
|
|
[
|
|
|
|
4.0, 2.0, 0.60,
|
|
|
|
4.2, 2.1, 0.59,
|
|
|
|
3.9, 2.0, 0.58,
|
|
|
|
4.3, 2.1, 0.62,
|
|
|
|
4.1, 2.2, 0.63
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
let expected = DMat::from_vec(
|
|
|
|
3,
|
|
|
|
3,
|
|
|
|
[
|
|
|
|
0.025, 0.0075, 0.00175,
|
|
|
|
0.0075, 0.007, 0.00135,
|
|
|
|
0.00175, 0.00135, 0.00043
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(mat.cov().approx_eq(&expected));
|
2013-07-23 17:15:20 +08:00
|
|
|
}
|