LU Decomposition
An LU decomposition writes a square matrix A as A = LU, where L is lower triangular with 1s on its diagonal and U is upper triangular. U is the echelon form from Gaussian elimination, and L stores the multipliers used. To solve Ax = b, solve Ly = b by forward substitution, then Ux = y by back substitution.
Before this lesson: Gaussian Elimination, Elementary Matrices and Invertible Matrix Theorem
Why factor a matrix?
Gaussian elimination does most of its work on A, not on b. If you must solve Ax = b for many different right-hand sides b, repeating the elimination each time wastes effort. An LU decomposition does the elimination once and saves the result.
A matrix is lower triangular if every entry above the main diagonal is 0, and upper triangular if every entry below it is 0. Triangular systems are easy: you can solve them one variable at a time, from the top down for L or from the bottom up for U.
How LU decomposition works
Suppose A can be reduced to echelon form U using only row replacements of the form Rᵢ − mRⱼ → Rᵢ with j less than i (no row swaps and no scaling). Each step is an elementary matrix Eₖ that is lower triangular, so
E_k \cdots E_2 E_1 A = U \quad\Longrightarrow\quad A = (E_k \cdots E_1)^{-1}U = LUThe inverse of each Eₖ adds back what Eₖ subtracted, so L = E₁⁻¹⋯Eₖ⁻¹ is also lower triangular. Better still, no multiplication is needed: the multiplier m from the step Rᵢ − mRⱼ → Rᵢ goes straight into row i, column j of L.
L = \begin{bmatrix} 1 & 0 & 0 \\ m_{21} & 1 & 0 \\ m_{31} & m_{32} & 1 \end{bmatrix}How to find an LU decomposition
- Reduce A to echelon form U using only steps Rᵢ − mRⱼ → Rᵢ, working down one column at a time. Each multiplier is m = (entry to remove) ÷ (pivot above it).
- Record each multiplier m in row i, column j of L. Put 1s on the diagonal of L and 0s above it.
- Check that LU = A by multiplying.
- To solve Ax = b, solve Ly = b from the top down (forward substitution), then Ux = y from the bottom up (back substitution).
If a 0 shows up in a pivot position, elimination needs a row swap and A has no LU decomposition in this form. Then one uses PA = LU, where P is a permutation matrix that does the swaps first.
LU decomposition examples
Example 1: A 2 × 2 matrix
Find the LU decomposition of A = [2 3; 4 7] (rows separated by semicolons).
m_{21} = \dfrac{4}{2} = 2The entry to remove is 4 and the pivot above it is 2.
\begin{bmatrix} 2 & 3 \\ 4 & 7 \end{bmatrix} \to \begin{bmatrix} 2 & 3 \\ 0 & 1 \end{bmatrix} = UR₂ − 2R₁ → R₂. Row 2: (4 − 4, 7 − 6) = (0, 1).
L = \begin{bmatrix} 1 & 0 \\ 2 & 1 \end{bmatrix}Put the multiplier 2 in row 2, column 1.
LU = \begin{bmatrix} 1(2) + 0(0) & 1(3) + 0(1) \\ 2(2) + 1(0) & 2(3) + 1(1) \end{bmatrix} = \begin{bmatrix} 2 & 3 \\ 4 & 7 \end{bmatrix}Check by multiplying (see matrix multiplication).
Answer: L = [1 0; 2 1] and U = [2 3; 0 1].
Example 2: A 3 × 3 matrix
Find the LU decomposition of A = [2 1 1; 4 3 3; −2 1 4].
m_{21} = \dfrac{4}{2} = 2,\quad m_{31} = \dfrac{-2}{2} = -1Column 1: divide each entry below the pivot 2 by 2.
\to \begin{bmatrix} 2 & 1 & 1 \\ 0 & 1 & 1 \\ -2 & 1 & 4 \end{bmatrix}R₂ − 2R₁ → R₂. Row 2: (4 − 4, 3 − 2, 3 − 2) = (0, 1, 1).
\to \begin{bmatrix} 2 & 1 & 1 \\ 0 & 1 & 1 \\ 0 & 2 & 5 \end{bmatrix}R₃ − (−1)R₁ → R₃. Row 3: (−2 + 2, 1 + 1, 4 + 1) = (0, 2, 5).
m_{32} = \dfrac{2}{1} = 2Column 2: the entry to remove is 2 and the pivot is 1.
\to \begin{bmatrix} 2 & 1 & 1 \\ 0 & 1 & 1 \\ 0 & 0 & 3 \end{bmatrix} = UR₃ − 2R₂ → R₃. Row 3: (0, 2 − 2, 5 − 2) = (0, 0, 3).
L = \begin{bmatrix} 1 & 0 & 0 \\ 2 & 1 & 0 \\ -1 & 2 & 1 \end{bmatrix}Place m₂₁ = 2, m₃₁ = −1 and m₃₂ = 2.
\text{row 3 of } LU = -1(2, 1, 1) + 2(0, 1, 1) + 1(0, 0, 3) = (-2, 1, 4)Check the hardest row: it matches row 3 of A.
Answer: L = [1 0 0; 2 1 0; −1 2 1] and U = [2 1 1; 0 1 1; 0 0 3]. Rows 1 and 2 of LU also match A: (2, 1, 1) and 2(2, 1, 1) + (0, 1, 1) = (4, 3, 3).
Example 3: Solve a system with the factorization
Use the LU decomposition from Example 2 to solve Ax = b for b = (5, 13, 4).
y_1 = 5
Forward substitution on Ly = b. Row 1 of L says y₁ = 5.
2(5) + y_2 = 13 \Rightarrow y_2 = 3
Row 2: 2y₁ + y₂ = 13. Subtract 10 from both sides.
-1(5) + 2(3) + y_3 = 4 \Rightarrow y_3 = 3
Row 3: −y₁ + 2y₂ + y₃ = 4. Simplify to 1 + y₃ = 4 and subtract 1.
3x_3 = 3 \Rightarrow x_3 = 1
Back substitution on Ux = y = (5, 3, 3). Row 3: divide by 3.
x_2 + 1 = 3 \Rightarrow x_2 = 2
Row 2: x₂ + x₃ = 3. Subtract 1 from both sides.
2x_1 + 2 + 1 = 5 \Rightarrow x_1 = 1
Row 1: 2x₁ + x₂ + x₃ = 5. Subtract 3, then divide by 2.
Answer: x = (1, 2, 1). Check in A: (2 + 2 + 1, 4 + 6 + 3, −2 + 2 + 4) = (5, 13, 4).
Example 4: A matrix with no LU decomposition
Show that A = [0 2; 3 1] has no LU decomposition, and fix it with a row swap.
\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}Multiply a general L and U.
u_{11} = 0Match entry (1, 1) with A: u₁₁ = 0.
\ell u_{11} = \ell(0) = 0 \ne 3Match entry (2, 1): LU would have 0 there, but A has 3.
PA = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} 0 & 2 \\ 3 & 1 \end{bmatrix} = \begin{bmatrix} 3 & 1 \\ 0 & 2 \end{bmatrix}Swap the rows first with the permutation matrix P.
PA = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}\begin{bmatrix} 3 & 1 \\ 0 & 2 \end{bmatrix}PA is already upper triangular, so L = I and U = PA.
Answer: A has no LU decomposition because its first pivot is 0, even though A is invertible (det A = −6). With the swap, PA = LU with L = I and U = [3 1; 0 2].
Example 5: The same matrix, many right-hand sides
A workshop makes two products, X and Y. Each X needs 2 hours on machine 1 and 4 hours on machine 2; each Y needs 3 hours and 7 hours. In week 1 the machines have 18 and 40 hours available; in week 2, 13 and 29 hours. How many of each product use all the hours each week?
A = \begin{bmatrix} 2 & 3 \\ 4 & 7 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 2 & 1 \end{bmatrix}\begin{bmatrix} 2 & 3 \\ 0 & 1 \end{bmatrix}Rows are machines, columns are products. Reuse L and U from Example 1.
y_1 = 18,\quad 2(18) + y_2 = 40 \Rightarrow y_2 = 4
Week 1, forward: subtract 36 from both sides.
x_2 = 4,\quad 2x_1 + 3(4) = 18 \Rightarrow x_1 = 3
Week 1, back: Ux = (18, 4). Subtract 12, then divide by 2.
y_1 = 13,\quad 2(13) + y_2 = 29 \Rightarrow y_2 = 3
Week 2, forward: subtract 26 from both sides.
x_2 = 3,\quad 2x_1 + 3(3) = 13 \Rightarrow x_1 = 2
Week 2, back: Ux = (13, 3). Subtract 9, then divide by 2.
Answer: week 1, make 3 X and 4 Y (check: 2(3) + 3(4) = 18 and 4(3) + 7(4) = 40); week 2, make 2 X and 3 Y (check: 4 + 9 = 13 and 8 + 21 = 29). The elimination was done once and reused.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Putting the wrong sign in L | Writing −2 in L after the step R₂ − 2R₁ → R₂ | L stores the multiplier m from Rᵢ − mRⱼ, so L gets +2. |
| Scaling rows during elimination | Dividing row 1 by its pivot to make it 1 | Use only row replacements; the pivots stay in U and L keeps 1s on its diagonal. |
| Swapping rows without a P | Swapping rows of [0 2; 3 1] and still writing A = LU | A swap changes the factorization to PA = LU. |
| Doing back substitution first | Solving Ux = b directly | Solve Ly = b first, then Ux = y. |
| Putting a multiplier in the wrong place | Writing m₃₂ in row 2, column 3 | The multiplier for Rᵢ − mRⱼ goes in row i, column j, below the diagonal. |
Related ideas
Why LU saves time. Finding L and U for an n × n matrix takes about ⅔n³ arithmetic operations, but each later solve with forward and back substitution takes only about 2n². For a large matrix and many right-hand sides, that is far less work than repeating elimination or computing A⁻¹.
Determinants from LU. Since det L = 1, det A = det U, the product of the pivots on the diagonal of U. In Example 2, det A = 2 · 1 · 3 = 6. See the determinant of a matrix lesson for other methods; Cofactor Expansion begins the next unit. Partial pivoting, which swaps rows to avoid small pivots and rounding errors, is part of Numerical Methods.
Practice problems
- Find the LU decomposition of A = [1 2; 3 8].
- Find the LU decomposition of A = [4 −2; 2 5].
- Solve Ly = b by forward substitution, where L = [1 0 0; 2 1 0; −3 4 1] and b = (2, 7, 1).
- Solve Ux = y by back substitution, where U = [2 −1 3; 0 1 2; 0 0 4] and y = (9, 4, 8).
- Find the LU decomposition of A = [1 2 0; 3 7 2; −2 −2 5].
- Use your answer to Problem 5 to solve Ax = b for b = (3, 12, 1).
- L = [1 0; −2 1] and U = [3 1; 0 4]. Find A = LU.
- Does A = [0 1; 1 1] have an LU decomposition without row swaps? If not, find P with PA = LU.
- Use the LU decomposition from Problem 5 to find det A.
- The workshop in Example 5 has 11 hours on machine 1 and 25 hours on machine 2 in week 3. How many of each product use all the hours?
Show answers
1.
m_{21} = \dfrac{3}{1} = 3Divide the entry below the pivot by the pivot.
\begin{bmatrix} 1 & 2 \\ 3 & 8 \end{bmatrix} \to \begin{bmatrix} 1 & 2 \\ 0 & 2 \end{bmatrix}R₂ − 3R₁ → R₂. Row 2: (3 − 3, 8 − 6) = (0, 2).
Answer: L = [1 0; 3 1] and U = [1 2; 0 2].
2.
m_{21} = \dfrac{2}{4} = \dfrac{1}{2}Divide the entry below the pivot by the pivot.
\begin{bmatrix} 4 & -2 \\ 2 & 5 \end{bmatrix} \to \begin{bmatrix} 4 & -2 \\ 0 & 6 \end{bmatrix}R₂ − ½R₁ → R₂. Row 2: (2 − 2, 5 + 1) = (0, 6).
Answer: L = [1 0; ½ 1] and U = [4 −2; 0 6].
3.
y_1 = 2
Row 1 of L.
2(2) + y_2 = 7 \Rightarrow y_2 = 3
Row 2: subtract 4 from both sides.
-3(2) + 4(3) + y_3 = 1 \Rightarrow y_3 = -5
Row 3: −6 + 12 = 6, then subtract 6 from both sides.
Answer: y = (2, 3, −5).
4.
4x_3 = 8 \Rightarrow x_3 = 2
Row 3: divide by 4.
x_2 + 2(2) = 4 \Rightarrow x_2 = 0
Row 2: subtract 4 from both sides.
2x_1 - 0 + 3(2) = 9 \Rightarrow 2x_1 = 3
Row 1: subtract 6 from both sides.
x_1 = \dfrac{3}{2}Divide both sides by 2.
Answer: x = (3/2, 0, 2).
5.
m_{21} = 3,\quad m_{31} = \dfrac{-2}{1} = -2Column 1 multipliers: divide by the pivot 1.
\to \begin{bmatrix} 1 & 2 & 0 \\ 0 & 1 & 2 \\ -2 & -2 & 5 \end{bmatrix}R₂ − 3R₁ → R₂. Row 2: (3 − 3, 7 − 6, 2 − 0) = (0, 1, 2).
\to \begin{bmatrix} 1 & 2 & 0 \\ 0 & 1 & 2 \\ 0 & 2 & 5 \end{bmatrix}R₃ − (−2)R₁ → R₃. Row 3: (−2 + 2, −2 + 4, 5 + 0) = (0, 2, 5).
\to \begin{bmatrix} 1 & 2 & 0 \\ 0 & 1 & 2 \\ 0 & 0 & 1 \end{bmatrix}m₃₂ = 2/1 = 2, so R₃ − 2R₂ → R₃. Row 3: (0, 2 − 2, 5 − 4) = (0, 0, 1).
Answer: L = [1 0 0; 3 1 0; −2 2 1] and U = [1 2 0; 0 1 2; 0 0 1].
6.
y_1 = 3
Forward substitution, row 1.
3(3) + y_2 = 12 \Rightarrow y_2 = 3
Row 2: subtract 9 from both sides.
-2(3) + 2(3) + y_3 = 1 \Rightarrow y_3 = 1
Row 3: −6 + 6 = 0, so y₃ = 1.
x_3 = 1
Back substitution on Ux = (3, 3, 1). Row 3.
x_2 + 2(1) = 3 \Rightarrow x_2 = 1
Row 2: subtract 2 from both sides.
x_1 + 2(1) + 0 = 3 \Rightarrow x_1 = 1
Row 1: subtract 2 from both sides.
Answer: x = (1, 1, 1). Check: (1 + 2, 3 + 7 + 2, −2 − 2 + 5) = (3, 12, 1).
7.
LU = \begin{bmatrix} 1(3) + 0(0) & 1(1) + 0(4) \\ -2(3) + 1(0) & -2(1) + 1(4) \end{bmatrix}Multiply rows of L by columns of U.
= \begin{bmatrix} 3 & 1 \\ -6 & 2 \end{bmatrix}Multiply and add.
Answer: A = [3 1; −6 2].
8.
a_{11} = 0 = u_{11}The first pivot is 0, so no row replacement can clear the 1 below it.
P = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix},\; PA = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}Swap the rows first.
Answer: no LU decomposition without swaps. With P = [0 1; 1 0], PA = [1 1; 0 1] is already upper triangular, so L = I and U = PA.
9.
\det A = \det L \cdot \det U
The determinant of a product is the product of the determinants.
= 1 \cdot (1 \cdot 1 \cdot 1) = 1
Each triangular determinant is the product of its diagonal.
Answer: det A = 1.
10.
y_1 = 11,\quad 2(11) + y_2 = 25 \Rightarrow y_2 = 3
Forward substitution with L = [1 0; 2 1]: subtract 22.
x_2 = 3,\quad 2x_1 + 3(3) = 11 \Rightarrow x_1 = 1
Back substitution with U = [2 3; 0 1]: subtract 9, then divide by 2.
Answer: make 1 X and 3 Y. Check: 2(1) + 3(3) = 11 and 4(1) + 7(3) = 25.
LU decomposition FAQ
What is LU decomposition used for?
It solves Ax = b quickly for many different b with the same A, because the elimination is done once. It also gives det A as the product of the pivots in U.
Does every matrix have an LU decomposition?
No. If a zero pivot appears and a row swap is needed, A has no LU decomposition in this form. Every invertible matrix does have one after reordering its rows, written PA = LU.
Is LU decomposition the same as Gaussian elimination?
It is Gaussian elimination with a record kept. U is the echelon form that elimination produces, and L stores the multipliers so the same steps can be applied to any b later.
What is the difference between forward and back substitution?
Forward substitution solves a lower triangular system from the first equation down. Back substitution solves an upper triangular system from the last equation up.
Summary
- A = LU, where L is lower triangular with 1s on the diagonal and U is the echelon form of A.
- The multiplier m from Rᵢ − mRⱼ → Rᵢ goes in row i, column j of L.
- To solve Ax = b: forward substitution for Ly = b, then back substitution for Ux = y.
- If elimination needs a row swap, use PA = LU instead.
- det A is the product of the diagonal entries of U.
Previous lesson: Invertible Matrix Theorem
Next lesson: Cofactor Expansion
Back to the Linear Algebra Index