import numpy A = numpy.array([[1, 2], [2, 1]]) u, P = numpy.linalg.eig(A) D = numpy.diag(u) print("P: \n", P) print("D: \n", D) print("Inverse of P: \n", numpy.linalg.inv(P)) A_cal = P.dot(D).dot(numpy.linalg.inv(P)) if numpy.allclose(A, A_cal): print("A is eigen decomposed") else: print("A cannot be eigen decomposed")
Here, A is a square matrix. We are using the numpy.linalg.eig() function to calculate the eigen values and eigen vectors of A. Here, u contains the eigenvalues, and P is a matrix whose columns are the eigenvectors of A.
So, we can get the diagonal matrix D from the eigenvalue u using the numpy.diag() function. And we get the inverse of P using the numpy.linalg.inv() function.
The output of the mentioned program will be:
P: [[ 0.70710678 -0.70710678] [ 0.70710678 0.70710678]] D: [[ 3. 0.] [ 0. -1.]] Inverse of P: [[ 0.70710678 0.70710678] [-0.70710678 0.70710678]] A is eigen decomposed






0 Comments