Kedar_dg
Kedar_dg

Reputation: 41

Similar matrix computation using numpy

I am trying to find a similar matrix B to a 3 X 3 matrix :A using a random invertible matrix P . B = P_inv.A.P

import numpy as np
from scipy import linalg as LA
from numpy.linalg import inv

A = np.random.randint(1,10,9).reshape(3,3)
P = np.random.randn(3,3)
P_inv = inv(P)
eig1 = LA.eigvalsh(A)
eig1 = np.sort(eig1)

B1 = P_inv.dot(A)
B = B1.dot(P)
eig2 = LA.eigvalsh(B)
eig2 = np.sort(eig2)

print(np.round(eig1 ,3))
print(np.round(eig2,3))

However ,I ntoice that eig1 & eig2 are never equal. What am I missing, or is it a numerical error ?

Thanks

Kedar

Upvotes: 1

Views: 180

Answers (1)

macroeconomist
macroeconomist

Reputation: 701

You're using eigvalsh, which requires that the matrix be real symmetric (or complex Hermitian), which your randomly generated matrix is not.

Deleting the h and using eigvals instead fixes this.

Upvotes: 1

Related Questions