---
title: LU and Cholesky factorizations
heading: Factor once and solve many times, halve the work for symmetric matrices, and refine the answer
eyebrow: A13 · Lesson 24 · LU and Cholesky
xp: 40
---

::: hook
Gaussian elimination (lesson 23) is best thought of as a factorization, $PA = LU$, done once and reused. A circuit simulator stepping through 10 000 time steps with the same matrix, or a sensitivity analysis with a hundred right-hand sides, pays for the factorization once and then solves each new case cheaply. Matrices from resistor networks and from most physical problems are symmetric and positive definite, and for them the **Cholesky** factorization $A = LL^T$ does half the work with no pivoting. Banded matrices cost far less again. And when a factorization is done in low precision, a few steps of **iterative refinement** recover full accuracy.
:::

::: play
- The curves show the operation count against matrix size n for four ways of solving $Ax = b$: dense LU ($\frac23n^3$), Cholesky ($\frac13n^3$), banded LU with bandwidth b ($2nb^2$), and a tridiagonal solve ($8n$).
- Move b. A band of a few hundred costs far less than dense LU once n is large; a band of 1 is the tridiagonal case.
- The readouts give each method's time for n = 10 000 at 10 GFLOP/s.
:::

```sim plot
view: graph
x: n = 10..1000000 log
y: operations = 100..100000000000000000 log
slider: b = 1..1000 step 1 = 100 log
plot: 2/3*n^3 | label: dense LU
plot: 1/3*n^3 | label: Cholesky | color: ch2
plot: 2*n*b^2 | label: banded LU | color: ch3
plot: 8*n | label: tridiagonal | color: ch4
readout: dense LU seconds for ten thousand unknowns = 2/3*1e12/1e10 | digits: 3
readout: banded LU seconds for ten thousand unknowns = 2*1e4*b^2/1e10 | digits: 3
```

::: predict
## A 1000-node resistive circuit's nodal matrix is symmetric with positive pivots. Compared with LU, what does Cholesky save?

- [x] Half the arithmetic, and it needs no pivoting
- [ ] Nothing: the work is the same
- [ ] A factor of n
- [ ] It's faster but less accurate

::: reveal
Half the arithmetic, $\frac13n^3$ instead of $\frac23n^3$, and half the storage, since only L is kept. A symmetric positive definite matrix is also safe to factor without pivoting: the pivots stay positive and the entries of L stay bounded by those of A. If Cholesky meets a zero or negative pivot, the matrix wasn't positive definite, which makes the factorization a test as well.
:::
:::

::: nameit
## Factor once, solve many times

$PA = LU$ costs $\frac23n^3$. Each solve afterward is a forward substitution with L and a back substitution with U, about $2n^2$ in total. The determinant comes for free: $\det A = \pm\prod u_{ii}$, with the sign set by the number of row swaps. You rarely need $A^{-1}$ itself: forming it costs three times as much as factoring, and $x = A^{-1}b$ is less accurate than solving with the factors.

## Cholesky

A symmetric positive definite matrix (one with $x^TAx > 0$ for every nonzero x) factors as $A = LL^T$, with

$$l_{jj} = \sqrt{a_{jj} - \sum_{k<j}l_{jk}^2}, \qquad l_{ij} = \frac{a_{ij} - \sum_{k<j}l_{ik}l_{jk}}{l_{jj}} \quad (i > j).$$

It costs $\frac13n^3$ and needs no pivoting. Conductance matrices of resistive networks with a ground node, stiffness matrices from finite elements and covariance matrices are all of this kind.

## Banded and tridiagonal systems

If every nonzero is within b of the diagonal, elimination creates no fill outside the band, and LU costs about $2nb^2$. For tridiagonal matrices (b = 1) the **Thomas algorithm** sweeps down and back in about 8n operations: a spline through a million points costs a few milliseconds.

## Iterative refinement

With the factors in hand, compute the residual $r = b - A\hat x$ (in higher precision if you can), solve $Ad = r$ with the same factors, and update $\hat x \leftarrow \hat x + d$. Each step multiplies the error by about $\kappa(A)u$, where u is the factorization's unit roundoff. That lets a fast float32 factorization deliver float64 accuracy when $\kappa(A)u_{32} \ll 1$.
:::

::: example
## Factor $A = \begin{pmatrix}4 & 2\\ 2 & 3\end{pmatrix}$ by Cholesky.

::: solution
1. **The first column.**
   $$\begin{aligned} l_{11} &= \sqrt4 \\ &= 2 \\ l_{21} &= \frac22 \\ &= 1 \end{aligned}$$
