New code and modified tests for qz

This commit is contained in:
metric-space 2022-02-03 06:36:41 -05:00 committed by Saurabh
parent 714f2ac987
commit 5828a0a6ad
2 changed files with 57 additions and 41 deletions

View File

@ -42,11 +42,11 @@ where
{ {
alphar: OVector<T, D>, alphar: OVector<T, D>,
alphai: OVector<T, D>, alphai: OVector<T, D>,
beta: OVector<T, D>, beta: OVector<T, D>,
vsl: OMatrix<T, D, D>, vsl: OMatrix<T, D, D>,
s: OMatrix<T, D, D>, s: OMatrix<T, D, D>,
vsr: OMatrix<T, D, D>, vsr: OMatrix<T, D, D>,
t: OMatrix<T, D, D>, t: OMatrix<T, D, D>,
} }
impl<T: Scalar + Copy, D: Dim> Copy for QZ<T, D> impl<T: Scalar + Copy, D: Dim> Copy for QZ<T, D>
@ -176,36 +176,19 @@ where
(self.vsl, self.s, self.t, self.vsr) (self.vsl, self.s, self.t, self.vsr)
} }
/// computes the generalized eigenvalues /// outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alpai), beta)
/// straight from LAPACK
#[must_use] #[must_use]
pub fn eigenvalues(&self) -> OVector<Complex<T>, D> pub fn raw_eigenvalues(&self) -> OVector<(Complex<T>, T), D>
where where
DefaultAllocator: Allocator<Complex<T>, D>, DefaultAllocator: Allocator<(Complex<T>, T), D>,
{ {
let mut out = Matrix::zeros_generic(self.t.shape_generic().0, Const::<1>); let mut out = Matrix::from_element_generic(self.vsl.shape_generic().0, Const::<1>, (Complex::zero(), T::RealField::zero()));
for i in 0..out.len() { for i in 0..out.len() {
out[i] = if self.beta[i].clone().abs() < T::RealField::default_epsilon() { out[i] = (Complex::new(self.alphar[i].clone(),
Complex::zero() self.alphai[i].clone()),
} else { self.beta[i].clone())
let mut cr = self.alphar[i].clone();
let mut ci = self.alphai[i].clone();
let b = self.beta[i].clone();
if cr.clone().abs() < T::RealField::default_epsilon() {
cr = T::RealField::zero()
} else {
cr = cr / b.clone()
};
if ci.clone().abs() < T::RealField::default_epsilon() {
ci = T::RealField::zero()
} else {
ci = ci / b
};
Complex::new(cr, ci)
}
} }
out out

View File

@ -1,5 +1,7 @@
use na::DMatrix; use na::{DMatrix, EuclideanNorm, Norm};
use nl::{GE, QZ}; use nl::QZ;
use num_complex::Complex;
use simba::scalar::ComplexField;
use std::cmp; use std::cmp;
use crate::proptest::*; use crate::proptest::*;
@ -14,28 +16,59 @@ proptest! {
let qz = QZ::new(a.clone(), b.clone()); let qz = QZ::new(a.clone(), b.clone());
let (vsl,s,t,vsr) = qz.clone().unpack(); let (vsl,s,t,vsr) = qz.clone().unpack();
let eigenvalues = qz.eigenvalues(); let eigenvalues = qz.raw_eigenvalues();
let ge = GE::new(a.clone(), b.clone());
let eigenvalues2 = ge.eigenvalues();
prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a.clone(), 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.clone(), epsilon = 1.0e-7)); prop_assert!(relative_eq!(vsl * t * vsr.transpose(), b.clone(), epsilon = 1.0e-7));
prop_assert!(eigenvalues == eigenvalues2);
let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a)));
let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b)));
if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 {
let a_c = a.clone().map(|x| Complex::new(x, 0.0));
let b_c = b.clone().map(|x| Complex::new(x, 0.0));
for (alpha,beta) in eigenvalues.iter() {
let l_a = a_c.clone() * Complex::new(*beta, 0.0);
let l_b = b_c.clone() * *alpha;
prop_assert!(
relative_eq!(
(&l_a - &l_b).determinant().modulus(),
0.0,
epsilon = 1.0e-7));
};
};
} }
#[test] #[test]
fn qz_static(a in matrix4(), b in matrix4()) { fn qz_static(a in matrix4(), b in matrix4()) {
let qz = QZ::new(a.clone(), b.clone()); let qz = QZ::new(a.clone(), b.clone());
let ge = GE::new(a.clone(), b.clone());
let (vsl,s,t,vsr) = qz.unpack(); let (vsl,s,t,vsr) = qz.unpack();
let eigenvalues = qz.eigenvalues(); let eigenvalues = qz.raw_eigenvalues();
let eigenvalues2 = ge.eigenvalues();
prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, epsilon = 1.0e-7)); prop_assert!(relative_eq!(&vsl * s * vsr.transpose(), a, 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, epsilon = 1.0e-7));
prop_assert!(eigenvalues == eigenvalues2); let a_condition_no = a.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&a)));
let b_condition_no = b.clone().try_inverse().and_then(|x| Some(EuclideanNorm.norm(&x)* EuclideanNorm.norm(&b)));
if a_condition_no.unwrap_or(200000.0) < 5.0 && b_condition_no.unwrap_or(200000.0) < 5.0 {
let a_c =a.clone().map(|x| Complex::new(x, 0.0));
let b_c = b.clone().map(|x| Complex::new(x, 0.0));
for (alpha,beta) in eigenvalues.iter() {
let l_a = a_c.clone() * Complex::new(*beta, 0.0);
let l_b = b_c.clone() * *alpha;
prop_assert!(
relative_eq!(
(&l_a - &l_b).determinant().modulus(),
0.0,
epsilon = 1.0e-7));
}
};
} }
} }