A matrix is a rectangular sequence of numbers, expressions, and symbols organized in rows and columns. The multiplication of a square matrix and its inverse is equal to the identity matrix I. We can write it using the following equation:
AA-1= I
The numpy.linalg subpackage provides a function for an inverse operation: the inv() function. Let's invert a matrix using the numpy.linalg subpackage. First, we create a matrix using the mat() function and then find the inverse of the matrix using the inv() function, as illustrated in the following code block:
# Import numpy
import numpy as np
# Create matrix using NumPy
mat=np.mat([[2,4],[5,7]])
print("Input Matrix:\n",mat)
# Find matrix inverse
inverse = np.linalg.inv(mat)
print("Inverse:\n",inverse)
This results in the following output:
Input Matrix:
[[2 4]
[5 7]]
Inverse:
[[-1.16666667 0.66666667]
[ 0.83333333 -0.33333333]]
In the preceding code block, we have computed the inverse of a matrix using the...