Skip to content

Commit b8fea67

Browse files
Volodymyr OrlovVolodymyr Orlov
authored andcommitted
fix: formatting
1 parent 6473a6c commit b8fea67

File tree

5 files changed

+50
-50
lines changed

5 files changed

+50
-50
lines changed

src/linalg/cholesky.rs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! # Cholesky Decomposition
22
//!
3-
//! every positive definite matrix \\(A \in R^{n \times n}\\) can be factored as
3+
//! every positive definite matrix \\(A \in R^{n \times n}\\) can be factored as
44
//!
55
//! \\[A = R^TR\\]
6-
//!
6+
//!
77
//! where \\(R\\) is upper triangular matrix with positive diagonal elements
88
//!
99
//! Example:
@@ -12,8 +12,8 @@
1212
//! use crate::smartcore::linalg::cholesky::*;
1313
//!
1414
//! let A = DenseMatrix::from_2d_array(&[
15-
//! &[25., 15., -5.],
16-
//! &[15., 18., 0.],
15+
//! &[25., 15., -5.],
16+
//! &[15., 18., 0.],
1717
//! &[-5., 0., 11.]
1818
//! ]);
1919
//!
@@ -41,14 +41,14 @@ use crate::math::num::RealNumber;
4141
/// Results of Cholesky decomposition.
4242
pub struct Cholesky<T: RealNumber, M: BaseMatrix<T>> {
4343
R: M,
44-
t: PhantomData<T>
44+
t: PhantomData<T>,
4545
}
4646

4747
impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
4848
pub(crate) fn new(R: M) -> Cholesky<T, M> {
4949
Cholesky {
5050
R: R,
51-
t: PhantomData
51+
t: PhantomData,
5252
}
5353
}
5454

@@ -65,10 +65,10 @@ impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
6565
}
6666
}
6767
R
68-
}
69-
68+
}
69+
7070
/// Get upper triangular matrix.
71-
pub fn U(&self) -> M {
71+
pub fn U(&self) -> M {
7272
let (n, _) = self.R.shape();
7373
let mut R = M::zeros(n, n);
7474

@@ -80,20 +80,20 @@ impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
8080
}
8181
}
8282
R
83-
}
83+
}
8484

8585
/// Solves Ax = b
86-
pub(crate) fn solve(&self, mut b: M) -> Result<M, Failed> {
87-
86+
pub(crate) fn solve(&self, mut b: M) -> Result<M, Failed> {
8887
let (bn, m) = b.shape();
8988
let (rn, _) = self.R.shape();
9089

9190
if bn != rn {
92-
return Err(Failed::because(FailedError::SolutionFailed, &format!(
93-
"Can't solve Ax = b for x. Number of rows in b != number of rows in R."
94-
)));
91+
return Err(Failed::because(
92+
FailedError::SolutionFailed,
93+
&format!("Can't solve Ax = b for x. Number of rows in b != number of rows in R."),
94+
));
9595
}
96-
96+
9797
for k in 0..bn {
9898
for j in 0..m {
9999
for i in 0..k {
@@ -102,7 +102,7 @@ impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
102102
b.div_element_mut(k, j, self.R.get(k, k));
103103
}
104104
}
105-
105+
106106
for k in (0..bn).rev() {
107107
for j in 0..m {
108108
for i in k + 1..bn {
@@ -128,11 +128,12 @@ pub trait CholeskyDecomposableMatrix<T: RealNumber>: BaseMatrix<T> {
128128
let (m, n) = self.shape();
129129

130130
if m != n {
131-
return Err(Failed::because(FailedError::DecompositionFailed, &format!(
132-
"Can't do Cholesky decomposition on a non-square matrix"
133-
)));
131+
return Err(Failed::because(
132+
FailedError::DecompositionFailed,
133+
&format!("Can't do Cholesky decomposition on a non-square matrix"),
134+
));
134135
}
135-
136+
136137
for j in 0..n {
137138
let mut d = T::zero();
138139
for k in 0..j {
@@ -147,9 +148,10 @@ pub trait CholeskyDecomposableMatrix<T: RealNumber>: BaseMatrix<T> {
147148
d = self.get(j, j) - d;
148149

149150
if d < T::zero() {
150-
return Err(Failed::because(FailedError::DecompositionFailed, &format!(
151-
"The matrix is not positive definite."
152-
)));
151+
return Err(Failed::because(
152+
FailedError::DecompositionFailed,
153+
&format!("The matrix is not positive definite."),
154+
));
153155
}
154156

155157
self.set(j, j, d.sqrt());
@@ -172,35 +174,33 @@ mod tests {
172174
#[test]
173175
fn cholesky_decompose() {
174176
let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]);
175-
let l = DenseMatrix::from_2d_array(&[
176-
&[5.0, 0.0, 0.0],
177-
&[3.0, 3.0, 0.0],
178-
&[-1.0, 1.0, 3.0],
179-
]);
180-
let u = DenseMatrix::from_2d_array(&[
181-
&[5.0, 3.0, -1.0],
182-
&[0.0, 3.0, 1.0],
183-
&[0.0, 0.0, 3.0],
184-
]);
177+
let l =
178+
DenseMatrix::from_2d_array(&[&[5.0, 0.0, 0.0], &[3.0, 3.0, 0.0], &[-1.0, 1.0, 3.0]]);
179+
let u =
180+
DenseMatrix::from_2d_array(&[&[5.0, 3.0, -1.0], &[0.0, 3.0, 1.0], &[0.0, 0.0, 3.0]]);
185181
let cholesky = a.cholesky().unwrap();
186-
187-
assert!(cholesky.L().abs().approximate_eq(&l.abs(), 1e-4));
188-
assert!(cholesky.U().abs().approximate_eq(&u.abs(), 1e-4));
189-
assert!(cholesky.L().matmul(&cholesky.U()).abs().approximate_eq(&a.abs(), 1e-4));
182+
183+
assert!(cholesky.L().abs().approximate_eq(&l.abs(), 1e-4));
184+
assert!(cholesky.U().abs().approximate_eq(&u.abs(), 1e-4));
185+
assert!(cholesky
186+
.L()
187+
.matmul(&cholesky.U())
188+
.abs()
189+
.approximate_eq(&a.abs(), 1e-4));
190190
}
191191

192192
#[test]
193193
fn cholesky_solve_mut() {
194194
let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]);
195195
let b = DenseMatrix::from_2d_array(&[&[40., 51., 28.]]);
196-
let expected = DenseMatrix::from_2d_array(&[
197-
&[1.0, 2.0, 3.0]
198-
]);
199-
196+
let expected = DenseMatrix::from_2d_array(&[&[1.0, 2.0, 3.0]]);
197+
200198
let cholesky = a.cholesky().unwrap();
201199

202-
assert!(cholesky.solve(b.transpose()).unwrap().transpose().approximate_eq(&expected, 1e-4));
203-
200+
assert!(cholesky
201+
.solve(b.transpose())
202+
.unwrap()
203+
.transpose()
204+
.approximate_eq(&expected, 1e-4));
204205
}
205-
206206
}

src/linalg/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
//! let u: DenseMatrix<f64> = svd.U;
3434
//! ```
3535
36+
pub mod cholesky;
3637
/// The matrix is represented in terms of its eigenvalues and eigenvectors.
3738
pub mod evd;
3839
/// Factors a matrix as the product of a lower triangular matrix and an upper triangular matrix.
@@ -49,18 +50,17 @@ pub mod ndarray_bindings;
4950
pub mod qr;
5051
/// Singular value decomposition.
5152
pub mod svd;
52-
pub mod cholesky;
5353

5454
use std::fmt::{Debug, Display};
5555
use std::marker::PhantomData;
5656
use std::ops::Range;
5757

5858
use crate::math::num::RealNumber;
59+
use cholesky::CholeskyDecomposableMatrix;
5960
use evd::EVDDecomposableMatrix;
6061
use lu::LUDecomposableMatrix;
6162
use qr::QRDecomposableMatrix;
6263
use svd::SVDDecomposableMatrix;
63-
use cholesky::CholeskyDecomposableMatrix;
6464

6565
/// Column or row vector
6666
pub trait BaseVector<T: RealNumber>: Clone + Debug {

src/linalg/naive/dense_matrix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use serde::de::{Deserializer, MapAccess, SeqAccess, Visitor};
88
use serde::ser::{SerializeStruct, Serializer};
99
use serde::{Deserialize, Serialize};
1010

11+
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
1112
use crate::linalg::evd::EVDDecomposableMatrix;
1213
use crate::linalg::lu::LUDecomposableMatrix;
1314
use crate::linalg::qr::QRDecomposableMatrix;
1415
use crate::linalg::svd::SVDDecomposableMatrix;
15-
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
1616
use crate::linalg::Matrix;
1717
pub use crate::linalg::{BaseMatrix, BaseVector};
1818
use crate::math::num::RealNumber;

src/linalg/nalgebra_bindings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ use std::ops::{AddAssign, DivAssign, MulAssign, Range, SubAssign};
4242

4343
use nalgebra::{DMatrix, Dynamic, Matrix, MatrixMN, RowDVector, Scalar, VecStorage, U1};
4444

45+
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
4546
use crate::linalg::evd::EVDDecomposableMatrix;
4647
use crate::linalg::lu::LUDecomposableMatrix;
4748
use crate::linalg::qr::QRDecomposableMatrix;
4849
use crate::linalg::svd::SVDDecomposableMatrix;
49-
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
5050
use crate::linalg::Matrix as SmartCoreMatrix;
5151
use crate::linalg::{BaseMatrix, BaseVector};
5252
use crate::math::num::RealNumber;

src/linalg/ndarray_bindings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ use std::ops::SubAssign;
4949
use ndarray::ScalarOperand;
5050
use ndarray::{s, stack, Array, ArrayBase, Axis, Ix1, Ix2, OwnedRepr};
5151

52+
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
5253
use crate::linalg::evd::EVDDecomposableMatrix;
5354
use crate::linalg::lu::LUDecomposableMatrix;
5455
use crate::linalg::qr::QRDecomposableMatrix;
5556
use crate::linalg::svd::SVDDecomposableMatrix;
56-
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
5757
use crate::linalg::Matrix;
5858
use crate::linalg::{BaseMatrix, BaseVector};
5959
use crate::math::num::RealNumber;

0 commit comments

Comments
 (0)