2017-08-03 01:38:28 +08:00
|
|
|
use std::cmp;
|
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
use na::{DMatrix, DVector, Matrix4x3, Vector4};
|
2018-10-22 13:00:10 +08:00
|
|
|
use nl::Cholesky;
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
use crate::proptest::*;
|
|
|
|
use proptest::{prop_assert, proptest};
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
proptest! {
|
|
|
|
#[test]
|
|
|
|
fn cholesky(m in dmatrix()) {
|
|
|
|
let m = &m * m.transpose();
|
|
|
|
if let Some(chol) = Cholesky::new(m.clone()) {
|
|
|
|
let l = chol.unpack();
|
|
|
|
let reconstructed_m = &l * l.transpose();
|
|
|
|
|
|
|
|
prop_assert!(relative_eq!(reconstructed_m, m, epsilon = 1.0e-7));
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
#[test]
|
|
|
|
fn cholesky_static(m in matrix3()) {
|
2017-08-03 01:38:28 +08:00
|
|
|
let m = &m * m.transpose();
|
|
|
|
if let Some(chol) = Cholesky::new(m) {
|
|
|
|
let l = chol.unpack();
|
|
|
|
let reconstructed_m = &l * l.transpose();
|
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
prop_assert!(relative_eq!(reconstructed_m, m, epsilon = 1.0e-7))
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
#[test]
|
|
|
|
fn cholesky_solve(n in PROPTEST_MATRIX_DIM, nb in PROPTEST_MATRIX_DIM) {
|
|
|
|
let n = cmp::min(n, 15); // To avoid slowing down the test too much.
|
|
|
|
let nb = cmp::min(nb, 15); // To avoid slowing down the test too much.
|
|
|
|
let m = DMatrix::<f64>::new_random(n, n);
|
|
|
|
let m = &m * m.transpose();
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
if let Some(chol) = Cholesky::new(m.clone()) {
|
|
|
|
let b1 = DVector::new_random(n);
|
|
|
|
let b2 = DMatrix::new_random(n, nb);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
let sol1 = chol.solve(&b1).unwrap();
|
|
|
|
let sol2 = chol.solve(&b2).unwrap();
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
prop_assert!(relative_eq!(&m * sol1, b1, epsilon = 1.0e-6));
|
|
|
|
prop_assert!(relative_eq!(&m * sol2, b2, epsilon = 1.0e-6));
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
#[test]
|
|
|
|
fn cholesky_solve_static(m in matrix4()) {
|
2017-08-03 01:38:28 +08:00
|
|
|
let m = &m * m.transpose();
|
2021-03-01 01:39:18 +08:00
|
|
|
if let Some(chol) = Cholesky::new(m) {
|
|
|
|
let b1 = Vector4::new_random();
|
|
|
|
let b2 = Matrix4x3::new_random();
|
|
|
|
|
|
|
|
let sol1 = chol.solve(&b1).unwrap();
|
|
|
|
let sol2 = chol.solve(&b2).unwrap();
|
|
|
|
|
2022-10-31 00:22:08 +08:00
|
|
|
prop_assert!(relative_eq!(m * sol1, b1, epsilon = 1.0e-4));
|
|
|
|
prop_assert!(relative_eq!(m * sol2, b2, epsilon = 1.0e-4));
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
#[test]
|
|
|
|
fn cholesky_inverse(n in PROPTEST_MATRIX_DIM) {
|
|
|
|
let n = cmp::min(n, 15); // To avoid slowing down the test too much.
|
|
|
|
let m = DMatrix::<f64>::new_random(n, n);
|
|
|
|
let m = &m * m.transpose();
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
if let Some(m1) = Cholesky::new(m.clone()).unwrap().inverse() {
|
|
|
|
let id1 = &m * &m1;
|
|
|
|
let id2 = &m1 * &m;
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
prop_assert!(id1.is_identity(1.0e-6) && id2.is_identity(1.0e-6));
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
#[test]
|
|
|
|
fn cholesky_inverse_static(m in matrix4()) {
|
2017-08-03 01:38:28 +08:00
|
|
|
let m = m * m.transpose();
|
2021-03-01 01:39:18 +08:00
|
|
|
if let Some(m1) = Cholesky::new(m.clone()).unwrap().inverse() {
|
|
|
|
let id1 = &m * &m1;
|
|
|
|
let id2 = &m1 * &m;
|
|
|
|
|
2022-10-31 00:22:08 +08:00
|
|
|
prop_assert!(id1.is_identity(1.0e-4) && id2.is_identity(1.0e-4))
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|