2017-08-14 01:53:04 +08:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
2018-10-22 13:00:10 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2017-08-14 01:53:04 +08:00
|
|
|
|
2019-03-03 02:33:49 +08:00
|
|
|
use alga::general::Complex;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
use allocator::Allocator;
|
2018-05-19 23:15:15 +08:00
|
|
|
use base::{DefaultAllocator, Matrix, MatrixMN, MatrixN, SquareMatrix};
|
|
|
|
use constraint::{SameNumberOfRows, ShapeConstraint};
|
2018-02-02 19:26:35 +08:00
|
|
|
use dimension::{Dim, DimSub, Dynamic};
|
2018-05-19 23:15:15 +08:00
|
|
|
use storage::{Storage, StorageMut};
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2018-09-24 12:48:42 +08:00
|
|
|
/// The Cholesky decomposition of a symmetric-definite-positive matrix.
|
2017-08-14 01:53:04 +08:00
|
|
|
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
2018-05-19 23:15:15 +08:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2018-10-22 13:00:10 +08:00
|
|
|
serde(bound(
|
|
|
|
serialize = "DefaultAllocator: Allocator<N, D>,
|
2018-09-13 12:55:58 +08:00
|
|
|
MatrixN<N, D>: Serialize"
|
2018-10-22 13:00:10 +08:00
|
|
|
))
|
2018-05-19 23:15:15 +08:00
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2018-10-22 13:00:10 +08:00
|
|
|
serde(bound(
|
|
|
|
deserialize = "DefaultAllocator: Allocator<N, D>,
|
2018-09-13 12:55:58 +08:00
|
|
|
MatrixN<N, D>: Deserialize<'de>"
|
2018-10-22 13:00:10 +08:00
|
|
|
))
|
2018-05-19 23:15:15 +08:00
|
|
|
)]
|
2017-08-14 01:53:00 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2019-03-03 02:33:49 +08:00
|
|
|
pub struct Cholesky<N: Complex, D: Dim>
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Allocator<N, D, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
|
|
|
chol: MatrixN<N, D>,
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 02:33:49 +08:00
|
|
|
impl<N: Complex, D: Dim> Copy for Cholesky<N, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
DefaultAllocator: Allocator<N, D, D>,
|
|
|
|
MatrixN<N, D>: Copy,
|
2018-10-22 13:00:10 +08:00
|
|
|
{}
|
2017-08-14 01:53:00 +08:00
|
|
|
|
2019-03-03 02:33:49 +08:00
|
|
|
impl<N: Complex, D: DimSub<Dynamic>> Cholesky<N, D>
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Allocator<N, D, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Attempts to compute the Cholesky decomposition of `matrix`.
|
2017-08-03 01:37:44 +08:00
|
|
|
///
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Returns `None` if the input matrix is not definite-positive. The input matrix is assumed
|
2017-08-03 01:37:44 +08:00
|
|
|
/// to be symmetric and only the lower-triangular part is read.
|
|
|
|
pub fn new(mut matrix: MatrixN<N, D>) -> Option<Self> {
|
|
|
|
assert!(matrix.is_square(), "The input matrix must be square.");
|
|
|
|
|
|
|
|
let n = matrix.nrows();
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
for j in 0..n {
|
|
|
|
for k in 0..j {
|
2018-12-03 04:00:08 +08:00
|
|
|
let factor = unsafe { -*matrix.get_unchecked((j, k)) };
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
let (mut col_j, col_k) = matrix.columns_range_pair_mut(j, k);
|
2018-02-02 19:26:35 +08:00
|
|
|
let mut col_j = col_j.rows_range_mut(j..);
|
|
|
|
let col_k = col_k.rows_range(j..);
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2019-03-23 18:46:56 +08:00
|
|
|
col_j.axpy(factor.conjugate(), &col_k, N::one());
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2018-12-03 04:00:08 +08:00
|
|
|
let diag = unsafe { *matrix.get_unchecked((j, j)) };
|
2019-03-23 18:46:56 +08:00
|
|
|
if !diag.is_zero() {
|
|
|
|
if let Some(denom) = diag.try_sqrt() {
|
|
|
|
unsafe {
|
|
|
|
*matrix.get_unchecked_mut((j, j)) = denom;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut col = matrix.slice_range_mut(j + 1.., j);
|
|
|
|
col /= denom;
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
2019-03-23 18:46:56 +08:00
|
|
|
|
2019-03-23 21:13:00 +08:00
|
|
|
// The diagonal element is either zero or its square root could not
|
|
|
|
// be taken (e.g. for negative real numbers).
|
2019-03-23 18:46:56 +08:00
|
|
|
return None;
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(Cholesky { chol: matrix })
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Retrieves the lower-triangular factor of the Cholesky decomposition with its strictly
|
|
|
|
/// upper-triangular part filled with zeros.
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn unpack(mut self) -> MatrixN<N, D> {
|
|
|
|
self.chol.fill_upper_triangle(N::zero(), 1);
|
|
|
|
self.chol
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Retrieves the lower-triangular factor of the Cholesky decomposition, without zeroing-out
|
2017-08-03 01:37:44 +08:00
|
|
|
/// its strict upper-triangular part.
|
|
|
|
///
|
2017-08-14 01:53:04 +08:00
|
|
|
/// The values of the strict upper-triangular part are garbage and should be ignored by further
|
|
|
|
/// computations.
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn unpack_dirty(self) -> MatrixN<N, D> {
|
|
|
|
self.chol
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Retrieves the lower-triangular factor of the Cholesky decomposition with its strictly
|
|
|
|
/// uppen-triangular part filled with zeros.
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn l(&self) -> MatrixN<N, D> {
|
|
|
|
self.chol.lower_triangle()
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Retrieves the lower-triangular factor of the Cholesky decomposition, without zeroing-out
|
2017-08-03 01:37:44 +08:00
|
|
|
/// 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<N, D> {
|
|
|
|
&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<R2: Dim, C2: Dim, S2>(&self, b: &mut Matrix<N, R2, C2, S2>)
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
S2: StorageMut<N, R2, C2>,
|
|
|
|
ShapeConstraint: SameNumberOfRows<R2, D>,
|
|
|
|
{
|
2017-08-16 00:24:34 +08:00
|
|
|
let _ = self.chol.solve_lower_triangular_mut(b);
|
2019-03-23 18:48:12 +08:00
|
|
|
let _ = self.chol.ad_solve_lower_triangular_mut(b);
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Returns the solution of the system `self * x = b` where `self` is the decomposed matrix and
|
|
|
|
/// `x` the unknown.
|
2017-08-03 01:37:44 +08:00
|
|
|
pub fn solve<R2: Dim, C2: Dim, S2>(&self, b: &Matrix<N, R2, C2, S2>) -> MatrixMN<N, R2, C2>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
|
|
|
S2: StorageMut<N, R2, C2>,
|
|
|
|
DefaultAllocator: Allocator<N, R2, C2>,
|
|
|
|
ShapeConstraint: SameNumberOfRows<R2, D>,
|
|
|
|
{
|
2017-08-03 01:37:44 +08:00
|
|
|
let mut res = b.clone_owned();
|
|
|
|
self.solve_mut(&mut res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the inverse of the decomposed matrix.
|
|
|
|
pub fn inverse(&self) -> MatrixN<N, D> {
|
|
|
|
let shape = self.chol.data.shape();
|
|
|
|
let mut res = MatrixN::identity_generic(shape.0, shape.1);
|
|
|
|
|
|
|
|
self.solve_mut(&mut res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 01:52:46 +08:00
|
|
|
|
2019-03-03 02:33:49 +08:00
|
|
|
impl<N: Complex, D: DimSub<Dynamic>, S: Storage<N, D, D>> SquareMatrix<N, D, S>
|
2018-10-22 13:00:10 +08:00
|
|
|
where DefaultAllocator: Allocator<N, D, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2017-08-14 01:53:04 +08:00
|
|
|
/// Attempts to compute the Cholesky decomposition of this matrix.
|
2017-08-14 01:52:46 +08:00
|
|
|
///
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Returns `None` if the input matrix is not definite-positive. The input matrix is assumed
|
2017-08-14 01:52:46 +08:00
|
|
|
/// to be symmetric and only the lower-triangular part is read.
|
|
|
|
pub fn cholesky(self) -> Option<Cholesky<N, D>> {
|
|
|
|
Cholesky::new(self.into_owned())
|
|
|
|
}
|
|
|
|
}
|