Last updated 13 November 2026
What are LU and QR decomposition?
LU decomposition writes a square matrix as , a lower-triangular matrix times an upper-triangular one, which is Gaussian elimination stored for reuse. QR decomposition writes any matrix as , an orthogonal matrix times an upper-triangular one, which is the numerically stable way to solve least-squares problems. Both turn one hard system into two easy triangular ones. On TensorTonic you implement LU decomposition, QR decomposition and Gram-Schmidt from scratch, with tests that check your output.
Why decompose matrices?
Solving directly by computing is expensive () and numerically unstable. Matrix decompositions factor into simpler matrices that make solving systems faster and more stable.
LU Decomposition
Factor into Lower × Upper triangular. Good for repeated solves with same A, different b. Analogy: Gaussian Elimination.
QR Decomposition
Factor into Orthogonal × Upper triangular. Numerically stable, ideal for least squares. Analogy: Gram-Schmidt.
LU Decomposition
Factor a square matrix into a product of Lower and Upper triangular matrices. It is essentially recording the steps of Gaussian Elimination.
Solving with LU
- Factor once: (O(n³))
- For each b, solve (forward substitution, O(n²))
- Then solve (back substitution, O(n²))
Partial Pivoting (PA = LU)
In practice, we permute rows to avoid division by small numbers. This gives where is a permutation matrix.
Interactive Simulator
Step through the LU and QR decomposition process. Watch how matrices are factored at each step.
Matrix Decomposition
Factorizing A into simpler components.
Setup
Start with A. Our goal is to transform A into Upper Triangular form (U) using row operations, while recording the multipliers in L.
QR Decomposition
Factor any matrix (even rectangular) into an Orthogonal matrix times an Upper triangular matrix.
Why QR is Stable
Orthogonal matrices preserve lengths: . This means errors do not amplify during computation (condition number of is 1).
Gram-Schmidt Process
The algorithm to construct is called Gram-Schmidt. It iteratively subtracts projections to force orthogonality.
1. Define Vectors
Start with two linearly independent vectors.
Initial Vectors
Current Operation
- , then
- , then
- , then
Modified Gram-Schmidt is more numerically stable. Modern libraries use Householder reflections (a sequence of mirrors) instead of projections to compute QR.
How do you compute LU and QR? Worked examples
LU of a 2×2 matrix
For , eliminate the entry below the first pivot by subtracting times row 1 from row 2. The multiplier goes into and the result of elimination is , so and . Multiplying them back gives , and solving now takes one forward substitution with and one back substitution with . Library routines also swap rows (partial pivoting) for stability, which this small example does not need.
QR of a 2×2 matrix with Gram-Schmidt
For , normalise the first column , whose length is 5, to get . The second column has a component of along ; removing it leaves , whose length is 0.4, so . The lengths and components form , and is orthogonal. NumPy's np.linalg.qr returns the same factors with both signs flipped, which is an equally valid QR decomposition.
Case Study: Bulb Characteristic Fitting
The Problem
You have voltage vs brightness data for a bulb. Fit a polynomial: brightness = a₀ + a₁V + a₂V². This is an overdetermined system (more data points than unknowns).
Using QR for Least Squares
- Build Vandermonde matrix:
- Compute
- Solve by back substitution (since R is triangular, this is easy)
Why Not Normal Equations?
For high degree polynomials, becomes ill-conditioned. QR avoids forming this product, preserving numerical stability.
LU vs QR: which should you use?
Use LU when the matrix is square and you need to solve many systems with the same matrix, because it is the fastest factorization and can be reused for every new right-hand side. Use QR for least squares and other problems with more equations than unknowns, because its orthogonal factor keeps the computation numerically stable. SVD is slower again but works on any matrix and reveals its rank, so the table includes it for comparison.
| LU | QR | SVD | |
|---|---|---|---|
| Matrix Shape | Square only | Any shape | Any shape |
| Speed | Fastest | Medium | Slowest |
| Stability | Needs pivoting | Good | Best (Total Least Squares) |
| Best For | Linear solves (Ax=b) | Least squares | Rank, compression |
ML Applications
Cholesky (LLᵀ)
For symmetric positive definite matrices (like covariance matrices), Cholesky is 2x faster than LU. Used in Gaussian Processes for sampling.
Eigenvalue Algorithms
The QR Algorithm for eigenvalues repeatedly computes QR decompositions. This is the standard way numpy.linalg.eig works for non-symmetric matrices.
Backpropagation Efficiency
Computing gradients through matrix inverses involves solving linear systems, which typically uses pre-computed LU factors. Frameworks like JAX exploit this for efficiency.
Randomized Linear Algebra
Large scale ML uses randomized QR (sampling columns) to get approximate decompositions. This is much faster than full SVD for massive recommendation system matrices.
LU and QR are two of the main matrix factorizations; the third, the singular value decomposition (SVD), works on any matrix and gives the best low-rank approximation.
Frequently asked questions about LU and QR decomposition
When should you use LU instead of QR?
Use LU when the matrix is square and you need to solve many systems with the same matrix, because it is the fastest factorization and can be reused for every new right-hand side. Use QR for least squares and other problems with more equations than unknowns, because its orthogonal factor keeps the computation numerically stable.
Why is QR decomposition numerically stable?
Orthogonal matrices preserve lengths, so multiplying by Q does not amplify errors and its condition number is 1. For least squares, QR also avoids forming AᵀA, which becomes ill-conditioned for problems such as high-degree polynomial fits.
Practice this
On TensorTonic you implement this from scratch and test it in the browser.
- LU Decomposition: factor a square matrix into L and U.
- QR Decomposition: factor a matrix into an orthogonal Q and triangular R.
- Gram-Schmidt Orthogonalization: the process behind the QR example above.
- Least Squares Solution: where QR is used in practice.
- Solve Linear System: where LU is used in practice.