1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use ndarray::prelude::*;
use num_traits::{Float, ToPrimitive};
use std::fmt::Display;
use ndarray::{Ix1, Ix2};

/// Trait for singular values
///
/// A type implementing `SingularValue` can be returned as a singular
/// value by `SVD::compute*`.
pub trait SingularValue: Float + Display + ToPrimitive {}

impl<T: Float + Display + ToPrimitive> SingularValue for T {}


/// A solution to the singular value decomposition.
///
/// A singular value decomposition solution includes singular values
/// and, optionally, the left and right singular vectors, stored as a
/// mtrix.
pub struct SVDSolution<IV: Sized, SV: Sized> {
    /// Singular values of the matrix.
    ///
    /// Singular values, which are guaranteed to be non-negative
    /// reals, are returned in descending order.
    pub values: Array<SV, Ix1>,

    /// The matrix `U` of left singular vectors.
    pub left_vectors: Option<Array<IV, Ix2>>,

    /// The matrix V^t of singular vectors.
    ///
    /// The transpose of V is stored, not V itself.
    pub right_vectors: Option<Array<IV, Ix2>>,
}

/// An error resulting from a `SVD::compute*` method.
#[derive(Debug)]
pub enum SVDError {
    Unconverged,
    IllegalParameter(i32),
    BadLayout,
}