A system of linear equations in equations and unknowns has the form
and is written compactly as
Solving it means determining the set . That set is empty, a single point, or an affine subspace of positive dimension; there are no other cases.
The system used throughout this lesson is
Matrix form
Row of holds the coefficients of equation ; component of holds its constant term. Zero coefficients occupy a position: the third equation has no , hence .
import numpy as np
A = np.array([[1, 1, 1],
[1, -1, 2],
[0, 1, 1]])
b = np.array([3, 2, 2])
Solution with NumPy
When and is invertible, the solution is unique and is obtained with np.linalg.solve:
x = np.linalg.solve(A, b)
# array([1., 1., 1.])
The routine does not compute . It applies Gaussian elimination with partial pivoting, which yields the factorisation
with a permutation matrix, unit lower triangular and upper triangular. The system is then solved by two chained substitutions, and , each immediate by triangularity. The cost is operations. The implementation delegates to LAPACK.
The permutation matrix is not a dispensable technicality: without pivoting a zero pivot halts the process, and a pivot of small magnitude amplifies rounding error.
Gaussian elimination
The figure applies Gauss-Jordan elimination to the augmented matrix . Each step is an elementary row operation. The process terminates when the left block is the identity, at which point the right-hand column holds the solution.
Augmented matrix [A | b]
Step 1 / 9
The three elementary operations — swapping two rows, multiplying a row by a non-zero scalar, and adding a multiple of one row to another — are invertible, and therefore preserve the solution set.
Verification
np.allclose(A @ x, b)
# True
The @ operator denotes the matrix product. The comparison uses allclose rather than ==: in floating-point arithmetic the computed solution satisfies with a residual on the order of machine epsilon, and strict equality would return false.
Geometric interpretation
For , each equation with describes a line in , and the solution set of the system is their intersection.
Each equation describes a line. The sliders modify its coefficients.
1·x₁ + 1·x₂ = 1.25
1·x₁ − 2·x₂ = 0.5
Unique solution: the lines meet at one point.
det = -3 · x = (1, 0.25)
Two lines in the plane meet at a point, are parallel and disjoint, or coincide. Those three cases are exhaustive and correspond to a unique solution, an inconsistent system, and an underdetermined system. For each equation describes a plane, and the intersection of the three is a point, a line, a plane, or the empty set.
Existence and uniqueness
The rank of a matrix is the number of linearly independent rows, equivalently the number of linearly independent columns. The governing criterion is the Rouché-Capelli theorem:
A square matrix with is called singular and admits no inverse. np.linalg.solve requires square and non-singular; otherwise it raises an exception.
C = np.array([[1, 1, 1],
[1, -1, 2],
[2, 0, 3]]) # row 3 = row 1 + row 2
np.linalg.solve(C, np.array([3, 2, 1]))
# LinAlgError: Singular matrix
np.linalg.matrix_rank(C) # 2
Here . The constant term decides between the two remaining cases: with the relation holds, the rank of the augmented matrix remains 2 and the system has infinitely many solutions; with the augmented rank is 3 and the system is inconsistent.
Augmented matrix of an inconsistent system
Step 1 / 8
The final state exhibits a zero row in the block corresponding to with a non-zero constant term. That row encodes the equation with , which is how elimination exposes inconsistency.
For systems without an exact solution, np.linalg.lstsq returns the minimiser of ; when that minimiser is not unique, it returns the one of minimum norm.
x, residuals, rank, sv = np.linalg.lstsq(C, np.array([3, 2, 1]), rcond=None)
solve versus inv
For solving with invertible, both expressions are mathematically equivalent:
x = np.linalg.solve(A, b) # recommended
x = np.linalg.inv(A) @ b # discouraged
Numerically they are not. Computing requires solving systems rather than one, and the subsequent product introduces a second source of rounding error. The error bound for the second route is worse, and the gap widens with the condition number of . An explicit inverse is warranted only when the matrix is itself the object of interest, which in practice is uncommon.
Exercise. Replace the coefficient in C with and recompute the rank. Determine whether the resulting matrix is still singular, and which configuration of the three planes corresponds to each case.