Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


QR Decomposition


Symbolic visualization of A = QR by Gram–Schmidt on the columns — coefficients into R, unit columns into Q.

Shape of A?The QR decomposition writes a matrix with independent columns as A = QR, where Q has orthonormal columns and R is upper triangular. It is Gram–Schmidt on the columns of A, with the bookkeeping kept: each coefficient q_j · a_k goes into R above the diagonal, each length ‖u_k‖ goes on the diagonal, and the normalized columns go into Q. Solving least squares and finding eigenvalues both run on this factorization.
A3×3rows × columns — Q is 3×3, R is 3×3
A3×3
a1,1
a1,2
a1,3
a2,1
a2,2
a2,3
a3,1
a3,2
a3,3
=
Q3×3
?
?
?
?
?
?
?
?
?
·
R3×3
?
?
?
0
?
?
0
0
?
Step 1 / 14

Step explanations

1QR decomposition of an 3×3 matrix
A will be written as A = QR: Q with orthonormal columns, R upper triangular. The method is Gram–Schmidt on the columns of A, keeping the bookkeeping: every coefficient qj · ak goes into R above the diagonal, every length ‖uk‖ onto the diagonal, and every normalized column into Q. The zeros below the diagonal of R are known before any arithmetic: column k of A only ever involves q1…qk.
Gram-Schmidt on the columns, with the bookkeeping kept in R. Learn more about the opening scene · what it is








Key Terms

QR decomposition — the factorization A=QRA = QR of a matrix with linearly independent columns, where QQ has orthonormal columns and RR is upper triangular.

Orthonormal columnsqiqj=0\mathbf{q}_i \cdot \mathbf{q}_j = 0 for iji \neq j and qiqi=1\mathbf{q}_i \cdot \mathbf{q}_i = 1; equivalently QTQ=IQ^T Q = I.

Upper triangular — all entries below the main diagonal are zero: rj,k=0r_{j,k} = 0 for j>kj > k.

Gram-Schmidt process — the procedure that builds QQ column by column, and whose coefficients and lengths are the entries of RR.

Working columnuk=akj<krj,kqj\mathbf{u}_k = \mathbf{a}_k - \sum_{j<k} r_{j,k} \mathbf{q}_j, column kk of AA with its projections onto the finished columns removed.

Diagonal of RRrk,k=ukr_{k,k} = \|\mathbf{u}_k\|, the length of each working column before normalization.

Above the diagonalrj,k=qjakr_{j,k} = \mathbf{q}_j \cdot \mathbf{a}_k, the coefficient of qj\mathbf{q}_j in column kk of AA.

Reduced versus full — this tool shows the reduced form, with QQ the same shape as AA; the full form pads QQ to a square orthogonal matrix and RR with zero rows.

Getting Started with the Visualizer

Set the shape of AA, then watch QQ and RR fill in together.

• Use the Shape steppers for 22 to 44 rows and 22 or 33 columns; QQ takes the shape of AA and RR is square with one row per column
• Hover the ? icon for a reminder of what the factorization is and where least squares and eigenvalue algorithms use it
• Press play or step manually through the scene player; the speed selector and step log let you control pace and review
• The layout reads A=QRA = Q \cdot R throughout: AA on the left stays fixed, QQ fills column by column, and RR fills entry by entry as each coefficient and length is produced
• Everything is symbolic: the coefficients are named rj,kr_{j,k} and the working columns uk\mathbf{u}_k, so the cells show the structure of the formula rather than numbers

Choosing more columns than rows is allowed and instructive: the columns then cannot be independent, and the caption points out that a working column would reduce to zero.

The Phases

The visualizer processes the columns of AA from left to right, and each column goes through the same short cycle.

Start — column kk of AA is copied into column kk of QQ as the working column uk\mathbf{u}_k
Coefficient — for each finished column qj\mathbf{q}_j with j<kj < k, the dot product rj,k=qjakr_{j,k} = \mathbf{q}_j \cdot \mathbf{a}_k is written into RR above the diagonal
Subtractrj,kqjr_{j,k} \mathbf{q}_j is subtracted from the working column, and its cells grow a term
Normalize — the length rk,k=ukr_{k,k} = \|\mathbf{u}_k\| goes onto the diagonal of RR, and the working column divided by it becomes qk\mathbf{q}_k
DoneA=QRA = QR

The first column skips the coefficient and subtract steps, since there is nothing yet to be perpendicular to. The zeros below the diagonal of RR are drawn from the first scene, because column kk of AA only ever involves q1\mathbf{q}_1 through qk\mathbf{q}_k; nothing later can appear.

