Gradium × TensorTonic Voice Challenge is open for registration.Register now
Modules
06/11
Linear Algebra

Contents

LU / QR Decomposition

Breaking matrices into simpler factors for efficient computation.

By Pratham Grover

Last updated 13 November 2026

What are LU and QR decomposition?

LU decomposition writes a square matrix as A=LUA = LU, a lower-triangular matrix times an upper-triangular one, which is Gaussian elimination stored for reuse. QR decomposition writes any matrix as A=QRA = QR, 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 Ax=bAx = b directly by computing A−1A^{-1} is expensive (O(n3)O(n^3)) and numerically unstable. Matrix decompositions factor AA 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.

A=LUA = LU
LL = lower triangular (1s on diagonal), UU = upper triangular (pivots on diagonal)

Solving with LU

  1. Factor once: A=LUA = LU (O(n³))
  2. For each b, solve Ly=bLy = b (forward substitution, O(n²))
  3. Then solve Ux=yUx = y (back substitution, O(n²))

Partial Pivoting (PA = LU)

In practice, we permute rows to avoid division by small numbers. This gives PA=LUPA = LU where PP 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.

Step 1

Setup

Start with A. Our goal is to transform A into Upper Triangular form (U) using row operations, while recording the multipliers in L.

L (Lower)
1
0
0
1
×
U (Upper)
4
3
6
3

QR Decomposition

Factor any matrix (even rectangular) into an Orthogonal matrix times an Upper triangular matrix.

A=QRA = QR
QTQ=IQ^TQ = I (orthonormal columns), RR = upper triangular

Why QR is Stable

Orthogonal matrices preserve lengths: ∣∣Qx∣∣=∣∣x∣∣||Qx|| = ||x||. This means errors do not amplify during computation (condition number of QQ is 1).

Gram-Schmidt Process

The algorithm to construct QQ is called Gram-Schmidt. It iteratively subtracts projections to force orthogonality.

v₁v₂

Initial Vectors

v₁ Angle15°
v₂ Angle60°

Current Operation

Configure vectors...
  1. u1=a1u_1 = a_1, then q1=u1/∣∣u1∣∣q_1 = u_1 / ||u_1||
  2. u2=a2−(a2⋅q1)q1u_2 = a_2 - (a_2 \cdot q_1)q_1, then q2=u2/∣∣u2∣∣q_2 = u_2 / ||u_2||
  3. uk=ak−∑j=1k−1(ak⋅qj)qju_k = a_k - \sum_{j=1}^{k-1}(a_k \cdot q_j)q_j, then qk=uk/∣∣uk∣∣q_k = u_k / ||u_k||

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 A=[4363]A = \begin{bmatrix} 4 & 3 \\ 6 & 3 \end{bmatrix}, eliminate the entry below the first pivot by subtracting 6/4=1.56/4 = 1.5 times row 1 from row 2. The multiplier goes into LL and the result of elimination is UU, so L=[101.51]L = \begin{bmatrix} 1 & 0 \\ 1.5 & 1 \end{bmatrix} and U=[430−1.5]U = \begin{bmatrix} 4 & 3 \\ 0 & -1.5 \end{bmatrix}. Multiplying them back gives AA, and solving Ax=bAx = b now takes one forward substitution with LL and one back substitution with UU. 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 B=[3142]B = \begin{bmatrix} 3 & 1 \\ 4 & 2 \end{bmatrix}, normalise the first column (3,4)(3, 4), whose length is 5, to get q1=(0.6,0.8)q_1 = (0.6, 0.8). The second column (1,2)(1, 2) has a component of q1⋅(1,2)=2.2q_1 \cdot (1, 2) = 2.2 along q1q_1; removing it leaves (−0.32,0.24)(-0.32, 0.24), whose length is 0.4, so q2=(−0.8,0.6)q_2 = (-0.8, 0.6). The lengths and components form R=[52.200.4]R = \begin{bmatrix} 5 & 2.2 \\ 0 & 0.4 \end{bmatrix}, and Q=[q1  q2]Q = [q_1 \; q_2] 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

  1. Build Vandermonde matrix: Aij=VijA_{ij} = V_i^j
  2. Compute A=QRA = QR
  3. Solve Ra^=QTbR\hat{a} = Q^Tb by back substitution (since R is triangular, this is easy)

Why Not Normal Equations?

For high degree polynomials, ATAA^TA 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.

LUQRSVD
Matrix ShapeSquare onlyAny shapeAny shape
SpeedFastest (∼n3/3)(\sim n^3/3)Medium (∼2n3/3)(\sim 2n^3/3)Slowest (∼10n3)(\sim 10n^3)
StabilityNeeds pivotingGoodBest (Total Least Squares)
Best ForLinear solves (Ax=b)Least squaresRank, 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.