A13 · Lesson 23 · Gaussian elimination: pivoting, cost and accuracy
The workhorse of linear algebra, and the one swap that keeps it honest
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 , a tiny pivot that magnifies rounding by , and the condition number from lesson 3.
- The system is , , whose solution is . Both curves are computed in double precision in your browser.
- Eliminating with ε as the pivot, without swapping rows, multiplies by . As ε shrinks, rounding errors are magnified by that much: wrong in the fifth digit near , and below .
- Swapping the rows first (partial pivoting) makes the multiplier ε instead, and the answer stays right for every ε.
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?
About 70 s: the work grows as , 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).
Name it
Cost
| Step | Operations |
|---|---|
| LU factorization, | |
| forward and back substitution, per right-hand side | |
| tridiagonal (banded) factorization and solve | about 8n |
| inverse (don't form it) |
Factor once and reuse: k right-hand sides cost . 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 with a modest multiple of .
Residual and error
The residual is computable; the error isn't. A backward-stable solver always makes the residual tiny, but
so with a residual at rounding level still allows an error in the fifth digit. Iterative refinement (solve 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.
Worked example
Solve , with and without pivoting, in double.
Show the solution
- Without pivoting: eliminate. The multiplier is , so the second row becomes
- Rounding. Both sides round to , so .
- Back substitute. Completely wrong.
- With the rows swapped. The multiplier is , and the second row becomes so .
- Back substitute. Right to full precision.
Answer without pivoting the computed solution is , , completely wrong; with pivoting it is , right to full precision.
Worked 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.
Show the solution
- One factorization. operations.
- One solve.
- Factor once and solve 10 000 times.
- Refactor every step. That costs , over 600 times more.
- Nonlinear circuits. They change the matrix each Newton step, which is why their simulators work so hard on sparsity.
Answer factoring once costs about operations, against for refactoring every step, over 600 times more.
Worked example
Factor as LU, and find its determinant.
Show the solution
- 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).
- Eliminate the second column. Subtract 3 × the new row 2 from row 3: (0, 0, 2).
- The factors. U is what's left, and L holds the multipliers:
- Determinant. It is the product of the pivots:
- Reuse. With the factors stored, each new right-hand side costs two triangular solves, about operations instead of .
Answer , , and the determinant is 4.
Check
About how many arithmetic operations does LU factorization of a dense 2000 × 2000 matrix take?
Check
A computer runs dense linear algebra at 10 GFLOP/s. About how long does an LU factorization of a 3000 × 3000 matrix take?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
You factor a 500 × 500 matrix once and solve for 10 right-hand sides. About how many operations in all?
Check
How much memory does a dense 10000 × 10000 matrix of doubles need?
Check
A stable solver leaves a residual at rounding level, and . About how many digits of the solution can you trust?
Check
In partial pivoting, which row becomes the pivot row for a column?
Check
Factor without pivoting.
(a)
What is the multiplier ?
(b)
What is ?
Check
Solve , by elimination.
Step 1
After subtracting times the first equation from the second, what is the second's right side?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is ?
Explain why
Why does a tiny pivot ruin Gaussian elimination even when the matrix is well-conditioned?
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 rounds to . 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
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.