mljs / matrix

Matrix manipulation and computation library
https://mljs.github.io/matrix/
MIT License
358 stars 54 forks source link

SVD output gives unanticipated left and right singular matrices. #111

Closed geoff-kf-lee closed 3 years ago

geoff-kf-lee commented 3 years ago

I'd like to start by saying I'm not as much of a mathematician so this may be a non-issue and is simply my lack of understanding of linear algebra. However, I have cross-referenced the code and output for SVDs very thoroughly against other implementations in other languages so this discrepancy sticks out to me.

Given a matrix M:

M = [
  [5, 5, 5],
  [5, 5, 5],
  [5, 5, 5]
]

Running new SingularValueDecomposition returns the following left and right matrices:

0: Float64Array(3) [-0.5773502691896257, -5.551115123125783e-17, 0.8164965809277261]
1: Float64Array(3) [-0.5773502691896258, -0.7071067811865475, -0.40824829046386285]
2: Float64Array(3) [-0.5773502691896258, 0.7071067811865476, -0.40824829046386285]
0: Float64Array(3) [-0.5773502691896257, -0, 0.8164965809277261]
1: Float64Array(3) [-0.5773502691896258, 0.7071067811865475, -0.40824829046386296]
2: Float64Array(3) [-0.5773502691896258, -0.7071067811865476, -0.40824829046386296]

The second and third columns of the resultant left and right matrices are flipped - any further operations on these will result in very strange issues.