Linear Algebra

scipy.linalg solves the kind of problems you'd otherwise do by hand with substitution and elimination — systems of linear equations, determinants, and matrix inverses — on matrices of any size.

Solving a system of equations

A system like 3x + y = 9 and x + 2y = 8 can be written as a matrix equation Ax = b, and linalg.solve finds the x and y that satisfy both at once:

>>> solving 3x + y = 9, x + 2y = 8
import numpy as np
from scipy import linalg

A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])

x = linalg.solve(A, b)
print(x)
Output
[2. 3.]

Each row of A is one equation's coefficients, and b is the right-hand side of each equation. The answer, x = 2, y = 3, checks out: 3(2) + 3 = 9 and 2 + 2(3) = 8.

Determinant and inverse

>>> determinant and inverse of the same matrix
import numpy as np
from scipy import linalg

A = np.array([[3, 1], [1, 2]])

print(linalg.det(A))
print(linalg.inv(A))
Output
5.0
[[ 0.4 -0.2]
 [-0.2  0.6]]

linalg.det returns a single number — here, 5.0, computed as (3×2) - (1×1) for a 2×2 matrix. linalg.inv returns the matrix that, multiplied by the original, gives the identity matrix — useful conceptually, but rarely the right tool for actually solving equations, as the next note explains.

Note: it's tempting to solve Ax = b by computing x = inv(A) @ b — mathematically that's correct, but linalg.solve(A, b) is both faster and more numerically stable, especially for larger matrices. Computing a full inverse does unnecessary extra work and can amplify floating-point rounding errors that a direct solve avoids. Reach for inv() when you actually need the inverse matrix itself, not as a step toward solving a system.