mljs / matrix

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

EVD difference + broken links #173

Closed ghost closed 9 months ago

ghost commented 10 months ago

We may need to update some links, example here to the source so that anyone fixing the code can find it.


Difference of EVD compared to Julia language.

This is just a comparison, and shows minor differences in the output. They may be unimportant.

Julia code. EVD example:

using LinearAlgebra

W = Matrix{Float64}([
     47714.6 -21547.0 -7255.84  7158.56 -26070.2;
    -21547.0  10315.4 -9737.44  3962.96  17006.2;
    -7255.84 -9737.44  181810  -82433.8 -82382.6;
     7158.56  3962.96 -82433.8  35246.6  36065.8;
    -26070.2  17006.2 -82382.6  36065.8  55381.0
  ])

eigenvals, eigenvecs = eigen(W)
println(eigenvals)

Returns:

[
  -2961.6863711342066, 
  0.17599780693672074, <----
  712.4076686428466, 
  72953.5755817964, 
  259763.12712288793
 ]

Whereas we get one different value (although most of them differ in the first decimal.)

 [
  -2961.7500245350157,
  -4.433786671143025e-12,  <----
  712.3961094077385,
  72953.56681338367,
  259762.98710174352,
]

The imaginary part is consistent with a symmetric matrix ([ 0, 0, 0, 0, 0 ])

targos commented 10 months ago

What do you mean by "broken links"? Your example is not.

targos commented 10 months ago

I wonder what we should seek as a goal?

  1. Implementation that gives the same exact result as other implementations.
  2. Implementation that satisfies the mathematical definition(s).

You showed that 1. is wrong (at least compared to Julia), but what about 2. ?

ghost commented 10 months ago

The example page has this link in the first bit:

https://github.com/lutzroeder/Mapack/blob/master/Source/CholeskyDecomposition.cs

which fails at least here. Same for the rest of the decompositions.

Yes, I don't have much to say respect 1 or 2.

In some cases though, that difference in sign may amplify (for example in Multidimensional Scaling, or anything that requires taking the square root of n number of eigenvalues.)

Checking if 2 is wrong takes more time, but I don't mind doing it. Probably comparing with other implementations before.

ghost commented 10 months ago

In python, the result is (by eye seems very close):

[ 
 2.59763127e+05,  
 7.29535756e+04, 
 -2.96168637e+03, 
 1.75997807e-01,
 7.12407669e+02
]

Still it's not enough, but an indication maybe.

targos commented 10 months ago

The example page has this link in the first bit:

https://github.com/lutzroeder/Mapack/blob/master/Source/CholeskyDecomposition.cs

which fails at least here. Same for the rest of the decompositions.

I guess the repo has unfortunately been deleted.

Here is a fork. Can't say if it contains the same code that I saw at the time: https://github.com/Seveland12/mapack