Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Lower-Upper Decomposition






Gaussian Elimination as a Matrix Factorization

The LU decomposition factors a square matrix into a lower triangular factor L and an upper triangular factor U. The upper factor is the row echelon form; the lower factor stores the multipliers used to get there. Once computed, the factorization converts every subsequent system solve into two cheap triangular substitutions — making LU the workhorse of direct linear system solvers.

For step-by-step results on your own numbers, use the LU decomposition and LDU decomposition calculators on CalculateMatrix.



What LU Decomposition Is

Gaussian elimination solves Ax = b — but every new right-hand side b starts the elimination over from the beginning.
When the same matrix AA meets many different right-hand sides — the same structure under different loads, the same circuit under different voltages — the row operations on AA are identical every time. Only b\mathbf{b} changes, yet the whole elimination is repeated.
For A with rows (2, 1), (6, 4), one row operation does all the work: subtract 3 times row 1 from row 2.
piecerowswhere it comes from
A(2, 1), (6, 4)the matrix to be eliminated
row operationrow 2 − 3 · row 1the multiplier 3 clears the 6
U(2, 1), (0, 1)the result of elimination: upper triangular
L(1, 0), (3, 1)the multiplier 3 kept below a diagonal of ones
L · U(2, 1), (6, 4)multiplying back returns A
The multiplier and the result are all that elimination produces — keep both, and A is rebuilt as their product.
Row 1 of L⋅UL \cdot U is 1⋅(2,1)=(2,1)1 \cdot (2, 1) = (2, 1); row 2 is 3⋅(2,1)+1⋅(0,1)=(6,4)3 \cdot (2, 1) + 1 \cdot (0, 1) = (6, 4). The elimination has been stored as two triangular matrices, ready to be reused for any b\mathbf{b}.
The definition
The LU decomposition writes a square matrix as A=LUA = LU, with LL unit lower triangular, holding the elimination multipliers, and UU upper triangular, the result of elimination.
The LU decomposition writes an n×nn \times n matrix AA as
LU Decomposition
A=LUA = LU
where LL is lower triangular with ones on the diagonal (unit lower triangular) and UU is upper triangular. The matrix UU is the row echelon form of AA, and LL stores the multipliers that Gaussian elimination used to produce it.
So AA with rows (2,1)(2, 1), (6,4)(6, 4) is LULU with LL rows (1,0)(1, 0), (3,1)(3, 1) and UU rows (2,1)(2, 1), (0,1)(0, 1).
LU is elimination saved for reuse.
The factorization captures the entire elimination process in a reusable form. Instead of performing elimination from scratch for every new right-hand side b\mathbf{b}, the work is done once (producing LL and UU) and reused cheaply for each solve.
A3×3211433879=L3×3100210431·U3×3211011002
A = LU, complete

The finished factorization: L below, with ones on its diagonal and the elimination multipliers beneath; U above, the upper triangular result of elimination, with its diagonal — the pivots — marked. Multiplying the two back together returns A. Build it one elimination step at a time on the LU decomposition visualizer.

Everything elimination does is recorded in these two triangles, so it never has to be done twice.
Not every matrix has an LU decomposition without row swaps.
For the matrix with rows (0,1)(0, 1), (1,0)(1, 0) the first pivot is 00, so elimination cannot even start. Swapping the rows first fixes it, and the swap is recorded as described in partial pivoting.
The rest of the page covers notation, construction from Gaussian elimination, the structure of L and U, when LU exists without pivoting, partial pivoting, solving systems, the determinant and inverse, a worked 4×4 example, and the computational cost.

LU Notation

Notation

LU Notation

Two letters chosen for what they look like, a diagonal of ones nobody writes down as a choice, and a permutation whose meaning depends entirely on which side of AA it stands.
The echelon form that UU turns out to be, and the pivot vocabulary that comes with it, are set out at row echelon form. The row operations themselves, their arrow convention and the augmented bar belong to Gaussian elimination, and the general habit of naming a factorisation by its factors is described at the singular value decomposition.
A=LUA = LU
A equals L U — A written as a lower triangular matrix times an upper triangular one
The letters are mnemonic rather than arbitrary: LL is lower, UU is upper. That is unusual. Most factorisations pick letters for the role a factor plays, and here they name the shape instead, which is why the equation can be read off without a legend.
CasesThe two factors are not interchangeable. ULUL is a different product describing a different matrix, and the order in A=LUA = LU is fixed by the elimination that produced it — the lower factor records what was done, the upper factor records what was left.
Also writtenGerman-language texts write A=LRA = LR, keeping LL and taking RR from the word for the right-hand, upper triangle. The three-factor spelling A=LDUA = LDU pulls the pivots out into a diagonal DD so that both triangular factors carry ones.
Do not confuseTreating LULU as an expression waiting to be multiplied out. The point of the factorisation is that the pair is kept and reused; multiplying it back to AA is only ever a check.
Same glyph elsewhereThe letter UU names the orthogonal left factor at the singular value decomposition, where the clash with upper triangular is set out.
L=(100l2110l31l321)L = \begin{pmatrix} 1 & 0 & 0 \\ l_{21} & 1 & 0 \\ l_{31} & l_{32} & 1 \end{pmatrix}
L is unit lower triangular — ones along the diagonal, multipliers below it
Unit here refers to the diagonal entries being 11 and to nothing else. The ones are a normalisation that has to be supplied from outside, because A=LUA = LU on its own does not determine the factors: for any invertible diagonal DD, the pair LDLD and D−1UD^{-1}U multiplies to the same AA. Fixing LL to carry ones removes the freedom and makes the factorisation unique.
CasesThe three-factor form A=LDUA = LDU makes the choice visible instead of silent, putting the pivots in DD and leaving both triangular factors with unit diagonals.
Also writtenSome treatments normalise UU instead, giving UU the ones and leaving the pivots in LL. Both conventions produce a correct A=LUA = LU, and they produce different LL and UU.
Do not confuseReading unit as unit length or orthonormal columns, neither of which is claimed. The second misreading is expecting UU to have ones as well: the diagonal of UU holds the pivots, and it is their product that gives the determinant.
PA=LUPA = LU
P A equals L U — permute the rows of A, then factor the result
Which side PP stands on is the whole content of the line. On the left it multiplies the rows of AA, and the equation asserts an order of events: the swaps are decided first, and elimination runs on the reordered matrix. The factorisation belongs to PAPA, not to AA.
CasesRearranged forms say the same thing: since PP is a permutation, P−1=PTP^{-1} = P^{T}, so A=PTLUA = P^{T}LU. Numerical libraries commonly return the transposed permutation and report A=PLUA = PLU — the same letter naming the inverse matrix, which is worth checking before mixing output from two sources.
Also writtenThe row swaps are sometimes reported as a list of indices rather than a matrix, which stores the same information without committing to a side.
Do not confuseDropping PP once the factors are in hand. Solving Ax=bA\mathbf{x} = \mathbf{b} requires the right-hand side to be permuted too, so forward substitution runs on PbP\mathbf{b}; using b\mathbf{b} untouched silently solves a system with its rows in the wrong order.
Same glyph elsewherePP carries the eigenvectors at diagonalization, where it is an arbitrary invertible matrix rather than a rearrangement of rows.
lij,i>jl_{ij}, \quad i > j
the entry of L in row i, column j, below the diagonal
These entries are a transcript, not a computation on AA. Each one is the multiplier that cleared position (i,j)(i, j) during forward elimination, so LL is a record of the procedure and UU is its result.
CasesThe sign follows subtraction. Subtracting 22 times row 11 from row 22 stores +2+2; adding row 11 to row 33 stores −1-1. The number in LL therefore reads with the opposite sign to the operation as it is usually spoken.
Also writtenThe multipliers are written mijm_{ij} while elimination is being described and lijl_{ij} once they have been placed in the matrix — two names for one number at two moments.
Do not confuseReading the packed storage as a matrix. Since the ones in LL and the zeros in UU carry no information, software keeps both factors in one n×nn \times n array with the multipliers in the lower triangle and UU in the upper. Printed, it looks like a single matrix and is neither factor.
DownloadPNG2×PDF
Notation · Cheat sheet
Decomposition Notation at a Glance
The marks from the notation cards across the decomposition pages: how each is read and what it means. Every group links to the full card on its page.
LU decompositionThis page
A=LUA = LU
A equals L U
Lower triangular times upper triangular.
PA=LUPA = LU
permute, then factor
PP on the left reorders the rows of AA.
lij,  i>jl_{ij}, \; i > j
entries of L below the diagonal
The multipliers used in elimination; ones on the diagonal.
QR decomposition
A=QRA = QR
A equals Q R
Orthonormal columns times upper triangular; Q1R1Q_1R_1 is the thin form.
QTQ=IQ^{T}Q = I
but not always Q Q transpose
For a thin QQ, QQT≠IQQ^{T} \neq I.
Ak+1=RkQkA_{k+1} = R_kQ_k
the QR iteration
Factor, then multiply back in reverse order.
Cholesky decomposition
A=LLTA = LL^{T}
L times L transpose
One factor, used twice; LL≠ALL \neq A.
xTAx>0\mathbf{x}^{T}A\mathbf{x} > 0
positive definite
Required for all x≠0\mathbf{x} \neq \mathbf{0}.
A=LDLTA = LDL^{T}
L D L transpose
The diagonal carried separately; no square roots.
Spectral decomposition
A=QDQTA = QDQ^{T}
Q D Q transpose
For symmetric AA: QTQ^{T} stands where Q−1Q^{-1} would be.
∑λiqiqiT\sum \lambda_i\mathbf{q}_i\mathbf{q}_i^{T}
a sum of rank-one terms
The same matrix, written by eigenvalues.
Singular value decomposition
A=UΣVTA = U\Sigma V^{T}
U Sigma V transpose
Orthogonal UU and VV, diagonal Σ\Sigma.
Σ\Sigma
capital sigma
A diagonal matrix here, not a sum.
σ1≥σ2≥⋯≥0\sigma_1 \geq \sigma_2 \geq \cdots \geq 0
the singular values
σ1\sigma_1 always means the largest.
Same marks, different job
Σ\SigmaA diagonal matrix in the SVD; elsewhere the summation sign.
QQAlways orthonormal columns; QTQ^{T} then acts as the inverse.
LLLower triangular in both LU and Cholesky, but with ones on the diagonal only in LU.
Decomposition Notation at a Glance·/linear-algebra/decompositions/lower-upperLearn Math Class

Construction from Gaussian Elimination

Fact: An astronomer saw the product first
Elimination was in use for centuries before anyone wrote it as a product of two triangular matrices. The Polish astronomer Tadeusz Banachiewicz did so in 1938, and Alan Turing's 1948 paper on rounding errors recast elimination as A=LDUA = LDU.

Gaussian elimination is usually carried out as a list of row operations on an augmented matrix, used once to reach a solution and then set aside. Read as matrix multiplications instead, the same operations turn the procedure into a statement about AA itself: a product of two triangular matrices.

Forward elimination on AA applies a sequence of row-addition operations, each represented by an elementary matrix EiE_i. The product Ek⋯E2E1A=UE_k \cdots E_2 E_1 A = U reduces AA to upper triangular form.

Rearranging: A=E1−1E2−1⋯Ek−1UA = E_1^{-1} E_2^{-1} \cdots E_k^{-1} U. Each EiE_i is a lower triangular matrix that adds a multiple of one row to a row below. Its inverse simply negates the multiplier. The product L=E1−1E2−1⋯Ek−1L = E_1^{-1} E_2^{-1} \cdots E_k^{-1} is lower triangular, with the multipliers sitting in the sub-diagonal positions.

Worked Example


Construction from Gaussian Elimination
A=(21−1402−253)A = \begin{pmatrix} 2 & 1 & -1 \\ 4 & 0 & 2 \\ -2 & 5 & 3 \end{pmatrix}

Subtract 22 times row 11 from row 22 (multiplier m21=2m_{21} = 2) and add row 11 to row 33 (multiplier m31=−1m_{31} = -1):

Construction from Gaussian Elimination
(21−10−24062)\begin{pmatrix} 2 & 1 & -1 \\ 0 & -2 & 4 \\ 0 & 6 & 2 \end{pmatrix}

Add 33 times row 22 to row 33 (multiplier m32=−3m_{32} = -3):

Construction from Gaussian Elimination
U=(21−10−240014)U = \begin{pmatrix} 2 & 1 & -1 \\ 0 & -2 & 4 \\ 0 & 0 & 14 \end{pmatrix}

The multipliers fill LL: L=(100210−1−31)L = \begin{pmatrix} 1 & 0 & 0 \\ 2 & 1 & 0 \\ -1 & -3 & 1 \end{pmatrix}.

Verification: LU=(21−1402−253)=ALU = \begin{pmatrix} 2 & 1 & -1 \\ 4 & 0 & 2 \\ -2 & 5 & 3 \end{pmatrix} = A.

Pitfall: Storing the negated multiplier
Writing −2-2, 11, 33 below the diagonal because those are the amounts added gives L=(100−210131)L = \begin{pmatrix} 1 & 0 & 0 \\ -2 & 1 & 0 \\ 1 & 3 & 1 \end{pmatrix} and LU=(21−1−4−462−525)≠ALU = \begin{pmatrix} 2 & 1 & -1 \\ -4 & -4 & 6 \\ 2 & -5 & 25 \end{pmatrix} \neq A. The entries of LL are the multipliers m21=2m_{21} = 2, m31=−1m_{31} = -1, m32=−3m_{32} = -3.

Reading LL off the elimination takes no extra arithmetic. Each multiplier is filed at the position of the zero it created: m21m_{21} at (2,1)(2, 1), m31m_{31} at (3,1)(3, 1) and m32m_{32} at (3,2)(3, 2). Multiplying LL by UU then reverses the row operations one by one, which is why the product rebuilds AA exactly.

To compute the LU decomposition of your own matrix, use the matrix calculator.
A3×3211433879=L3×3100210431·U3×3211011002
The multiplier stored, not discarded

Ordinary elimination throws away the multiplier once the zero is in place. Here it is kept and written into the lower factor, so L records the elimination and U records the result of it. Nothing extra is computed; the same arithmetic is simply being saved. Follow both factors filling in on the LU decomposition visualizer.

The factorisation is therefore a by-product of elimination rather than a separate algorithm with its own cost.

The Structure of L and U

The lower factor LL is unit lower triangular: ones on the diagonal and multipliers below. Entry lijl_{ij} (with i>ji > j) is the multiplier used to eliminate position (i,j)(i, j) during forward elimination. The diagonal is always ones because no row scaling is performed — only row additions.

The upper factor UU is the echelon form: pivots on the diagonal, zeros below, and the result of all elimination steps. The diagonal entries of UU are the pivots, and their product gives the determinant: det⁡(A)=det⁡(L)det⁡(U)=1⋅u11u22⋯unn\det(A) = \det(L)\det(U) = 1 \cdot u_{11}u_{22}\cdots u_{nn}.

The factorization stores LL and UU compactly. Since LL has ones on the diagonal (which need not be stored) and UU has zeros below the diagonal (which need not be stored), both factors fit in a single n×nn \times n array: the lower triangle holds the multipliers and the upper triangle holds UU.

The two factors compare cleanly on the same set of attributes — diagonal, below diagonal, above diagonal, role — as the table below shows.
DownloadPNG2×PDF
Decompositions · LU

What each factor holds

Between them LL and UU store the whole elimination — UU is what was left, LL is what was done. Read region by region, neither factor wastes a position.

3factors
TypeConditionon the diagonalbelow itabove it
What the elimination produced
U — upper triangular§ 3the echelon form of AAthe pivots — ∏uii=±det⁡A\prod u_{ii} = \pm\det Azeros — what elimination removedthe surviving entries
What the elimination did
L — unit lower triangular§ 3the multipliers, in positionall ones — not storedthe multiplier ℓij=mij\ell_{ij} = m_{ij}zeros
What the pivoting recorded
P — permutation§ 5the row swaps, if anyones where no swap occurreda single one per rowa single one per row
The two triangles are complementary, so both fit in one n×nn \times n array with nothing overlapping: UU on and above the diagonal, LL strictly below, and the unit diagonal of LL left implicit because it is known. Storing a factorization therefore costs no more memory than storing AA did.
What each factor holds·/linear-algebra/decompositions/lower-upperLearn Math Class
The two triangles occupy complementary regions, which is not a coincidence but the reason the factorization is cheap to store. UU owns the diagonal and everything above it; LL owns everything strictly below; and the unit diagonal of LL need not be written down because it is known in advance. Both factors therefore fit into the same n×nn \times n array the original matrix occupied, with no position used twice and none left empty.

When LU Exists Without Pivoting

For an invertible matrix AA, the factorization A=LUA = LU without row swaps exists if and only if every leading principal submatrix of AA is nonsingular. The kk-th leading principal submatrix is the upper-left k×kk \times k block of AA, and its determinant must be nonzero for k=1,2,…,nk = 1, 2, \dots, n.

When a zero appears in a pivot position during elimination of an invertible matrix, no row-addition operation can produce a nonzero pivot — a row swap is required. The simple A=LUA = LU factorization breaks down, and the pivoted version PA=LUPA = LU is needed.

For example, A=(0110)A = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} has no LULU factorization without pivoting: the (1,1)(1,1) entry is zero, and no multiple of row 11 can create a nonzero pivot. But after swapping the two rows, elimination proceeds immediately.
Row additions can never turn a zero pivot into a usable one; for an invertible matrix, only a row swap can.

Partial Pivoting: PA = LU

Partial pivoting modifies the elimination process: at each step, the row with the largest absolute value in the current pivot column (among rows at or below the pivot position) is swapped into the pivot position. All row swaps are recorded in a permutation matrix PP.

The factorization becomes:

PA LU Partial Pivoting
PA=LUPA = LU

where PP is the product of all row-swap permutation matrices. This factorization exists for every invertible matrix — partial pivoting eliminates the restriction on leading principal submatrices.

Pitfall: Choosing the largest signed entry
In a pivot column holding 22 and −5-5, keeping 22 because 2>−52 > -5 gives the multiplier −5/2-5/2, larger than 11 in size. Partial pivoting compares ∣2∣|2| with ∣−5∣|-5|, swaps the −5-5 row up, and the multiplier becomes 2/(−5)=−2/52/(-5) = -2/5.

Partial pivoting also improves numerical stability. Small pivots amplify rounding errors (dividing by a number near zero magnifies imprecision), and selecting the largest available pivot keeps the multipliers in LL bounded by 11 in absolute value, limiting error accumulation.

In numerical software, LU with partial pivoting is the default — the unpivoted version A=LUA = LU is a theoretical simplification that is rarely used in practice.
PA3×3123012259=L3×3100?10??1·U3×3123012259
A row swap recorded in the permutation

A zero, or an uncomfortably small number, has appeared where a pivot was needed, so rows have been exchanged and the exchange logged in P. This is why the honest statement of the factorisation is PA = LU: without the permutation there are perfectly ordinary matrices that have no LU at all. Trigger a swap on the LU decomposition visualizer.

In practice pivoting is used even when it is not strictly required, because small pivots damage accuracy long before they reach zero.
Fact: The doubling bound is reached
For the matrix with 11 on the diagonal and in the last column and −1-1 everywhere below the diagonal, partial pivoting never swaps, and the last pivot doubles at every step to 2n−12^{n-1}. At n=60n = 60, solving with it in double precision gives answers wrong in the first digit, although the matrix is well conditioned.

Bounding the multipliers is not the same as bounding the entries. Each elimination step replaces an entry by itself minus a multiplier times an entry of the pivot row, and with every multiplier at most 11 in size, the largest entry in the remaining rows can at most double. Over the n−1n - 1 steps of an n×nn \times n factorization, that allows growth by a factor of up to 2n−12^{n-1}.

Large entries in UU matter because rounding errors are made relative to the numbers being handled. When the entries grow, the errors committed along the way grow with them, and the computed factors reproduce PAPA less accurately.

Solving Systems with LU

Given PA=LUPA = LU, the system Ax=bA\mathbf{x} = \mathbf{b} is solved in two steps:

LU Solve Steps
Ax=b  ⟺  {Ly=Pb(forward sub)Ux=y(back sub)A\mathbf{x} = \mathbf{b} \;\Longleftrightarrow\; \begin{cases} L\mathbf{y} = P\mathbf{b} & (\text{forward sub}) \\ U\mathbf{x} = \mathbf{y} & (\text{back sub}) \end{cases}

Forward substitution: solve Ly=PbL\mathbf{y} = P\mathbf{b} for y\mathbf{y}. Since LL is lower triangular, this starts at the top and works downward — each equation involves one new unknown plus previously solved values. Cost: O(n2)O(n^2).

Back substitution: solve Ux=yU\mathbf{x} = \mathbf{y} for x\mathbf{x}. Since UU is upper triangular, this starts at the bottom and works upward. Cost: O(n2)O(n^2).

Pitfall: Forgetting to permute the right-hand side
For A=(0231)A = \begin{pmatrix} 0 & 2 \\ 3 & 1 \end{pmatrix}, one swap gives L=IL = I and U=(3102)U = \begin{pmatrix} 3 & 1 \\ 0 & 2 \end{pmatrix}. Solving with b=(2,4)\mathbf{b} = (2, 4) instead of PbP\mathbf{b} gives x=(0,2)\mathbf{x} = (0, 2), but Ax=(4,2)A\mathbf{x} = (4, 2). With Pb=(4,2)P\mathbf{b} = (4, 2), x=(1,1)\mathbf{x} = (1, 1).

The factorization itself costs 23n3\frac{2}{3}n^3 operations. Each subsequent solve costs O(n2)O(n^2). For kk systems with the same coefficient matrix but different right-hand sides b1,…,bk\mathbf{b}_1, \dots, \mathbf{b}_k: factor once at cost 23n3\frac{2}{3}n^3, then solve kk times at cost 2kn22kn^2. When kk is large, the per-system cost drops to essentially O(n2)O(n^2) — far cheaper than kk independent eliminations at 23n3\frac{2}{3}n^3 each.
The cubic cost is paid once per matrix; every right-hand side after that costs only two triangular solves.

LU and the Determinant

The determinant of AA is a free byproduct of the LU factorization. Since det⁡(L)=1\det(L) = 1 (the diagonal is all ones) and det⁡(U)=u11u22⋯unn\det(U) = u_{11}u_{22}\cdots u_{nn} (the product of the diagonal for a triangular matrix), with pivoting each row swap flips the sign:

Determinant via LU
det⁡(A)=(−1)s∏i=1nuii\det(A) = (-1)^s \prod_{i=1}^{n} u_{ii}

where ss is the number of row swaps recorded in PP. Without pivoting (s=0s = 0), the sign factor drops and det⁡(A)=u11u22⋯unn\det(A) = u_{11}u_{22}\cdots u_{nn}.

Pitfall: Dropping the sign of the row swaps
For A=(0110)A = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}, one swap gives U=IU = I, and multiplying its pivots alone gives det⁡A=1\det A = 1. With s=1s = 1, det⁡A=(−1)1⋅1=−1\det A = (-1)^1 \cdot 1 = -1, which matches 0⋅0−1⋅10 \cdot 0 - 1 \cdot 1.

This makes determinant computation essentially free once LU is available — just multiply the diagonal of UU and account for the sign. It is far more efficient than cofactor expansion, and it is the method every numerical library uses.

LU and the Inverse

To compute A−1A^{-1}, solve Axj=ejA\mathbf{x}_j = \mathbf{e}_j for each standard basis vector ej\mathbf{e}_j, j=1,…,nj = 1, \dots, n. The solutions x1,…,xn\mathbf{x}_1, \dots, \mathbf{x}_n are the columns of A−1A^{-1}.

With LU: factor AA once (23n3\frac{2}{3}n^3), then solve nn systems (n×O(n2)=O(n3)n \times O(n^2) = O(n^3)). Total cost: roughly 83n3\frac{8}{3}n^3 counted directly, or 2n32n^3 when the zeros in the columns of II are used — cheaper than nn independent eliminations.

In practice, computing A−1A^{-1} explicitly is rarely the right approach. Solving Ax=bA\mathbf{x} = \mathbf{b} via LU costs about the same per right-hand side as multiplying A−1bA^{-1}\mathbf{b} (about 2n22n^2 each), but it never forms A−1A^{-1}, which costs about 2n32n^3 against 23n3\frac{2}{3}n^3 for the factorization, and it is more numerically stable. The inverse formula is primarily a theoretical tool; for computation, LU solves are preferred.

Worked Example: 4×4 with Pivoting

A=(0213103−131022310)A = \begin{pmatrix} 0 & 2 & 1 & 3 \\ 1 & 0 & 3 & -1 \\ 3 & 1 & 0 & 2 \\ 2 & 3 & 1 & 0 \end{pmatrix}


The (1,1)(1,1) entry is zero — a row swap is needed. The largest entry in column 11 is 33 in row 33. Swap rows 11 and 33:

Worked Example: 4×4 with Pivoting
(3102103−102132310)\begin{pmatrix} 3 & 1 & 0 & 2 \\ 1 & 0 & 3 & -1 \\ 0 & 2 & 1 & 3 \\ 2 & 3 & 1 & 0 \end{pmatrix}

Eliminate below the (1,1)(1,1) pivot. Multipliers: m21=1/3m_{21} = 1/3, m31=0m_{31} = 0, m41=2/3m_{41} = 2/3:

Worked Example: 4×4 with Pivoting
(31020−1/33−5/3021307/31−4/3)\begin{pmatrix} 3 & 1 & 0 & 2 \\ 0 & -1/3 & 3 & -5/3 \\ 0 & 2 & 1 & 3 \\ 0 & 7/3 & 1 & -4/3 \end{pmatrix}

The largest entry in column 22 below position (2,2)(2,2) is 7/37/3 in row 44. Swap rows 22 and 44. Continue elimination on columns 22 and 33 with the appropriate multipliers.

After completing all steps, the factorization PA=LUPA = LU is assembled with PP recording all three row swaps (column 33 needs one too, since 22/7>1/722/7 > 1/7), LL storing all multipliers, and UU storing the final upper triangular result. Verification: LU=PALU = PA.

Computational Cost

The LU factorization of an n×nn \times n matrix requires roughly 23n3\frac{2}{3}n^3 arithmetic operations (multiplications and additions). Each triangular solve (forward or back substitution) requires roughly n2n^2 operations.

For a single system Ax=bA\mathbf{x} = \mathbf{b}, LU costs about the same as Gaussian elimination applied directly. The advantage appears with multiple right-hand sides: kk systems sharing the same AA cost 23n3+2kn2\frac{2}{3}n^3 + 2kn^2, versus 23kn3\frac{2}{3}kn^3 for kk independent eliminations.

Compared to alternatives: Cramer's rule costs O(n⋅n!)O(n \cdot n!) — absurdly expensive. Explicit inverse computation costs roughly 2n32n^3. The Cholesky factorization costs 13n3\frac{1}{3}n^3 but requires symmetry and positive definiteness. LU is the general-purpose baseline — the standard direct solver for dense linear systems in scientific computing.

These options can be set side by side on cost per system, cost amortized over many systems, and the matrix conditions each requires.
Method Cost for one system Cost for k systems (same A) Restrictions
LU with partial pivoting ≈ 2n3/3 + O(n2) 2n3/3 + 2kn2 A square and invertible
Cholesky ≈ n3/3 + O(n2) n3/3 + 2kn2 A symmetric positive definite
Explicit inverse A−1b ≈ 8n3/3 to form A−1, then 2n2 per multiply 8n3/3 + 2kn2 numerically less stable; rarely the right choice
Cramer's rule O(n · n!) — impractical beyond n ≈ 4 k × O(n · n!) theoretical only; not used in computation

Summary: What LU Computes

Once PA=LUPA = LU is in hand, three of the most common matrix-level computations — solving a system, taking the determinant, and forming the inverse — collapse to cheap operations on the factors. The table below collects each task with the procedure it reduces to, the cost given the factorization, and the cost of doing the same task from scratch for comparison.
The split below is between paying once and paying per use. The factorization is the expensive step and happens a single time; everything after it is a triangular solve or a product along a diagonal. Reading the cost column downward shows why the decomposition is stored rather than recomputed — the first row is cubic, the rest are quadratic or less.
DownloadPNG2×PDF
Decompositions · LU

What LU computes, and at what cost

The factorization costs 23n3\tfrac{2}{3}n^3 once. Everything below reuses it, and the cost column is the reason the decomposition exists at all — each task afterwards is cheaper than redoing the elimination.

4tasks
The factorization itself1
1
once per matrix
23n3\tfrac{2}{3}n^3 flops
Gaussian elimination with the multipliers kept rather than discarded — LL holds what was subtracted, UU holds what remained, PP records the row swaps. The elimination was being done anyway; LU is the observation that its byproducts are worth storing.
Reusing it — cost per use3
2
forward solve Ly=PbL\mathbf{y} = P\mathbf{b}, then back solve Ux=yU\mathbf{x} = \mathbf{y}
2n22n^2 per right-hand side
Two triangular solves, each n2n^2. The saving arrives on the second right-hand side: a fresh elimination would cost 23n3\tfrac{2}{3}n^3 again, while reusing the factors costs n2n^2 — which is why LU is what a solver stores when the matrix is fixed and b\mathbf{b} varies.
3
ss = number of row swaps in PP
det⁡A=(−1)s∏uii\det A = (-1)^{s} \prod u_{ii}
Multiply the diagonal of UU and correct the sign for the swaps — O(n)O(n) once the factorization exists. Cofactor expansion is O(n!)O(n!) for the same number, which is the gap between a definition and a method.
4
solve Axi=eiA\mathbf{x}_i = \mathbf{e}_i for each ii
2n32n^3 total
nn solves at 2n22n^2 each. Worth doing only when the inverse is genuinely wanted as an object — if the goal is solving systems, the triangular solves above are three times cheaper and numerically better behaved.
The economics are the whole argument. One elimination at 23n3\tfrac{2}{3}n^3, then n2n^2 per right-hand side — so ten systems sharing a matrix cost barely more than one. Compare recomputing from scratch each time, and the saving is a factor of nn; compare inverting, and LU is three times cheaper and more accurate besides.
What LU computes, and at what cost·/linear-algebra/decompositions/lower-upperLearn Math Class
The break-even point arrives immediately. Solving one system by elimination costs 23n3\tfrac{2}{3}n^3 whether or not the factors are kept, so keeping them is free; the second right-hand side then costs 2n22n^2 instead of another 23n3\tfrac{2}{3}n^3. For a 1000×10001000 \times 1000 matrix that is two million operations against roughly seven hundred million — a factor of several hundred, on the same matrix, for a different b\mathbf{b}.
The last row is the one to read as a warning. Computing A−1A^{-1} through LU is perfectly possible and rarely the right thing to do: it costs three times a single solve, and using the inverse afterwards is both slower and less accurate than the two triangular solves it replaced. The inverse is worth forming when it is the answer, not when it is a step toward one.

Common Pitfalls

Four mistakes account for many wrong factorizations and wrong answers obtained from correct ones. Each is marked with a box where it happens in the sections above; here each is worked through in full.

Storing the negated multiplier

The row operation is written as "row 22 minus 22 times row 11", and the −2-2 from that operation is copied into LL. The same is done for every step, so every entry below the diagonal of LL has the wrong sign.

Wrong working: for the worked example, the operations add −2-2 times row 11 to row 22, 11 times row 11 to row 33 and 33 times row 22 to row 33. Storing those amounts gives
LU=(100−210131)(21−10−240014)=(21−1−4−462−525)≠ALU = \begin{pmatrix} 1 & 0 & 0 \\ -2 & 1 & 0 \\ 1 & 3 & 1 \end{pmatrix}\begin{pmatrix} 2 & 1 & -1 \\ 0 & -2 & 4 \\ 0 & 0 & 14 \end{pmatrix} = \begin{pmatrix} 2 & 1 & -1 \\ -4 & -4 & 6 \\ 2 & -5 & 25 \end{pmatrix} \neq A


Right working: LL holds the multipliers m21=2m_{21} = 2, m31=−1m_{31} = -1, m32=−3m_{32} = -3, the numbers that were subtracted.
LU=(100210−1−31)(21−10−240014)=(21−1402−253)=ALU = \begin{pmatrix} 1 & 0 & 0 \\ 2 & 1 & 0 \\ -1 & -3 & 1 \end{pmatrix}\begin{pmatrix} 2 & 1 & -1 \\ 0 & -2 & 4 \\ 0 & 0 & 14 \end{pmatrix} = \begin{pmatrix} 2 & 1 & -1 \\ 4 & 0 & 2 \\ -2 & 5 & 3 \end{pmatrix} = A


Why it happens: the elementary matrix E1E_1 of the first step really does carry −2-2 in position (2,1)(2, 1). But LL is built from the inverses Ei−1E_i^{-1}, and each inverse negates its multiplier.

How to check: the first column of LULU is u11u_{11} times the first column of LL, and it must equal the first column of AA. Here 2⋅(1,2,−1)=(2,4,−2)2 \cdot (1, 2, -1) = (2, 4, -2) is correct, while 2⋅(1,−2,1)=(2,−4,2)2 \cdot (1, -2, 1) = (2, -4, 2) is not.

Choosing the largest signed entry

Partial pivoting is applied by picking the largest number in the pivot column, comparing signed values instead of absolute values. A negative entry of large size is passed over.

Wrong working: for A=(21−53)A = \begin{pmatrix} 2 & 1 \\ -5 & 3 \end{pmatrix}, 2>−52 > -5, so row 11 stays as the pivot row. The multiplier is m21=−5/2m_{21} = -5/2, and
L=(10−5/21),U=(21011/2)L = \begin{pmatrix} 1 & 0 \\ -5/2 & 1 \end{pmatrix}, \quad U = \begin{pmatrix} 2 & 1 \\ 0 & 11/2 \end{pmatrix}

This is a valid A=LUA = LU, but not the partial-pivoting factorization: the multiplier exceeds 11 in size.

Right working: ∣−5∣>∣2∣|-5| > |2|, so the rows are swapped and PA=(−5321)PA = \begin{pmatrix} -5 & 3 \\ 2 & 1 \end{pmatrix}. The multiplier is m21=2/(−5)=−2/5m_{21} = 2/(-5) = -2/5, and
L=(10−2/51),U=(−53011/5)L = \begin{pmatrix} 1 & 0 \\ -2/5 & 1 \end{pmatrix}, \quad U = \begin{pmatrix} -5 & 3 \\ 0 & 11/5 \end{pmatrix}


Why it happens: "largest" is read as the largest number. When all entries in the column are positive the two readings agree, so the difference shows up only with negative entries.

How to check: every multiplier stored in LL must satisfy ∣lij∣≤1|l_{ij}| \le 1. A multiplier of larger size means a larger pivot was available and was skipped.

