Understanding the LU decomposition calculator — concept and principle
This calculator factors a square matrix as P·A = L·U with partial pivoting: L is lower triangular with ones on the diagonal, U is upper triangular, and P records the row swaps. It also counts the swaps and gives the determinant. Use it for numerical linear algebra, solving several systems with the same matrix, and checking Gaussian elimination by hand.
How it is calculated
- In each column k, swap rows so the pivot is the entry with the largest absolute value (partial pivoting); record the swap in P.
- Eliminate the entries below the pivot; each multiplier l_ik = u_ik ÷ u_kk is stored in L.
- What remains is the upper-triangular U.
det(A) = (−1)^(number of swaps) × (product of the diagonal of U)
The principle
Gaussian elimination turns A into an upper-triangular matrix by subtracting multiples of rows. Recording those multiples in L lets you undo the elimination: L·U rebuilds A (with rows in the order P). Once L and U are known, solving A x = b takes only two quick triangular substitutions, so the expensive elimination is done once for many right-hand sides. Pivoting on the largest entry keeps all multipliers at most 1 in size, which limits rounding errors.
Worked example
Default input: A = [2 1 1; 4 −6 0; −2 7 2], partial pivoting.
- Column 1: the largest entry is 4 (row 2), so swap rows 1 and 2: P = [0 1 0; 1 0 0; 0 0 1], 1 row swap.
- Multipliers: row 2 gets 2/4 = 1/2, row 3 gets −2/4 = −1/2; after elimination the second pivot is 4.
- Column 2: multiplier 4/4 = 1; the last pivot is 1.
- L = [1 0 0; 1/2 1 0; −1/2 1 1], U = [4 −6 0; 0 4 1; 0 0 1].
- det(A) = (−1)¹ × 4 × 4 × 1 = −16.
How to read the result
The diagonal of U holds the pivots; a zero pivot means the matrix is singular. Multiplying L·U should give A with its rows in the order given by P. The number of row swaps only affects the sign of the determinant.
Common mistakes
- Forgetting to apply the row swaps to L's earlier multipliers.
- Storing the multiplier with the wrong sign.
- Expecting L·U = A without considering P.
- Skipping pivoting, which can fail on a zero pivot even for an invertible matrix.
Frequently asked questions
What is LU decomposition used for?
Solving linear systems efficiently, computing determinants and inverses.
Why use partial pivoting?
To avoid dividing by zero or tiny pivots, which keeps rounding errors small.
How do I get the determinant from LU?
Multiply the diagonal of U and change the sign once for every row swap.
Sources: J. Demmel, UC Berkeley Math 221 lecture notes, Lecture 5 (2020), Gaussian elimination with partial pivoting