2017-08-14 01:53:04 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-10-22 13:00:10 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2017-08-14 01:53:04 +08:00
|
|
|
|
2017-08-07 01:41:12 +08:00
|
|
|
use num::Zero;
|
|
|
|
use num_complex::Complex;
|
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::RealField;
|
2017-08-07 01:41:12 +08:00
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use crate::ComplexHelper;
|
2018-05-25 05:51:57 +08:00
|
|
|
use na::allocator::Allocator;
|
2021-04-12 16:34:44 +08:00
|
|
|
use na::dimension::{Const, Dim};
|
2017-08-07 01:41:12 +08:00
|
|
|
use na::storage::Storage;
|
2021-04-11 17:00:38 +08:00
|
|
|
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};
|
2017-08-07 01:41:12 +08:00
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
use lapack;
|
2017-08-07 01:41:12 +08:00
|
|
|
|
|
|
|
/// Eigendecomposition of a real square matrix with real eigenvalues.
|
2017-08-14 01:53:04 +08:00
|
|
|
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
2018-05-25 05:51:57 +08:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2020-03-21 19:16:46 +08:00
|
|
|
serde(
|
2021-04-11 17:00:38 +08:00
|
|
|
bound(serialize = "DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
|
|
|
OVector<T, D>: Serialize,
|
|
|
|
OMatrix<T, D, D>: Serialize")
|
2020-03-21 19:16:46 +08:00
|
|
|
)
|
2018-05-25 05:51:57 +08:00
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2020-03-21 19:16:46 +08:00
|
|
|
serde(
|
2021-04-11 17:00:38 +08:00
|
|
|
bound(deserialize = "DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
|
|
|
OVector<T, D>: Serialize,
|
|
|
|
OMatrix<T, D, D>: Deserialize<'de>")
|
2020-03-21 19:16:46 +08:00
|
|
|
)
|
2018-05-25 05:51:57 +08:00
|
|
|
)]
|
2017-08-14 01:53:04 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub struct Schur<T: Scalar, D: Dim>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D> + Allocator<T, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
re: OVector<T, D>,
|
|
|
|
im: OVector<T, D>,
|
|
|
|
t: OMatrix<T, D, D>,
|
|
|
|
q: OMatrix<T, D, D>,
|
2017-08-07 01:41:12 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Copy, D: Dim> Copy for Schur<T, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
|
|
|
OMatrix<T, D, D>: Copy,
|
|
|
|
OVector<T, D>: Copy,
|
2020-03-21 19:16:46 +08:00
|
|
|
{
|
|
|
|
}
|
2017-08-07 01:41:12 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: SchurScalar + RealField, D: Dim> Schur<T, D>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D> + Allocator<T, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Computes the eigenvalues and real Schur form of the matrix `m`.
|
2017-08-07 01:41:12 +08:00
|
|
|
///
|
|
|
|
/// Panics if the method did not converge.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new(m: OMatrix<T, D, D>) -> Self {
|
2019-03-23 18:46:56 +08:00
|
|
|
Self::try_new(m).expect("Schur decomposition: convergence failed.")
|
2017-08-07 01:41:12 +08:00
|
|
|
}
|
|
|
|
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Computes the eigenvalues and real Schur form of the matrix `m`.
|
2017-08-07 01:41:12 +08:00
|
|
|
///
|
|
|
|
/// Returns `None` if the method did not converge.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn try_new(mut m: OMatrix<T, D, D>) -> Option<Self> {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
m.is_square(),
|
|
|
|
"Unable to compute the eigenvalue decomposition of a non-square matrix."
|
|
|
|
);
|
2017-08-07 01:41:12 +08:00
|
|
|
|
|
|
|
let (nrows, ncols) = m.data.shape();
|
|
|
|
let n = nrows.value();
|
|
|
|
|
|
|
|
let lda = n as i32;
|
|
|
|
|
|
|
|
let mut info = 0;
|
|
|
|
|
2021-04-12 16:34:44 +08:00
|
|
|
let mut wr = unsafe { Matrix::new_uninitialized_generic(nrows, Const::<1>).assume_init() };
|
|
|
|
let mut wi = unsafe { Matrix::new_uninitialized_generic(nrows, Const::<1>).assume_init() };
|
2021-02-25 22:07:15 +08:00
|
|
|
let mut q = unsafe { Matrix::new_uninitialized_generic(nrows, ncols).assume_init() };
|
2017-08-07 01:41:12 +08:00
|
|
|
// Placeholders:
|
2018-02-02 19:26:35 +08:00
|
|
|
let mut bwork = [0i32];
|
2017-08-07 01:41:12 +08:00
|
|
|
let mut unused = 0;
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let lwork = T::xgees_work_size(
|
2018-02-02 19:26:35 +08:00
|
|
|
b'V',
|
2021-04-11 17:00:38 +08:00
|
|
|
b'T',
|
2018-02-02 19:26:35 +08:00
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
&mut unused,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
q.as_mut_slice(),
|
|
|
|
n as i32,
|
|
|
|
&mut bwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-07 01:41:12 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
let mut work = unsafe { crate::uninitialized_vec(lwork as usize) };
|
2017-08-07 01:41:12 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xgees(
|
2018-02-02 19:26:35 +08:00
|
|
|
b'V',
|
2021-04-11 17:00:38 +08:00
|
|
|
b'T',
|
2018-02-02 19:26:35 +08:00
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
&mut unused,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
q.as_mut_slice(),
|
|
|
|
n as i32,
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut bwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-07 01:41:12 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
2019-03-23 18:46:56 +08:00
|
|
|
Some(Schur {
|
2018-02-02 19:26:35 +08:00
|
|
|
re: wr,
|
|
|
|
im: wi,
|
|
|
|
t: m,
|
|
|
|
q: q,
|
|
|
|
})
|
2017-08-07 01:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the unitary matrix `Q` and the upper-quasitriangular matrix `T` such that the
|
|
|
|
/// decomposed matrix equals `Q * T * Q.transpose()`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn unpack(self) -> (OMatrix<T, D, D>, OMatrix<T, D, D>) {
|
2017-08-07 01:41:12 +08:00
|
|
|
(self.q, self.t)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the real eigenvalues of the decomposed matrix.
|
|
|
|
///
|
|
|
|
/// Return `None` if some eigenvalues are complex.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn eigenvalues(&self) -> Option<OVector<T, D>> {
|
2017-08-07 01:41:12 +08:00
|
|
|
if self.im.iter().all(|e| e.is_zero()) {
|
|
|
|
Some(self.re.clone())
|
2018-02-02 19:26:35 +08:00
|
|
|
} else {
|
2017-08-07 01:41:12 +08:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the complex eigenvalues of the decomposed matrix.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn complex_eigenvalues(&self) -> OVector<Complex<T>, D>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<Complex<T>, D>,
|
2020-04-06 00:49:48 +08:00
|
|
|
{
|
2021-04-12 16:34:44 +08:00
|
|
|
let mut out = unsafe {
|
|
|
|
OVector::new_uninitialized_generic(self.t.data.shape().0, Const::<1>).assume_init()
|
|
|
|
};
|
2017-08-07 01:41:12 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
for i in 0..out.len() {
|
2017-08-07 01:41:12 +08:00
|
|
|
out[i] = Complex::new(self.re[i], self.im[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Lapack functions dispatch.
|
|
|
|
*
|
|
|
|
*/
|
2019-03-25 18:21:41 +08:00
|
|
|
/// Trait implemented by scalars for which Lapack implements the RealField Schur decomposition.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub trait SchurScalar: Scalar {
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgees(
|
|
|
|
jobvs: u8,
|
|
|
|
sort: u8,
|
|
|
|
// select: ???
|
|
|
|
n: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
sdim: &mut i32,
|
|
|
|
wr: &mut [Self],
|
|
|
|
wi: &mut [Self],
|
|
|
|
vs: &mut [Self],
|
|
|
|
ldvs: i32,
|
|
|
|
work: &mut [Self],
|
|
|
|
lwork: i32,
|
|
|
|
bwork: &mut [i32],
|
|
|
|
info: &mut i32,
|
|
|
|
);
|
2017-08-14 01:52:58 +08:00
|
|
|
|
|
|
|
#[allow(missing_docs)]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgees_work_size(
|
|
|
|
jobvs: u8,
|
|
|
|
sort: u8,
|
|
|
|
// select: ???
|
|
|
|
n: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
sdim: &mut i32,
|
|
|
|
wr: &mut [Self],
|
|
|
|
wi: &mut [Self],
|
|
|
|
vs: &mut [Self],
|
|
|
|
ldvs: i32,
|
|
|
|
bwork: &mut [i32],
|
|
|
|
info: &mut i32,
|
|
|
|
) -> i32;
|
2017-08-07 01:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! real_eigensystem_scalar_impl (
|
|
|
|
($N: ty, $xgees: path) => (
|
2019-03-23 18:46:56 +08:00
|
|
|
impl SchurScalar for $N {
|
2017-08-07 01:41:12 +08:00
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgees(jobvs: u8,
|
|
|
|
sort: u8,
|
2017-08-07 01:41:12 +08:00
|
|
|
// select: ???
|
2018-02-02 19:26:35 +08:00
|
|
|
n: i32,
|
|
|
|
a: &mut [$N],
|
|
|
|
lda: i32,
|
|
|
|
sdim: &mut i32,
|
|
|
|
wr: &mut [$N],
|
|
|
|
wi: &mut [$N],
|
|
|
|
vs: &mut [$N],
|
|
|
|
ldvs: i32,
|
|
|
|
work: &mut [$N],
|
|
|
|
lwork: i32,
|
|
|
|
bwork: &mut [i32],
|
2017-08-07 01:41:12 +08:00
|
|
|
info: &mut i32) {
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xgees(jobvs, sort, None, n, a, lda, sdim, wr, wi, vs, ldvs, work, lwork, bwork, info); }
|
2017-08-07 01:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgees_work_size(jobvs: u8,
|
|
|
|
sort: u8,
|
2017-08-07 01:41:12 +08:00
|
|
|
// select: ???
|
2018-02-02 19:26:35 +08:00
|
|
|
n: i32,
|
|
|
|
a: &mut [$N],
|
|
|
|
lda: i32,
|
|
|
|
sdim: &mut i32,
|
|
|
|
wr: &mut [$N],
|
|
|
|
wi: &mut [$N],
|
|
|
|
vs: &mut [$N],
|
|
|
|
ldvs: i32,
|
|
|
|
bwork: &mut [i32],
|
2017-08-07 01:41:12 +08:00
|
|
|
info: &mut i32)
|
|
|
|
-> i32 {
|
|
|
|
let mut work = [ Zero::zero() ];
|
|
|
|
let lwork = -1 as i32;
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xgees(jobvs, sort, None, n, a, lda, sdim, wr, wi, vs, ldvs, &mut work, lwork, bwork, info); }
|
2017-08-07 01:41:12 +08:00
|
|
|
ComplexHelper::real_part(work[0]) as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
real_eigensystem_scalar_impl!(f32, lapack::sgees);
|
|
|
|
real_eigensystem_scalar_impl!(f64, lapack::dgees);
|