Question

I have a very simple question. It is related to the computational tolerance error.

Let me do (see at the end) the eigendecomposition of a matrix A in eigenvector V and diagonal eigenvalues D, and build it again by multiplication V^-1*D*V.

The obtained value is far from being A, the error is quite big.

I would like to know if I am using incorrect functions to do this task or, at least, how can I reduce this error. Thank you in advance

in[1]:import numpy
      from scipy import linalg
      A=matrix([[16,-9,0],[-9,20,-11],[0,-11,11]])
      D,V=linalg.eig(A)
      D=diagflat(D)
      matrix(linalg.inv(V))*matrix(D)*matrix(V)


out[1]:matrix([[ 15.52275377,   9.37603361,   0.79257097],  
       [9.37603361,  21.12538282, -10.23535271],  
       [0.79257097, -10.23535271,  10.35186341]])
Was it helpful?

Solution

Isn't that backwards? A*V = V*D from the definition, so A = V*D*V^(-1).

>>> import numpy as np
>>> from scipy import linalg
>>> A = np.matrix([[16,-9,0],[-9,20,-11],[0,-11,11]])
>>> D, V = linalg.eig(A)
>>> D = np.diagflat(D)
>>> 
>>> b = np.matrix(linalg.inv(V))*np.matrix(D)*np.matrix(V)
>>> b
matrix([[ 15.52275377+0.j,   9.37603361+0.j,   0.79257097+0.j],
        [  9.37603361+0.j,  21.12538282+0.j, -10.23535271+0.j],
        [  0.79257097+0.j, -10.23535271+0.j,  10.35186341+0.j]])
>>> np.allclose(A, b)
False

but

>>> f = np.matrix(V)*np.matrix(D)*np.matrix(linalg.inv(V))
>>> f
matrix([[  1.60000000e+01+0.j,  -9.00000000e+00+0.j,  -9.54791801e-15+0.j],
        [ -9.00000000e+00+0.j,   2.00000000e+01+0.j,  -1.10000000e+01+0.j],
        [ -1.55431223e-15+0.j,  -1.10000000e+01+0.j,   1.10000000e+01+0.j]])
>>> np.allclose(A, f)
True

Aside: there are recipes for using np.dot to avoid all these conversions to matrix, like

>>> dotm = lambda *args: reduce(np.dot, args)
>>> dotm(V, D, inv(V))
array([[  1.60000000e+01+0.j,  -9.00000000e+00+0.j,  -9.54791801e-15+0.j],
       [ -9.00000000e+00+0.j,   2.00000000e+01+0.j,  -1.10000000e+01+0.j],
       [ -1.55431223e-15+0.j,  -1.10000000e+01+0.j,   1.10000000e+01+0.j]])

which I often find cleaner, but YMMV.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top