
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Compute the eigenvalues of a complex Hermitian or real symmetric matrix in Python
To compute the eigenvalues of a complex Hermitian or real symmetric matrix, use the numpy.eigvalsh() method. The method returns the eigenvalues in ascending order, each repeated according to its multiplicity.
The 1st parameter, a is a complex- or real-valued matrix whose eigenvalues are to be computed. The 2nd parameter, UPLO specifies whether the calculation is done with the lower triangular part of a (‘L’, default) or the upper triangular part (‘U’). Irrespective of this value only the real parts of the diagonal will be considered in the computation to preserve the notion of a Hermitian matrix. It therefore follows that the imaginary part of the diagonal will always be treated as zero.
Steps
At first, import the required libraries -
import numpy as np from numpy import linalg as LA
Creating a 2D numpy array using the numpy.array() method −
arr = np.array([[5+2j, 9-2j], [0+2j, 2-1j]])
Display the array −
print("Our Array...\n",arr)
Check the Dimensions −
print("\nDimensions of our Array...\n",arr.ndim)
Get the Datatype −
print("\nDatatype of our Array object...\n",arr.dtype)
Get the Shape −
print("\nShape of our Array object...\n",arr.shape)
To compute the eigenvalues of a complex Hermitian or real symmetric matrix, use the numpy.eigvalsh() method −
print("\nResult...\n",LA.eigvalsh(arr))
Example
from numpy import linalg as LA import numpy as np # Creating a 2D numpy array using the numpy.array() method arr = np.array([[5+2j, 9-2j], [0+2j, 2-1j]]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To compute the eigenvalues of a complex Hermitian or real symmetric matrix, use the numpy.eigvalsh() method print("\nResult...\n",LA.eigvalsh(arr))
Output
Our Array... [[5.+2.j 9.-2.j] [0.+2.j 2.-1.j]] Dimensions of our Array... 2 Datatype of our Array object... complex128 Shape of our Array object... (2, 2) Result... [1. 6.]
- Related Articles
- PyTorch – How to compute the eigenvalues and eigenvectors of a square matrix?
- A square matrix as sum of symmetric and skew-symmetric matrix ?
- Python Pandas - Compute the symmetric difference of two Index objects
- Change the real part of the complex argument in Python
- Return the real part of the complex argument in Python
- Compute the multiplicative inverse of a matrix in Python
- Compute the multiplicative inverse of a matrix object with matrix() in Python
- Compute the roots of a polynomial with given complex roots in Python
- Check if a matrix is symmetric using Python
- Compute the Moore-Penrose pseudoinverse of a matrix in Python
- Compute the square root of complex inputs with scimath in Python
- Compute the roots of a Laguerre series with given complex roots in Python
- Compute the roots of a Hermite series with given complex roots in Python
- Compute the roots of a Chebyshev series with given complex roots in Python
- Compute the roots of a Legendre series with given complex roots in Python
