dimforge / dimforge/nalgebra

SVD fails to order singular values for 3x3 matrix

Open
#1,215 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

I had a bug in my physics simulation (not rapier) that I tracked down to relying on svd to provide singular values in sorted order. The doc for `svd` states:

> Computes the Singular Value Decomposition using implicit shift. The singular values are guaranteed to be sorted in descending order. If this order is not required consider using svd_unordered.

But the singular values I was getting were not sorted. Here is a test that reproduces the bug:

```rs
#[test]
fn svd3_sorted() {
let mat = Matrix3::::new(
1.0,
0.0,
0.0,
0.0,
0.99999994,
0.0,
0.0,
-0.00000054691924,
1.0000005,
);
let svd = mat.svd(true, true);
assert!(
svd.singular_values[2] <= svd.singular_values[1]
&& svd.singular_values[1] <= svd.singular_values[0],
"Singular values are not sorted in descending order: {}",
svd.singular_values
);
}
```

This results in:

```
thread 'svd3_sorted' panicked at 'Singular values are not sorted in descending order:
┌ ┐
│ 1.0000005 │
│ 0.99999994 │
│ 1 │
└ ┘
```

I am using nalgebra version 0.32.1.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.