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;
|
2016-04-17 23:26:58 +08:00
|
|
|
use na::{Rotation2, Rotation3, Isometry2, Isometry3, Similarity2, Similarity3, Vector3, Matrix1, Matrix2, Matrix3, Matrix4, Matrix5, Matrix6, DMatrix, DVector,
|
|
|
|
Row, Column, Diagonal, Transpose, RowSlice, ColumnSlice, Shape};
|
2013-05-19 01:04:03 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
macro_rules! test_inverse_mat_impl(
|
2013-06-09 22:04:54 +08:00
|
|
|
($t: ty) => (
|
2016-01-10 21:49:37 +08:00
|
|
|
for _ in 0usize .. 10000 {
|
2016-04-17 23:26:58 +08:00
|
|
|
let randmatrix : $t = random();
|
2013-05-22 07:15:03 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
match na::inverse(&randmatrix) {
|
2014-09-20 05:53:36 +08:00
|
|
|
None => { },
|
2016-03-28 20:56:25 +08:00
|
|
|
Some(i) => {
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq(&(i * randmatrix), &na::one()))
|
2016-03-28 20:56:25 +08:00
|
|
|
}
|
2014-09-20 05:53:36 +08:00
|
|
|
}
|
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) => (
|
2016-01-10 21:49:37 +08:00
|
|
|
for _ in 0usize .. 10000 {
|
2016-04-17 23:26:58 +08:00
|
|
|
let randmatrix : $t = random();
|
2013-07-23 17:15:20 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::transpose(&na::transpose(&randmatrix)) == randmatrix);
|
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) => (
|
2016-01-10 21:49:37 +08:00
|
|
|
for _ in 0usize .. 10000 {
|
2016-04-17 23:26:58 +08:00
|
|
|
let randmatrix : $t = random();
|
2014-05-12 20:06:25 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let (q, r) = na::qr(&randmatrix);
|
2014-05-12 20:06:25 +08:00
|
|
|
let recomp = q * r;
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq(&randmatrix, &recomp));
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
);
|
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) => (
|
2016-01-10 21:49:37 +08:00
|
|
|
for _ in 0usize .. 10000 {
|
2015-08-07 20:44:25 +08:00
|
|
|
|
|
|
|
// construct symmetric positive definite matrix
|
2016-04-17 23:26:58 +08:00
|
|
|
let mut randmatrix : $t = random();
|
2016-04-18 01:47:56 +08:00
|
|
|
let mut diagmatrix : $t = Diagonal::from_diagonal(&na::diagonal(&randmatrix));
|
2015-08-07 20:44:25 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
diagmatrix = na::abs(&diagmatrix) + 1.0;
|
|
|
|
randmatrix = randmatrix * diagmatrix * na::transpose(&randmatrix);
|
2015-08-07 20:44:25 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let result = na::cholesky(&randmatrix);
|
2015-08-07 20:44:25 +08:00
|
|
|
|
2015-08-08 23:22:47 +08:00
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
let v = result.unwrap();
|
|
|
|
let recomp = v * na::transpose(&v);
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq(&randmatrix, &recomp));
|
2015-08-07 20:44:25 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
2015-09-22 06:19:45 +08:00
|
|
|
macro_rules! test_hessenberg_impl(
|
|
|
|
($t: ty) => (
|
2016-01-10 21:49:37 +08:00
|
|
|
for _ in 0usize .. 10000 {
|
2015-09-22 06:19:45 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let randmatrix : $t = random();
|
2015-09-22 06:19:45 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let (q, h) = na::hessenberg(&randmatrix);
|
2015-09-22 06:19:45 +08:00
|
|
|
let recomp = q * h * na::transpose(&q);
|
|
|
|
|
|
|
|
let (rows, cols) = h.shape();
|
|
|
|
|
|
|
|
// Check if `h` has zero entries below the first subdiagonal
|
|
|
|
if cols > 2 {
|
|
|
|
for j in 0..(cols-2) {
|
|
|
|
for i in (j+2)..rows {
|
|
|
|
assert!(na::approx_eq(&h[(i,j)], &0.0f64));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq(&randmatrix, &recomp));
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
macro_rules! test_eigen_qr_impl(
|
|
|
|
($t: ty) => {
|
2016-01-10 21:49:37 +08:00
|
|
|
for _ in 0usize .. 10000 {
|
2016-04-17 23:26:58 +08:00
|
|
|
let randmatrix : $t = random();
|
2015-09-22 06:19:45 +08:00
|
|
|
// Make it symetric so that we can recompose the matrix to test at the end.
|
2016-04-17 23:26:58 +08:00
|
|
|
let randmatrix = na::transpose(&randmatrix) * randmatrix;
|
|
|
|
let (eigenvectors, eigenvalues) = na::eigen_qr(&randmatrix, &1e-13, 100);
|
2015-09-22 06:19:45 +08:00
|
|
|
|
2016-04-18 01:47:56 +08:00
|
|
|
let diagonal: $t = Diagonal::from_diagonal(&eigenvalues);
|
2016-04-17 23:26:58 +08:00
|
|
|
let recomp = eigenvectors * diagonal * na::transpose(&eigenvectors);
|
2015-09-22 06:19:45 +08:00
|
|
|
println!("eigenvalues: {:?}", eigenvalues);
|
2016-04-17 23:26:58 +08:00
|
|
|
println!(" matrix: {:?}", randmatrix);
|
2015-09-22 06:19:45 +08:00
|
|
|
println!("recomp: {:?}", recomp);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq_eps(&randmatrix, &recomp, &1.0e-2));
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
2015-11-14 22:40:35 +08:00
|
|
|
|
2016-01-10 21:49:37 +08:00
|
|
|
for _ in 0usize .. 10000 {
|
2016-04-17 23:26:58 +08:00
|
|
|
let randmatrix : $t = random();
|
2015-11-14 22:40:35 +08:00
|
|
|
// Take only diagonal part
|
2016-04-18 01:47:56 +08:00
|
|
|
let randmatrix: $t = Diagonal::from_diagonal(&randmatrix.diagonal());
|
2016-04-17 23:26:58 +08:00
|
|
|
let (eigenvectors, eigenvalues) = na::eigen_qr(&randmatrix, &1e-13, 100);
|
2015-11-14 22:40:35 +08:00
|
|
|
|
2016-04-18 01:47:56 +08:00
|
|
|
let diagonal: $t = Diagonal::from_diagonal(&eigenvalues);
|
2016-04-17 23:26:58 +08:00
|
|
|
let recomp = eigenvectors * diagonal * na::transpose(&eigenvectors);
|
2015-11-14 22:40:35 +08:00
|
|
|
println!("eigenvalues: {:?}", eigenvalues);
|
2016-04-17 23:26:58 +08:00
|
|
|
println!(" matrix: {:?}", randmatrix);
|
2015-11-14 22:40:35 +08:00
|
|
|
println!("recomp: {:?}", recomp);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq_eps(&randmatrix, &recomp, &1.0e-2));
|
2015-11-14 22:40:35 +08:00
|
|
|
}
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
);
|
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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_transpose_mat_impl!(Matrix1<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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_transpose_mat_impl!(Matrix2<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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_transpose_mat_impl!(Matrix3<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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_transpose_mat_impl!(Matrix4<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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_transpose_mat_impl!(Matrix5<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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_transpose_mat_impl!(Matrix6<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]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_mat1() {
|
|
|
|
test_inverse_mat_impl!(Matrix1<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_mat2() {
|
|
|
|
test_inverse_mat_impl!(Matrix2<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_mat3() {
|
|
|
|
test_inverse_mat_impl!(Matrix3<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]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_mat4() {
|
|
|
|
test_inverse_mat_impl!(Matrix4<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-01 05:19:36 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_mat5() {
|
|
|
|
test_inverse_mat_impl!(Matrix5<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-07-01 05:19:36 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_mat6() {
|
|
|
|
test_inverse_mat_impl!(Matrix6<f64>);
|
2013-08-05 16:13:44 +08:00
|
|
|
}
|
2013-05-19 01:04:03 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_rot2() {
|
|
|
|
test_inverse_mat_impl!(Rotation2<f64>);
|
2013-05-19 01:04:03 +08:00
|
|
|
}
|
2013-06-29 23:19:21 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_rot3() {
|
|
|
|
test_inverse_mat_impl!(Rotation3<f64>);
|
2013-06-29 23:19:21 +08:00
|
|
|
}
|
2013-07-23 17:15:20 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_iso2() {
|
|
|
|
test_inverse_mat_impl!(Isometry2<f64>);
|
2013-09-22 20:22:17 +08:00
|
|
|
}
|
|
|
|
|
2015-06-02 04:37:54 +08:00
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_iso3() {
|
|
|
|
test_inverse_mat_impl!(Isometry3<f64>);
|
2015-06-02 04:37:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_sim2() {
|
|
|
|
test_inverse_mat_impl!(Similarity2<f64>);
|
2015-06-02 04:37:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_inverse_sim3() {
|
|
|
|
test_inverse_mat_impl!(Similarity3<f64>);
|
2015-06-02 04:37:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-03-28 20:56:25 +08:00
|
|
|
fn test_index_mat2() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let matrix: Matrix2<f64> = random();
|
2015-06-02 04:37:54 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(matrix[(0, 1)] == na::transpose(&matrix)[(1, 0)]);
|
2015-06-02 04:37:54 +08:00
|
|
|
}
|
|
|
|
|
2016-03-28 20:56:25 +08:00
|
|
|
|
2013-09-22 20:22:17 +08:00
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_mean_dmatrix() {
|
|
|
|
let matrix = DMatrix::from_row_vector(
|
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,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq(&na::mean(&matrix), &DVector::from_slice(3, &[4.0f64, 5.0, 6.0])));
|
2013-09-22 20:22:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_covariance_dmatrix() {
|
|
|
|
let matrix = DMatrix::from_row_vector(
|
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
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let expected = DMatrix::from_row_vector(
|
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
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq(&na::covariance(&matrix), &expected));
|
2013-07-23 17:15:20 +08:00
|
|
|
}
|
2013-10-18 04:40:44 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_transpose_dmatrix() {
|
|
|
|
let matrix = DMatrix::from_row_vector(
|
2013-10-18 04:40:44 +08:00
|
|
|
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
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::transpose(&na::transpose(&matrix)) == matrix);
|
2013-10-18 04:40:44 +08:00
|
|
|
}
|
|
|
|
|
2016-01-10 21:49:55 +08:00
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_row_dmatrix() {
|
|
|
|
let matrix = DMatrix::from_row_vector(
|
2016-01-10 21:49:55 +08:00
|
|
|
8,
|
|
|
|
4,
|
|
|
|
&[
|
|
|
|
1u32,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
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert_eq!(&DVector::from_slice(4, &[1u32, 2, 3, 4]), &matrix.row(0));
|
|
|
|
assert_eq!(&DVector::from_slice(4, &[5u32, 6, 7, 8]), &matrix.row(1));
|
|
|
|
assert_eq!(&DVector::from_slice(4, &[9u32, 10, 11, 12]), &matrix.row(2));
|
|
|
|
assert_eq!(&DVector::from_slice(4, &[13u32, 14, 15, 16]), &matrix.row(3));
|
|
|
|
assert_eq!(&DVector::from_slice(4, &[17u32, 18, 19, 20]), &matrix.row(4));
|
|
|
|
assert_eq!(&DVector::from_slice(4, &[21u32, 22, 23, 24]), &matrix.row(5));
|
|
|
|
assert_eq!(&DVector::from_slice(4, &[25u32, 26, 27, 28]), &matrix.row(6));
|
|
|
|
assert_eq!(&DVector::from_slice(4, &[29u32, 30, 31, 32]), &matrix.row(7));
|
2016-01-10 21:49:55 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 22:53:20 +08:00
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_row_slice_dmatrix() {
|
|
|
|
let matrix = DMatrix::from_row_vector(
|
2015-08-27 22:53:20 +08:00
|
|
|
5,
|
|
|
|
4,
|
|
|
|
&[
|
|
|
|
1u32,2, 3, 4,
|
|
|
|
5, 6, 7, 8,
|
|
|
|
9, 10, 11, 12,
|
|
|
|
13, 14, 15, 16,
|
|
|
|
17, 18, 19, 20,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert_eq!(&DVector::from_slice(4, &[1u32, 2, 3, 4]), &matrix.row_slice(0, 0, 4));
|
|
|
|
assert_eq!(&DVector::from_slice(2, &[1u32, 2]), &matrix.row_slice(0, 0, 2));
|
|
|
|
assert_eq!(&DVector::from_slice(2, &[10u32, 11]), &matrix.row_slice(2, 1, 3));
|
|
|
|
assert_eq!(&DVector::from_slice(2, &[19u32, 20]), &matrix.row_slice(4, 2, 4));
|
2015-08-27 22:53:20 +08:00
|
|
|
}
|
|
|
|
|
2016-01-10 21:49:55 +08:00
|
|
|
#[test]
|
2016-04-18 14:29:22 +08:00
|
|
|
fn test_column_dmatrix() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let matrix = DMatrix::from_row_vector(
|
2016-01-10 21:49:55 +08:00
|
|
|
8,
|
|
|
|
4,
|
|
|
|
&[
|
|
|
|
1u32,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
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert_eq!(&DVector::from_slice(8, &[1u32, 5, 9, 13, 17, 21, 25, 29]), &matrix.column(0));
|
|
|
|
assert_eq!(&DVector::from_slice(8, &[2u32, 6, 10, 14, 18, 22, 26, 30]), &matrix.column(1));
|
|
|
|
assert_eq!(&DVector::from_slice(8, &[3u32, 7, 11, 15, 19, 23, 27, 31]), &matrix.column(2));
|
|
|
|
assert_eq!(&DVector::from_slice(8, &[4u32, 8, 12, 16, 20, 24, 28, 32]), &matrix.column(3));
|
2016-01-10 21:49:55 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 22:53:20 +08:00
|
|
|
#[test]
|
2016-04-18 14:29:22 +08:00
|
|
|
fn test_column_slice_dmatrix() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let matrix = DMatrix::from_row_vector(
|
2015-08-27 22:53:20 +08:00
|
|
|
8,
|
|
|
|
4,
|
|
|
|
&[
|
|
|
|
1u32,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
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-18 14:29:22 +08:00
|
|
|
assert_eq!(&DVector::from_slice(8, &[1u32, 5, 9, 13, 17, 21, 25, 29]), &matrix.column_slice(0, 0, 8));
|
|
|
|
assert_eq!(&DVector::from_slice(3, &[1u32, 5, 9]), &matrix.column_slice(0, 0, 3));
|
|
|
|
assert_eq!(&DVector::from_slice(5, &[11u32, 15, 19, 23, 27]), &matrix.column_slice(2, 2, 7));
|
|
|
|
assert_eq!(&DVector::from_slice(2, &[28u32, 32]), &matrix.column_slice(3, 6, 8));
|
2015-08-27 22:53:20 +08:00
|
|
|
}
|
|
|
|
|
2013-10-18 04:40:44 +08:00
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_dmat_from_vector() {
|
|
|
|
let mat1 = DMatrix::from_row_vector(
|
2013-10-18 04:40:44 +08:00
|
|
|
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
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-18 14:29:22 +08:00
|
|
|
let mat2 = DMatrix::from_column_vector(
|
2013-10-18 04:40:44 +08:00
|
|
|
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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let mat1 = DMatrix::from_row_vector(
|
2015-06-06 18:53:40 +08:00
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
1.0, 2.0,
|
|
|
|
3.0, 4.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let mat2 = DMatrix::from_row_vector(
|
2015-06-06 18:53:40 +08:00
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
10.0, 20.0,
|
|
|
|
30.0, 40.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let res = DMatrix::from_row_vector(
|
2015-06-06 18:53:40 +08:00
|
|
|
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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let mat1 = DMatrix::from_row_vector(
|
2015-06-20 22:20:39 +08:00
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
1.0, 2.0,
|
|
|
|
3.0, 4.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let mat2 = DMatrix::from_row_vector(
|
2015-06-20 22:20:39 +08:00
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
10.0, 20.0,
|
|
|
|
30.0, 40.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let res = DMatrix::from_row_vector(
|
2015-06-20 22:20:39 +08:00
|
|
|
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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let mat1 = DMatrix::from_row_vector(
|
2015-07-12 14:42:31 +08:00
|
|
|
1,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
1.0, 2.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let mat2 = DMatrix::from_row_vector(
|
2015-07-12 14:42:31 +08:00
|
|
|
2,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
3.0, 4.0, 5.0,
|
|
|
|
6.0, 7.0, 8.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let res = DMatrix::from_row_vector(
|
2015-07-12 14:42:31 +08:00
|
|
|
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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let mat1 = DMatrix::from_row_vector(
|
2015-06-06 18:53:40 +08:00
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
1.0, 2.0,
|
|
|
|
3.0, 4.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let mat2 = DMatrix::from_row_vector(
|
2015-06-06 18:53:40 +08:00
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
10.0, 20.0,
|
|
|
|
30.0, 40.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let res = DMatrix::from_row_vector(
|
2015-06-06 18:53:40 +08:00
|
|
|
2,
|
|
|
|
2,
|
|
|
|
&[
|
|
|
|
-09.0, -18.0,
|
|
|
|
-27.0, -36.0
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!((mat1 - mat2) == res);
|
|
|
|
}
|
|
|
|
|
2016-01-10 21:03:04 +08:00
|
|
|
#[test]
|
2016-04-18 14:29:22 +08:00
|
|
|
fn test_dmat_column() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let matrix = DMatrix::from_row_vector(
|
2016-01-10 21:03:04 +08:00
|
|
|
3,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
1.0, 2.0, 3.0,
|
|
|
|
4.0, 5.0, 6.0,
|
|
|
|
7.0, 8.0, 9.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(matrix.column(1) == DVector::from_slice(3, &[2.0, 5.0, 8.0]));
|
2016-01-10 21:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-04-18 14:29:22 +08:00
|
|
|
fn test_dmat_set_column() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let mut matrix = DMatrix::from_row_vector(
|
2016-01-10 21:03:04 +08:00
|
|
|
3,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
1.0, 2.0, 3.0,
|
|
|
|
4.0, 5.0, 6.0,
|
|
|
|
7.0, 8.0, 9.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-18 14:29:22 +08:00
|
|
|
matrix.set_column(1, DVector::from_slice(3, &[12.0, 15.0, 18.0]));
|
2016-01-10 21:03:04 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let expected = DMatrix::from_row_vector(
|
2016-01-10 21:03:04 +08:00
|
|
|
3,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
1.0, 12.0, 3.0,
|
|
|
|
4.0, 15.0, 6.0,
|
|
|
|
7.0, 18.0, 9.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(matrix == expected);
|
2016-01-10 21:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dmat_row() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let matrix = DMatrix::from_row_vector(
|
2016-01-10 21:03:04 +08:00
|
|
|
3,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
1.0, 2.0, 3.0,
|
|
|
|
4.0, 5.0, 6.0,
|
|
|
|
7.0, 8.0, 9.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(matrix.row(1) == DVector::from_slice(3, &[4.0, 5.0, 6.0]));
|
2016-01-10 21:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dmat_set_row() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let mut matrix = DMatrix::from_row_vector(
|
2016-01-10 21:03:04 +08:00
|
|
|
3,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
1.0, 2.0, 3.0,
|
|
|
|
4.0, 5.0, 6.0,
|
|
|
|
7.0, 8.0, 9.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
matrix.set_row(1, DVector::from_slice(3, &[14.0, 15.0, 16.0]));
|
2016-01-10 21:03:04 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let expected = DMatrix::from_row_vector(
|
2016-01-10 21:03:04 +08:00
|
|
|
3,
|
|
|
|
3,
|
|
|
|
&[
|
|
|
|
1.0, 2.0, 3.0,
|
|
|
|
14.0, 15.0, 16.0,
|
|
|
|
7.0, 8.0, 9.0,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(matrix == expected);
|
2016-01-10 21:03:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
/* FIXME: review qr decomposition to make it work with DMatrix.
|
2014-05-10 00:59:26 +08:00
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr() {
|
2016-01-10 21:49:37 +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));
|
2016-04-17 23:26:58 +08:00
|
|
|
let randmatrix: DMatrix<f64> = DMatrix::new_random(rows, cols);
|
|
|
|
let (q, r) = na::qr(&randmatrix);
|
2014-05-12 20:06:25 +08:00
|
|
|
let recomp = q * r;
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(na::approx_eq(&randmatrix, &recomp));
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
}
|
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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_qr_impl!(Matrix1<f64>);
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat2() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_qr_impl!(Matrix2<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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_qr_impl!(Matrix3<f64>);
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat4() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_qr_impl!(Matrix4<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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_qr_impl!(Matrix5<f64>);
|
2014-05-12 20:06:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn test_qr_mat6() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_qr_impl!(Matrix6<f64>);
|
2014-05-10 00:59:26 +08:00
|
|
|
}
|
2014-07-27 08:03:37 +08:00
|
|
|
|
2015-09-22 06:19:45 +08:00
|
|
|
#[test]
|
|
|
|
fn test_eigen_qr_mat1() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_eigen_qr_impl!(Matrix1<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eigen_qr_mat2() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_eigen_qr_impl!(Matrix2<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eigen_qr_mat3() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_eigen_qr_impl!(Matrix3<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eigen_qr_mat4() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_eigen_qr_impl!(Matrix4<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eigen_qr_mat5() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_eigen_qr_impl!(Matrix5<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eigen_qr_mat6() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_eigen_qr_impl!(Matrix6<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
2014-07-27 08:03:37 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_fn() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let actual: DMatrix<usize> = DMatrix::from_fn(3, 4, |i, j| 10 * i + j);
|
|
|
|
let expected: DMatrix<usize> = DMatrix::from_row_vector(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() {
|
2016-04-17 23:26:58 +08:00
|
|
|
let matrix = Matrix3::new(0.0f32, 1.0, 2.0,
|
2014-09-20 05:51:27 +08:00
|
|
|
3.0, 4.0, 5.0,
|
|
|
|
6.0, 7.0, 8.0);
|
2016-04-17 23:26:58 +08:00
|
|
|
let second_row = matrix.row(1);
|
|
|
|
let second_col = matrix.column(1);
|
2014-09-20 05:51:27 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert!(second_row == Vector3::new(3.0, 4.0, 5.0));
|
|
|
|
assert!(second_col == Vector3::new(1.0, 4.0, 7.0));
|
2014-09-20 05:51:27 +08:00
|
|
|
}
|
2014-10-13 02:21:06 +08:00
|
|
|
|
2015-08-07 20:44:25 +08:00
|
|
|
#[test]
|
|
|
|
fn test_cholesky_const() {
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let a : Matrix3<f64> = Matrix3::<f64>::new(1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 3.0);
|
|
|
|
let g : Matrix3<f64> = Matrix3::<f64>::new(1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0);
|
2015-08-07 20:44:25 +08:00
|
|
|
|
|
|
|
let result = na::cholesky(&a);
|
|
|
|
|
2015-08-08 20:52:57 +08:00
|
|
|
assert!(result.is_ok());
|
2015-08-07 20:44:25 +08:00
|
|
|
|
2015-08-08 20:52:57 +08:00
|
|
|
let v = result.unwrap();
|
|
|
|
assert!(na::approx_eq(&v, &g));
|
2015-08-07 20:44:25 +08:00
|
|
|
|
2015-08-08 20:52:57 +08:00
|
|
|
let recomp = v * na::transpose(&v);
|
|
|
|
assert!(na::approx_eq(&recomp, &a));
|
2015-08-07 20:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_not_spd() {
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let a : Matrix3<f64> = Matrix3::<f64>::new(1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 1.0, 1.0, 1.0);
|
2015-08-07 20:44:25 +08:00
|
|
|
|
|
|
|
let result = na::cholesky(&a);
|
|
|
|
|
2015-08-08 20:52:57 +08:00
|
|
|
assert!(result.is_err());
|
2015-08-07 20:44:25 +08:00
|
|
|
}
|
|
|
|
|
2015-08-07 21:03:38 +08:00
|
|
|
#[test]
|
|
|
|
fn test_cholesky_not_symmetric() {
|
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
let a : Matrix2<f64> = Matrix2::<f64>::new(1.0, 1.0, -1.0, 1.0);
|
2015-08-07 21:03:38 +08:00
|
|
|
|
|
|
|
let result = na::cholesky(&a);
|
|
|
|
|
2015-08-08 20:52:57 +08:00
|
|
|
assert!(result.is_err());
|
2015-08-07 21:03:38 +08:00
|
|
|
}
|
|
|
|
|
2015-08-07 20:44:25 +08:00
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat1() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_cholesky_impl!(Matrix1<f64>);
|
2015-08-07 20:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat2() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_cholesky_impl!(Matrix2<f64>);
|
2015-08-07 20:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat3() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_cholesky_impl!(Matrix3<f64>);
|
2015-08-07 20:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat4() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_cholesky_impl!(Matrix4<f64>);
|
2015-08-07 20:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat5() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_cholesky_impl!(Matrix5<f64>);
|
2015-08-07 20:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cholesky_mat6() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_cholesky_impl!(Matrix6<f64>);
|
2015-08-13 07:52:55 +08:00
|
|
|
}
|
|
|
|
|
2015-09-22 06:19:45 +08:00
|
|
|
#[test]
|
|
|
|
fn test_hessenberg_mat1() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_hessenberg_impl!(Matrix1<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hessenberg_mat2() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_hessenberg_impl!(Matrix2<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hessenberg_mat3() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_hessenberg_impl!(Matrix3<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hessenberg_mat4() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_hessenberg_impl!(Matrix4<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hessenberg_mat5() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_hessenberg_impl!(Matrix5<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hessenberg_mat6() {
|
2016-04-17 23:26:58 +08:00
|
|
|
test_hessenberg_impl!(Matrix6<f64>);
|
2015-09-22 06:19:45 +08:00
|
|
|
}
|
|
|
|
|
2015-08-13 07:52:55 +08:00
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_transpose_square_matrix() {
|
2016-04-18 14:29:22 +08:00
|
|
|
let column_major_matrix = &[0, 1, 2, 3,
|
2015-08-13 07:52:55 +08:00
|
|
|
0, 1, 2, 3,
|
|
|
|
0, 1, 2, 3,
|
|
|
|
0, 1, 2, 3];
|
|
|
|
let num_rows = 4;
|
|
|
|
let num_cols = 4;
|
2016-04-18 14:29:22 +08:00
|
|
|
let mut matrix = DMatrix::from_column_vector(num_rows, num_cols, column_major_matrix);
|
2016-04-17 23:26:58 +08:00
|
|
|
matrix.transpose_mut();
|
2015-08-13 07:52:55 +08:00
|
|
|
for i in 0..num_rows {
|
2016-04-17 23:26:58 +08:00
|
|
|
assert_eq!(&[0, 1, 2, 3], &matrix.row_slice(i, 0, num_cols)[..]);
|
2015-08-13 07:52:55 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-17 19:20:58 +08:00
|
|
|
|
|
|
|
#[test]
|
2016-04-17 23:26:58 +08:00
|
|
|
fn test_outer_dvector() {
|
|
|
|
let vector = DVector::from_slice(5, &[ 1.0, 2.0, 3.0, 4.0, 5.0 ]);
|
|
|
|
let row = DMatrix::from_row_vector(1, 5, &vector[..]);
|
2016-04-17 19:20:58 +08:00
|
|
|
|
2016-04-17 23:26:58 +08:00
|
|
|
assert_eq!(row.transpose() * row, na::outer(&vector, &vector))
|
2016-04-17 19:20:58 +08:00
|
|
|
}
|