|
| 1 | +//! Singular-value decomposition (SVD) by divide-and-conquer (?gesdd) |
| 2 | +
|
| 3 | +use ndarray::*; |
| 4 | + |
| 5 | +use super::convert::*; |
| 6 | +use super::error::*; |
| 7 | +use super::layout::*; |
| 8 | +use super::types::*; |
| 9 | + |
| 10 | +#[derive(Clone, Copy, Eq, PartialEq)] |
| 11 | +#[repr(u8)] |
| 12 | +pub enum UVTFlag { |
| 13 | + Full = b'A', |
| 14 | + Some = b'S', |
| 15 | + None = b'N', |
| 16 | +} |
| 17 | + |
| 18 | +/// Singular-value decomposition of matrix (copying) by divide-and-conquer |
| 19 | +pub trait SVDDC { |
| 20 | + type U; |
| 21 | + type VT; |
| 22 | + type Sigma; |
| 23 | + fn svddc(&self, uvt_flag: UVTFlag) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)>; |
| 24 | +} |
| 25 | + |
| 26 | +/// Singular-value decomposition of matrix by divide-and-conquer |
| 27 | +pub trait SVDDCInto { |
| 28 | + type U; |
| 29 | + type VT; |
| 30 | + type Sigma; |
| 31 | + fn svddc_into( |
| 32 | + self, |
| 33 | + uvt_flag: UVTFlag, |
| 34 | + ) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)>; |
| 35 | +} |
| 36 | + |
| 37 | +/// Singular-value decomposition of matrix reference by divide-and-conquer |
| 38 | +pub trait SVDDCInplace { |
| 39 | + type U; |
| 40 | + type VT; |
| 41 | + type Sigma; |
| 42 | + fn svddc_inplace( |
| 43 | + &mut self, |
| 44 | + uvt_flag: UVTFlag, |
| 45 | + ) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)>; |
| 46 | +} |
| 47 | + |
| 48 | +impl<A, S> SVDDC for ArrayBase<S, Ix2> |
| 49 | +where |
| 50 | + A: Scalar + Lapack, |
| 51 | + S: DataMut<Elem = A>, |
| 52 | +{ |
| 53 | + type U = Array2<A>; |
| 54 | + type VT = Array2<A>; |
| 55 | + type Sigma = Array1<A::Real>; |
| 56 | + |
| 57 | + fn svddc(&self, uvt_flag: UVTFlag) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> { |
| 58 | + self.to_owned().svddc_into(uvt_flag) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<A, S> SVDDCInto for ArrayBase<S, Ix2> |
| 63 | +where |
| 64 | + A: Scalar + Lapack, |
| 65 | + S: DataMut<Elem = A>, |
| 66 | +{ |
| 67 | + type U = Array2<A>; |
| 68 | + type VT = Array2<A>; |
| 69 | + type Sigma = Array1<A::Real>; |
| 70 | + |
| 71 | + fn svddc_into( |
| 72 | + mut self, |
| 73 | + uvt_flag: UVTFlag, |
| 74 | + ) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> { |
| 75 | + self.svddc_inplace(uvt_flag) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<A, S> SVDDCInplace for ArrayBase<S, Ix2> |
| 80 | +where |
| 81 | + A: Scalar + Lapack, |
| 82 | + S: DataMut<Elem = A>, |
| 83 | +{ |
| 84 | + type U = Array2<A>; |
| 85 | + type VT = Array2<A>; |
| 86 | + type Sigma = Array1<A::Real>; |
| 87 | + |
| 88 | + fn svddc_inplace( |
| 89 | + &mut self, |
| 90 | + uvt_flag: UVTFlag, |
| 91 | + ) -> Result<(Option<Self::U>, Self::Sigma, Option<Self::VT>)> { |
| 92 | + let l = self.layout()?; |
| 93 | + let svd_res = unsafe { A::svddc(l, uvt_flag, self.as_allocated_mut()?)? }; |
| 94 | + let (m, n) = l.size(); |
| 95 | + let k = m.min(n); |
| 96 | + let (ldu, tdu, ldvt, tdvt) = match uvt_flag { |
| 97 | + UVTFlag::Full => (m, m, n, n), |
| 98 | + UVTFlag::Some => (m, k, k, n), |
| 99 | + UVTFlag::None => (1, 1, 1, 1), |
| 100 | + }; |
| 101 | + let u = svd_res |
| 102 | + .u |
| 103 | + .map(|u| into_matrix(l.resized(ldu, tdu), u).expect("Size of U mismatches")); |
| 104 | + let vt = svd_res |
| 105 | + .vt |
| 106 | + .map(|vt| into_matrix(l.resized(ldvt, tdvt), vt).expect("Size of VT mismatches")); |
| 107 | + let s = ArrayBase::from_vec(svd_res.s); |
| 108 | + Ok((u, s, vt)) |
| 109 | + } |
| 110 | +} |
0 commit comments