Skip to content

Commit e39a17f

Browse files
authored
Merge pull request #194 from rust-ndarray/format
cargo-fmt --check on CI
2 parents 55b46e9 + b729220 commit e39a17f

37 files changed

+706
-190
lines changed

.github/workflows/rust.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,12 @@ jobs:
5555
with:
5656
command: test
5757
args: --features=openblas --no-default-features
58+
59+
check-format:
60+
runs-on: ubuntu-18.04
61+
steps:
62+
- uses: actions/checkout@v1
63+
- uses: actions-rs/cargo@v1
64+
with:
65+
command: fmt
66+
args: -- --check

examples/eig.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ fn main() {
99
let a_c: Array2<c64> = a.map(|f| c64::new(*f, 0.0));
1010
let av = a_c.dot(&vecs);
1111
println!("AV = \n{:?}", av);
12-
}
12+
}

examples/truncated_svd.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ fn main() {
88
let a = arr2(&[[3., 2., 2.], [2., 3., -2.]]);
99

1010
// calculate the truncated singular value decomposition for 2 singular values
11-
let result = TruncatedSvd::new(a, TruncatedOrder::Largest).decompose(2).unwrap();
11+
let result = TruncatedSvd::new(a, TruncatedOrder::Largest)
12+
.decompose(2)
13+
.unwrap();
1214

1315
// acquire singular values, left-singular vectors and right-singular vectors
1416
let (u, sigma, v_t) = result.values_vectors();

rustfmt.toml

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/cholesky.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ where
166166
A: Scalar + Lapack,
167167
S: Data<Elem = A>,
168168
{
169-
fn solvec_inplace<'a, Sb>(&self, b: &'a mut ArrayBase<Sb, Ix1>) -> Result<&'a mut ArrayBase<Sb, Ix1>>
169+
fn solvec_inplace<'a, Sb>(
170+
&self,
171+
b: &'a mut ArrayBase<Sb, Ix1>,
172+
) -> Result<&'a mut ArrayBase<Sb, Ix1>>
170173
where
171174
Sb: DataMut<Elem = A>,
172175
{
@@ -327,7 +330,10 @@ pub trait SolveC<A: Scalar> {
327330
/// Solves a system of linear equations `A * x = b` with Hermitian (or real
328331
/// symmetric) positive definite matrix `A`, where `A` is `self`, `b` is
329332
/// the argument, and `x` is the successful result.
330-
fn solvec_into<S: DataMut<Elem = A>>(&self, mut b: ArrayBase<S, Ix1>) -> Result<ArrayBase<S, Ix1>> {
333+
fn solvec_into<S: DataMut<Elem = A>>(
334+
&self,
335+
mut b: ArrayBase<S, Ix1>,
336+
) -> Result<ArrayBase<S, Ix1>> {
331337
self.solvec_inplace(&mut b)?;
332338
Ok(b)
333339
}
@@ -346,7 +352,10 @@ where
346352
A: Scalar + Lapack,
347353
S: Data<Elem = A>,
348354
{
349-
fn solvec_inplace<'a, Sb>(&self, b: &'a mut ArrayBase<Sb, Ix1>) -> Result<&'a mut ArrayBase<Sb, Ix1>>
355+
fn solvec_inplace<'a, Sb>(
356+
&self,
357+
b: &'a mut ArrayBase<Sb, Ix1>,
358+
) -> Result<&'a mut ArrayBase<Sb, Ix1>>
350359
where
351360
Sb: DataMut<Elem = A>,
352361
{

src/convert.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ where
9393
} else {
9494
ArrayBase::from_shape_vec(a.dim().f(), a.into_raw_vec()).unwrap()
9595
};
96-
assert_eq!(new.strides(), strides.as_slice(), "Custom stride is not supported");
96+
assert_eq!(
97+
new.strides(),
98+
strides.as_slice(),
99+
"Custom stride is not supported"
100+
);
97101
new
98102
}
99103

src/eig.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Eigenvalue decomposition for non-symmetric square matrices
22
3-
use ndarray::*;
43
use crate::error::*;
54
use crate::layout::*;
65
use crate::types::*;
6+
use ndarray::*;
77

88
/// Eigenvalue decomposition of general matrix reference
99
pub trait Eig {
@@ -27,7 +27,12 @@ where
2727
let layout = a.square_layout()?;
2828
let (s, t) = unsafe { A::eig(true, layout, a.as_allocated_mut()?)? };
2929
let (n, _) = layout.size();
30-
Ok((ArrayBase::from(s), ArrayBase::from(t).into_shape((n as usize, n as usize)).unwrap()))
30+
Ok((
31+
ArrayBase::from(s),
32+
ArrayBase::from(t)
33+
.into_shape((n as usize, n as usize))
34+
.unwrap(),
35+
))
3136
}
3237
}
3338

