fix: formatting
This commit is contained in:
+45
-45
@@ -1,9 +1,9 @@
|
|||||||
//! # Cholesky Decomposition
|
//! # Cholesky Decomposition
|
||||||
//!
|
//!
|
||||||
//! every positive definite matrix \\(A \in R^{n \times n}\\) can be factored as
|
//! every positive definite matrix \\(A \in R^{n \times n}\\) can be factored as
|
||||||
//!
|
//!
|
||||||
//! \\[A = R^TR\\]
|
//! \\[A = R^TR\\]
|
||||||
//!
|
//!
|
||||||
//! where \\(R\\) is upper triangular matrix with positive diagonal elements
|
//! where \\(R\\) is upper triangular matrix with positive diagonal elements
|
||||||
//!
|
//!
|
||||||
//! Example:
|
//! Example:
|
||||||
@@ -12,8 +12,8 @@
|
|||||||
//! use crate::smartcore::linalg::cholesky::*;
|
//! use crate::smartcore::linalg::cholesky::*;
|
||||||
//!
|
//!
|
||||||
//! let A = DenseMatrix::from_2d_array(&[
|
//! let A = DenseMatrix::from_2d_array(&[
|
||||||
//! &[25., 15., -5.],
|
//! &[25., 15., -5.],
|
||||||
//! &[15., 18., 0.],
|
//! &[15., 18., 0.],
|
||||||
//! &[-5., 0., 11.]
|
//! &[-5., 0., 11.]
|
||||||
//! ]);
|
//! ]);
|
||||||
//!
|
//!
|
||||||
@@ -41,14 +41,14 @@ use crate::math::num::RealNumber;
|
|||||||
/// Results of Cholesky decomposition.
|
/// Results of Cholesky decomposition.
|
||||||
pub struct Cholesky<T: RealNumber, M: BaseMatrix<T>> {
|
pub struct Cholesky<T: RealNumber, M: BaseMatrix<T>> {
|
||||||
R: M,
|
R: M,
|
||||||
t: PhantomData<T>
|
t: PhantomData<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
|
impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
|
||||||
pub(crate) fn new(R: M) -> Cholesky<T, M> {
|
pub(crate) fn new(R: M) -> Cholesky<T, M> {
|
||||||
Cholesky {
|
Cholesky {
|
||||||
R: R,
|
R: R,
|
||||||
t: PhantomData
|
t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,10 +65,10 @@ impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
R
|
R
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get upper triangular matrix.
|
/// Get upper triangular matrix.
|
||||||
pub fn U(&self) -> M {
|
pub fn U(&self) -> M {
|
||||||
let (n, _) = self.R.shape();
|
let (n, _) = self.R.shape();
|
||||||
let mut R = M::zeros(n, n);
|
let mut R = M::zeros(n, n);
|
||||||
|
|
||||||
@@ -80,20 +80,20 @@ impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
R
|
R
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Solves Ax = b
|
/// Solves Ax = b
|
||||||
pub(crate) fn solve(&self, mut b: M) -> Result<M, Failed> {
|
pub(crate) fn solve(&self, mut b: M) -> Result<M, Failed> {
|
||||||
|
|
||||||
let (bn, m) = b.shape();
|
let (bn, m) = b.shape();
|
||||||
let (rn, _) = self.R.shape();
|
let (rn, _) = self.R.shape();
|
||||||
|
|
||||||
if bn != rn {
|
if bn != rn {
|
||||||
return Err(Failed::because(FailedError::SolutionFailed, &format!(
|
return Err(Failed::because(
|
||||||
"Can't solve Ax = b for x. Number of rows in b != number of rows in R."
|
FailedError::SolutionFailed,
|
||||||
)));
|
&format!("Can't solve Ax = b for x. Number of rows in b != number of rows in R."),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
for k in 0..bn {
|
for k in 0..bn {
|
||||||
for j in 0..m {
|
for j in 0..m {
|
||||||
for i in 0..k {
|
for i in 0..k {
|
||||||
@@ -102,7 +102,7 @@ impl<T: RealNumber, M: BaseMatrix<T>> Cholesky<T, M> {
|
|||||||
b.div_element_mut(k, j, self.R.get(k, k));
|
b.div_element_mut(k, j, self.R.get(k, k));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for k in (0..bn).rev() {
|
for k in (0..bn).rev() {
|
||||||
for j in 0..m {
|
for j in 0..m {
|
||||||
for i in k + 1..bn {
|
for i in k + 1..bn {
|
||||||
@@ -128,11 +128,12 @@ pub trait CholeskyDecomposableMatrix<T: RealNumber>: BaseMatrix<T> {
|
|||||||
let (m, n) = self.shape();
|
let (m, n) = self.shape();
|
||||||
|
|
||||||
if m != n {
|
if m != n {
|
||||||
return Err(Failed::because(FailedError::DecompositionFailed, &format!(
|
return Err(Failed::because(
|
||||||
"Can't do Cholesky decomposition on a non-square matrix"
|
FailedError::DecompositionFailed,
|
||||||
)));
|
&format!("Can't do Cholesky decomposition on a non-square matrix"),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
for j in 0..n {
|
for j in 0..n {
|
||||||
let mut d = T::zero();
|
let mut d = T::zero();
|
||||||
for k in 0..j {
|
for k in 0..j {
|
||||||
@@ -147,9 +148,10 @@ pub trait CholeskyDecomposableMatrix<T: RealNumber>: BaseMatrix<T> {
|
|||||||
d = self.get(j, j) - d;
|
d = self.get(j, j) - d;
|
||||||
|
|
||||||
if d < T::zero() {
|
if d < T::zero() {
|
||||||
return Err(Failed::because(FailedError::DecompositionFailed, &format!(
|
return Err(Failed::because(
|
||||||
"The matrix is not positive definite."
|
FailedError::DecompositionFailed,
|
||||||
)));
|
&format!("The matrix is not positive definite."),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
self.set(j, j, d.sqrt());
|
self.set(j, j, d.sqrt());
|
||||||
@@ -172,35 +174,33 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn cholesky_decompose() {
|
fn cholesky_decompose() {
|
||||||
let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]);
|
let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]);
|
||||||
let l = DenseMatrix::from_2d_array(&[
|
let l =
|
||||||
&[5.0, 0.0, 0.0],
|
DenseMatrix::from_2d_array(&[&[5.0, 0.0, 0.0], &[3.0, 3.0, 0.0], &[-1.0, 1.0, 3.0]]);
|
||||||
&[3.0, 3.0, 0.0],
|
let u =
|
||||||
&[-1.0, 1.0, 3.0],
|
DenseMatrix::from_2d_array(&[&[5.0, 3.0, -1.0], &[0.0, 3.0, 1.0], &[0.0, 0.0, 3.0]]);
|
||||||
]);
|
|
||||||
let u = DenseMatrix::from_2d_array(&[
|
|
||||||
&[5.0, 3.0, -1.0],
|
|
||||||
&[0.0, 3.0, 1.0],
|
|
||||||
&[0.0, 0.0, 3.0],
|
|
||||||
]);
|
|
||||||
let cholesky = a.cholesky().unwrap();
|
let cholesky = a.cholesky().unwrap();
|
||||||
|
|
||||||
assert!(cholesky.L().abs().approximate_eq(&l.abs(), 1e-4));
|
assert!(cholesky.L().abs().approximate_eq(&l.abs(), 1e-4));
|
||||||
assert!(cholesky.U().abs().approximate_eq(&u.abs(), 1e-4));
|
assert!(cholesky.U().abs().approximate_eq(&u.abs(), 1e-4));
|
||||||
assert!(cholesky.L().matmul(&cholesky.U()).abs().approximate_eq(&a.abs(), 1e-4));
|
assert!(cholesky
|
||||||
|
.L()
|
||||||
|
.matmul(&cholesky.U())
|
||||||
|
.abs()
|
||||||
|
.approximate_eq(&a.abs(), 1e-4));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cholesky_solve_mut() {
|
fn cholesky_solve_mut() {
|
||||||
let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]);
|
let a = DenseMatrix::from_2d_array(&[&[25., 15., -5.], &[15., 18., 0.], &[-5., 0., 11.]]);
|
||||||
let b = DenseMatrix::from_2d_array(&[&[40., 51., 28.]]);
|
let b = DenseMatrix::from_2d_array(&[&[40., 51., 28.]]);
|
||||||
let expected = DenseMatrix::from_2d_array(&[
|
let expected = DenseMatrix::from_2d_array(&[&[1.0, 2.0, 3.0]]);
|
||||||
&[1.0, 2.0, 3.0]
|
|
||||||
]);
|
|
||||||
|
|
||||||
let cholesky = a.cholesky().unwrap();
|
let cholesky = a.cholesky().unwrap();
|
||||||
|
|
||||||
assert!(cholesky.solve(b.transpose()).unwrap().transpose().approximate_eq(&expected, 1e-4));
|
assert!(cholesky
|
||||||
|
.solve(b.transpose())
|
||||||
|
.unwrap()
|
||||||
|
.transpose()
|
||||||
|
.approximate_eq(&expected, 1e-4));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -33,6 +33,7 @@
|
|||||||
//! let u: DenseMatrix<f64> = svd.U;
|
//! let u: DenseMatrix<f64> = svd.U;
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
pub mod cholesky;
|
||||||
/// The matrix is represented in terms of its eigenvalues and eigenvectors.
|
/// The matrix is represented in terms of its eigenvalues and eigenvectors.
|
||||||
pub mod evd;
|
pub mod evd;
|
||||||
/// Factors a matrix as the product of a lower triangular matrix and an upper triangular matrix.
|
/// Factors a matrix as the product of a lower triangular matrix and an upper triangular matrix.
|
||||||
@@ -49,18 +50,17 @@ pub mod ndarray_bindings;
|
|||||||
pub mod qr;
|
pub mod qr;
|
||||||
/// Singular value decomposition.
|
/// Singular value decomposition.
|
||||||
pub mod svd;
|
pub mod svd;
|
||||||
pub mod cholesky;
|
|
||||||
|
|
||||||
use std::fmt::{Debug, Display};
|
use std::fmt::{Debug, Display};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|
||||||
use crate::math::num::RealNumber;
|
use crate::math::num::RealNumber;
|
||||||
|
use cholesky::CholeskyDecomposableMatrix;
|
||||||
use evd::EVDDecomposableMatrix;
|
use evd::EVDDecomposableMatrix;
|
||||||
use lu::LUDecomposableMatrix;
|
use lu::LUDecomposableMatrix;
|
||||||
use qr::QRDecomposableMatrix;
|
use qr::QRDecomposableMatrix;
|
||||||
use svd::SVDDecomposableMatrix;
|
use svd::SVDDecomposableMatrix;
|
||||||
use cholesky::CholeskyDecomposableMatrix;
|
|
||||||
|
|
||||||
/// Column or row vector
|
/// Column or row vector
|
||||||
pub trait BaseVector<T: RealNumber>: Clone + Debug {
|
pub trait BaseVector<T: RealNumber>: Clone + Debug {
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ use serde::de::{Deserializer, MapAccess, SeqAccess, Visitor};
|
|||||||
use serde::ser::{SerializeStruct, Serializer};
|
use serde::ser::{SerializeStruct, Serializer};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
|
||||||
use crate::linalg::evd::EVDDecomposableMatrix;
|
use crate::linalg::evd::EVDDecomposableMatrix;
|
||||||
use crate::linalg::lu::LUDecomposableMatrix;
|
use crate::linalg::lu::LUDecomposableMatrix;
|
||||||
use crate::linalg::qr::QRDecomposableMatrix;
|
use crate::linalg::qr::QRDecomposableMatrix;
|
||||||
use crate::linalg::svd::SVDDecomposableMatrix;
|
use crate::linalg::svd::SVDDecomposableMatrix;
|
||||||
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
|
|
||||||
use crate::linalg::Matrix;
|
use crate::linalg::Matrix;
|
||||||
pub use crate::linalg::{BaseMatrix, BaseVector};
|
pub use crate::linalg::{BaseMatrix, BaseVector};
|
||||||
use crate::math::num::RealNumber;
|
use crate::math::num::RealNumber;
|
||||||
|
|||||||
@@ -42,11 +42,11 @@ use std::ops::{AddAssign, DivAssign, MulAssign, Range, SubAssign};
|
|||||||
|
|
||||||
use nalgebra::{DMatrix, Dynamic, Matrix, MatrixMN, RowDVector, Scalar, VecStorage, U1};
|
use nalgebra::{DMatrix, Dynamic, Matrix, MatrixMN, RowDVector, Scalar, VecStorage, U1};
|
||||||
|
|
||||||
|
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
|
||||||
use crate::linalg::evd::EVDDecomposableMatrix;
|
use crate::linalg::evd::EVDDecomposableMatrix;
|
||||||
use crate::linalg::lu::LUDecomposableMatrix;
|
use crate::linalg::lu::LUDecomposableMatrix;
|
||||||
use crate::linalg::qr::QRDecomposableMatrix;
|
use crate::linalg::qr::QRDecomposableMatrix;
|
||||||
use crate::linalg::svd::SVDDecomposableMatrix;
|
use crate::linalg::svd::SVDDecomposableMatrix;
|
||||||
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
|
|
||||||
use crate::linalg::Matrix as SmartCoreMatrix;
|
use crate::linalg::Matrix as SmartCoreMatrix;
|
||||||
use crate::linalg::{BaseMatrix, BaseVector};
|
use crate::linalg::{BaseMatrix, BaseVector};
|
||||||
use crate::math::num::RealNumber;
|
use crate::math::num::RealNumber;
|
||||||
|
|||||||
@@ -49,11 +49,11 @@ use std::ops::SubAssign;
|
|||||||
use ndarray::ScalarOperand;
|
use ndarray::ScalarOperand;
|
||||||
use ndarray::{s, stack, Array, ArrayBase, Axis, Ix1, Ix2, OwnedRepr};
|
use ndarray::{s, stack, Array, ArrayBase, Axis, Ix1, Ix2, OwnedRepr};
|
||||||
|
|
||||||
|
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
|
||||||
use crate::linalg::evd::EVDDecomposableMatrix;
|
use crate::linalg::evd::EVDDecomposableMatrix;
|
||||||
use crate::linalg::lu::LUDecomposableMatrix;
|
use crate::linalg::lu::LUDecomposableMatrix;
|
||||||
use crate::linalg::qr::QRDecomposableMatrix;
|
use crate::linalg::qr::QRDecomposableMatrix;
|
||||||
use crate::linalg::svd::SVDDecomposableMatrix;
|
use crate::linalg::svd::SVDDecomposableMatrix;
|
||||||
use crate::linalg::cholesky::CholeskyDecomposableMatrix;
|
|
||||||
use crate::linalg::Matrix;
|
use crate::linalg::Matrix;
|
||||||
use crate::linalg::{BaseMatrix, BaseVector};
|
use crate::linalg::{BaseMatrix, BaseVector};
|
||||||
use crate::math::num::RealNumber;
|
use crate::math::num::RealNumber;
|
||||||
|
|||||||
Reference in New Issue
Block a user