Computing eigendecompositions
Computing eigendecompositions is rather straightforward with a numerical or symbolic computing tool such as those available in Matlab or Python. The following sections show how to use Python to compute numerical and symbolic eigendecompositions.
Python eigendecompositions
In Python, we can use the numpy
package for numerics and the sympy
package for symbolics.
Python numerical eigendecompositions
In Python, we first need to load the appropriate packages.
import numpy as np # for numerics
from numpy import linalg as la # for eig
from IPython.display import display, Markdown, Latex # prty
np.set_printoptions(precision=3) # for prettyConsider the same numerical A
matrix from the section above. Create it as a numpy.array object.
A = np.array(
[
[-3, 5, 9],
[0, 2, -10],
[5, 0, -4],
]
)The numpy.linalg module (loaded
as la) gives us access to the eig function.
e_vals,e_vecs = la.eig(A)
print(f'e-vals: {e_vals}')
print(f'modal matrix:\n {e_vecs}')e-vals: [-11.487+0.j 3.243+4.122j 3.243-4.122j]
modal matrix:
[[-0.769+0.j 0.122-0.537j 0.122+0.537j]
[ 0.381+0.j 0.767+0.j 0.767-0.j ]
[ 0.514+0.j -0.095-0.316j -0.095+0.316j]]
Note that the eigenvalues are returned as a one-dimensional array, not along the diagonal of a matrix as with Matlab.
print(f"the third eigenvalue is {e_vals[2]:.3e}")the third eigenvalue is 3.243e+00-4.122e+00j
Python symbolic eigendecompositions
We use the sympy package for
symbolics.
import sympy as spDeclare symbolic variables.
sp.var('a b c')(a, b, c)
Define a symbolic matrix A.
A = sp.Matrix([
[a,b],
[0,c]
])
display(A)\(\displaystyle \left[\begin{matrix}a & b\\0 & c\end{matrix}\right]\)
The sympy.Matrix class has
methods eigenvals and eigenvects. Let’s consider them in
turn.
A.eigenvals(){a: 1, c: 1}
What is returned is a dictionary
with our eigenvalues as its keys and the multiplicity (how
many) of each eigenvalue as its corresponding value.
The eigenvects method returns
even more complexly structured results.
A.eigenvects()[(a, 1, [Matrix([
[1],
[0]])]), (c, 1, [Matrix([
[-b/(a - c)],
[ 1]])])]
This is a list of tuples with structure as follows.
(<eigenvalue>,<multiplicity>,<eigenvector>)Each eigenvector is given as a list of symbolic matrices.
Extracting the second eigenvector can be achieved as follows.
A.eigenvects()[1][2][0]\(\displaystyle \left[\begin{matrix}- \frac{b}{a - c}\\1\end{matrix}\right]\)
Online Resources for Section 6.9
No online resources.