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.
QR decomposition — the factorization A=QR of a matrix with linearly independent columns, where Q has orthonormal columns and R is upper triangular.
Orthonormal columns — qi⋅qj=0 for i=j and qi⋅qi=1; equivalently QTQ=I.
Upper triangular — all entries below the main diagonal are zero: rj,k=0 for j>k.
Gram-Schmidt process — the procedure that builds Q column by column, and whose coefficients and lengths are the entries of R.
Working column — uk=ak−∑j<krj,kqj, column k of A with its projections onto the finished columns removed.
Diagonal of R — rk,k=∥uk∥, the length of each working column before normalization.
Above the diagonal — rj,k=qj⋅ak, the coefficient of qj in column k of A.
Reduced versus full — this tool shows the reduced form, with Q the same shape as A; the full form pads Q to a square orthogonal matrix and R with zero rows.
Getting Started with the Visualizer
Set the shape of A, then watch Q and R fill in together.
• Use the Shape steppers for 2 to 4 rows and 2 or 3 columns; Q takes the shape of A and R 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=Q⋅R throughout: A on the left stays fixed, Q fills column by column, and R fills entry by entry as each coefficient and length is produced • Everything is symbolic: the coefficients are named rj,k and the working columns uk, 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 A from left to right, and each column goes through the same short cycle.
• Start — column k of A is copied into column k of Q as the working column uk • Coefficient — for each finished column qj with j<k, the dot product rj,k=qj⋅ak is written into R above the diagonal • Subtract — rj,kqj is subtracted from the working column, and its cells grow a term • Normalize — the length rk,k=∥uk∥ goes onto the diagonal of R, and the working column divided by it becomes qk • Done — A=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 R are drawn from the first scene, because column k of A only ever involves q1 through qk; nothing later can appear.
The Opening Scene: A Equals Q Times R
The player opens with A on the left, an empty Q of the same shape in the middle, and R on the right showing only its zeros below the diagonal. At the default size everything is 3×3.
Nothing is computed yet. What the scene establishes is the shape of the answer: Q as wide as A, R square, and R upper triangular before a single entry is known.
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 k of A is decomposed against q1 through qk only, so column k of R has nothing below row k. 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 A 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=qj⋅ak, the dot product of a finished unit column with the original column being decomposed, written into R above the diagonal.
The frozen picture below is the last coefficient at the default size, r2,3=q2⋅a3, with the first two columns of Q complete.
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, 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, but the original is the definition. And there is no division, because qj has length one.
Reading R column by column afterwards gives the recipe for each column of A in terms of the columns of Q, which is the content of A=QR.
Subtracting the Projections
Each recorded coefficient is immediately used: rj,kqj is subtracted from the working column, entry by entry, and the cells of column k of Q grow one term.
The frozen picture below is the last subtraction at the default size: column 3 of Q reads ai,3−r1,3qi,1−r2,3qi,2, perpendicular to both finished columns.
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 R rather than recomputed. What remains after all the subtractions is the part of ak that lies outside the span of the earlier columns, the genuinely new direction.
If that remainder were zero, ak would have been a combination of the earlier columns, and the factorization would stall: there would be no direction to normalize and rk,k would be 0. The diagonal of R 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 R and the column divided by that length becomes the next column of Q: rk,k=∥uk∥ and qk=uk/rk,k.
The frozen picture below is the last normalization at the default size: r3,3 filled and column 3 of Q reading ui,3/r3,3.
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 Q; storing the length in R is what lets QR reproduce A, because rk,kqk=uk restores the working column and the coefficients above restore the parts subtracted from it.
Read down column k of R and the original column reassembles: ak=r1,kq1+⋯+rk,kqk. The diagonal entry is the length of the new direction, and for a square A the product of the diagonal is ∣detA∣.
The Completed Factorization
The final scene shows Q full of unit columns and R full above the diagonal: A=QR.
The frozen picture below is the default run complete, with every column of Q and every recorded entry of R highlighted.
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. Q is an orthonormal basis of the column space of A, so projecting onto that space is QQT and coordinates in it are QT times the vector. R is triangular, so systems involving it are solved by back substitution.
Put together they give the least-squares recipe: Ax≈b becomes Rx=QTb, one orthogonal projection and one triangular solve, with none of the loss of precision that forming ATA 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 A is primary and the destination column of Q is accent • In a coefficient scene, the finished column qj is secondary, the column ak is primary, and the destination entry of R is accent, with arrows from both columns into it • In a subtract scene, qj is secondary, the working column is accent, and the coefficient just recorded is primary, with an arrow from R back into Q • In a normalize scene, the column of Q and the diagonal entry of R are both accent • Cells of Q show the growing expression ai,k−r1,kqi,1−⋯ while a column is being built, then ui,k/rk,k once it is normalized; cells of R show qj⋅ak above the diagonal and ∥uk∥ 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 2 columns, R is 2×2 with one coefficient above the diagonal: a2=r1,2q1+r2,2q2 • With 3 columns, R is 3×3 with three coefficients, and the third column shows the essential point: it is projected onto q1 and q2, the finished unit columns, never onto the original a2 • A tall A (4×2 or 4×3) is the least-squares shape: more equations than unknowns, and Q is tall with R small • A square A gives a square Q, which is then an orthogonal matrix with Q−1=QT
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×k matrix A with linearly independent columns a1,…,ak, the QR decomposition is
A=QR,Q=(q1⋯qk),QTQ=I,R upper triangular
It is the Gram-Schmidt process with the bookkeeping kept. Gram-Schmidt produces orthonormal q1,…,qk with the property that each ak is a combination of q1,…,qk only:
ak=r1,kq1+r2,kq2+⋯+rk,kqk
Collect those coefficients as column k of a matrix R, and the k equations together say exactly A=QR. Because ak never involves qj for j>k, column k of R has zeros below row k: R is upper triangular.
The entries have direct meaning. Above the diagonal, rj,k=qj⋅ak is the projection coefficient, with no division because qj is a unit vector. On the diagonal, rk,k=∥uk∥ is the length of the working column, the part of ak that was genuinely new. And since QTQ=I, the whole of R can be recovered at once as R=QTA.
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 R required to be positive, Q and R are unique • Orthonormality: QTQ=I, so R=QTA • Nested spans: the first m columns of Q span the same space as the first m columns of A, for every m • Determinant (square case): ∣detA∣=∣detR∣=r1,1r2,2⋯rk,k, since ∣detQ∣=1 • Column space: the columns of Q are an orthonormal basis of the column space of A • Diagonal as independence test: rk,k=0 exactly when ak is a combination of the earlier columns • Order matters: permuting the columns of A changes both Q and R • 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 Q and R
Why It Matters
QR is the factorization behind most of numerical linear algebra's daily work.
• Least squares: with A=QR, the normal equations ATAx=ATb collapse to Rx=QTb, a triangular system solved by back substitution, without ever forming the ill-conditioned ATA • Eigenvalues: the QR algorithm factors A=QR, forms RQ, and repeats; the iterates converge to a triangular matrix with the eigenvalues on the diagonal • Orthonormal bases: Q is an orthonormal basis of the column space of A, ready for projections and coordinates • Solving square systems: Ax=b becomes Rx=QTb, as stable as any direct method • Rank and independence: a tiny diagonal entry of R flags a nearly dependent column • Determinants: the product of the diagonal of R gives ∣detA∣ for square A
Check column 2 of QR: 21q1+3/2q2=(21,21,0)+(21,−21,1)=(1,0,1)=a2. The product of the diagonal of R is 2⋅3/2⋅32=2, and detA=2. Set the visualizer to 3×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 qj⋅ak with the finished unit columns qj, never aj⋅ak • Dividing the coefficient — because qj is a unit vector, rj,k is a plain dot product; dividing by qj⋅qj=1 is harmless but dividing by ∥aj∥ is wrong • Putting coefficients in the wrong slot — rj,k sits in row j, column k: the row is the q being projected onto, the column is the a being decomposed • Forgetting the diagonal — rk,k is the length of the working column before normalization, not 1 and not the length of ak • Feeding in dependent columns — a dependent ak gives uk=0 and rk,k=0; the reduced factorization does not exist, and column pivoting is needed • Skipping the check — QTQ should be the identity and QR should reproduce A; both are quick to verify and catch most slips