Search code examples
numpyalgebraeigenvector

numpy eigen decomposition produce unexpected output?


According to documentation of numpy.linalg.eig and what I understand about eigen decomposition, the following code :

a = [[1,1],[-1,-1]]
w, v = np.linalg.eig(a)
c = a@v
print(c)
print(w)

should produce :

[[√2,0],[-√2,0]]
[4,0]

but instead it produced :

[[ 1.11022302e-16+1.11022302e-16j  1.11022302e-16-1.11022302e-16j]
 [-1.11022302e-16-1.11022302e-16j -1.11022302e-16+1.11022302e-16j]]
[-3.25176795e-17+1.57009246e-16j -3.25176795e-17-1.57009246e-16j]

so where was I wrong?


Solution

  • With matrix a

    a = np.array([[ 1,  1],\
                  [-1, -1]])
    

    your two eigenvalues should theoretically be w_th=[0,0], so :

    w
    >>> array([-3.25176795e-17+1.57009246e-16j, -3.25176795e-17-1.57009246e-16j])
    

    is just some zero +/- round-off error in complex form. Concerning the eigenvectors, these are v_th=[[1,1],[-1,-1]] but numpy.linalg.eig normalized them to be unitary length (e.g. for the first one np.linalg.norm(v[:,0],2) = 0.99...), which means it just gave you an approximation of [[1/sqrt(2),1/sqrt(2)],[-1/sqrt(2),-1/sqrt(2)]] :

    v
    >>> array([[ 0.70710678+0.00000000e+00j,  0.70710678-0.00000000e+00j],
                [-0.70710678+1.11022302e-16j, -0.70710678-1.11022302e-16j]])
    

    Knowing all of the above, you can now verify it numerically by comparing both sides of the equation formula:

    np.allclose(a@v,w*v)
    >>> True
    

    or with theorical values, i.e. "without" round-off errors :

    a@np.asarray(v_th)
    >>> array([[0, 0],
               [0, 0]])
    
    np.asarray(w_th)*np.asarray(v_th)
    >>> array([[0, 0],
               [0, 0]])
    

    so there is nothing unexpected from numpy output here, seems just that your analytical eigenvalues [4,0] are false.