np.linalg.inv()
The inverse of a matrix undoes it: multiplying a matrix by its inverse gives the identity matrix.
In this page:
Syntax
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()
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
- Inverting a singular matrix
- Testing equality with identity without allclose
- 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: