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. The diagonal values of the matrix are only real and the rest are 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 equal to the trace(sum of all the 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( M, UPLO = 'L'/'U'/'None'(optional))
```

**The parameters of the function are:**

**M( array_like / matrix)**: The M matrix is the input which is a complex Hermitian or a real symmetric in nature.**UPLO**{‘L’,’U’}(optional): It refers to “upper(U)” or “lower(L)” triangular portion of the matrix along which the eigenvalues of the matrix are to be calculated. Regardless of this parameter, only the real values are used to compute the eigenvalues of a matrix. In case, there is a complex value in the diagonal, then it is ignored.

This function returns two objects: ‘EVA'(data type-ndarray) which consists of the eigenvalues of the matrix in increasing order according to its multiplicity. And the ‘EVE'(datatype-array or matrix) which consists of the eigenvector for each eigenvalue respectively.

## Working with Numpy linalg.eigh()

This function also raises an error LinAlgError if the eigenvalues diverge.

### Installing the Numpy module

For using the above function you need to have the latest version of numpy installed in your system. If you don’t already have numpy, run the following code in your command prompt in administrator mode.

```
pip install numpy
```

If you’re using anaconda, run the following code in your command prompt in administrator mode:

```
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.