Skip to content

Commit 13cf2b3

Browse files
committed
Run cargo fmt again
1 parent 4acb85c commit 13cf2b3

File tree

6 files changed

+111
-86
lines changed

6 files changed

+111
-86
lines changed

examples/eigh.rs

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

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub use eigh::*;
7474
pub use generate::*;
7575
pub use inner::*;
7676
pub use layout::*;
77-
pub use lobpcg::{TruncatedEig, TruncatedSvd, TruncatedOrder};
77+
pub use lobpcg::{TruncatedEig, TruncatedOrder, TruncatedSvd};
7878
pub use norm::*;
7979
pub use operator::*;
8080
pub use opnorm::*;

src/lobpcg/eig.rs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
use super::lobpcg::{lobpcg, EigResult, Order};
2+
use crate::{Lapack, Scalar};
13
///! Implements truncated eigenvalue decomposition
24
///
3-
45
use ndarray::prelude::*;
56
use ndarray::stack;
67
use ndarray_rand::rand_distr::Uniform;
78
use ndarray_rand::RandomExt;
89
use num_traits::{Float, NumCast};
9-
use crate::{Scalar, Lapack};
10-
use super::lobpcg::{lobpcg, EigResult, Order};
1110

1211
/// Truncated eigenproblem solver
1312
///
@@ -21,7 +20,7 @@ pub struct TruncatedEig<A: Scalar> {
2120
pub constraints: Option<Array2<A>>,
2221
preconditioner: Option<Array2<A>>,
2322
precision: A::Real,
24-
maxiter: usize
23+
maxiter: usize,
2524
}
2625

2726
impl<A: Scalar + Lapack + PartialOrd + Default> TruncatedEig<A> {
@@ -31,8 +30,8 @@ impl<A: Scalar + Lapack + PartialOrd + Default> TruncatedEig<A> {
3130
maxiter: problem.len_of(Axis(0)) * 2,
3231
preconditioner: None,
3332
constraints: None,
34-
order,
35-
problem
33+
order,
34+
problem,
3635
}
3736
}
3837

@@ -46,7 +45,6 @@ impl<A: Scalar + Lapack + PartialOrd + Default> TruncatedEig<A> {
4645
self.maxiter = maxiter;
4746

4847
self
49-
5048
}
5149

5250
pub fn orthogonal_to(mut self, constraints: Array2<A>) -> Self {
@@ -66,18 +64,26 @@ impl<A: Scalar + Lapack + PartialOrd + Default> TruncatedEig<A> {
6664
let x = Array2::random((self.problem.len_of(Axis(0)), num), Uniform::new(0.0, 1.0))
6765
.mapv(|x| NumCast::from(x).unwrap());
6866

69-
lobpcg(|y| self.problem.dot(&y), x, self.preconditioner.clone(), self.constraints.clone(), self.precision, self.maxiter, self.order.clone())
67+
lobpcg(
68+
|y| self.problem.dot(&y),
69+
x,
70+
self.preconditioner.clone(),
71+
self.constraints.clone(),
72+
self.precision,
73+
self.maxiter,
74+
self.order.clone(),
75+
)
7076
}
7177
}
7278

7379
impl<A: Float + Scalar + Lapack + PartialOrd + Default> IntoIterator for TruncatedEig<A> {
7480
type Item = (Array1<A>, Array2<A>);
7581
type IntoIter = TruncatedEigIterator<A>;
7682

77-
fn into_iter(self) -> TruncatedEigIterator<A>{
83+
fn into_iter(self) -> TruncatedEigIterator<A> {
7884
TruncatedEigIterator {
7985
step_size: 1,
80-
eig: self
86+
eig: self,
8187
}
8288
}
8389
}
@@ -88,7 +94,7 @@ impl<A: Float + Scalar + Lapack + PartialOrd + Default> IntoIterator for Truncat
8894
/// eigenvalue/vector pair. Useful for generating pairs until a certain condition is met.
8995
pub struct TruncatedEigIterator<A: Scalar> {
9096
step_size: usize,
91-
eig: TruncatedEig<A>
97+
eig: TruncatedEig<A>,
9298
}
9399

94100
impl<A: Float + Scalar + Lapack + PartialOrd + Default> Iterator for TruncatedEigIterator<A> {
@@ -108,7 +114,9 @@ impl<A: Float + Scalar + Lapack + PartialOrd + Default> Iterator for TruncatedEi
108114

109115
// add the new eigenvector to the internal constrain matrix
110116
let new_constraints = if let Some(ref constraints) = self.eig.constraints {
111-
let eigvecs_arr = constraints.gencolumns().into_iter()
117+
let eigvecs_arr = constraints
118+
.gencolumns()
119+
.into_iter()
112120
.chain(vecs.gencolumns().into_iter())
113121
.map(|x| x.insert_axis(Axis(1)))
114122
.collect::<Vec<_>>();
@@ -121,16 +129,16 @@ impl<A: Float + Scalar + Lapack + PartialOrd + Default> Iterator for TruncatedEi
121129
self.eig.constraints = Some(new_constraints);
122130

123131
Some((vals, vecs))
124-
},
125-
EigResult::NoResult(_) => None
132+
}
133+
EigResult::NoResult(_) => None,
126134
}
127135
}
128136
}
129137

