sloisel / numeric

Numerical analysis in Javascript
http://www.numericjs.com/
Other
1.42k stars 177 forks source link

SVD outputs NaN for certain inputs #75

Open dllu opened 7 years ago

dllu commented 7 years ago

Example input:

IN> numeric.svd([[         1.2314470096270005,         -8.927990819795772],
 [         0.0710192233504547,        -0.5148893692907976]])

OUT> {U: [[ NaN, NaN], [ NaN, NaN]], S: [ 0, NaN], V: [[ 0.9906, 0.1366], [ 0.1366, -0.9906]]}

Tested on both Chromium and Firefox.

image

However, GNU Octave manages to compute a correct SVD without outputting NaN.

octave:23> A = [[         1.2314470096270005,         -8.927990819795772],
>  [         0.0710192233504547,        -0.5148893692907976]]
A =

   1.231447  -8.927991
   0.071019  -0.514889

octave:24> [U, S, V] = svd(A)
U =

  -0.998341  -0.057576
  -0.057576   0.998341

S =

Diagonal Matrix

   9.0275e+00            0
            0   3.0340e-17

V =

  -0.13664  -0.99062
   0.99062  -0.13664

octave:25> U * S * V'
ans =

   1.231447  -8.927991
   0.071019  -0.514889
cladelpino commented 7 years ago

can reproduce: x=[[1,2],[2,4]]

numeric.svd(x).S [0, NaN]

x=[[1,2],[3,6]]

numeric.svd(x).S [7.071067811865476, 0]

sloisel commented 7 years ago

Hi,

Thanks for this bug report. I apologize, I can't really make the fixes. I know it's my fault, but my build/test/deploy system takes me a whole week of work to update every time I have to bugfix something. This is because I need to test in multiple browsers and that software stack is a mess (not my fault). I really don't have the time to do it right now!

I will try to fix it when I can fix the whole thing though.

Thanks!

On 25 April 2017 at 02:57, cladelpino notifications@github.com wrote:

can reproduce: x=[[1,2],[2,4]]

numeric.svd(x).S [0, NaN]

x=[[1,2],[3,6]]

numeric.svd(x).S [7.071067811865476, 0]

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sloisel/numeric/issues/75#issuecomment-296861563, or mute the thread https://github.com/notifications/unsubscribe-auth/AAwKXKyirCryjrbB8guGFc3u3soik6TKks5rzUUGgaJpZM4KZWb3 .

-- Sébastien Loisel Assistant Professor Department of Mathematics, Heriot-Watt University Riccarton, EH14 4AS, United Kingdom web: http://www.ma.hw.ac.uk/~loisel/ email: S.Loisel at hw.ac.uk phone: +44 131 451 3234 fax: +44 131 451 3249

cladelpino commented 7 years ago

Thanks to you for this piece of software. Do you have this bug located or do you need help locating it ?

sloisel commented 7 years ago

Hi,

First, like I said, I literally cannot put any fixes in there because my build/test/deploy system is the most painful thing to fix so I've not even been applying patches people are sending me!

Second, the SVD code is supposedly a port of the code by Golub and Reisch. It was sent to me by Shanti Rao a long time ago and I haven't looked inside the routines. So no, I don't know where the problem is.

Thanks,

On 26 April 2017 at 21:16, cladelpino notifications@github.com wrote:

Thanks to you for this piece of software. Do you have this bug located or do you need help locating it ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sloisel/numeric/issues/75#issuecomment-297513247, or mute the thread https://github.com/notifications/unsubscribe-auth/AAwKXPsxvgRzoiGdD9xTtemvp-vq5G4vks5rz5gcgaJpZM4KZWb3 .

-- Sébastien Loisel Assistant Professor Department of Mathematics, Heriot-Watt University Riccarton, EH14 4AS, United Kingdom web: http://www.ma.hw.ac.uk/~loisel/ email: S.Loisel at hw.ac.uk phone: +44 131 451 3234 fax: +44 131 451 3249

ankitrohatgi commented 5 years ago

For a 3x2 matrix (e.g. numeric.svd([[1,4],[2,5],[3,6]])), I get U as a 3x2 matrix instead of a 3x3 as I was expecting. Any ideas why that might be?