[Numpy-discussion] linalg.eig getting the original matrix back ?

josef.pktd@gmai... josef.pktd@gmai...
Fri Jan 15 09:31:44 CST 2010


I had a problem because linal.eig doesn't rebuild the original matrix,
linalg.eigh does, see script below

Whats the trick with linalg.eig to get the original (or the inverse)
back ? None of my variations on the formulas worked.

Thanks,
Josef


import numpy as np
import scipy as sp
import scipy.linalg

omega =  np.array([[ 6.,  2.,  2.,  0.,  0.,  3.,  0.,  0.],
                   [ 2.,  6.,  2.,  3.,  0.,  0.,  3.,  0.],
                   [ 2.,  2.,  6.,  0.,  3.,  0.,  0.,  3.],
                   [ 0.,  3.,  0.,  6.,  2.,  0.,  3.,  0.],
                   [ 0.,  0.,  3.,  2.,  6.,  0.,  0.,  3.],
                   [ 3.,  0.,  0.,  0.,  0.,  6.,  2.,  2.],
                   [ 0.,  3.,  0.,  3.,  0.,  2.,  6.,  2.],
                   [ 0.,  0.,  3.,  0.,  3.,  2.,  2.,  6.]])

for fun in [np.linalg.eig, np.linalg.eigh, sp.linalg.eig, sp.linalg.eigh]:
    print fun.__module__, fun
    ev, evec = fun(omega)
    omegainv = np.dot(evec, (1/ev * evec).T)
    omegainv2 = np.linalg.inv(omega)
    omegacomp = np.dot(evec, (ev * evec).T)
    print 'composition',
    print np.max(np.abs(omegacomp - omega))
    print 'inverse',
    print np.max(np.abs(omegainv - omegainv2))

this prints:

numpy.linalg.linalg <function eig at 0x017EDDF0>
composition 0.405241032278
inverse 0.405241032278

numpy.linalg.linalg <function eigh at 0x017EDE30>
composition 3.5527136788e-015
inverse 7.21644966006e-016

scipy.linalg.decomp <function eig at 0x01DB14F0>
composition 0.238386662463
inverse 0.238386662463

scipy.linalg.decomp <function eigh at 0x01DB1530>
composition 3.99680288865e-015
inverse 4.99600361081e-016


More information about the NumPy-Discussion mailing list