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-06 22:12:05 +08:00
|
|
|
use num::Zero;
|
|
|
|
use std::ops::MulAssign;
|
|
|
|
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::RealField;
|
2017-08-06 22:12:05 +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-06 22:12:05 +08:00
|
|
|
use na::storage::Storage;
|
2021-04-11 17:00:38 +08:00
|
|
|
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};
|
2017-08-06 22:12:05 +08:00
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
use lapack;
|
2017-08-06 22:12:05 +08:00
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Eigendecomposition of a real square symmetric matrix with real eigenvalues.
|
|
|
|
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
2018-05-25 05:51:57 +08:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2021-04-11 17:00:38 +08:00
|
|
|
serde(bound(serialize = "DefaultAllocator: Allocator<T, D, D> +
|
|
|
|
Allocator<T, D>,
|
|
|
|
OVector<T, D>: Serialize,
|
|
|
|
OMatrix<T, D, D>: Serialize"))
|
2018-05-25 05:51:57 +08:00
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2021-04-11 17:00:38 +08:00
|
|
|
serde(bound(deserialize = "DefaultAllocator: Allocator<T, D, D> +
|
|
|
|
Allocator<T, D>,
|
|
|
|
OVector<T, D>: Deserialize<'de>,
|
|
|
|
OMatrix<T, D, D>: Deserialize<'de>"))
|
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 SymmetricEigen<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
|
|
|
{
|
2017-08-14 01:52:58 +08:00
|
|
|
/// The eigenvectors of the decomposed matrix.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub eigenvectors: OMatrix<T, D, D>,
|
2017-08-14 01:52:58 +08:00
|
|
|
|
|
|
|
/// The unsorted eigenvalues of the decomposed matrix.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub eigenvalues: OVector<T, D>,
|
2017-08-06 22:12:05 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Copy, D: Dim> Copy for SymmetricEigen<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-14 01:53:04 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: SymmetricEigenScalar + RealField, D: Dim> SymmetricEigen<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
|
|
|
{
|
2017-08-06 22:12:05 +08:00
|
|
|
/// Computes the eigenvalues and eigenvectors of the symmetric matrix `m`.
|
|
|
|
///
|
|
|
|
/// Only the lower-triangular part of `m` is read. If `eigenvectors` is `false` then, the
|
|
|
|
/// eigenvectors are not computed explicitly. Panics if the method did not converge.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new(m: OMatrix<T, D, D>) -> Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
let (vals, vecs) =
|
|
|
|
Self::do_decompose(m, true).expect("SymmetricEigen: convergence failure.");
|
2019-02-17 05:29:41 +08:00
|
|
|
Self {
|
2018-02-02 19:26:35 +08:00
|
|
|
eigenvalues: vals,
|
|
|
|
eigenvectors: vecs.unwrap(),
|
|
|
|
}
|
2017-08-06 22:12:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the eigenvalues and eigenvectors of the symmetric matrix `m`.
|
|
|
|
///
|
|
|
|
/// Only the lower-triangular part of `m` is read. If `eigenvectors` is `false` then, the
|
|
|
|
/// eigenvectors are not computed explicitly. Returns `None` if the method did not converge.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn try_new(m: OMatrix<T, D, D>) -> Option<Self> {
|
2018-02-02 19:26:35 +08:00
|
|
|
Self::do_decompose(m, true).map(|(vals, vecs)| SymmetricEigen {
|
|
|
|
eigenvalues: vals,
|
|
|
|
eigenvectors: vecs.unwrap(),
|
2017-08-06 22:12:05 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
fn do_decompose(
|
2021-04-11 17:00:38 +08:00
|
|
|
mut m: OMatrix<T, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
eigenvectors: bool,
|
2021-04-11 17:00:38 +08:00
|
|
|
) -> Option<(OVector<T, D>, Option<OMatrix<T, D, 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-06 22:12:05 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let jobz = if eigenvectors { b'V' } else { b'T' };
|
2017-08-06 22:12:05 +08:00
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
let nrows = m.data.shape().0;
|
2017-08-06 22:12:05 +08:00
|
|
|
let n = nrows.value();
|
|
|
|
|
|
|
|
let lda = n as i32;
|
|
|
|
|
2021-04-12 16:34:44 +08:00
|
|
|
let mut values =
|
|
|
|
unsafe { Matrix::new_uninitialized_generic(nrows, Const::<1>).assume_init() };
|
2017-08-06 22:12:05 +08:00
|
|
|
let mut info = 0;
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let lwork = T::xsyev_work_size(jobz, b'L', n as i32, m.as_mut_slice(), lda, &mut info);
|
2017-08-06 22:12:05 +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-06 22:12:05 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xsyev(
|
2018-02-02 19:26:35 +08:00
|
|
|
jobz,
|
|
|
|
b'L',
|
|
|
|
n as i32,
|
|
|
|
m.as_mut_slice(),
|
|
|
|
lda,
|
|
|
|
values.as_mut_slice(),
|
|
|
|
&mut work,
|
|
|
|
lwork,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-06 22:12:05 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
let vectors = if eigenvectors { Some(m) } else { None };
|
|
|
|
Some((values, vectors))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes only the eigenvalues of the input matrix.
|
|
|
|
///
|
|
|
|
/// Panics if the method does not converge.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn eigenvalues(m: OMatrix<T, D, D>) -> OVector<T, D> {
|
2018-02-02 19:26:35 +08:00
|
|
|
Self::do_decompose(m, false)
|
|
|
|
.expect("SymmetricEigen eigenvalues: convergence failure.")
|
|
|
|
.0
|
2017-08-06 22:12:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes only the eigenvalues of the input matrix.
|
|
|
|
///
|
|
|
|
/// Returns `None` if the method does not converge.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn try_eigenvalues(m: OMatrix<T, D, D>) -> Option<OVector<T, D>> {
|
2017-08-06 22:12:05 +08:00
|
|
|
Self::do_decompose(m, false).map(|res| res.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The determinant of the decomposed matrix.
|
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn determinant(&self) -> T {
|
|
|
|
let mut det = T::one();
|
2017-08-06 22:12:05 +08:00
|
|
|
for e in self.eigenvalues.iter() {
|
|
|
|
det *= *e;
|
|
|
|
}
|
|
|
|
|
|
|
|
det
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rebuild the original matrix.
|
|
|
|
///
|
|
|
|
/// This is useful if some of the eigenvalues have been manually modified.
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn recompose(&self) -> OMatrix<T, D, D> {
|
2017-08-06 22:12:05 +08:00
|
|
|
let mut u_t = self.eigenvectors.clone();
|
2018-02-02 19:26:35 +08:00
|
|
|
for i in 0..self.eigenvalues.len() {
|
2017-08-06 22:12:05 +08:00
|
|
|
let val = self.eigenvalues[i];
|
|
|
|
u_t.column_mut(i).mul_assign(val);
|
|
|
|
}
|
|
|
|
u_t.transpose_mut();
|
|
|
|
&self.eigenvectors * u_t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Lapack functions dispatch.
|
|
|
|
*
|
|
|
|
*/
|
2017-08-14 01:52:58 +08:00
|
|
|
/// Trait implemented by scalars for which Lapack implements the eigendecomposition of symmetric
|
|
|
|
/// real matrices.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub trait SymmetricEigenScalar: Scalar {
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xsyev(
|
|
|
|
jobz: u8,
|
|
|
|
uplo: u8,
|
|
|
|
n: i32,
|
|
|
|
a: &mut [Self],
|
|
|
|
lda: i32,
|
|
|
|
w: &mut [Self],
|
|
|
|
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 xsyev_work_size(jobz: u8, uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32)
|
|
|
|
-> i32;
|
2017-08-06 22:12:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! real_eigensystem_scalar_impl (
|
|
|
|
($N: ty, $xsyev: path) => (
|
2017-08-07 01:41:33 +08:00
|
|
|
impl SymmetricEigenScalar for $N {
|
2017-08-06 22:12:05 +08:00
|
|
|
#[inline]
|
|
|
|
fn xsyev(jobz: u8, uplo: u8, n: i32, a: &mut [Self], lda: i32, w: &mut [Self], work: &mut [Self],
|
|
|
|
lwork: i32, info: &mut i32) {
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xsyev(jobz, uplo, n, a, lda, w, work, lwork, info) }
|
2017-08-06 22:12:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn xsyev_work_size(jobz: u8, uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32) -> i32 {
|
|
|
|
let mut work = [ Zero::zero() ];
|
|
|
|
let mut w = [ Zero::zero() ];
|
|
|
|
let lwork = -1 as i32;
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xsyev(jobz, uplo, n, a, lda, &mut w, &mut work, lwork, info); }
|
2017-08-06 22:12:05 +08:00
|
|
|
ComplexHelper::real_part(work[0]) as i32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
real_eigensystem_scalar_impl!(f32, lapack::ssyev);
|
|
|
|
real_eigensystem_scalar_impl!(f64, lapack::dsyev);
|