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-11-17 20:10:50 +08:00
|
|
|
use num::One;
|
2020-03-21 19:16:46 +08:00
|
|
|
use simba::scalar::ComplexField;
|
2020-05-23 21:13:13 +08:00
|
|
|
use simba::simd::SimdComplexField;
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::allocator::Allocator;
|
2021-04-11 17:00:38 +08:00
|
|
|
use crate::base::{Const, DefaultAllocator, Matrix, OMatrix, Vector};
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::constraint::{SameNumberOfRows, ShapeConstraint};
|
2020-08-04 00:03:01 +08:00
|
|
|
use crate::dimension::{Dim, DimAdd, DimDiff, DimSub, DimSum, U1};
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::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",
|
2021-04-11 17:00:38 +08:00
|
|
|
serde(bound(serialize = "DefaultAllocator: Allocator<T, D>,
|
|
|
|
OMatrix<T, D, D>: Serialize"))
|
2018-05-19 23:15:15 +08:00
|
|
|
)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde-serialize",
|
2021-04-11 17:00:38 +08:00
|
|
|
serde(bound(deserialize = "DefaultAllocator: Allocator<T, D>,
|
|
|
|
OMatrix<T, D, D>: Deserialize<'de>"))
|
2018-05-19 23:15:15 +08:00
|
|
|
)]
|
2017-08-14 01:53:00 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub struct Cholesky<T: SimdComplexField, D: Dim>
|
2020-06-07 15:07:25 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2021-04-11 17:00:38 +08:00
|
|
|
chol: OMatrix<T, D, D>,
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: SimdComplexField, D: Dim> Copy for Cholesky<T, D>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D>,
|
|
|
|
OMatrix<T, D, D>: Copy,
|
2019-11-02 21:59:07 +08:00
|
|
|
{
|
|
|
|
}
|
2017-08-14 01:53:00 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: SimdComplexField, D: Dim> Cholesky<T, D>
|
2020-06-07 15:07:25 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2020-05-23 21:13:13 +08:00
|
|
|
/// Computes the Cholesky decomposition of `matrix` without checking that the matrix is definite-positive.
|
2017-08-03 01:37:44 +08:00
|
|
|
///
|
2020-05-23 21:13:13 +08:00
|
|
|
/// If the input matrix is not definite-positive, the decomposition may contain trash values (Inf, NaN, etc.)
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new_unchecked(mut matrix: OMatrix<T, D, D>) -> Self {
|
2017-08-03 01:37:44 +08:00
|
|
|
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..);
|
2021-04-11 17:00:38 +08:00
|
|
|
col_j.axpy(factor.simd_conjugate(), &col_k, T::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)) };
|
2020-05-23 21:13:13 +08:00
|
|
|
let denom = diag.simd_sqrt();
|
2019-03-23 18:46:56 +08:00
|
|
|
|
2020-05-23 21:13:13 +08:00
|
|
|
unsafe {
|
|
|
|
*matrix.get_unchecked_mut((j, j)) = denom;
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
2019-03-23 18:46:56 +08:00
|
|
|
|
2020-05-23 21:13:13 +08:00
|
|
|
let mut col = matrix.slice_range_mut(j + 1.., j);
|
|
|
|
col /= denom;
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
2020-05-23 21:13:13 +08:00
|
|
|
Cholesky { chol: matrix }
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn unpack(mut self) -> OMatrix<T, D, D> {
|
|
|
|
self.chol.fill_upper_triangle(T::zero(), 1);
|
2017-08-03 01:37:44 +08:00
|
|
|
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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn unpack_dirty(self) -> OMatrix<T, D, D> {
|
2017-08-03 01:37:44 +08:00
|
|
|
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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn l(&self) -> OMatrix<T, D, D> {
|
2017-08-03 01:37:44 +08:00
|
|
|
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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn l_dirty(&self) -> &OMatrix<T, D, D> {
|
2017-08-03 01:37:44 +08:00
|
|
|
&self.chol
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Solves the system `self * x = b` where `self` is the decomposed matrix and `x` the unknown.
|
|
|
|
///
|
|
|
|
/// The result is stored on `b`.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn solve_mut<R2: Dim, C2: Dim, S2>(&self, b: &mut Matrix<T, R2, C2, S2>)
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S2: StorageMut<T, R2, C2>,
|
2018-02-02 19:26:35 +08:00
|
|
|
ShapeConstraint: SameNumberOfRows<R2, D>,
|
|
|
|
{
|
2020-05-23 21:13:13 +08:00
|
|
|
self.chol.solve_lower_triangular_unchecked_mut(b);
|
|
|
|
self.chol.ad_solve_lower_triangular_unchecked_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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn solve<R2: Dim, C2: Dim, S2>(&self, b: &Matrix<T, R2, C2, S2>) -> OMatrix<T, R2, C2>
|
2018-02-02 19:26:35 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S2: Storage<T, R2, C2>,
|
|
|
|
DefaultAllocator: Allocator<T, R2, C2>,
|
2018-02-02 19:26:35 +08:00
|
|
|
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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn inverse(&self) -> OMatrix<T, D, D> {
|
2017-08-03 01:37:44 +08:00
|
|
|
let shape = self.chol.data.shape();
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut res = OMatrix::identity_generic(shape.0, shape.1);
|
2017-08-03 01:37:44 +08:00
|
|
|
|
|
|
|
self.solve_mut(&mut res);
|
|
|
|
res
|
|
|
|
}
|
2021-04-08 10:43:27 +08:00
|
|
|
|
|
|
|
/// Computes the determinant of the decomposed matrix.
|
2021-04-10 01:55:40 +08:00
|
|
|
pub fn determinant(&self) -> N::SimdRealField {
|
2021-04-08 10:43:27 +08:00
|
|
|
let dim = self.chol.nrows();
|
|
|
|
let mut prod_diag = N::one();
|
|
|
|
for i in 0..dim {
|
|
|
|
prod_diag *= unsafe { *self.chol.get_unchecked((i, i)) };
|
|
|
|
}
|
2021-04-10 01:55:40 +08:00
|
|
|
prod_diag.simd_modulus_squared()
|
2021-04-08 10:43:27 +08:00
|
|
|
}
|
2020-05-23 21:13:13 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: ComplexField, D: Dim> Cholesky<T, D>
|
2020-06-07 15:07:25 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D>,
|
2020-05-23 21:13:13 +08:00
|
|
|
{
|
|
|
|
/// 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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new(mut matrix: OMatrix<T, D, D>) -> Option<Self> {
|
2020-05-23 21:13:13 +08:00
|
|
|
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..);
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
col_j.axpy(factor.conjugate(), &col_k, T::one());
|
2020-05-23 21:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let diag = unsafe { *matrix.get_unchecked((j, j)) };
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The diagonal element is either zero or its square root could not
|
|
|
|
// be taken (e.g. for negative real numbers).
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Cholesky { chol: matrix })
|
|
|
|
}
|
2019-11-02 21:59:07 +08:00
|
|
|
|
2019-11-02 23:49:57 +08:00
|
|
|
/// Given the Cholesky decomposition of a matrix `M`, a scalar `sigma` and a vector `v`,
|
2019-11-17 20:24:00 +08:00
|
|
|
/// performs a rank one update such that we end up with the decomposition of `M + sigma * (v * v.adjoint())`.
|
2019-11-04 03:00:15 +08:00
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn rank_one_update<R2: Dim, S2>(&mut self, x: &Vector<T, R2, S2>, sigma: T::RealField)
|
2019-11-03 01:27:01 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
S2: Storage<T, R2, U1>,
|
|
|
|
DefaultAllocator: Allocator<T, R2, U1>,
|
2019-11-02 21:59:07 +08:00
|
|
|
ShapeConstraint: SameNumberOfRows<R2, D>,
|
|
|
|
{
|
2019-11-17 20:10:50 +08:00
|
|
|
Self::xx_rank_one_update(&mut self.chol, &mut x.clone_owned(), sigma)
|
2019-11-02 21:59:07 +08:00
|
|
|
}
|
2019-11-03 20:20:56 +08:00
|
|
|
|
2019-11-04 03:00:15 +08:00
|
|
|
/// Updates the decomposition such that we get the decomposition of a matrix with the given column `col` in the `j`th position.
|
2019-11-03 20:20:56 +08:00
|
|
|
/// Since the matrix is square, an identical row will be added in the `j`th row.
|
2019-11-04 01:02:27 +08:00
|
|
|
pub fn insert_column<R2, S2>(
|
2019-11-17 20:10:50 +08:00
|
|
|
&self,
|
2019-11-03 20:20:56 +08:00
|
|
|
j: usize,
|
2021-04-11 17:00:38 +08:00
|
|
|
col: Vector<T, R2, S2>,
|
|
|
|
) -> Cholesky<T, DimSum<D, U1>>
|
2019-11-03 20:20:56 +08:00
|
|
|
where
|
|
|
|
D: DimAdd<U1>,
|
2019-11-04 01:02:27 +08:00
|
|
|
R2: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
S2: Storage<T, R2, U1>,
|
|
|
|
DefaultAllocator: Allocator<T, DimSum<D, U1>, DimSum<D, U1>> + Allocator<T, R2>,
|
2019-11-03 20:20:56 +08:00
|
|
|
ShapeConstraint: SameNumberOfRows<R2, DimSum<D, U1>>,
|
|
|
|
{
|
2019-11-17 20:10:50 +08:00
|
|
|
let mut col = col.into_owned();
|
2019-11-04 01:02:27 +08:00
|
|
|
// for an explanation of the formulas, see https://en.wikipedia.org/wiki/Cholesky_decomposition#Updating_the_decomposition
|
2019-11-03 22:17:20 +08:00
|
|
|
let n = col.nrows();
|
2020-03-21 19:16:46 +08:00
|
|
|
assert_eq!(
|
|
|
|
n,
|
|
|
|
self.chol.nrows() + 1,
|
|
|
|
"The new column must have the size of the factored matrix plus one."
|
|
|
|
);
|
2019-11-03 20:20:56 +08:00
|
|
|
assert!(j < n, "j needs to be within the bound of the new matrix.");
|
2019-11-04 03:00:15 +08:00
|
|
|
|
2019-11-04 04:24:44 +08:00
|
|
|
// loads the data into a new matrix with an additional jth row/column
|
2020-03-21 19:16:46 +08:00
|
|
|
let mut chol = unsafe {
|
2020-11-28 04:58:48 +08:00
|
|
|
crate::unimplemented_or_uninitialized_generic!(
|
2021-01-03 22:20:34 +08:00
|
|
|
self.chol.data.shape().0.add(Const::<1>),
|
|
|
|
self.chol.data.shape().1.add(Const::<1>)
|
2020-03-21 19:16:46 +08:00
|
|
|
)
|
|
|
|
};
|
|
|
|
chol.slice_range_mut(..j, ..j)
|
|
|
|
.copy_from(&self.chol.slice_range(..j, ..j));
|
|
|
|
chol.slice_range_mut(..j, j + 1..)
|
|
|
|
.copy_from(&self.chol.slice_range(..j, j..));
|
|
|
|
chol.slice_range_mut(j + 1.., ..j)
|
|
|
|
.copy_from(&self.chol.slice_range(j.., ..j));
|
|
|
|
chol.slice_range_mut(j + 1.., j + 1..)
|
|
|
|
.copy_from(&self.chol.slice_range(j.., j..));
|
2019-11-03 22:17:20 +08:00
|
|
|
|
2019-11-03 22:43:49 +08:00
|
|
|
// update the jth row
|
2019-11-04 03:00:15 +08:00
|
|
|
let top_left_corner = self.chol.slice_range(..j, ..j);
|
2019-11-17 20:10:50 +08:00
|
|
|
|
|
|
|
let col_j = col[j];
|
|
|
|
let (mut new_rowj_adjoint, mut new_colj) = col.rows_range_pair_mut(..j, j + 1..);
|
2020-03-21 19:16:46 +08:00
|
|
|
assert!(
|
|
|
|
top_left_corner.solve_lower_triangular_mut(&mut new_rowj_adjoint),
|
|
|
|
"Cholesky::insert_column : Unable to solve lower triangular system!"
|
|
|
|
);
|
2019-11-17 20:10:50 +08:00
|
|
|
|
2019-11-04 03:00:15 +08:00
|
|
|
new_rowj_adjoint.adjoint_to(&mut chol.slice_range_mut(j, ..j));
|
2019-11-03 22:17:20 +08:00
|
|
|
|
2019-11-03 22:43:49 +08:00
|
|
|
// update the center element
|
2021-04-11 17:00:38 +08:00
|
|
|
let center_element = T::sqrt(col_j - T::from_real(new_rowj_adjoint.norm_squared()));
|
2019-11-17 20:10:50 +08:00
|
|
|
chol[(j, j)] = center_element;
|
2019-11-03 22:17:20 +08:00
|
|
|
|
2019-11-03 22:43:49 +08:00
|
|
|
// update the jth column
|
2019-11-04 03:00:15 +08:00
|
|
|
let bottom_left_corner = self.chol.slice_range(j.., ..j);
|
|
|
|
// new_colj = (col_jplus - bottom_left_corner * new_rowj.adjoint()) / center_element;
|
2020-03-21 19:16:46 +08:00
|
|
|
new_colj.gemm(
|
2021-04-11 17:00:38 +08:00
|
|
|
-T::one() / center_element,
|
2020-03-21 19:16:46 +08:00
|
|
|
&bottom_left_corner,
|
|
|
|
&new_rowj_adjoint,
|
2021-04-11 17:00:38 +08:00
|
|
|
T::one() / center_element,
|
2020-03-21 19:16:46 +08:00
|
|
|
);
|
2019-11-17 20:24:00 +08:00
|
|
|
chol.slice_range_mut(j + 1.., j).copy_from(&new_colj);
|
2019-11-03 22:17:20 +08:00
|
|
|
|
|
|
|
// update the bottom right corner
|
2019-11-17 20:24:00 +08:00
|
|
|
let mut bottom_right_corner = chol.slice_range_mut(j + 1.., j + 1..);
|
2020-03-21 19:16:46 +08:00
|
|
|
Self::xx_rank_one_update(
|
|
|
|
&mut bottom_right_corner,
|
|
|
|
&mut new_colj,
|
2021-04-11 17:00:38 +08:00
|
|
|
-T::RealField::one(),
|
2020-03-21 19:16:46 +08:00
|
|
|
);
|
2019-11-03 20:20:56 +08:00
|
|
|
|
|
|
|
Cholesky { chol }
|
|
|
|
}
|
2019-11-03 20:26:18 +08:00
|
|
|
|
|
|
|
/// Updates the decomposition such that we get the decomposition of the factored matrix with its `j`th column removed.
|
|
|
|
/// Since the matrix is square, the `j`th row will also be removed.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn remove_column(&self, j: usize) -> Cholesky<T, DimDiff<D, U1>>
|
2020-03-21 19:16:46 +08:00
|
|
|
where
|
|
|
|
D: DimSub<U1>,
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, DimDiff<D, U1>, DimDiff<D, U1>> + Allocator<T, D>,
|
2019-11-03 20:26:18 +08:00
|
|
|
{
|
|
|
|
let n = self.chol.nrows();
|
2019-11-03 21:33:35 +08:00
|
|
|
assert!(n > 0, "The matrix needs at least one column.");
|
2019-11-03 20:26:18 +08:00
|
|
|
assert!(j < n, "j needs to be within the bound of the matrix.");
|
2019-11-04 03:00:15 +08:00
|
|
|
|
2019-11-04 04:24:44 +08:00
|
|
|
// loads the data into a new matrix except for the jth row/column
|
2020-03-21 19:16:46 +08:00
|
|
|
let mut chol = unsafe {
|
2020-11-28 04:58:48 +08:00
|
|
|
crate::unimplemented_or_uninitialized_generic!(
|
2021-01-03 22:20:34 +08:00
|
|
|
self.chol.data.shape().0.sub(Const::<1>),
|
|
|
|
self.chol.data.shape().1.sub(Const::<1>)
|
2020-03-21 19:16:46 +08:00
|
|
|
)
|
|
|
|
};
|
|
|
|
chol.slice_range_mut(..j, ..j)
|
|
|
|
.copy_from(&self.chol.slice_range(..j, ..j));
|
|
|
|
chol.slice_range_mut(..j, j..)
|
|
|
|
.copy_from(&self.chol.slice_range(..j, j + 1..));
|
|
|
|
chol.slice_range_mut(j.., ..j)
|
|
|
|
.copy_from(&self.chol.slice_range(j + 1.., ..j));
|
|
|
|
chol.slice_range_mut(j.., j..)
|
|
|
|
.copy_from(&self.chol.slice_range(j + 1.., j + 1..));
|
2019-11-03 21:33:35 +08:00
|
|
|
|
2019-11-03 22:17:20 +08:00
|
|
|
// updates the bottom right corner
|
2019-11-04 03:00:15 +08:00
|
|
|
let mut bottom_right_corner = chol.slice_range_mut(j.., j..);
|
2019-11-17 20:10:50 +08:00
|
|
|
let mut workspace = self.chol.column(j).clone_owned();
|
2019-11-17 20:24:00 +08:00
|
|
|
let mut old_colj = workspace.rows_range_mut(j + 1..);
|
2021-04-11 17:00:38 +08:00
|
|
|
Self::xx_rank_one_update(&mut bottom_right_corner, &mut old_colj, T::RealField::one());
|
2019-11-03 20:26:18 +08:00
|
|
|
|
|
|
|
Cholesky { chol }
|
|
|
|
}
|
2019-11-04 04:24:44 +08:00
|
|
|
|
2019-11-17 20:40:19 +08:00
|
|
|
/// Given the Cholesky decomposition of a matrix `M`, a scalar `sigma` and a vector `x`,
|
2019-11-17 20:24:00 +08:00
|
|
|
/// performs a rank one update such that we end up with the decomposition of `M + sigma * (x * x.adjoint())`.
|
2019-11-04 04:24:44 +08:00
|
|
|
///
|
2019-11-17 20:40:19 +08:00
|
|
|
/// This helper method is called by `rank_one_update` but also `insert_column` and `remove_column`
|
2019-11-04 04:24:44 +08:00
|
|
|
/// where it is used on a square slice of the decomposition
|
2020-03-21 19:16:46 +08:00
|
|
|
fn xx_rank_one_update<Dm, Sm, Rx, Sx>(
|
2021-04-11 17:00:38 +08:00
|
|
|
chol: &mut Matrix<T, Dm, Dm, Sm>,
|
|
|
|
x: &mut Vector<T, Rx, Sx>,
|
|
|
|
sigma: T::RealField,
|
2020-03-21 19:16:46 +08:00
|
|
|
) where
|
2021-04-11 17:00:38 +08:00
|
|
|
//T: ComplexField,
|
2020-03-21 19:16:46 +08:00
|
|
|
Dm: Dim,
|
|
|
|
Rx: Dim,
|
2021-04-11 17:00:38 +08:00
|
|
|
Sm: StorageMut<T, Dm, Dm>,
|
|
|
|
Sx: StorageMut<T, Rx, U1>,
|
2019-11-04 04:24:44 +08:00
|
|
|
{
|
|
|
|
// heavily inspired by Eigen's `llt_rank_update_lower` implementation https://eigen.tuxfamily.org/dox/LLT_8h_source.html
|
|
|
|
let n = x.nrows();
|
|
|
|
assert_eq!(
|
|
|
|
n,
|
|
|
|
chol.nrows(),
|
|
|
|
"The input vector must be of the same size as the factorized matrix."
|
|
|
|
);
|
2019-11-17 20:10:50 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
let mut beta = crate::one::<T::RealField>();
|
2019-11-17 20:10:50 +08:00
|
|
|
|
2019-11-04 04:24:44 +08:00
|
|
|
for j in 0..n {
|
|
|
|
// updates the diagonal
|
2021-04-11 17:00:38 +08:00
|
|
|
let diag = T::real(unsafe { *chol.get_unchecked((j, j)) });
|
2019-11-04 04:24:44 +08:00
|
|
|
let diag2 = diag * diag;
|
|
|
|
let xj = unsafe { *x.get_unchecked(j) };
|
2021-04-11 17:00:38 +08:00
|
|
|
let sigma_xj2 = sigma * T::modulus_squared(xj);
|
2019-11-04 04:24:44 +08:00
|
|
|
let gamma = diag2 * beta + sigma_xj2;
|
|
|
|
let new_diag = (diag2 + sigma_xj2 / beta).sqrt();
|
2021-04-11 17:00:38 +08:00
|
|
|
unsafe { *chol.get_unchecked_mut((j, j)) = T::from_real(new_diag) };
|
2019-11-04 04:24:44 +08:00
|
|
|
beta += sigma_xj2 / diag2;
|
|
|
|
// updates the terms of L
|
|
|
|
let mut xjplus = x.rows_range_mut(j + 1..);
|
|
|
|
let mut col_j = chol.slice_range_mut(j + 1.., j);
|
2021-04-11 17:00:38 +08:00
|
|
|
// temp_jplus -= (wj / T::from_real(diag)) * col_j;
|
|
|
|
xjplus.axpy(-xj / T::from_real(diag), &col_j, T::one());
|
|
|
|
if gamma != crate::zero::<T::RealField>() {
|
|
|
|
// col_j = T::from_real(nljj / diag) * col_j + (T::from_real(nljj * sigma / gamma) * T::conjugate(wj)) * temp_jplus;
|
2019-11-04 04:24:44 +08:00
|
|
|
col_j.axpy(
|
2021-04-11 17:00:38 +08:00
|
|
|
T::from_real(new_diag * sigma / gamma) * T::conjugate(xj),
|
2019-11-04 04:24:44 +08:00
|
|
|
&xjplus,
|
2021-04-11 17:00:38 +08:00
|
|
|
T::from_real(new_diag / diag),
|
2019-11-04 04:24:44 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
}
|