#[cfg(feature = "serde-serialize")] use serde::{Deserialize, Serialize}; use num::Zero; use alga::general::Complex; use allocator::Allocator; use base::{DefaultAllocator, Matrix, MatrixMN, MatrixN, SquareMatrix}; use constraint::{SameNumberOfRows, ShapeConstraint}; use dimension::{Dim, DimSub, Dynamic}; use storage::{Storage, StorageMut}; /// The Cholesky decomposition of a symmetric-definite-positive matrix. #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] #[cfg_attr( feature = "serde-serialize", serde(bound( serialize = "DefaultAllocator: Allocator, MatrixN: Serialize" )) )] #[cfg_attr( feature = "serde-serialize", serde(bound( deserialize = "DefaultAllocator: Allocator, MatrixN: Deserialize<'de>" )) )] #[derive(Clone, Debug)] pub struct Cholesky where DefaultAllocator: Allocator { chol: MatrixN, } impl Copy for Cholesky where DefaultAllocator: Allocator, MatrixN: Copy, {} impl> Cholesky where DefaultAllocator: Allocator { /// Attempts to compute the Cholesky decomposition of `matrix`. /// /// Returns `None` if the input matrix is not definite-positive. The input matrix is assumed /// to be symmetric and only the lower-triangular part is read. pub fn new(mut matrix: MatrixN) -> Option { assert!(matrix.is_square(), "The input matrix must be square."); let n = matrix.nrows(); for j in 0..n { for k in 0..j { let factor = unsafe { -*matrix.get_unchecked((j, k)) }; let (mut col_j, col_k) = matrix.columns_range_pair_mut(j, k); let mut col_j = col_j.rows_range_mut(j..); let col_k = col_k.rows_range(j..); col_j.axpy(factor, &col_k, N::one()); } let diag = unsafe { *matrix.get_unchecked((j, j)) }; if diag.real() > N::Real::zero() { let denom = diag.sqrt(); unsafe { *matrix.get_unchecked_mut((j, j)) = denom; } let mut col = matrix.slice_range_mut(j + 1.., j); col /= denom; } else { return None; } } Some(Cholesky { chol: matrix }) } /// Retrieves the lower-triangular factor of the Cholesky decomposition with its strictly /// upper-triangular part filled with zeros. pub fn unpack(mut self) -> MatrixN { self.chol.fill_upper_triangle(N::zero(), 1); self.chol } /// Retrieves the lower-triangular factor of the Cholesky decomposition, without zeroing-out /// its strict upper-triangular part. /// /// The values of the strict upper-triangular part are garbage and should be ignored by further /// computations. pub fn unpack_dirty(self) -> MatrixN { self.chol } /// Retrieves the lower-triangular factor of the Cholesky decomposition with its strictly /// uppen-triangular part filled with zeros. pub fn l(&self) -> MatrixN { self.chol.lower_triangle() } /// Retrieves the lower-triangular factor of the Cholesky decomposition, without zeroing-out /// its strict upper-triangular part. /// /// This is an allocation-less version of `self.l()`. The values of the strict upper-triangular /// part are garbage and should be ignored by further computations. pub fn l_dirty(&self) -> &MatrixN { &self.chol } /// Solves the system `self * x = b` where `self` is the decomposed matrix and `x` the unknown. /// /// The result is stored on `b`. pub fn solve_mut(&self, b: &mut Matrix) where S2: StorageMut, ShapeConstraint: SameNumberOfRows, { let _ = self.chol.solve_lower_triangular_mut(b); let _ = self.chol.tr_solve_lower_triangular_mut(b); } /// Returns the solution of the system `self * x = b` where `self` is the decomposed matrix and /// `x` the unknown. pub fn solve(&self, b: &Matrix) -> MatrixMN where S2: StorageMut, DefaultAllocator: Allocator, ShapeConstraint: SameNumberOfRows, { let mut res = b.clone_owned(); self.solve_mut(&mut res); res } /// Computes the inverse of the decomposed matrix. pub fn inverse(&self) -> MatrixN { let shape = self.chol.data.shape(); let mut res = MatrixN::identity_generic(shape.0, shape.1); self.solve_mut(&mut res); res } } impl, S: Storage> SquareMatrix where DefaultAllocator: Allocator { /// Attempts to compute the Cholesky decomposition of this matrix. /// /// Returns `None` if the input matrix is not definite-positive. The input matrix is assumed /// to be symmetric and only the lower-triangular part is read. pub fn cholesky(self) -> Option> { Cholesky::new(self.into_owned()) } }