Numpy np.eigvals() method - Python
The np.eigvals() method in NumPy is used to compute the eigenvalues of a square matrix.
Example 1: Here we calculate the eigenvalues of a simple 2x2 matrix.
from numpy import linalg as LA
val = LA.eigvals([[1, 2], [3, 4]])
print(val)
Output
[-0.37228132 5.37228132]
Explanation: matrix [[1, 2], [3, 4]] has two eigenvalues: -0.37 and 5.37. These values indicate the scaling factors when the matrix acts on its eigenvectors.
Syntax:
numpy.linalg.eigvals(a)- Parameters: a 2D square matrix (array-like) whose eigenvalues are to be calculated.
- Returns: eigenvalues 1D NumPy array of eigenvalues (real or complex).
Example 2: Now, let’s compute eigenvalues for a 3x3 matrix.
from numpy import linalg as LA
val = LA.eigvals([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(val)
Output
[ 1.61168440e+01 -1.11684397e+00 -9.75918483e-16]
Explanation: 3x3 matrix has three eigenvalues: 16.11, -1.11 and a very small value close to 0 (floating-point approximation of 0).
Example 3: This example shows eigenvalues of a symmetric matrix, which are always real numbers.
from numpy import linalg as LA
val = LA.eigvals([[2, -1], [-1, 2]])
print(val)
Output
[3. 1.]
Explanation: For symmetric matrix [[2, -1], [-1, 2]], the eigenvalues are 3 and 1. Symmetric matrices guarantee real eigenvalues, which is why the result has no complex part.