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::Signed;
|
2018-05-25 05:51:57 +08:00
|
|
|
use std::cmp;
|
2017-08-03 01:38:28 +08:00
|
|
|
|
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, DimMin, DimMinimum, U1};
|
2021-04-11 17:00:38 +08:00
|
|
|
use na::{DefaultAllocator, Matrix, OMatrix, OVector, Scalar};
|
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
|
|
|
|
|
|
|
/// The SVD decomposition of a general matrix.
|
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",
|
2021-04-11 17:00:38 +08:00
|
|
|
serde(bound(serialize = "DefaultAllocator: Allocator<T, DimMinimum<R, C>> +
|
|
|
|
Allocator<T, R, R> +
|
|
|
|
Allocator<T, C, C>,
|
|
|
|
OMatrix<T, R>: Serialize,
|
|
|
|
OMatrix<T, C>: Serialize,
|
|
|
|
OVector<T, DimMinimum<R, C>>: Serialize"))
|
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, DimMinimum<R, C>> +
|
|
|
|
Allocator<T, R, R> +
|
|
|
|
Allocator<T, C, C>,
|
|
|
|
OMatrix<T, R>: Deserialize<'de>,
|
|
|
|
OMatrix<T, C>: Deserialize<'de>,
|
|
|
|
OVector<T, DimMinimum<R, C>>: 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 SVD<T: Scalar, R: DimMin<C>, C: Dim>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, R, R> + Allocator<T, DimMinimum<R, C>> + Allocator<T, C, C>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-14 01:52:58 +08:00
|
|
|
/// The left-singular vectors `U` of this SVD.
|
2021-04-12 16:34:44 +08:00
|
|
|
pub u: OMatrix<T, R, R>, // TODO: should be OMatrix<T, R, DimMinimum<R, C>>
|
2017-08-14 01:52:58 +08:00
|
|
|
/// The right-singular vectors `V^t` of this SVD.
|
2021-04-12 16:34:44 +08:00
|
|
|
pub vt: OMatrix<T, C, C>, // TODO: should be OMatrix<T, DimMinimum<R, C>, C>
|
2017-08-14 01:52:58 +08:00
|
|
|
/// The singular values of this SVD.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub singular_values: OVector<T, DimMinimum<R, C>>,
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Copy, R: DimMin<C>, C: Dim> Copy for SVD<T, R, C>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, C, C> + Allocator<T, R, R> + Allocator<T, DimMinimum<R, C>>,
|
|
|
|
OMatrix<T, R, R>: Copy,
|
|
|
|
OMatrix<T, C, C>: Copy,
|
|
|
|
OVector<T, DimMinimum<R, C>>: Copy,
|
2020-04-06 00:49:48 +08:00
|
|
|
{
|
|
|
|
}
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
/// Trait implemented by floats (`f32`, `f64`) and complex floats (`Complex<f32>`, `Complex<f64>`)
|
|
|
|
/// supported by the Singular Value Decompotition.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub trait SVDScalar<R: DimMin<C>, C: Dim>: Scalar
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<Self, R, R>
|
2018-02-02 19:26:35 +08:00
|
|
|
+ Allocator<Self, R, C>
|
|
|
|
+ Allocator<Self, DimMinimum<R, C>>
|
2020-04-06 00:49:48 +08:00
|
|
|
+ Allocator<Self, C, C>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-03 01:38:28 +08:00
|
|
|
/// Computes the SVD decomposition of `m`.
|
2021-04-11 17:00:38 +08:00
|
|
|
fn compute(m: OMatrix<Self, R, C>) -> Option<SVD<Self, R, C>>;
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: SVDScalar<R, C>, R: DimMin<C>, C: Dim> SVD<T, R, C>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, R, R>
|
|
|
|
+ Allocator<T, R, C>
|
|
|
|
+ Allocator<T, DimMinimum<R, C>>
|
|
|
|
+ Allocator<T, C, C>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-14 01:52:58 +08:00
|
|
|
/// Computes the Singular Value Decomposition of `matrix`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new(m: OMatrix<T, R, C>) -> Option<Self> {
|
|
|
|
T::compute(m)
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! svd_impl(
|
|
|
|
($t: ty, $lapack_func: path) => (
|
|
|
|
impl<R: Dim, C: Dim> SVDScalar<R, C> for $t
|
|
|
|
where R: DimMin<C>,
|
|
|
|
DefaultAllocator: Allocator<$t, R, C> +
|
|
|
|
Allocator<$t, R, R> +
|
|
|
|
Allocator<$t, C, C> +
|
|
|
|
Allocator<$t, DimMinimum<R, C>> {
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
fn compute(mut m: OMatrix<$t, R, C>) -> Option<SVD<$t, R, C>> {
|
2021-08-03 00:41:46 +08:00
|
|
|
let (nrows, ncols) = m.shape_generic();
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
if nrows.value() == 0 || ncols.value() == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let job = b'A';
|
|
|
|
|
|
|
|
let lda = nrows.value() as i32;
|
|
|
|
|
2021-08-03 23:39:45 +08:00
|
|
|
let mut u = Matrix::zeros_generic(nrows, nrows);
|
|
|
|
let mut s = Matrix::zeros_generic(nrows.min(ncols), Const::<1>);
|
|
|
|
let mut vt = Matrix::zeros_generic(ncols, ncols);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
let ldu = nrows.value();
|
|
|
|
let ldvt = ncols.value();
|
|
|
|
|
|
|
|
let mut work = [ 0.0 ];
|
|
|
|
let mut lwork = -1 as i32;
|
|
|
|
let mut info = 0;
|
2021-08-03 23:39:45 +08:00
|
|
|
let mut iwork = vec![0; 8 * cmp::min(nrows.value(), ncols.value())];
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe {
|
|
|
|
$lapack_func(job, nrows.value() as i32, ncols.value() as i32, m.as_mut_slice(),
|
2017-08-03 01:38:28 +08:00
|
|
|
lda, &mut s.as_mut_slice(), u.as_mut_slice(), ldu as i32, vt.as_mut_slice(),
|
|
|
|
ldvt as i32, &mut work, lwork, &mut iwork, &mut info);
|
2018-05-25 05:51:57 +08:00
|
|
|
}
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
lwork = work[0] as i32;
|
2021-08-03 23:39:45 +08:00
|
|
|
let mut work = vec![0.0; lwork as usize];
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe {
|
2017-08-03 01:38:28 +08:00
|
|
|
$lapack_func(job, nrows.value() as i32, ncols.value() as i32, m.as_mut_slice(),
|
|
|
|
lda, &mut s.as_mut_slice(), u.as_mut_slice(), ldu as i32, vt.as_mut_slice(),
|
|
|
|
ldvt as i32, &mut work, lwork, &mut iwork, &mut info);
|
2018-05-25 05:51:57 +08:00
|
|
|
}
|
|
|
|
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
Some(SVD { u: u, singular_values: s, vt: vt })
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: DimMin<C>, C: Dim> SVD<$t, R, C>
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: All those bounds…
|
2017-08-03 01:38:28 +08:00
|
|
|
where DefaultAllocator: Allocator<$t, R, C> +
|
|
|
|
Allocator<$t, C, R> +
|
|
|
|
Allocator<$t, U1, R> +
|
|
|
|
Allocator<$t, U1, C> +
|
|
|
|
Allocator<$t, R, R> +
|
|
|
|
Allocator<$t, DimMinimum<R, C>> +
|
|
|
|
Allocator<$t, DimMinimum<R, C>, R> +
|
|
|
|
Allocator<$t, DimMinimum<R, C>, C> +
|
|
|
|
Allocator<$t, R, DimMinimum<R, C>> +
|
|
|
|
Allocator<$t, C, C> {
|
|
|
|
/// Reconstructs the matrix from its decomposition.
|
|
|
|
///
|
|
|
|
/// Useful if some components (e.g. some singular values) of this decomposition have
|
|
|
|
/// been manually changed by the user.
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn recompose(self) -> OMatrix<$t, R, C> {
|
2021-08-03 00:41:46 +08:00
|
|
|
let nrows = self.u.shape_generic().0;
|
|
|
|
let ncols = self.vt.shape_generic().1;
|
2017-08-03 01:38:28 +08:00
|
|
|
let min_nrows_ncols = nrows.min(ncols);
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut res: OMatrix<_, R, C> = Matrix::zeros_generic(nrows, ncols);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
{
|
2022-11-14 22:21:20 +08:00
|
|
|
let mut sres = res.generic_view_mut((0, 0), (min_nrows_ncols, ncols));
|
2017-08-03 01:38:28 +08:00
|
|
|
sres.copy_from(&self.vt.rows_generic(0, min_nrows_ncols));
|
|
|
|
|
|
|
|
for i in 0 .. min_nrows_ncols.value() {
|
2017-08-14 01:52:58 +08:00
|
|
|
let eigval = self.singular_values[i];
|
2017-08-03 01:38:28 +08:00
|
|
|
let mut row = sres.row_mut(i);
|
|
|
|
row *= eigval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
self.u * res
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the pseudo-inverse of the decomposed matrix.
|
|
|
|
///
|
2017-10-27 12:13:35 +08:00
|
|
|
/// All singular value below epsilon will be set to zero instead of being inverted.
|
2017-08-03 01:38:28 +08:00
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn pseudo_inverse(&self, epsilon: $t) -> OMatrix<$t, C, R> {
|
2021-08-03 00:41:46 +08:00
|
|
|
let nrows = self.u.shape_generic().0;
|
|
|
|
let ncols = self.vt.shape_generic().1;
|
2017-08-03 01:38:28 +08:00
|
|
|
let min_nrows_ncols = nrows.min(ncols);
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut res: OMatrix<_, C, R> = Matrix::zeros_generic(ncols, nrows);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
{
|
2022-11-14 22:21:20 +08:00
|
|
|
let mut sres = res.generic_view_mut((0, 0), (min_nrows_ncols, nrows));
|
2017-08-03 01:38:28 +08:00
|
|
|
self.u.columns_generic(0, min_nrows_ncols).transpose_to(&mut sres);
|
|
|
|
|
|
|
|
for i in 0 .. min_nrows_ncols.value() {
|
2017-08-14 01:52:58 +08:00
|
|
|
let eigval = self.singular_values[i];
|
2017-08-03 01:38:28 +08:00
|
|
|
let mut row = sres.row_mut(i);
|
|
|
|
|
|
|
|
if eigval.abs() > epsilon {
|
|
|
|
row /= eigval
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
row.fill(0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.vt.tr_mul(&res)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The rank of the decomposed matrix.
|
|
|
|
///
|
|
|
|
/// This is the number of singular values that are not too small (i.e. greater than
|
|
|
|
/// the given `epsilon`).
|
|
|
|
#[inline]
|
2021-06-07 22:34:03 +08:00
|
|
|
#[must_use]
|
2017-08-03 01:38:28 +08:00
|
|
|
pub fn rank(&self, epsilon: $t) -> usize {
|
|
|
|
let mut i = 0;
|
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
for e in self.singular_values.as_slice().iter() {
|
2017-08-03 01:38:28 +08:00
|
|
|
if e.abs() > epsilon {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: add methods to retrieve the null-space and column-space? (Respectively
|
2017-08-03 01:38:28 +08:00
|
|
|
// corresponding to the zero and non-zero singular values).
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
macro_rules! svd_complex_impl(
|
|
|
|
($name: ident, $t: ty, $lapack_func: path) => (
|
|
|
|
impl SVDScalar for Complex<$t> {
|
|
|
|
fn compute<R: Dim, C: Dim, S>(mut m: Matrix<$t, R, C, S>) -> Option<SVD<$t, R, C, S::Alloc>>
|
2021-04-11 17:00:38 +08:00
|
|
|
Option<(OMatrix<Complex<$t>, R, S::Alloc>,
|
|
|
|
OVector<$t, DimMinimum<R, C>, S::Alloc>,
|
|
|
|
OMatrix<Complex<$t>, C, S::Alloc>)>
|
2017-08-03 01:38:28 +08:00
|
|
|
where R: DimMin<C>,
|
|
|
|
S: ContiguousStorage<Complex<$t>, R, C>,
|
|
|
|
S::Alloc: OwnedAllocator<Complex<$t>, R, C, S> +
|
|
|
|
Allocator<Complex<$t>, R, R> +
|
|
|
|
Allocator<Complex<$t>, C, C> +
|
|
|
|
Allocator<$t, DimMinimum<R, C>> {
|
2021-08-03 00:41:46 +08:00
|
|
|
let (nrows, ncols) = m.shape_generic();
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
if nrows.value() == 0 || ncols.value() == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let jobu = b'A';
|
|
|
|
let jobvt = b'A';
|
|
|
|
|
|
|
|
let lda = nrows.value() as i32;
|
|
|
|
let min_nrows_ncols = nrows.min(ncols);
|
|
|
|
|
|
|
|
|
2021-08-03 23:39:45 +08:00
|
|
|
let mut u = Matrix::zeros_generic(nrows, nrows);
|
|
|
|
let mut s = Matrix::zeros_generic(min_nrows_ncols, U1);
|
|
|
|
let mut vt = Matrix::zeros_generic(ncols, ncols);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
let ldu = nrows.value();
|
|
|
|
let ldvt = ncols.value();
|
|
|
|
|
|
|
|
let mut work = [ Complex::new(0.0, 0.0) ];
|
|
|
|
let mut lwork = -1 as i32;
|
|
|
|
let mut rwork = vec![ 0.0; (5 * min_nrows_ncols.value()) ];
|
|
|
|
let mut info = 0;
|
|
|
|
|
|
|
|
$lapack_func(jobu, jobvt, nrows.value() as i32, ncols.value() as i32, m.as_mut_slice(),
|
|
|
|
lda, s.as_mut_slice(), u.as_mut_slice(), ldu as i32, vt.as_mut_slice(),
|
|
|
|
ldvt as i32, &mut work, lwork, &mut rwork, &mut info);
|
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
lwork = work[0].re as i32;
|
|
|
|
let mut work = vec![Complex::new(0.0, 0.0); lwork as usize];
|
|
|
|
|
|
|
|
$lapack_func(jobu, jobvt, nrows.value() as i32, ncols.value() as i32, m.as_mut_slice(),
|
|
|
|
lda, s.as_mut_slice(), u.as_mut_slice(), ldu as i32, vt.as_mut_slice(),
|
|
|
|
ldvt as i32, &mut work, lwork, &mut rwork, &mut info);
|
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
Some((u, s, vt))
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
svd_impl!(f32, lapack::sgesdd);
|
|
|
|
svd_impl!(f64, lapack::dgesdd);
|
|
|
|
// svd_complex_impl!(lapack_svd_complex_f32, f32, lapack::cgesvd);
|
|
|
|
// svd_complex_impl!(lapack_svd_complex_f64, f64, lapack::zgesvd);
|