Skip to content

Commit 4acb85c

Browse files
committed
Add example for truncated SVD
1 parent 3334034 commit 4acb85c

File tree

5 files changed

+12
-10
lines changed

5 files changed

+12
-10
lines changed

examples/eigh.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ fn main() {
1212
let av = a.dot(&vecs);
1313
println!("AV = \n{:?}", av);
1414
}
15+

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub use eigh::*;
7474
pub use generate::*;
7575
pub use inner::*;
7676
pub use layout::*;
77+
pub use lobpcg::{TruncatedEig, TruncatedSvd, TruncatedOrder};
7778
pub use norm::*;
7879
pub use operator::*;
7980
pub use opnorm::*;

src/lobpcg/eig.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ impl<A: Scalar + Lapack + PartialOrd + Default> TruncatedEig<A> {
6161
self
6262
}
6363

64-
// calculate the eigenvalues once
65-
pub fn once(&self, num: usize) -> EigResult<A> {
64+
// calculate the eigenvalues decompose
65+
pub fn decompose(&self, num: usize) -> EigResult<A> {
6666
let x = Array2::random((self.problem.len_of(Axis(0)), num), Uniform::new(0.0, 1.0))
6767
.mapv(|x| NumCast::from(x).unwrap());
6868

@@ -95,7 +95,7 @@ impl<A: Float + Scalar + Lapack + PartialOrd + Default> Iterator for TruncatedEi
9595
type Item = (Array1<A>, Array2<A>);
9696

9797
fn next(&mut self) -> Option<Self::Item> {
98-
let res = self.eig.once(self.step_size);
98+
let res = self.eig.decompose(self.step_size);
9999

100100
match res {
101101
EigResult::Ok(vals, vecs, norms) | EigResult::Err(vals, vecs, norms, _) => {

src/lobpcg/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ mod lobpcg;
22
mod eig;
33
mod svd;
44

5-
pub use lobpcg::{lobpcg, EigResult, Order};
5+
pub use lobpcg::{lobpcg, EigResult, Order as TruncatedOrder};
66
pub use eig::TruncatedEig;
77
pub use svd::TruncatedSvd;

src/lobpcg/svd.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<A: Float + PartialOrd + DivAssign<A> + 'static> TruncatedSvdResult<A> {
5151
}
5252

5353
/// Returns singular values, left-singular vectors and right-singular vectors
54-
pub fn values_vecs(&self) -> (Array2<A>, Array1<A>, Array2<A>) {
54+
pub fn values_vectors(&self) -> (Array2<A>, Array1<A>, Array2<A>) {
5555
let (values, indices) = self.singular_values_with_indices();
5656

5757
// branch n > m (for A is [n x m])
@@ -115,8 +115,8 @@ impl<A: Scalar + Lapack + PartialOrd + Default> TruncatedSvd<A> {
115115

116116
}
117117

118-
// calculate the eigenvalues once
119-
pub fn once(self, num: usize) -> Result<TruncatedSvdResult<A>> {
118+
// calculate the eigenvalue decomposition
119+
pub fn decompose(self, num: usize) -> Result<TruncatedSvdResult<A>> {
120120
let (n,m) = (self.problem.nrows(), self.problem.ncols());
121121

122122
let x = Array2::random((usize::min(n,m), num), Uniform::new(0.0, 1.0))
@@ -164,7 +164,7 @@ mod tests {
164164
let res = TruncatedSvd::new(a, Order::Largest)
165165
.precision(1e-5)
166166
.maxiter(10)
167-
.once(2)
167+
.decompose(2)
168168
.unwrap();
169169

170170
let (_, sigma, _) = res.values_vecs();
@@ -179,10 +179,10 @@ mod tests {
179179
let res = TruncatedSvd::new(a.clone(), Order::Largest)
180180
.precision(1e-5)
181181
.maxiter(10)
182-
.once(10)
182+
.decompose(10)
183183
.unwrap();
184184

185-
let (u, sigma, v_t) = res.values_vecs();
185+
let (u, sigma, v_t) = res.values_vectors();
186186
let reconstructed = u.dot(&Array2::from_diag(&sigma).dot(&v_t));
187187

188188
close_l2(&a, &reconstructed, 1e-5);

0 commit comments

Comments
 (0)