Fundamentals / Numerical mathematics / Numerical linear algebra

Elimination, pivoting and factorisation

Build an LU factorisation by hand, reuse it for a second load and identify when an innocent-looking elimination step loses useful digits.

From first principles

Make one coefficient vanish without changing the solution

Consider a pair of linear balances \(ax+by=f\) and \(cx+dy=g\). Every solution of both also satisfies their linear combinations. Conversely, adding a multiple of one equation to another is reversible by subtracting the same multiple. For \(a\ne0\), choose the multiplier \(m=c/a\) so that subtracting \(m\) times the first equation from the second removes \(x\),

\[(d-mb)y=g-mf,\]
\[x=(f-by)/a .\]

The same two operations can be stored as a matrix factorization. The lower triangle records the multiplier and the upper triangle records the remaining coefficients,

\[\begin{pmatrix}a&b\\c&d\end{pmatrix}=\begin{pmatrix}1&0\\m&1\end{pmatrix}\begin{pmatrix}a&b\\0&d-mb\end{pmatrix}.\]

The second pivot \(d-mb\) must also be nonzero for a unique solution. Further right-hand sides can reuse both factors because neither depends on \(f\) or \(g\). When the first pivot is zero one can try exchanging rows. Exchanging rows to obtain a larger pivot can also reduce rounding errors in the multiplier.

Derivation and applications

Eliminate a variable from a symmetric quadratic energy

For a symmetric stiffness matrix the elimination pivot has a mechanical meaning. Consider the quadratic energy of two displacement coordinates \(u,v\), with stiffnesses \(a,d\), coupling \(c\), and applied loads \(f,g\). Stationarity with respect to each displacement gives the two linear balance equations. Assume the first stiffness \(a>0\),

\[E(u,v)=\tfrac12 a u^2+cuv+\tfrac12 d v^2-fu-gv,\]
\[\partial_u E=au+cv-f .\]

At a prescribed \(v\) the energy is minimized at \(u=(f-cv)/a\). Complete the square in \(u\) before solving the remaining balance. The reduced energy retains both the coupling-induced stiffness change and the force transmitted from the eliminated coordinate,

\[E=\frac a2\left(u-\frac{f-cv}{a}\right)^2+\frac12\left(d-\frac{c^2}{a}\right)v^2-\left(g-\frac{cf}{a}\right)v-\frac{f^2}{2a}.\]

The second elimination pivot is the curvature \(s=d-c^2/a\). For a stable equilibrium with a unique response one needs \(s>0\). The same result follows by expanding the symmetric factorization

\[A=\begin{pmatrix}1&0\\c/a&1\end{pmatrix}\begin{pmatrix}a&0\\0&s\end{pmatrix}\begin{pmatrix}1&c/a\\0&1\end{pmatrix}.\]

Small \(s\) means one combined motion changes the energy very little. A large resulting displacement can therefore be a property of the model even if the elimination is performed accurately.

Worked example

Compute the effective load after eliminating a support

Let \(a=4\), \(c=-2\), \(d=3\) in \(\mathrm{N/m}\), with loads \(f=2\) and \(g=1\,\mathrm N\). The retained stiffness is

\[3-4/4=2\,\mathrm{N/m}.\]

Its effective load is \(1-(-2)(2)/4=2\,\mathrm N\), so \(v=1\,\mathrm m\) and \(u=(2+2)/4=1\,\mathrm m\). Substitution gives original loads \(4-2=2\) and \(-2+3=1\).

Second application

Diagnose loss of stability without computing eigenvalues

With \(a=9\), \(d=4\), and \(c=6\) the second pivot vanishes. The trial motion \(u=-2v/3\) has zero quadratic energy for every \(v\). Changing only \(c\) to \(6.3\) gives

\[s=4-6.3^2/9=-0.41.\]

Minimizing over \(u\) at \(v=2\) and zero load now yields \(E=s v^2/2=-0.82\), demonstrating an unstable direction in the supposed stiffness model.

OneSimulation laboratory

Separate algebraic error from mesh error

Start from this chapter. Use the direct factorisation as a fixed-mesh reference. Compare it with a converged iterative solve, then check why the peak temperature itself moves on a different mesh.

Compare recorded OneSimulation runs on this page. To test your own inputs, download the Python model and run the model in an activated OneSimulation environment.

A unit cube has conductivity 1 W/(m·K), uniform heating 1 W/m³ and zero temperature rise on every face. The script compares CG and a direct solver on the same trilinear finite-element mesh. Predict the effects of first refining the mesh and then relaxing only the CG tolerance. Download the Python experiment · OneSimulation linear solvers.

