General linear systems
Compute unknown coefficients from a square system of linear equations.
SciPy · Function
Solves the square linear system a @ x = b. Known matrix structures can be supplied to choose a dedicated solver, and leading batch dimensions are supported.
Provide a square matrix a and right-hand side b to compute x satisfying a @ x = b. Optional flags select a matrix structure, permit input overwrites, skip finite-value checks, or solve the transposed system instead.
scipy.linalg.solve(a, b, lower=False, overwrite_a=False, overwrite_b=False, check_finite=True, assume_a=None, transposed=False)| Name | Type | Description |
|---|---|---|
a | array_like, shape (..., N, N) | Square left-hand-side matrix or a batch of matrices. |
b | array_like, shape (..., N) or (..., N, NRHS) | Right-hand-side data or a batch of right-hand sides. |
lower | bool, default=False | For symmetric, Hermitian, or positive-definite assumptions, indicates whether the relevant triangle is lower. |
overwrite_a | bool, default=False | Allow overwriting a; may improve performance. |
overwrite_b | bool, default=False | Allow overwriting b; may improve performance. |
check_finite | bool, default=True | Check that input matrices contain only finite numbers. |
assume_a | str, optional | Matrix structure used to choose a dedicated solver; supported values include diagonal, tridiagonal, banded, triangular, symmetric, Hermitian, positive definite, and general. |
transposed | bool, default=False | If true, solve a.T @ x = b. |
x: ndarray, shape (N, NRHS) or (..., N) — the solution array.
import numpy as np
from scipy.linalg import solve
a = np.array([[3.0, 1.0], [1.0, 2.0]])
b = np.array([9.0, 8.0])
x = solve(a, b, assume_a="sym")
print(x) # [2. 3.]Solves the symmetric two-by-two system and prints its solution.
Compute unknown coefficients from a square system of linear equations.
Exploit a matrix known to be symmetric, banded, triangular, or positive definite.
Solve several compatible systems stored with leading dimensions.
solve computes the solution x of the matrix equation a @ x = b, with a required to be square. b may represent one right-hand side or a batch of right-hand sides.
Follow-up: How can a known matrix structure affect the computation? Pass assume_a with a supported structure such as diagonal, banded, triangular, symmetric, Hermitian, or positive definite so a dedicated solver can be selected.
assume_a tells SciPy which structure the data matrix has, allowing a specialized solver to be used. Supported categories include diagonal, tridiagonal, banded, triangular, symmetric, Hermitian, positive definite, and general matrices.
Follow-up: What is the risk of declaring a structure incorrectly? The selected specialized method may not be valid for the actual matrix, so the assumption must match the input.
The function accepts arrays with batch dimensions before the core matrix dimensions. The result has shape (N, NRHS) for multiple right-hand sides or a corresponding batched vector shape.
Follow-up: How would you solve the transposed system? Set transposed=True, which makes the equation a.T @ x = b.
scipy.linalg.solve_triangularscipy.linalg.solve_bandedscipy.linalg.lu_factorscipy.linalg.lu_solve