4.5.36Linear Algebra (Full)

QR decomposition

1,692 words8 min readdifficulty · medium6 backlinks

WHAT is QR decomposition?

WHY do we want this?

  • Solving least squares minAxb\min \|Ax-b\| becomes trivial: Rx=QbRx = Q^\top b (just back-substitution, no normal-equation squaring that wrecks numerical accuracy).
  • Orthonormal columns mean lengths and angles are preserved — QQ is a rotation/reflection.
  • It is the engine of the QR algorithm for eigenvalues.

HOW: derive QR from Gram–Schmidt (first principles)

Let A=[a1a2an]A=[\mathbf a_1 \mid \mathbf a_2 \mid \cdots \mid \mathbf a_n] (columns).

Step 1 — make the first axis. We need a unit vector pointing along a1\mathbf a_1: u1=a1,q1=u1u1.\mathbf u_1=\mathbf a_1,\qquad \mathbf q_1=\frac{\mathbf u_1}{\|\mathbf u_1\|}. Why? q1\mathbf q_1 is our first orthonormal direction; everything else will be measured against it.

Step 2 — remove what's already explained. For a2\mathbf a_2, subtract its shadow (projection) onto q1\mathbf q_1: u2=a2(q1a2)q1,q2=u2u2.\mathbf u_2=\mathbf a_2-(\mathbf q_1^\top\mathbf a_2)\,\mathbf q_1,\qquad \mathbf q_2=\frac{\mathbf u_2}{\|\mathbf u_2\|}. Why? The leftover u2\mathbf u_2 is the part of a2\mathbf a_2 perpendicular to q1\mathbf q_1, so q2q1\mathbf q_2\perp\mathbf q_1.

Step k — subtract all previous shadows. uk=aki<k(qiak)qi,qk=ukuk.\mathbf u_k=\mathbf a_k-\sum_{i<k}(\mathbf q_i^\top\mathbf a_k)\,\mathbf q_i,\qquad \mathbf q_k=\frac{\mathbf u_k}{\|\mathbf u_k\|}.

Step — read off RR. Invert each relation to express the original ak\mathbf a_k in terms of the qi\mathbf q_i: ak=ukrkkqk+i<k(qiak)rikqi.\mathbf a_k=\underbrace{\|\mathbf u_k\|}_{r_{kk}}\,\mathbf q_k+\sum_{i<k}\underbrace{(\mathbf q_i^\top\mathbf a_k)}_{r_{ik}}\,\mathbf q_i.

So the coefficient of qi\mathbf q_i in ak\mathbf a_k is rik=qiak (i<k),rkk=uk,rik=0 (i>k).\boxed{r_{ik}=\mathbf q_i^\top\mathbf a_k\ (i<k),\qquad r_{kk}=\|\mathbf u_k\|,\qquad r_{ik}=0\ (i>k).}

Stacking ak=irikqi\mathbf a_k=\sum_i r_{ik}\mathbf q_i over all kk is exactly the matrix equation A=QRA=QR. ∎

Figure — QR decomposition

Worked Example 1 — a clean 2×22\times 2

A=[110110].A=\begin{bmatrix}1 & 1\\ 0 & 1\\ 1 & 0\end{bmatrix}.

Column 1. u1=a1=(1,0,1)\mathbf u_1=\mathbf a_1=(1,0,1), u1=2\|\mathbf u_1\|=\sqrt2. q1=12(1,0,1)\mathbf q_1=\tfrac1{\sqrt2}(1,0,1). Why this step? First axis = normalized first column.

Projection coefficient. r12=q1a2=12(11+01+10)=12r_{12}=\mathbf q_1^\top\mathbf a_2=\tfrac1{\sqrt2}(1\cdot1+0\cdot1+1\cdot0)=\tfrac1{\sqrt2}. Why? This measures how much of a2\mathbf a_2 already lies along q1\mathbf q_1.