The Opening Scene: A Equals Q Times R

The player opens with AA on the left, an empty QQ of the same shape in the middle, and RR on the right showing only its zeros below the diagonal. At the default size everything is 3×33 \times 3.

Nothing is computed yet. What the scene establishes is the shape of the answer: QQ as wide as AA, RR square, and RR upper triangular before a single entry is known.
A3×3a1,1a1,2a1,3a2,1a2,2a2,3a3,1a3,2a3,3=Q3×3?????????·R3×3???0??00?
Opening scene, frozen

A on the left, an empty Q of the same shape, and R showing only its zeros below the diagonal. The triangular shape is known before any arithmetic: column k of A only ever involves q1…qk.

The zeros can be drawn in advance because of how the columns will be built: column kk of AA is decomposed against q1\mathbf{q}_1 through qk\mathbf{q}_k only, so column kk of RR has nothing below row kk. The triangular shape is not a coincidence of the numbers; it is the order of the process.

The one precondition is that the columns of AA be independent. The tool does not check it, because its inputs are symbolic, but the caption notes when the chosen shape makes independence impossible.

Recording a Coefficient

Before a projection is subtracted, its coefficient is recorded: rj,k=qjakr_{j,k} = \mathbf{q}_j \cdot \mathbf{a}_k, the dot product of a finished unit column with the original column being decomposed, written into RR above the diagonal.

The frozen picture below is the last coefficient at the default size, r2,3=q2a3r_{2,3} = \mathbf{q}_2 \cdot \mathbf{a}_3, with the first two columns of QQ complete.
A3×3a1,1a1,2a1,3a2,1a2,2a2,3a3,1a3,2a3,3=Q3×3u1,1/r1,1u1,2/r2,2a1,3r1,3q1,1u2,1/r1,1u2,2/r2,2a2,3r1,3q2,1u3,1/r1,1u3,2/r2,2a3,3r1,3q3,1·R3×3u1q1·a2q1·a30u2q2·a300?
Last coefficient, frozen

r2,3 = q2·a3 being written into R above the diagonal, with the first two columns of Q complete. A dot product with a unit column, so no division.

Two details are worth noticing. The dot product is with ak\mathbf{a}_k, the original column, not with the partly reduced working column; in exact arithmetic both give the same number, since the parts already removed are perpendicular to qj\mathbf{q}_j, but the original is the definition. And there is no division, because qj\mathbf{q}_j has length one.

Reading RR column by column afterwards gives the recipe for each column of AA in terms of the columns of QQ, which is the content of A=QRA = QR.

Subtracting the Projections

Each recorded coefficient is immediately used: rj,kqjr_{j,k} \mathbf{q}_j is subtracted from the working column, entry by entry, and the cells of column kk of QQ grow one term.

The frozen picture below is the last subtraction at the default size: column 33 of QQ reads ai,3r1,3qi,1r2,3qi,2a_{i,3} - r_{1,3} q_{i,1} - r_{2,3} q_{i,2}, perpendicular to both finished columns.
A3×3a1,1a1,2a1,3a2,1a2,2a2,3a3,1a3,2a3,3=Q3×3u1,1/r1,1u1,2/r2,2a1,3r1,3q1,1r2,3q1,2u2,1/r1,1u2,2/r2,2a2,3r1,3q2,1r2,3q2,2u3,1/r1,1u3,2/r2,2a3,3r1,3q3,1r2,3q3,2·R3×3u1q1·a2q1·a30u2q2·a300?
Last subtraction, frozen

Column 3 of Q reading ai,3 − r1,3qi,1 − r2,3qi,2: the original column with both projections removed, perpendicular to q1 and q2.

This is the Gram-Schmidt step, with the coefficient taken from RR rather than recomputed. What remains after all the subtractions is the part of ak\mathbf{a}_k that lies outside the span of the earlier columns, the genuinely new direction.

If that remainder were zero, ak\mathbf{a}_k would have been a combination of the earlier columns, and the factorization would stall: there would be no direction to normalize and rk,kr_{k,k} would be 00. The diagonal of RR is therefore a running independence test.

Normalizing into Q and the Diagonal of R

Once a working column is perpendicular to everything before it, its length goes on the diagonal of RR and the column divided by that length becomes the next column of QQ: rk,k=ukr_{k,k} = \|\mathbf{u}_k\| and qk=uk/rk,k\mathbf{q}_k = \mathbf{u}_k / r_{k,k}.

