---
title: "Gaussian elimination: pivoting, cost and accuracy"
heading: The workhorse of linear algebra, and the one swap that keeps it honest
eyebrow: "A13 · Lesson 23 · Gaussian elimination: pivoting, cost and accuracy"
xp: 40
---

::: hook
A7 factored matrices into LU and used the factors to solve systems. This lesson asks the numerical questions. How long does it take for a 10 000-node circuit? Why does every library swap rows as it goes, and what goes wrong if it doesn't? And when the computed solution satisfies the equations to 16 digits, why might it still be wrong in the fifth? The answers are an operation count of $\frac23n^3$, a tiny pivot that magnifies rounding by $10^{20}$, and the condition number from lesson 3.
:::

::: play
- The system is $\varepsilon x_1 + x_2 = 1$, $x_1 + x_2 = 2$, whose solution is $x_1 = \frac{1}{1 - \varepsilon} \approx 1$. Both curves are computed in double precision in your browser.
- Eliminating with ε as the pivot, without swapping rows, multiplies by $1/\varepsilon$. As ε shrinks, rounding errors are magnified by that much: wrong in the fifth digit near $10^{-12}$, and $x_1 = 0$ below $10^{-16}$.
- Swapping the rows first (partial pivoting) makes the multiplier ε instead, and the answer stays right for every ε.
:::
:::

```sim plot
view: graph
x: e = 1e-18..1 log
y: x1 = -0.2..2
plot: (1 - (2 - 1/e)/(1 - 1/e))/e | samples: 1500 | label: no pivoting
plot: 2 - (1 - 2*e)/(1 - e) | dashed | color: ch3 | label: with partial pivoting
```

::: predict
## Solving a dense 1000 × 1000 system by LU takes about 0.07 s on a laptop. About how long does a dense 10 000 × 10 000 system take?

- [ ] 0.7 s
- [ ] 7 s
- [x] 70 s
- [ ] 7000 s

::: reveal
About 70 s: the work grows as $n^3$, so 10 times the size is 1000 times the time. It also needs 800 MB just to store the matrix. Real circuits are sparse, which changes everything (lesson 49).
:::
:::

::: nameit
## Cost

