Matrices in mathematics are rectangular arrangements of an array of homogeneous data types such as numbers, symbols, or expressions. They are arranged in the form of rows and columns which represents objects or some specific properties of that object. Eigenvalues are values that are scalar in type and are associated with linear matrix equations. They are used to compress dimensional space.
What is Numpy linalg.eigvals?
Numpy is the numerical python package/library that contains various functions used for scientific and complex mathematical computations involving linear algebra, matrices, and many more. Its built-in functions and libraries help us to calculate complicated equations easily. The numpy linalg.eigvals() computes the eigenvalues of a general matrix.
The required matrix should be a NxN matrix, that is, the number of rows and the number of columns of the matrix should be equal.
To use any numpy function you have to have the NumPy package installed in your system. Run the following code in your command prompt to install numpy in your system. Make sure you run the command prompt in administrator mode.
pip install numpy
The only difference between the two functions is that the former returns only one singular array containing the eigenvalues whereas the latter returns two arrays, one containing the eigenvalues and the other containing the eigenvectors.
Syntax and parameters of the linalg.eigvals() function:
The syntax of the numpy linalg.eigvals() function is as follows:
The parameters of the function are:
- the input: m: the required matrix(array-like)-> The matrix whose eigenvalues we have to compute.
- the output: x: array or ndarray -> The array of eigenvalues is unordered but repeated according to their multiplicities. Most of the time these values are complex, but in case the complex part is zero, the value returned is real.
- Error raised: In case the matrix is not a square matrix or if the eigenvalues diverge, it raises a LinAlgError.
How to use the function? (with examples)
Let’s now take a look at 3 examples of using the numpy eigvals() function
Example 1: Compute the eigenvalues of a general predefined matrix
A simple program to compute the eigenvalues of a general predefined matrix. This is the most simple form of using the linalg.eigvals() function.
#from numpy calling the function from numpy import linalg as L m=[[4,2],[1,3]] #pre defined matrix print("the initial matrix is=") print(m) #displaying the matrix result = L.eigvals(m) #compute result #display result print("the computed eigenvalues of the matrix is=") print(result)
The output of the above code is:
the initial matrix is= [[4, 2], [1, 3]] the computed eigenvalues of the matrix is= [5. 2.]
Example 2: Take user input for the matrix
This example illustrates how you can take user input for the matrix and every time you run the code, the user can modify the values, hence, this program can be used as an eigenvalue calculator.
import numpy as py from numpy import linalg as L #taking user input row = int(input("Enter the number of rows:")) col= int(input("Enter the number of columns:")) print("NOTE!! The number of rows should be equal to the number of columns") # Initializing the required matrix m =  print("enter the values rowwise:") # For user input for i in range(row): # loop for row entries b = for j in range(col): # loop for column entries b.append(int(input())) m.append(b) # For displaying the matrix print("The matrix is as follows:") print("[") for i in range(row): for j in range(col): print(m[i][j], end = " ") print() print("]") result= L.eigvals(m) #computing the values and vectors #displaying the result print("the eigenvalues are:",result)
The output is:
Enter the number of rows:2 Enter the number of columns:2 NOTE!! The number of rows should be equal to the number of columns enter the values rowwise: 6 5 4 1 The matrix is as follows: [ 6 5 4 1 ] the eigenvalues are: [ 8.62347538 -1.62347538]
Example 3: Generating a random matrix
Another example can be illustrated by generating a random matrix using the numpy.random() function and then using the linalg.eigvals() function. Here the matrix generated has random numbers and is not predefined or given by the user as input.
#from numpy calling the function import numpy as py from numpy import linalg as L num=py.random.random() #generating random number using the function random #creating the random matrix m = py.array([[py.sin(num), -py.cos(num)], [py.cos(num), py.sin(num)]]) print("the initial matrix is=") print(m) #displaying the matrix result = L.eigvals(m) #compute result #display result print("the computed eigenvalues of the matrix is=") print(result)
the initial matrix is= [[ 0.81665335 -0.5771285 ] [ 0.5771285 0.81665335]] the computed eigenvalues of the matrix is= [0.81665335+0.5771285j 0.81665335-0.5771285j]
This is a basic tutorial on how to use the eigvals() function, which involves three examples of different types of usage of the function. The only thing that needs to be in check is the shape of the matrix. It must be a square matrix so that the function doesn’t throw an error. Numpy also has many other linear algebraic functions which makes computation easy and less tedious for the user. For more info on numpy linear algebraic functions, click here.