python linear-algebra.py
python linear-algebra.py --cells 10
python linear-algebra.py --cells 10 --rtol 0.05
Solver comparison on the heated cube. The difference uses all nodes.
Cells per axis, CG toleranceDirect peak (K)CG peak (K)Max. difference (K)
5, 10⁻¹⁰0.054830740.054830742.8 × 10⁻¹⁷
10, 10⁻¹⁰0.057089000.057089003.6 × 10⁻¹³
10, 0.050.057089000.056207828.8 × 10⁻⁴
On the ten-cell mesh, relaxing the CG tolerance increases the peak solver-to-solver difference to about 0.00088 K. The logarithmic axis resolves the many orders of magnitude.
On the ten-cell mesh, relaxing the CG tolerance increases the peak solver-to-solver difference to about 0.00088 K. The logarithmic axis resolves the many orders of magnitude.

The refined mesh includes the cube centre as a node, whereas the five-cell mesh samples the peak off-centre. The change between direct nodal maxima therefore includes a sampling effect. The final two rows use identical nodes, isolating the effect of the CG stopping test. Differences near roundoff can vary between implementations.

Change parameters · Change the laboratory inputs

Change one input at a time and compare the recalculated curves with the argument above. The browser evaluates this small model locally; the accompanying numerical values state its assumptions and reference checks. Reset restores the reference inputs, and the data can be downloaded as CSV.

Problem sheet 3 · Bound the effect of stopping a linear solve early

Read the model setup and preparatory problems

How large can the last column grow?

A test matrix can be easy to describe and still produce surprisingly large intermediate values during elimination. Build an \(n\)-by-\(n\) matrix with ones on the diagonal and in its last column, minus ones strictly below the diagonal outside that column, and zeros everywhere else. Every input coefficient has magnitude at most one. Choose ordinary partial pivoting with the first candidate retained on ties. Every active diagonal pivot in the first \(n-1\) columns equals one and the entries below it equal minus one. There is no tiny pivot and the elimination multipliers all equal minus one.

Follow the last column instead. In the first elimination stage every lower row gains the pivot row. Its last entry changes from one to two. At the next stage every remaining row gains a pivot row whose last entry now equals two. The surviving last entries become four. An induction gives a final pivot of magnitude \(2^{n-1}\): the growth factor is exponential even though each multiplier is bounded. For six rows the last column of the upper triangular factor reads \(1,2,4,8,16,32\).

The largest intermediate matrix entry doubles at each elimination stage. A second graph compares growth with the considerably smaller condition number of the original matrix.Open full-size diagram
The first curve follows the implementation's arithmetic; the second panel also tests the sensitivity of the original input-output problem. The two quantities describe different possible sources of error.

The plotted condition number is the ratio of the largest and smallest singular values in Euclidean norm. It quantifies the possible relative amplification of perturbations in the original system. The growth factor instead anticipates how large the numbers in intermediate elimination stages can become compared with the input. Standard backward-error estimates contain the absolute sizes of those factors because roundoff is incurred in the intermediate arithmetic. A large growth factor weakens such a guarantee. It does not by itself establish a large solution error for every right-hand side.

In fact all the operations exposed above are small integer additions, sign changes and powers of two. They are exactly representable at the modest dimensions used by this notebook. A demonstration that simply solves this one exact test and prints a small residual would therefore miss the point of the construction. Reverse the row order and repeat the measurement. The singular values of the matrix remain the same, because a row permutation preserves Euclidean norm. Intermediate growth can change dramatically through a different sequence of tied pivot choices.

For a five-row matrix, a solver reports largest intermediate coefficient 16. Another researcher claims this proves that rounding must have lost four binary digits in every solution. Which part of the claim follows?

Work through the solution

The factor 16 is correct for the stated tie convention: each of four elimination stages doubles the surviving last entry. Four binary bits describe the ratio of scales. No automatic loss of four actual solution bits follows. Test a perturbed matrix and a specific right-hand side, and report forward error against a reliable reference together with a scaled residual. The exact integer example may incur no rounding in elimination at all.

Eliminate an unknown without changing the solution

Gaussian elimination replaces one equation by itself minus a multiple of another. In exact arithmetic this is reversible: add the same multiple back. In the first column choose a nonzero pivot \(a_{11}\) and form multipliers \(l_{i1}=a_{i1}/a_{11}\) for the rows below it. Subtract \(l_{i1}\) times row one from row \(i\). All lower entries in column one vanish, and the remaining unknowns satisfy a smaller system. Applying the same operations in the same order to the load vector is essential.

