Merge pull request #860 from jturner314/cholesky-determinant

Add determinant method to Cholesky
This commit is contained in:
Sébastien Crozet 2021-04-08 09:25:44 +02:00 committed by GitHub
commit 5aa6033a3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -138,6 +138,16 @@ where
self.solve_mut(&mut res); self.solve_mut(&mut res);
res res
} }
/// Computes the determinant of the decomposed matrix.
pub fn determinant(&self) -> N {
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)) };
}
prod_diag * prod_diag
}
} }
impl<N: ComplexField, D: Dim> Cholesky<N, D> impl<N: ComplexField, D: Dim> Cholesky<N, D>

View File

@ -79,6 +79,24 @@ macro_rules! gen_tests(
prop_assert!(id1.is_identity(1.0e-7) && id2.is_identity(1.0e-7)); prop_assert!(id1.is_identity(1.0e-7) && id2.is_identity(1.0e-7));
} }
#[test]
fn cholesky_determinant(n in PROPTEST_MATRIX_DIM) {
let m = RandomSDP::new(Dynamic::new(n), || random::<$scalar>().0).unwrap();
let lu_det = m.clone().lu().determinant();
let chol_det = m.cholesky().unwrap().determinant();
prop_assert!(relative_eq!(lu_det, chol_det, epsilon = 1.0e-7));
}
#[test]
fn cholesky_determinant_static(_n in PROPTEST_MATRIX_DIM) {
let m = RandomSDP::new(U4, || random::<$scalar>().0).unwrap();
let lu_det = m.clone().lu().determinant();
let chol_det = m.cholesky().unwrap().determinant();
prop_assert!(relative_eq!(lu_det, chol_det, epsilon = 1.0e-7));
}
#[test] #[test]
fn cholesky_rank_one_update(_n in PROPTEST_MATRIX_DIM) { fn cholesky_rank_one_update(_n in PROPTEST_MATRIX_DIM) {
let mut m = RandomSDP::new(U4, || random::<$scalar>().0).unwrap(); let mut m = RandomSDP::new(U4, || random::<$scalar>().0).unwrap();