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

NumPy · Function

np.linalg.solve

Solves a square, full-rank linear system a x = b and returns x. It also supports multiple right-hand sides and broadcastable leading dimensions.

How it works

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.

Syntax

linalg.solve(a, b)

Parameters

NameTypeDescription
a(..., M, M) array_likeCoefficient matrix with shape (..., M, M).
b(M,), (..., M, K) array_likeRight-hand-side values with shape (M,) or (..., M, K).

Returns

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.

Key points

  • Use solve for a linear matrix equation or a system of scalar linear equations.
  • It targets the exact solution of a well-determined full-rank system.
  • The b argument supplies the dependent-variable values or right-hand side.
  • The returned array contains x satisfying a @ x = b.
  • Leading dimensions of a and b are broadcast when solving batched systems.

Common mistakes

  • The coefficient matrix must be square and nonsingular; otherwise the solve is not applicable.
  • Use numpy.linalg.lstsq when the matrix is not square or lacks full rank.

Example: Solve a 2×2 linear system

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.

When to use it

Solve a single linear system

Compute unknown coefficients from a square set of linear equations.

Handle multiple right-hand sides

Solve several right-hand sides using one coefficient matrix.

Process batched systems

Apply the same solve operation across compatible leading dimensions.

Interview questions

QWhat problem does numpy.linalg.solve solve?

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.

QWhat input shapes does solve accept?

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).

QWhat does the function return?

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.lstsq
  • numpy.linalg.inv
  • numpy.linalg.tensorinv
  • numpy.linalg.pinv

Source: numpy.linalg.solve. Checked 2026-10-15.