Mathematical Foundations

VIII.7 · Numerical Computing

Can a tiny residual hide a wrong solution?

Solve a bounded 2×2 system with partial pivoting and compare explicit inversion. Use independent exact rational solutions to separate residual, forward error, conditioning and rank.

Solve, form an inverse and measure separate errors

Intended exact system: unique. Represented binary64 system: unique. Partial pivoting produced a finite candidate. Numerical residual uses represented inputs; exact residual and forward error refer to the intended system. A small residual is not a forward-error certificate.

Intended system classification
unique
Represented system classification
unique
Exact intended determinant
5
Exact infinity condition number
16/5
Exact intended reference solution
(1, 1)
Exact represented-system reference
(1, 1)
Computed second pivot
1.66666666666666674
Relative pivot warning tolerance
0.00000000000001
Computed stored-system residual
0
Exact residual to intended system
0
Absolute forward error
0
Relative forward error (nonzero reference)
0

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

r=Ax^−b,x^−x∗=A−1r(det⁡A≠0)r=A\widehat x-b,\quad\widehat x-x_*=A^{-1}r\quad(\det A\ne0)
κ∞(A)=∥A∥∞∥A−1∥∞\kappa_\infty(A)=\|A\|_\infty\|A^{-1}\|_\infty
∣p2∣≤τ∥A∥∞is a diagnostic, not an exact rank rule|p_2|\le\tau\|A\|_\infty\quad\text{is a diagnostic, not an exact rank rule}
Fixed shape 2×2. Inputs are row-major; indices here are zero-based.
rowintended coefficient 0intended coefficient 1intended rhs
0314
1123
Actual computed methods and independently measured errors.
methodx[0]x[1]numerical residualexact intended residualforward error
pivot solve≈ 1≈ 1≈ 000
explicit inverse≈ 1≈ 0.99999999999999978≈ 0.000000000000000441/22517998136852481/4503599627370496

Choose the first pivot

The first row already has the larger first-column pivot.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
swappedfalse
a00≈ 3
a01≈ 1
b0≈ 4
a10≈ 1
a11≈ 2
b1≈ 3

A[0,0], A[0,1], A[1,0], A[1,1]; finite decimals within ±10.

b[0], b[1]; finite decimals within ±10.

Finite decimal in [0,0.01]. Zero disables the nonzero-pivot warning; exact rank is unchanged.

Edit a bounded 2\u00d72 system; compare partial pivoting and explicit inversion against an exact rational solution and independent Python references.

Intuition: satisfying equations is a different measurement

A residual asks how well a candidate solution satisfies the equations. Forward error asks how far that candidate is from the specified solution. In a sensitive system, a tiny equation discrepancy can correspond to a much larger solution change. Read both quantities before deciding whether a numerical answer is useful.

The explorer keeps intended exact-decimal matrix entries and right-hand sides, their binary64 representations, an exact reference solution, and both computed methods. It can therefore reveal input-conversion error as well as evaluation error. A zero binary64 residual is not a certificate that the intended exact system was solved accurately.

Definition: residual, forward error and conditioning

For intended A and b and computed vector x^\widehat x, the exact residual to that intended problem is

r=Ax^−b.r=A\widehat x-b.

If the intended matrix is nonsingular, let x∗=A−1bx_*=A^{-1}b denote its exact solution. Forward error is ∥x^−x∗∥∞\|\widehat x-x_*\|_\infty, and relative forward error divides by ∥x∗∥∞\|x_*\|_\infty when that scale is nonzero. The condition number displayed here is the exact rational infinity-norm quantity

κ∞(A)=∥A∥∞∥A−1∥∞.\kappa_\infty(A)=\|A\|_\infty\|A^{-1}\|_\infty.

An infinity matrix norm is the largest absolute row sum. The solution reference is computed independently from the browser’s numerical elimination using the accepted exact 2×2 matrix API and Python Fraction row reduction. Singular systems receive a rank/consistency classification rather than a fabricated unique reference.

Mechanics: solve first, compare inverse second

