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

np.linalg.solve()

solve finds the unknown values in a system of linear equations.

In this page:

  1. np.linalg.solve()
Syntax
python
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()

python
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
  1. Using inv(A) @ b instead of solve
  2. Passing a singular matrix
  3. 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:

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.