2017-08-03 01:38:28 +08:00
|
|
|
use std::cmp;
|
|
|
|
|
|
|
|
use na::{DMatrix, Matrix4};
|
2018-10-22 13:00:10 +08:00
|
|
|
use nl::Eigen;
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
use crate::proptest::*;
|
|
|
|
use proptest::{prop_assert, proptest};
|
|
|
|
|
|
|
|
proptest! {
|
|
|
|
#[test]
|
|
|
|
fn eigensystem(n in PROPTEST_MATRIX_DIM) {
|
|
|
|
let n = cmp::min(n, 25);
|
|
|
|
let m = DMatrix::<f64>::new_random(n, n);
|
|
|
|
|
|
|
|
if let Some(eig) = Eigen::new(m.clone(), true, true) {
|
2022-10-31 00:22:08 +08:00
|
|
|
// TODO: test the complex case too.
|
|
|
|
if eig.eigenvalues_are_real() {
|
|
|
|
let eigvals = DMatrix::from_diagonal(&eig.eigenvalues_re);
|
|
|
|
let transformed_eigvectors = &m * eig.eigenvectors.as_ref().unwrap();
|
|
|
|
let scaled_eigvectors = eig.eigenvectors.as_ref().unwrap() * &eigvals;
|
|
|
|
|
|
|
|
let transformed_left_eigvectors = m.transpose() * eig.left_eigenvectors.as_ref().unwrap();
|
|
|
|
let scaled_left_eigvectors = eig.left_eigenvectors.as_ref().unwrap() * &eigvals;
|
|
|
|
|
|
|
|
prop_assert!(relative_eq!(transformed_eigvectors, scaled_eigvectors, epsilon = 1.0e-5));
|
|
|
|
prop_assert!(relative_eq!(transformed_left_eigvectors, scaled_left_eigvectors, epsilon = 1.0e-5));
|
|
|
|
}
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:39:18 +08:00
|
|
|
#[test]
|
|
|
|
fn eigensystem_static(m in matrix4()) {
|
|
|
|
if let Some(eig) = Eigen::new(m, true, true) {
|
2022-10-31 00:22:08 +08:00
|
|
|
// TODO: test the complex case too.
|
|
|
|
if eig.eigenvalues_are_real() {
|
|
|
|
let eigvals = Matrix4::from_diagonal(&eig.eigenvalues_re);
|
|
|
|
let transformed_eigvectors = m * eig.eigenvectors.unwrap();
|
|
|
|
let scaled_eigvectors = eig.eigenvectors.unwrap() * eigvals;
|
|
|
|
|
|
|
|
let transformed_left_eigvectors = m.transpose() * eig.left_eigenvectors.unwrap();
|
|
|
|
let scaled_left_eigvectors = eig.left_eigenvectors.unwrap() * eigvals;
|
|
|
|
|
|
|
|
prop_assert!(relative_eq!(transformed_eigvectors, scaled_eigvectors, epsilon = 1.0e-5));
|
|
|
|
prop_assert!(relative_eq!(transformed_left_eigvectors, scaled_left_eigvectors, epsilon = 1.0e-5));
|
|
|
|
}
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|