Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


QR Decompositions






Orthogonal Times Triangular

The QR decomposition factors a matrix into an orthogonal factor Q and an upper triangular factor R. It is the matrix form of the Gram-Schmidt process, the standard method for least-squares computation, and the foundation of the most widely used eigenvalue algorithm. The orthogonal factor preserves lengths and condition numbers, making QR the numerically safest of the triangular factorizations.



What QR Decomposition Is

An m×nm \times n matrix AA with mnm \geq n and linearly independent columns factors as

A=QRA = QR


where QQ is m×nm \times n with orthonormal columns and RR is n×nn \times n upper triangular with positive diagonal entries.

The columns of QQ form an orthonormal basis for the column space of AA. The matrix RR stores the coefficients: each column of AA is a linear combination of the columns of QQ with weights given by the corresponding column of RR. The upper triangular structure of RR reflects the sequential nature of the orthogonalization — each column depends only on the columns that came before it.

QR via Gram-Schmidt

Applying the Gram-Schmidt process to the columns a1,,an\mathbf{a}_1, \dots, \mathbf{a}_n of AA produces orthonormal vectors q1,,qn\mathbf{q}_1, \dots, \mathbf{q}_n. These become the columns of QQ.

The entries of RR are the dot products computed during Gram-Schmidt: Rij=qiajR_{ij} = \mathbf{q}_i \cdot \mathbf{a}_j for iji \leq j, and Rij=0R_{ij} = 0 for i>ji > j. Each column of AA decomposes as

aj=R1jq1+R2jq2++Rjjqj\mathbf{a}_j = R_{1j}\mathbf{q}_1 + R_{2j}\mathbf{q}_2 + \cdots + R_{jj}\mathbf{q}_j


The entry Rjj=ujR_{jj} = \|\mathbf{u}_j\| (the norm of the jj-th orthogonal vector before normalization) is always positive, which makes RR unique.

Worked Example


For A=(110110)A = \begin{pmatrix} 1 & 1 \\ 0 & 1 \\ 1 & 0 \end{pmatrix}: Gram-Schmidt on the two columns gives q1=12(1,0,1)T\mathbf{q}_1 = \frac{1}{\sqrt{2}}(1, 0, 1)^T and q2=16(1,2,1)T\mathbf{q}_2 = \frac{1}{\sqrt{6}}(1, 2, -1)^T. Then R=(21/203/6)R = \begin{pmatrix} \sqrt{2} & 1/\sqrt{2} \\ 0 & 3/\sqrt{6} \end{pmatrix} and A=QRA = QR.

QR via Householder Reflections

A Householder reflection is an orthogonal matrix H=I2vvT/(vTv)H = I - 2\mathbf{v}\mathbf{v}^T/(\mathbf{v}^T\mathbf{v}) that reflects Rm\mathbb{R}^m across the hyperplane perpendicular to v\mathbf{v}. By choosing v\mathbf{v} appropriately, a single Householder reflection zeros out all entries below the pivot in one column.

Applying Householder reflections sequentially — one per column — produces HnH2H1A=RH_n \cdots H_2 H_1 A = R. Since each HiH_i is orthogonal, Q=H1H2HnQ = H_1 H_2 \cdots H_n is orthogonal, giving A=QRA = QR.

Householder QR is more numerically stable than Gram-Schmidt. It achieves backward stability — the computed factors QQ and RR satisfy QR=A+EQR = A + E where E\|E\| is on the order of machine precision times A\|A\|. This makes Householder QR the standard algorithm in numerical libraries.

Thin QR vs. Full QR

The thin (reduced) QR factorization has Q1Q_1 of size m×nm \times n with orthonormal columns and R1R_1 of size n×nn \times n upper triangular: A=Q1R1A = Q_1 R_1. This is the version produced by Gram-Schmidt and is sufficient for most applications.

The full QR factorization extends Q1Q_1 to a square m×mm \times m orthogonal matrix QQ by appending mnm - n columns forming an orthonormal basis for Col(A)\text{Col}(A)^\perp. The factor RR is extended to m×nm \times n by appending mnm - n rows of zeros: A=QRA = QR.

The full version is needed when the orthogonal complement of the column space is required — for instance, when extracting a basis for the left null space. The thin version is more economical for system solving and least squares.

Existence and Uniqueness

Every m×nm \times n matrix with mnm \geq n and linearly independent columns has a thin QR factorization. Every m×nm \times n matrix (regardless of rank) has a full QR factorization.

The thin QR factorization with positive diagonal entries on RR is unique. If negative diagonal entries are permitted, the factorization is not unique — signs can be redistributed between QQ and RR (multiplying a column of QQ by 1-1 and the corresponding row of RR by 1-1 preserves the product). The convention of positive diagonal entries on RR resolves this ambiguity.

Solving Least Squares with QR

