AI Wiki中文/EN

Linear systems

线性方程组

Do more equations imply a unique solution? Use elimination to distinguish inconsistent, unique, and infinite solution sets, then examine residuals and sensitivity.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

Two constraints may repeat one fact

An invented mixing problem has two ingredient quantities, x1,x2x_1,x_2, constrained by 2x1+x2=72x_1+x_2=7 and x1−x2=2x_1-x_2=2. Adding the equations gives 3x1=93x_1=9, hence x1=3,x2=1x_1=3,x_2=1.

But constraints x1+2x2=3x_1+2x_2=3 and 2x1+4x2=62x_1+4x_2=6 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 A∈Rm×nA\in\mathbb R^{m\times n}, unknowns in x∈Rnx\in\mathbb R^n, and right-hand sides in b∈Rmb\in\mathbb R^m:

Ax=b.Ax=b.

A solution exists exactly when bb lies in the column space of AA. If xpx_p is one solution, subtracting it from any other solution produces a vector in the kernel. Thus:

{x:Ax=b}=xp+ker⁡A(Axp=b).\{x:Ax=b\}=x_p+\ker A\qquad(Ax_p=b).

The equation above applies when a solution exists. This is an affine set, generally missing zero unless b=0b=0. Rank of the augmented matrix [A∣b][A\mid b] classifies the cases: different coefficient and augmented ranks imply inconsistency; equal ranks of nn imply uniqueness; equal ranks below nn 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 AA, one may write x=A−1bx=A^{-1}b. 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 O(mnmin⁡(m,n))O(mn\min(m,n)) scalar operations. Explicitly constructing the kernel basis adds O(n(n−r))O(n(n-r)) space and work, where rr 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 r=Ax^−br=A\hat x-b measures substitution error, not necessarily distance to the true solution. Let A=diag⁡(1,ϵ)A=\operatorname{diag}(1,\epsilon) and b=(1,ϵ)⊤b=(1,\epsilon)^\top, with 0<ϵ≪10<\epsilon\ll1. The unique solution is (1,1)⊤(1,1)^\top. Candidate (1,0)⊤(1,0)^\top has residual length only ∣ϵ∣|\epsilon|, but solution error 1. Small ϵ\epsilon 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

  1. Describe every solution of x1+2x2=3x_1+2x_2=3. Answer: (3,0)⊤+t(−2,1)⊤(3,0)^\top+t(-2,1)^\top for any real tt.
  2. Are three equations in two unknowns necessarily inconsistent? Answer: no. The final code example has unique solution (2,3)⊤(2,3)^\top.
  3. 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.
  4. 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?

Choose one answer

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

  1. Mathematics for Machine Learning — mathematical foundations (2024-01-15 draft)
    Marc Peter Deisenroth; A. Aldo Faisal; Cheng Soon Ong · 2020
  2. 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:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.