2.3 LU Factorisation
Record the elimination once as $A=LU$ โ then every new right-hand side costs only two quick triangular solves, $L\mathbf{y}=\mathbf{b}$ and $U\mathbf{x}=\mathbf{y}$.
A structural engineer has a model of a bridge with a few thousand unknown joint displacements. The stiffness matrix $A$ depends only on the bridge โ its beams, joints and materials. The load vector $\mathbf{b}$ changes all the time: rush-hour traffic, a lorry parked on one span, wind from the north, wind from the south, snow. Each load case is a new system $A\mathbf{x}=\mathbf{b}$ with the same matrix. Running Gaussian elimination from scratch for every load would repeat exactly the same row operations on $A$ hundreds of times.
The cure is to do the elimination on $A$ once and record it. The record takes the form of a product $A=LU$ of a lower triangular matrix $L$ (which stores the row operations) and an upper triangular matrix $U$ (the echelon form). After that, each new right-hand side is handled by two triangular systems, which are very cheap to solve. This LU factorisation is how computers actually solve linear systems โ it is the engine inside the "solve" button of every engineering, statistics and graphics package.
Triangular Systems Are Easy
A triangular system needs no elimination at all. In $U\mathbf{x}=\mathbf{y}$ the last equation contains only $x_n$; solve it, substitute upwards, and each equation gives one new unknown. This is back substitution, which you met at the end of Gaussian elimination in Section 1.2. A lower triangular system $L\mathbf{y}=\mathbf{b}$ is solved the same way but from the top down โ forward substitution. For example, $$\begin{bmatrix}1&0&0\\2&1&0\\-3&-2&1\end{bmatrix}\begin{bmatrix}y_1\\y_2\\y_3\end{bmatrix}=\begin{bmatrix}-1\\-7\\8\end{bmatrix}\quad\Longrightarrow\quad\begin{aligned}y_1&=-1,\\ y_2&=-7-2y_1=-5,\\ y_3&=8+3y_1+2y_2=-5.\end{aligned}$$ Because $L$ has $1$s on its diagonal there is not even any dividing to do.
Finding $L$ and $U$ by Elimination
Where do $L$ and $U$ come from? Reduce $A$ to an echelon form using only the replacement operation "subtract a multiple of a higher row from a lower row", $R_i\to R_i-m_{ij}R_j$ with $j\lt i$. The number $m_{ij}$ is called the multiplier; it is $\dfrac{\text{entry to be eliminated}}{\text{pivot}}$. The echelon form we reach is $U$. Each operation is multiplication on the left by a unit lower triangular elementary matrix $E$ (Section 2.2), so $$E_k\cdots E_2E_1A=U\qquad\Longrightarrow\qquad A=\left(E_1^{-1}E_2^{-1}\cdots E_k^{-1}\right)U=LU .$$ The inverse of "subtract $m$ times row $j$ from row $i$" is "add $m$ times row $j$ to row $i$", and when we eliminate column by column the product of these inverses simply drops every multiplier into its own position. That is the remarkable fact behind the method:
- Start with $L=I$ (diagonal of $1$s, unknown entries below).
- Work down column $1$: for each row $i\gt 1$, compute $m_{i1}=a_{i1}/a_{11}$, do $R_i\to R_i-m_{i1}R_1$ and write $m_{i1}$ into position $(i,1)$ of $L$.
- Move to column $2$ of the new matrix and repeat with pivot in position $(2,2)$, then column $3$, and so on. If a pivot is $0$, stop โ a row interchange is needed (see the last part of this section).
- The final matrix is $U$. Check by multiplying $LU$ โ you must get $A$ back.
1. $L$ stores $m$, not $-m$. In Example 1 we added $3R_1$ to $R_3$. Written in the standard form $R_3\to R_3-m_{31}R_1$, that is $m_{31}=-3$, and $-3$ is what goes into $L$. A quick test: $m_{ij}$ always equals (entry being eliminated) รท (pivot), signs included.
2. Use the current matrix. The multiplier $m_{32}$ is computed from the matrix after column 1 has been cleared ($4\div(-2)$), not from the original $A$ ($7\div(-4)$).
3. No scaling. Do not divide a row by its pivot to make a leading $1$ โ that would change the diagonal of $L$. The pivots stay in $U$.
Solving $A\mathbf{x}=\mathbf{b}$ with $L$ and $U$
Suppose $A=LU$. Then $A\mathbf{x}=\mathbf{b}$ reads $L(U\mathbf{x})=\mathbf{b}$. Give the bracket a name, $\mathbf{y}=U\mathbf{x}$, and the single system splits into two triangular ones.
When Does LU Exist? Row Swaps and $PA=LU$
Elimination without row swaps breaks down as soon as a pivot is $0$. The simplest example is $$A=\begin{bmatrix}0&1\\1&1\end{bmatrix}.$$ Suppose $$A=\begin{bmatrix}1&0\\ \ell&1\end{bmatrix}\begin{bmatrix}u_{11}&u_{12}\\0&u_{22}\end{bmatrix}=\begin{bmatrix}u_{11}&u_{12}\\ \ell u_{11}&\ell u_{12}+u_{22}\end{bmatrix}.$$ Comparing $(1,1)$ entries, $u_{11}=0$, so the $(2,1)$ entry is $\ell\cdot0=0\ne1$. This invertible matrix has no LU factorisation. Swapping the two rows first fixes everything โ and swaps are recorded by a permutation matrix.
(a) Let $A$ be an invertible $n\times n$ matrix. Then $A$ has an LU factorisation (with $L$ unit lower triangular) if and only if elimination without row interchanges never meets a zero pivot; equivalently, if and only if every leading submatrix $A_k$ (the top-left $k\times k$ block, $k=1,\dots,n$) is invertible. When it exists, this factorisation is unique.
(b) For every $n\times n$ matrix $A$ there is a permutation matrix $P$ such that $PA$ has an LU factorisation: $$PA=LU.$$ Then $A\mathbf{x}=\mathbf{b}$ is equivalent to $LU\mathbf{x}=P\mathbf{b}$, solved by forward and back substitution as before.
1. Singular matrices can have an LU factorisation. $\begin{bmatrix}1&2\\2&4\end{bmatrix}=\begin{bmatrix}1&0\\2&1\end{bmatrix}\begin{bmatrix}1&2\\0&0\end{bmatrix}$ โ the zero simply shows up on the diagonal of $U$. Theorem 2.13 then does not apply: $U\mathbf{x}=\mathbf{y}$ has no solution or infinitely many.
2. Tiny pivots are dangerous too. On a computer a pivot like $10^{-12}$ produces huge multipliers that magnify rounding errors. Library routines therefore use partial pivoting: in each column they swap up the entry of largest absolute value before eliminating. That is why a numerical package returns three matrices $P$, $L$, $U$ even when no swap was strictly necessary.
Counting the Work
Why go to the trouble of storing $L$? Count the arithmetic. A flop (floating-point operation) is one addition, subtraction, multiplication or division.
- Computing $A=LU$ by elimination takes $\tfrac23n^3-\tfrac12n^2-\tfrac16n$ flops โ about $\tfrac23n^3$ for large $n$.
- Forward substitution with unit lower triangular $L$ takes $n^2-n$ flops; back substitution with $U$ takes $n^2$ flops. Together: about $2n^2$.
- Computing $A^{-1}$ takes about $2n^3$ flops, and then $A^{-1}\mathbf{b}$ costs another $2n^2$.
For $n=3$: factorising costs $18-4.5-0.5=13$ flops and each new right-hand side $6+9=15$ flops. For small $n$ the difference is minor โ but $n^3$ grows much faster than $n^2$.
Exercises
- 1 $\begin{bmatrix}2&5\\6&13\end{bmatrix}$
- 2 $\begin{bmatrix}4&-2\\-8&7\end{bmatrix}$
- 3 $\begin{bmatrix}1&2&3\\2&7&9\\-1&4&1\end{bmatrix}$
- 4 $\begin{bmatrix}2&-1&3\\-4&5&-4\\6&0&13\end{bmatrix}$
- 5 $\begin{bmatrix}3&1&0\\9&5&2\\0&4&7\end{bmatrix}$
- 6 $\begin{bmatrix}1&1&0&0\\2&3&1&0\\0&2&4&1\\0&0&3&5\end{bmatrix}$
- 7 The $2\times3$ matrix $\begin{bmatrix}2&1&4\\6&5&9\end{bmatrix}$ ($L$ is $2\times2$, $U$ is $2\times3$).
- 8 The general $2\times2$ matrix $\begin{bmatrix}a&b\\c&d\end{bmatrix}$ with $a\ne0$.
- 1 $\begin{bmatrix}2&5\\6&13\end{bmatrix}\mathbf{x}=\begin{bmatrix}1\\5\end{bmatrix}$ (use Ex. I.1).
- 2 $\begin{bmatrix}1&2&3\\2&7&9\\-1&4&1\end{bmatrix}\mathbf{x}=\begin{bmatrix}3\\6\\-5\end{bmatrix}$ (use Ex. I.3).
- 3 Solve $A\mathbf{x}=\mathbf{b}_1$ and $A\mathbf{x}=\mathbf{b}_2$ for the matrix of Ex. I.4, with $\mathbf{b}_1=\begin{bmatrix}-1\\0\\-7\end{bmatrix}$ and $\mathbf{b}_2=\begin{bmatrix}2\\1\\13\end{bmatrix}$.
- 4 $A=LU$ with $L=\begin{bmatrix}1&0&0\\-1&1&0\\2&-3&1\end{bmatrix}$ and $U=\begin{bmatrix}1&-2&1\\0&2&4\\0&0&-1\end{bmatrix}$. Solve $A\mathbf{x}=\begin{bmatrix}3\\-1\\4\end{bmatrix}$ without forming $A$, then form $A$ and check.
- 5 $\begin{bmatrix}3&1&0\\9&5&2\\0&4&7\end{bmatrix}\mathbf{x}=\begin{bmatrix}4\\16\\11\end{bmatrix}$ (use Ex. I.5).
- 6 Find the first column of $A^{-1}$ for the matrix of Ex. I.3 by solving $A\mathbf{x}=\mathbf{e}_1$.
- 7 Find $A^{-1}$ for $A=\begin{bmatrix}2&5\\6&13\end{bmatrix}$ by solving $A\mathbf{x}=\mathbf{e}_1$ and $A\mathbf{x}=\mathbf{e}_2$ with the factors of Ex. I.1.
- 8 Electrical circuit. The loop currents $i_1,i_2,i_3$ (amperes) in a three-loop circuit satisfy $R\mathbf{i}=\mathbf{v}$ with resistance matrix $R=\begin{bmatrix}4&-2&0\\-2&5&-1\\0&-1&3\end{bmatrix}$ (ohms) and source voltages $\mathbf{v}$ (volts). Factor $R=LU$ and find the currents for the two settings $\mathbf{v}=(8,3,1)$ and $\mathbf{v}=(2,1,5)$.
- 1 Show that $\begin{bmatrix}0&1\\1&1\end{bmatrix}$ has no LU factorisation, and find a permutation matrix $P$ with $PA=LU$.
- 2 Does the singular matrix $\begin{bmatrix}1&2\\2&4\end{bmatrix}$ have an LU factorisation? If so, find it.
- 3 Find $P$, $L$ and $U$ with $PA=LU$ for $A=\begin{bmatrix}0&3&1\\2&4&6\\1&5&2\end{bmatrix}$.
- 4 For which values of $k$ does $A=\begin{bmatrix}1&2&0\\2&k&1\\0&1&3\end{bmatrix}$ have an LU factorisation (no row interchanges)?
- 5 Write down the $3\times3$ permutation matrix $P$ that swaps rows 1 and 3. Compute $P^2$ and explain why $P^{-1}=P=P^{T}$.
- 6 Suppose $A=LU$ is invertible. Explain why no diagonal entry of $U$ can be $0$.
- 1 For $n=500$, estimate the flops for computing $A=LU$ and for one pair of triangular solves. How many solves cost as much as one factorisation?
- 2 A system with $n=2000$ must be solved for $200$ right-hand sides. Compare the cost of repeating elimination each time with the cost of factorising once.
- 3 A computer performs $10^{9}$ flops per second. How long does the LU factorisation of a $10\,000\times10\,000$ matrix take, and how long does each further right-hand side take?
- 4 If $n$ is doubled, by what factor does the cost of the factorisation grow? And the cost of a pair of triangular solves?
- 5 A classmate suggests computing $A^{-1}$ once and then forming $A^{-1}\mathbf{b}$ for every load. Compare the costs and give two reasons to prefer $LU$.
- 6 Long chains. For a tridiagonal matrix (nonzero entries only on the diagonal and directly above and below it), such as the spring matrix of Example 3 with $n$ masses, explain why $L$ and $U$ are also mostly zeros and why the whole solve then costs a number of flops proportional to $n$ rather than $n^3$.
Research question. When engineers model a long structure โ a rail track on sleepers, a pipeline, a heated metal rod, a chain of floors in a tall building โ the matrix is tridiagonal. What pattern do the pivots and multipliers of its LU factorisation follow, and how fast can such a system really be solved?
Why it matters. Finite-difference models of heat flow, beam bending and vibration in civil, mechanical and biomedical engineering all lead to the matrix $K_n$ with $2$ on the diagonal and $-1$ beside it. Solvers for these "banded" systems run inside weather models, bridge-monitoring software and heart-tissue simulations, often millions of times.
What to do.
- Factor $K_4$ and $K_5$ by hand, extending Example 3. List the pivots $u_{11},u_{22},\dots$ and the multipliers. Guess the general pattern for $K_n$ and test it on $K_6$ with Interactive 1's method.
- Use your pattern to predict the product of the pivots of $K_n$ (this number is $\det K_n$, Chapter 3).
- In a spreadsheet, solve $K_{10}\mathbf{u}=\mathbf{f}$ for the uniform load $\mathbf{f}=(1,1,\dots,1)$ by forward and back substitution (one column for $\mathbf{y}$, one for $\mathbf{u}$). Plot $u_i$ against $i$ โ what shape do you see?
- Optional: in Python (
scipy.linalg.lu_factor/lu_solve) or Octave, time solving with $n=1000,2000,4000$ for one and for $100$ right-hand sides; compare with the predictions $\tfrac23n^3$ and $2n^2$.
Tools: Pen and paper, spreadsheet; optionally Python or Octave
Hand in: A one-page note: the pivot and multiplier patterns with a short justification, the displacement plot, and one sentence on why banded solvers make very large engineering models possible.
Going further. Look up the "Thomas algorithm" used for tridiagonal systems in computational fluid dynamics, and compare its operation count with your answer to Exercise IV.6.
๐งช Self-check quiz
Q1. Elimination on a $3\times3$ matrix uses, in order, $R_2\to R_2-4R_1$, $R_3\to R_3+2R_1$, $R_3\to R_3-5R_2$ and no other operations. Then $L=$
Q2. Given $A=LU$, the first step in solving $A\mathbf{x}=\mathbf{b}$ is to solve
Q3. Which matrix has no LU factorisation without row interchanges?
Q4. For large $n$ the LU factorisation costs about $\tfrac23n^3$ flops. If the number of unknowns grows from $n$ to $10n$, the factorisation cost is multiplied by about
Q5. If $PA=LU$ with a permutation matrix $P$, then $A\mathbf{x}=\mathbf{b}$ is equivalent to