We use np.linalg.inv() to find the inverse of a square matrix. The inverse helps us solve equations and understand matrix behavior.
np.linalg.inv() for matrix inverse in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
np.linalg.inv(matrix)
The input matrix must be a square 2D array (same number of rows and columns).
If the matrix is not invertible (singular), this function will raise an error.
Examples
NumPy
import numpy as np A = np.array([[1, 2], [3, 4]]) inv_A = np.linalg.inv(A) print(inv_A)
NumPy
import numpy as np B = np.array([[4, 7], [2, 6]]) inv_B = np.linalg.inv(B) print(inv_B)
NumPy
import numpy as np C = np.array([[1, 2, 3], [0, 1, 4], [5, 6, 0]]) inv_C = np.linalg.inv(C) print(inv_C)
Sample Program
This program shows how to find the inverse of a 2x2 matrix and verifies the result by multiplying the matrix with its inverse. The product should be the identity matrix.
NumPy
import numpy as np # Define a 2x2 matrix matrix = np.array([[2, 3], [1, 4]]) # Calculate its inverse inverse_matrix = np.linalg.inv(matrix) # Print the original and inverse matrices print("Original matrix:") print(matrix) print("\nInverse matrix:") print(inverse_matrix) # Verify by multiplying original and inverse (should be identity matrix) identity = np.dot(matrix, inverse_matrix) print("\nProduct of original and inverse (Identity matrix):") print(identity)
Important Notes
If the matrix is singular (no inverse), np.linalg.inv() will raise a LinAlgError.
For large matrices, computing the inverse can be slow and unstable; consider other methods like solving linear systems directly.
Summary
np.linalg.inv() finds the inverse of a square matrix.
The matrix must be square and invertible.
Multiplying a matrix by its inverse gives the identity matrix.
Practice
1. What does the function
np.linalg.inv() do in numpy?easy
Solution
Step 1: Understand the function purpose
np.linalg.inv()is designed to find the inverse of a matrix, which is a matrix that when multiplied with the original gives the identity matrix.Step 2: Differentiate from other matrix operations
Calculating determinant, transpose, or multiplication are different functions in numpy, notnp.linalg.inv().Final Answer:
It calculates the inverse of a square matrix. -> Option AQuick Check:
np.linalg.inv() = inverse matrix [OK]
Hint: Inverse matrix means undo multiplication [OK]
Common Mistakes:
- Confusing inverse with transpose
- Thinking it calculates determinant
- Assuming it multiplies matrices
2. Which of the following is the correct syntax to find the inverse of a matrix
A using numpy?easy
Solution
Step 1: Recall numpy linear algebra module
The inverse function is inside thelinalgmodule of numpy, so it must be called asnp.linalg.inv().Step 2: Check function names
Functions likenp.inv(),np.inverse(), ornp.linalg.inverse()do not exist in numpy.Final Answer:
np.linalg.inv(A) -> Option CQuick Check:
Correct syntax = np.linalg.inv(A) [OK]
Hint: Use np.linalg.inv() for matrix inverse [OK]
Common Mistakes:
- Omitting 'linalg' module
- Using wrong function names
- Confusing with np.inverse()
3. You have a matrix
C = np.array([[2, 3], [1, 4]]). You want to verify that np.linalg.inv(C) is correct by multiplying C with its inverse. What should the result be?easy
Solution
Step 1: Recall property of inverse matrices
Multiplying a matrix by its inverse results in the identity matrix, which has 1s on the diagonal and 0s elsewhere.Step 2: Apply to matrix C
So,C @ np.linalg.inv(C)should produce the identity matrix of size 2x2.Final Answer:
An identity matrix of the same size as C. -> Option AQuick Check:
Matrix x inverse = identity matrix [OK]
Hint: Matrix times inverse equals identity [OK]
Common Mistakes:
- Expecting zero matrix instead
- Confusing with original matrix
- Thinking result is all ones
4. What is the output of the following code?
import numpy as np A = np.array([[1, 2], [3, 4]]) inv_A = np.linalg.inv(A) print(np.round(inv_A, 2))
medium
Solution
Step 1: Calculate determinant of A
Determinant = (1*4) - (2*3) = 4 - 6 = -2.Step 2: Compute inverse using formula
Inverse = (1/det) * [[4, -2], [-3, 1]] = (-1/2) * [[4, -2], [-3, 1]] = [[-2, 1], [1.5, -0.5]].Step 3: Check numpy output
Numpy returns approximately [[-2., 1.], [1.5, -0.5]].np.round(inv_A, 2)prints as [[-2. 1. ] [ 1.5 -0.5]], matching [[-2. 1. ] [ 1.5 -0.5]].Final Answer:
[[ 0.5 -0.25] [-0.75 0.25]] -> Option DQuick Check:
np.linalg.inv(A) rounded = [[ 0.5 -0.25] [-0.75 0.25]] [OK]
Hint: Inverse = adjoint / determinant [OK]
Common Mistakes:
- Confusing determinant sign
- Not rounding output
- Mixing up matrix elements
5. What is the error in the following code snippet?
import numpy as np B = np.array([[1, 2, 3], [4, 5, 6]]) inv_B = np.linalg.inv(B) print(inv_B)
medium
Solution
Step 1: Check matrix shape
Matrix B has shape (2, 3), which is not square (rows != columns).Step 2: Understand np.linalg.inv() requirements
np.linalg.inv() requires a square matrix to compute the inverse. Non-square matrices cannot have inverses.Final Answer:
ValueError because B is not a square matrix. -> Option BQuick Check:
Non-square matrix = ValueError [OK]
Hint: Inverse only for square matrices [OK]
Common Mistakes:
- Trying inverse on non-square matrix
- Confusing error type
- Assuming code runs without error
