Finding the inverse of a matrix

The numpy Python library has the inv() function from the lining module that finds the square matrix for a given matrix. The inv() function can be a passed multiple matrices and their inverses are returned.

Linear Algebra Updated 1 Apr 2025 1 min read

Overview

  • When a square matrix is multiplied with its inverse matrix, the resulatant matrix is the identity matrix.
  • The function inv() of the numpy.linalg module returns the inverse matrix of a given matrix.
  • Not all matrices have their inverse matrices. The matrices that do not have an inverse are called singular matrices.

Example

python
# Example Python program that finds the inverse matrix of a given matrix

            import numpy

# Format of floating point numbers while printing

            numpy.set_printoptions(suppress=True, formatter={'float_kind':'{:0.2f}'.format})

# Create a square matrix

            numbers = numpy.arange(1, 8, 2)

            matrix  = numbers.reshape(2, 2)

            print("Matrix:")

            print(matrix)

# Find the inverse of the matrix

            inverse = numpy.linalg.inv(matrix)

            print("Inverse of the matrix:")

            print(inverse)

Output

Output
Matrix:

[[1 3]

 [5 7]]

Inverse of the matrix:

[[-0.88 0.38]

 [0.62 -0.12]]