np.linalg.solve()
solve finds the unknown values in a system of linear equations.
In this page:
Syntax
x = np.linalg.solve(A, b)
np.linalg.solve()
np.linalg.solve(A, b) returns x such that A x = b for a square, non-singular A. It is more stable and faster than computing the inverse yourself. Each row of A is one equation and each column one unknown.
Note:
Check your answer with np.allclose(A @ x, b).
Example: np.linalg.solve()
import numpy as np
# 2x + y = 5 and x + 3y = 10
A = np.array([[2, 1], [1, 3]])
b = np.array([5, 10])
x = np.linalg.solve(A, b)
print(x)
print(np.allclose(A @ x, b))
# Output:
# [1. 3.]
# True
Related Topics
Common Mistakes
- Using inv(A) @ b instead of solve
- Passing a singular matrix
- Getting the equation layout wrong
Chapter Summary
- solve(A, b) solves A x = b
- A must be square and non-singular
- More stable than inverting
- Verify with A @ x
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: