Merge pull request #1192 from fortify-iq/ln-determinant

Add ln_determinant to Cholesky
This commit is contained in:
Sébastien Crozet 2023-01-14 14:41:57 +01:00 committed by GitHub
commit 9e5854034c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 1 deletions

View File

@ -1,7 +1,7 @@
#[cfg(feature = "serde-serialize-no-std")]
use serde::{Deserialize, Serialize};
use num::One;
use num::{One, Zero};
use simba::scalar::ComplexField;
use simba::simd::SimdComplexField;
@ -161,6 +161,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<T: ComplexField, D: Dim> Cholesky<T, D>