Skip to content
LearnPY
NumPyfunction reference

np.linalg.solve

Solve a square linear system a @ x = b.

Signature

np.linalg.solve(a, b)

What it does

np.linalg.solve computes the vector or matrix x that satisfies a @ x = b. It is more stable and usually faster than multiplying by an explicit inverse.

Parameters

andarray
The square coefficient matrix.
bndarray
The right-hand side vector or matrix.

Returns

ndarray

Example

pythonnp.linalg.solve example
import numpy as np

a = np.array([[2.0, 1.0], [1.0, 3.0]])
b = np.array([5.0, 10.0])
x = np.linalg.solve(a, b)
print(x)

Watch out for

  • The coefficient matrix must be square and non-singular for a unique solution.
  • Check the result with a @ x; floating-point answers may differ from b by tiny amounts.