← Back to NumPy Course | Chapter 7: Linear Algebra | Lesson 2 of 6

np.linalg.inv()

The inverse of a matrix undoes it: multiplying a matrix by its inverse gives the identity matrix.

In this page:

  1. np.linalg.inv()
Syntax
python
inverse = np.linalg.inv(matrix)

np.linalg.inv()

np.linalg.inv returns the inverse of a square, non-singular matrix. Multiplying A by its inverse yields the identity up to rounding error. A singular matrix raises LinAlgError.

Note: To solve Ax = b, prefer np.linalg.solve over inv; it is faster and more accurate.

Example: np.linalg.inv()

python
import numpy as np

A = np.array([[4.0, 7.0], [2.0, 6.0]])
Ainv = np.linalg.inv(A)
print(Ainv.round(2))
print(np.allclose(A @ Ainv, np.eye(2)))

# Output:
# [[ 0.6 -0.7]
#  [-0.2  0.4]]
# True
Related Topics
Common Mistakes
  1. Inverting a singular matrix
  2. Testing equality with identity without allclose
  3. Using inv when solve is better
Chapter Summary
  • inv needs a square non-singular matrix
  • A @ inv(A) is the identity
  • Use allclose for checks
  • Prefer solve for linear systems
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.