RubixML / Tensor

A library and extension that provides objects for scientific computing in PHP.
https://rubixml.com
MIT License
223 stars 27 forks source link

Eigenvalues and vectors from JAMA implementation are wrong #3

Closed henrique-borba closed 3 years ago

henrique-borba commented 3 years ago

After some failed tests with CRubix interface I realized the expected eigenvalues and vectors from MatrixTest are actually wrong compared to other libraries. The following matrix was used:

[
      [22, -17, 12],
      [4, 11, -2],
      [20, -6, -9],
]

Tensor with JAMA

$values = [25.108706520450326, -15.096331148319537, 13.9876246278692];
$vectors = [
            [-0.5029346679560592, -0.1309992382037118, -0.33107976181279675],
            [0.15580805853732102, -0.08643645234319261, -0.6918777439682378],
            [0.8501650243704214, 0.987607178637524, 0.641631809310763],
        ];

Tensor with CArray

$values = [-15.09633115,  25.10870652,  13.98762463];
$vectors = [[  0.25848695, -0.86227193, -0.66844722 ]
            [ -0.11314538, -0.17721180, -0.61268791 ]
            [ -0.95936574,  -0.47442924, -0.42165370 ]];

Computed using LAPACK_dgeev

NumPy

values = [-15.09633115,  25.10870652,  13.98762463]
vectors = [[ 0.25848695, -0.86227193, -0.66844722],
           [-0.11314538, -0.1772118 , -0.61268791],
           [-0.95936574, -0.47442924, -0.4216537 ]]

Computed using LAPACK_dgeev

andrewdalpino commented 3 years ago

Interesting @henrique-borba nice catch, that library is pretty old. Do you think it's using a different method to obtain the eigenvalues?

Let's not worry too much about it, I'm willing to remove the dependency on JAMA and just throw an exception (method not implemented) in the polyfill if need be. We only use eig for PCA and LDA in the main package anyway.

andrewdalpino commented 3 years ago

I'm getting the same results for eigenvalues as you are ... haven't looked at eigenvectors yet

It looks like the eigenvalues are just in different order (probably not an issue). Also, eigenvectors are not unique so that's not necessarily a problem either. Is it safe to assume different algorithms can produce a different set of eigenvectors?