@@ -24,7 +24,7 @@ pub struct TruncatedSvdResult<A> {
24
24
}
25
25
26
26
impl < A : Float + PartialOrd + DivAssign < A > + ' static + MagnitudeCorrection > TruncatedSvdResult < A > {
27
- /// Returns singular values ordered by magnitude with indices.
27
+ /// Returns singular values ordered by magnitude with indices
28
28
fn singular_values_with_indices ( & self ) -> ( Array1 < A > , Vec < usize > ) {
29
29
// numerate eigenvalues
30
30
let mut a = self . eigvals . iter ( ) . enumerate ( ) . collect :: < Vec < _ > > ( ) ;
@@ -90,7 +90,7 @@ impl<A: Float + PartialOrd + DivAssign<A> + 'static + MagnitudeCorrection> Trunc
90
90
/// Truncated singular value decomposition
91
91
///
92
92
/// Wraps the LOBPCG algorithm and provides convenient builder-pattern access to
93
- /// parameter like maximal iteration, precision and constraint matrix.
93
+ /// parameter like maximal iteration, precision and contrain matrix.
94
94
pub struct TruncatedSvd < A : Scalar > {
95
95
order : Order ,
96
96
problem : Array2 < A > ,
@@ -99,6 +99,11 @@ pub struct TruncatedSvd<A: Scalar> {
99
99
}
100
100
101
101
impl < A : Float + Scalar + ScalarOperand + Lapack + PartialOrd + Default > TruncatedSvd < A > {
102
+ /// Create a new truncated SVD problem
103
+ ///
104
+ /// # Parameters
105
+ /// * `problem`: rectangular matrix which is decomposed
106
+ /// * `order`: whether to return large or small (close to zero) singular values
102
107
pub fn new ( problem : Array2 < A > , order : Order ) -> TruncatedSvd < A > {
103
108
TruncatedSvd {
104
109
precision : 1e-5 ,
@@ -108,19 +113,48 @@ impl<A: Float + Scalar + ScalarOperand + Lapack + PartialOrd + Default> Truncate
108
113
}
109
114
}
110
115
116
+ /// Set the required precision of the solution
117
+ ///
118
+ /// The precision is, in the context of SVD, the square-root precision of the underlying
119
+ /// eigenproblem solution. The eigenproblem-precision is used to check the L2 error of each
120
+ /// eigenvector and stops its optimization when the required precision is reached.
111
121
pub fn precision ( mut self , precision : f32 ) -> Self {
112
122
self . precision = precision;
113
123
114
124
self
115
125
}
116
126
127
+ /// Set the maximal number of iterations
128
+ ///
129
+ /// The LOBPCG is an iterative approach to eigenproblems and stops when this maximum
130
+ /// number of iterations are reached.
117
131
pub fn maxiter ( mut self , maxiter : usize ) -> Self {
118
132
self . maxiter = maxiter;
119
133
120
134
self
121
135
}
122
136
123
- // calculate the eigenvalue decomposition
137
+ /// Calculate the singular value decomposition
138
+ ///
139
+ /// # Parameters
140
+ ///
141
+ /// * `num`: number of singular-value/vector pairs, ordered by magnitude
142
+ ///
143
+ /// # Example
144
+ ///
145
+ /// ```rust
146
+ /// use ndarray::{arr1, Array2};
147
+ /// use ndarray_linalg::{TruncatedSvd, TruncatedOrder};
148
+ ///
149
+ /// let diag = arr1(&[1., 2., 3., 4., 5.]);
150
+ /// let a = Array2::from_diag(&diag);
151
+ ///
152
+ /// let eig = TruncatedSvd::new(a, TruncatedOrder::Largest)
153
+ /// .precision(1e-5)
154
+ /// .maxiter(500);
155
+ ///
156
+ /// let res = eig.decompose(3);
157
+ /// ```
124
158
pub fn decompose ( self , num : usize ) -> Result < TruncatedSvdResult < A > > {
125
159
if num < 1 {
126
160
panic ! ( "The number of singular values to compute should be larger than zero!" ) ;
0 commit comments