Matrix LU
decomposition.
Split a square matrix into lower and upper triangular factors, then solve Ax = b.
- det(A)
- −1
- Row swaps
- 0
- Solution x
- (2,3,−1)
No row swaps were needed, so A = LU directly. L holds the multipliers and U the pivots. With the factors, x = (2, 3, −1) took two triangular solves.
The shape of the two factors
L is 0 above its diagonal and has 1s on it; its shaded cells below hold the multipliers −3/2, −1, 4. U is 0 below its diagonal, and its diagonal holds the pivots 2, 1/2, −1. Plain cells inside a triangle are entries that came out 0.
Working
- 1Start with U = A and L = IEach elimination multiplier is stored in L below the diagonal.U =
- 2Eliminate below pivot 2Multipliers ℓ21 = −32, ℓ31 = −1; each row i becomes Rᵢ − ℓᵢ1 × R1.U =
- 3Eliminate below pivot 12Multipliers ℓ32 = 4; each row i becomes Rᵢ − ℓᵢ2 × R2.U =
- 4Read off the factorsL =U =
- 5Forward substitution: solve Ly = b from the topy1 = 8 = 8y2 = −11 − (−32)(8) = 1y3 = −3 − (−1)(8) − (4)(1) = 1
- 6Back substitution: solve Ux = y from the bottomx3 = (1) ÷ −1 = −1x2 = (1 − (12)(−1)) ÷ 12 = 3x1 = (8 − (1)(3) − (−1)(−1)) ÷ 2 = 2
- 7Check: L times U gives back ALU =and Ax =(8,−11,−3), which is b
Type or paste a matrix
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.
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.
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.
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.
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
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.
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.
A zero pivot that needs one swap
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.
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.
Partial pivoting keeps the multipliers small
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.
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.