How to find the eigenvalues and eigenvectors of a matrix with SymPy?
The methods eigenvals
and eigenvects
is what one would normally use here.
A.eigenvals()
returns {-sqrt(17)/2 - 3/2: 1, -3/2 + sqrt(17)/2: 1}
which is a dictionary of eigenvalues and their multiplicities. If you don't care about multiplicities, use list(A.eigenvals().keys())
to get a plain list of eigenvalues.
The output of eigenvects
is a bit more complicated, and consists of triples (eigenvalue, multiplicity of this eigenvalue, basis of the eigenspace). Note that the multiplicity is algebraic multiplicity, while the number of eigenvectors returned is the geometric multiplicity, which may be smaller. The eigenvectors are returned as 1-column matrices for some reason...
For your matrix, A.eigenvects()
returns the eigenvector [-2/(-sqrt(17)/2 + 3/2), 1]
for the eigenvalue -3/2 + sqrt(17)/2
, and eigenvector [-2/(3/2 + sqrt(17)/2), 1]
for eigenvalue -sqrt(17)/2 - 3/2
.
If you want the eigenvectors presented as plain lists of coordinates, the following
[list(tup[2][0]) for tup in A.eigenvects()]
would output [[-2/(-sqrt(17)/2 + 3/2), 1], [-2/(3/2 + sqrt(17)/2), 1]]
. (Note this just picks one eigenvector for each eigenvalue, which is not always what you want)
sympy has a very convenient way of getting eigenvalues and eigenvectors: sympy-doc
Your example would simply become:
from sympy import *
A = Matrix([[0, 2], [1, -3]])
print(A.eigenvals()) #returns eigenvalues and their algebraic multiplicity
print(A.eigenvects()) #returns eigenvalues, eigenvects