2. **The corner.**
   $$\begin{aligned} l_{22} &= \sqrt{3 - 1^2} \\ &= \sqrt2 \end{aligned}$$
3. **The factor.**
   $$L = \begin{pmatrix}2 & 0\\ 1 & \sqrt2\end{pmatrix}$$
   Multiplying $LL^T$ gives back A.
4. **Determinant.**
   $$\begin{aligned} (l_{11}l_{22})^2 &= (2\sqrt2)^2 \\ &= 8 \end{aligned}$$
   which matches $4 \times 3 - 2 \times 2$.
5. **On the edge.** If the corner entry had been 1 instead of 3, $l_{22}$ would be $\sqrt{0}$: the matrix would be singular, sitting on the edge of positive definiteness.

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

::: example
## Solve $\begin{pmatrix}2 & -1 & 0\\ -1 & 2 & -1\\ 0 & -1 & 2\end{pmatrix}x = \begin{pmatrix}1\\ 0\\ 1\end{pmatrix}$ with the Thomas algorithm.

::: solution
1. **Sweep down: row 2.** Row 2 becomes row 2 + ½ row 1:
   $$\begin{aligned} \text{diagonal} &= 2 - \frac12 \\ &= 1.5 \\ \text{right side} &= 0 + \frac12 \\ &= 0.5 \end{aligned}$$
2. **Sweep down: row 3.** Row 3 becomes row 3 + $\frac{1}{1.5}$ row 2:
   $$\begin{aligned} \text{diagonal} &= 2 - \frac{1}{1.5} \\ &= \frac43 \\ \text{right side} &= 1 + \frac{0.5}{1.5} \\ &= \frac43 \end{aligned}$$
3. **Back substitute.**
   $$\begin{aligned} x_3 &= \frac{4/3}{4/3} \\ &= 1 \\ x_2 &= \frac{0.5 + x_3}{1.5} \\ &= 1 \\ x_1 &= \frac{1 + x_2}{2} \\ &= 1 \end{aligned}$$
4. **What it means.** This matrix is the discrete second derivative, and the answer is the voltages along a resistor ladder with both ends driven at 1.

**Answer:** $x = (1, 1, 1)$.
:::
:::

::: example
## A matrix with $\kappa = 10^4$ is factored in float32 ($u \approx 6 \times 10^{-8}$) and refined in float64. How does the error fall?

::: solution
1. **The first solve.** It is accurate to about
   $$\kappa u_{32} = 6 \times 10^{-4}$$
2. **Each refinement step.** It multiplies the error by about $6 \times 10^{-4}$ again: $4 \times 10^{-7}$, then $2 \times 10^{-10}$, then $10^{-13}$.
3. **The floor.** The error falls until it reaches float64's own limit, about $\kappa u_{64} = 10^{-12}$.
4. **The payoff.** Three cheap refinement steps, each costing $O(n^2)$, recover what a float64 factorization would give, while the expensive $O(n^3)$ factorization ran in float32, often two to four times faster on GPUs.

**Answer:** from about $6 \times 10^{-4}$ the error shrinks by about $6 \times 10^{-4}$ per step, reaching float64's limit of about $10^{-12}$ after three steps.
:::
:::

::: check
kind: parts
given: a = 4 | 9; b = 2 | 3; c = 5 | 10

## Factor $\begin{pmatrix}\given{a} & \given{b}\\ \given{b} & \given{c}\end{pmatrix} = LL^T$.

::: part
answer: b/sqrt(a)
tolerance: 0.5%

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

::: misses
- [b/a] Divide by $l_{11} = \sqrt a$, not by a.
- [any] $l_{21} = a_{21}/l_{11}$.
:::
:::

::: part
answer: sqrt(c - b^2/a)
tolerance: 0.5%

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

::: misses
- [sqrt(c)] Subtract $l_{21}^2$ first.
- [any] $\sqrt{c - l_{21}^2}$.
:::
:::
:::

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

## About how long does a Cholesky factorization of a dense {n} × {n} matrix take at {g} GFLOP/s?

::: right
Right. $\frac{n^3}{3}$ operations: half of LU's.
:::

::: misses
- [2*n^3/3/(g*1e9)] That's LU; Cholesky does half the work.
- [any] $\frac{n^3/3}{\text{rate}}$.
:::
:::

::: check
given: u1 = 2 | 4; u2 = 3 | -1; u3 = 5 | 0.5; p = 1 | 3
answer: (-1)^p*u1*u2*u3
tolerance: 0.5%

## $PA = LU$ needed {p} row swaps, and U has diagonal {u1}, {u2}, {u3}. What is $\det A$?

::: right
Right. The product of the pivots, with one sign change per swap.
:::

