Skip to content

Instantly share code, notes, and snippets.

@reox
Created August 5, 2020 08:34
Show Gist options
  • Save reox/235f84dfed96b1648e012ca0bd1e1c6d to your computer and use it in GitHub Desktop.
Save reox/235f84dfed96b1648e012ca0bd1e1c6d to your computer and use it in GitHub Desktop.
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