Add non-naive way of calculate generalized eigenvalue, write spotty test for generalized eigenvalues

This commit is contained in:
metric-space 2022-01-19 21:47:44 -05:00 committed by Saurabh
parent ebe6d10a47
commit d7a0e415bd
2 changed files with 25 additions and 8 deletions

View File

@ -176,11 +176,18 @@ where
let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>); let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>);
for i in 0..out.len() { for i in 0..out.len() {
out[i] = Complex::new( let b = self.beta[i].clone();
self.alphar[i].clone() / self.beta[i].clone(), out[i] = {
self.alphai[i].clone() / self.beta[i].clone(), if b < T::RealField::zero() {
Complex::<T>::zero()
} else {
Complex::new(
self.alphar[i].clone() / b.clone(),
self.alphai[i].clone() / b.clone(),
) )
} }
}
}
out out
} }

View File

@ -1,5 +1,6 @@
use na::DMatrix; use na::{zero, DMatrix, Normed};
use nl::QZ; use nl::QZ;
use num_complex::Complex;
use std::cmp; use std::cmp;
use crate::proptest::*; use crate::proptest::*;
@ -12,10 +13,19 @@ proptest! {
let a = DMatrix::<f64>::new_random(n, n); let a = DMatrix::<f64>::new_random(n, n);
let b = DMatrix::<f64>::new_random(n, n); let b = DMatrix::<f64>::new_random(n, n);
let (vsl,s,t,vsr) = QZ::new(a.clone(), b.clone()).unpack(); let qz = QZ::new(a.clone(), b.clone());
let (vsl,s,t,vsr) = qz.clone().unpack();
let eigenvalues = qz.eigenvalues();
let a_c = a.clone().map(|x| Complex::new(x, zero::<f64>()));
prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a.clone(), epsilon = 1.0e-7));
prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b, epsilon = 1.0e-7)) prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b.clone(), epsilon = 1.0e-7));
// spotty test that skips over the first eiegenvalue which in some cases is extremely large relative to the other ones
// and fails the condition
for i in 1..n {
let b_c = b.clone().map(|x| eigenvalues[i]*Complex::new(x,zero::<f64>()));
prop_assert!(relative_eq!((&a_c - &b_c).determinant().norm(), 0.0, epsilon = 1.0e-6));
}
} }
#[test] #[test]