Repeating the argument produces an upper triangular matrix \(U\). The inverse row additions can be collected into a unit lower triangular matrix \(L\) containing the multipliers. If rows have been permuted, the relation is \(PA=LU\), where \(P\) records those exchanges. To solve for a new right-hand side, first solve \(Ly=Pb\) by forward substitution, then \(Ux=y\) by backward substitution. The factorisation depends on the matrix alone. This is valuable in static analysis when the material and supports stay fixed and only the applied loads change.

Factor once and load twice

Consider \(A=\left(\begin{smallmatrix}2&1&0\\1&3&1\\0&1&2\end{smallmatrix}\right)\). Subtract half the first row from the second to get \((0,2.5,1)\). Subtract 0.4 times the new second row from the third to get \((0,0,1.6)\). Thus \(U\) has diagonal \(2,2.5,1.6\) with ones immediately above, and \(L\) has multipliers 0.5 and 0.4 immediately below its unit diagonal. This matrix needs no pivot exchange. Multiplying the two factors recovers the original entries and gives a quick arithmetic check.

For the load \(b=(1,2,3)^T\), forward substitution yields

\[y=(1,1.5,2.4)^T.\]

Back substitution starts with \(x_3=2.4/1.6=1.5\), then gives

\[x_2=(1.5-1.5)/2.5=0,\]
\[x_1=(1-0)/2=0.5.\]

A second load \(b=(0,1,0)^T\) gives

\[y=(0,1,-0.4)^T,\]
\[x=(-0.25,0.5,-0.25)^T.\]

Both calculations are short once the triangular factors are available.

Eliminate a chain in two linear-time sweeps

In a chain of nearest-neighbour balances, row \(i\) contains only a left coefficient \(a_i\), diagonal \(d_i\), and right coefficient \(c_i\). Subtracting a multiple of the preceding, already reduced row removes the left coefficient and updates just one diagonal and one load. Write the reduced pivot and load as \(p_i,g_i\). The first pair is \(p_1=d_1,\ g_1=b_1\). Thereafter the multiplier, pivot and load satisfy

\[m_i=a_i/p_{i-1},\]
\[p_i=d_i-m_ic_{i-1},\]
\[g_i=b_i-m_ig_{i-1}.\]

The last equation gives

\[x_n=g_n/p_n.\]

Proceed backwards with \(x_i=(g_i-c_ix_{i+1})/p_i\). This is the Thomas algorithm. Each row costs a fixed number of operations and only three coefficient diagonals need storage. On repeated load cases the pivots and multipliers depend only on the original coefficient matrix and can be retained. Symmetric positive definiteness guarantees positive pivots here. An arbitrary nonsingular tridiagonal matrix can still have a zero leading pivot and require pivoting.

A four-node diffusion chain

With conductance one between successive nodes and fixed end values 1 and 2, the four internal balances have diagonal two, neighbours minus one and load \((1,0,0,2)^T\). The reduced pivots are \(2,3/2,4/3,5/4\). The corresponding loads are \(1,1/2,1/3,9/4\). The last unknown is

\[(9/4)/(5/4)=9/5.\]

One step back gives \((1/3+9/5)/(4/3)=8/5\), then \(7/5\) and \(6/5\). Each of the five temperature differences is one fifth, as conservation without an interior source requires. A dense factorisation would return the same field but would obscure why a million-node chain is computationally tractable.

Practise on sheet 1 · Reuse chain factors after a load change. Further reading · University of Limerick · MS6021 Scientific Computation, W. T. Lee · Tridiagonal Matrices: Thomas Algorithm, sections I–II, Stage 1 and Stage 2.

Why exchange rows?

Consider \(10^{-5}x+y=1\) and \(x+y=2\). The exact solution is \(x=1/(1-10^{-5})\), \(y=2-x\). Eliminating with the first coefficient gives multiplier \(10^5\). The reduced equation contains the difference between numbers of size \(10^5\). In arithmetic rounded to three significant digits, both the new coefficient and right-hand side round to \(-1.00\times10^5\). Back substitution gives \(y=1\), then \(x=0\). Substitution in the second original equation exposes a residual of one.

Swap the rows first. The multiplier is now \(10^{-5}\), and the reduced equation rounds to \(y=1.00\). Back substitution in the first pivot equation gives \(x=1.00\), close to the exact answer. Partial pivoting chooses the largest absolute available coefficient in the pivot column, bounding the magnitudes of the multipliers by one. It usually limits numerical growth, though it cannot remove sensitivity already present in the original matrix.

For a dense matrix of size \(n\), the triangular factorisation takes order \(n^3\) arithmetic operations and substitutions take order \(n^2\) per load. Sparse discretisations can be much cheaper, but eliminating an unknown may create new nonzero entries between its neighbours. This fill depends on ordering. Ordering for small fill and pivoting for accuracy answer different questions, and production solvers may need both.

Use symmetry when the physics supplies it