Forgetting to permute the right-hand side

After computing PA=LUPA = LU, forward substitution is run on b\mathbf{b} itself. The rows of AA were reordered, but the entries of b\mathbf{b} were not.

Wrong working: for A=(0231)A = \begin{pmatrix} 0 & 2 \\ 3 & 1 \end{pmatrix} and b=(2,4)\mathbf{b} = (2, 4), one swap gives P=(0110)P = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}, L=IL = I and U=(3102)U = \begin{pmatrix} 3 & 1 \\ 0 & 2 \end{pmatrix}. Solving Ly=bL\mathbf{y} = \mathbf{b} gives y=(2,4)\mathbf{y} = (2, 4). Back substitution gives 2x2=42x_2 = 4, so x2=2x_2 = 2, and 3x1+2=23x_1 + 2 = 2, so x1=0x_1 = 0. But A(0,2)=(4,2)≠bA(0, 2) = (4, 2) \neq \mathbf{b}.

Right working: Pb=(4,2)P\mathbf{b} = (4, 2), so y=(4,2)\mathbf{y} = (4, 2). Then 2x2=22x_2 = 2 gives x2=1x_2 = 1, and 3x1+1=43x_1 + 1 = 4 gives x1=1x_1 = 1. Check: A(1,1)=(2,4)=bA(1, 1) = (2, 4) = \mathbf{b}.

Why it happens: PP is used during the factorization and then put aside, and LL and UU look as if they factor AA itself. They factor PAPA.

How to check: substitute the answer into the original system Ax=bA\mathbf{x} = \mathbf{b}, not into LUx=bLU\mathbf{x} = \mathbf{b}.

Dropping the sign of the row swaps

The determinant is taken as the product of the pivots on the diagonal of UU, and the row swaps recorded in PP are ignored.

Wrong working: for A=(0231)A = \begin{pmatrix} 0 & 2 \\ 3 & 1 \end{pmatrix}, one swap gives U=(3102)U = \begin{pmatrix} 3 & 1 \\ 0 & 2 \end{pmatrix}, and the product of the pivots is 3⋅2=63 \cdot 2 = 6.

Right working: with s=1s = 1 swap, det⁡A=(−1)1⋅3⋅2=−6\det A = (-1)^1 \cdot 3 \cdot 2 = -6. Directly, det⁡A=0⋅1−2⋅3=−6\det A = 0 \cdot 1 - 2 \cdot 3 = -6.

Why it happens: without pivoting the determinant is exactly the product of the pivots, and that is the form most often remembered. The sign factor only appears once PP is in play.

How to check: count the swaps, or compute det⁡P\det P, which is +1+1 or −1-1. For a 2×22 \times 2 matrix, compare with ad−bcad - bc.

LU Decomposition FAQ

Do both LL and UU have ones on the diagonal?

+
Only LL does. The unit diagonal is a normalisation chosen to make the factorisation unique, and UU instead carries the pivots along its diagonal, whose product gives the determinant. Note that unit here means the entries equal one, with no claim about unit length or orthonormal columns.Read more →

Can you drop the permutation PP once you have LL and UU?

+
No, because PP recorded the row swaps and those swaps must reach the right-hand side too. Solving Ax=bA\mathbf{x} = \mathbf{b} from PA=LUPA = LU means running forward substitution on PbP\mathbf{b}, not on b\mathbf{b}. Using the untouched vector silently solves a system whose rows are in the wrong order.Read more →

Is ULUL the same as LULU?

+
No, they are different products describing different matrices, and the order in A=LUA = LU is fixed by the elimination that produced it: the lower factor records what was done, the upper factor what was left. The pair is also meant to be kept and reused, so multiplying back to AA is only ever a check.Read more →

What is the difference between LU decomposition and Gaussian elimination?

+
None in the arithmetic: LU is Gaussian elimination with the multipliers kept instead of thrown away. UU is the echelon form that elimination reaches, and LL stores each multiplier at the position of the zero it created. For a single system both cost about 23n3\frac{2}{3}n^3 operations. The difference shows with the next right-hand side, which costs only two triangular solves, about 2n22n^2, instead of a fresh elimination.Read more →

Does partial pivoting guarantee an accurate LU factorization?

+
No, though it rarely fails in practice. Pivoting keeps every multiplier in LL at most 11 in size, but an entry can still double at each step, so the entries of UU can grow by a factor of up to 2n−12^{n-1}, and rounding errors grow with them. The bound is reached by the matrix with ones on the diagonal and in the last column and −1-1 below the diagonal; at n=60n = 60 that growth ruins a double-precision solution.Read more →

Should you compute the inverse to solve Ax=bA\mathbf{x} = \mathbf{b}?

+
No, solve with the LU factors unless the inverse itself is the answer wanted. Forming A−1A^{-1} costs about 2n32n^3 operations, three times the 23n3\frac{2}{3}n^3 of the factorization, and multiplying A−1bA^{-1}\mathbf{b} afterwards costs as much as the two triangular solves it replaces while being less accurate. With the factors stored, every new right-hand side is just a forward and a back substitution.Read more →