The frozen picture below is the last normalization at the default size: r3,3r_{3,3} filled and column 33 of QQ reading ui,3/r3,3u_{i,3} / r_{3,3}.
A3×3a1,1a1,2a1,3a2,1a2,2a2,3a3,1a3,2a3,3=Q3×3u1,1/r1,1u1,2/r2,2u1,3/r3,3u2,1/r1,1u2,2/r2,2u2,3/r3,3u3,1/r1,1u3,2/r2,2u3,3/r3,3·R3×3u1q1·a2q1·a30u2q2·a300u3
Last normalization, frozen

r3,3 = ‖u3‖ on the diagonal of R and column 3 of Q reading ui,3 / r3,3. Length into R, unit vector into Q - the pair that makes QR reproduce A exactly.

The two halves of this step are what make the factorization exact rather than approximate. Dividing by the length is what puts a unit vector into QQ; storing the length in RR is what lets QRQR reproduce AA, because rk,kqk=ukr_{k,k} \mathbf{q}_k = \mathbf{u}_k restores the working column and the coefficients above restore the parts subtracted from it.

Read down column kk of RR and the original column reassembles: ak=r1,kq1++rk,kqk\mathbf{a}_k = r_{1,k}\mathbf{q}_1 + \cdots + r_{k,k}\mathbf{q}_k. The diagonal entry is the length of the new direction, and for a square AA the product of the diagonal is detA|\det A|.

The Completed Factorization

The final scene shows QQ full of unit columns and RR full above the diagonal: A=QRA = QR.

The frozen picture below is the default run complete, with every column of QQ and every recorded entry of RR highlighted.
A3×3a1,1a1,2a1,3a2,1a2,2a2,3a3,1a3,2a3,3=Q3×3u1,1/r1,1u1,2/r2,2u1,3/r3,3u2,1/r1,1u2,2/r2,2u2,3/r3,3u3,1/r1,1u3,2/r2,2u3,3/r3,3·R3×3u1q1·a2q1·a30u2q2·a300u3
Completed factorization, frozen

Q with three orthonormal columns, R upper triangular with every recorded entry in place: A = QR. Read down any column of R for the recipe that rebuilds that column of A.

From here the two factors do different jobs. QQ is an orthonormal basis of the column space of AA, so projecting onto that space is QQTQQ^T and coordinates in it are QTQ^T times the vector. RR is triangular, so systems involving it are solved by back substitution.

Put together they give the least-squares recipe: AxbA\mathbf{x} \approx \mathbf{b} becomes Rx=QTbR\mathbf{x} = Q^T\mathbf{b}, one orthogonal projection and one triangular solve, with none of the loss of precision that forming ATAA^T A would bring. That single application is the reason QR is computed thousands of times a second inside statistical and scientific software.

Reading the Scene Player

Each scene combines column and cell highlights, arrows, and a caption.

• In a start scene, the source column of AA is primary and the destination column of QQ is accent
• In a coefficient scene, the finished column qj\mathbf{q}_j is secondary, the column ak\mathbf{a}_k is primary, and the destination entry of RR is accent, with arrows from both columns into it
• In a subtract scene, qj\mathbf{q}_j is secondary, the working column is accent, and the coefficient just recorded is primary, with an arrow from RR back into QQ
• In a normalize scene, the column of QQ and the diagonal entry of RR are both accent
• Cells of QQ show the growing expression ai,kr1,kqi,1a_{i,k} - r_{1,k} q_{i,1} - \cdots while a column is being built, then ui,k/rk,ku_{i,k} / r_{k,k} once it is normalized; cells of RR show qjak\mathbf{q}_j \cdot \mathbf{a}_k above the diagonal and uk\|\mathbf{u}_k\| on it
• The step log on the right keeps a record of every completed step

Choosing the Shape

Two and three columns cover the whole pattern; the row count only changes how tall the columns are.

• With 22 columns, RR is 2×22 \times 2 with one coefficient above the diagonal: a2=r1,2q1+r2,2q2\mathbf{a}_2 = r_{1,2} \mathbf{q}_1 + r_{2,2} \mathbf{q}_2
• With 33 columns, RR is 3×33 \times 3 with three coefficients, and the third column shows the essential point: it is projected onto q1\mathbf{q}_1 and q2\mathbf{q}_2, the finished unit columns, never onto the original a2\mathbf{a}_2
• A tall AA (4×24 \times 2 or 4×34 \times 3) is the least-squares shape: more equations than unknowns, and QQ is tall with RR small
• A square AA gives a square QQ, which is then an orthogonal matrix with Q1=QTQ^{-1} = Q^T

Beyond three columns the cycle simply repeats with more coefficients per column; the tool stops there because the working-column expressions are already three terms long.

