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 m≥nm \geq n and linearly independent columns factors as

QR Decomposition
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.

COROLLARY
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.
A3×3a1,1​a1,2​a1,3​a2,1​a2,2​a2,3​a3,1​a3,2​a3,3​=Q3×3u1,1​/r1,1​u1,2​/r2,2​u1,3​/r3,3​u2,1​/r1,1​u2,2​/r2,2​u2,3​/r3,3​u3,1​/r1,1​u3,2​/r2,2​u3,3​/r3,3​·R3×3‖u1​‖q1​·a2​q1​·a3​0‖u2​‖q2​·a3​00‖u3​‖
A = QR, complete

The finished factorization: Q holds orthonormal columns spanning the same space as the columns of A, and R is upper triangular, recording how much of each orthonormal direction every original column uses. Because each column of A only involves the directions found up to that point, R has zeros below its diagonal. Build it column by column on the QR decomposition visualizer.

Q carries the geometry and R carries the bookkeeping; together they rebuild A exactly.

QR Notation

Notation

QR Notation

The same two letters appear on this page with three different jobs, and the shapes behind them are never written down. Five marks decide which reading applies.
The contract A=QRA = QR itself — what each factor carries and why RR comes out upper triangular — is set out at the Gram-Schmidt process. The reserved letter QQ and the equation QTQ=IQ^{T}Q = I belong to orthogonal sets, the normal equations and the hat on x^\hat{\mathbf{x}} to least squares.
A=Q1R1versusA=QRA = Q_1R_1 \qquad \text{versus} \qquad A = QR
A equals Q one R one — the thin factorisation — against A equals Q R, the full one
The letters match and the matrices do not. Thin: Q1Q_1 is m×nm \times n with orthonormal columns and R1R_1 is n×nn \times n. Full: QQ is square m×mm \times m and RR is m×nm \times n, carrying m−nm - n rows of zeros underneath. The line A=QRA = QR records neither shape, so which version is meant has to come from the surrounding sentence.
CasesThe subscript 11 marks the thin factors only when both versions are in play; on their own the thin factors are usually written QQ and RR as well. Gram-Schmidt produces the thin version, and the full one is reached by appending m−nm - n columns that span the orthogonal complement of the column space.
Also writtenReduced and economy are the same thing as thin. Numerical libraries select between them with an argument rather than a symbol, which is why code and page can disagree about what QQ names.
Do not confuseAssuming QQ is square. That assumption is harmless for the full version and is exactly where the identity in the next entry stops holding.
QTQ=In,QQT≠ImQ^{T}Q = I_n, \qquad QQ^{T} \neq I_m
Q transpose Q is the n by n identity; Q Q transpose is not the identity
For a thin QQ the order of the product decides whether the statement is true. Orthonormal columns give QTQ=InQ^{T}Q = I_n directly. Reversing the factors gives QQTQQ^{T}, the projection onto the column space of AA — equal to the identity only when QQ is square and there is nothing left over to project away.
CasesIn the full factorisation QQ is square and both products give ImI_m, which is the case set out at orthogonal sets. This page needs the other one, and that is why QQTQQ^{T} appears here as a projection matrix rather than as a way of writing II.
Also writtenThe thin case is sometimes flagged in words as orthonormal columns rather than by the equation, precisely to avoid claiming more than holds.
Do not confuseCalling a thin QQ an orthogonal matrix. That name requires a square matrix, and a thin QQ has no inverse at all. The practical version of the error is cancelling QQTQQ^{T} inside a longer expression as though it were II.
Rjj=∥uj∥>0R_{jj} = \|\mathbf{u}_j\| > 0
the j-th diagonal entry of R is the norm of the j-th orthogonal vector, and it is positive
Positivity buys uniqueness and is a convention, not something the factorisation forces. Multiplying a column of QQ by −1-1 and the matching row of RR by −1-1 leaves the product untouched, so without the sign rule an nn-column matrix has 2n2^n different QR factorisations. The line A=QRA = QR records none of that.
CasesGram-Schmidt gets the positive diagonal for free, since each RjjR_{jj} is a norm. Householder reflections do not, and numerical libraries generally do not enforce the convention — two correct computed factorisations of one matrix can differ by a column of sign flips with nothing wrong in either.
Also writtenStated as a condition on RR rather than built into the notation: with the diagonal of RR kept positive, appended to the factorisation whenever uniqueness is being claimed.
Do not confuseReading the QR factorisation as though the article were earned. It is earned only once the sign convention is stated, in the same way that the diagonal of ones earns uniqueness at LU.
Ak=QkRk,Ak+1=RkQkA_k = Q_kR_k, \qquad A_{k+1} = R_kQ_k
factor A sub k into Q sub k times R sub k, then multiply those same factors back in the opposite order
The letters QQ and RR name a factorisation everywhere else on this page and an iteration here. The whole method sits in the reversal: the second line multiplies the factors just produced in swapped order, which amounts to Ak+1=QkTAkQkA_{k+1} = Q_k^{T}A_kQ_k — a similarity transformation, so the eigenvalues survive while the sub-diagonal shrinks.
CasesThe subscript kk counts iteration steps. It is the only subscript on the page that indexes time rather than a column, a row or an entry, and QkQ_k is a different matrix at every step.
Also writtenThe shifted form factors Ak−σkIA_k - \sigma_kI and adds σkI\sigma_kI back afterwards, keeping the same two-line shape with a scalar moved in and out.
Do not confuseReading RkQkR_kQ_k as a second factorisation, or expecting the reversal to undo the first line. Swapping the order changes the matrix, and repeating the swap is what drives AkA_k toward upper triangular form with the eigenvalues on its diagonal.
H=I−2vvTvTvH = I - \frac{2\mathbf{v}\mathbf{v}^{T}}{\mathbf{v}^{T}\mathbf{v}}
H equals the identity minus two times v v transpose, over v transpose v
One vector appears four times in two products that are not the same operation. vvT\mathbf{v}\mathbf{v}^{T} is an m×mm \times m matrix of rank one; vTv\mathbf{v}^{T}\mathbf{v} is a single number, the squared length. Only the position of the transpose separates them, and it determines the shape of the result.
CasesThe numerator is a matrix and the denominator a scalar, so the fraction is ordinary scaling of a matrix and the subtraction from II is well formed. Writing v\mathbf{v} as a unit vector makes the denominator 11 and shortens the formula to I−2vvTI - 2\mathbf{v}\mathbf{v}^{T}, the form most texts quote — the same reflection either way.
Also writtenThe rank-one term is sometimes written v⊗v\mathbf{v} \otimes \mathbf{v} to name the outer product explicitly instead of leaving it to the transpose position.
Do not confuseReading the two products as interchangeable. Swapping them makes the subtraction ill-formed in one direction and the division ill-formed in the other, which is a useful check when the formula is being recalled rather than copied.

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:

QR Gram-Schmidt R Entries
Rij=qi⋅aj    (i≤j),Rij=0    (i>j),Rjj=∥uj∥R_{ij} = \mathbf{q}_i \cdot \mathbf{a}_j \;\; (i \leq j), \qquad R_{ij} = 0 \;\; (i > j), \qquad R_{jj} = \|\mathbf{u}_j\|

Each column of AA decomposes as

QR via Gram-Schmidt
aj=R1jq1+R2jq2+⋯+Rjjqj\mathbf{a}_j = R_{1j}\mathbf{q}_1 + R_{2j}\mathbf{q}_2 + \cdots + R_{jj}\mathbf{q}_j

The diagonal entry Rjj=∥uj∥R_{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.

Pitfall: Leaving the columns unnormalized
Putting u1=(1,0,1)T\mathbf{u}_1 = (1, 0, 1)^T into QQ without dividing by its length gives QTQ≠IQ^TQ \neq I, and with R11=2R_{11} = \sqrt{2} the first column of QRQR becomes (2,0,2)T(\sqrt{2}, 0, \sqrt{2})^T instead of a1\mathbf{a}_1. With q1=12(1,0,1)T\mathbf{q}_1 = \frac{1}{\sqrt{2}}(1, 0, 1)^T it is (1,0,1)T(1, 0, 1)^T.

Two quantities come out of every step of the computation: the leftover vector uj\mathbf{u}_j, whose length becomes the diagonal entry RjjR_{jj}, and the unit vector qj=uj/∥uj∥\mathbf{q}_j = \mathbf{u}_j / \|\mathbf{u}_j\| that becomes a column of QQ. In the example, u1=(1,0,1)T\mathbf{u}_1 = (1, 0, 1)^T has length 2\sqrt{2}, and u2=(1/2,1,−1/2)T\mathbf{u}_2 = (1/2, 1, -1/2)^T has length 3/2=3/6\sqrt{3/2} = 3/\sqrt{6}, the entry in the bottom-right corner of RR. The entry above it, 1/21/\sqrt{2}, is the dot product of q1\mathbf{q}_1 with the second column (1,1,0)T(1, 1, 0)^T.
A3×3a1,1​a1,2​a1,3​a2,1​a2,2​a2,3​a3,1​a3,2​a3,3​=Q3×3u1,1​/r1,1​u1,2​/r2,2​a1,3​−r1,3​q1,1​−r2,3​q1,2​u2,1​/r1,1​u2,2​/r2,2​a2,3​−r1,3​q2,1​−r2,3​q2,2​u3,1​/r1,1​u3,2​/r2,2​a3,3​−r1,3​q3,1​−r2,3​q3,2​·R3×3‖u1​‖q1​·a2​q1​·a3​0‖u2​‖q2​·a3​00?
The projection removed from a column

The part of this column lying along the directions already fixed is being subtracted away, leaving only what is genuinely new. The coefficients removed are not discarded — they are exactly the entries of R, which is why R comes out upper triangular without anyone arranging it. Watch both factors emerge on the QR decomposition visualizer.

Classical Gram-Schmidt is the clearest route to QR and the least stable one, which is why Householder reflections follow in the next section.

QR via Householder Reflections

A Householder reflection is an orthogonal matrix H=I−2vvT/(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 Hn⋯H2H1A=RH_n \cdots H_2 H_1 A = R. Since each HiH_i is orthogonal, Q=H1H2⋯HnQ = H_1 H_2 \cdots H_n is orthogonal, giving A=QRA = QR.

Pitfall: Reversing the order of the reflections
Taking Q=H2H1Q = H_2H_1 from H2H1A=RH_2H_1A = R puts QTQ^T in place of QQ, and QRQR no longer returns AA. Inverting the product reverses it, (H2H1)−1=H1H2(H_2H_1)^{-1} = H_1H_2, so A=H1H2RA = H_1H_2R.

In the equation Hn⋯H2H1A=RH_n \cdots H_2 H_1 A = R the reflections stand to the left of AA in the order they were applied: H1H_1 acts first and sits next to AA, and HnH_n acts last and sits on the far left. Each HiH_i is symmetric as well as orthogonal, so it is its own transpose and its own inverse. Recovering QQ means moving the whole product of reflections to the right-hand side of that equation.

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 m−nm - 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 m−nm - 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.

The two variants compare cleanly on the dimensions of their factors and what each one captures.
Decompositions · QR

Thin QR and full QR

Both reconstruct AA exactly. The full form keeps m−nm - n extra columns of QQ that multiply against rows of zeros — useless for the product, and the only route to a basis for the left null space.

2forms
TypeConditionQRbases obtainedused for
The working form
Thin QR§ 4m≥nm \geq n, columns of AA independentm×nm \times n, orthonormal columnsn×nn \times n upper triangulara basis for Col⁡(A)\operatorname{Col}(A)solving, least squares
The complete form
Full QR§ 4same AA, QQ completed to a squarem×mm \times m, fully orthogonalm×nm \times n — triangular block, then zerosbases for Col⁡(A)\operatorname{Col}(A) and its complementleft null space, theory
The extra columns are not redundant so much as unused. They complete QQ into a genuine orthogonal matrix, which is what makes QQT=IQQ^{\mathsf{T}} = I hold as well as QTQ=IQ^{\mathsf{T}}Q = I — and that second identity is what any argument about the left null space needs.
Thin QR and full QR·/linear-algebra/decompositions/qrLearn Math Class
The difference is m−nm - n columns of QQ paired against m−nm - n rows of zeros in RR, so they contribute nothing to the product and AA comes back identically either way. What they do contribute is a basis for everything AA cannot reach — the left null space — and the squareness that makes QQ orthogonal in both directions rather than only one.

Existence and Uniqueness

THEOREM
Every m×nm \times n matrix with m≥nm \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):

Solving Least Squares with QR
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.

RULE
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.

Least squares through QR needs one triangular solve and keeps the accuracy that forming ATAA^TA throws away.

The QR Algorithm for Eigenvalues

The QR algorithm is the standard method for computing eigenvalues of general (non-symmetric) matrices. It proceeds iteratively, starting from A0=AA_0 = A and at each step factoring and reassembling the product in reversed order:

QR Algorithm for Eigenvalues
Ak=QkRk,Ak+1=RkQkA_k = Q_k R_k, \qquad A_{k+1} = R_k Q_k

THEOREM
Under mild conditions, AkA_k converges to an upper triangular matrix with the eigenvalues on the diagonal.

Fact: Its inventor never saw it take over
John Francis published the QR algorithm in 1961, and Vera Kublanovskaya found it independently the same year. Francis left numerical analysis soon after and learned only in 2007 that his method had been named one of the ten most important algorithms of the 20th century.

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.

Reversing the factors is a similarity, so the QR algorithm can reshape a matrix freely without ever touching its eigenvalues.

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.

Pitfall: Treating QQᵀ as the identity
Replacing QQTQQ^T by II for a thin QQ claims QQTb=bQQ^T\mathbf{b} = \mathbf{b} for every b\mathbf{b}. For the worked example and b=(1,1,1)T\mathbf{b} = (1, 1, 1)^T, the product is (4/3,2/3,2/3)T(4/3, 2/3, 2/3)^T; only QTQ=I2Q^TQ = I_2 holds.

For the matrix of the worked example, m=3m = 3 and n=2n = 2, so QQTQQ^T is a 3×33 \times 3 matrix of rank 22. It leaves every vector in the plane Col(A)\text{Col}(A) unchanged and sends every vector perpendicular to that plane to 0\mathbf{0}. A general vector of R3\mathbb{R}^3 keeps only its component in the plane spanned by q1\mathbf{q}_1 and q2\mathbf{q}_2.

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.

Fact: At most nn mirrors make any orthogonal matrix
Householder QR on an n×nn \times n orthogonal matrix, with each diagonal entry kept positive, ends after n−1n - 1 reflections with an RR that is triangular and orthogonal: the identity, except perhaps a −1-1 in the last corner, which is one more reflection. So every orthogonal matrix is a product of at most nn reflections.

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.
DownloadPNG2×PDF
Before you read
Same result, different stability
All three methods produce the same QR factorization in exact arithmetic. In floating-point arithmetic they rank by how well the computed columns of QQ stay orthogonal.
most stablenumerical stabilityleast stable
Householder reflectionsbackward stable
Modified Gram-Schmidtsignificantly better
Classical Gram-Schmidtcan lose orthogonality
Takeaway
In practice
Householder is the default for dense matrices, Givens rotations for sparse ones, and modified Gram-Schmidt when QQ is needed explicitly.
QR methods ranked by numerical stability·/linear-algebra/decompositions/qrLearn Math Class

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=qi⋅ajR_{ij} = \mathbf{q}_i \cdot \mathbf{a}_j along the way. Assembling these into matrices gives A=QRA = QR.

Conversely, given A=QRA = QR with positive diagonal entries on RR, 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.
U3×3v1,1​v1,2​v1,3​v2,1​−c2,1​u1,1​v2,2​−c2,1​u1,2​v2,3​−c2,1​u1,3​v3,1​−c3,1​u1,1​−c3,2​u2,1​v3,2​−c3,1​u1,2​−c3,2​u2,2​v3,3​−c3,1​u1,3​−c3,2​u2,3​→E3×3u1,1​/‖u1​‖u1,2​/‖u1​‖u1,3​/‖u1​‖??????
The leftover normalised to length one

What remained after the projections was a direction with an arbitrary length; dividing by that length makes it a unit vector and the next column of Q. Gram-Schmidt and QR are not two procedures but one, read as a geometric construction or as a factorisation. Step through the orthogonalisation on the Gram-Schmidt visualizer.

Seeing them as one procedure explains why improving the stability of Gram-Schmidt improves QR at the same time.

Summary: Four Routes to the Same Factorization

The QR factorization is unique (for linearly independent columns, with the positive-diagonal convention), but the algorithms that produce it are not. Classical Gram-Schmidt, modified Gram-Schmidt, Householder reflections, and Givens rotations all yield the same A=QRA = QR in exact arithmetic once signs are normalized so the diagonal of RR is positive, but they differ sharply in numerical stability and in the kinds of matrices they handle most efficiently. The table below collects each method alongside how it operates, its stability behavior, and the setting in which it is the right choice.
The four methods divide cleanly by strategy. The first two build QQ directly, orthogonalizing the columns of AA one at a time; the second two leave QQ implicit and instead apply orthogonal operations to AA until what remains is triangular. That difference is what decides the stability, so the grouping below is also the ranking.
Decompositions · QR

Four routes to the same factorization

All four produce A=QRA = QR and none is a variant of the others in output — they differ only in how they get there and how much accuracy survives the trip. The split below is by numerical standing, which is the column that decides in practice.

4methods
Orthogonalization — build Q a column at a time2
1
unstable — loses orthogonality
uj=aj−∑i<jproj⁡qi(aj)\mathbf{u}_j = \mathbf{a}_j - \sum_{i<j} \operatorname{proj}_{\mathbf{q}_i}(\mathbf{a}_j), qj=uj/∥uj∥\mathbf{q}_j = \mathbf{u}_j / \|\mathbf{u}_j\|
Each column is orthogonalized against the original earlier columns. In exact arithmetic this is correct; in floating point the accumulated rounding means QTQQ^{\mathsf{T}}Q drifts measurably from II, and badly when the columns are nearly dependent. Worth knowing as the definition, not as a method.
2
better, still not backward stable
subtract each projection immediately
The same arithmetic reassociated: update the remaining columns in place after each subtraction rather than accumulating. Substantially more accurate than the classical form for no extra cost, which is why it is the version to use if Gram–Schmidt is used at all.
Triangularization — zero out A instead2
3
backward stable — the default
H=I−2vvT/vTvH = I - 2\mathbf{v}\mathbf{v}^{\mathsf{T}} / \mathbf{v}^{\mathsf{T}}\mathbf{v}
One reflection per column zeros everything below the pivot at once. QQ arrives as a product of reflections rather than being built directly, which is what keeps it orthogonal to machine precision. This is what a numerical library calls unless told otherwise.
4
backward stable; local
a plane rotation per entry
Zeros one entry at a time instead of a whole column. Slower on a dense matrix, but each rotation touches only two rows — so a sparse matrix stays sparse, and the work can be parallelised. The reason to choose it is structure, not speed.
Two of these are used and two are taught. Gram–Schmidt makes the connection between orthogonalization and QR visible, which is why it appears in every course; Householder and Givens are what libraries actually call, because backward stability is the property that matters once the matrix is anything other than well behaved.
Four routes to the same factorization·/linear-algebra/decompositions/qrLearn Math Class
Why the second group wins is worth stating plainly. Building QQ by orthogonalization means every column inherits the rounding error of the columns before it, so orthogonality degrades as the computation proceeds — and degrades fastest exactly when the columns are nearly dependent, which is when the factorization was most needed. Householder and Givens never construct QQ from the data at all: each step is an orthogonal matrix by construction, and a product of orthogonal matrices is orthogonal whatever the arithmetic did.
In practice the choice is narrow. Householder is the default for dense matrices and is what least squares routines call. Givens is chosen when the matrix is sparse or when entries arrive one at a time, since a rotation acts on two rows and leaves the rest untouched. Modified Gram–Schmidt survives where the columns of QQ are wanted explicitly and progressively — and classical Gram–Schmidt survives as an explanation.

Common Pitfalls

Three mistakes come up again and again when a QR factorization is computed by hand or used afterwards. Each is marked with a box where it happens in the sections above. Here each is worked through in full, with the wrong and the right working side by side.

Leaving the columns unnormalized

Gram-Schmidt first produces the orthogonal vectors uj\mathbf{u}_j, and these are put into QQ as they are. The division by ∥uj∥\|\mathbf{u}_j\| that turns them into unit vectors is skipped, while RR is filled in with the lengths as usual.

Wrong working: for A=(110110)A = \begin{pmatrix} 1 & 1 \\ 0 & 1 \\ 1 & 0 \end{pmatrix}, take u1=(1,0,1)T\mathbf{u}_1 = (1, 0, 1)^T and u2=(1/2,1,−1/2)T\mathbf{u}_2 = (1/2, 1, -1/2)^T as the columns of QQ, with R=(21/203/6)R = \begin{pmatrix} \sqrt{2} & 1/\sqrt{2} \\ 0 & 3/\sqrt{6} \end{pmatrix}. Then
QTQ=(2003/2)≠I,first column of QR=2 u1=(2,0,2)T≠a1Q^TQ = \begin{pmatrix} 2 & 0 \\ 0 & 3/2 \end{pmatrix} \neq I, \qquad \text{first column of } QR = \sqrt{2}\,\mathbf{u}_1 = (\sqrt{2}, 0, \sqrt{2})^T \neq \mathbf{a}_1


Right working: divide each uj\mathbf{u}_j by its length.
q1=u12=12(1,0,1)T,q2=u23/2=16(1,2,−1)T\mathbf{q}_1 = \frac{\mathbf{u}_1}{\sqrt{2}} = \frac{1}{\sqrt{2}}(1, 0, 1)^T, \qquad \mathbf{q}_2 = \frac{\mathbf{u}_2}{\sqrt{3/2}} = \frac{1}{\sqrt{6}}(1, 2, -1)^T

Now QTQ=I2Q^TQ = I_2 and QR=AQR = A. The first column of QRQR is 2 q1=(1,0,1)T\sqrt{2}\,\mathbf{q}_1 = (1, 0, 1)^T, and the second is 12q1+36q2=(12,0,12)T+(12,1,−12)T=(1,1,0)T\frac{1}{\sqrt{2}}\mathbf{q}_1 + \frac{3}{\sqrt{6}}\mathbf{q}_2 = (\frac{1}{2}, 0, \frac{1}{2})^T + (\frac{1}{2}, 1, -\frac{1}{2})^T = (1, 1, 0)^T.

Why it happens: the length ∥uj∥\|\mathbf{u}_j\| is already recorded in RR as the diagonal entry, so it feels as if it has been used. It is needed twice: once as RjjR_{jj}, and once as the divisor that makes qj\mathbf{q}_j a unit vector.

How to check: compute QTQQ^TQ. Its diagonal entries are the squared lengths of the columns of QQ, and every one of them must be 11.

Reversing the order of the reflections

Householder reflections are applied to AA one after another, giving Hn⋯H2H1A=RH_n \cdots H_2 H_1 A = R. The product on the left is then taken as QQ in the same order, although QQ has to undo it.

Wrong working: take A=(25102−2)A = \begin{pmatrix} 2 & 5 \\ 1 & 0 \\ 2 & -2 \end{pmatrix}. The reflections
H1=13(21212−22−2−1),H2=15(50003404−3)H_1 = \frac{1}{3}\begin{pmatrix} 2 & 1 & 2 \\ 1 & 2 & -2 \\ 2 & -2 & -1 \end{pmatrix}, \qquad H_2 = \frac{1}{5}\begin{pmatrix} 5 & 0 & 0 \\ 0 & 3 & 4 \\ 0 & 4 & -3 \end{pmatrix}

give H1A=(320304)H_1A = \begin{pmatrix} 3 & 2 \\ 0 & 3 \\ 0 & 4 \end{pmatrix} and H2H1A=R=(320500)H_2H_1A = R = \begin{pmatrix} 3 & 2 \\ 0 & 5 \\ 0 & 0 \end{pmatrix}. Taking Q=H2H1Q = H_2H_1:
H2H1=115(1051011−2−10−214−5),H2H1R=(2311/54/5−2/522/5)≠AH_2H_1 = \frac{1}{15}\begin{pmatrix} 10 & 5 & 10 \\ 11 & -2 & -10 \\ -2 & 14 & -5 \end{pmatrix}, \qquad H_2H_1R = \begin{pmatrix} 2 & 3 \\ 11/5 & 4/5 \\ -2/5 & 22/5 \end{pmatrix} \neq A


Right working: move the product to the other side. Each HiH_i is its own inverse, so A=(H2H1)−1R=H1−1H2−1R=H1H2RA = (H_2H_1)^{-1}R = H_1^{-1}H_2^{-1}R = H_1H_2R, and
Q=H1H2=115(1011−25−21410−10−5),QR=(25102−2)=AQ = H_1H_2 = \frac{1}{15}\begin{pmatrix} 10 & 11 & -2 \\ 5 & -2 & 14 \\ 10 & -10 & -5 \end{pmatrix}, \qquad QR = \begin{pmatrix} 2 & 5 \\ 1 & 0 \\ 2 & -2 \end{pmatrix} = A

The wrong product H2H1H_2H_1 is exactly QTQ^T.

Why it happens: H2H1H_2H_1 is the matrix that appears on the page, and it is orthogonal, so it looks like a valid QQ. The inversion that reverses the order is a single step that is easy to skip.

How to check: the first column of QQ must be the first column of AA divided by R11R_{11}. Here that is 13(2,1,2)T\frac{1}{3}(2, 1, 2)^T, which is the first column of H1H2H_1H_2 and not of H2H1H_2H_1.

Treating QQᵀ as the identity

For a thin QQ with m>nm > n, the identity QTQ=InQ^TQ = I_n is turned around and QQTQQ^T is replaced by ImI_m, as if QQ were square.

Wrong working: with QQ from the worked example and b=(1,1,1)T\mathbf{b} = (1, 1, 1)^T, the least-squares solution x^=R−1QTb\hat{\mathbf{x}} = R^{-1}Q^T\mathbf{b} gives Ax^=QRR−1QTb=QQTbA\hat{\mathbf{x}} = QRR^{-1}Q^T\mathbf{b} = QQ^T\mathbf{b}, and replacing QQTQQ^T by II makes this b\mathbf{b}. The fit looks exact, with zero residual.

Right working: QQTbQQ^T\mathbf{b} is the projection of b\mathbf{b} onto Col(A)\text{Col}(A).
QQTb=(q1⋅b) q1+(q2⋅b) q2=(1,0,1)T+(13,23,−13)T=(43,23,23)TQQ^T\mathbf{b} = (\mathbf{q}_1 \cdot \mathbf{b})\,\mathbf{q}_1 + (\mathbf{q}_2 \cdot \mathbf{b})\,\mathbf{q}_2 = (1, 0, 1)^T + (\tfrac{1}{3}, \tfrac{2}{3}, -\tfrac{1}{3})^T = (\tfrac{4}{3}, \tfrac{2}{3}, \tfrac{2}{3})^T

Solving Rx^=QTb=(2,2/6)TR\hat{\mathbf{x}} = Q^T\mathbf{b} = (\sqrt{2}, 2/\sqrt{6})^T gives x^=(2/3,2/3)T\hat{\mathbf{x}} = (2/3, 2/3)^T, and Ax^=(4/3,2/3,2/3)TA\hat{\mathbf{x}} = (4/3, 2/3, 2/3)^T. The residual is b−Ax^=(−1/3,1/3,1/3)T\mathbf{b} - A\hat{\mathbf{x}} = (-1/3, 1/3, 1/3)^T, not zero.

Why it happens: for a square orthogonal matrix both products equal II, and the two identities are learned together. For a thin QQ only the product of size n×nn \times n is the identity.

How to check: compare sizes and rank. QQTQQ^T is m×mm \times m but has rank only nn, so for m>nm > n it cannot be ImI_m. The residual b−QQTb\mathbf{b} - QQ^T\mathbf{b} is zero only when b\mathbf{b} already lies in Col(A)\text{Col}(A).

QR Decomposition FAQ

Is a thin QQ an orthogonal matrix?

+
No, that name requires a square matrix, and a thin QQ has none of the properties it implies, including an inverse. The practical form of the error is cancelling QQTQQ^{T} inside a longer expression as though it were the identity. For a thin QQ only QTQ=IQ^{T}Q = I holds, while QQTQQ^{T} is a projection matrix.Read more →

Is the QR factorisation unique?

+
Only once a sign convention is fixed, usually requiring the diagonal of RR to be positive. Gram-Schmidt satisfies that automatically since each diagonal entry is a norm, but Householder reflections do not, and numerical libraries rarely enforce it. Two correct factorisations of one matrix can therefore differ by a column of sign flips.Read more →

In the QR algorithm, is RkQkR_kQ_k a second factorisation?

+
No, it is a deliberate reversal of the factors just computed, and reversing them changes the matrix rather than undoing anything. Repeating that swap is exactly what drives the sequence toward upper triangular form with the eigenvalues along the diagonal. The subscript kk counts iterations rather than rows or columns.Read more →