QR Decomposition and Gram-Schmidt

Turn any basis into an orthonormal one by subtracting the part that already points the wrong way. Watch the subtraction happen.

QR Decomposition and Gram-Schmidt

This module needs JavaScript: the numbers are computed in the page rather than recorded.

Worth knowing

A = QR: Q holds an orthonormal basis for the columns of A, R holds the coefficients that rebuild them.
Gram-Schmidt: normalise the first vector, then subtract from the second everything that points along the first.
The solid orange line is the projection being removed. What remains is perpendicular by construction.
R is upper triangular because the k-th original vector is built only from the first k orthonormal ones.

Overview

What Q and R contain

For a real matrix A, QR factorisation writes A = QR. The columns of Q are orthonormal; R is upper triangular. For a full-column-rank matrix, the reduced factorisation keeps only the columns needed for its column space. Signs of paired columns in Q and rows in R may vary while their product stays the same.

To solve a least-squares problem min‖Ax − b‖₂, substitute A = QR. Since QᵀQ = I, solve the triangular system Rx = Qᵀb. This avoids forming AᵀA, which can worsen conditioning. See projections for the geometric view and matrix multiplication for the products involved.

In this guide
  1. A two-vector illustration
  2. See where the triangular factor comes from
  3. Try this above
  4. Sources and next steps

QR Decomposition

QR writes a matrix as an orthonormal basis times a triangular matrix, making least-squares problems easier to solve.

A two-vector illustration

Start with a₁ = (1, 1) and a₂ = (1, 0). Normalise a₁ to get q₁ = (1, 1)/√2. Subtract from a₂ its component along q₁; the remaining vector is perpendicular to q₁. Normalising that residual gives q₂. The columns q₁ and q₂ form Q, and the coefficients used in these steps fill R.

Classical Gram–Schmidt can lose orthogonality in floating-point arithmetic for nearly dependent columns. Stable QR implementations use Householder reflections or reorthogonalisation. QR is useful, but it does not make an ill-conditioned problem well-conditioned.

See where the triangular factor comes from

For two independent columns a₁ and a₂, QR first normalises a₁ to q₁=a₁/‖a₁‖. It removes from a₂ its component r₁₂q₁, where r₁₂=q₁ᵀa₂, then normalises the leftover vector to q₂. The result is A=QR, with Q=[q₁ q₂] and R containing the projection coefficients and residual length. QᵀQ=I means its columns preserve dot products; R records how the original columns are assembled from those orthogonal directions.

For least squares, b is projected onto A's column space. With A=QR, the fitted coefficients solve Rx=Qᵀb. A triangular system can be solved by back-substitution. This is more stable than forming AᵀA because that product squares the condition number. If columns are dependent, the reduced triangular factor has a zero diagonal or rank deficiency and the solution may not be unique. Householder QR is preferred in robust numerical libraries; classical Gram–Schmidt can lose orthogonality on nearly parallel columns.

Try this above

Compare the original columns with the orthogonalised directions and inspect QᵀQ. Nearly dependent inputs make the numerical challenge visible.

For an orthonormal Q, multiplying QᵀQ gives the identity because each column has unit length and different columns have dot product zero. That check verifies orthonormality but not that QR reconstructs A; check both QᵀQ≈I and QR≈A. Numerical tolerances matter because floating-point arithmetic introduces small residuals. QR is also used to compute eigenvalues iteratively, but the basic decomposition does not itself produce eigenvalues in one step. Its practical strength is stable orthogonalisation and triangular solution.

If A has dependent columns, a full-column-rank reduced QR factorisation does not have a nonsingular square R. Rank-revealing QR with column pivoting can expose which columns add independent directions. This is useful for diagnosing redundant features in regression. QR is a factorisation, not a regulariser: it reorganises the same system and does not remove uncertainty or make collinear predictors independently identifiable.

Sources and next steps

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why is R upper triangular?

  2. Why do least-squares solvers avoid the normal equations?

  3. What does modified Gram-Schmidt change?

Cheat sheet

QR Decomposition and Gram-Schmidt

Orthogonalise a set of vectors with Gram-Schmidt and factor the matrix into Q times R. See why this makes least squares numerically stable.

MATHS · vizlearn.in/maths/qr_decomposition.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.