SciPy - ishermitian() Function



The SciPy ishermitian() function checks for whether the matrix is hermitian or not. The matrix is known to be hermitian if a = a^H is its conjugate transpose.

Checking whether the matrix is hermitian or not is really useful because, for hermitian matrices, all eigenvalues are guaranteed to be real numbers and orthogonal eigenvectors. This is crucial in applications like quantum mechanics, signal processing, and numerical stability in computations.

Syntax

The syntax for the SciPy ishermitian() method is as follows −

.ishermitian(a, atol=1e-8, rtol=1e-5)

Parameters

This method accepts the following parameters −

  • a ndarray − The input matrix.

  • atol (float, optional) − Absolute tolerance. Default is 1 x 10^-8.

  • rtol (float, optional) − Relative tolerance. Default is 1 x 10^-5.

Return Value

Returns True if the matrix 'a' is hermitian, otherwise False.

Example 1

Following is the basic example to check whether the matrices A and B are hermitian. Following is the code −

from scipy.linalg import ishermitian
import numpy as np

A = np.array([[2, 1+1j], [1-1j, 3]])
print(ishermitian(A)) 
B = np.array([[2, 1], [2, 3]])
print(ishermitian(B)) 

When we run above program, it produces following result −

True
False

Example 2

In the below code we used absolute tolerance to check whether the matrix C is hermitian. This parameter is useful when the matrices have small floating point errors. Following is the code −

from scipy.linalg import ishermitian
import numpy as np

C = np.array([[1, 1e-9 + 0j], [1e-9 - 0j, 1]])
print(ishermitian(C, atol=1e-8)) 

Following is an output of the above code −

True

Example 3

This example demonstrates how one can use a Hermitian matrix in order to determine if all of its eigenvalues are real.

from scipy.linalg import ishermitian, eigvals
import numpy as np

matrix = np.array([
    [2, 1-1j],
    [1+1j, 3]
])

if ishermitian(matrix):
    print("The matrix is Hermitian.")
    
    eigenvalues = eigvals(matrix)
    print("Eigenvalues:", eigenvalues)
else:
    print("The matrix is not Hermitian.")
is_real = np.allclose(eigenvalues.imag, 0)
print(is_real)

Output of the above code is as follows −

The matrix is Hermitian.
Eigenvalues: [1.-2.52702567e-16j 4.-1.91386643e-16j]
True
scipy_linalg.htm
Advertisements