Linear systems
线性方程组
Do more equations imply a unique solution? Use elimination to distinguish inconsistent, unique, and infinite solution sets, then examine residuals and sensitivity.
linear-systemsTwo constraints may repeat one fact
An invented mixing problem has two ingredient quantities, , constrained by and . Adding the equations gives , hence .
But constraints and provide only one independent fact: the second repeats the first. Changing the second right-hand side to 7 makes them contradictory. Equation count alone cannot determine the solution type; independence and consistency matter.
One matrix expression, three outcomes
Collect coefficients in , unknowns in , and right-hand sides in :
A solution exists exactly when lies in the column space of . If is one solution, subtracting it from any other solution produces a vector in the kernel. Thus:
The equation above applies when a solution exists. This is an affine set, generally missing zero unless . Rank of the augmented matrix classifies the cases: different coefficient and augmented ranks imply inconsistency; equal ranks of imply uniqueness; equal ranks below leave free variables and infinitely many real solutions. MML, Sections 2.3 and 2.6
Elimination uses reversible operations: exchanging equations, multiplying an equation by a nonzero scalar, and adding a multiple of another equation. Each preserves the solution set. Pivots eliminate entries in their columns until an echelon or reduced-echelon form remains. A row with all-zero coefficients and a nonzero right-hand side exposes a contradiction.
For an invertible square , one may write . This mathematical expression does not recommend computing the entire inverse first. Factorization and substitution can solve directly for the required vector. Other matrix shapes require distinguishing exact solutions from least-squares approximations. Deep Learning, Sections 2.3–2.4
Return the solution type and free directions
This complete exact-fraction program accepts small nonempty rectangular matrices. Its null_basis describes kernel directions; all solutions are the particular solution plus arbitrary linear combinations of those directions.
from fractions import Fraction as F
def solve(a, b):
if not a or not a[0] or len(a) != len(b):
raise ValueError("invalid system dimensions")
n = len(a[0])
if any(len(row) != n for row in a):
raise ValueError("ragged matrix")
rows = [[F(v) for v in row] + [F(rhs)] for row, rhs in zip(a, b)]
pivots = []
for col in range(n):
r = len(pivots)
p = next((i for i in range(r, len(rows)) if rows[i][col]), None)
if p is None:
continue
rows[r], rows[p] = rows[p], rows[r]
scale = rows[r][col]
rows[r] = [v / scale for v in rows[r]]
for i in range(len(rows)):
if i != r:
scale = rows[i][col]
rows[i] = [v - scale * w for v, w in zip(rows[i], rows[r])]
pivots.append(col)
if any(all(v == 0 for v in row[:n]) and row[n] != 0 for row in rows):
return {"status": "inconsistent"}
particular = [F(0)] * n
for r, col in enumerate(pivots):
particular[col] = rows[r][n]
basis = []
for free in sorted(set(range(n)) - set(pivots)):
direction = [F(0)] * n
direction[free] = F(1)
for r, col in enumerate(pivots):
direction[col] = -rows[r][free]
basis.append(direction)
return {"status": "unique" if not basis else "infinite",
"particular": particular, "null_basis": basis}
assert solve([[2, 1], [1, -1]], [7, 2])["particular"] == [3, 1]
many = solve([[1, 2], [2, 4]], [3, 6])
assert many == {"status": "infinite", "particular": [3, 0], "null_basis": [[-2, 1]]}
assert solve([[1, 2], [2, 4]], [3, 7])["status"] == "inconsistent"
assert solve([[1, 0], [0, 1], [1, 1]], [2, 3, 5])["status"] == "unique"
Elimination takes scalar operations. Explicitly constructing the kernel basis adds space and work, where is rank. Fraction bit growth is a separate cost. These costs suit small teaching problems; large applications should not request every free direction unconditionally.
A small residual can hide a large solution error
The residual measures substitution error, not necessarily distance to the true solution. Let and , with . The unique solution is . Candidate has residual length only , but solution error 1. Small makes the second direction sensitive.
Floating-point solvers must consider pivoting, scaling, and conditioning. With noisy data, exact inconsistency may simply mean that a model cannot match every observation. Least squares or regularization can help, but the changed objective must be stated.
An equation solution may also violate business constraints. Negative ingredient quantities are not an executable recipe when quantities must be nonnegative. Those restrictions need explicit modeling. Linear equations and linear programming are neighboring but distinct problems.
Classify before calculating
- Describe every solution of . Answer: for any real .
- Are three equations in two unknowns necessarily inconsistent? Answer: no. The final code example has unique solution .
- Can multiplying an equation by zero preserve equivalence? Answer: not as a general row operation. It deletes a constraint and can enlarge the solution set.
- Is a least-squares result for an inconsistent system an exact solution of the original equations? Answer: no. Report its residual and the objective actually optimized.
Where machine learning uses solvers
Least-squares regression, Newton steps, control, and graph algorithms repeatedly solve linear systems. Sparse factorizations and iterative methods exploit structure, while conditioning describes error amplification. Keep existence, uniqueness, stability, and application feasibility separate when studying these methods.
Make a prediction, then check
Can x+2y=3 and 2x+4y=7 both hold?
Learning objectives
- Use elimination to distinguish unique, infinitely many, and inconsistent solutions.
- Express solutions as a particular solution plus the kernel, and explain why a small residual may hide a large solution error.
Prerequisites & connections
Explore related knowledge →References
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020 - Deep Learning — Chapter 2: Linear Algebra
Ian Goodfellow; Yoshua Bengio; Aaron Courville · 2016
Version1 · Editorial status: Recorded technical checks completed
View technical check record (AI)
Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.
- Checked unique, infinite, and inconsistent systems and particular-plus-kernel solution sets; ran 80 additional consistent rational systems, substituting each particular solution and all kernel directions, plus zero and inconsistent cases.
- Added 0<ε≪1 to the sensitivity example and recalculated residual ε versus solution error 1; checked elimination, explicit-kernel output costs, and fraction bit-growth limits.
- Compared arguments, numbers, examples, limitations, and exercise answers across languages; structural validation separately compares display formulas, code, sources, and sections. No independent human peer review.
Source locations checked:
- Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft) · §2.3 and §2.6: elimination, solution sets, and rank
- Deep Learning — Chapter 2: Linear Algebra · §2.3–2.4: inverse, independence, and solution existence
Related topics