Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/linalg/cholesky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,16 @@ where
}

let sqrt_denom = |v: T| {
if v.is_zero() {
// positive definiteness requires the diagonal to be real and strictly positive
if !v.clone().imaginary().is_zero() {
return None;
}
v.try_sqrt()
let re = v.real();
if re <= T::RealField::zero() {
return None;
}
Comment on lines +239 to +245
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be clearer to write this as something like

let re = v.real();
let im = v.imaginary();

if re <= T::RealField::zero() || !im.is_zero() {
    return None;
}

// fixme: could be improved by sqrt on real, e.g. `T::from_real(re.sqrt())`
Some(T::from_real(re).sqrt())
};

let diag = unsafe { matrix.get_unchecked((j, j)).clone() };
Expand Down
14 changes: 14 additions & 0 deletions tests/linalg/cholesky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ fn cholesky_with_substitute() {
assert!(na::Cholesky::new_with_substitute(m, 1e-8).is_some());
}

// 2x2 matrix filled with -1 is not positive definite and should not return a Cholesky
// decomposition (regardless of element type).
// See https://github.com/dimforge/nalgebra/issues/1536 .
#[test]
fn cholesky_check_positive_definiteness() {
let x = -1.0;
let m = na::Matrix2::new(x, x, x, x);
assert!(nalgebra::Cholesky::new(m).is_none());

let x = na::Complex::new(-1.0, 0.0);
let m = na::Matrix2::new(x, x, x, x);
assert!(nalgebra::Cholesky::new(m).is_none());
}

macro_rules! gen_tests(
($module: ident, $scalar: ty) => {
mod $module {
Expand Down
Loading