This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

# Eigenvalues and Eigenvectors in Python¶

Though the methods we introduced so far look complicated, the actually calculation of the eigenvalues and eigenvectors in Python is fairly easy. The main built-in function in Python to solve the eigenvalue/eigenvector problem for a square array is the eig function in numpy.linalg. Let’s see how we can use it.

TRY IT Calculate the eigenvalues and eigenvectors for matrix $$A = \begin{bmatrix} 0 & 2\\ 2 & 3\\ \end{bmatrix}$$.

import numpy as np
from numpy.linalg import eig

a = np.array([[0, 2],
[2, 3]])
w,v=eig(a)
print('E-value:', w)
print('E-vector', v)

E-value: [-1.  4.]
E-vector [[-0.89442719 -0.4472136 ]
[ 0.4472136  -0.89442719]]


TRY IT! Compute the eigenvalues and eigenvectors for matrix $$A = \begin{bmatrix} 2 & 2 & 4\\ 1 & 3 & 5\\ 2 & 3 & 4\\ \end{bmatrix}$$.

a = np.array([[2, 2, 4],
[1, 3, 5],
[2, 3, 4]])
w,v=eig(a)
print('E-value:', w)
print('E-vector', v)

E-value: [ 8.80916362  0.92620912 -0.73537273]
E-vector [[-0.52799324 -0.77557092 -0.36272811]
[-0.604391    0.62277013 -0.7103262 ]
[-0.59660259 -0.10318482  0.60321224]]