In our previous article, we discussed what eigen values and eigenvectors of a square matrix are and how we can calculate the eigenvalues and eigenvectors of a square matrix mathematically. We discussed that if A is a square matrix, then
If we solve for λ, we get the eigenvalues. And if we solve for u for a particular value of λ, we get the eigenvector for the corresponding eigenvalue.
We can use the following Python code to calculate the eigenvalues and eigenvectors of a square matrix A.
import numpy A = numpy.array([[1, 2], [2, 1]]) w, v = numpy.linalg.eig(A) print(w) print(v)
Here, A is a square matrix. We are using the eig() function from the numpy.linalg module to get the eigenvalues and the corresponding eigenvectors of A. The variable w here contains the eigenvalues. And v is a matrix whose columns contain the eigenvectors.
The output of the mentioned program will be:
[ 3. -1.] [[ 0.70710678 -0.70710678] [ 0.70710678 0.70710678]]
0 Comments