NumPy Inverse Matrix in Python

Spread the love

NumPy linalg.inv() function in Python is used to compute the (multiplicative) inverse of a matrix. The inverse of a matrix is that matrix which when multiplied with the original matrix, results in an identity matrix. In this article, I will explain how to use the NumPy inverse matrix to compute the inverse of the matrix array using this function.

1. Quick Examples of Inverse Matrix

If you are in a hurry, below are some quick examples of how to use Python NumPy inverse matrix.


# Below are the quick examples

# Example 1: Use numpy.linalg.inv() to get inverse of a matrix
arr = np.array([[7, 2,], [3, -5]])
arr2 = np.linalg.inv(arr)

# Example 2: get the inverse of a matrix using scipy.linalg.inv() function
arr = np.matrix([[7, 2,], [3, -5]])
arr2 = linalg.inv(arr)

# Example 3: Use np.linalg.inv() function 
arr = np.array([[[2., 6.], [5., 8.]], 
                [[3, 7], [4, 1]]])
arr2 = np.linalg.inv(arr)

2. Syntax of numpy.linalg.inv() Function

Following is the syntax to create numpy.linalg.inv() function.


# Syntax of numpy.linalg.inv() function
numpy.linalg.inv(arr)

2.1 Parameter of Inverse Matrix

Following are the parameters of the inverse matrix.

arr : This parameter represents the matrix to be inverted.

2.2 Return Value of Inverse Matrix

This function returns the inverse of the matrix array.

3. Usage of numpy.linalg.inv() Function

Using Python numpy.linalg.inv() function to the inverse of a matrix in simple mathematics can be defined as a matrix.

3.1 Use numpy.linalg.inv() Function

We can use a matrix as a rectangular arrangement of data or numbers, in other words, we can say that it is a rectangular array of data the horizontal entries in the matrix are called rows and the vertical entries are called columns. For the matrix inverse function, we need to use np.linalg.inv() function. This function will inverse the given matrix. Python NumPy provides an easy function to calculate the inverse of the matrix. The function helps the user to check numpy.linalg.inv() is available in the Python library.


import numpy as np

# creating an input array  
arr = np.array([[7, 2,], [3, -5]])
               
# Use numpy.linalg.inv() to get inverse of a matrix
arr2 = np.linalg.inv(arr)
print(arr2)

# Output
# [[ 0.12195122  0.04878049]
#  [ 0.07317073 -0.17073171]]

4. Get the Inverse of a Matrix Using scipy.linalg.inv() Function

We can also use the scipy module to perform different scientific calculations using its functionalities. Using scipy.linalg.inv() function is used to return the inverse of a given square matrix in NumPy Python. It works the same way as the numpy.linalg.inv() function.


import numpy as np
from scipy import linalg 

# creating an input array  
arr = np.matrix([[7, 2,],[3, -5]])
          
# get the inverse of a matrix using scipy.linalg.inv() function
arr2 = linalg.inv(arr)
print(arr2)

Yields the same output as above.

5. Inverse of a Matrix NumPy Two Multi-Dimensional Arrays

We can also use np.linalg.inv() function to compute the multiplicative inverse of a matrix of the two multi-dimensional arrays elementwise.


import numpy as np

# inverse of 4X4 matrix   
arr = np.array([[[2., 6.], [5., 8.]],
               [[3, 7], [4, 1]]])
 
# Use np.linalg.inv() function 
arr2 = np.linalg.inv(arr)
print(arr2)

# Output
# [[[-0.57142857  0.42857143]
#  [ 0.35714286 -0.14285714]]

# [[-0.04        0.28   ]
# [ 0.16       -0.12   ]]]

6. Conclusion

In this article, I have explained how to use the inverse matrix to compute the inverse of the matrix array with examples.

Happy Learning!!

References

Leave a Reply

You are currently viewing NumPy Inverse Matrix in Python