Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

linalg.solve

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.

How it works

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.

Syntax

scipy.linalg.solve(a, b, lower=False, overwrite_a=False, overwrite_b=False, check_finite=True, assume_a=None, transposed=False)

Parameters

NameTypeDescription
aarray_like, shape (..., N, N)Square left-hand-side matrix or a batch of matrices.
barray_like, shape (..., N) or (..., N, NRHS)Right-hand-side data or a batch of right-hand sides.
lowerbool, default=FalseFor symmetric, Hermitian, or positive-definite assumptions, indicates whether the relevant triangle is lower.
overwrite_abool, default=FalseAllow overwriting a; may improve performance.
overwrite_bbool, default=FalseAllow overwriting b; may improve performance.
check_finitebool, default=TrueCheck that input matrices contain only finite numbers.
assume_astr, optionalMatrix structure used to choose a dedicated solver; supported values include diagonal, tridiagonal, banded, triangular, symmetric, Hermitian, positive definite, and general.
transposedbool, default=FalseIf true, solve a.T @ x = b.

Returns

x: ndarray, shape (N, NRHS) or (..., N) — the solution array.

Key points

  • The function returns x for a square linear system whose left-hand matrix is a.
  • Use assume_a to select a solver tailored to a known matrix structure.
  • Inputs can include leading batch dimensions, enabling several systems to be represented together.
  • check_finite controls validation that matrix inputs contain finite values.
  • overwrite_a permits mutation of the left-hand input and may improve performance.
  • overwrite_b permits mutation of the right-hand input and may improve performance.

Common mistakes

  • The lower flag only affects symmetric, Hermitian, and positive-definite assumptions; it has no effect for general systems.
  • Turning off check_finite can improve speed but risks crashes or non-termination when inputs contain NaNs or infinities.
  • transposed solves with the ordinary transpose, not the conjugate transpose, so it is unsuitable for complex Hermitian-transpose behavior.

Example: Solve a symmetric linear system

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.

When to use it

General linear systems

Compute unknown coefficients from a square system of linear equations.

Structure-aware solving

Exploit a matrix known to be symmetric, banded, triangular, or positive definite.

Batched systems

Solve several compatible systems stored with leading dimensions.

Interview questions

QWhat problem does scipy.linalg.solve solve?

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.

QWhy would you set assume_a?

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.

QWhat shapes can a and b have?

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_triangular
  • scipy.linalg.solve_banded
  • scipy.linalg.lu_factor
  • scipy.linalg.lu_solve

Source: Solve. Checked 2026-10-15.