Inverse of a matrix in Python
In this post, you will learn how to get the inverse of a matrix using Python.
The concept of inverse of a matrix is a multidimensional generalization of the concept of reciprocal of a number, the product between a number and its reciprocal is equal to 1. The product between a square matrix and its inverse is equal to the identity matrix.
In linear algebra, the inverse of a matrix A is a matrix that, when multiplied by A, results in the identity. The notation for this inverse matrix is A–1.
AA-1=I
where I is the identity matrix.
Not all matrices have inverses. The inverse of the matrix exists only if the matrix has a non-zero determinant. The matrix whose determinant is non-zero and for which the inverse matrix can be calculated is called an invertible matrix.
We can easily calculate the inverse of a matrix in Python. It provides numpy.linalg.inv() function to calculate the inverse of a matrix.
Matrix Inverse of 2 X 2 Matrix
import numpy as np
x = np.array([[21,5],[12,40]])
# Calculating the inverse of the matrix
y = np.linalg.inv(x)
print x
print y
print np.dot(x,y)
Output of the above code:
[[21 5]
[12 40]]
[[ 0.05128205 -0.00641026]
[-0.01538462 0.02692308]]
[[1.00000000e+00 2.42861287e-17]
[2.77555756e-17 1.00000000e+00]]
Matrix Inverse of 3 X 3 Matrix
import numpy as np
arr = np.array([[3, 1, 2],
[9, 5, 6],
[7, 4, 8]])
x = np.array(arr)
# Calculating the inverse of the matrix
y = np.linalg.inv(x)
print(x)
print(y)
Output of the above code:
[[3 1 2]
[9 5 6]
[7 4 8]]
[[ 8.00000000e-01 -4.93432455e-17 -2.00000000e-01]
[-1.50000000e+00 5.00000000e-01 -0.00000000e+00]
[ 5.00000000e-02 -2.50000000e-01 3.00000000e-01]]
Matrix Inverse of 4 X 4 Matrix
import numpy as np
arr = np.array([[5, 2, 6, 1],
[1, 3, 4, 7],
[1, 8, 2, 5],
[4, 9, 2, 1]])
x = np.array(arr)
# Calculating the inverse of the matrix
y = np.linalg.inv(x)
print(x)
print(y)
Output of the above code:
[[5 2 6 1]
[1 3 4 7]
[1 8 2 5]
[4 9 2 1]]
[[-0.40441176 0.75 -1.13970588 0.85294118]
[ 0.09558824 -0.25 0.36029412 -0.14705882]
[ 0.51838235 -0.625 0.91544118 -0.72058824]
[-0.27941176 0.5 -0.51470588 0.35294118]]
Calculating inverses of several matrices
Here, we have given several matrices as an input to the function.
# Import required package
import numpy as np
A = np.array([[[3., 1.], [2., 4.]],
[[1, 4], [3, 2]]])
# Calculating the inverse of the matrix
print(np.linalg.inv(A))
Output of the above code:
[[[ 0.4 -0.1]
[-0.2 0.3]]
[[-0.2 0.4]
[ 0.3 -0.1]]]
Related Articles
Transpose of a matrix in pythonPython program to multiply two matrices
Matrix addition in Python
Python program to multiply two matrices
Python Numpy Array Shape
Python Pandas Dataframe to CSV
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
2d arrays in Python
Python add list to list
Python convert xml to dict
Python dict inside list
Multiply all elements in list Python
Python heap implementation using heapq module
numpy dot product
Python convert dict to xml
Python weather api
Python raise keyword