LU Decomposition Calculator

Factor a matrix into lower and upper triangular matrices with pivoting and system solving.

Results are calculated automatically as you enter data.

Matrix A
Matrix Size:
3

DecompositionEnter a valid square matrix.
Row operations and verification

▼ See explanations and tips below ▼

What Is LU Decomposition?

LU decomposition is a way to rewrite a square matrix as the product of two simpler triangular matrices. The letters stand for lower and upper:

  • \(L\) is a lower triangular matrix, meaning its entries above the main diagonal are zero.
  • \(U\) is an upper triangular matrix, meaning its entries below the main diagonal are zero.

For many matrices, the basic idea is:

$$ A = LU $$

In practical numerical work, row swaps are often included. Those swaps are recorded in a permutation matrix \(P\), so the factorization is written as:

$$ PA = LU $$

This means that after the rows of \(A\) are rearranged according to \(P\), the rearranged matrix can be rebuilt by multiplying \(L\) and \(U\).

LU decomposition is closely connected to Gaussian elimination. Gaussian elimination turns a matrix into an upper triangular form. LU decomposition keeps track of that process in a reusable form: the multipliers used during elimination become entries of \(L\), while the final upper triangular matrix becomes \(U\).


Why LU Decomposition Matters

LU decomposition matters because triangular systems are much easier to solve than a general system of linear equations. Instead of solving \(Ax=b\) directly every time, a matrix can be factored once and then reused.

This is especially useful when the same coefficient matrix \(A\) is paired with different right-hand-side vectors \(b\). For example, engineers, scientists, and students may solve several related systems where the relationships between variables stay the same but the input values change.

LU decomposition also helps with:

  • solving systems of linear equations,
  • understanding the structure of Gaussian elimination,
  • computing determinants efficiently,
  • checking whether row swaps were needed,
  • studying numerical methods and matrix algorithms.

For small classroom examples, LU decomposition is mainly a learning tool. For larger numerical problems, it is one of the standard building blocks behind many linear algebra solvers.


Key Terms to Know

  • Square matrix: A matrix with the same number of rows and columns. LU factorization for solving \(Ax=b\) is usually discussed for square coefficient matrices.
  • Lower triangular matrix: A matrix whose entries above the main diagonal are zero.
  • Upper triangular matrix: A matrix whose entries below the main diagonal are zero.
  • Unit lower triangular matrix: A lower triangular matrix whose diagonal entries are all \(1\).
  • Permutation matrix: A matrix that represents row swaps. Multiplying by \(P\) rearranges the rows of another matrix.
  • Pivot: The entry used to eliminate values below it during Gaussian elimination.
  • Partial pivoting: A row-swap strategy that chooses a strong pivot from the current column, usually by selecting the largest available absolute value in that column.
  • Forward substitution: The process of solving a lower triangular system such as \(Ly = Pb\).
  • Back substitution: The process of solving an upper triangular system such as \(Ux = y\).
  • Relative reconstruction error: A scale-adjusted numerical check of how closely \(LU\) matches \(PA\) after the factorization is computed.

How LU Decomposition Works

LU decomposition follows the same basic logic as elimination. At each step, a pivot is chosen, entries below that pivot are eliminated, and the multiplier used for each elimination is stored.

For a pivot in column \(k\), the multiplier for a lower row \(i\) is:

$$ m_{ik} = \frac{u_{ik}}{u_{kk}} $$

The row operation then subtracts that multiple of the pivot row:

$$ R_i \leftarrow R_i - m_{ik}R_k $$

Those multipliers become the entries of \(L\) below the diagonal. The matrix left after the eliminations becomes \(U\).

When row swaps are used, the factorization is written as:

$$ PA = LU $$

Where:

  • \(A\) is the original square matrix.
  • \(P\) records the row swaps.
  • \(L\) stores the elimination multipliers and has \(1\) on its diagonal.
  • \(U\) is the upper triangular matrix produced by elimination.

If no row swaps are needed, \(P\) is the identity matrix and the relationship reduces to:

$$ A = LU $$

Solving \(Ax=b\) after LU decomposition

When the goal is to solve a system of equations, start with:

$$ Ax = b $$

With pivoting, multiply both sides by \(P\):

$$ PAx = Pb $$

Since \(PA = LU\), this becomes:

$$ LUx = Pb $$

Now solve two simpler triangular systems:

$$ Ly = Pb $$

Then:

$$ Ux = y $$

The first system is solved by forward substitution. The second is solved by back substitution.

Computing the determinant from LU decomposition

For a triangular matrix, the determinant is the product of the diagonal entries. Because a unit lower triangular matrix has diagonal entries all equal to \(1\), its determinant is \(1\).

If \(s\) row swaps were used, the determinant of \(A\) can be computed from \(U\) as:

$$ \det(A) = (-1)^s \prod_{i=1}^{n} u_{ii} $$

Each row swap changes the sign of the determinant. That is why the sign depends on whether the number of swaps is even or odd.


Examples of LU Decomposition in Practice

Example 1: A simple \(2 \times 2\) factorization

Suppose:

$$ A = \begin{bmatrix} 2 & 1 \\ 4 & 3 \end{bmatrix} $$

Eliminate the entry below the first pivot. The multiplier is:

$$ m_{21} = \frac{4}{2} = 2 $$

So one valid factorization is:

$$ L = \begin{bmatrix} 1 & 0 \\ 2 & 1 \end{bmatrix}, \qquad U = \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix} $$

Check the product:

$$ LU = \begin{bmatrix} 1 & 0 \\ 2 & 1 \end{bmatrix} \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix} = \begin{bmatrix} 2 & 1 \\ 4 & 3 \end{bmatrix} =A $$

The determinant is the product of the diagonal entries of \(U\) because no row swap was used:

$$ \det(A) = 2 \times 1 = 2 $$

Example 2: Solving a system with forward and back substitution

Use the same matrix:

$$ A = \begin{bmatrix} 2 & 1 \\ 4 & 3 \end{bmatrix} $$

Let:

$$ b = \begin{bmatrix} 5 \\ 11 \end{bmatrix} $$

Since \(A=LU\), solve:

$$ Ly=b $$

That is:

$$ \begin{bmatrix} 1 & 0 \\ 2 & 1 \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \end{bmatrix} = \begin{bmatrix} 5 \\ 11 \end{bmatrix} $$

The first equation gives \(y_1=5\). The second gives:

$$ 2(5)+y_2 = 11 $$

So:

$$ y_2 = 1 $$

Now solve:

$$ Ux=y $$
$$ \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} 5 \\ 1 \end{bmatrix} $$

The second equation gives \(x_2=1\). The first equation gives:

$$ 2x_1 + 1 = 5 $$

So:

$$ x_1 = 2 $$

The solution is:

$$ x = \begin{bmatrix} 2 \\ 1 \end{bmatrix} $$

Example 3: A zero pivot that needs row swapping

A zero entry on the diagonal does not always mean the matrix is singular. Sometimes the rows only need to be swapped.

Suppose:

$$ A = \begin{bmatrix} 0 & 2 \\ 1 & 3 \end{bmatrix} $$

The first diagonal entry is \(0\), so elimination without pivoting would immediately run into a division by zero. Partial pivoting swaps the two rows:

$$ P = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} $$

Then:

$$ PA = \begin{bmatrix} 1 & 3 \\ 0 & 2 \end{bmatrix} $$

In this case:

$$ L = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}, \qquad U = \begin{bmatrix} 1 & 3 \\ 0 & 2 \end{bmatrix} $$

There was one row swap, so:

$$ \det(A) = (-1)^1(1 \times 2) = -2 $$

The row swap made the factorization possible even though the original first pivot was zero.


How to Interpret the Result

The result should be read as a set of connected matrix facts, not just as separate numbers.

\(P\), \(L\), and \(U\)

The factorization \(PA=LU\) means that the row-permuted version of \(A\) is reconstructed by multiplying \(L\) and \(U\).

If \(P\) is the identity matrix, no row swaps were used. In that case, the relationship is simply \(A=LU\).

The matrix \(L\) shows the elimination multipliers. The matrix \(U\) shows the upper triangular result after elimination.

Determinant

The determinant is derived from the diagonal entries of \(U\), with a sign correction for row swaps. The calculator accumulates its sign and base-10 magnitude separately, so determinants outside the ordinary floating-point range can still be displayed in scientific notation. A determinant of exactly zero means the matrix is singular.

A very small determinant may deserve caution, but it should not be treated as a complete measure of numerical reliability by itself. Matrix scaling and conditioning also matter.

Relative reconstruction error