@@ -49,4 +54,4 @@ where
4954
let (s, _) = unsafe { A::eig(true, a.square_layout()?, a.as_allocated_mut()?)? };
5055
Ok(ArrayBase::from(s))
5156
}
52-
}
57+
}

src/error.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ pub enum LinalgError {
2424
impl fmt::Display for LinalgError {
2525
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2626
match self {
27-
LinalgError::NotSquare { rows, cols } => write!(f, "Not square: rows({}) != cols({})", rows, cols),
28-
LinalgError::Lapack { return_code } => write!(f, "LAPACK: return_code = {}", return_code),
29-
LinalgError::InvalidStride { s0, s1 } => write!(f, "invalid stride: s0={}, s1={}", s0, s1),
27+
LinalgError::NotSquare { rows, cols } => {
28+
write!(f, "Not square: rows({}) != cols({})", rows, cols)
29+
}
30+
LinalgError::Lapack { return_code } => {
31+
write!(f, "LAPACK: return_code = {}", return_code)
32+
}
33+
LinalgError::InvalidStride { s0, s1 } => {
34+
write!(f, "invalid stride: s0={}, s1={}", s0, s1)
35+
}
3036
LinalgError::MemoryNotCont => write!(f, "Memory is not contiguous"),
3137
LinalgError::Shape(err) => write!(f, "Shape Error: {}", err),
3238
}

src/inner.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ where
2424
assert_eq!(self.len(), rhs.len());
2525
Zip::from(self)
2626
.and(rhs)
27-
.fold_while(A::zero(), |acc, s, r| FoldWhile::Continue(acc + s.conj() * *r))
27+
.fold_while(A::zero(), |acc, s, r| {
28+
FoldWhile::Continue(acc + s.conj() * *r)
29+
})
2830
.into_inner()
2931
}
3032
}

src/krylov/arnoldi.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ where
9797
}
9898

9999
/// Utility to execute Arnoldi iteration with Householder reflection
100-
pub fn arnoldi_householder<A, S>(a: impl LinearOperator<Elem = A>, v: ArrayBase<S, Ix1>, tol: A::Real) -> (Q<A>, H<A>)
100+
pub fn arnoldi_householder<A, S>(
101+
a: impl LinearOperator<Elem = A>,
102+
v: ArrayBase<S, Ix1>,
103+
tol: A::Real,
104+
) -> (Q<A>, H<A>)
101105
where
102106
A: Scalar + Lapack,
103107
S: DataMut<Elem = A>,
@@ -107,7 +111,11 @@ where
107111
}
108112

109113
/// Utility to execute Arnoldi iteration with modified Gram-Schmit orthogonalizer
110-
pub fn arnoldi_mgs<A, S>(a: impl LinearOperator<Elem = A>, v: ArrayBase<S, Ix1>, tol: A::Real) -> (Q<A>, H<A>)
114+
pub fn arnoldi_mgs<A, S>(
115+
a: impl LinearOperator<Elem = A>,
116+
v: ArrayBase<S, Ix1>,
117+
tol: A::Real,
118+
) -> (Q<A>, H<A>)
111119
where
112120
A: Scalar + Lapack,
113121
S: DataMut<Elem = A>,