What the QR Decomposition Is

For an n×kn \times k matrix AA with linearly independent columns a1,,ak\mathbf{a}_1, \ldots, \mathbf{a}_k, the QR decomposition is

A=QR,Q=(q1qk),QTQ=I,R upper triangularA = QR, \qquad Q = \begin{pmatrix} \mathbf{q}_1 & \cdots & \mathbf{q}_k \end{pmatrix}, \quad Q^T Q = I, \quad R \text{ upper triangular}


It is the Gram-Schmidt process with the bookkeeping kept. Gram-Schmidt produces orthonormal q1,,qk\mathbf{q}_1, \ldots, \mathbf{q}_k with the property that each ak\mathbf{a}_k is a combination of q1,,qk\mathbf{q}_1, \ldots, \mathbf{q}_k only:

ak=r1,kq1+r2,kq2++rk,kqk\mathbf{a}_k = r_{1,k} \mathbf{q}_1 + r_{2,k} \mathbf{q}_2 + \cdots + r_{k,k} \mathbf{q}_k


Collect those coefficients as column kk of a matrix RR, and the kk equations together say exactly A=QRA = QR. Because ak\mathbf{a}_k never involves qj\mathbf{q}_j for j>kj > k, column kk of RR has zeros below row kk: RR is upper triangular.

The entries have direct meaning. Above the diagonal, rj,k=qjakr_{j,k} = \mathbf{q}_j \cdot \mathbf{a}_k is the projection coefficient, with no division because qj\mathbf{q}_j is a unit vector. On the diagonal, rk,k=ukr_{k,k} = \|\mathbf{u}_k\| is the length of the working column, the part of ak\mathbf{a}_k that was genuinely new. And since QTQ=IQ^T Q = I, the whole of RR can be recovered at once as R=QTAR = Q^T A.

For the full treatment, including the full versus reduced forms and Householder reflections as an alternative construction, see the QR decomposition theory page.

Key Properties

The factorization carries the properties of its two factors.

Existence: every matrix with linearly independent columns has a QR decomposition
Uniqueness: with the diagonal of RR required to be positive, QQ and RR are unique
Orthonormality: QTQ=IQ^T Q = I, so R=QTAR = Q^T A
Nested spans: the first mm columns of QQ span the same space as the first mm columns of AA, for every mm
Determinant (square case): detA=detR=r1,1r2,2rk,k|\det A| = |\det R| = r_{1,1} r_{2,2} \cdots r_{k,k}, since detQ=1|\det Q| = 1
Column space: the columns of QQ are an orthonormal basis of the column space of AA
Diagonal as independence test: rk,k=0r_{k,k} = 0 exactly when ak\mathbf{a}_k is a combination of the earlier columns
Order matters: permuting the columns of AA changes both QQ and RR
Numerical note: the classical Gram-Schmidt shown here loses orthogonality in floating point; production code uses modified Gram-Schmidt or Householder reflections, which produce the same QQ and RR

Why It Matters

QR is the factorization behind most of numerical linear algebra's daily work.

Least squares: with A=QRA = QR, the normal equations ATAx=ATbA^T A \mathbf{x} = A^T \mathbf{b} collapse to Rx=QTbR\mathbf{x} = Q^T \mathbf{b}, a triangular system solved by back substitution, without ever forming the ill-conditioned ATAA^T A
Eigenvalues: the QR algorithm factors A=QRA = QR, forms RQRQ, and repeats; the iterates converge to a triangular matrix with the eigenvalues on the diagonal
Orthonormal bases: QQ is an orthonormal basis of the column space of AA, ready for projections and coordinates
Solving square systems: Ax=bA\mathbf{x} = \mathbf{b} becomes Rx=QTbR\mathbf{x} = Q^T\mathbf{b}, as stable as any direct method
Rank and independence: a tiny diagonal entry of RR flags a nearly dependent column
Determinants: the product of the diagonal of RR gives detA|\det A| for square AA

Worked Example

Take the 3×33 \times 3 matrix whose columns are

a1=(110),a2=(101),a3=(011)\mathbf{a}_1 = \begin{pmatrix} 1 \\ 1 \\ 0 \end{pmatrix}, \quad \mathbf{a}_2 = \begin{pmatrix} 1 \\ 0 \\ 1 \end{pmatrix}, \quad \mathbf{a}_3 = \begin{pmatrix} 0 \\ 1 \\ 1 \end{pmatrix}


Column 1: u1=a1\mathbf{u}_1 = \mathbf{a}_1, r1,1=u1=2r_{1,1} = \|\mathbf{u}_1\| = \sqrt{2}, q1=12(1,1,0)\mathbf{q}_1 = \tfrac{1}{\sqrt{2}}(1, 1, 0).

