Skip to content

Instantly share code, notes, and snippets.

@reox

reox/eigen.py

Created Aug 5, 2020
Embed
What would you like to do?
Eigenvalues and -vectors of matrix and getting matrix back in numpy
import numpy as np
A = np.array([[2,1,2.5], [1,3,1], [2.5,1,4]])
# Eigenvalues and -vectors have the property such that: Ax = lx
# x ... Eigenvectors
# l ... Eigenvalues
l, x = np.linalg.eig(A)
# it follows:
# A = x l x^-1
A_reconstructed = np.matmul(np.matmul(x, l * np.eye(3)), np.linalg.inv(x))
# or in python3, @ is used for matrix multiplication:
A_reconstructed = x @ (l * np.eye(3)) @ np.linalg.inv(x)
print(A_reconstructed)
# array([[2. , 1. , 2.5],
# [1. , 3. , 1. ],
# [2.5, 1. , 4. ]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.