130138
#[cfg(test)]
131139
mod tests {
132-
use super::TruncatedEig;
133140
use super::Order;
141+
use super::TruncatedEig;
134142
use ndarray::{arr1, Array2};
135143

136144
#[test]
@@ -140,13 +148,18 @@ mod tests {
140148
]);
141149
let a = Array2::from_diag(&diag);
142150

143-
let teig = TruncatedEig::new(a, Order::Largest)
144-
.precision(1e-5)
145-
.maxiter(500);
146-
151+
let teig = TruncatedEig::new(a, Order::Largest).precision(1e-5).maxiter(500);
152+
147153
let res = teig.into_iter().take(3).flat_map(|x| x.0.to_vec()).collect::<Vec<_>>();
148154
let ground_truth = vec![20., 19., 18.];
149155

150-
assert!(ground_truth.into_iter().zip(res.into_iter()).map(|(x,y)| (x-y)*(x-y)).sum::<f64>() < 0.01);
156+
assert!(
157+
ground_truth
158+
.into_iter()
159+
.zip(res.into_iter())
160+
.map(|(x, y)| (x - y) * (x - y))
161+
.sum::<f64>()
162+
< 0.01
163+
);
151164
}
152165
}

src/lobpcg/lobpcg.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
///! Locally Optimal Block Preconditioned Conjugated
22
///!
33
///! This module implements the Locally Optimal Block Preconditioned Conjugated (LOBPCG) algorithm,
4-
///which can be used as a solver for large symmetric positive definite eigenproblems.
5-
4+
///which can be used as a solver for large symmetric positive definite eigenproblems.
65
use crate::error::{LinalgError, Result};
76
use crate::{cholesky::*, close_l2, eigh::*, norm::*, triangular::*};
87
use crate::{Lapack, Scalar};
@@ -145,7 +144,7 @@ fn orthonormalize<T: Scalar + Lapack>(v: Array2<T>) -> Result<(Array2<T>, Array2
145144
/// approximates the inverse of `a`.
146145
/// * `y` - Constraints of (n,size_y), iterations are performed in the orthogonal complement of the
147146
/// column-space of `y`. It must be full rank.
148-
/// * `tol` - The tolerance values defines at which point the solver stops the optimization. The l2-norm
147+
/// * `tol` - The tolerance values defines at which point the solver stops the optimization. The l2-norm
149148
/// of the residual is compared to this value and the eigenvalue approximation returned if below
150149
/// the threshold.
151150
/// * `maxiter` - The maximal number of iterations
@@ -188,8 +187,8 @@ pub fn lobpcg<A: Scalar + Lapack + PartialOrd + Default, F: Fn(ArrayView2<A>) ->
188187

189188
apply_constraints(x.view_mut(), &fact_yy, y.view());
190189
Some(fact_yy)
191-
},
192-
None => None
190+
}
191+
None => None,
193192
};
194193

195194
// orthonormalize the initial guess and calculate matrices AX and XAX
@@ -477,7 +476,7 @@ mod tests {
477476
if ground_truth_eigvals.len() == num {
478477
close_l2(&Array1::from(ground_truth_eigvals.to_vec()), &vals, 5e-2)
479478
}
480-
},
479+
}
481480
EigResult::NoResult(err) => panic!("Did not converge: {:?}", err),
482481
}
483482
}
@@ -490,7 +489,7 @@ mod tests {
490489
]);
491490
let a = Array2::from_diag(&diag);
492491

493-
check_eigenvalues(&a, Order::Largest, 3, &[20.,19.,18.]);
492+
check_eigenvalues(&a, Order::Largest, 3, &[20., 19., 18.]);
494493
check_eigenvalues(&a, Order::Smallest, 3, &[1., 2., 3.]);
495494
}
496495

@@ -513,12 +512,10 @@ mod tests {
513512

514513
#[test]
515514
fn test_eigsolver_constrainted() {
516-
let diag = arr1(&[
517-
1., 2., 3., 4., 5., 6., 7., 8., 9., 10.
518-
]);
515+
let diag = arr1(&[1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]);
519516
let a = Array2::from_diag(&diag);
520517
let x: Array2<f64> = Array2::random((10, 1), Uniform::new(0.0, 1.0));
521-
let y: Array2<f64> = arr2(&[[1.0,0.,0.,0.,0.,0.,0.,0.,0.,0.]]).reversed_axes();
518+
let y: Array2<f64> = arr2(&[[1.0, 0., 0., 0., 0., 0., 0., 0., 0., 0.]]).reversed_axes();
522519

523520
let result = lobpcg(|y| a.dot(&y), x, None, Some(y), 1e-10, 100, Order::Smallest);
524521
dbg!(&result);
@@ -535,8 +532,12 @@ mod tests {
535532

536533
// should be the second eigenvalue
537534
close_l2(&vals, &Array1::from(vec![2.0]), 1e-2);
538-
close_l2(&vecs.column(0).mapv(|x| x.abs()), &arr1(&[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]), 1e-5);
539-
},
535+
close_l2(
536+
&vecs.column(0).mapv(|x| x.abs()),
537+
&arr1(&[0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
538+
1e-5,
539+
);
540+
}
540541
EigResult::NoResult(err) => panic!("Did not converge: {:?}", err),
541542
}
542543
}

src/lobpcg/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
mod lobpcg;
21
mod eig;
2+
mod lobpcg;
33
mod svd;
44

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

0 commit comments

Comments
 (0)