From dc571838bbc8d8bf3a1f846fa8c22c3ff93cc4eb Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 7 Aug 2015 15:03:38 +0200 Subject: [PATCH] Added check for symmetricity of input matrix --- src/linalg/decompositions.rs | 6 +++++- tests/mat.rs | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/linalg/decompositions.rs b/src/linalg/decompositions.rs index 01fad087..e56e8cc4 100644 --- a/src/linalg/decompositions.rs +++ b/src/linalg/decompositions.rs @@ -127,7 +127,11 @@ pub fn cholesky(m: &M) -> Result Sub + ColSlice + ApproxEq + Copy { - let mut out = m.clone(); + let mut out = m.clone().transpose(); + + if !ApproxEq::approx_eq(&out, &m) { + return Err("Cholesky: Input matrix is not symmetric"); + } for i in 0..out.nrows() { for j in 0..(i+1) { diff --git a/tests/mat.rs b/tests/mat.rs index 62edbfd2..28878b40 100644 --- a/tests/mat.rs +++ b/tests/mat.rs @@ -658,6 +658,19 @@ fn test_cholesky_not_spd() { } } +#[test] +fn test_cholesky_not_symmetric() { + + let a : Mat2 = Mat2::::new(1.0, 1.0, -1.0, 1.0); + + let result = na::cholesky(&a); + + match result { + Ok(_) => assert!(false), + Err(_) => assert!(true), + } +} + #[test] fn test_cholesky_mat1() { test_cholesky_impl!(Mat1);