Inverse of a matrix in Python
Written by
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:
-
Import the package
-
An array is initialized using numpy and stored in variable x.
-
The array is inversed using the function numpy.linalg.inv(x) .
-
Inversed array is stored in variable y.
-
Now we’ll print both array x and y.
-
Exit
-
Import the package
numpy
.. -
Initialize an array using
numpy
and store it in the variablex
. -
Use the function
numpy.linalg.inv(x)
to invert the arrayx
. Store the result in the variabley
. -
Print both the original array
x
and the inverted arrayy
.
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]]