From 598cb4fa8d9010dd95aa274e7764a178f13697c8 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Wed, 11 Jan 2023 15:16:16 +0200 Subject: [PATCH 1/3] Add ln_determinant to Cholesky --- src/linalg/cholesky.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/linalg/cholesky.rs b/src/linalg/cholesky.rs index f61a4e63..830cf2f1 100644 --- a/src/linalg/cholesky.rs +++ b/src/linalg/cholesky.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use num::One; +use num::Zero; use simba::scalar::ComplexField; use simba::simd::SimdComplexField; @@ -41,6 +42,7 @@ where impl Cholesky where DefaultAllocator: Allocator, + T::SimdRealField: Zero, { /// Computes the Cholesky decomposition of `matrix` without checking that the matrix is definite-positive. /// @@ -161,6 +163,27 @@ where } prod_diag.simd_modulus_squared() } + + /// Computes the natural logarithm of determinant of the decomposed matrix. + /// + /// This method is more robust than `.determinant()` to very small or very + /// large determinants since it returns the natural logarithm of the + /// determinant rather than the determinant itself. + #[must_use] + pub fn ln_determinant(&self) -> T::SimdRealField { + let dim = self.chol.nrows(); + let mut sum_diag = T::SimdRealField::zero(); + for i in 0..dim { + sum_diag += unsafe { + self.chol + .get_unchecked((i, i)) + .clone() + .simd_modulus_squared() + .simd_ln() + }; + } + sum_diag + } } impl Cholesky From f333bb4ba5d1375978ca1bd84000dc1a81418c99 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Sat, 14 Jan 2023 13:03:09 +0200 Subject: [PATCH 2/3] Update src/linalg/cholesky.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Crozet --- src/linalg/cholesky.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/linalg/cholesky.rs b/src/linalg/cholesky.rs index 830cf2f1..eda64134 100644 --- a/src/linalg/cholesky.rs +++ b/src/linalg/cholesky.rs @@ -1,8 +1,7 @@ #[cfg(feature = "serde-serialize-no-std")] use serde::{Deserialize, Serialize}; -use num::One; -use num::Zero; +use num::{One, Zero}; use simba::scalar::ComplexField; use simba::simd::SimdComplexField; From ff88fad23cf92fe6ca56516deccc5c1784971773 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Sat, 14 Jan 2023 13:24:04 +0200 Subject: [PATCH 3/3] Remove redundant `T::SimdRealField: Zero` --- src/linalg/cholesky.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/linalg/cholesky.rs b/src/linalg/cholesky.rs index eda64134..2ff22914 100644 --- a/src/linalg/cholesky.rs +++ b/src/linalg/cholesky.rs @@ -41,7 +41,6 @@ where impl Cholesky where DefaultAllocator: Allocator, - T::SimdRealField: Zero, { /// Computes the Cholesky decomposition of `matrix` without checking that the matrix is definite-positive. ///