Bird
Raised Fist0
SciPydata~20 mins

Matrix inverse (inv) in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Matrix Inverse Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of matrix inverse calculation
What is the output of this code that calculates the inverse of a 2x2 matrix using scipy?
SciPy
import numpy as np
from scipy.linalg import inv

matrix = np.array([[4, 7], [2, 6]])
result = inv(matrix)
print(result)
A
[[ 1.5 -1.7]
 [-0.2  0.4]]
B
[[ 0.6 -0.7]
 [-0.2  0.4]]
C
[[ 0.6  0.7]
 [ 0.2  0.4]]
D
[[ 0.6 -0.7]
 [ 0.2 -0.4]]
Attempts:
2 left
💡 Hint
Recall the formula for inverse of 2x2 matrix [[a,b],[c,d]] is (1/(ad-bc))*[[d,-b],[-c,a]]
❓ data_output
intermediate
1:30remaining
Number of elements in inverse matrix
After computing the inverse of a 3x3 matrix using scipy.linalg.inv, how many elements does the resulting matrix contain?
SciPy
import numpy as np
from scipy.linalg import inv

matrix = np.array([[1,2,3],[0,1,4],[5,6,0]])
inverse = inv(matrix)
print(inverse.size)
A9
B6
C3
D1
Attempts:
2 left
💡 Hint
The inverse matrix has the same shape as the original matrix.
🔧 Debug
advanced
2:00remaining
Error type when inverting a singular matrix
What error does this code raise when trying to invert a singular matrix using scipy.linalg.inv?
SciPy
import numpy as np
from scipy.linalg import inv

singular_matrix = np.array([[1, 2], [2, 4]])
inv(singular_matrix)
AIndexError: index out of bounds
BValueError: Input must be square
CLinAlgError: Singular matrix
DTypeError: Unsupported operand type(s)
Attempts:
2 left
💡 Hint
A singular matrix has zero determinant and cannot be inverted.
🧠 Conceptual
advanced
1:00remaining
Effect of matrix inverse on identity matrix
If you multiply a matrix by its inverse, what is the resulting matrix?
AA matrix with all elements equal to one
BA zero matrix
CThe original matrix squared
DThe identity matrix
Attempts:
2 left
💡 Hint
Think about what an inverse matrix means in multiplication.
🚀 Application
expert
2:30remaining
Using matrix inverse to solve linear equations
Given the system of equations represented by Ax = b, where A is a 2x2 matrix and b is a vector, which code correctly computes x using scipy.linalg.inv?
SciPy
import numpy as np
from scipy.linalg import inv

A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
A
x = inv(A).dot(b)
print(x)
B
x = b.dot(inv(A))
print(x)
C
x = inv(b).dot(A)
print(x)
D
x = A.dot(inv(b))
print(x)
Attempts:
2 left
💡 Hint
Remember the formula x = A^-1 * b for solving Ax = b.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It calculates the inverse of a square, non-singular matrix. -> Option B
  4. 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

  1. Step 1: Recall correct import syntax

    The function inv is inside scipy.linalg, so we import it using from scipy.linalg import inv.
  2. Step 2: Use the function correctly

    After import, call inv(matrix) to get the inverse.
  3. Final Answer:

    from scipy.linalg import inv matrix_inv = inv(matrix) -> Option D
  4. 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))
medium
A. [[ -2. 1. ] [ 1.5 -0.5]]
B. [[ 4. -2. ] [-3. 1. ]]
C. [[ 0.25 0.5 ] [ 0.75 1. ]]
D. [[ 1.5 -1. ] [-1.5 1. ]]

Solution

  1. Step 1: Calculate determinant of the matrix

    Determinant = (1*4) - (2*3) = 4 - 6 = -2 (non-zero, so invertible).
  2. Step 2: Compute inverse using formula

    Inverse = (1/det) * [[d, -b], [-c, a]] = (1/-2) * [[4, -2], [-3, 1]] = [[-2, 1], [1.5, -0.5]].
  3. Final Answer:

    [[ -2. 1. ] [ 1.5 -0.5]] -> Option A
  4. Quick Check:

    Inverse matrix matches [[ -2. 1. ] [ 1.5 -0.5]] [OK]
Hint: Use formula inverse = 1/det * adjugate for 2x2 matrices [OK]
Common Mistakes:
  • Mixing up signs in the inverse formula
  • Not rounding output
  • Confusing transpose with inverse
4. Identify the error in this code snippet:
import numpy as np
from scipy.linalg import inv
matrix = np.array([[1, 2, 3], [4, 5, 6]])
inverse_matrix = inv(matrix)
print(inverse_matrix)
medium
A. Matrix is not square, so it cannot be inverted.
B. Missing import statement for numpy.
C. Using inv instead of inv().
D. Matrix contains integers instead of floats.

Solution

  1. Step 1: Check matrix shape

    The matrix has shape (2, 3), which is not square (rows ≠ columns).
  2. Step 2: Understand inverse requirements

    Only square matrices can have an inverse. Trying to invert a non-square matrix causes an error.
  3. Final Answer:

    Matrix is not square, so it cannot be inverted. -> Option A
  4. Quick Check:

    Non-square matrix = no inverse [OK]
Hint: Only square matrices can be inverted [OK]
Common Mistakes:
  • Ignoring matrix shape
  • Assuming all matrices are invertible
  • Confusing function call syntax
5. You have a 3x3 matrix A and want to solve the system Ax = b for x. Which approach using scipy.linalg.inv is correct and efficient?
hard
A. Calculate inv(A) and then multiply by b to get x.
B. Transpose A and multiply by b to get x.
C. Directly compute x = scipy.linalg.solve(A, b) without using inverse.
D. Use np.dot(A, b) to find x.

Solution

  1. Step 1: Understand solving linear systems

    To solve Ax = b, the best practice is to use specialized solvers like scipy.linalg.solve which are optimized and more stable.
  2. Step 2: Compare with inverse method

    Calculating inv(A) and then multiplying by b is less efficient and can introduce numerical errors.
  3. Final Answer:

    Directly compute x = scipy.linalg.solve(A, b) without using inverse. -> Option C
  4. Quick Check:

    Use solve() for linear systems, not inv() [OK]
Hint: Use solve() for Ax=b, avoid explicit inverse [OK]
Common Mistakes:
  • Using inverse for solving linear systems
  • Confusing transpose with inverse
  • Multiplying matrices incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard