nalgebra: Singular Value Decomposition (SVD) returns wrong decomposition [bug]
PROBLEM:
Hey, I’m trying to do a singular value decomposition of a 2x2 Matrix, and it seems that the decomposition performed by nalgebra when comparing to numpy and an online calculator for SVD is wrong. I get consistent results with numpy and a online calculator, however nalgebra produce a different decomposition. To me it seems that the decomposition performed by nalgebra produces the correct numerical values, just in the wrong places and with the wrong sign. Also the decomposition by numpy and the online calculator has u and v_t being identical matrices, which is not the case in nalgebra.
To reproduce my results, you can do:
SVD IN RUST WITH NALGEBRA
let H = na::Matrix2::new(8.75, 10.75,
10.75, 14.75);
// testing both approaches
let svd1 = na::linalg::SVD::new(H, true, true);
let svd2 = H.svd(true, true);
// output from both svd1 and svd2 are:
u: Matrix { data: [[0.7964919860908672, -0.6046490850840893],
[0.6046490850840898, 0.7964919860908671]] }
v_t: Matrix { data: [[0.7964919860908662, 0.60464908508409],
[-0.60464908508409, 0.7964919860908662]] }
s: Matrix { data: [[0.5892428572251418, 22.910757142774862]] }
SVD IN PYTHON WITH NUMPY
I compared this to numpy in Python:
H = np.array([[ 8.75, 10.75],
[10.75, 14.75]])
U, S, Vt = np.linalg.svd(H)
// output
H: [[ 8.75 10.75]
[10.75 14.75]]
U: [[-0.60464909 -0.79649199]
[-0.79649199 0.60464909]]
Vt: [[-0.60464909 -0.79649199]
[-0.79649199 0.60464909]]
S: [22.91075714 0.58924286]
SVD WITH ONLINE CALCULATOR see link to calculator

About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15
@lebensterben I’m now able to successfully compute the rotation matrix and translation matrix by not transposing the vectors and matrices like I do in Python.
So in Python I transpose the
v_tandumatrices and then compute the product to get the rotation matrix. I also transpose the centroids which are 2-dim vectorsin rust I had to skip the
transposeto achieve the same resultThank you for your time and patience 👍 I hope though you could have a look into the debug format of the matrices. I found it hard to debug with the print showing matrices in column-major format.