Column 2: r1,2=q1a2=12r_{1,2} = \mathbf{q}_1 \cdot \mathbf{a}_2 = \tfrac{1}{\sqrt{2}}, so u2=a212q1=(12,12,1)\mathbf{u}_2 = \mathbf{a}_2 - \tfrac{1}{\sqrt{2}}\mathbf{q}_1 = \left(\tfrac{1}{2}, -\tfrac{1}{2}, 1\right), r2,2=3/2r_{2,2} = \sqrt{3/2}, q2=16(1,1,2)\mathbf{q}_2 = \tfrac{1}{\sqrt{6}}(1, -1, 2).

Column 3: r1,3=q1a3=12r_{1,3} = \mathbf{q}_1 \cdot \mathbf{a}_3 = \tfrac{1}{\sqrt{2}} and r2,3=q2a3=16r_{2,3} = \mathbf{q}_2 \cdot \mathbf{a}_3 = \tfrac{1}{\sqrt{6}}, so u3=a312q116q2=(23,23,23)\mathbf{u}_3 = \mathbf{a}_3 - \tfrac{1}{\sqrt{2}}\mathbf{q}_1 - \tfrac{1}{\sqrt{6}}\mathbf{q}_2 = \left(-\tfrac{2}{3}, \tfrac{2}{3}, \tfrac{2}{3}\right), r3,3=23r_{3,3} = \tfrac{2}{\sqrt{3}}, q3=13(1,1,1)\mathbf{q}_3 = \tfrac{1}{\sqrt{3}}(-1, 1, 1).

So

Q=(12161312161302613),R=(2121203/2160023)Q = \begin{pmatrix} \tfrac{1}{\sqrt{2}} & \tfrac{1}{\sqrt{6}} & -\tfrac{1}{\sqrt{3}} \\ \tfrac{1}{\sqrt{2}} & -\tfrac{1}{\sqrt{6}} & \tfrac{1}{\sqrt{3}} \\ 0 & \tfrac{2}{\sqrt{6}} & \tfrac{1}{\sqrt{3}} \end{pmatrix}, \qquad R = \begin{pmatrix} \sqrt{2} & \tfrac{1}{\sqrt{2}} & \tfrac{1}{\sqrt{2}} \\ 0 & \sqrt{3/2} & \tfrac{1}{\sqrt{6}} \\ 0 & 0 & \tfrac{2}{\sqrt{3}} \end{pmatrix}


Check column 2 of QRQR: 12q1+3/2q2=(12,12,0)+(12,12,1)=(1,0,1)=a2\tfrac{1}{\sqrt{2}}\mathbf{q}_1 + \sqrt{3/2}\,\mathbf{q}_2 = \left(\tfrac{1}{2}, \tfrac{1}{2}, 0\right) + \left(\tfrac{1}{2}, -\tfrac{1}{2}, 1\right) = (1, 0, 1) = \mathbf{a}_2. The product of the diagonal of RR is 23/223=2\sqrt{2} \cdot \sqrt{3/2} \cdot \tfrac{2}{\sqrt{3}} = 2, and detA=2\det A = 2. Set the visualizer to 3×33 \times 3 and step through to see the same fourteen scenes assembled symbolically.

Common Mistakes

A few mistakes recur.

Projecting onto the original columns — the coefficients are qjak\mathbf{q}_j \cdot \mathbf{a}_k with the finished unit columns qj\mathbf{q}_j, never ajak\mathbf{a}_j \cdot \mathbf{a}_k
Dividing the coefficient — because qj\mathbf{q}_j is a unit vector, rj,kr_{j,k} is a plain dot product; dividing by qjqj=1\mathbf{q}_j \cdot \mathbf{q}_j = 1 is harmless but dividing by aj\|\mathbf{a}_j\| is wrong
Putting coefficients in the wrong slotrj,kr_{j,k} sits in row jj, column kk: the row is the q\mathbf{q} being projected onto, the column is the a\mathbf{a} being decomposed
Forgetting the diagonalrk,kr_{k,k} is the length of the working column before normalization, not 11 and not the length of ak\mathbf{a}_k
Feeding in dependent columns — a dependent ak\mathbf{a}_k gives uk=0\mathbf{u}_k = \mathbf{0} and rk,k=0r_{k,k} = 0; the reduced factorization does not exist, and column pivoting is needed
Skipping the checkQTQQ^T Q should be the identity and QRQR should reproduce AA; both are quick to verify and catch most slips