1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why does my Hermitian matrix return complex eigenvalues and determinant in NumPy?

Discussão em 'Python' iniciado por Stack, Setembro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am working with a Hermitian matrix Gamma (complex, shape (3,3) inside a larger array). I verified that it is Hermitian:

    np.allclose(Gamma, Gamma.conj().T, atol=1e-10, rtol=0) # returns True

    I also symmetrized it explicitly:

    A = 0.5 * (Gamma + Gamma.conj().T)
    np.all(A - Gamma == 0) # returns True


    The diagonal entries are real (+0j), and off-diagonal elements are forced to be conjugates.

    However, when I compute eigenvalues, they are not real:

    [ 1.70674158e+09-3.5e-09j, 2.29744006e+01-8.1e-09j,
    -1.01287448e-08+1.2e-08j ]


    One might think this is just numerical noise, but when I compute the determinant (both with NumPy and SciPy) I get a significant imaginary part:

    (4783.194482059961+83.43640215104267j)


    The matrix is built as follows:

    Gamma = np.zeros((n_z, n_k, n_mu, 3, 3), dtype=complex)

    def P_auto_tilde(mui, hi, ki, alpha, b, fi, ng, pki):
    return ((b + fi*mui**2)**2 + (hi/ki)**2 * alpha**2 * fi**2 * mui**2) * pki + ng

    def Pxy(mui, hi, ki, a1, a2, b1, b2, fi):
    return (
    (b1+fi*mui**2)*(b2+fi*mui**2)
    + (hi/ki)**2 * a1*a2 * fi**2 * mui**2
    - 1j * (
    fi*hi*mui*(a1*(b2+fi*mui**2) - a2*(b1+fi*mui**2)) / ki
    )
    )

    for i in range(n_z):
    for m in range(n_k):
    for n in range(n_mu):
    mu_val = mu[i, n]
    h_val = h
    k_val = k[i, m]
    f_val = f
    pk_val = pk[i,m]
    a1_val= α_1
    a2_val= α_2
    b1_val= b_1
    b2_val= b_2
    ng1_val= n_g_1
    ng2_val= n_g_2
    Pxx = P_auto_tilde(mu_val, h_val, k_val, a1_val, b1_val, f_val, ng1_val, pk_val)
    Pyy = P_auto_tilde(mu_val, h_val, k_val, a2_val, b2_val, f_val, ng2_val, pk_val)
    Pxy_val = Pxy(mu_val, h_val, k_val, a1_val, a2_val, b1_val, b2_val,f_val) * pk_val
    Pyx_val = Pxy_val.conj()

    M = np.zeros((3, 3), dtype=complex)
    M[0, 0] = Pxx**2
    M[0, 1] = Pxx * Pxy_val
    M[0, 2] = Pxy_val**2

    M[1, 0] = M[0, 1].conj()
    M[1, 1] = 0.5 * (Pxx*Pyy + Pxy_val*Pyx_val)
    M[1, 2] = Pxy_val * Pyy

    M[2, 0] = M[0, 2].conj()
    M[2, 1] = M[1, 2].conj()
    M[2, 2] = Pyy**2

    Gamma[i, m, n] = M * pref


    Despite all the checks, eigenvalues and determinant come out complex.

    Why am I seeing complex eigenvalues and a complex determinant for a matrix that appears Hermitian under the tests above? Is there something subtle I might be missing in the construction or numerical handling of the matrix, and how can I fix this so the results are real as expected?

    Continue reading...

Compartilhe esta Página