Search code examples
pythonpython-3.xnumpyeigenvalueeigenvector

Python numpy produces wrong eigenvalues and eigenvectors in my computer


I am using a numpy to calculate the eigenvalues and eigenvectors of a matrix, but I keep getting the wrong result on my local computer. My input array is:

ATA =np.array([[     56100,      33750,        350,          0,          0,          0,  -13434400,   -6298800,     -67340]
      [     33750,      39025,        365,          0,          0,          0,   -6298800,   -4105800,     -40530]
      [       350,        365,          4,          0,          0,          0,    -67340,     -40530,       -421]
      [         0,          0,          0,      56100,      33750,        350,   -6297800,   -4104300,     -40480]
      [         0,          0,          0,      33750,      39025,        365,   -4104300,   -5323650,     -46800]
      [         0,          0,          0,        350,        365,          4,    -40480,     -46800,       -437]
      [ -13434400,   -6298800,     -67340,   -6297800,   -4104300,     -40480, -143294096, 1988812200,   21046240]
      [  -6298800,   -4105800,     -40530,   -4104300,  -5323650,     -46800, 1988812200, 1463802300,   13946640]
      [    -67340,     -40530,       -421,     -40480,     -46800,       -437,  21046240,   13946640,     136958]])

so when I run np.linalg.eig(ATA) the output should be:

(array([ 5.20824997e+09,  4.07454810e+08,  7.42539687e+04,  2.15085782e+04,
        1.47325883e+03,  6.95623993e+02,  1.00620174e+00, -2.34183896e-10,
        4.89068886e-03]), array([[ 2.84539953e-03,  1.81479448e-03, -4.03677749e-01,
         8.40141629e-02, -1.62954584e-01,  6.55925095e-01,
        -4.22667744e-01,  4.28089963e-01,  1.06176905e-01],
       [ 1.43790972e-03, -1.64731179e-03, -4.11218161e-01,
         8.20390032e-01, -1.04799961e-01, -3.83178690e-01,
        -2.95123482e-03, -3.17384527e-03, -5.22638141e-03],
       [ 1.50694971e-05, -1.03183136e-05, -3.89314681e-03,
         7.14037036e-03, -1.10215138e-02,  5.07017350e-03,
         5.91938347e-01,  3.92898770e-01,  7.03580978e-01],
       [ 1.43759890e-03, -1.64599502e-03,  7.08867672e-01,
         5.61266733e-01,  1.50187808e-01,  3.99898006e-01,
        -1.95860586e-03,  1.11445719e-03, -1.27734452e-03],
       [ 1.17548483e-03, -6.81423034e-03,  4.06683631e-01,
        -6.55054431e-02, -5.23337672e-01, -4.35335264e-01,
        -4.11520452e-01,  4.32430093e-01,  1.02594642e-01],
       [ 1.10796389e-05, -5.48396552e-05,  4.32865661e-03,
         5.41689188e-04, -1.94131236e-02,  6.27255853e-04,
        -4.22766929e-01, -5.91916200e-01,  6.85935194e-01],
       [-8.83129590e-01, -4.69109327e-01, -2.08350880e-03,
         1.14444168e-03, -5.76087521e-04,  3.54706596e-03,
        -1.71586709e-05,  6.93819107e-06, -3.51316614e-05],
       [-4.69089792e-01,  8.83088500e-01,  3.43558431e-03,
         2.66295777e-03, -9.46771468e-03, -9.51037214e-04,
         3.67021032e-05,  2.30158491e-05, -1.70205343e-04],
       [-4.82500030e-03,  5.99890239e-03, -2.98470711e-03,
        -2.30397046e-02,  8.15742461e-01, -2.71037947e-01,
        -3.50524557e-01,  3.53550068e-01,  1.12421281e-01]]))

But in my environment, it comes up to:

(array([ 2.80556520e+09, -1.48486833e+09,  8.91358635e+04,  3.12310662e+04,
        1.67293828e+04,  1.45256927e+03,  1.02287725e+00,  1.71486141e-04,
        7.14907196e-02]), array([[ 4.53911407e-03, -5.12844789e-03,  5.81815676e-01,
         4.03075138e-01, -3.54070037e-01,  5.22579137e-02,
        -4.18346581e-01, -4.39977631e-01,  4.77848175e-02],
       [ 2.46880709e-03, -1.97041290e-03,  3.86847218e-01,
         1.97085313e-01,  8.84776717e-01,  1.69008001e-01,
        -4.91082887e-03,  4.02327384e-03, -7.92374720e-03],
       [ 2.53995133e-05, -2.23326997e-05,  3.95854042e-03,
         2.45012019e-03,  6.76807972e-03,  1.08295840e-02,
         6.12610346e-01, -5.13976174e-01,  6.00293512e-01],
       [ 2.46815358e-03, -1.97050701e-03, -5.08932001e-01,
         8.30051437e-01,  7.85246368e-02, -2.14057675e-01,
        -7.15437012e-05, -8.15717096e-04,  2.31896436e-03],
       [ 2.39125344e-03, -2.86679627e-04, -4.90583621e-01,
        -1.62932815e-01,  1.38557602e-01,  5.87624795e-01,
        -4.12659947e-01, -4.43735069e-01,  3.29341124e-02],
       [ 2.18983860e-05, -4.97622085e-06, -4.62793574e-03,
         2.95201534e-04,  1.07051252e-03,  1.95471546e-02,
        -3.95612985e-01,  4.58193429e-01,  7.95703714e-01],
       [-5.59185122e-01, -8.29039916e-01, -1.58639818e-03,
        -1.34262675e-03,  7.05133701e-04,  8.24587719e-06,
         2.26139620e-07,  5.65965747e-09,  4.33823831e-07],
       [-8.28977882e-01,  5.59121133e-01,  3.58122146e-03,
         8.59258680e-03, -1.73055059e-03,  9.46280078e-03,
         2.88384523e-05,  8.09952365e-06, -1.76507697e-04],
       [-8.31631249e-03,  6.49815429e-03, -1.10014765e-01,
        -2.88224906e-01,  2.57662176e-01, -7.59600488e-01,
        -3.50532044e-01, -3.67952844e-01,  5.53810458e-02]]))

Which is vastly different then what I intended. Does anyone know why I might be getting a different result? I double-checked the first output in an online compiler and I am sure that it's the right answer, so why am I getting a different result for the same code?


Solution

  • Well, that was silly. Apparently, I declared ATA as an int array in Windows and all the other tests that I ran was in macOS and Linux, so it's probably got to do with how those OSes store large numbers. So when I changed ATA to a float array, I got the correct eigenvalues.