dimforge / dimforge/nalgebra

2×2 SVD is ~7 digits less accurate through `DMatrix` than through `Matrix2`

Open
#1,612 0 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

```rust
use nalgebra::{Matrix2, DMatrix};

let vals = [2.27, -2.5e-6, 0.0, 9.008];
let a = Matrix2::new(vals[0], vals[1], vals[2], vals[3]);
let ad = DMatrix::from_row_slice(2, 2, &vals);

let e_static = (a.svd(true, true).recompose().unwrap() - a).norm();
let e_dynamic = (ad.clone().svd(true, true).recompose().unwrap() - ad).norm();
println!("static {e_static:.3e}");
println!("dynamic {e_dynamic:.3e}");
```

prints

```
static 2.051e-15
dynamic 8.966e-9
```

Reconstructing the same matrix from its own SVD (`U * Σ * Vᵀ`) is accurate to ~2e-15 through the static `Matrix2` path but only ~9e-9 through the dynamic `DMatrix` path — about seven digits worse for an ordinary, well-conditioned matrix.

Tested on nalgebra 0.35.0.

BTW, this bug was found using [hegel](https://crates.io/crates/hegeltest). Happy to contribute the tests if you're interested.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the Matrix2 and DMatrix SVD reconstruction example against nalgebra 0.35.0, then trace the two SVD paths to find where their accuracy diverges. Done means the dynamic DMatrix reconstruction no longer loses roughly seven digits and regression tests cover the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.