This LU decomposition calculator factors a square matrix into a lower triangular matrix L and an upper triangular matrix U, using partial pivoting. It shows the permutation, L, U and the determinant, and it can solve Ax = b for a right-hand side that you supply.
LU Decomposition Calculator
Factor a square matrix as PA = LU (Doolittle or Crout, with or without pivoting), get the determinant and inverse, and solve Ax = b by forward and back substitution.
Starting values: the calculator opens with A = rows (2, 1, 1), (4, -6, 0) and (-2, 7, 2). With partial pivoting, the determinant is -16, and for b = (5, -2, 9) the solution is x = (1, 1, 2).
What LU decomposition is
LU decomposition writes a matrix as a product of two triangular matrices. With row swaps for stability, the identity is P A = L U, where P is a permutation matrix, L is lower triangular with 1 on its diagonal, and U is upper triangular. It is the matrix form of Gaussian elimination: U is the result of the elimination, and L stores the multipliers used on the way.
How partial pivoting works
At each step, the algorithm looks down the current column and swaps the row with the largest absolute value into the pivot position. This avoids dividing by a tiny number and keeps rounding errors under control. Every swap is recorded in the permutation. For the default matrix, the first column holds 2, 4 and -2, so the row with 4 moves to the top.
Worked examples
Example 1: the default matrix
After the swap, the rows in order are (4, -6, 0), (2, 1, 1) and (-2, 7, 2), and the permutation is 1, 0, 2 in zero-based row numbers. The multipliers are 2/4 = 0.5 and -2/4 = -0.5. After the first step, the remaining rows are (0, 4, 1) and (0, 4, 2). The multiplier for the last row is 1, and the final pivot is 1.
The factors are L = rows (1, 0, 0), (0.5, 1, 0) and (-0.5, 1, 1), and U = rows (4, -6, 0), (0, 4, 1) and (0, 0, 1). You can check with the second row: 0.5 x (4, -6, 0) + (0, 4, 1) gives (2, 1, 1), which is the original row.
Example 2: the determinant
The determinant of U is the product of its diagonal: 4 x 4 x 1 = 16. One row swap flips the sign, so det A = -16. You can confirm this by cofactor expansion of the original matrix: 2 x (-12) – 1 x 8 + 1 x 16 = -16.
Example 3: solving a system
To solve Ax = b with b = (5, -2, 9), first reorder b with the permutation to get (-2, 5, 9). Solve Ly = Pb by forward substitution: y1 = -2, y2 = 5 – 0.5 x (-2) = 6 and y3 = 9 + 0.5 x (-2) – 6 = 2. Then solve Ux = y by back substitution: z = 2, then 4y + 2 = 6 gives y = 1, and 4x – 6 = -2 gives x = 1. The solution is x = 1, y = 1, z = 2.
Why use LU instead of the inverse
If you have to solve the same matrix with many different right-hand sides, you factor once and then run two cheap triangular solves for each new b. That is faster and usually more accurate than computing the inverse. It is also the standard route to a determinant.
Limits
- The matrix must be square.
- A singular matrix, with zero determinant, has no unique solution, and a zero pivot will appear even after pivoting.
- Different programs choose different pivoting rules, so your L and U may not match another tool exactly, although P A = L U will hold in each case.
Worked check of the factors
It is worth multiplying L and U back together once. The first row of L times U gives (4, -6, 0), which is the first row of the reordered matrix. The third row of L, (-0.5, 1, 1), times U gives -0.5 x (4, -6, 0) + 1 x (0, 4, 1) + 1 x (0, 0, 1) = (-2, 7, 2), the third row. If every row of L times U matches the corresponding row of the reordered matrix, the factorization is right. The reordering is what the permutation P records, which is why the identity reads P A = L U and not A = L U.
Cost and accuracy in brief
For an n by n matrix, factoring takes work proportional to n cubed, while each later solve takes work proportional to n squared. That gap is why a stored factorization pays off when you have several right-hand sides. Partial pivoting is not a perfect guard against rounding error in every case, but it is the default in most numerical libraries because it works well in practice.
Frequently asked questions
What is LU decomposition used for?
It is used to solve linear systems, to compute determinants and to find inverses, and it reuses one factorization for many right-hand sides.
Why does the calculator use pivoting?
Swapping the largest entry into the pivot position avoids division by very small numbers and keeps rounding errors small.
How do I get the determinant from LU?
Multiply the diagonal of U, then flip the sign once for every row swap.
Is the LU factorization unique?
With a fixed pivoting rule and 1s on the diagonal of L, yes. Other pivoting rules give other valid factors.
Can every matrix be decomposed?
Every square matrix has a factorization with row swaps, but a singular matrix will have a zero on the diagonal of U.
