Cleanup of QZ module and added GE's calculation of eigenvalues as a test for QZ's calculation of eigenvalues

This commit is contained in:
metric-space 2022-01-24 23:58:21 -05:00
parent 7d8fb3d384
commit 748848fea7
2 changed files with 24 additions and 24 deletions

View File

@ -13,7 +13,11 @@ use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};
use lapack; use lapack;
/// Generalized eigendecomposition of a pair of N*N square matrices. /// QZ decomposition of a pair of N*N square matrices.
/// Retrieves the left and right matrices of Schur Vectors (VSL and VSR)
/// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the
/// decomposed input matrix `a` equals `VSL * S * VSL.transpose()` and
/// decomposed input matrix `b` equals `VSL * T * VSL.transpose()`.
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serde-serialize", feature = "serde-serialize",
@ -59,6 +63,11 @@ where
{ {
/// Attempts to compute the QZ decomposition of input square matrices `a` and `b`. /// Attempts to compute the QZ decomposition of input square matrices `a` and `b`.
/// ///
/// i.e retrieves the left and right matrices of Schur Vectors (VSL and VSR)
/// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the
/// decomposed matrix `a` equals `VSL * S * VSL.transpose()` and
/// decomposed matrix `b` equals `VSL * T * VSL.transpose()`.
///
/// Panics if the method did not converge. /// Panics if the method did not converge.
pub fn new(a: OMatrix<T, D, D>, b: OMatrix<T, D, D>) -> Self { pub fn new(a: OMatrix<T, D, D>, b: OMatrix<T, D, D>) -> Self {
Self::try_new(a, b).expect("QZ decomposition: convergence failed.") Self::try_new(a, b).expect("QZ decomposition: convergence failed.")
@ -154,8 +163,8 @@ where
/// Retrieves the left and right matrices of Schur Vectors (VSL and VSR) /// Retrieves the left and right matrices of Schur Vectors (VSL and VSR)
/// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the /// the upper-quasitriangular matrix `S` and upper triangular matrix `T` such that the
/// decomposed matrix `A` equals `VSL * S * VSL.transpose()` and /// decomposed input matrix `a` equals `VSL * S * VSL.transpose()` and
/// decomposed matrix `B` equals `VSL * T * VSL.transpose()`. /// decomposed input matrix `b` equals `VSL * T * VSL.transpose()`.
pub fn unpack( pub fn unpack(
self, self,
) -> ( ) -> (
@ -167,8 +176,6 @@ where
(self.vsl, self.s, self.t, self.vsr) (self.vsl, self.s, self.t, self.vsr)
} }
/// computes the generalized eigenvalues /// computes the generalized eigenvalues
#[must_use] #[must_use]
pub fn eigenvalues(&self) -> OVector<Complex<T>, D> pub fn eigenvalues(&self) -> OVector<Complex<T>, D>

View File

@ -1,7 +1,5 @@
use na::{zero, DMatrix, SMatrix}; use na::DMatrix;
use nl::QZ; use nl::{GE, QZ};
use num_complex::Complex;
use simba::scalar::ComplexField;
use std::cmp; use std::cmp;
use crate::proptest::*; use crate::proptest::*;
@ -16,33 +14,28 @@ 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.eigenvalues();
//let a_c = a.clone().map(|x| Complex::new(x, zero::<f64>()));
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));
// spotty test that skips over the first eigenvalue which in some cases is extremely large relative to the other ones prop_assert!(eigenvalues == eigenvalues2);
// 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().modulus(), 0.0, epsilon = 1.0e-6));
//}
} }
#[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.eigenvalues();
//let a_c = a.clone().map(|x| Complex::new(x, zero::<f64>())); 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));
//for i in 0..4 { prop_assert!(eigenvalues == eigenvalues2);
// let b_c = b.clone().map(|x| eigenvalues[i]*Complex::new(x,zero::<f64>()));
// println!("{}",eigenvalues);
// prop_assert!(relative_eq!((&a_c - &b_c).determinant().modulus(), 0.0, epsilon = 1.0e-4))
//}
} }
} }