The relative reconstruction error compares the largest entrywise difference between \(PA\) and \(LU\) with the size of the matrices:

$$ \frac{\max_{i,j}\left|(PA)_{ij}-(LU)_{ij}\right|} {\max\left(\max_{i,j}|(PA)_{ij}|,\max_{i,j}|(LU)_{ij}|\right)} $$

A smaller value means the displayed factors reconstruct the permuted matrix more closely under floating-point arithmetic. Dividing by the matrix scale makes this check meaningful for both very small and very large inputs.

Solution vector \(x\)

When a valid right-hand-side vector \(b\) is provided, the solution vector \(x\) represents the values that satisfy:

$$ Ax=b $$

The solution is found by solving \(Ly=Pb\) and then \(Ux=y\). If no \(b\) vector is provided, the factorization and determinant can still be useful, but there is no linear system solution to display.

For a solved system, the scaled residual compares \(\|Ax-b\|_\infty\) with \(\|A\|_\infty\|x\|_\infty+\|b\|_\infty\). The condition estimate and pivot-growth value provide additional warnings when a small residual alone could be misleading.

If the matrix is singular, the calculator does not claim a unique solution. With a supplied \(b\), scaled rank estimates classify the system as inconsistent or as having infinitely many solutions.


Common Mistakes and Misconceptions

Pasting a rectangular matrix

LU decomposition for this kind of calculator requires a square matrix. A matrix with unequal row and column counts does not fit the expected input format.

Entering the wrong number of \(b\) values

The right-hand-side vector must match the matrix size. A \(3 \times 3\) matrix needs exactly three values in \(b\); a \(6 \times 6\) matrix needs exactly six.

Leaving cells blank accidentally

Blank matrix cells are treated as \(0\). This can be useful when the zero is intentional, but it can change the problem completely if a blank cell was accidental.

Turning off pivoting when a pivot is zero or tiny

Without partial pivoting, a matrix can fail even when a row swap would make the calculation proceed. Partial pivoting is usually the safer choice for numerical work.

Expecting exact fractions or symbolic answers

Numerical LU decomposition uses decimal arithmetic. It may display values such as \(0.3333333\) instead of an exact fraction such as \(\frac{1}{3}\).

Reading reconstruction error as the determinant

The determinant and relative reconstruction error measure different things. The determinant is a property of the matrix. Relative reconstruction error is a scale-adjusted numerical check on how closely the computed factors reproduce \(PA\).

Assuming every row operation is shown

For larger matrices, there may be many elimination steps. A step display may show only the first part of the process and summarize additional operations.


When to Use LU Decomposition

Use LU decomposition when you want to:

  • solve a square system of linear equations \(Ax=b\),
  • reuse the same matrix \(A\) with different right-hand-side vectors,
  • understand how Gaussian elimination can be stored as matrix factors,
  • compute a determinant from triangular factors,
  • study pivoting, row swaps, and numerical stability,
  • check whether a matrix factorization reconstructs the original matrix accurately.

For hand calculations, LU decomposition is most comfortable with small matrices. For numerical computing, it is a general-purpose tool used in many algorithms for dense linear systems.


Limitations and Things to Keep in Mind

LU decomposition is powerful, but the result depends on the assumptions behind the calculation.

This calculator works with square real-valued matrices from \(2 \times 2\) through \(6 \times 6\). It does not support rectangular matrices, complex entries, symbolic variables, exact rational arithmetic, or matrices outside that size range.

All entries must be finite real decimal numbers. Standard scientific notation such as -1.5e-3 is accepted. Values such as hexadecimal notation, NaN, infinity, fractions written with a slash, non-numeric text, or symbolic expressions are rejected.

The calculation uses floating-point arithmetic, so displayed decimals are numerical approximations. Small nonzero values are retained and shown in scientific notation rather than being replaced by zero.

An exact zero pivot is treated as a singularity or, when pivoting is disabled and a row swap could help, as a no-pivoting method breakdown. Small nonzero pivots are retained and generate scale-aware conditioning warnings instead of an absolute-threshold rejection.

Partial pivoting can improve robustness, especially when a pivot is zero or very small, but it is not the same as full pivoting or column pivoting. Some difficult or ill-conditioned matrices may still produce results that need careful interpretation.

Relative reconstruction error, scaled solution residual, condition estimate, and pivot growth are numerical diagnostics rather than proofs of exact accuracy. Extremely ill-conditioned problems may still lose meaningful digits.


How to Use This Calculator

  1. Choose the square matrix size, from \(2 \times 2\) to \(6 \times 6\).
  2. Enter the values of matrix \(A\), or paste square matrix data into the import area.
  3. Choose the pivoting option. Partial pivoting is usually preferred when row swaps may be needed.
  4. Optionally enter a \(b\) vector with exactly one value for each row of \(A\).
  5. Review \(P\), \(L\), \(U\), the determinant, relative reconstruction error, condition estimate, pivot growth, row-operation steps, and the solution and scaled residual if \(b\) was provided.

Frequently Asked Questions

Is LU decomposition the same as Gaussian elimination?

Not exactly. Gaussian elimination is the process of eliminating entries below pivots to form an upper triangular matrix. LU decomposition stores that elimination process as matrix factors, with the elimination multipliers in \(L\) and the upper triangular result in \(U\).


Why does the formula use \(PA=LU\) instead of just \(A=LU\)?

The matrix \(P\) appears when row swaps are used. Row swaps help avoid zero or very small pivots. If no row swaps are needed, \(P\) is the identity matrix and \(PA=LU\) reduces to \(A=LU\).


Why can a matrix fail without pivoting but work with partial pivoting?

Without pivoting, elimination must use the current diagonal entry as the pivot. If that pivot is zero or too small, the calculation can fail or become unreliable. Partial pivoting looks lower in the same column for a better pivot and swaps rows when needed.


How is the determinant found from LU decomposition?

The determinant of a triangular matrix is the product of its diagonal entries. Since \(L\) has diagonal entries equal to \(1\), the determinant mainly comes from the diagonal of \(U\). Row swaps change the sign, so the final determinant is adjusted by \((-1)^s\), where \(s\) is the number of swaps.


Does a small reconstruction error guarantee the solution is accurate?

No. A small relative reconstruction error means \(LU\) closely reproduces \(PA\), but an ill-conditioned matrix can amplify small input or rounding errors. Review the scaled \(Ax-b\) residual, condition estimate, and pivot-growth warning as well.


Can LU decomposition be reused for different \(b\) vectors?

Yes. Once \(A\) is factored, each new \(b\) vector can be handled by solving \(Ly=Pb\) and then \(Ux=y\). This is one reason LU decomposition is useful in numerical methods.


Sources and References

Books

  1. Gilbert Strang. Introduction to Linear Algebra. 5th edition, Wellesley-Cambridge Press, 2016. Relevant sections: 2.2 “The Idea of Elimination,” 2.3 “Elimination Using Matrices,” 2.6 “Elimination = Factorization: A = LU,” 2.7 “Transposes and Permutations,” 5.1 “The Properties of Determinants,” and 11.1 “Gaussian Elimination in Practice.” Book information and table of contents: https://math.mit.edu/~gs/linearalgebra/ila5/indexila5.html
  2. Lloyd N. Trefethen and David Bau III. Numerical Linear Algebra: Twenty-Fifth Anniversary Edition. SIAM, 2022. Relevant lectures/chapters: “Stability of Back Substitution,” “Systems of Equations,” “Gaussian Elimination,” “Pivoting,” and “Stability of Gaussian Elimination.” Publisher page: https://epubs.siam.org/doi/10.1137/1.9781611977165

Online and Educational Sources

  1. MIT OpenCourseWare. “Lecture 2: Elimination with matrices.” 18.06 Linear Algebra, Spring 2010. Accessed July 4, 2026. https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/resources/lecture-2-elimination-with-matrices/
  2. MIT OpenCourseWare. “Lecture 4: Factorization into A = LU.” 18.06 Linear Algebra, Spring 2010. Accessed July 4, 2026. https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/resources/lecture-4-factorization-into-a-lu/
  3. University of Illinois Urbana-Champaign, CS 357. “LU Decomposition for Solving Linear Equations.” Accessed July 4, 2026. https://courses.grainger.illinois.edu/cs357/sp2022/notes/ref-9-linsys.html
  4. LAPACK. “DGETRF2: LU factorization using partial pivoting with row interchanges.” LAPACK 3.12.1 documentation. Accessed July 4, 2026. https://www.netlib.org/lapack/explore-html/d8/d2f/group__getrf2_gaebbbe6a533dfea4454123e0553bd3575.html