🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeNumpy linalg.eigh: Finding Eigenvalues and Vectors of Symmetric Matrices

The numpy or the numerical python library is like a gold mine of functions that can be used for computing difficult linear algebraic problems. One such function is the numpy linalg.eigh() which can be used to calculate the eigenvalues and the eigenvectors of a complex Hermitian or a real symmetric matrix.
A Hermitian matrix is a square matrix of NxN dimensions whose conjugate transpose is equal to the original matrix. Its diagonal values are real, while its off-diagonal values may be complex. In the case of a real symmetric matrix, the transpose of the original matrix is equal to the original matrix.
An eigenvector is a non-zero vector that only changes by a scalar factor when linear transformations are applied to it.
Eigenvalues concerning eigenvectors are the scalar factors by which an eigenvector is scaled. A Hermitian matrix’s eigenvalues are always real, and their sum equals the trace (the sum of the diagonal values) of the matrix.
Also read: Numpy linalg.eig – Compute the eigenvalues and right eigenvectors of a square array
Syntax of the numpy linalg.eigh() function:
numpy.linalg.eigh(a, UPLO='L')
The parameters of the function are:
- a (array_like): The input array must be complex Hermitian or real symmetric.
- UPLO{‘L’,’U’}(optional): It specifies whether the calculation uses the upper (‘U’) or lower (‘L’) triangular portion of the matrix. Only the real parts of diagonal entries are considered; any imaginary parts are treated as zero.
This function returns two objects: ‘EVA’ (an ndarray) contains the eigenvalues in ascending order, each repeated according to its multiplicity. ‘EVE’ (an array or matrix) contains the corresponding normalized eigenvectors in columns.
Working with Numpy linalg.eigh()
This function also raises an error LinAlgError if the eigenvalue computation does not converge.
Installing the Numpy module
To use the function, you need NumPy installed. If it isn’t installed, run the following command in your command prompt:
pip install numpy
If you’re using Anaconda, run the following command in your command prompt:
conda install -c anaconda numpy
Also read: Cohort Analysis using Python: A Detailed Guide
Example 1: Calculating the eigenvalues and eigenvector of a 2×2 Hermitian matrix
In this example, the code for computing the eigenvalues and the eigenvectors using the numpy.linalg.eigh() function of a 2X2 matrix.
#import the required module
import numpy as np
#the predefined Hermitian matrix
M= np.array([[5, 3+7j], [3-7j, 2]])
print("the original matrix is=")
#displaying the original matrix
print(M)
#calculating the eigenvalues and eigenvectors
EVA,EVE= np.linalg.eigh(M)
#display the result
print("The eigenvalues of the Hermitian matrix are=")
print(EVA)
print("The eigenvectors of the Hermitian matrix are=")
print(EVE)
The output would be as shown below:
the original matrix is=
[[5.+0.j 3.+7.j]
[3.-7.j 2.+0.j]]
The eigenvalues of the Hermitian matrix are=
[-4.26208735 11.26208735]
The eigenvectors of the Hermitian matrix are=
[[-0.63511928+0.j -0.77241407+0.j ]
[ 0.30426881-0.70996055j -0.25018574+0.58376673j]]

Example 2: Computing the eigenvalues and eigenvectors of a 3×3 Hermitian matrix
Now we will use the linalg.eigh() on a square matrix of order 3 and this time we will mention the UPLO parameter and assign it to ‘U’ so that the eigenvalues are calculated based on the upper triangular region of the matrix.
#import the required module
import numpy as np
#the predefined Hermitian matrix
M= np.array([[0, 1j, 2-1j], [-1j, 0, 1- 1j],[-2+1j,-1+1j,0]])
print("the original matrix is=")
#displaying the original matrix
print(M)
#calculating the eigenvalues and eigenvectors
EVA,EVE= np.linalg.eigh(M,UPLO='U')
#display the result
print("The eigenvalues of the Hermitian matrix are=")
print(EVA)
print("The eigenvectors of the Hermitian matrix are=")
print(EVE)
The output of the above code would be:
the original matrix is=
[[ 0.+0.j 0.+1.j 2.-1.j]
[-0.-1.j 0.+0.j 1.-1.j]
[-2.+1.j -1.+1.j 0.+0.j]]
The eigenvalues of the Hermitian matrix are=
[-2.69399482 -0.25200039 2.9459952 ]
The eigenvectors of the Hermitian matrix are=
[[-0.47266115+0.3979061j -0.18340739-0.4629566j 0.58562203-0.16535393j]
[-0.3979061 +0.07475505j 0.4629566 +0.64636398j 0.16535393-0.42026811j]
[ 0.67405088+0.j 0.34629136+0.j 0.65248579-0.j ]]

Also read: NumPy linalg.matrix_power: Computing the power of a square matrix.
Conclusion
This tutorial covers a very important linear algebraic function for calculating the eigenvalues and eigenvectors of a Hermitian matrix using the Numpy module. This is a very easy-to-use function and the syntax is quite simple as shown in the examples here. To know more about numpy or to report bugs, visit the official documentation.