The numerical solver selects the larger-magnitude first-column pivot, swaps the entire augmented rows if needed, eliminates below it and back-substitutes. Every multiplier, second pivot and transformed right-hand side is retained. The inverse comparison explicitly forms a numerical determinant and four inverse entries, then multiplies by b.

The inspector distinguishes the stored-system numerical residual from an exactly evaluated rational residual using the intended A and b. Forward error likewise refers to the intended exact solution. The exact solution of the represented input system is also retained, making representation changes visible.

The relative pivot tolerance is a diagnostic warning, not a rank definition. A small nonzero pivot may trigger a near-singular warning while the selected computation proceeds. A zero numerical pivot produces no unique numerical solve. Exact rank remains determined by the rational problem, independently of a tolerance.

Derivation: a small residual is amplified by an inverse

For nonsingular A and an exactly evaluated residual,

A(x^−x∗)=r,x^−x∗=A−1r.A(\widehat x-x_*)=r,\qquad\widehat x-x_*=A^{-1}r.

Thus

∥x^−x∗∥∞≤∥A−1∥∞∥r∥∞.\|\widehat x-x_*\|_\infty\le\|A^{-1}\|_\infty\|r\|_\infty.

A large inverse norm can amplify a tiny residual. Appropriate relative forms involve the condition number and scales of A, b and the reference solution. This is a mathematical sensitivity statement; whether the computed residual itself is reliable is a separate numerical question.

Elimination avoids explicitly forming every inverse entry when only one right-hand side is needed. It often reduces avoidable work and error, but a tiny system does not prove that inversion always produces a worse result. The explorer reports the actual outcome instead of enforcing that slogan.

Worked examples: ordinary, ill-conditioned and singular

For

A=[3112],b=[43],A=\begin{bmatrix}3&1\\1&2\end{bmatrix},\qquad b=\begin{bmatrix}4\\3\end{bmatrix},

the exact solution is (1,1). Its determinant is 5 and infinity condition number is 16/5. Partial pivoting records a first pivot 3, multiplier 1/3 and second pivot near 5/3. Back substitution and inverse multiplication are both compared with the exact solution.

For the ill-conditioned preset, intended rows are [1,1] and [1,1.000000000001], with b=[2,2.0000000000010001]. The exact solution is (0.9999,1.0001). Binary64 rounds the slightly changed right-hand side to the same value as a different intended system and the computed solution is (1,1). Its stored-system numerical residual is zero; its intended-system exact residual norm is 1e-16, while its absolute forward error is 1e-4. The condition number is approximately 4e12, so the sensitivity is consistent with that discrepancy.

The singular preset has rows [1,2] and [2,4], with b=[3,6]. These are the same constraint and admit infinitely many solutions. The inconsistent preset changes the second right-hand side to 7 and admits no solution. Neither gets an inverse or a unique numerical answer.

Worked example: Solve, form an inverse and measure separate errors

Intended exact system: unique. Represented binary64 system: unique. Partial pivoting produced a finite candidate. Numerical residual uses represented inputs; exact residual and forward error refer to the intended system. A small residual is not a forward-error certificate.

Intended system classification
unique
Represented system classification
unique
Exact intended determinant
5
Exact infinity condition number
16/5
Exact intended reference solution
(1, 1)
Exact represented-system reference
(1, 1)
Computed second pivot
1.66666666666666674
Relative pivot warning tolerance
0.00000000000001
Computed stored-system residual
0
Exact residual to intended system
0
Absolute forward error
0
Relative forward error (nonzero reference)
0

= exact rationals or classifications · ≈ a formatted numerical display. Exceptional values retain their stated meaning.