src/krylov/householder.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ impl<A: Scalar + Lapack> Householder<A> {
7171
S: DataMut<Elem = A>,
7272
{
7373
assert!(k < self.v.len());
74-
assert_eq!(a.len(), self.dim, "Input array size mismaches to the dimension");
74+
assert_eq!(
75+
a.len(),
76+
self.dim,
77+
"Input array size mismaches to the dimension"
78+
);
7579
reflect(&self.v[k].slice(s![k..]), &mut a.slice_mut(s![k..]));
7680
}
7781

src/lapack/cholesky.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub trait Cholesky_: Sized {
1818
/// **Warning: Only the portion of `a` corresponding to `UPLO` is written.**
1919
unsafe fn inv_cholesky(l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<()>;
2020
/// Wrapper of `*potrs`
21-
unsafe fn solve_cholesky(l: MatrixLayout, uplo: UPLO, a: &[Self], b: &mut [Self]) -> Result<()>;
21+
unsafe fn solve_cholesky(l: MatrixLayout, uplo: UPLO, a: &[Self], b: &mut [Self])
22+
-> Result<()>;
2223
}
2324

2425
macro_rules! impl_cholesky {
@@ -36,7 +37,12 @@ macro_rules! impl_cholesky {
3637
into_result(info, ())
3738
}
3839

39-
unsafe fn solve_cholesky(l: MatrixLayout, uplo: UPLO, a: &[Self], b: &mut [Self]) -> Result<()> {
40+
unsafe fn solve_cholesky(
41+
l: MatrixLayout,
42+
uplo: UPLO,
43+
a: &[Self],
44+
b: &mut [Self],
45+
) -> Result<()> {
4046
let (n, _) = l.size();
4147
let nrhs = 1;
4248
let ldb = 1;

src/lapack/eig.rs

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,39 @@ use super::into_result;
1111

1212
/// Wraps `*geev` for real/complex
1313
pub trait Eig_: Scalar {
14-
unsafe fn eig(calc_v: bool, l: MatrixLayout, a: &mut [Self]) -> Result<(Vec<Self::Complex>, Vec<Self::Complex>)>;
14+
unsafe fn eig(
15+
calc_v: bool,
16+
l: MatrixLayout,
17+
a: &mut [Self],
18+
) -> Result<(Vec<Self::Complex>, Vec<Self::Complex>)>;
1519
}
1620

1721
macro_rules! impl_eig_complex {
1822
($scalar:ty, $ev:path) => {
1923
impl Eig_ for $scalar {
20-
unsafe fn eig(calc_v: bool, l: MatrixLayout, mut a: &mut [Self]) -> Result<(Vec<Self::Complex>, Vec<Self::Complex>)> {
24+
unsafe fn eig(
25+
calc_v: bool,
26+
l: MatrixLayout,
27+
mut a: &mut [Self],
28+
) -> Result<(Vec<Self::Complex>, Vec<Self::Complex>)> {
2129
let (n, _) = l.size();
2230
let jobvr = if calc_v { b'V' } else { b'N' };
2331
let mut w = vec![Self::Complex::zero(); n as usize];
2432
let mut vl = Vec::new();
2533
let mut vr = vec![Self::Complex::zero(); (n * n) as usize];
26-
let info = $ev(l.lapacke_layout(), b'N', jobvr, n, &mut a, n, &mut w, &mut vl, n, &mut vr, n);
34+
let info = $ev(
35+
l.lapacke_layout(),
36+
b'N',
37+
jobvr,
38+
n,
39+
&mut a,
40+
n,
41+
&mut w,
42+
&mut vl,
43+
n,
44+
&mut vr,
45+
n,
46+
);
2747
into_result(info, (w, vr))
2848
}
2949
}
@@ -33,49 +53,75 @@ macro_rules! impl_eig_complex {
3353
macro_rules! impl_eig_real {
3454
($scalar:ty, $ev:path) => {
3555
impl Eig_ for $scalar {
36-
unsafe fn eig(calc_v: bool, l: MatrixLayout, mut a: &mut [Self]) -> Result<(Vec<Self::Complex>, Vec<Self::Complex>)> {
56+
unsafe fn eig(
57+
calc_v: bool,
58+
l: MatrixLayout,
59+
mut a: &mut [Self],
60+
) -> Result<(Vec<Self::Complex>, Vec<Self::Complex>)> {
3761
let (n, _) = l.size();
3862
let jobvr = if calc_v { b'V' } else { b'N' };
3963
let mut wr = vec![Self::Real::zero(); n as usize];
4064
let mut wi = vec![Self::Real::zero(); n as usize];
4165
let mut vl = Vec::new();
4266
let mut vr = vec![Self::Real::zero(); (n * n) as usize];
43-
let info = $ev(l.lapacke_layout(), b'N', jobvr, n, &mut a, n, &mut wr, &mut wi, &mut vl, n, &mut vr, n);
44-
let w: Vec<Self::Complex> = wr.iter().zip(wi.iter()).map(|(&r, &i)| Self::Complex::new(r, i)).collect();
67+
let info = $ev(
68+
l.lapacke_layout(),
69+
b'N',
70+
jobvr,
71+
n,
72+
&mut a,
73+
n,
74+
&mut wr,
75+
&mut wi,
76+
&mut vl,
77+
n,
78+
&mut vr,
79+
n,
80+
);
81+
let w: Vec<Self::Complex> = wr
82+
.iter()
83+
.zip(wi.iter())
84+
.map(|(&r, &i)| Self::Complex::new(r, i))
85+
.collect();
4586
// If the j-th eigenvalue is real, then
4687
// eigenvector = [ vr[j], vr[j+n], vr[j+2*n], ... ].
4788
//
48-
// If the j-th and (j+1)-st eigenvalues form a complex conjugate pair,
89+
// If the j-th and (j+1)-st eigenvalues form a complex conjugate pair,
4990
// eigenvector(j) = [ vr[j] + i*vr[j+1], vr[j+n] + i*vr[j+n+1], vr[j+2*n] + i*vr[j+2*n+1], ... ] and
5091
// eigenvector(j+1) = [ vr[j] - i*vr[j+1], vr[j+n] - i*vr[j+n+1], vr[j+2*n] - i*vr[j+2*n+1], ... ].
51-
//
92+
//
5293
// Therefore, if eigenvector(j) is written as [ v_{j0}, v_{j1}, v_{j2}, ... ],
53-
// you have to make
94+
// you have to make
5495
// v = vec![ v_{00}, v_{10}, v_{20}, ..., v_{jk}, v_{(j+1)k}, v_{(j+2)k}, ... ] (v.len() = n*n)
5596
// based on wi and vr.
5697
// After that, v is converted to Array2 (see ../eig.rs).
5798
let n = n as usize;
5899
let mut flg = false;
59-
let conj: Vec<i8> = wi.iter().map(|&i| {
60-
if flg {
61-
flg = false;
62-
-1
63-
} else if i != 0.0 {
64-
flg = true;
65-
1
66-
} else {
67-
0
68-
}
69-
}).collect();
70-
let v: Vec<Self::Complex> = (0..n*n).map(|i| {
71-
let j = i % n;
72-
match conj[j] {
73-
1 => Self::Complex::new(vr[i], vr[i+1]),
74-
-1 => Self::Complex::new(vr[i-1], -vr[i]),
75-
_ => Self::Complex::new(vr[i], 0.0),
76-
}
77-
}).collect();
78-
100+
let conj: Vec<i8> = wi
101+
.iter()
102+
.map(|&i| {
103+
if flg {
104+
flg = false;
105+
-1
106+
} else if i != 0.0 {
107+
flg = true;
108+
1
109+
} else {
110+
0
111+
}
112+
})
113+
.collect();
114+
let v: Vec<Self::Complex> = (0..n * n)
115+
.map(|i| {
116+
let j = i % n;
117+
match conj[j] {
118+
1 => Self::Complex::new(vr[i], vr[i + 1]),
119+
-1 => Self::Complex::new(vr[i - 1], -vr[i]),
120+
_ => Self::Complex::new(vr[i], 0.0),
121+
}
122+
})
123+
.collect();
124+
79125
into_result(info, (w, v))
80126
}
81127
}
@@ -85,4 +131,4 @@ macro_rules! impl_eig_real {
85131
impl_eig_real!(f64, lapacke::dgeev);
86132
impl_eig_real!(f32, lapacke::sgeev);
87133
impl_eig_complex!(c64, lapacke::zgeev);
88-
impl_eig_complex!(c32, lapacke::cgeev);
134+
impl_eig_complex!(c32, lapacke::cgeev);

src/lapack/eigh.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ use super::{into_result, UPLO};
1111

1212
/// Wraps `*syev` for real and `*heev` for complex
1313
pub trait Eigh_: Scalar {
14-
unsafe fn eigh(calc_eigenvec: bool, l: MatrixLayout, uplo: UPLO, a: &mut [Self]) -> Result<Vec<Self::Real>>;
14+
unsafe fn eigh(
15+
calc_eigenvec: bool,
16+
l: MatrixLayout,
17+
uplo: UPLO,
18+
a: &mut [Self],
19+
) -> Result<Vec<Self::Real>>;
1520
unsafe fn eigh_generalized(
1621
calc_eigenvec: bool,
1722
l: MatrixLayout,
@@ -24,7 +29,12 @@ pub trait Eigh_: Scalar {
2429
macro_rules! impl_eigh {
2530
($scalar:ty, $ev:path, $evg:path) => {
2631
impl Eigh_ for $scalar {
27-
unsafe fn eigh(calc_v: bool, l: MatrixLayout, uplo: UPLO, mut a: &mut [Self]) -> Result<Vec<Self::Real>> {
32+
unsafe fn eigh(
33+
calc_v: bool,
34+
l: MatrixLayout,
35+
uplo: UPLO,
36+
mut a: &mut [Self],
37+
) -> Result<Vec<Self::Real>> {
2838
let (n, _) = l.size();
2939
let jobz = if calc_v { b'V' } else { b'N' };
3040
let mut w = vec![Self::Real::zero(); n as usize];

0 commit comments

Comments
 (0)