2018-01-17 23:48:47 +08:00
|
|
|
use na::Matrix3;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
#[test]
|
2020-06-07 15:28:39 +08:00
|
|
|
#[rustfmt::skip]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn lu_simple() {
|
|
|
|
let m = Matrix3::new(
|
|
|
|
2.0, -1.0, 0.0,
|
|
|
|
-1.0, 2.0, -1.0,
|
|
|
|
0.0, -1.0, 2.0);
|
|
|
|
|
2017-08-14 01:52:46 +08:00
|
|
|
let lu = m.lu();
|
2017-08-03 01:37:44 +08:00
|
|
|
assert_eq!(lu.determinant(), 4.0);
|
|
|
|
|
|
|
|
let (p, l, u) = lu.unpack();
|
|
|
|
|
|
|
|
let mut lu = l * u;
|
|
|
|
p.inv_permute_rows(&mut lu);
|
|
|
|
|
|
|
|
assert!(relative_eq!(m, lu, epsilon = 1.0e-7));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-06-07 15:28:39 +08:00
|
|
|
#[rustfmt::skip]
|
2017-08-03 01:37:44 +08:00
|
|
|
fn lu_simple_with_pivot() {
|
|
|
|
let m = Matrix3::new(
|
|
|
|
0.0, -1.0, 2.0,
|
|
|
|
-1.0, 2.0, -1.0,
|
|
|
|
2.0, -1.0, 0.0);
|
|
|
|
|
2017-08-14 01:52:46 +08:00
|
|
|
let lu = m.lu();
|
2017-08-03 01:37:44 +08:00
|
|
|
assert_eq!(lu.determinant(), -4.0);
|
|
|
|
|
|
|
|
let (p, l, u) = lu.unpack();
|
|
|
|
|
|
|
|
let mut lu = l * u;
|
|
|
|
p.inv_permute_rows(&mut lu);
|
|
|
|
|
|
|
|
assert!(relative_eq!(m, lu, epsilon = 1.0e-7));
|
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[cfg(feature = "proptest-support")]
|
|
|
|
mod proptest_tests {
|
2019-03-03 02:33:49 +08:00
|
|
|
macro_rules! gen_tests(
|
2021-03-01 00:52:14 +08:00
|
|
|
($module: ident, $scalar: expr, $scalar_type: ty) => {
|
2019-03-03 02:33:49 +08:00
|
|
|
mod $module {
|
2021-03-01 00:52:14 +08:00
|
|
|
use na::{DMatrix, Matrix4x3, DVector, Vector4};
|
2019-03-03 02:33:49 +08:00
|
|
|
#[allow(unused_imports)]
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::core::helper::{RandScalar, RandComplex};
|
2021-03-01 00:52:14 +08:00
|
|
|
use crate::proptest::*;
|
|
|
|
use proptest::{prop_assert, proptest};
|
2019-03-03 02:33:49 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
proptest! {
|
|
|
|
#[test]
|
|
|
|
fn lu(m in dmatrix_($scalar)) {
|
2019-03-03 02:33:49 +08:00
|
|
|
let lu = m.clone().lu();
|
|
|
|
let (p, l, u) = lu.unpack();
|
|
|
|
let mut lu = l * u;
|
|
|
|
p.inv_permute_rows(&mut lu);
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(m, lu, epsilon = 1.0e-7))
|
2019-03-03 02:33:49 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn lu_static_3_5(m in matrix3x5_($scalar)) {
|
2019-03-03 02:33:49 +08:00
|
|
|
let lu = m.lu();
|
|
|
|
let (p, l, u) = lu.unpack();
|
|
|
|
let mut lu = l * u;
|
|
|
|
p.inv_permute_rows(&mut lu);
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(m, lu, epsilon = 1.0e-7))
|
2019-03-03 02:33:49 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
fn lu_static_5_3(m in matrix5x3_($scalar)) {
|
2019-03-03 02:33:49 +08:00
|
|
|
let lu = m.lu();
|
|
|
|
let (p, l, u) = lu.unpack();
|
|
|
|
let mut lu = l * u;
|
|
|
|
p.inv_permute_rows(&mut lu);
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(m, lu, epsilon = 1.0e-7));
|
2019-03-03 02:33:49 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn lu_static_square(m in matrix4_($scalar)) {
|
2019-03-03 02:33:49 +08:00
|
|
|
let lu = m.lu();
|
|
|
|
let (p, l, u) = lu.unpack();
|
|
|
|
let mut lu = l * u;
|
|
|
|
p.inv_permute_rows(&mut lu);
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(m, lu, epsilon = 1.0e-7));
|
2019-03-03 02:33:49 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn lu_solve(n in PROPTEST_MATRIX_DIM, nb in PROPTEST_MATRIX_DIM) {
|
|
|
|
let m = DMatrix::<$scalar_type>::new_random(n, n).map(|e| e.0);
|
2019-03-03 02:33:49 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
let lu = m.clone().lu();
|
|
|
|
let b1 = DVector::<$scalar_type>::new_random(n).map(|e| e.0);
|
|
|
|
let b2 = DMatrix::<$scalar_type>::new_random(n, nb).map(|e| e.0);
|
2019-03-03 02:33:49 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
let sol1 = lu.solve(&b1);
|
|
|
|
let sol2 = lu.solve(&b2);
|
2019-03-03 02:33:49 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(sol1.is_none() || relative_eq!(&m * sol1.unwrap(), b1, epsilon = 1.0e-6));
|
|
|
|
prop_assert!(sol2.is_none() || relative_eq!(&m * sol2.unwrap(), b2, epsilon = 1.0e-6));
|
2019-03-03 02:33:49 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn lu_solve_static(m in matrix4_($scalar)) {
|
2019-03-03 02:33:49 +08:00
|
|
|
let lu = m.lu();
|
2021-03-01 00:52:14 +08:00
|
|
|
let b1 = Vector4::<$scalar_type>::new_random().map(|e| e.0);
|
|
|
|
let b2 = Matrix4x3::<$scalar_type>::new_random().map(|e| e.0);
|
2019-03-03 02:33:49 +08:00
|
|
|
|
|
|
|
let sol1 = lu.solve(&b1);
|
|
|
|
let sol2 = lu.solve(&b2);
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(sol1.is_none() || relative_eq!(&m * sol1.unwrap(), b1, epsilon = 1.0e-6));
|
|
|
|
prop_assert!(sol2.is_none() || relative_eq!(&m * sol2.unwrap(), b2, epsilon = 1.0e-6));
|
2019-03-03 02:33:49 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
2021-03-01 17:02:45 +08:00
|
|
|
fn lu_inverse(n in PROPTEST_MATRIX_DIM) {
|
|
|
|
let m = DMatrix::<$scalar_type>::new_random(n, n).map(|e| e.0);
|
2019-03-03 02:33:49 +08:00
|
|
|
let mut l = m.lower_triangle();
|
|
|
|
let mut u = m.upper_triangle();
|
|
|
|
|
|
|
|
// Ensure the matrix is well conditioned for inversion.
|
|
|
|
l.fill_diagonal(na::one());
|
|
|
|
u.fill_diagonal(na::one());
|
|
|
|
let m = l * u;
|
|
|
|
|
|
|
|
let m1 = m.clone().lu().try_inverse().unwrap();
|
|
|
|
let id1 = &m * &m1;
|
|
|
|
let id2 = &m1 * &m;
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(id1.is_identity(1.0e-5));
|
|
|
|
prop_assert!(id2.is_identity(1.0e-5));
|
2019-03-03 02:33:49 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn lu_inverse_static(m in matrix4_($scalar)) {
|
2019-03-03 02:33:49 +08:00
|
|
|
let lu = m.lu();
|
|
|
|
|
|
|
|
if let Some(m1) = lu.try_inverse() {
|
|
|
|
let id1 = &m * &m1;
|
|
|
|
let id2 = &m1 * &m;
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(id1.is_identity(1.0e-5));
|
|
|
|
prop_assert!(id2.is_identity(1.0e-5));
|
2019-03-03 02:33:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 23:48:47 +08:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 02:33:49 +08:00
|
|
|
);
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
gen_tests!(complex, complex_f64(), RandComplex<f64>);
|
|
|
|
gen_tests!(f64, PROPTEST_F64, RandScalar<f64>);
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|