I have been trying to find my matrix from eigenvalues and eigenvectors. I calculated eigen values and eigen vectors from a matrix first. Then, I want to verify the values of eigenvalues and eigenvectors. But the didn't get the expected value. my code
import numpy as np
from numpy.linalg import eig
a=np.array([[1,2],[2,np.pi]])
w,v=eig(a)
c0=np.outer(v[0],v[0].T)
c1=np.outer(v[1],v[1].T)
a_=w[0]*c0+w[1]*c1
the result I got was:
array ([[ 1. , -2. ],
[-2. , 3.14159]])
but I was expecting same array as a. Can anyone tell me what's wrong here?
Eigenvectors of
aare notv[0]andv[1].vis a transformation matrix. So columns ofv, that isv[:,0]andv[:,1]are the eigenvectors.You wrote the code as if
v[0]andv[1]were eigenvectors, which they are not.Also, I expect that you are aware that your code (once replaced
v[0]andv[1]byv[:,0]andv[:,1]) only works becausevis unitary and thereforev⁻¹isvᵀ.Generic way to get back
awould be[email protected](w)@np.linalg.inv(v). What you are doing is the equivalent of[email protected](w)@v.T. Which is ok, as long asnp.linalg.inv(v)andv.Tare the same thing. Which is the case whenvis unitary.Since your initial matrix
ais symmetric, then we know that it exists a set of orthogonal eigenvectors, that form, once normalized, an unitary matrix. So, we know that a unitary matrixvis possible.And because you have 2 different eigenvalues, in dimension 2, then, the vectors had to be orthogonal (each subspace is a line; so whatever eigenvector we choose, they are colinear the the eigenvectors of the orthogonal set we know exist). So in the case where
ais symmetric, and you have as many eigenvalues as the dimension ofa,vis orthogonal, and unitary, sinceeiggarantees that each vector is normalized (norm 1)But if
ahappened to have double eigenvalues, then, all we know is thatvCOULD be unitary, buteigdoesn't guarantees that.For example, a caricatural example, is
A=IdentityUnique eigenvalue is 1. And eigenspace is the whole space. So anyvmade of two independent vectors is a validv. Sure, there exist some unitary sets of eigenvectors ([1,0], [0,1]is the obvious one. And indeednp.array([[1,0],[0,1]])@np.diag([1,1])@np.array([[1,0],[0,1]]).T) isA. But there also exist others, non unitary, even after normalization. For example,[1,0], [1/√2, 1/√2]is another valid set of eigenvectors that could be returned byeig(both are eigenvectors. They form a valid basis of the eigenspace. And they are normalized).Yet
np.array([[1, 1/np.sqrt(2)], [0, 1/np.sqrt(2)]])@np.diag([1,1])@np.array([[1, 1/np.sqrt(2)], [0, 1/np.sqrt(2)]]).Tis notA(np.array([[1, 1/np.sqrt(2)], [0, 1/np.sqrt(2)]])@np.diag([1,1])@np.linalg.inv(np.array([[1, 1/np.sqrt(2)], [0, 1/np.sqrt(2)]]))is).So, long story short, in dim 2, even with symmetric matrices
a, you have no guarantee thatvis unitary. Maths tells that it could be. Butlinalg.eigdoesn't guarantee that. And therefore, you don't know if[email protected](w)@v.T(which is what you compute, with longer code) isa.On the other hand,
eigh, which can be used only on hermitian matrices (so, symmetric ones in real world), guarantees thatvis unitary.So tl;dr
Your code assume that
v[0]andv[1]are eigenvectors. They are not.v[:,0]andv[:,1]are.Once that corrected, your code is the equivalent of computing
[email protected](w)@v.TThat is the same thing as
a(which is equal to[email protected](w)@np.linalg.inv(v)) only ifvis unitary.eigdoesn't guarantee that it is the case (even if,abeing symmetric, it is possible that it is). So, no guarantee that your code will find backaeven after correction. Even tho, in your case (after correction) it happens to work.eigh, which works only with symmetric (hermitians) matrices, as yourais, does guarantees that. So, Replaceeigbyeighin your code. And then, you know for sure that (as long asais symmetric),a_isa.