Merge pull request #1067 from metric-space/qz-decomposition-lapack
QZ-decomposition
This commit is contained in:
commit
a850592f7b
|
@ -0,0 +1,350 @@
|
||||||
|
#[cfg(feature = "serde-serialize")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use num::Zero;
|
||||||
|
use num_complex::Complex;
|
||||||
|
|
||||||
|
use simba::scalar::RealField;
|
||||||
|
|
||||||
|
use crate::ComplexHelper;
|
||||||
|
use na::allocator::Allocator;
|
||||||
|
use na::dimension::{Const, Dim};
|
||||||
|
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};
|
||||||
|
|
||||||
|
use lapack;
|
||||||
|
|
||||||
|
/// Generalized eigenvalues and generalized eigenvectors (left and right) of a pair of N*N real square matrices.
|
||||||
|
///
|
||||||
|
/// Each generalized eigenvalue (lambda) satisfies determinant(A - lambda*B) = 0
|
||||||
|
///
|
||||||
|
/// The right eigenvector v(j) corresponding to the eigenvalue lambda(j)
|
||||||
|
/// of (A,B) satisfies
|
||||||
|
///
|
||||||
|
/// A * v(j) = lambda(j) * B * v(j).
|
||||||
|
///
|
||||||
|
/// The left eigenvector u(j) corresponding to the eigenvalue lambda(j)
|
||||||
|
/// of (A,B) satisfies
|
||||||
|
///
|
||||||
|
/// u(j)**H * A = lambda(j) * u(j)**H * B .
|
||||||
|
/// where u(j)**H is the conjugate-transpose of u(j).
|
||||||
|
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serde-serialize",
|
||||||
|
serde(
|
||||||
|
bound(serialize = "DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
||||||
|
OVector<T, D>: Serialize,
|
||||||
|
OMatrix<T, D, D>: Serialize")
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serde-serialize",
|
||||||
|
serde(
|
||||||
|
bound(deserialize = "DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
||||||
|
OVector<T, D>: Deserialize<'de>,
|
||||||
|
OMatrix<T, D, D>: Deserialize<'de>")
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct GeneralizedEigen<T: Scalar, D: Dim>
|
||||||
|
where
|
||||||
|
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
|
||||||
|
{
|
||||||
|
alphar: OVector<T, D>,
|
||||||
|
alphai: OVector<T, D>,
|
||||||
|
beta: OVector<T, D>,
|
||||||
|
vsl: OMatrix<T, D, D>,
|
||||||
|
vsr: OMatrix<T, D, D>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Scalar + Copy, D: Dim> Copy for GeneralizedEigen<T, D>
|
||||||
|
where
|
||||||
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
||||||
|
OMatrix<T, D, D>: Copy,
|
||||||
|
OVector<T, D>: Copy,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: GeneralizedEigenScalar + RealField + Copy, D: Dim> GeneralizedEigen<T, D>
|
||||||
|
where
|
||||||
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
||||||
|
{
|
||||||
|
/// Attempts to compute the generalized eigenvalues, and left and right associated eigenvectors
|
||||||
|
/// via the raw returns from LAPACK's dggev and sggev routines
|
||||||
|
///
|
||||||
|
/// Panics if the method did not converge.
|
||||||
|
pub fn new(a: OMatrix<T, D, D>, b: OMatrix<T, D, D>) -> Self {
|
||||||
|
Self::try_new(a, b).expect("Calculation of generalized eigenvalues failed.")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to compute the generalized eigenvalues (and eigenvectors) via the raw returns from LAPACK's
|
||||||
|
/// dggev and sggev routines
|
||||||
|
///
|
||||||
|
/// Returns `None` if the method did not converge.
|
||||||
|
pub fn try_new(mut a: OMatrix<T, D, D>, mut b: OMatrix<T, D, D>) -> Option<Self> {
|
||||||
|
assert!(
|
||||||
|
a.is_square() && b.is_square(),
|
||||||
|
"Unable to compute the generalized eigenvalues of non-square matrices."
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
a.shape_generic() == b.shape_generic(),
|
||||||
|
"Unable to compute the generalized eigenvalues of two square matrices of different dimensions."
|
||||||
|
);
|
||||||
|
|
||||||
|
let (nrows, ncols) = a.shape_generic();
|
||||||
|
let n = nrows.value();
|
||||||
|
|
||||||
|
let mut info = 0;
|
||||||
|
|
||||||
|
let mut alphar = Matrix::zeros_generic(nrows, Const::<1>);
|
||||||
|
let mut alphai = Matrix::zeros_generic(nrows, Const::<1>);
|
||||||
|
let mut beta = Matrix::zeros_generic(nrows, Const::<1>);
|
||||||
|
let mut vsl = Matrix::zeros_generic(nrows, ncols);
|
||||||
|
let mut vsr = Matrix::zeros_generic(nrows, ncols);
|
||||||
|
|
||||||
|
let lwork = T::xggev_work_size(
|
||||||
|
b'V',
|
||||||
|
b'V',
|
||||||
|
n as i32,
|
||||||
|
a.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
b.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
alphar.as_mut_slice(),
|
||||||
|
alphai.as_mut_slice(),
|
||||||
|
beta.as_mut_slice(),
|
||||||
|
vsl.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
vsr.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
&mut info,
|
||||||
|
);
|
||||||
|
lapack_check!(info);
|
||||||
|
|
||||||
|
let mut work = vec![T::zero(); lwork as usize];
|
||||||
|
|
||||||
|
T::xggev(
|
||||||
|
b'V',
|
||||||
|
b'V',
|
||||||
|
n as i32,
|
||||||
|
a.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
b.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
alphar.as_mut_slice(),
|
||||||
|
alphai.as_mut_slice(),
|
||||||
|
beta.as_mut_slice(),
|
||||||
|
vsl.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
vsr.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
&mut work,
|
||||||
|
lwork,
|
||||||
|
&mut info,
|
||||||
|
);
|
||||||
|
lapack_check!(info);
|
||||||
|
|
||||||
|
Some(GeneralizedEigen {
|
||||||
|
alphar,
|
||||||
|
alphai,
|
||||||
|
beta,
|
||||||
|
vsl,
|
||||||
|
vsr,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calculates the generalized eigenvectors (left and right) associated with the generalized eigenvalues
|
||||||
|
///
|
||||||
|
/// Outputs two matrices.
|
||||||
|
/// The first output matrix contains the left eigenvectors of the generalized eigenvalues
|
||||||
|
/// as columns.
|
||||||
|
/// The second matrix contains the right eigenvectors of the generalized eigenvalues
|
||||||
|
/// as columns.
|
||||||
|
pub fn eigenvectors(&self) -> (OMatrix<Complex<T>, D, D>, OMatrix<Complex<T>, D, D>)
|
||||||
|
where
|
||||||
|
DefaultAllocator:
|
||||||
|
Allocator<Complex<T>, D, D> + Allocator<Complex<T>, D> + Allocator<(Complex<T>, T), D>,
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
How the eigenvectors are built up:
|
||||||
|
|
||||||
|
Since the input entries are all real, the generalized eigenvalues if complex come in pairs
|
||||||
|
as a consequence of the [complex conjugate root thorem](https://en.wikipedia.org/wiki/Complex_conjugate_root_theorem)
|
||||||
|
The Lapack routine output reflects this by expecting the user to unpack the real and complex eigenvalues associated
|
||||||
|
eigenvectors from the real matrix output via the following procedure
|
||||||
|
|
||||||
|
(Note: VL stands for the lapack real matrix output containing the left eigenvectors as columns,
|
||||||
|
VR stands for the lapack real matrix output containing the right eigenvectors as columns)
|
||||||
|
|
||||||
|
If the j-th and (j+1)-th eigenvalues form a complex conjugate pair,
|
||||||
|
then
|
||||||
|
|
||||||
|
u(j) = VL(:,j)+i*VL(:,j+1)
|
||||||
|
u(j+1) = VL(:,j)-i*VL(:,j+1)
|
||||||
|
|
||||||
|
and
|
||||||
|
|
||||||
|
u(j) = VR(:,j)+i*VR(:,j+1)
|
||||||
|
v(j+1) = VR(:,j)-i*VR(:,j+1).
|
||||||
|
*/
|
||||||
|
|
||||||
|
let n = self.vsl.shape().0;
|
||||||
|
|
||||||
|
let mut l = self.vsl.map(|x| Complex::new(x, T::RealField::zero()));
|
||||||
|
|
||||||
|
let mut r = self.vsr.map(|x| Complex::new(x, T::RealField::zero()));
|
||||||
|
|
||||||
|
let eigenvalues = self.raw_eigenvalues();
|
||||||
|
|
||||||
|
let mut c = 0;
|
||||||
|
|
||||||
|
while c < n {
|
||||||
|
if eigenvalues[c].0.im.abs() != T::RealField::zero() && c + 1 < n {
|
||||||
|
// taking care of the left eigenvector matrix
|
||||||
|
l.column_mut(c).zip_apply(&self.vsl.column(c + 1), |r, i| {
|
||||||
|
*r = Complex::new(r.re.clone(), i.clone());
|
||||||
|
});
|
||||||
|
l.column_mut(c + 1).zip_apply(&self.vsl.column(c), |i, r| {
|
||||||
|
*i = Complex::new(r.clone(), -i.re.clone());
|
||||||
|
});
|
||||||
|
|
||||||
|
// taking care of the right eigenvector matrix
|
||||||
|
r.column_mut(c).zip_apply(&self.vsr.column(c + 1), |r, i| {
|
||||||
|
*r = Complex::new(r.re.clone(), i.clone());
|
||||||
|
});
|
||||||
|
r.column_mut(c + 1).zip_apply(&self.vsr.column(c), |i, r| {
|
||||||
|
*i = Complex::new(r.clone(), -i.re.clone());
|
||||||
|
});
|
||||||
|
|
||||||
|
c += 2;
|
||||||
|
} else {
|
||||||
|
c += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(l, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alphai), beta)
|
||||||
|
/// straight from LAPACK
|
||||||
|
#[must_use]
|
||||||
|
pub fn raw_eigenvalues(&self) -> OVector<(Complex<T>, T), D>
|
||||||
|
where
|
||||||
|
DefaultAllocator: Allocator<(Complex<T>, T), D>,
|
||||||
|
{
|
||||||
|
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() {
|
||||||
|
out[i] = (Complex::new(self.alphar[i], self.alphai[i]), self.beta[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Lapack functions dispatch.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/// Trait implemented by scalars for which Lapack implements the RealField GeneralizedEigen decomposition.
|
||||||
|
pub trait GeneralizedEigenScalar: Scalar {
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
fn xggev(
|
||||||
|
jobvsl: u8,
|
||||||
|
jobvsr: u8,
|
||||||
|
n: i32,
|
||||||
|
a: &mut [Self],
|
||||||
|
lda: i32,
|
||||||
|
b: &mut [Self],
|
||||||
|
ldb: i32,
|
||||||
|
alphar: &mut [Self],
|
||||||
|
alphai: &mut [Self],
|
||||||
|
beta: &mut [Self],
|
||||||
|
vsl: &mut [Self],
|
||||||
|
ldvsl: i32,
|
||||||
|
vsr: &mut [Self],
|
||||||
|
ldvsr: i32,
|
||||||
|
work: &mut [Self],
|
||||||
|
lwork: i32,
|
||||||
|
info: &mut i32,
|
||||||
|
);
|
||||||
|
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
fn xggev_work_size(
|
||||||
|
jobvsl: u8,
|
||||||
|
jobvsr: u8,
|
||||||
|
n: i32,
|
||||||
|
a: &mut [Self],
|
||||||
|
lda: i32,
|
||||||
|
b: &mut [Self],
|
||||||
|
ldb: i32,
|
||||||
|
alphar: &mut [Self],
|
||||||
|
alphai: &mut [Self],
|
||||||
|
beta: &mut [Self],
|
||||||
|
vsl: &mut [Self],
|
||||||
|
ldvsl: i32,
|
||||||
|
vsr: &mut [Self],
|
||||||
|
ldvsr: i32,
|
||||||
|
info: &mut i32,
|
||||||
|
) -> i32;
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! generalized_eigen_scalar_impl (
|
||||||
|
($N: ty, $xggev: path) => (
|
||||||
|
impl GeneralizedEigenScalar for $N {
|
||||||
|
#[inline]
|
||||||
|
fn xggev(jobvsl: u8,
|
||||||
|
jobvsr: u8,
|
||||||
|
n: i32,
|
||||||
|
a: &mut [$N],
|
||||||
|
lda: i32,
|
||||||
|
b: &mut [$N],
|
||||||
|
ldb: i32,
|
||||||
|
alphar: &mut [$N],
|
||||||
|
alphai: &mut [$N],
|
||||||
|
beta : &mut [$N],
|
||||||
|
vsl: &mut [$N],
|
||||||
|
ldvsl: i32,
|
||||||
|
vsr: &mut [$N],
|
||||||
|
ldvsr: i32,
|
||||||
|
work: &mut [$N],
|
||||||
|
lwork: i32,
|
||||||
|
info: &mut i32) {
|
||||||
|
unsafe { $xggev(jobvsl, jobvsr, n, a, lda, b, ldb, alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, work, lwork, info); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn xggev_work_size(jobvsl: u8,
|
||||||
|
jobvsr: u8,
|
||||||
|
n: i32,
|
||||||
|
a: &mut [$N],
|
||||||
|
lda: i32,
|
||||||
|
b: &mut [$N],
|
||||||
|
ldb: i32,
|
||||||
|
alphar: &mut [$N],
|
||||||
|
alphai: &mut [$N],
|
||||||
|
beta : &mut [$N],
|
||||||
|
vsl: &mut [$N],
|
||||||
|
ldvsl: i32,
|
||||||
|
vsr: &mut [$N],
|
||||||
|
ldvsr: i32,
|
||||||
|
info: &mut i32)
|
||||||
|
-> i32 {
|
||||||
|
let mut work = [ Zero::zero() ];
|
||||||
|
let lwork = -1 as i32;
|
||||||
|
|
||||||
|
unsafe { $xggev(jobvsl, jobvsr, n, a, lda, b, ldb, alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, &mut work, lwork, info); }
|
||||||
|
ComplexHelper::real_part(work[0]) as i32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
generalized_eigen_scalar_impl!(f32, lapack::sggev);
|
||||||
|
generalized_eigen_scalar_impl!(f64, lapack::dggev);
|
|
@ -83,9 +83,11 @@ mod lapack_check;
|
||||||
|
|
||||||
mod cholesky;
|
mod cholesky;
|
||||||
mod eigen;
|
mod eigen;
|
||||||
|
mod generalized_eigenvalues;
|
||||||
mod hessenberg;
|
mod hessenberg;
|
||||||
mod lu;
|
mod lu;
|
||||||
mod qr;
|
mod qr;
|
||||||
|
mod qz;
|
||||||
mod schur;
|
mod schur;
|
||||||
mod svd;
|
mod svd;
|
||||||
mod symmetric_eigen;
|
mod symmetric_eigen;
|
||||||
|
@ -94,9 +96,11 @@ use num_complex::Complex;
|
||||||
|
|
||||||
pub use self::cholesky::{Cholesky, CholeskyScalar};
|
pub use self::cholesky::{Cholesky, CholeskyScalar};
|
||||||
pub use self::eigen::Eigen;
|
pub use self::eigen::Eigen;
|
||||||
|
pub use self::generalized_eigenvalues::GeneralizedEigen;
|
||||||
pub use self::hessenberg::Hessenberg;
|
pub use self::hessenberg::Hessenberg;
|
||||||
pub use self::lu::{LUScalar, LU};
|
pub use self::lu::{LUScalar, LU};
|
||||||
pub use self::qr::QR;
|
pub use self::qr::QR;
|
||||||
|
pub use self::qz::QZ;
|
||||||
pub use self::schur::Schur;
|
pub use self::schur::Schur;
|
||||||
pub use self::svd::SVD;
|
pub use self::svd::SVD;
|
||||||
pub use self::symmetric_eigen::SymmetricEigen;
|
pub use self::symmetric_eigen::SymmetricEigen;
|
||||||
|
|
|
@ -0,0 +1,321 @@
|
||||||
|
#[cfg(feature = "serde-serialize")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use num::Zero;
|
||||||
|
use num_complex::Complex;
|
||||||
|
|
||||||
|
use simba::scalar::RealField;
|
||||||
|
|
||||||
|
use crate::ComplexHelper;
|
||||||
|
use na::allocator::Allocator;
|
||||||
|
use na::dimension::{Const, Dim};
|
||||||
|
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};
|
||||||
|
|
||||||
|
use lapack;
|
||||||
|
|
||||||
|
/// 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",
|
||||||
|
serde(
|
||||||
|
bound(serialize = "DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
||||||
|
OVector<T, D>: Serialize,
|
||||||
|
OMatrix<T, D, D>: Serialize")
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serde-serialize",
|
||||||
|
serde(
|
||||||
|
bound(deserialize = "DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
||||||
|
OVector<T, D>: Deserialize<'de>,
|
||||||
|
OMatrix<T, D, D>: Deserialize<'de>")
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct QZ<T: Scalar, D: Dim>
|
||||||
|
where
|
||||||
|
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
|
||||||
|
{
|
||||||
|
alphar: OVector<T, D>,
|
||||||
|
alphai: OVector<T, D>,
|
||||||
|
beta: OVector<T, D>,
|
||||||
|
vsl: OMatrix<T, D, D>,
|
||||||
|
s: OMatrix<T, D, D>,
|
||||||
|
vsr: OMatrix<T, D, D>,
|
||||||
|
t: OMatrix<T, D, D>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Scalar + Copy, D: Dim> Copy for QZ<T, D>
|
||||||
|
where
|
||||||
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
||||||
|
OMatrix<T, D, D>: Copy,
|
||||||
|
OVector<T, D>: Copy,
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: QZScalar + RealField, D: Dim> QZ<T, D>
|
||||||
|
where
|
||||||
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
||||||
|
{
|
||||||
|
/// Attempts to compute the QZ decomposition of input real 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.
|
||||||
|
pub fn new(a: OMatrix<T, D, D>, b: OMatrix<T, D, D>) -> Self {
|
||||||
|
Self::try_new(a, b).expect("QZ decomposition: convergence failed.")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Computes the decomposition of input matrices `a` and `b` into a pair of matrices of Schur vectors
|
||||||
|
/// , a quasi-upper triangular matrix and an upper-triangular matrix .
|
||||||
|
///
|
||||||
|
/// Returns `None` if the method did not converge.
|
||||||
|
pub fn try_new(mut a: OMatrix<T, D, D>, mut b: OMatrix<T, D, D>) -> Option<Self> {
|
||||||
|
assert!(
|
||||||
|
a.is_square() && b.is_square(),
|
||||||
|
"Unable to compute the qz decomposition of non-square matrices."
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
a.shape_generic() == b.shape_generic(),
|
||||||
|
"Unable to compute the qz decomposition of two square matrices of different dimensions."
|
||||||
|
);
|
||||||
|
|
||||||
|
let (nrows, ncols) = a.shape_generic();
|
||||||
|
let n = nrows.value();
|
||||||
|
|
||||||
|
let mut info = 0;
|
||||||
|
|
||||||
|
let mut alphar = Matrix::zeros_generic(nrows, Const::<1>);
|
||||||
|
let mut alphai = Matrix::zeros_generic(nrows, Const::<1>);
|
||||||
|
let mut beta = Matrix::zeros_generic(nrows, Const::<1>);
|
||||||
|
let mut vsl = Matrix::zeros_generic(nrows, ncols);
|
||||||
|
let mut vsr = Matrix::zeros_generic(nrows, ncols);
|
||||||
|
// Placeholders:
|
||||||
|
let mut bwork = [0i32];
|
||||||
|
let mut unused = 0;
|
||||||
|
|
||||||
|
let lwork = T::xgges_work_size(
|
||||||
|
b'V',
|
||||||
|
b'V',
|
||||||
|
b'N',
|
||||||
|
n as i32,
|
||||||
|
a.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
b.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
&mut unused,
|
||||||
|
alphar.as_mut_slice(),
|
||||||
|
alphai.as_mut_slice(),
|
||||||
|
beta.as_mut_slice(),
|
||||||
|
vsl.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
vsr.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
&mut bwork,
|
||||||
|
&mut info,
|
||||||
|
);
|
||||||
|
lapack_check!(info);
|
||||||
|
|
||||||
|
let mut work = vec![T::zero(); lwork as usize];
|
||||||
|
|
||||||
|
T::xgges(
|
||||||
|
b'V',
|
||||||
|
b'V',
|
||||||
|
b'N',
|
||||||
|
n as i32,
|
||||||
|
a.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
b.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
&mut unused,
|
||||||
|
alphar.as_mut_slice(),
|
||||||
|
alphai.as_mut_slice(),
|
||||||
|
beta.as_mut_slice(),
|
||||||
|
vsl.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
vsr.as_mut_slice(),
|
||||||
|
n as i32,
|
||||||
|
&mut work,
|
||||||
|
lwork,
|
||||||
|
&mut bwork,
|
||||||
|
&mut info,
|
||||||
|
);
|
||||||
|
lapack_check!(info);
|
||||||
|
|
||||||
|
Some(QZ {
|
||||||
|
alphar,
|
||||||
|
alphai,
|
||||||
|
beta,
|
||||||
|
vsl,
|
||||||
|
s: a,
|
||||||
|
vsr,
|
||||||
|
t: b,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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()`.
|
||||||
|
pub fn unpack(
|
||||||
|
self,
|
||||||
|
) -> (
|
||||||
|
OMatrix<T, D, D>,
|
||||||
|
OMatrix<T, D, D>,
|
||||||
|
OMatrix<T, D, D>,
|
||||||
|
OMatrix<T, D, D>,
|
||||||
|
) {
|
||||||
|
(self.vsl, self.s, self.t, self.vsr)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// outputs the unprocessed (almost) version of generalized eigenvalues ((alphar, alpai), beta)
|
||||||
|
/// straight from LAPACK
|
||||||
|
#[must_use]
|
||||||
|
pub fn raw_eigenvalues(&self) -> OVector<(Complex<T>, T), D>
|
||||||
|
where
|
||||||
|
DefaultAllocator: Allocator<(Complex<T>, T), D>,
|
||||||
|
{
|
||||||
|
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() {
|
||||||
|
out[i] = (
|
||||||
|
Complex::new(self.alphar[i].clone(), self.alphai[i].clone()),
|
||||||
|
self.beta[i].clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Lapack functions dispatch.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/// Trait implemented by scalars for which Lapack implements the RealField QZ decomposition.
|
||||||
|
pub trait QZScalar: Scalar {
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
fn xgges(
|
||||||
|
jobvsl: u8,
|
||||||
|
jobvsr: u8,
|
||||||
|
sort: u8,
|
||||||
|
// select: ???
|
||||||
|
n: i32,
|
||||||
|
a: &mut [Self],
|
||||||
|
lda: i32,
|
||||||
|
b: &mut [Self],
|
||||||
|
ldb: i32,
|
||||||
|
sdim: &mut i32,
|
||||||
|
alphar: &mut [Self],
|
||||||
|
alphai: &mut [Self],
|
||||||
|
beta: &mut [Self],
|
||||||
|
vsl: &mut [Self],
|
||||||
|
ldvsl: i32,
|
||||||
|
vsr: &mut [Self],
|
||||||
|
ldvsr: i32,
|
||||||
|
work: &mut [Self],
|
||||||
|
lwork: i32,
|
||||||
|
bwork: &mut [i32],
|
||||||
|
info: &mut i32,
|
||||||
|
);
|
||||||
|
|
||||||
|
#[allow(missing_docs)]
|
||||||
|
fn xgges_work_size(
|
||||||
|
jobvsl: u8,
|
||||||
|
jobvsr: u8,
|
||||||
|
sort: u8,
|
||||||
|
// select: ???
|
||||||
|
n: i32,
|
||||||
|
a: &mut [Self],
|
||||||
|
lda: i32,
|
||||||
|
b: &mut [Self],
|
||||||
|
ldb: i32,
|
||||||
|
sdim: &mut i32,
|
||||||
|
alphar: &mut [Self],
|
||||||
|
alphai: &mut [Self],
|
||||||
|
beta: &mut [Self],
|
||||||
|
vsl: &mut [Self],
|
||||||
|
ldvsl: i32,
|
||||||
|
vsr: &mut [Self],
|
||||||
|
ldvsr: i32,
|
||||||
|
bwork: &mut [i32],
|
||||||
|
info: &mut i32,
|
||||||
|
) -> i32;
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! qz_scalar_impl (
|
||||||
|
($N: ty, $xgges: path) => (
|
||||||
|
impl QZScalar for $N {
|
||||||
|
#[inline]
|
||||||
|
fn xgges(jobvsl: u8,
|
||||||
|
jobvsr: u8,
|
||||||
|
sort: u8,
|
||||||
|
// select: ???
|
||||||
|
n: i32,
|
||||||
|
a: &mut [$N],
|
||||||
|
lda: i32,
|
||||||
|
b: &mut [$N],
|
||||||
|
ldb: i32,
|
||||||
|
sdim: &mut i32,
|
||||||
|
alphar: &mut [$N],
|
||||||
|
alphai: &mut [$N],
|
||||||
|
beta : &mut [$N],
|
||||||
|
vsl: &mut [$N],
|
||||||
|
ldvsl: i32,
|
||||||
|
vsr: &mut [$N],
|
||||||
|
ldvsr: i32,
|
||||||
|
work: &mut [$N],
|
||||||
|
lwork: i32,
|
||||||
|
bwork: &mut [i32],
|
||||||
|
info: &mut i32) {
|
||||||
|
unsafe { $xgges(jobvsl, jobvsr, sort, None, n, a, lda, b, ldb, sdim, alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, work, lwork, bwork, info); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn xgges_work_size(jobvsl: u8,
|
||||||
|
jobvsr: u8,
|
||||||
|
sort: u8,
|
||||||
|
// select: ???
|
||||||
|
n: i32,
|
||||||
|
a: &mut [$N],
|
||||||
|
lda: i32,
|
||||||
|
b: &mut [$N],
|
||||||
|
ldb: i32,
|
||||||
|
sdim: &mut i32,
|
||||||
|
alphar: &mut [$N],
|
||||||
|
alphai: &mut [$N],
|
||||||
|
beta : &mut [$N],
|
||||||
|
vsl: &mut [$N],
|
||||||
|
ldvsl: i32,
|
||||||
|
vsr: &mut [$N],
|
||||||
|
ldvsr: i32,
|
||||||
|
bwork: &mut [i32],
|
||||||
|
info: &mut i32)
|
||||||
|
-> i32 {
|
||||||
|
let mut work = [ Zero::zero() ];
|
||||||
|
let lwork = -1 as i32;
|
||||||
|
|
||||||
|
unsafe { $xgges(jobvsl, jobvsr, sort, None, n, a, lda, b, ldb, sdim, alphar, alphai, beta, vsl, ldvsl, vsr, ldvsr, &mut work, lwork, bwork, info); }
|
||||||
|
ComplexHelper::real_part(work[0]) as i32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
qz_scalar_impl!(f32, lapack::sgges);
|
||||||
|
qz_scalar_impl!(f64, lapack::dgges);
|
|
@ -0,0 +1,72 @@
|
||||||
|
use na::dimension::Const;
|
||||||
|
use na::{DMatrix, OMatrix};
|
||||||
|
use nl::GeneralizedEigen;
|
||||||
|
use num_complex::Complex;
|
||||||
|
use simba::scalar::ComplexField;
|
||||||
|
|
||||||
|
use crate::proptest::*;
|
||||||
|
use proptest::{prop_assert, prop_compose, proptest};
|
||||||
|
|
||||||
|
prop_compose! {
|
||||||
|
fn f64_dynamic_dim_squares()
|
||||||
|
(n in PROPTEST_MATRIX_DIM)
|
||||||
|
(a in matrix(PROPTEST_F64,n,n), b in matrix(PROPTEST_F64,n,n)) -> (DMatrix<f64>, DMatrix<f64>){
|
||||||
|
(a,b)
|
||||||
|
}}
|
||||||
|
|
||||||
|
proptest! {
|
||||||
|
#[test]
|
||||||
|
fn ge((a,b) in f64_dynamic_dim_squares()){
|
||||||
|
|
||||||
|
let a_c = a.clone().map(|x| Complex::new(x, 0.0));
|
||||||
|
let b_c = b.clone().map(|x| Complex::new(x, 0.0));
|
||||||
|
let n = a.shape_generic().0;
|
||||||
|
|
||||||
|
let ge = GeneralizedEigen::new(a.clone(), b.clone());
|
||||||
|
let (vsl,vsr) = ge.clone().eigenvectors();
|
||||||
|
|
||||||
|
|
||||||
|
for (i,(alpha,beta)) in ge.raw_eigenvalues().iter().enumerate() {
|
||||||
|
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)*vsr.column(i)).map(|x| x.modulus()),
|
||||||
|
OMatrix::zeros_generic(n, Const::<1>),
|
||||||
|
epsilon = 1.0e-5));
|
||||||
|
|
||||||
|
prop_assert!(
|
||||||
|
relative_eq!(
|
||||||
|
(vsl.column(i).adjoint()*(&l_a - &l_b)).map(|x| x.modulus()),
|
||||||
|
OMatrix::zeros_generic(Const::<1>, n),
|
||||||
|
epsilon = 1.0e-5))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ge_static(a in matrix4(), b in matrix4()) {
|
||||||
|
|
||||||
|
let ge = GeneralizedEigen::new(a.clone(), b.clone());
|
||||||
|
let a_c =a.clone().map(|x| Complex::new(x, 0.0));
|
||||||
|
let b_c = b.clone().map(|x| Complex::new(x, 0.0));
|
||||||
|
let (vsl,vsr) = ge.eigenvectors();
|
||||||
|
let eigenvalues = ge.raw_eigenvalues();
|
||||||
|
|
||||||
|
for (i,(alpha,beta)) in eigenvalues.iter().enumerate() {
|
||||||
|
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)*vsr.column(i)).map(|x| x.modulus()),
|
||||||
|
OMatrix::zeros_generic(Const::<4>, Const::<1>),
|
||||||
|
epsilon = 1.0e-5));
|
||||||
|
prop_assert!(
|
||||||
|
relative_eq!((vsl.column(i).adjoint()*(&l_a - &l_b)).map(|x| x.modulus()),
|
||||||
|
OMatrix::zeros_generic(Const::<1>, Const::<4>),
|
||||||
|
epsilon = 1.0e-5))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
mod cholesky;
|
mod cholesky;
|
||||||
|
mod generalized_eigenvalues;
|
||||||
mod lu;
|
mod lu;
|
||||||
mod qr;
|
mod qr;
|
||||||
|
mod qz;
|
||||||
mod real_eigensystem;
|
mod real_eigensystem;
|
||||||
mod schur;
|
mod schur;
|
||||||
mod svd;
|
mod svd;
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
use na::DMatrix;
|
||||||
|
use nl::QZ;
|
||||||
|
|
||||||
|
use crate::proptest::*;
|
||||||
|
use proptest::{prop_assert, prop_compose, proptest};
|
||||||
|
|
||||||
|
prop_compose! {
|
||||||
|
fn f64_dynamic_dim_squares()
|
||||||
|
(n in PROPTEST_MATRIX_DIM)
|
||||||
|
(a in matrix(PROPTEST_F64,n,n), b in matrix(PROPTEST_F64,n,n)) -> (DMatrix<f64>, DMatrix<f64>){
|
||||||
|
(a,b)
|
||||||
|
}}
|
||||||
|
|
||||||
|
proptest! {
|
||||||
|
#[test]
|
||||||
|
fn qz((a,b) in f64_dynamic_dim_squares()) {
|
||||||
|
|
||||||
|
let qz = QZ::new(a.clone(), b.clone());
|
||||||
|
let (vsl,s,t,vsr) = qz.clone().unpack();
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn qz_static(a in matrix4(), b in matrix4()) {
|
||||||
|
let qz = QZ::new(a.clone(), b.clone());
|
||||||
|
let (vsl,s,t,vsr) = qz.unpack();
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue