2021-02-25 20:20:20 +08:00
|
|
|
use na::Matrix3;
|
2020-09-25 13:21:13 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn udu_simple() {
|
|
|
|
let m = Matrix3::new(
|
|
|
|
2.0, -1.0, 0.0,
|
|
|
|
-1.0, 2.0, -1.0,
|
|
|
|
0.0, -1.0, 2.0);
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
let udu = m.udu().unwrap();
|
2021-02-25 20:20:20 +08:00
|
|
|
|
2020-09-25 13:21:13 +08:00
|
|
|
// Rebuild
|
2020-09-28 05:28:50 +08:00
|
|
|
let p = udu.u * udu.d_matrix() * udu.u.transpose();
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2020-09-26 09:21:14 +08:00
|
|
|
assert!(relative_eq!(m, p, epsilon = 3.0e-16));
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 06:18:47 +08:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn udu_non_sym_panic() {
|
|
|
|
let m = Matrix3::new(
|
|
|
|
2.0, -1.0, 0.0,
|
|
|
|
1.0, -2.0, 3.0,
|
2021-03-01 00:52:14 +08:00
|
|
|
-2.0, 1.0, 0.3);
|
2020-09-28 06:18:47 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
let udu = m.udu().unwrap();
|
2020-09-28 06:18:47 +08:00
|
|
|
// Rebuild
|
|
|
|
let p = udu.u * udu.d_matrix() * udu.u.transpose();
|
|
|
|
|
|
|
|
assert!(relative_eq!(m, p, epsilon = 3.0e-16));
|
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[cfg(feature = "proptest-support")]
|
|
|
|
mod proptest_tests {
|
2020-09-25 13:21:13 +08:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
use crate::core::helper::{RandComplex, RandScalar};
|
|
|
|
|
|
|
|
macro_rules! gen_tests(
|
2021-03-01 00:52:14 +08:00
|
|
|
($module: ident, $scalar: expr) => {
|
2020-09-25 13:21:13 +08:00
|
|
|
mod $module {
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use crate::core::helper::{RandScalar, RandComplex};
|
2021-03-01 00:52:14 +08:00
|
|
|
use crate::proptest::*;
|
|
|
|
use proptest::{prop_assert, proptest};
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
proptest! {
|
|
|
|
#[test]
|
|
|
|
fn udu(m in dmatrix_($scalar)) {
|
|
|
|
let m = &m * m.adjoint();
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
if let Some(udu) = m.clone().udu() {
|
|
|
|
let p = &udu.u * &udu.d_matrix() * &udu.u.transpose();
|
|
|
|
println!("m: {}, p: {}", m, p);
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
prop_assert!(relative_eq!(m, p, epsilon = 1.0e-7));
|
|
|
|
}
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
#[test]
|
|
|
|
fn udu_static(m in matrix4_($scalar)) {
|
|
|
|
let m = m.hermitian_part();
|
2020-09-25 13:21:13 +08:00
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
if let Some(udu) = m.udu() {
|
|
|
|
let p = udu.u * udu.d_matrix() * udu.u.transpose();
|
|
|
|
prop_assert!(relative_eq!(m, p, epsilon = 1.0e-7));
|
|
|
|
}
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-03-01 00:52:14 +08:00
|
|
|
gen_tests!(f64, PROPTEST_F64);
|
2020-09-25 13:21:13 +08:00
|
|
|
}
|