The Numpy library provides a function `linalg.eigvalsh`

that calculates the eigenvalues of a complex Hermitian or real symmetric matrix. This function is efficient and robust, making it a valuable tool for linear algebraic computations. In this article, we will explore the functionality of `linalg.eigvalsh`

and how it can be used to obtain the eigenvalues of a matrix.

**Also read: Numpy linalg.cond() â€“ Compute the condition number of a matrix**

## What is a Hermitian matrix?

A Hermitian matrix is a square matrix with complex numbers as its elements, characterized by the property that its conjugate transpose is equal to the original matrix. In other words, if you take the transpose of a Hermitian matrix and also take the complex conjugate of each element, you will get the same matrix as the original.

This type of matrix is used extensively in various fields of science and engineering, such as quantum mechanics, electrical engineering, and signal processing. In quantum mechanics, Hermitian matrices are used to describe the observables of a quantum system, such as the position, momentum, and energy of a particle. In electrical engineering, Hermitian matrices are used to represent linear time-invariant systems, such as filters and transformers, and to analyze the stability and convergence of algorithms.

In terms of implementation, Hermitian matrices can be efficiently represented and manipulated using various numerical methods, such as eigenvalue decomposition and singular value decomposition. Developers working in these fields should have a solid understanding of Hermitian matrices and their properties, as well as the numerical methods used to handle them.

The numpy.linalg.eigvalsh() function computes the eigenvalues of a complex Hermitian matrix or a real symmetric matrix.

*Also read: Numpy linalg.pinv(): Computing the Pseudo-Inverse of a Matrix*

## Mathematical representation of a Hermitian matrix

Let’s consider a simple example to understand this better. Suppose we have a 2×2 matrix A with complex numbers as elements:

```
A = [a + bi, c + di; e + fi, g + hi]
```

Where “a”, “c”, “e”, “g” are real numbers, and “bi”, “di”, “fi”, “hi” are imaginary numbers.

To see if this matrix is a Hermitian matrix, we need to take its conjugate transpose and see if it’s equal to the original matrix. The conjugate transpose of a matrix is found by taking the transpose (flipping it over) and also changing the sign of the imaginary part of each element. So for our matrix A, the conjugate transpose would be:

```
A* = [a - bi, e - fi; c - di, g - hi]
```

Now we compare the original matrix A and its conjugate transpose A*. If they are the same, then we can say that the matrix A is a Hermitian matrix. In our example, we can see that the two matrices are indeed the same, so A is a Hermitian matrix.

Let’s look at another example:

let the original matrix be A, where A is :

Its conjugate transpose matrix would also be the same as the original matrix. Therefore, A* = A, hence it is a Hermitian matrix.

## Eigenvalues of a complex Hermitian or real symmetric matrix

Eigenvalues in association with eigenvectors of a matrix are used for analyzing systems of linear equations and transformations related to the same. Eigenvalues are scalar in nature and they tell us about the magnitude of linear transformation associated with the eigenvectors of a matrix.

In the case of Hermitian matrices, the eigenvalues are always real. The sum of the eigenvalues of a square matrix is always equal to the trace of a matrix. The trace of a square matrix is defined by the sum of the values along its main diagonal. Since a Hermitian matrix’s diagonal values are always real, the eigenvalues of a Hermitian matrix must always be real.

## Installing numpy and basic syntax of the numpy.linalg.eigvalsh() function

To use the numpy linalg.eigvalsh() function it is necessary that you have Numpy preinstalled in your system. If not, run the following command in your command prompt in administrator mode to fulfill all dependencies:

```
pip install numpy
```

Now you can import the numpy library in your code and utilize the function.

*Also read: Numpy linalg.eig â€“ Compute the eigenvalues and right eigenvectors of a square array.*

The syntax of the function is shown below:

```
linalg.eigvalsh(M , UPLO = 'L/M/optional)
```

The parameters of the function are:

**M**: (array_like / matrix): The numpy square Hermitian/real symmetric matrix whose eigenvalues we need to calculate.**UPLO**: {‘L’ , ‘U’}, optional: This parameter is used to specify whether we want to calculate the eigenvalues considering the lower triangular portion or the upper triangular portion. Irrespective of this parameter, only the real values are considered when calculating the eigenvalues and even if there are imaginary values along the diagonals, they are ignored.

This function returns R(ndarray) which contains the eigenvalues in increasing order.

If the matrix is not square, or if the eigenvalues diverge, a LinAlgError is raised by this function.

### Example 1: A simple program using the eigvalsh() function to calculate the eigenvalues of a predefined matrix

This example illustrates the code for calculating the eigenvalues of a predefined complex Hermitian matrix.

```
#import the required module
import numpy as py
#the predefined Hermitian matrix
M= py.array([[5, 3+7j], [3-7j, 2]])
print("the original matrix is=")
#displaying the original matrix
print(M)
#calculating the eigenvalues
R= py.linalg.eigvalsh(M)
#display the result
print("The eigenvalues of the Hermitian matrix are=")
print(R)
```

The output would be:

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

### Example 2: Calculating the eigenvalues of a 3X3 Hermitian matrix

The code for this example is as follows:

```
#import the required module
import numpy as py
#the predefined Hermitian matrix
M= py.array([[7,2 + 1j,3j],[2 -1j,6,2],[-3j ,-2, -5]])
print("the original matrix is=")
#displaying the original matrix
print(M)
#calculating the eigenvalues
R= py.linalg.eigvalsh(M)
#display the result
print("The eigenvalues of the Hermitian matrix are=")
print(R)
```

The output of the above code would be:

```
the original matrix is=
[[ 7.+0.j 2.+1.j 0.+3.j]
[ 2.-1.j 6.+0.j 2.+0.j]
[-0.-3.j -2.+0.j -5.+0.j]]
The eigenvalues of the Hermitian matrix are=
[-6.12627843 4.97833209 9.14794634]
```

## Summary

This article entails he working of the numpy linalg.eigvalsh() function which is a part of the numerical python library used for calculating the real eigenvalues of a complex Hermitian matrix or a real symmetric matrix. To know more about this function click here. To find out about more numpy linear algebra functions click here.