Numpy linalg.cond() – Compute the condition number of a matrix

Numpy Linalg Cond Compute The Condition Number Of A Matrix

The condition number defines the sensitivity of the output with respect to minute changes in the input data. It is an important feature of a matrix as it helps in identifying rounding off errors while solving systems of linear equations. It predicts the worst-case change in output due to some relative change in the input data of a problem.

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

How to use the Numpy linalg.cond function?

The NumPy library offers a huge range of functionalities for scientific calculations. Some of them can be used to solve systems of linear equations which are grouped as the linalg(linear algebra) functions. The linalg cond- function computes the condition number of a matrix. To know more about numpy linear algebra functions, click here.

In order to use the above-mentioned function, you need to have numpy installed in your system if you don’t already have it.

Run the code given below in your command prompt by running it in administrator mode to install numpy:

pip install numpy

Voila! Now you can easily import the numpy library in your programs and use the linalg.cond function.

Syntax of the Numpy Linalg.cond() function

The syntax of the function is as shown below:

numpy.linalg.cond( M, para= None)

The parameters of the functions are:

  • M (array_like, matrix) : This is the input matrix whose condition number we need to find out.
  • parameter(= None, optional): parameter or order of the matrix which can be used to calculate the norm of a matrix and to find out the condition number depending on the value of para.
  • The function returns R, which is a float data type and might be infinite(inf) in some cases.

Also read: Numpy fabs- Compute the absolute value element wise.

Example 1: Calculating the condition number of a predefined matrix.

In this example, we will simply observe how the linalg cond function calculates the condition number of an already defined matrix.

#importing the required library
import numpy as np
#initializing the array
a= np.array([[1,0,-1],[0,1,0],[1,0,1]])
#displaying the original matrix
print("The original matrix is:")
print(a)
#calculating the condition number using the linalg.cond function
R= np.linalg.cond(a)
#displaying the condition number of the matrix.
print("the condition number of the matrix is=",R)

The output of the above code would be:

The original matrix is:
[[ 1  0 -1]
 [ 0  1  0]
 [ 1  0  1]]
the condition number of the matrix is= 1.4142135623730951
The code and the output of the first example.
The code and the output of the first example.

Example 2: Condition number of a matrix using numpy arange() and reshape()

Using the arange function and the reshape function we can implicitly create a matrix of our desired dimension and find out it’s condition number.

#importing the required library
import numpy as np
#creating a 1dimensional array
a = np.arange(9) - 4
#reshaping the array into a 3X3 matrix
b = a.reshape((3, 3))
#displaying the original matrix
print("The original matrix is:")
print(b)
#calculating the condition number using the linalg.cond function
R= np.linalg.cond(b)
#displaying the condition number of the matrix.
print("the condition number of the matrix is=",R)

The output of the above code would be:

The original matrix is:
[[-4 -3 -2]
 [-1  0  1]
 [ 2  3  4]]
the condition number of the matrix is= 3.957101722134421e+16
The code and output for example 2
The code and output for example 2

Example 3: Calculating the condition number of a matrix when it is infinite

When the condition number of a matrix is infinite, the function returns inf. Let’s take a look at the same.

#importing the required library
import numpy as np
a = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])
#displaying the original matrix
print("The original matrix is:")
print(a)
#calculating the condition number using the linalg.cond function
R= np.linalg.cond(a)
#displaying the condition number of the matrix.
print("the condition number of the matrix is=",R)

The output of the code would be something like the one shown below.

The original matrix is:
[[1 0 1]
 [0 1 0]
 [1 0 1]]
the condition number of the matrix is= inf
The code and the output for example 3
The code and the output for example 3

Summary

We hope you now understnad the numpy library and one of its most important linear algebraic functions, namely, linalg. cond() which calculates the condition number of a matrix depending on various parameters hence eliminating the need to perform tedious manual calculations for complex values in a matrix. This article goes through the syntax of the function followed by three examples that illustrate the various ways in which you can use this function. To know more about numpy.linalg.cond() , visit the official documentation.