Inverse of a matrix in Python

Written by

Himani Kohli

Inverse of a matrix program in Python

Here, we will learn to write the code for the inverse of a matrix.

We will use numpy.linalg.inv() function to find the inverse of a matrix.

If we multiply the inverse matrix with its original matrix then we get the identity matrix.

Algorithm:

  1. Import the package

  2. An array is initialized using numpy and stored in variable x.

  3. The array is inversed using the function numpy.linalg.inv(x) .

  4. Inversed array is stored in variable y.

  5. Now we’ll print both array x and y.

  6. Exit 

  7. Import the package numpy..

  8. Initialize an array using numpy and store it in the variable x.

  9. Use the function numpy.linalg.inv(x) to invert the array x. Store the result in the variable y.

  10. Print both the original array x and the inverted array y.

Code:

import numpy

x = numpy.array([[1,2], [3,4]])

y = numpy.linalg.inv(x)

print(x)

print (y)

Output:

[[1 2]
[3 4]]

[[-2. 1. ]
[ 1.5 -0.5]]
Inverse of a matrix in Python