Column 2. u2=a2r12q1=(1,1,0)1212(1,0,1)=(12,1,12).\mathbf u_2=\mathbf a_2-r_{12}\mathbf q_1=(1,1,0)-\tfrac1{\sqrt2}\cdot\tfrac1{\sqrt2}(1,0,1)=(\tfrac12,1,-\tfrac12). u2=14+1+14=32\|\mathbf u_2\|=\sqrt{\tfrac14+1+\tfrac14}=\sqrt{\tfrac32}, so r22=3/2r_{22}=\sqrt{3/2}. q2=13/2(12,1,12)\mathbf q_2=\tfrac{1}{\sqrt{3/2}}(\tfrac12,1,-\tfrac12).

Result. Q=[121/23/2013/2121/23/2],R=[21203/2].Q=\begin{bmatrix}\tfrac1{\sqrt2}&\tfrac{1/2}{\sqrt{3/2}}\\[2pt]0&\tfrac{1}{\sqrt{3/2}}\\[2pt]\tfrac1{\sqrt2}&\tfrac{-1/2}{\sqrt{3/2}}\end{bmatrix},\quad R=\begin{bmatrix}\sqrt2&\tfrac1{\sqrt2}\\0&\sqrt{3/2}\end{bmatrix}. Check: QQ=IQ^\top Q=I and QR=AQR=A.


Worked Example 2 — least squares via QR

Solve minAxb\min\|Ax-b\| with the AA above and b=(1,2,3)b=(1,2,3).

Why QR helps. Axb2=QRxb2\|Ax-b\|^2=\|QRx-b\|^2. Since QQ preserves length, multiply inside by QQ^\top (extend to full QQ): the minimizer satisfies Rx=Qb.Rx=Q^\top b. Why? A=QRAAx=AbRQQRx=RQbRRx=RQbA=QR\Rightarrow A^\top A x=A^\top b\Rightarrow R^\top Q^\top QR\,x=R^\top Q^\top b\Rightarrow R^\top R x=R^\top Q^\top b. Cancel invertible RR^\top: Rx=QbRx=Q^\top b. No squaring of AA → numerically stable.

Compute QbQ^\top b:

  • (q1b)=12(1+0+3)=42=22.(\mathbf q_1^\top b)=\tfrac1{\sqrt2}(1+0+3)=\tfrac4{\sqrt2}=2\sqrt2.
  • (q2b)=13/2(121+12123)=13/21=2/3.(\mathbf q_2^\top b)=\tfrac1{\sqrt{3/2}}(\tfrac12\cdot1+1\cdot2-\tfrac12\cdot3)=\tfrac{1}{\sqrt{3/2}}\cdot 1=\sqrt{2/3}.

Back-substitute Rx=QbRx=Q^\top b: Row 2: 3/2x2=2/3x2=2/33/2=23.\sqrt{3/2}\,x_2=\sqrt{2/3}\Rightarrow x_2=\tfrac{\sqrt{2/3}}{\sqrt{3/2}}=\tfrac{2}{3}. Row 1: 2x1+12x2=22x1=212x2=213=53.\sqrt2\,x_1+\tfrac1{\sqrt2}x_2=2\sqrt2\Rightarrow x_1=2-\tfrac12 x_2=2-\tfrac13=\tfrac53.

So x=(53,23)x=(\tfrac53,\tfrac23). Why trust it? Triangular solve is exact and avoids forming AAA^\top A.


Forecast-then-Verify


Common Mistakes


Active Recall

Recall Test yourself (hidden)
  • Why is RR upper triangular? → because ak\mathbf a_k uses only q1qk\mathbf q_1\dots\mathbf q_k.
  • What's the least-squares shortcut? → Rx=QbRx=Q^\top b.
  • What property does QQ guarantee? → QQ=IQ^\top Q=I (length/angle preserving).
  • What makes QR unique? → rkk>0r_{kk}>0.
Recall Feynman: explain to a 12-year-old

Imagine you have three slightly crooked arrows drawn on paper. You want to describe them using a clean set of perpendicular arrows (like the corner of a room: up, right, forward). First you keep arrow 1, just shrink it to length 1 — that's your first clean direction. For arrow 2, you erase the part that points the same way as arrow 1, and whatever's left points in a brand-new perpendicular direction — clean it up to length 1. Repeat. The clean perpendicular arrows are the columns of Q. The "how much of each clean arrow I used" instructions form R. Multiply them back and you rebuild the original crooked arrows: A=QRA=QR.


Connections

  • Gram-Schmidt process — the algorithm QR packages.
  • Orthonormal bases and projections — projections =qiak=\mathbf q_i^\top\mathbf a_k are the entries of RR.
  • Least squares and normal equations — QR is the stable solver.
  • Householder reflections — production-grade way to get QR.
  • Eigenvalues and the QR algorithm — iterating Ak+1=RkQkA_{k+1}=R_kQ_k.
  • LU decomposition — sibling factorization (triangular but not orthogonal).

What does QR decomposition factor AA into?
A=QRA=QR with QQ orthonormal columns (QQ=IQ^\top Q=I) and RR upper-triangular with positive diagonal.
Why is RR upper-triangular?
Because each ak\mathbf a_k is a combination of only q1,,qk\mathbf q_1,\dots,\mathbf q_k, so coefficients rik=0r_{ik}=0 for i>ki>k.
Formula for off-diagonal entries of RR?
rik=qiakr_{ik}=\mathbf q_i^\top\mathbf a_k for i<ki<k.
Formula for diagonal entries of RR?
rkk=ukr_{kk}=\|\mathbf u_k\|, the norm of the orthogonalized kk-th column.
How do you solve least squares with QR?
Solve Rx=QbRx=Q^\top b by back-substitution.
Difference between thin and full QR?
Thin: QQ is m×nm\times n (only QQ=IQ^\top Q=I). Full: QQ is m×mm\times m orthogonal.
What makes the QR decomposition unique?
Requiring rkk>0r_{kk}>0 (positive diagonal).
QR of an orthogonal matrix AA?
Q=AQ=A, R=IR=I.
Which algorithm is QR built from?
The Gram–Schmidt process.
Why prefer QR over normal equations AAx=AbA^\top A x=A^\top b?
Avoids forming AAA^\top A, which squares the condition number; QR is numerically stable.

Concept Map

factorized as

contains

contains

satisfies

produces

coefficients form

normalize a1

subtract projections

normalized gives

diagonal

enables

drives

Matrix A columns skewed

A = QR

Q orthonormal columns

R upper triangular

Q transpose Q = I

Gram-Schmidt process

q1 first axis

perpendicular leftover

r_kk = norm of u_k > 0

Least squares Rx = Q transpose b

QR algorithm for eigenvalues

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, QR decomposition ka matlab simple hai: tumhare paas matrix AA hai jiske columns thode "tedhe" (skewed) hain, aur tum unhe do clean hisson me todh dete ho — A=QRA = QR. Yaha QQ ke columns orthonormal hote hain (yaani perpendicular aur length exactly 1), aur RR upper-triangular hota hai. Soch lo ki QQ ek "clean perpendicular axes" ka set hai (jaise room ka corner), aur RR wo recipe hai jo batata hai ki original tedhe columns ko in clean axes se kaise banao.

Ye banta kaise hai? Bas Gram–Schmidt process se. Pehla column le lo, normalize karke q1q_1 banao. Doosre column me se uska wo part hata do jo q1q_1 ke direction me already point kar raha hai (uski "shadow" / projection), bacha hua part automatically perpendicular hoga — usko normalize karke q2q_2. Aise hi aage badhte jao. Jo projection coefficients (qiakq_i^\top a_k) nikalte ho, wahi RR ke entries ban jaate hain. Aur kyunki har aka_k sirf pehle waale qq's se banta hai, isliye RR apne aap upper-triangular ho jaata hai — yahi key insight hai.

Iska sabse bada faayda least squares me hai. Jab Ax=bAx=b ka exact solution nahi milta, tab normal equation AAx=AbA^\top A x = A^\top b use karte hain — lekin AAA^\top A banane se numbers bigad jaate hain (condition number square ho jaata hai). QR me direct Rx=QbRx = Q^\top b solve karo back-substitution se — fast aur stable. Exam aur real computation, dono me ye trick gold hai.

Do common galtiyan yaad rakhna: thin QR me QQ square nahi hota, sirf QQ=IQ^\top Q = I hota hai (poora QQ=IQQ^\top=I nahi). Aur QR unique tabhi hota hai jab RR ke diagonal positive ho. Bas itna dhyaan rakho, baaki concept solid hai.

Go deeper — visual, from zero

Test yourself — Linear Algebra (Full)

Connections