The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. The inverse of a matrix can also be calculated in Python.
This tutorial demonstrates the different ways available to find the inverse of a matrix in Python.
Table of Contents
NumPy
module, which is an abbreviation for Numerical Python, in dealing with matrices and arrays in Python. All the matrices are stored and worked on using functions from this huge library.
We can easily determine the inverse of a matrix by using simple mathematical concepts. However, there is also an in-built function that makes things quicker and easier and will be explained further in the article below.
The one important condition to note while finding an inverse of a matrix is that an inverse only exists for a particular matrix only if its determinant is zero. This condition may help in saving some time and energy if you get any unexpected errors during the runtime of any of the codes mentioned below.
Using the Gauss-Jordan method to find the inverse of a given matrix in Python.
The Gauss-Jordan method is an algorithm that exists in simple mathematics which is utilized to find out the inverse of any matrix whose inverse exists. The implementation of this method relies on elementary row operations. This method can be implemented in Python by simply making user-defined functions.
The NumPy
library is used and needs to be imported to the working code to run the program without any errors.
The following code uses the Gauss-Jordan method to find the inverse of a matrix in Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import numpy as np import sys x = int(input('The order of matrix is? -> ')) m = np.zeros((x,2*x)) print('Enter the matrix values ->') for i in range(x): for j in range(x): m[i][j] = float(input( 'm['+str(i)+']['+ str(j)+']=')) for i in range(x): for j in range(x): if i == j: m[i][j+x] = 1 for i in range(x): if m[i][i] == 0.0: sys.exit('Divide by zero detected!') for j in range(x): if i != j: ratio = m[j][i]/m[i][i] for k in range(2*x): m[j][k] = m[j][k] - ratio * m[i][k] for i in range(x): divisor = a[i][i] for j in range(2*x): m[i][j] = m[i][j]/divisor print('\nINVERSE MATRIX IS:') for i in range(x): for j in range(x, 2*x): print(m[i][j], end='\t') print() |
The above code provides the following output:
Enter the matrix values ->
a[0][0]=5
a[0][1]=6
a[1][0]=7
a[1][1]=8
INVERSE MATRIX IS:
-4.0 3.0
3.5 -2.5
Explanation
- The order of the matrix is stored in the variable
x
. - A new matrix is initialized using the
np.zeros()
function according to the desired order entered by the user. - Then, the values of all the coefficients of the matrix are made to input using the
for
loop. - Then, the process of the Gauss-Jordan elimination method is initiated with the help of custom user-defined functions.
- The inverse matrix is then printed out in the end.
Further reading:
Using the numpy.linalg.inv()
function to find the inverse of a given matrix in Python.
The NumPy
module contains a function numpy.linalg.inv()
, which is utilized to directly find the inverse of a given NumPy
matrix.
This functioned is contained within the NumPy
module, which manually needs to be imported to the working code to run the program without any errors at runtime.
The following code uses the numpy.linalg.inv()
function to find the inverse of a given matrix in Python.
1 2 3 4 5 6 7 8 9 |
import numpy a = np.array([[5,6],[7,8]]) b=numpy.linalg.inv(a) print("Original Matrix:") print(a) print("Inverse Matrix:") print(b) |
The above code provides the following output:
[[5 6] [7 8]] Inverse matrix:
[[-4. 3. ] [ 3.5 -2.5]]
Explanation
- The
NumPy
module is imported to the Python code. - A matrix of any specific order is initialized and its value is stocked in the variable
a
. - The
numpy.linalg.inv()
function is implemented on the original matrix and the result is stored in the variableb
. - Both variables
a
andb
are printed and the results are shown.
That’s all about how to inverse matrix in Python.