MathsLinear Algebra

Matrix LU
decomposition.

Split a square matrix into lower and upper triangular factors, then solve Ax = b.

How it's worked out

An LU decomposition writes A as L times U. L is lower triangular with 1s on its diagonal, and U is upper triangular. It is Gaussian elimination with its record kept, as Bindel's Numerical Analysis notes explain.

A\displaystyle A=LU\displaystyle {}= LU

Building U. Work down the columns. In each column, the pivot is the entry on the diagonal. To clear an entry below it, subtract a multiple of the pivot row. That multiple is the multiplier.

ℓik\displaystyle \ell_{ik}=uikukk\displaystyle {}= \frac{u_{ik}}{u_{kk}}

Building L. Each multiplier goes into L at the same spot as the entry it cleared. When every column is done, what is left of A is U.

When a pivot is 0. Some matrices cannot be factored as they stand, such as the one with rows 0, 1 and 1, 0. Swapping rows fixes that, and a permutation matrix P records the swaps. Nicholas Hu's notes show that some PA = LU exists for any square matrix.

PA\displaystyle PA=LU\displaystyle {}= LU

Solving Ax = b. Put the factors in and the system splits into two triangular ones. Forward substitution solves the first from the top down, and back substitution solves the second from the bottom up.

Ly\displaystyle Ly=Pb\displaystyle {}= Pb
Ux\displaystyle Ux=y\displaystyle {}= y

The determinant comes for free. L has 1s on its diagonal, so det(A) is the product of the diagonal of U, with the sign flipped once per row swap.

Worked examples

Ravi factors a 3 by 3 and solves it

A\displaystyle A=[21−1−3−12−212]\displaystyle {}= \begin{bmatrix} 2 & 1 & -1 \\ -3 & -1 & 2 \\ -2 & 1 & 2 \end{bmatrix}

The first pivot is 2. Clearing column 1 takes multipliers −3/2 and −1. The next pivot is 1/2, and clearing below it takes 4. No swaps are needed.

L\displaystyle L=[100−3210−141]\displaystyle {}= \begin{bmatrix} 1 & 0 & 0 \\ -\frac{3}{2} & 1 & 0 \\ -1 & 4 & 1 \end{bmatrix}
U\displaystyle U=[21−10121200−1]\displaystyle {}= \begin{bmatrix} 2 & 1 & -1 \\ 0 & \frac{1}{2} & \frac{1}{2} \\ 0 & 0 & -1 \end{bmatrix}

With b = (8, −11, −3), forward substitution gives y = (8, 1, 1). Back substitution then gives x = (2, 3, −1). The diagonal of U multiplies to det(A) = −1.

Open this example in the tool

A zero pivot that needs one swap

A\displaystyle A=[021111213]\displaystyle {}= \begin{bmatrix} 0 & 2 & 1 \\ 1 & 1 & 1 \\ 2 & 1 & 3 \end{bmatrix}

Row 1 starts with 0, and a 0 can never serve as a pivot. Swapping rows 1 and 2 brings a 1 into place, and P records that swap. Elimination then runs as usual, with multipliers 2 and −1/2.

P\displaystyle P=[010100001]\displaystyle {}= \begin{bmatrix} 0 & 1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix}
U\displaystyle U=[1110210032]\displaystyle {}= \begin{bmatrix} 1 & 1 & 1 \\ 0 & 2 & 1 \\ 0 & 0 & \frac{3}{2} \end{bmatrix}

For b = (3, 3, 6), the solution is x = (1, 1, 1). Putting it back gives Ax = (3, 3, 6), which matches b. With one swap, det(A) is minus the product of the diagonal of U, −3.

Open this example in the tool

Partial pivoting keeps the multipliers small

A\displaystyle A=[1234567810]\displaystyle {}= \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 10 \end{bmatrix}

Partial pivoting swaps the largest entry of each column onto the diagonal before clearing it. Here that takes 2 swaps, and the multipliers are 1/7, 4/7, 1/2. None is larger than 1 in size.

Without it, the multipliers are 4, 7, 2. Both routes give det(A) = −3, but with decimal inputs, large multipliers let rounding errors grow. That is why software uses partial pivoting.

Open this example in the tool

Reading your result

Read L for the multipliers. In the matrix with rows 2, 1 and 6, 8, the one entry below the diagonal of L is 3. That says 6 is 3 times the pivot 2. The diagonal of U is 2 and 5, so det(A) = 10.

A P that is not I means rows moved. For the matrix with rows 0, 1 and 1, 0, the factors need 1 swap, and det(A) = −1. Apply the same swaps to b before forward substitution.

A 0 on the diagonal of U means singular. In the matrix with rows 1, 2 and 2, 4, row 2 is twice row 1, so U ends with a 0 pivot and det(A) = 0. The factors still exist, but there is no single solution to Ax = b.

The point is reuse. The Linear Algebra Notes LU section puts factoring an n by n matrix at about 2n³/3 flops, and each triangular solve at n². With the factors kept, each new b costs two triangular solves, so you can solve many right sides at once.

Common mistakes

  • Storing the multiplier with the wrong sign. L holds the number you subtract times the pivot row. If you added 3/2 times row 1, the entry in L is −3/2.
  • Forgetting to swap b. With PA = LU, forward substitution starts from Pb, not b.
  • Swapping rows of U and not of L. When a swap happens part way through, the multipliers already stored in L for those two rows swap too.
  • Mixing up Doolittle and Crout. This tool puts the 1s on the diagonal of L (Doolittle). Crout puts them on U instead, so its numbers sit in different places.

Questions people ask

For one system they cost about the same, since LU is elimination with the multipliers kept. The gain comes with a second right side. With the factors saved, each new b needs only two triangular solves, not a full elimination.

Sources