We use matrix inverse to find a matrix that, when multiplied with the original, gives the identity matrix. This helps solve equations and understand relationships in data.
Solving systems of linear equations in engineering or physics.
Finding unknown variables in economic models.
Transforming data in computer graphics.
Calculating coefficients in linear regression manually.
Undoing transformations in image processing.
Syntax
SciPy
from scipy.linalg import inv
inverse_matrix = inv(matrix)
The input matrix must be a square 2D array (same number of rows and columns).
If the matrix is not invertible (singular), the function will raise an error.
Examples
Calculate the inverse of a 2x2 matrix.
SciPy
from scipy.linalg import inv
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
inverse = inv(matrix)
print(inverse)
Inverse of a diagonal matrix with 2s on the diagonal.
SciPy
from scipy.linalg import inv
import numpy as np
matrix = np.array([[2, 0], [0, 2]])
inverse = inv(matrix)
print(inverse)
Sample Program
This program calculates and prints the inverse of a 3x3 matrix using scipy's inv function.
SciPy
from scipy.linalg import inv
import numpy as np
# Define a 3x3 matrix
matrix = np.array([[4, 7, 2],
[3, 6, 1],
[2, 5, 1]])
# Calculate its inverse
inverse_matrix = inv(matrix)
# Print the inverse matrixprint(inverse_matrix)
OutputSuccess
Important Notes
Only square matrices can have an inverse.
If the matrix is singular (determinant zero), inv will raise a LinAlgError.
Use numpy.linalg.det to check if the determinant is zero before inverting.
Summary
Matrix inverse helps solve equations and reverse transformations.
Use scipy.linalg.inv on square, non-singular matrices.
Always check if the matrix is invertible before calculating the inverse.
Practice
(1/5)
1. What does the function scipy.linalg.inv do when applied to a matrix?
easy
A. It computes the determinant of the matrix.
B. It calculates the inverse of a square, non-singular matrix.
C. It transposes the matrix.
D. It multiplies the matrix by itself.
Solution
Step 1: Understand the purpose of scipy.linalg.inv
This function is designed to find the inverse of a matrix, which is a matrix that when multiplied by the original gives the identity matrix.
Step 2: Identify the matrix requirements
The matrix must be square (same number of rows and columns) and non-singular (determinant not zero) to have an inverse.
Final Answer:
It calculates the inverse of a square, non-singular matrix. -> Option B
Quick Check:
Inverse matrix = calculated by inv [OK]
Hint: Inverse = reverse matrix effect, only for square non-singular [OK]
Common Mistakes:
Confusing inverse with transpose
Trying to invert non-square matrices
Assuming singular matrices have inverses
2. Which of the following is the correct way to import and use the inverse function from scipy?
easy
A. import inv from scipy.linalg
matrix_inv = inv(matrix)
B. import scipy.linalg
matrix_inv = scipy.linalg.inverse(matrix)
C. from scipy import inv
matrix_inv = inv(matrix)
D. from scipy.linalg import inv
matrix_inv = inv(matrix)
Solution
Step 1: Recall correct import syntax
The function inv is inside scipy.linalg, so we import it using from scipy.linalg import inv.
Step 2: Use the function correctly
After import, call inv(matrix) to get the inverse.
Final Answer:
from scipy.linalg import inv
matrix_inv = inv(matrix) -> Option D
Quick Check:
Correct import and call = from scipy.linalg import inv
matrix_inv = inv(matrix) [OK]
Hint: Import inv from scipy.linalg, then call inv(matrix) [OK]
Common Mistakes:
Using wrong function name like 'inverse'
Incorrect import syntax
Trying to import inv directly from scipy
3. What is the output of this code?
import numpy as np
from scipy.linalg import inv
matrix = np.array([[1, 2], [3, 4]])
inverse_matrix = inv(matrix)
print(np.round(inverse_matrix, 2))