r=Ax^−b,x^−x∗=A−1r(det⁡A≠0)r=A\widehat x-b,\quad\widehat x-x_*=A^{-1}r\quad(\det A\ne0)
κ∞(A)=∥A∥∞∥A−1∥∞\kappa_\infty(A)=\|A\|_\infty\|A^{-1}\|_\infty
∣p2∣≤τ∥A∥∞is a diagnostic, not an exact rank rule|p_2|\le\tau\|A\|_\infty\quad\text{is a diagnostic, not an exact rank rule}
Fixed shape 2×2. Inputs are row-major; indices here are zero-based.
rowintended coefficient 0intended coefficient 1intended rhs
0314
1123
Actual computed methods and independently measured errors.
methodx[0]x[1]numerical residualexact intended residualforward error
pivot solve≈ 1≈ 1≈ 000
explicit inverse≈ 1≈ 0.99999999999999978≈ 0.000000000000000441/22517998136852481/4503599627370496

Choose the first pivot

The first row already has the larger first-column pivot.

Inspect the recorded values\text{Inspect the recorded values}
Actual intermediate values; = exact rationals, ≈ binary64 displays.
quantityvalue
swappedfalse
a00≈ 3
a01≈ 1
b0≈ 4
a10≈ 1
a11≈ 2
b1≈ 3

Select the recorded intermediate values. Previous, next and direct replay navigation preserve immutable numerical history.

Executable reference code

The pedagogical reference independently solves the intended system with Fraction row reduction, then performs numerical partial pivoting. NumPy uses actual solve and inverse operations and reports residuals and forward differences against the exact intended-decimal reference. A NumPy condition estimate concerns its stored array, which can differ from the intended rational matrix. Literal checks include a required pivot swap and both singular classifications.

code/python/numerical_linear_algebra.py
"""Fraction row reduction supplies a reference; floating partial pivoting computes the candidate."""
from fractions import Fraction as F
def exact_solve(values,rhs):
    rows=[[F(values[2*i]),F(values[2*i+1]),F(rhs[i])] for i in range(2)]
    for col in range(2):
        pivot=next((i for i in range(col,2) if rows[i][col]),None)
        if pivot is None:return None
        rows[col],rows[pivot]=rows[pivot],rows[col]
        scale=rows[col][col];rows[col]=[v/scale for v in rows[col]]
        for i in range(2):
            if i!=col:
                factor=rows[i][col];rows[i]=[a-factor*b for a,b in zip(rows[i],rows[col])]
    return [row[2] for row in rows]
def solve(values,rhs):
    a=list(map(float,values));b=list(map(float,rhs));rows=[[a[0],a[1],b[0]],[a[2],a[3],b[1]]]
    if abs(rows[1][0])>abs(rows[0][0]):rows.reverse()
    if rows[0][0]==0:return None
    factor=rows[1][0]/rows[0][0];pivot=rows[1][1]-factor*rows[0][1];r=rows[1][2]-factor*rows[0][2]
    if pivot==0:return None
    y=r/pivot;x=(rows[0][2]-rows[0][1]*y)/rows[0][0]
    return [x,y]
if __name__=='__main__':
    values=['1','1','1','1.000000000001'];rhs=['2','2.0000000000010001']
    reference=exact_solve(values,rhs);answer=solve(values,rhs)
    assert reference==[F(9999,10000),F(10001,10000)] and answer==[1,1]
    residual=[sum(F(values[2*i+j])*F.from_float(answer[j]) for j in range(2))-F(rhs[i]) for i in range(2)]
    assert max(map(abs,residual))==F(1,10**16)
    assert max(abs(F.from_float(v)-r) for v,r in zip(answer,reference))==F(1,10000)
    assert solve(['0','1','1','1'],['1','2'])==[1,1]
    assert exact_solve(['1','2','2','4'],['3','6']) is None
    print('reference',reference,'computed',answer,'exact intended residual',residual)