::: misses
- [u1*u2*u3] Each row swap flips the sign of the determinant.
- [any] $(-1)^p\prod u_{ii}$.
:::
:::

::: check
given: a = 0.5 | 0.9 | 0.99
answer: sqrt(1 - a^2)
tolerance: 0.5%

## Cholesky on $\begin{pmatrix}1 & \given{a}\\ \given{a} & 1\end{pmatrix}$: what is $l_{22}$?

::: right
Right. $\sqrt{1 - a^2}$, real only while $\lvert a\rvert < 1$, which is exactly when the matrix is positive definite.
:::

::: misses
- [1 - a^2] Take the square root.
- [any] $\sqrt{1 - l_{21}^2}$ with $l_{21} = a$.
:::
:::

::: check
kind: parts
given: k = 1000 | 100000; s = 1 | 2 | 3

## A matrix with κ = {k} is factored in float32 ($u = 6 \times 10^{-8}$) and refined.

::: part
answer: k*6e-8
tolerance: 1%

### About how accurate is the first solve?

::: misses
- [any] About $\kappa u_{32}$.
:::
:::

::: part
answer: (k*6e-8)^(s + 1)
tolerance: 1%

### About how accurate is it after {s} refinement steps, before float64's limit?

::: misses
- [(k*6e-8)^s] The first solve already carries one factor of $\kappa u$.
- [any] Each step multiplies the error by $\kappa u_{32}$.
:::
:::
:::

::: check
given: n = 100 | 1000 | 10000 | 100000 | 1000000 | 10000000
answer: 8*n
tolerance: 1%

## About how many operations does the Thomas algorithm take on a tridiagonal system with {n} unknowns?

::: right
Right. About 8n: a few operations per row going down and coming back.
:::

::: misses
- [2*n^3/3] That's dense elimination, which ignores the zeros.
- [any] Linear in n.
:::
:::

::: check
kind: choice

## You need $x = A^{-1}b$ for 50 right-hand sides b. What should you compute?

- [ ] $A^{-1}$, then 50 matrix–vector products
- [x] The LU (or Cholesky) factors, then 50 pairs of triangular solves
- [ ] 50 separate Gaussian eliminations
- [ ] $A^{-1}$ by Cramer's rule

::: right
Right. Factoring costs $\frac23n^3$ once, and each solve $2n^2$; forming the inverse costs about $2n^3$ and gives slightly less accurate answers.
:::

::: misses
- [1] The inverse costs three times the factorization and adds rounding error.
- [3] That repeats the expensive part 50 times.
- [4] Cramer's rule is hopelessly slow beyond tiny matrices.
:::
:::

::: check
kind: derivation
vars: a = 1..4, b = -1..1, c = 2..5

## Cholesky on the symmetric matrix $\begin{pmatrix}a & b\\ b & c\end{pmatrix}$.

::: step
answer: sqrt(a)

### What is $l_{11}$?
:::

::: step
answer: b/sqrt(a)

### What is $l_{21}$?
:::

::: step
answer: sqrt(c - b^2/a)

### What is $l_{22}$?
:::

::: right
Right. $l_{22}^2 = \frac{ac - b^2}{a} = \frac{\det A}{a}$, which is positive exactly when A is positive definite.
:::
:::

::: explain
## Why does iterative refinement with a low-precision factorization converge to high-precision accuracy?

::: model
The low-precision factors define an approximate solver that is wrong by a relative amount of about $\kappa(A)u$ on whatever it's given. Iterative refinement only ever asks it to solve for the correction, using a residual computed accurately from the original A and the current answer. Each solve fixes the correction to within that relative error, so the remaining error shrinks by a factor of about $\kappa u$ each time. As long as that factor is below 1, the error falls geometrically until it reaches the limit set by how accurately the residual is computed, the high precision. The expensive factorization never needs to be accurate; it needs only to point in roughly the right direction.
:::
:::

::: practice

::: check
given: a = 16 | 9; b = 2 | 1; c = 5 | 2
answer: sqrt(c - b^2/a)
tolerance: 0.5%

## $l_{22}$ for $\begin{pmatrix}\given{a} & \given{b}\\ \given{b} & \given{c}\end{pmatrix}$?

::: misses
- [any] $\sqrt{c - b^2/a}$.
:::
:::

::: check
given: u1 = 3 | -2; u2 = 2 | 4; u3 = 1.5 | 6; p = 0 | 3
answer: (-1)^p*u1*u2*u3
tolerance: 0.5%

## det A from pivots {u1}, {u2}, {u3} and {p} swaps?

::: misses
- [any] $(-1)^p\prod u_{ii}$.
:::
:::
:::
