Solve a single linear system
Compute unknown coefficients from a square set of linear equations.
NumPy · Function
Solves a square, full-rank linear system a x = b and returns x. It also supports multiple right-hand sides and broadcastable leading dimensions.
Pass a square coefficient matrix as a and one or more right-hand-side vectors as b. The function computes x for a @ x = b; with multiple right-hand sides, the final b dimensions represent separate columns, while leading dimensions can broadcast.
linalg.solve(a, b)| Name | Type | Description |
|---|---|---|
a | (..., M, M) array_like | Coefficient matrix with shape (..., M, M). |
b | (M,), (..., M, K) array_like | Right-hand-side values with shape (M,) or (..., M, K). |
x: ndarray with shape (..., M) when b has shape (M,), or (..., M, K) when b has shape (..., M, K); it is the solution to a x = b.
import numpy as np
a = np.array([[3.0, 1.0], [1.0, 2.0]])
b = np.array([9.0, 8.0])
x = np.linalg.solve(a, b)
print(x) # [2. 3.]Solves the square system a @ x = b and prints the solution.
Compute unknown coefficients from a square set of linear equations.
Solve several right-hand sides using one coefficient matrix.
Apply the same solve operation across compatible leading dimensions.
It computes the exact solution x to the well-determined linear system a x = b. The coefficient matrix a must be square and full-rank.
Follow-up: What should be used for a non-square or rank-deficient system? Use numpy.linalg.lstsq to obtain a least-squares solution.
a has shape (..., M, M), while b has shape (M,) or (..., M, K). The leading dimensions may broadcast between a and b.
Follow-up: How does the output shape depend on b? A vector right-hand side produces (..., M); a matrix right-hand side produces (..., M, K).
The result is an ndarray containing the solution to a @ x = b. Its shape is (..., M) for a vector b and (..., M, K) for a matrix b.
Follow-up: Why might multiple right-hand sides be useful? They let one factorization solve several systems sharing the same coefficient matrix.
numpy.linalg.lstsqnumpy.linalg.invnumpy.linalg.tensorinvnumpy.linalg.pinv