Search code examples
rubymatrixeigenvalueeigenvector

Is this a ruby's bug in class Matrix?


I use Matrix class in ruby to caculate eigenvalues and eigenvectors. With this code:

m = Matrix[[0.6868,0.6067],[0.6067,0.5978]]
v, d, v_inv = m.eigensystem

the correct result should be:

[ 1.25057433  0.03398123]
[[ 0.73251454 -0.68075138]
 [ 0.68075138  0.73251454]]

which I confirmed with numpy using Python.

However, I got the result below:

d=[[0.033970204576497576,   0],
 [0,    1.2506297954235022]]

v=[[0.6807528514962294, 0.7325131774785713],
 [-0.7325131774785713,  0.6807528514962294]]

Is this a ruby's bug? My ruby's version below:

ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]

Solution

  • This would be by no mean an answer, but I’ll put it here for the sake of formatting:

    When it comes to math, I would use to check the correctness of the result:

    > entries <- c(0.6868, 0.6067, 0.6067, 0.5978)
    > input <- matrix(entries, nrow=2, byrow=TRUE)
    > input
    
    #        [,1]   [,2]
    # [1,] 0.6868 0.6067
    # [2,] 0.6067 0.5978
    
    > input_eigen <- eigen(input)
    > input_eigen 
    
    $values
    # [1] 1.2506298 0.0339702
    
    $vectors
    #            [,1]       [,2]
    # [1,] -0.7325132  0.6807529
    # [2,] -0.6807529 -0.7325132
    

    I trust the result above, which means ruby is probably doing better, than python/numpy.