
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can SciPy be used to calculate the inverse of a matrix in Python?
Sometimes, it may be required to mathematically compute the inverse of a matrix and use the result of the operation for other purposes. Below are the steps to manually find the inverse of a matrix.
Calculate the value of ‘minors’
In this calculation, the values of current row and column are ignored, and the determinant of the remaining values are found. The calculated minors are stored in a matrix.
The next step is to find the cofactors, wherein the alternate sign of values in the ‘minors’ matrix are changed from ‘+’ to ‘-‘ and vice-versa.
Next, the matrix is transposed, i.e the rows are converted to columns and columns are converted to rows.
The determinant of the original matrix is found, and all the elements in the previously computed matrix is divided by the determinant. The resulting matrix would be the inverse of the original matrix.
Finding the inverse of a matrix manually using calculations is a lengthy process. This is where the ‘inv’ function present in ‘SciPy’ library comes into play.
Syntax of ‘inv’ function
scipy.linalg.inv(matrix)
The ‘matrix’ is the parameter that is passed to the ‘inv’ function to find its inverse value.
Example
from scipy import linalg import numpy as np two_d_matrix = np.array([ [7, 9], [33, 8] ]) print("The inverse of the matrix is :") print(linalg.inv(two_d_matrix ))
Output
The inverse of the matrix is : [[-0.03319502 0.0373444 ] [ 0.13692946 -0.02904564]]
Explanation
- The required libraries are imported.
- A matrix is defined with certain values in it.
- Parameters are passed to the ‘inv’ function that computes the inverse of the matrix.
- The function is called.
- This output is displayed on the console.
- Related Articles
- How can SciPy be used to calculate the determinant value of a matrix in Python?
- How can SciPy be used to calculate the eigen values and eigen vectors of a matrix in Python?
- How can SciPy be used to calculate the permutations and combination values in Python?
- How can SciPy be used to calculate the cube root of values and exponential values in Python?
- Finding inverse of a square matrix using SciPy library
- How can Tensorflow be used to sum specific elements/rows of a matrix in Python?
- How can discrete Fourier transform be performed in SciPy Python?
- Explain how the minimum of a scalar function can be found in SciPy using Python?
- Compute the multiplicative inverse of a matrix in Python
- How to solve a circulant matrix equation using Python SciPy?
- Compute the multiplicative inverse of a matrix object with matrix() in Python
- How to find the inverse of a matrix in R?
- How to solve triangular matrix equations using Python SciPy?
- Which linear function of SciPy is used to solve a banded matrix equation?
- Which linear function of SciPy is used to solve the circulant matrix equation?
