How to find the eigenvalues and eigenvectors of a matrix with SymPy?

13,292

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
Share:
13,292

Related videos on Youtube

Heretic
Author by

Heretic

Updated on June 04, 2022

Comments

  • Heretic
    Heretic almost 2 years

    I want to calculate the eigenvectors x from a system A by using this: A x = λ x

    The problem is that I don't know how to solve the eigenvalues by using SymPy. Here is my code. I want to get some values for x1 and x2 from matrix A

    from sympy import *
    x1, x2, Lambda = symbols('x1 x2 Lambda')
    I = eye(2)
    A = Matrix([[0, 2], [1, -3]])
    equation = Eq(det(Lambda*I-A), 0)
    D = solve(equation)
    print([N(element, 4) for element in D]) # Eigenvalus in decimal form
    print(pretty(D)) # Eigenvalues in exact form
    
    X = Matrix([[x1], [x2]]) # Eigenvectors
    T = A*X - D[0]*X # The Ax = %Lambda X with the first %Lambda = D[0]
    print(pretty(solve(T, x1, x2)))
    
  • Heretic
    Heretic almost 7 years
    [T, D] = A.diagonalize() was a better method in SymPy.