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-03 01:38:28 +08:00
|
|
|
use num::Zero;
|
2017-08-07 01:41:33 +08:00
|
|
|
use num_complex::Complex;
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::RealField;
|
2017-08-03 01:38:28 +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;
|
2017-08-03 01:38:28 +08:00
|
|
|
use na::dimension::{Dim, U1};
|
|
|
|
use na::storage::Storage;
|
2018-05-25 05:51:57 +08:00
|
|
|
use na::{DefaultAllocator, Matrix, MatrixN, Scalar, VectorN};
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
use lapack;
|
2017-08-03 01:38:28 +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(
|
|
|
|
bound(serialize = "DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
|
2018-09-13 12:55:58 +08:00
|
|
|
VectorN<N, D>: Serialize,
|
2020-03-21 19:16:46 +08:00
|
|
|
MatrixN<N, D>: Serialize")
|
|
|
|
)
|
2018-05-25 05:51:57 +08:00
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2020-03-21 19:16:46 +08:00
|
|
|
serde(
|
|
|
|
bound(deserialize = "DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
|
2018-09-13 12:55:58 +08:00
|
|
|
VectorN<N, D>: Serialize,
|
2020-03-21 19:16:46 +08:00
|
|
|
MatrixN<N, D>: Deserialize<'de>")
|
|
|
|
)
|
2018-05-25 05:51:57 +08:00
|
|
|
)]
|
2017-08-14 01:53:04 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2019-12-17 07:09:14 +08:00
|
|
|
pub struct Eigen<N: Scalar, D: Dim>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, D> + Allocator<N, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-14 01:52:58 +08:00
|
|
|
/// The eigenvalues of the decomposed matrix.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub eigenvalues: VectorN<N, D>,
|
2017-08-14 01:52:58 +08:00
|
|
|
/// The (right) eigenvectors of the decomposed matrix.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub eigenvectors: Option<MatrixN<N, D>>,
|
2017-08-14 01:52:58 +08:00
|
|
|
/// The left eigenvectors of the decomposed matrix.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub left_eigenvectors: Option<MatrixN<N, D>>,
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 06:15:18 +08:00
|
|
|
impl<N: Scalar + Copy, D: Dim> Copy for Eigen<N, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, D> + Allocator<N, D, D>,
|
|
|
|
VectorN<N, D>: Copy,
|
|
|
|
MatrixN<N, D>: Copy,
|
2020-03-21 19:16:46 +08:00
|
|
|
{
|
|
|
|
}
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2019-03-25 18:21:41 +08:00
|
|
|
impl<N: EigenScalar + RealField, D: Dim> Eigen<N, D>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, D, D> + Allocator<N, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-03 01:38:28 +08:00
|
|
|
/// Computes the eigenvalues and eigenvectors of the square matrix `m`.
|
|
|
|
///
|
|
|
|
/// If `eigenvectors` is `false` then, the eigenvectors are not computed explicitly.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub fn new(
|
|
|
|
mut m: MatrixN<N, D>,
|
|
|
|
left_eigenvectors: bool,
|
|
|
|
eigenvectors: bool,
|
2020-04-06 00:49:48 +08:00
|
|
|
) -> Option<Eigen<N, D>> {
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
m.is_square(),
|
|
|
|
"Unable to compute the eigenvalue decomposition of a non-square matrix."
|
|
|
|
);
|
|
|
|
|
|
|
|
let ljob = if left_eigenvectors { b'V' } else { b'N' };
|
2017-08-03 01:38:28 +08:00
|
|
|
let rjob = if eigenvectors { b'V' } else { b'N' };
|
|
|
|
|
|
|
|
let (nrows, ncols) = m.data.shape();
|
|
|
|
let n = nrows.value();
|
|
|
|
|
|
|
|
let lda = n as i32;
|
|
|
|
|
|
|
|
let mut wr = unsafe { Matrix::new_uninitialized_generic(nrows, U1) };
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: Tap into the workspace.
|
2017-08-03 01:38:28 +08:00
|
|
|
let mut wi = unsafe { Matrix::new_uninitialized_generic(nrows, U1) };
|
|
|
|
|
|
|
|
let mut info = 0;
|
2018-02-02 19:26:35 +08:00
|
|
|
let mut placeholder1 = [N::zero()];
|
|
|
|
let mut placeholder2 = [N::zero()];
|
|
|
|
|
|
|
|
let lwork = N::xgeev_work_size(
|
|
|
|
ljob,
|
|
|
|
rjob,
|
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
&mut placeholder1,
|
|
|
|
n as i32,
|
|
|
|
&mut placeholder2,
|
|
|
|
n as i32,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-03 01:38:28 +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-03 01:38:28 +08:00
|
|
|
|
|
|
|
match (left_eigenvectors, eigenvectors) {
|
|
|
|
(true, true) => {
|
|
|
|
let mut vl = unsafe { Matrix::new_uninitialized_generic(nrows, ncols) };
|
|
|
|
let mut vr = unsafe { Matrix::new_uninitialized_generic(nrows, ncols) };
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
N::xgeev(
|
|
|
|
ljob,
|
|
|
|
rjob,
|
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
&mut vl.as_mut_slice(),
|
|
|
|
n as i32,
|
|
|
|
&mut vr.as_mut_slice(),
|
|
|
|
n as i32,
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
if wi.iter().all(|e| e.is_zero()) {
|
2019-02-17 05:29:41 +08:00
|
|
|
return Some(Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
eigenvalues: wr,
|
|
|
|
left_eigenvectors: Some(vl),
|
|
|
|
eigenvectors: Some(vr),
|
|
|
|
});
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
2018-02-02 19:26:35 +08:00
|
|
|
}
|
2017-08-03 01:38:28 +08:00
|
|
|
(true, false) => {
|
|
|
|
let mut vl = unsafe { Matrix::new_uninitialized_generic(nrows, ncols) };
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
N::xgeev(
|
|
|
|
ljob,
|
|
|
|
rjob,
|
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
&mut vl.as_mut_slice(),
|
|
|
|
n as i32,
|
|
|
|
&mut placeholder2,
|
|
|
|
1 as i32,
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
if wi.iter().all(|e| e.is_zero()) {
|
2019-02-17 05:29:41 +08:00
|
|
|
return Some(Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
eigenvalues: wr,
|
|
|
|
left_eigenvectors: Some(vl),
|
|
|
|
eigenvectors: None,
|
2017-08-03 01:38:28 +08:00
|
|
|
});
|
|
|
|
}
|
2018-02-02 19:26:35 +08:00
|
|
|
}
|
2017-08-03 01:38:28 +08:00
|
|
|
(false, true) => {
|
|
|
|
let mut vr = unsafe { Matrix::new_uninitialized_generic(nrows, ncols) };
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
N::xgeev(
|
|
|
|
ljob,
|
|
|
|
rjob,
|
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
&mut placeholder1,
|
|
|
|
1 as i32,
|
|
|
|
&mut vr.as_mut_slice(),
|
|
|
|
n as i32,
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
if wi.iter().all(|e| e.is_zero()) {
|
2019-02-17 05:29:41 +08:00
|
|
|
return Some(Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
eigenvalues: wr,
|
|
|
|
left_eigenvectors: None,
|
|
|
|
eigenvectors: Some(vr),
|
2017-08-03 01:38:28 +08:00
|
|
|
});
|
|
|
|
}
|
2018-02-02 19:26:35 +08:00
|
|
|
}
|
2017-08-03 01:38:28 +08:00
|
|
|
(false, false) => {
|
2018-02-02 19:26:35 +08:00
|
|
|
N::xgeev(
|
|
|
|
ljob,
|
|
|
|
rjob,
|
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
&mut placeholder1,
|
|
|
|
1 as i32,
|
|
|
|
&mut placeholder2,
|
|
|
|
1 as i32,
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
if wi.iter().all(|e| e.is_zero()) {
|
2019-02-17 05:29:41 +08:00
|
|
|
return Some(Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
eigenvalues: wr,
|
|
|
|
left_eigenvectors: None,
|
|
|
|
eigenvectors: None,
|
2017-08-03 01:38:28 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-08-07 01:41:33 +08:00
|
|
|
/// The complex eigenvalues of the given matrix.
|
|
|
|
///
|
|
|
|
/// Panics if the eigenvalue computation does not converge.
|
|
|
|
pub fn complex_eigenvalues(mut m: MatrixN<N, D>) -> VectorN<Complex<N>, D>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<Complex<N>, D>,
|
|
|
|
{
|
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:33 +08:00
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
let nrows = m.data.shape().0;
|
2017-08-07 01:41:33 +08:00
|
|
|
let n = nrows.value();
|
|
|
|
|
|
|
|
let lda = n as i32;
|
|
|
|
|
|
|
|
let mut wr = unsafe { Matrix::new_uninitialized_generic(nrows, U1) };
|
|
|
|
let mut wi = unsafe { Matrix::new_uninitialized_generic(nrows, U1) };
|
|
|
|
|
|
|
|
let mut info = 0;
|
2018-02-02 19:26:35 +08:00
|
|
|
let mut placeholder1 = [N::zero()];
|
|
|
|
let mut placeholder2 = [N::zero()];
|
|
|
|
|
|
|
|
let lwork = N::xgeev_work_size(
|
|
|
|
b'N',
|
|
|
|
b'N',
|
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
&mut placeholder1,
|
|
|
|
n as i32,
|
|
|
|
&mut placeholder2,
|
|
|
|
n as i32,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-07 01:41:33 +08:00
|
|
|
|
|
|
|
lapack_panic!(info);
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
let mut work = unsafe { crate::uninitialized_vec(lwork as usize) };
|
2017-08-07 01:41:33 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
N::xgeev(
|
|
|
|
b'N',
|
|
|
|
b'N',
|
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
wr.as_mut_slice(),
|
|
|
|
wi.as_mut_slice(),
|
|
|
|
&mut placeholder1,
|
|
|
|
1 as i32,
|
|
|
|
&mut placeholder2,
|
|
|
|
1 as i32,
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-07 01:41:33 +08:00
|
|
|
lapack_panic!(info);
|
|
|
|
|
|
|
|
let mut res = unsafe { Matrix::new_uninitialized_generic(nrows, U1) };
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
for i in 0..res.len() {
|
2017-08-07 01:41:33 +08:00
|
|
|
res[i] = Complex::new(wr[i], wi[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:38:28 +08:00
|
|
|
/// The determinant of the decomposed matrix.
|
|
|
|
#[inline]
|
|
|
|
pub fn determinant(&self) -> N {
|
|
|
|
let mut det = N::one();
|
|
|
|
for e in self.eigenvalues.iter() {
|
|
|
|
det *= *e;
|
|
|
|
}
|
|
|
|
|
|
|
|
det
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Lapack functions dispatch.
|
|
|
|
*
|
|
|
|
*/
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Trait implemented by scalar type for which Lapack function exist to compute the
|
2017-08-14 01:52:58 +08:00
|
|
|
/// eigendecomposition.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub trait EigenScalar: Scalar {
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgeev(
|
|
|
|
jobvl: u8,
|
|
|
|
jobvr: u8,
|
|
|
|
n: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
wr: &mut [Self],
|
|
|
|
wi: &mut [Self],
|
|
|
|
vl: &mut [Self],
|
|
|
|
ldvl: i32,
|
|
|
|
vr: &mut [Self],
|
|
|
|
ldvr: i32,
|
|
|
|
work: &mut [Self],
|
|
|
|
lwork: i32,
|
|
|
|
info: &mut i32,
|
|
|
|
);
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xgeev_work_size(
|
|
|
|
jobvl: u8,
|
|
|
|
jobvr: u8,
|
|
|
|
n: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
wr: &mut [Self],
|
|
|
|
wi: &mut [Self],
|
|
|
|
vl: &mut [Self],
|
|
|
|
ldvl: i32,
|
|
|
|
vr: &mut [Self],
|
|
|
|
ldvr: i32,
|
|
|
|
info: &mut i32,
|
|
|
|
) -> i32;
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! real_eigensystem_scalar_impl (
|
|
|
|
($N: ty, $xgeev: path) => (
|
2017-08-07 01:41:33 +08:00
|
|
|
impl EigenScalar for $N {
|
2017-08-03 01:38:28 +08:00
|
|
|
#[inline]
|
|
|
|
fn xgeev(jobvl: u8, jobvr: u8, n: i32, a: &mut [Self], lda: i32,
|
|
|
|
wr: &mut [Self], wi: &mut [Self],
|
|
|
|
vl: &mut [Self], ldvl: i32, vr: &mut [Self], ldvr: i32,
|
|
|
|
work: &mut [Self], lwork: i32, info: &mut i32) {
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xgeev(jobvl, jobvr, n, a, lda, wr, wi, vl, ldvl, vr, ldvr, work, lwork, info) }
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn xgeev_work_size(jobvl: u8, jobvr: u8, n: i32, a: &mut [Self], lda: i32,
|
|
|
|
wr: &mut [Self], wi: &mut [Self], vl: &mut [Self], ldvl: i32,
|
|
|
|
vr: &mut [Self], ldvr: i32, info: &mut i32) -> i32 {
|
|
|
|
let mut work = [ Zero::zero() ];
|
|
|
|
let lwork = -1 as i32;
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xgeev(jobvl, jobvr, n, a, lda, wr, wi, vl, ldvl, vr, ldvr, &mut work, lwork, info) };
|
2017-08-03 01:38:28 +08:00
|
|
|
ComplexHelper::real_part(work[0]) as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
real_eigensystem_scalar_impl!(f32, lapack::sgeev);
|
|
|
|
real_eigensystem_scalar_impl!(f64, lapack::dgeev);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
//// TODO: decomposition of complex matrix and matrices with complex eigenvalues.
|
2018-05-25 05:51:57 +08:00
|
|
|
// eigensystem_complex_impl!(f32, lapack::cgeev);
|
|
|
|
// eigensystem_complex_impl!(f64, lapack::zgeev);
|