LU decomposition writes a square matrix A as the product of a lower triangular matrix L, with ones on its diagonal, and an upper triangular matrix U. It is Gaussian elimination with the bookkeeping kept: U is what elimination leaves behind, and L records the multipliers used to get there. This LU decomposition calculator performs the factorisation with partial pivoting, prints L, U and the permutation that was applied, and tells you plainly when the matrix needs a row swap — rather than dividing by a zero pivot and returning a factorisation that is silently wrong.
Arb Digital treats the pivoting case as the main event rather than a footnote. A textbook example almost always factors without a swap, so a script that ignores pivoting appears to work until the day it meets a real matrix with a zero in the top-left corner. From that point it produces infinities, or worse, plausible numbers built on a division by something microscopic. The tool above reports the permutation explicitly so that what you are given is a statement you can verify.
What This LU Decomposition Calculator Does
It reads a square matrix from 2×2 up to 5×5 and runs Gaussian elimination on it, storing each multiplier in the position it eliminated. With partial pivoting selected — the sensible default — it returns three matrices satisfying PA = LU, where P is the permutation matrix recording the row swaps. With pivoting switched off it attempts the plain Doolittle factorisation A = LU and reports honestly if no such factorisation exists.
The supporting figures are the determinant, computed as the product of the diagonal of U with a sign flip for each swap, the number of swaps, and a verification number: the largest entry-by-entry discrepancy between PA and LU. That last figure is the only honest measure of how much floating-point error has accumulated, and it should be at or near zero for exact input.
This is a different job from the pages beside it. Our determinant calculator returns the single number without the factors; our matrix inverse calculator runs Gauss-Jordan all the way to the identity to build A⁻¹; our row echelon form calculator reduces a matrix of any shape to echelon form for reading off pivots. LU stops halfway on purpose, because the halfway point is what you reuse.
How to Use It
- Enter the matrix one row per line. The row count must match the number of entries in every row; ragged input is refused with an explanation.
- Leave partial pivoting on for numerical work. It selects the largest available pivot in each column, which keeps the multipliers in L at magnitude one or below.
- Switch pivoting off to answer a coursework question. Many textbook exercises specifically ask for the Doolittle factorisation without swaps, and the tool will say if that factorisation does not exist.
- Read the swap count before anything else. Zero swaps means P is the identity and PA = LU simplifies to A = LU.
- Check the verification figure. Anything far from zero indicates an ill-conditioned matrix where the factors should not be trusted as exact.
The Method and How It's Calculated
Take the default matrix with pivoting switched off. The first pivot is 2. The multiplier for row two is 4 ÷ 2 = 2, so row two becomes row two minus twice row one, giving 0, −8, −2. The multiplier for row three is −2 ÷ 2 = −1, so row three becomes row three plus row one, giving 0, 8, 3. The second pivot is −8; the multiplier for row three is 8 ÷ (−8) = −1, and row three becomes 0, 0, 1.
What is left is U, with rows 2, 1, 1 / 0, −8, −2 / 0, 0, 1. The multipliers 2, −1 and −1 go into L below the diagonal, with ones on the diagonal itself: L is 1, 0, 0 / 2, 1, 0 / −1, −1, 1. Multiply L by U and you recover A exactly. The determinant is the product of the diagonal of U, 2 × (−8) × 1 = −16, with no sign flip because no rows were swapped.
With partial pivoting on, the same matrix behaves differently. The largest entry in the first column is 4, so rows one and two swap before elimination begins, and every multiplier that follows is at most one in magnitude. The determinant is unchanged in size but the sign flips once per swap, and the permutation is recorded in P. The systematic development of elimination into a matrix factorisation is the spine of the MIT OpenCourseWare 18.06SC Linear Algebra course.
Why the Factorisation Is Worth Having At All
Elimination on an n×n matrix costs roughly n³/3 operations. Solving a triangular system costs only n². If you have to solve Ax = b for one right-hand side, LU saves nothing. If you have to solve it for fifty different right-hand sides — a structural model under fifty load cases, a regression re-fitted against fifty response vectors — you factor once and then run fifty pairs of cheap triangular solves. That single reuse is the reason LU is the standard workhorse rather than a curiosity.
The solve itself has two stages. Given PA = LU and a system Ax = b, first permute the right-hand side to Pb, then solve Ly = Pb by forward substitution, working down from the top because L has ones on the diagonal and nothing above it. Then solve Ux = y by back substitution, working up from the bottom. No division by anything other than a pivot of U ever occurs. Our system of equations calculator carries out that solve directly if the solution vector is all you need.
The determinant comes free as well, and this matters more than it sounds. Computing a determinant by cofactor expansion costs on the order of n! operations, which is unusable past about 5×5. Reading it off the diagonal of U costs nothing beyond the factorisation you already have. Every serious library computes determinants exactly this way.
What Pivoting Actually Protects Against
There are two separate problems, and they are often confused. The first is a hard zero in a pivot position, which makes the multiplier undefined and stops the plain factorisation dead. That is a structural obstruction, and swapping in a row with a non-zero entry in that column removes it entirely.
The second problem is subtler and does more damage in practice. Suppose the pivot is not zero but is 0.0001 while another row has 1 in that column. The multiplier is then 10,000, and elimination subtracts 10,000 times one row from another. Any rounding error already present in the first row is multiplied by 10,000 as well, and the significant digits of the second row are swamped. The factorisation is computed, no warning appears, and the answer is wrong in the leading digits.
Partial pivoting fixes both at once by always choosing the largest available entry in the column as the pivot. Every multiplier is then at most one in absolute value, so errors are damped rather than amplified. The cost is a permutation to keep track of, which is why the identity is PA = LU rather than A = LU. This is the standard treatment in the Matrix Algebra for Engineers notes, which develop LU and the permuted form in consecutive chapters.
When No LU Factorisation Exists
The simplest matrix with no plain LU factorisation is a 2×2 with a zero in the top-left corner and non-zero entries elsewhere, such as 0, 1 / 1, 0. It is invertible, its determinant is −1, and yet no lower-triangular L with unit diagonal and upper-triangular U multiply to give it. The reason is arithmetic rather than deep: the top-left entry of LU is always the first pivot of U, so if A has zero there, U must have zero there too, and then the entire first column of LU is zero.
Swap the two rows and the obstruction vanishes: the permuted matrix is the identity, which factors trivially. This is the general fact — every invertible square matrix has a factorisation PA = LU for some permutation P, even when it has no A = LU. If the calculator above reports that the Doolittle factorisation does not exist, switching pivoting back on will produce the permuted one.
Singular matrices are a separate case again. A singular matrix can still be reduced to an upper triangular U, but U has a zero on its diagonal, the determinant is zero, and no forward-and-back substitution can solve the system uniquely. The tool reports the zero determinant and the row where elimination ran out of pivots. If you need to know how much of the matrix survived, our matrix rank calculator counts the pivots directly.
Doolittle, Crout and Cholesky
The unit diagonal has to go somewhere. Doolittle puts the ones on the diagonal of L, which is what this tool does and what almost all software assumes. Crout puts them on the diagonal of U instead and stores the scaling in L. The two factorisations contain the same information rearranged, and converting between them is a matter of dividing each row of U by its pivot and multiplying the corresponding column of L by it.
Cholesky is the specialised case worth knowing. When A is symmetric and positive definite, it factors as LLᵀ — one triangular matrix and its own transpose, no separate U at all. It costs half as much as LU, needs no pivoting to be stable, and fails precisely when the matrix is not positive definite, which makes it a practical test for that property. Covariance matrices are the common example, which is why Cholesky appears everywhere in statistics and finance.
A third variant, LDU, pulls the pivots out into a separate diagonal matrix D so that both L and U carry unit diagonals. It makes the symmetry of a symmetric matrix visible in the factors, since a symmetric A gives U = Lᵀ and A = LDLᵀ. For general work the Doolittle form is enough, and it is what pairs with the eigen-decomposition offered by our eigenvalue calculator when you need spectral information rather than a solve.
Arb Digital builds attribution and forecasting on numerically sound methods, and says so when the data cannot support the conclusion you were hoping for.
Browse All Free Tools Talk To Our TeamCommon Mistakes to Avoid
- Assuming every invertible matrix has A = LU — it does not, and a zero in a pivot position is enough to prevent it even when the determinant is far from zero.
- Forgetting to permute the right-hand side — with PA = LU you must solve Ly = Pb, not Ly = b, or the answer will be a rearrangement of nonsense.
- Storing the multipliers with the wrong sign — L holds the multiplier itself, not its negative, because A = LU reverses the subtractions elimination performed.
- Using a tiny pivot rather than swapping — a near-zero pivot produces enormous multipliers and destroys the significant digits of every row below it.
- Expecting the determinant sign to survive pivoting — each row swap flips it, so the sign is only meaningful once you have counted the swaps.
Related Free Tools From Arb Digital
Get the determinant on its own with the determinant calculator, build the full inverse with the matrix inverse calculator, solve the underlying system with the system of equations calculator, count pivots with the matrix rank calculator, or do the surrounding arithmetic on the matrix calculator. The full free online tools hub lists every mathematics tool we publish.
Frequently Asked Questions
U is the upper triangular matrix that Gaussian elimination leaves behind, and L is the lower triangular matrix holding the multipliers used during elimination, with ones along its diagonal. Multiplied together they reproduce the original matrix.
Because a zero, or a very small number, turned up in a pivot position. Swapping that row with one below it removes the obstruction, and P is the record of which rows were exchanged, giving PA = LU instead of A = LU.
Yes. The matrix with rows 0, 1 and 1, 0 is invertible but has no plain LU factorisation, because the top-left entry of any product LU is the first pivot and cannot be zero unless the whole first column is.
Multiply the diagonal entries of U, since L has ones on its diagonal and contributes a factor of one. Then flip the sign once for every row swap that the pivoting performed.
It chooses the largest available entry in each column as the pivot, which keeps every multiplier at one or below in size. That stops rounding errors from being amplified as elimination proceeds down the matrix.
When the matrix is symmetric and positive definite. Cholesky produces a single triangular factor and its transpose, costs about half as much as LU, and needs no pivoting to remain stable.
Because the factors can be reused. Solving one system directly costs the same as factoring, but each additional right-hand side then costs only two cheap triangular substitutions instead of a fresh elimination.
This page explains a mathematical method for study and for checking your own working. It is not a substitute for showing your method, and it is not medical, legal, or financial advice.