TExcelTAD
Home › Math for Machine Learning › Systems of Linear Equations

Systems of Linear Equations

Fitting a linear model, finding equilibrium prices or blending ingredients all lead to the same problem: find x\mathbf{x} with Ax=bA\mathbf{x} = \mathbf{b}. A system can have exactly one solution, none, or infinitely many, and ML runs into all three cases. This lesson shows how to tell them apart and how to solve each one.

Three possible outcomes

For A∈Rm×nA \in \mathbb{R}^{m \times n}, write the augmented matrix [A∣b][A \mid \mathbf{b}] and row reduce it (Gaussian Elimination). Then:

Case Sign in the row-echelon form Typical in ML
unique solution a pivot in every column of AA, no contradiction square, well-posed problems
no solution a row [0⋯0∣c][0 \cdots 0 \mid c] with c≠0c \ne 0 more data points than parameters (m>nm > n), noisy data
infinitely many a column of AA without a pivot (a free variable) more parameters than data points (n>mn > m)

When there are infinitely many solutions, the general solution is one particular solution plus anything in the null space:

x=xp+λ1n1+⋯+λknk,Ani=0\mathbf{x} = \mathbf{x}_p + \lambda_1 \mathbf{n}_1 + \cdots + \lambda_k \mathbf{n}_k, \qquad A\mathbf{n}_i = \mathbf{0}
python · try it yourself
import numpy as np

A = np.array([[1.0, 2.0, 1.0],
              [2.0, 4.0, 3.0]])          # 2 equations, 3 unknowns
b = np.array([4.0, 9.0])
x_p = np.linalg.lstsq(A, b, rcond=None)[0]   # one particular solution (the shortest one)
n = np.array([-2.0, 1.0, 0.0])               # A n = 0: a null-space direction
print("rank:", np.linalg.matrix_rank(A))
print("A x_p =", np.round(A @ x_p, 10), " A n =", A @ n)
for lam in (0.0, 1.0, -3.0):
    x = x_p + lam * n
    print(f"lambda={lam:4}: x = {np.round(x, 4)}, A x = {np.round(A @ x, 10)}")
rank: 2
A x_p = [4. 9.]  A n = [0. 0.]
lambda= 0.0: x = [0.6 1.2 1. ], A x = [4. 9.]
lambda= 1.0: x = [-1.4  2.2  1. ], A x = [4. 9.]
lambda=-3.0: x = [ 6.6 -1.8  1. ], A x = [4. 9.]

Every choice of λ\lambda solves the system. In ML this is the over-parameterized case: many weight vectors fit the training data perfectly, and something extra (regularization, or the implicit bias of gradient descent) has to choose one.

Solving square systems in code

For a square invertible AA, the solution is x=A−1b\mathbf{x} = A^{-1}\mathbf{b}, but code should call np.linalg.solve(A, b). It uses an LU factorization (Gaussian elimination stored as two triangular matrices), which needs about a third of the work of forming A−1A^{-1} and loses less accuracy to rounding.

python · try it yourself
import numpy as np

A = np.array([[4.0, 1.0, 0.0],
              [1.0, 3.0, 1.0],
              [0.0, 1.0, 2.0]])
b = np.array([1.0, 2.0, 3.0])
x1 = np.linalg.solve(A, b)
x2 = np.linalg.inv(A) @ b
print("solve:", np.round(x1, 6), " inv:", np.round(x2, 6))
print("residual norm:", np.linalg.norm(A @ x1 - b) < 1e-12)
solve: [0.222222 0.111111 1.444444]  inv: [0.222222 0.111111 1.444444]
residual norm: True

No solution: the least squares compromise

With more equations than unknowns, noisy data usually makes the system inconsistent. Instead of an exact solution, we look for the x\mathbf{x} that makes ∥Ax−b∥\lVert A\mathbf{x} - \mathbf{b} \rVert smallest. It solves the normal equations ATAx=ATbA^T A\mathbf{x} = A^T\mathbf{b}, and the Moore-Penrose pseudo-inverse A+=(ATA)−1ATA^+ = (A^T A)^{-1}A^T (for independent columns) packages that solution. This is exactly linear regression.

In software

  • np.linalg.solve, scipy.linalg.solve and torch.linalg.solve use LU factorization with pivoting.
  • np.linalg.lstsq and np.linalg.pinv handle the non-square and rank-deficient cases with the SVD.
  • Large sparse systems (finite elements, graph problems) use iterative solvers such as conjugate gradient instead.

Exercises

Exercise 1: Classify the system

How many solutions does x+y=2x + y = 2, 2x+2y=52x + 2y = 5 have?

Answer

None.

Solution

Subtract twice the first row from the second: [0 0∣1][0\ 0 \mid 1], which says 0=10 = 1. The two lines are parallel and never meet.

Exercise 2: General solution

Find all solutions of x1+2x2=4x_1 + 2x_2 = 4 (one equation, two unknowns).

Answer

x=(4,0)+λ(−2,1)\mathbf{x} = (4, 0) + \lambda(-2, 1) for any real λ\lambda.

Solution

x2x_2 is free. Setting x2=0x_2 = 0 gives the particular solution (4,0)(4, 0). The homogeneous equation x1+2x2=0x_1 + 2x_2 = 0 is solved by (−2,1)(-2, 1), so

x=[40]+λ[−21]\mathbf{x} = \begin{bmatrix} 4 \\ 0 \end{bmatrix} + \lambda \begin{bmatrix} -2 \\ 1 \end{bmatrix}

Exercise 3: Solve and check (code)

Solve 2x+y−z=12x + y - z = 1, x+3y+2z=13x + 3y + 2z = 13, x−y+z=2x - y + z = 2 with np.linalg.solve.

Answer

x=1x = 1, y=2y = 2, z=3z = 3.

Solution
python · try it yourself
import numpy as np
A = np.array([[2.0, 1, -1], [1, 3, 2], [1, -1, 1]])
b = np.array([1.0, 13, 2])
print(np.round(np.linalg.solve(A, b), 10))
[1. 2. 3.]

Check the last equation: 1−2+3=21 - 2 + 3 = 2.

Test yourself

A row [0 0 0∣5][0\ 0\ 0 \mid 5] in the reduced augmented matrix means…

  1. the system has infinitely many solutions
  2. the system has no solution
  3. x3=5x_3 = 5
  4. the matrix is invertible

A model has more parameters than training examples and fits them exactly. Typically…

  1. the fit is unique
  2. infinitely many parameter vectors fit equally well
  3. no parameter vector fits
  4. the data matrix is square

Why prefer np.linalg.solve(A, b) over np.linalg.inv(A) @ b?

  1. inv gives the wrong answer
  2. solve factorizes once and is faster and more accurate
  3. solve works without numbers
  4. they differ only in spelling