For a symmetric positive definite stiffness matrix, Cholesky factorisation represents \(A=CC^T\) with a lower triangular \(C\) and positive real diagonal. The first diagonal entry is \(\sqrt{a_{11}}\). Entries below it are fixed by the first column of the product, and subtracting that column’s outer product leaves a smaller positive definite Schur complement. Positivity follows by minimising the full quadratic energy over the eliminated variable for every fixed nonzero remaining vector.

For \(A=\left(\begin{smallmatrix}4&2\\2&5\end{smallmatrix}\right)\), the first column of \(C\) is \((2,1)^T\). The remaining diagonal is

\[\sqrt{5-1^2}=2.\]

A zero or negative candidate under the square root signals that the positive definite assumption or numerical precision needs inspection. Unrestrained mechanical assemblies can have rigid modes of zero energy. Supplying appropriate physical constraints addresses that singularity directly instead of treating it as a pivot-ordering problem.

Trace element growth during pivoted elimination

Partial pivoting chooses the largest available entry in magnitude within the active column. Each multiplier then has magnitude at most one, an important control on an individual elimination step. It does not bound the size of all updated entries by their original size. The trailing update subtracts a multiplier times the pivot row from another row, and two terms of similar magnitude may add. Repeated reinforcement can build large intermediate entries even when each column choice satisfies the pivot rule.

Measure this effect by the growth factor, the largest magnitude encountered during elimination divided by the largest initial matrix entry. Roundoff in a factorisation is tied to the magnitude of the computed factors, so a large growth factor can weaken a backward-error guarantee. The factor describes a potential loss mechanism. A particular solve can still be exact when every operation happens to be exactly representable, and a large forward error can arise for quite different reasons if the underlying problem is ill-conditioned. These distinctions matter when interpreting numerical experiments designed to stress an algorithm.

Construct a matrix where the final column doubles

Use the four rows \((1,0,0,1)\), \((-1,1,0,1)\), \((-1,-1,1,1)\), and \((-1,-1,-1,1)\). Every initial entry has magnitude at most one. Choose the first available row on tied pivots. Eliminating the first column adds row one to all three lower rows, so their final entries become 2. Eliminating column two adds the second active row to the next two rows, changing their final entries to 4. Eliminating column three adds the third pivot row to the last, producing the final pivot 8. No multiplier exceeded one. The triangular factor has last column \((1,2,4,8)^T\) and the observed growth factor is eight.

The construction extends directly to size \(n\), with an all-ones last column, unit diagonal before that column, and minus ones below the diagonal. After \(j\) stages each remaining row has last entry \(2^j\), as follows inductively by adding two equal entries during the next elimination. The resulting growth factor is \(2^{n-1}\) for this tie rule. Thus even a rule that prevents division by a small pivot relative to its column admits exponential worst-case growth. Selecting a different row on a tie can change the numerical path without changing the physical system.

For a concrete back-substitution test select exact state \({\bf1}\). Multiplication gives load \((2,1,0,-2)^T\). Applying the same row operations transforms it to \((2,3,5,8)^T\). Back substitution gives \(x_4=1\), \(x_3=5-4=1\), \(x_2=3-2=1\) and \(x_1=2-1=1\). Binary arithmetic represents these small integers exactly. A residual check therefore need not reveal anything unusual in this case even while the monitored growth factor exposes an unfavourable factorisation path.

The comparison with no growth is especially clear for a diagonal matrix with entries \((1,1,1,8)\). Its last pivot is also eight, but its largest original entry was already eight and its growth factor is one. A large absolute pivot alone cannot identify element growth; the ratio must refer to the size of the input.

How large can intermediate elimination entries become?

Apply partial pivoting, keeping the first candidate in a tie. The test matrix has ones on the diagonal and final column, minus ones below the diagonal, and zero elsewhere. Record the largest absolute intermediate entry divided by the largest original entry.

How large can intermediate elimination entries become?. Horizontal axis: Matrix dimension. Vertical axis: Element growth factor. Data follow the figure.
Elimination doubles successive last-column entries in the first family. At dimension twelve the growth factor is 2048, versus one for the Poisson example. The graph measures element growth; it does not claim the same factor for the observed solution error.
Read values from the plot
Element growth factor at selected values of Matrix dimension.
Matrix dimensionWilkinson example1D Poisson tridiagonal
221
5161
81281
1220481
15163841

Download all 14 plotted samples (CSV)

Change parameters · How large can intermediate elimination entries become?

Change one input at a time and compare the recalculated curves with the argument above. The browser evaluates this small model locally; the accompanying numerical values state its assumptions and reference checks. Reset restores the reference inputs, and the data can be downloaded as CSV.