Compute the inverse of a matrix using inv() function of NumPy


The inverse matrix is a matrix that gives a multiplicative identity when multiplied with its original matrix. It is denoted by A-1. The inverse matrix can be calculated for the square matrices with the n x n size.

The mathematical formula given for calculating the inverse matrix is as follows.

A-1 . A = A . A-1 = I

Where,

  • A is the original matrix.

  • A-1 is the inverse of the original matrix A.

  • I is the identity matrix.

Let’s take the original matrix as A with the size 2 x 2 and elements as , then the formula for calculating the A-1 is as follows.

A-1 = 1/ (ad - bc) * [[d, -b], [-c, a]]

Where,

  • a, b, c, d are the elements in the matrix

  • A-1 is the inverse of the original matrix A.

The most general formula for calculating the inverse of the matrix is given as below.

A^-1 = (1/det(A)) * adj(A)

Where,

  • det(A) is the determinant of the matrix A.

  • adj(A) is the adjugate of A, which is the transpose of the matrix cofactors.

The inv() function in Numpy

In Python the Numpy which has the module linalg and it has a function inv() which is used to calculate the inverse of the given matrices.

Syntax

Following is the syntax for applying the inv() function on the arrays to calculate the inverse of the matrix.

import numpy as np
np.linalg.inv(arr)

Where,

  • arr is the input array.

Example

To calculate the inverse of the given array, we have to pass it as a parameter to the inv() function as shown in the following example –

import numpy as np
a = np.array([[22,1],[14,5]])
print("The input array:",a)
inverse = np.linalg.inv(a)
print("The Inverse of the given input matrix:", inverse)

Output

The input array: [[22 1]
   [14 5]]
The Inverse of the given input matrix: [[ 0.05208333 -0.01041667]
   [-0.14583333 0.22916667]]

Example

The inv() function accepts only a square matrix i.e. 2 x 2, 3 x 3 etc. Let’s see an example in which we pass the array with the size 3 x 2 to the function, then the output will be an error as the function accepts the square size array.

import numpy as np
a = np.array([[22,1,7],[14,5,2]])
print("The input array:",a)
inverse = np.linalg.inv(a)
print("The Inverse of the given input matrix:”, inverse)

Error

 File "/home/cg/root/19762/main.py", line 5

    print("The Inverse of the given input matrix:”, inverse)
          ^
SyntaxError: unterminated string literal (detected at line 5)

Example

Let’s see another example to understand the inv() function for calculating the inverse of a 3-d array with the n x n size. The following is the code for the reference.

import numpy as np
a = np.array([[[34,23],[90,34]],[[43,23],[10,34]]])
print("The input array:",a)
inverse = np.linalg.inv(a)
print("The inverse of the given array:",inverse)

Output

The input array: [[[34 23]
  [90 34]][[43 23]
  [10 34]]]
The inverse of the given array: [[[-0.03719912  0.02516411]
  [ 0.09846827 -0.03719912]][[ 0.0275974  -0.01866883]
  [-0.00811688  0.0349026 ]]]

Updated on: 07-Aug-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements