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
|
|
|
|
2017-08-03 01:38:28 +08:00
|
|
|
use num::Zero;
|
|
|
|
use num_complex::Complex;
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
use na::allocator::Allocator;
|
2017-08-03 01:38:28 +08:00
|
|
|
use na::dimension::Dim;
|
2017-08-14 01:52:51 +08:00
|
|
|
use na::storage::Storage;
|
2021-04-11 17:00:38 +08:00
|
|
|
use na::{DefaultAllocator, Matrix, OMatrix, Scalar};
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
use lapack;
|
2017-08-03 01:38:28 +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-25 05:51:57 +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-25 05:51:57 +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-25 05:51:57 +08:00
|
|
|
)]
|
2017-08-14 01:53:04 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub struct Cholesky<T: Scalar, D: Dim>
|
2020-04-06 00:49:48 +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
|
|
|
l: OMatrix<T, D, D>,
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: Scalar + Copy, 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,
|
2020-04-06 00:49:48 +08:00
|
|
|
{
|
|
|
|
}
|
2017-08-14 01:53:04 +08:00
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
impl<T: CholeskyScalar + Zero, D: Dim> Cholesky<T, D>
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, D, D>,
|
2018-02-02 19:26:35 +08:00
|
|
|
{
|
2018-09-24 12:48:42 +08:00
|
|
|
/// Computes the cholesky decomposition of the given symmetric-definite-positive square
|
2017-08-03 01:38:28 +08:00
|
|
|
/// matrix.
|
|
|
|
///
|
|
|
|
/// Only the lower-triangular part of the input matrix is considered.
|
|
|
|
#[inline]
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn new(mut m: OMatrix<T, D, D>) -> Option<Self> {
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: check symmetry as well?
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
m.is_square(),
|
|
|
|
"Unable to compute the cholesky decomposition of a non-square matrix."
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
let uplo = b'L';
|
|
|
|
let dim = m.nrows() as i32;
|
2017-08-03 01:38:28 +08:00
|
|
|
let mut info = 0;
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xpotrf(uplo, dim, m.as_mut_slice(), dim, &mut info);
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
2019-02-17 05:29:41 +08:00
|
|
|
Some(Self { l: m })
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
/// Retrieves the lower-triangular factor of the cholesky decomposition.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn unpack(mut self) -> OMatrix<T, D, D> {
|
2017-08-03 01:38:28 +08:00
|
|
|
self.l.fill_upper_triangle(Zero::zero(), 1);
|
|
|
|
self.l
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
/// Retrieves the lower-triangular factor of che 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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn unpack_dirty(self) -> OMatrix<T, D, D> {
|
2017-08-14 01:52:58 +08:00
|
|
|
self.l
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the lower-triangular factor of the cholesky decomposition.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn l(&self) -> OMatrix<T, D, D> {
|
2017-08-03 01:38:28 +08:00
|
|
|
let mut res = self.l.clone();
|
|
|
|
res.fill_upper_triangle(Zero::zero(), 1);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2017-08-14 01:52:58 +08:00
|
|
|
/// 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.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn l_dirty(&self) -> &OMatrix<T, D, D> {
|
2017-08-14 01:52:58 +08:00
|
|
|
&self.l
|
|
|
|
}
|
|
|
|
|
2017-08-03 01:38:28 +08:00
|
|
|
/// Solves the symmetric-definite-positive linear system `self * x = b`, where `x` is the
|
|
|
|
/// unknown to be determined.
|
2018-02-02 19:26:35 +08:00
|
|
|
pub fn solve<R2: Dim, C2: Dim, S2>(
|
|
|
|
&self,
|
2021-04-11 17:00:38 +08:00
|
|
|
b: &Matrix<T, R2, C2, S2>,
|
|
|
|
) -> Option<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
|
|
|
{
|
2017-08-14 01:52:51 +08:00
|
|
|
let mut res = b.clone_owned();
|
|
|
|
if self.solve_mut(&mut res) {
|
|
|
|
Some(res)
|
2018-02-02 19:26:35 +08:00
|
|
|
} else {
|
2017-08-14 01:52:51 +08:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Solves in-place the symmetric-definite-positive linear system `self * x = b`, where `x` is
|
|
|
|
/// the unknown to be determined.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn solve_mut<R2: Dim, C2: Dim>(&self, b: &mut OMatrix<T, R2, C2>) -> bool
|
2020-04-06 00:49:48 +08:00
|
|
|
where
|
2021-04-11 17:00:38 +08:00
|
|
|
DefaultAllocator: Allocator<T, R2, C2>,
|
2020-04-06 00:49:48 +08:00
|
|
|
{
|
2017-08-03 01:38:28 +08:00
|
|
|
let dim = self.l.nrows();
|
|
|
|
|
2018-02-02 19:26:35 +08:00
|
|
|
assert!(
|
|
|
|
b.nrows() == dim,
|
|
|
|
"The number of rows of `b` must be equal to the dimension of the matrix `a`."
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
|
|
|
|
let nrhs = b.ncols() as i32;
|
2018-02-02 19:26:35 +08:00
|
|
|
let lda = dim as i32;
|
|
|
|
let ldb = dim as i32;
|
2017-08-03 01:38:28 +08:00
|
|
|
let mut info = 0;
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xpotrs(
|
2018-02-02 19:26:35 +08:00
|
|
|
b'L',
|
|
|
|
dim as i32,
|
|
|
|
nrhs,
|
|
|
|
self.l.as_slice(),
|
|
|
|
lda,
|
|
|
|
b.as_mut_slice(),
|
|
|
|
ldb,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-14 01:52:51 +08:00
|
|
|
lapack_test!(info)
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the inverse of the decomposed matrix.
|
2021-04-11 17:00:38 +08:00
|
|
|
pub fn inverse(mut self) -> Option<OMatrix<T, D, D>> {
|
2017-08-03 01:38:28 +08:00
|
|
|
let dim = self.l.nrows();
|
|
|
|
let mut info = 0;
|
|
|
|
|
2021-04-11 17:00:38 +08:00
|
|
|
T::xpotri(
|
2018-02-02 19:26:35 +08:00
|
|
|
b'L',
|
|
|
|
dim as i32,
|
|
|
|
self.l.as_mut_slice(),
|
|
|
|
dim as i32,
|
|
|
|
&mut info,
|
|
|
|
);
|
2017-08-03 01:38:28 +08:00
|
|
|
lapack_check!(info);
|
|
|
|
|
|
|
|
// Copy lower triangle to upper triangle.
|
2018-02-02 19:26:35 +08:00
|
|
|
for i in 0..dim {
|
|
|
|
for j in i + 1..dim {
|
2018-12-03 04:00:08 +08:00
|
|
|
unsafe { *self.l.get_unchecked_mut((i, j)) = *self.l.get_unchecked((j, i)) };
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(self.l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Lapack functions dispatch.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/// Trait implemented by floats (`f32`, `f64`) and complex floats (`Complex<f32>`, `Complex<f64>`)
|
2018-09-24 12:48:42 +08:00
|
|
|
/// supported by the cholesky decomposition.
|
2019-11-22 06:15:18 +08:00
|
|
|
pub trait CholeskyScalar: Scalar + Copy {
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2017-08-03 01:38:28 +08:00
|
|
|
fn xpotrf(uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32);
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2018-02-02 19:26:35 +08:00
|
|
|
fn xpotrs(
|
|
|
|
uplo: u8,
|
|
|
|
n: i32,
|
|
|
|
nrhs: i32,
|
|
|
|
a: &[Self],
|
|
|
|
lda: i32,
|
|
|
|
b: &mut [Self],
|
|
|
|
ldb: i32,
|
|
|
|
info: &mut i32,
|
|
|
|
);
|
2017-08-14 01:52:58 +08:00
|
|
|
#[allow(missing_docs)]
|
2017-08-03 01:38:28 +08:00
|
|
|
fn xpotri(uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32);
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! cholesky_scalar_impl(
|
|
|
|
($N: ty, $xpotrf: path, $xpotrs: path, $xpotri: path) => (
|
|
|
|
impl CholeskyScalar for $N {
|
|
|
|
#[inline]
|
|
|
|
fn xpotrf(uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32) {
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xpotrf(uplo, n, a, lda, info) }
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn xpotrs(uplo: u8, n: i32, nrhs: i32, a: &[Self], lda: i32,
|
|
|
|
b: &mut [Self], ldb: i32, info: &mut i32) {
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xpotrs(uplo, n, nrhs, a, lda, b, ldb, info) }
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn xpotri(uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32) {
|
2018-05-25 05:51:57 +08:00
|
|
|
unsafe { $xpotri(uplo, n, a, lda, info) }
|
2017-08-03 01:38:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-05-25 05:51:57 +08:00
|
|
|
cholesky_scalar_impl!(f32, lapack::spotrf, lapack::spotrs, lapack::spotri);
|
|
|
|
cholesky_scalar_impl!(f64, lapack::dpotrf, lapack::dpotrs, lapack::dpotri);
|
|
|
|
cholesky_scalar_impl!(Complex<f32>, lapack::cpotrf, lapack::cpotrs, lapack::cpotri);
|
|
|
|
cholesky_scalar_impl!(Complex<f64>, lapack::zpotrf, lapack::zpotrs, lapack::zpotri);
|