What is the numpy.linalg.inv() Function in Python

The numpy.linalg.inv() function computes the inverse of a matrix. The function takes a square matrix as input and returns a square matrix as output. The output matrix is the inverse of the input matrix.

The inverse of a matrix is that matrix which, when multiplied with the original matrix, results in an identity matrix.

Equation For Getting Inverse Of A Matrix

A*x= B
A^-1 A*x= A-1 B
x= A-1 B

Where A^-1: It denotes the inverse of a matrix.

                x: It denotes an unknown column.

                B: It denotes the solution matrix.

Now, let’s see the procedure for using Numpy to find the inverse of a matrix.

Syntax

numpy.linalg.inv(A) 

Parameters

A: It denotes the Matrix to be inverted.

Return Value

The inverse of Matrix A is returned.

Example 1

import numpy as np

A = np.array([[-5, -2, 3, 4],
              [3, 1, 2, 7],
              [2, 7, -5, 2],
              [6, -6, 8, 4]])

print(np.linalg.inv(A))

Output

[[-0.19186047 0.1627907 -0.11046512 -0.0377907 ]
 [ 0.64534884 -1.09302326 1.09883721 0.71802326]
 [ 0.73837209 -1.23255814 1.12209302 0.85755814]
 [-0.22093023 0.58139535 -0.43023256 -0.33139535]]

Explanation

Here, A matrix was given as input to the function, and then the Inverse of a matrix was returned as the output.

Example 2

import numpy as np

A = np.array([[[3., 4.], [4., 5.]],
              [[6, 7], [7, 9]]])

print(np.linalg.inv(A))

Output

[[[-5. 4. ]
 [ 4. -3. ]]

 [[ 1.8 -1.4]
 [-1.4 1.2]]]

Explanation

Here, we gave several matrices as an input to the function, and then the inverse of a matrix was returned as the output.

That is it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.