nalgebra/src/tests/mat.rs

158 lines
2.9 KiB
Rust
Raw Normal View History

use std::num::{Real, abs};
use std::rand::random;
use std::cmp::ApproxEq;
use na::{DMat, DVec};
use na::Indexable; // FIXME: get rid of that
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();
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
assert!((na::inv(&randmat).unwrap() * randmat).approx_eq(&na::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();
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
assert!(na::transpose(&na::transpose(&randmat)) == randmat);
2013-07-23 17:15:20 +08:00
}
);
)
#[test]
2013-08-05 16:13:44 +08:00
fn test_transpose_mat1() {
test_transpose_mat_impl!(na::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() {
test_transpose_mat_impl!(na::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() {
test_transpose_mat_impl!(na::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() {
test_transpose_mat_impl!(na::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() {
test_transpose_mat_impl!(na::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() {
test_transpose_mat_impl!(na::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() {
test_inv_mat_impl!(na::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() {
test_inv_mat_impl!(na::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() {
test_inv_mat_impl!(na::Mat3<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_mat4() {
test_inv_mat_impl!(na::Mat4<f64>);
2013-08-05 16:13:44 +08:00
}
#[test]
2013-08-05 16:13:44 +08:00
fn test_inv_mat5() {
test_inv_mat_impl!(na::Mat5<f64>);
2013-08-05 16:13:44 +08:00
}
#[test]
2013-08-05 16:13:44 +08:00
fn test_inv_mat6() {
test_inv_mat_impl!(na::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() {
do 10000.times {
let randmat: na::Rot2<f64> = na::one();
let ang = na::vec1(abs::<f64>(random()) % Real::pi());
2013-05-19 19:53:19 +08:00
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
assert!(na::rotation(&na::append_rotation(&randmat, &ang)).approx_eq(&ang));
}
2013-05-19 01:04:03 +08:00
}
#[test]
2013-08-05 16:13:44 +08:00
fn test_index_mat2() {
let mat: na::Mat2<f64> = random();
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
assert!(mat.at((0, 1)) == na::transpose(&mat).at((1, 0)));
}
2013-07-23 17:15:20 +08:00
#[test]
2013-08-05 16:13:44 +08:00
fn test_inv_rotation3() {
do 10000.times {
let randmat: na::Rot3<f64> = na::one();
let dir: na::Vec3<f64> = random();
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
let ang = na::normalize(&dir) * (abs::<f64>(random()) % Real::pi());
let rot = na::append_rotation(&randmat, &ang);
Api change: deal with inplace/out of place methods. Before, it was too easy to use an out of place method instead of the inplace one since they name were pretty mutch the same. This kind of confusion may lead to silly bugs very hard to understand. Thus the following changes have been made when a method is available both inplace and out-of-place: * inplace version keep a short name. * out-of-place version are suffixed by `_cpy` (meaning `copy`), and are static methods. Methods applying transformations (rotation, translation or general transform) are now prefixed by `append`, and a `prepend` version is available too. Also, free functions doing in-place modifications dont really make sense. They have been removed. Here are the naming changes: * `invert` -> `inv` * `inverted` -> `Inv::inv_cpy` * `transpose` -> `transpose` * `transposed` -> `Transpose::transpose_cpy` * `transform_by` -> `append_transformation` * `transformed` -> `Transform::append_transformation_cpy` * `rotate_by` -> `apppend_rotation` * `rotated` -> `Rotation::append_rotation_cpy` * `translate_by` -> `apppend_translation` * `translate` -> `Translation::append_translation_cpy` * `normalized` -> `Norm::normalize_cpy` * `rotated_wrt_point` -> `RotationWithTranslation::append_rotation_wrt_point_cpy` * `rotated_wrt_center` -> `RotationWithTranslation::append_rotation_wrt_center_cpy` Note that using those static methods is very verbose, and using in-place methods require an explicit import of the related trait. This is a way to convince the user to use free functions most of the time.
2013-10-14 16:22:32 +08:00
assert!((na::transpose(&rot) * rot).approx_eq(&na::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!(na::mean(&mat).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!(na::cov(&mat).approx_eq(&expected));
2013-07-23 17:15:20 +08:00
}