The normal equations ATAx^=ATbA^TA\hat{\mathbf{x}} = A^T\mathbf{b} transform under A=QRA = QR. Since ATA=RTQTQR=RTRA^TA = R^TQ^TQR = R^TR and ATb=RTQTbA^T\mathbf{b} = R^TQ^T\mathbf{b}, the normal equations become RTRx^=RTQTbR^TR\hat{\mathbf{x}} = R^TQ^T\mathbf{b}. Canceling RTR^T (invertible because RR has positive diagonal):

Rx^=QTbR\hat{\mathbf{x}} = Q^T\mathbf{b}


The right-hand side QTbQ^T\mathbf{b} is computed by nn dot products. The system Rx^=QTbR\hat{\mathbf{x}} = Q^T\mathbf{b} is upper triangular, solved by back substitution in O(n2)O(n^2) operations.

The critical advantage over the normal equations is numerical. Forming ATAA^TA squares the condition number: κ(ATA)=κ(A)2\kappa(A^TA) = \kappa(A)^2. If AA has condition number 10610^6, the normal equations work with condition number 101210^{12}, losing 1212 digits of accuracy in double precision. QR avoids this squaring and works with the original condition number 10610^6.

The QR Algorithm for Eigenvalues

The QR algorithm is the standard method for computing eigenvalues of general (non-symmetric) matrices. It proceeds iteratively:

Set A0=AA_0 = A. At each step, compute the QR factorization Ak=QkRkA_k = Q_k R_k, then form Ak+1=RkQkA_{k+1} = R_k Q_k.

Under mild conditions, AkA_k converges to an upper triangular matrix with the eigenvalues on the diagonal. The convergence is driven by the fact that Ak+1=QkTAkQkA_{k+1} = Q_k^T A_k Q_k — each iteration is a similarity transformation that preserves the eigenvalues while driving the sub-diagonal entries toward zero.

With shifts (replacing AkA_k by AkσkIA_k - \sigma_k I before factoring and adding σkI\sigma_k I back), convergence accelerates dramatically — cubic convergence for symmetric matrices with the Wilkinson shift. The QR algorithm computes eigenvalues without ever forming the characteristic polynomial, avoiding the severe numerical instability of polynomial root-finding.

Properties of the Factors

The orthonormality of QQ's columns (QTQ=InQ^TQ = I_n) has several immediate consequences.

The matrix QQTQQ^T is the projection matrix onto Col(A)\text{Col}(A). For any b\mathbf{b}, QQTbQQ^T\mathbf{b} is the orthogonal projection of b\mathbf{b} onto the column space.

Orthogonal multiplication preserves norms: Ax=QRx=Rx\|A\mathbf{x}\| = \|QR\mathbf{x}\| = \|R\mathbf{x}\|, since Qy=y\|Q\mathbf{y}\| = \|\mathbf{y}\| for any y\mathbf{y}. This means RR captures all the "size" information of AA — the orthogonal factor contributes nothing to stretching or compressing.

RR is invertible when AA has full column rank (the diagonal entries are the norms of the Gram-Schmidt vectors, all positive). The singular values of AA equal the singular values of RR, since the orthogonal factor does not affect them.

Gram-Schmidt vs. Householder

Classical Gram-Schmidt can lose orthogonality in floating-point arithmetic when the columns of AA are nearly dependent. The computed qi\mathbf{q}_i's may fail to be perpendicular to machine precision, and the errors accumulate with each step.

Modified Gram-Schmidt improves stability by updating the remaining vectors in place after each projection subtraction, rather than using the original columns throughout. The mathematical result is identical in exact arithmetic, but the numerical behavior is significantly better.

Householder reflections provide the strongest stability guarantee. Each reflection zeros an entire column below the diagonal in a single, orthogonally-implemented step. The resulting QR factorization is backward stable — the gold standard in numerical linear algebra.

Givens rotations offer a third option, zeroing entries one at a time via plane rotations. They are preferred for sparse matrices, where surgically placed zeros can be introduced without disturbing the existing sparsity structure.

In practice, Householder is the default for dense matrices, Givens for sparse ones, and Gram-Schmidt (modified) for situations where the orthogonal factor QQ is needed explicitly rather than implicitly.

QR and Gram-Schmidt: The Connection

The Gram-Schmidt process and the QR decomposition are two descriptions of the same computation.

Gram-Schmidt takes the columns of AA and produces orthonormal vectors q1,,qn\mathbf{q}_1, \dots, \mathbf{q}_n while recording the coefficients Rij=qiajR_{ij} = \mathbf{q}_i \cdot \mathbf{a}_j along the way. Assembling these into matrices gives A=QRA = QR.

Conversely, given A=QRA = QR, the columns of QQ are exactly what Gram-Schmidt would produce, and RR stores exactly the dot products Gram-Schmidt would compute. The factorization is the matrix-level summary of the vector-level algorithm.

This duality means every theorem about QR has an interpretation in terms of Gram-Schmidt, and vice versa. The QR decomposition is Gram-Schmidt made systematic, portable, and computable in a single matrix equation.