code/python/numerical_linear_algebra_numpy.py
import numpy as np
from fractions import Fraction as F
from numerical_linear_algebra import exact_solve
if __name__=='__main__':
    text=['1','1','1','1.000000000001'];rhs=['2','2.0000000000010001']
    a=np.array(text,dtype=np.float64).reshape(2,2);b=np.array(rhs,dtype=np.float64)
    answer=np.linalg.solve(a,b);inverse_answer=np.linalg.inv(a)@b;reference=exact_solve(text,rhs)
    residual=np.linalg.norm(a@answer-b,ord=np.inf)
    forward=max(abs(F.from_float(float(v))-r) for v,r in zip(answer,reference))
    assert residual==0 and forward==F(1,10000)
    well=np.array([[3.,1.],[1.,2.]]);well_b=np.array([4.,3.])
    np.testing.assert_allclose(np.linalg.solve(well,well_b),[1.,1.],rtol=0,atol=4*np.finfo(float).eps)
    assert np.linalg.matrix_rank(np.array([[1.,2.],[2.,4.]]))==1
    print('solve',answer,'explicit inverse',inverse_answer,'stored residual',residual,'intended forward error',forward,'stored-array condition',np.linalg.cond(a,np.inf))

Explore: change the sensitivity or diagnostic

Compare the well-conditioned and ill-conditioned presets. Predict which measurement stays small and explain its scale. Select a required-pivot example with a zero first diagonal coefficient; the row swap supplies a valid pivot rather than dividing by zero.

Edit the four row-major matrix entries and the two right-hand sides. The dimensions remain bounded at 2×2, entries lie within ±10, and invalid shape or non-finite input is rejected. Change the relative pivot diagnostic tolerance without changing the intended matrix. The exact rank and reference must remain unchanged even when a warning changes.

Choose the rounded-singular preset: an exact intended difference of 1e-16 in a coefficient can disappear during binary64 conversion. The intended system and represented system then have different rank. The model preserves both classifications.

Common mistakes and counterexamples

A small numerical residual need not imply a small exact residual, and even a small exact residual can coexist with a substantial forward error when the matrix is ill-conditioned. A finite determinant alone does not establish a well-conditioned problem. A tolerance should express a scale and purpose, not redefine real equality.

Explicit inversion is not a general shortcut to numerical reliability. Pivoting reduces a selected elimination failure but does not recover input digits already discarded, cure all ill-conditioning or make every matrix invertible. This bounded example is not a replacement for a tested numerical linear algebra library.

Numerical considerations

The accepted exact rational matrix model classifies the intended problem and supplies a 2×2 solution when unique. Actual binary64 operations own the computed methods. Residual evaluation against the intended problem reads computed solution components back as exact represented rationals, so it does not lose a small discrepancy in a second floating subtraction.

The pivot warning compares the second pivot magnitude with a chosen fraction of the matrix infinity norm. Zero disables the nonzero-pivot warning but never disables rank or finite-value guards. Non-finite computed solutions remain unavailable. For a near-singular system, measure the actual solution error; a looser tolerance does not make the problem less sensitive.

Engineering and ML connections

Regression, least squares, covariance analysis and optimization all produce linear systems. Choose a solver and decomposition appropriate to the matrix structure, inspect conditioning and residual scales, and retain a known or independently solved reference where feasible. Large libraries add richer rank and error estimates; the same distinction between problem sensitivity and algorithm behavior remains essential.

Inspect the corresponding representation and error policy before interpreting a computed value.

Exercises

Predict first, then calculate or test the stated claim. Every answer includes corrective feedback and a worked solution.

calculation

Hand solution

Solve rows [3,1], [1,2] with rhs [4,3]. Give x then y.

calculation

Residual is a different error

Computed (1,1), exact solution (0.9999,1.0001). Give infinity-norm forward error.

calculation

Infinity condition number

For A=[[3,1],[1,2]], the inverse is [[2/5,−1/5],[−1/5,3/5]]. Give its infinity condition number.

explanation

A warning tolerance

Does changing the relative pivot warning tolerance change exact rational rank?

Choose one answer

explanation

A small residual

When can a small residual coexist with a large forward error?

Choose one answer

Summary and checkpoints

Distinguish intended and represented systems, numerical and exact residuals, and residual versus forward error. Reproduce the 1e-16 residual/1e-4 forward-error counterexample, interpret the pivot swap and warning tolerance, and identify infinite-solution, inconsistent and rounded-singular cases without inventing inverses.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available