| Step | Operations |
|---|---|
| LU factorization, $n \times n$ | $\frac23n^3$ |
| forward and back substitution, per right-hand side | $2n^2$ |
| tridiagonal (banded) factorization and solve | about 8n |
| inverse (don't form it) | $2n^3$ |

Factor once and reuse: k right-hand sides cost $\frac23n^3 + 2kn^2$. Solving with an explicit inverse costs more and is less accurate.

## Pivoting

Elimination divides by the pivot, the diagonal entry of the current column. A small pivot makes large multipliers, which magnify rounding errors in everything they touch. **Partial pivoting** swaps rows so the pivot is the largest entry (in size) in its column below the diagonal; every multiplier is then at most 1. With it, LU is backward stable in practice: the computed solution solves $(A + \delta A)\hat x = b$ with $\lVert\delta A\rVert$ a modest multiple of $u\lVert A\rVert$.

## Residual and error

The **residual** $r = b - A\hat x$ is computable; the error $x - \hat x$ isn't. A backward-stable solver always makes the residual tiny, but

$$\frac{\lVert x - \hat x\rVert}{\lVert x\rVert} \le \kappa(A)\frac{\lVert r\rVert}{\lVert A\rVert\lVert\hat x\rVert}$$

so with $\kappa = 10^{11}$ a residual at rounding level still allows an error in the fifth digit. **Iterative refinement** (solve $A\delta = r$ with the same LU and add δ, computing r in higher precision) recovers digits when κu < 1.

## Scaling

Equations with wildly different units (volts, microamps and gigaohms in one matrix) make pivoting choose badly. Scale rows and columns to comparable sizes first; circuit simulators do this.
:::

::: example
## Solve $10^{-20}x_1 + x_2 = 1$, $x_1 + x_2 = 2$ with and without pivoting, in double.

::: solution
1. **Without pivoting: eliminate.** The multiplier is $10^{20}$, so the second row becomes
   $$(1 - 10^{20})x_2 = 2 - 10^{20}$$
2. **Rounding.** Both sides round to $-10^{20}$, so $x_2 = 1$.
3. **Back substitute.**
   $$\begin{aligned} x_1 &= (1 - 1)/10^{-20} \\ &= 0 \end{aligned}$$
   Completely wrong.
4. **With the rows swapped.** The multiplier is $10^{-20}$, and the second row becomes
   $$(1 - 10^{-20})x_2 = 1 - 2 \times 10^{-20}$$
   so $x_2 = 1$.
5. **Back substitute.**
   $$\begin{aligned} x_1 &= 2 - 1 \\ &= 1 \end{aligned}$$
   Right to full precision.

**Answer:** without pivoting the computed solution is $x_1 = 0$, $x_2 = 1$, completely wrong; with pivoting it is $x_1 = x_2 = 1$, right to full precision.
:::
:::

::: example
## A transient simulation of a 2000-node circuit takes 10 000 time steps with the same matrix. Compare refactoring every step with factoring once.

::: solution
1. **One factorization.**
   $$\frac23(2000)^3 = 5.3 \times 10^9$$
   operations.
2. **One solve.**
   $$2(2000)^2 = 8 \times 10^6$$
3. **Factor once and solve 10 000 times.**
   $$\begin{aligned} &5.3 \times 10^9 + 8 \times 10^{10} \\ &= 8.5 \times 10^{10} \end{aligned}$$
4. **Refactor every step.** That costs $5.3 \times 10^{13}$, over 600 times more.
5. **Nonlinear circuits.** They change the matrix each Newton step, which is why their simulators work so hard on sparsity.

**Answer:** factoring once costs about $8.5 \times 10^{10}$ operations, against $5.3 \times 10^{13}$ for refactoring every step, over 600 times more.
:::
:::

::: example
## Factor $A = \begin{pmatrix}2 & 1 & 1\\ 4 & 3 & 3\\ 8 & 7 & 9\end{pmatrix}$ as LU, and find its determinant.

::: solution
1. **Eliminate the first column.** Subtract 2 × row 1 from row 2 and 4 × row 1 from row 3: the rows become (0, 1, 1) and (0, 3, 5).
2. **Eliminate the second column.** Subtract 3 × the new row 2 from row 3: (0, 0, 2).
3. **The factors.** U is what's left, and L holds the multipliers:
   $$U = \begin{pmatrix}2 & 1 & 1\\ 0 & 1 & 1\\ 0 & 0 & 2\end{pmatrix}$$
   $$L = \begin{pmatrix}1 & 0 & 0\\ 2 & 1 & 0\\ 4 & 3 & 1\end{pmatrix}$$
4. **Determinant.** It is the product of the pivots:
   $$2 \times 1 \times 2 = 4$$
5. **Reuse.** With the factors stored, each new right-hand side costs two triangular solves, about $2n^2$ operations instead of $\frac23n^3$.

**Answer:** $L = \begin{pmatrix}1 & 0 & 0\\ 2 & 1 & 0\\ 4 & 3 & 1\end{pmatrix}$, $U = \begin{pmatrix}2 & 1 & 1\\ 0 & 1 & 1\\ 0 & 0 & 2\end{pmatrix}$, and the determinant is 4.
:::
:::

::: check
given: n = 100 | 2000 | 50000
answer: 2*n^3/3
tolerance: 1%

## About how many arithmetic operations does LU factorization of a dense {n} × {n} matrix take?

::: right
Right. $\frac23n^3$ = {answer}.
:::

::: misses
- [n^3] The leading count is $\frac23n^3$.
- [2*n^2] That's one forward and back substitution.
- [any] Not yet. $\frac23n^3$.
:::
:::

::: check
given: n = 1000 | 3000 | 10000; g = 10 | 100
answer: 2*n^3/3/(g*1e9)
unit: s
tolerance: 1%

## A computer runs dense linear algebra at {g} GFLOP/s. About how long does an LU factorization of a {n} × {n} matrix take?

::: right
Right. {answer} s.
:::

::: misses
- [n^3/(g*1e9)] Use $\frac23n^3$ operations.
- [any] Not yet. $\frac23n^3$ divided by the rate.
:::
:::

::: check
given: n = 500 | 4000; k = 10 | 100 | 10000
answer: 2*n^3/3 + 2*k*n^2
tolerance: 1%

## You factor a {n} × {n} matrix once and solve for {k} right-hand sides. About how many operations in all?

::: right
Right. $\frac23n^3 + 2kn^2$ = {answer}.
:::

::: misses
- [k*(2*n^3/3 + 2*n^2)] Factor once and reuse the factors.
- [any] Not yet. One factorization plus k substitutions.
:::
:::

::: check
given: n = 1000 | 5000 | 10000 | 50000 | 100000 | 1000000
answer: 8*n^2*1e-6
unit: MB
tolerance: 1%

## How much memory does a dense {n} × {n} matrix of doubles need?

::: right
Right. $8n^2$ bytes = {answer} MB.
:::

::: misses
- [4*n^2*1e-6] A double is 8 bytes.
- [any] Not yet. 8 bytes per entry.
:::
:::

::: check
given: k = 6 | 10 | 13
answer: 15.95 - k
tolerance: 1%

## A stable solver leaves a residual at rounding level, and $\kappa(A) = 10^{\given{k}}$. About how many digits of the solution can you trust?

::: right
Right. About {answer}.
:::

::: misses
- [15.95] A small residual doesn't mean a small error when κ is large.
- [any] Not yet. Lose $\log_{10}\kappa$ digits.
:::
:::

::: check
kind: choice

## In partial pivoting, which row becomes the pivot row for a column?

- [ ] The first row with a nonzero entry
- [x] The row with the largest entry, in size, on or below the diagonal in that column
- [ ] The row with the smallest entry
- [ ] The last row

::: misses
- [1] A tiny nonzero pivot is exactly the danger.
- [3] Small pivots make large multipliers.
- [4] Position doesn't matter; size does.
:::
:::

::: check
kind: parts
given: a = 2 | 4; b = 1 | 3; c = 6 | 1; d = 5 | 8

## Factor $A = \begin{pmatrix}\given{a} & \given{b}\\ \given{c} & \given{d}\end{pmatrix} = LU$ without pivoting.

::: part
answer: c/a
tolerance: 0.5%

### What is the multiplier $l_{21}$?

::: misses
- [a/c] It's the entry to eliminate over the pivot.
- [any] $l_{21} = a_{21}/a_{11}$.
:::
:::

::: part
answer: d - b*c/a
tolerance: 0.5%

### What is $u_{22}$?

::: misses
- [d - b*a/c] Subtract $l_{21}$ times the first row's second entry.
- [any] $u_{22} = d - l_{21}b$.
:::
:::
:::

::: check
kind: derivation
vars: a = 1..4, b = -2..2, c = -2..2, d = 3..6, e = -3..3, f = -3..3

## Solve $ax_1 + bx_2 = e$, $cx_1 + dx_2 = f$ by elimination.

::: step
answer: f - c*e/a

### After subtracting $\frac ca$ times the first equation from the second, what is the second's right side?
:::

::: step
answer: (f - c*e/a)/(d - b*c/a)

### What is $x_2$?
:::

::: right
Right. That's $\frac{af - ce}{ad - bc}$, Cramer's rule, reached with one multiplier.
:::
:::

::: explain
## Why does a tiny pivot ruin Gaussian elimination even when the matrix is well-conditioned?

::: model
Dividing by a tiny pivot produces a huge multiplier, and subtracting that multiple of the pivot row from another row adds huge numbers to its entries. The original entries of the other row, which carry the real information, are then rounded relative to those huge values and are lost, as when $1 - 10^{20}$ rounds to $-10^{20}$. The solution computed from what remains is the exact answer to a very different problem. The matrix itself may be perfectly well-conditioned, so the failure belongs to the algorithm. Swapping rows so the pivot is the largest available entry keeps every multiplier at most 1 and prevents the growth.
:::
:::

::: practice

::: check
given: n = 100 | 300 | 1000 | 5000 | 20000 | 100000
answer: 2*n^3/3
tolerance: 1%

## LU operations for n = {n}?

::: misses
- [any] $\frac23n^3$.
:::
:::

::: check
given: n = 2000 | 8000; g = 20 | 200
answer: 2*n^3/3/(g*1e9)
unit: s
tolerance: 1%

## LU time, n = {n}, {g} GFLOP/s?

::: misses
- [any] $\frac23n^3/\text{rate}$.
:::
:::

::: check
given: n = 1000 | 5000; k = 10 | 1000
answer: 2*n^3/3 + 2*k*n^2
tolerance: 1%

## Operations for one LU and {k} solves, n = {n}?

::: misses
- [any] $\frac23n^3 + 2kn^2$.
:::
:::
:::
