I am working on a project where I need to compute the eigenvalues of a symmetric real matrix **A** in Python. I encountered an issue where the eigenvalues returned by **eigvalsh**, **eigvals**, and **eigh** are different.
My code:
import numpy as np
def convert_Matrix(Var, dim):
var_i = np.zeros((dim, dim))
indices = np.tril_indices(dim)
var_i[indices] = Var
var = np.dot(var_i, var_i.T)
return var
Var = np.array([4.04640469e+05, 9.98970587e+07, 1.75522655e+02, 1.85808145e+08,
2.88277248e+08, 7.50545040e+05])
dim = 3
A= convert_Matrix(Var, dim)
det_A = np.linalg.det(A)
eigvalsh_values = np.linalg.eigvalsh(A)
eigvals_values = np.linalg.eigvals(A)
eigh_values = np.linalg.eigh(A)[0] # eigh returns (eigenvalues, eigenvectors), so we take the first element
print("A:n", A)
print("Eigenvalues from eigvalsh:", eigvalsh_values)
print("Eigenvalues from eigvals:", eigvals_values)
print("Eigenvalues from eigh:", eigh_values)
print('det:', det_A)`
The output:
A:[[1.63733909e+11 4.04223927e+13 7.51854949e+13]
[4.04223927e+13 9.97942234e+15 1.85617378e+16]
[7.51854949e+13 1.85617378e+16 1.17629002e+17]]
Eigenvalues from eigvalsh:[2.43186146e-04 6.86886614e+15 1.20739722e+17]
Eigenvalues from eigvals: [1.20739722e+17 -5.60191504e-01 6.86886614e+15]
Eigenvalues from eigh: [4.41546949e-04 6.86886614e+15 1.20739722e+17]
det: -1.4510608257526929e+28
The output eigenvalues are not consistent across these methods, and the determinant does not match the product of the eigenvalues. I would like to understand why these discrepancies occur and how I can ensure consistent results when working with symmetric real matrices.