Numpy eigenvalues and eigenvectors

We calculate the eigenvalues and eigenvectors of the matrix

\begin{displaymath}
A =
\begin{array}{ccc}
1 & 2 & 3 \\
3 & 2 & 1 \\
1 & 0 & -1
\end{array}\end{displaymath} (1)



>>> import numpy as np
>>> from numpy import linalg as LA
A = np.array([[1,2,3],[3,2,1],[1,0,-1]])
w, v = LA.eig(A)
>>> print(w)
[  4.31662479e+00  -2.31662479e+00   3.43699053e-17]
>>> print(v)
[[ 0.58428153  0.73595785  0.40824829]
 [ 0.80407569 -0.38198836 -0.81649658]
 [ 0.10989708 -0.55897311  0.40824829]]
>>>

The numpy.linalg.eig function returns a tuple consisting of a vector and an array. The vector (here w) contains the eigenvalues. The array (here v) contains the corresponding eigenvectors, one eigenvector per column. The eigenvectors are normalized so their Euclidean norms are 1.

The eigenvalue w[0] goes with the 0th column of v. The eigenvalue w[1] goes with column 1, etc. To extract the ith column vector, we use

>>> u = v[:,i]

(see the notes on numpy arrays).

Just to be clear about what is happening here, let's check the eigenvector/eigenvalue condition for the second eigenvalue and eigenvector.

\begin{displaymath}
A u = \lambda u     ,
\end{displaymath} (2)

where $u$ is the eigenvector and $\lambda$ is its eigenvalue. So we multiply the eigenvector v[:,1] by A and check that it is the same as multiplying the same eigenvector by its eigenvalue w[1].

>>> u = v[:,1]
>>> print(u)
[ 0.73595785 -0.38198836 -0.55897311]
>>> lam = w[1]
>>> print(lam)
-2.31662479036
>>> print(np.dot(A,u))
[-1.7049382   0.88492371  1.29493096]
>>> print(lam*u)
[-1.7049382   0.88492371  1.29493096]

We see that the results are the same, as they should be.