seegongsik
Saved words
Engineering Mathematics

QR Splits a Matrix into a Rotation and a Triangle

A = QR splits a matrix into an orthonormal Q and an upper-triangular R, solving least squares stably

A matrix's columns are usually crooked: not perpendicular to each other, all different lengths. QR splits those columns cleanly into two pieces. First it straightens the columns with Gram-Schmidt into mutually perpendicular unit vectors. That is Q, a pure rotation (or reflection) that touches neither length nor angle. Everything it had to shave off — the lengths and the overlaps — is gathered into R, which, remarkably, is always filled only in its upper triangle. A = QR: one rotation and one triangle. That single line is the most stable way to solve least squares, and the workhorse of numerical linear algebra.

Step through with the buttons. Stage 0 is A's two columns a1, a2 — slanted, unequal in length. Stage 1 runs the Gram-Schmidt of the previous lesson and straightens them. Shrinking a1's direction to unit length gives q1; subtracting a2's shadow onto q1 and normalizing the leftover orthogonal part gives q2. Now q1 and q2 are exactly perpendicular and both have length 1 (their dot product reads 0). Stage 2 produces R. Because R = Qᵀ A it is automatically upper triangular, with the lower-left entry r21 equal to 0. A is the crooked columns, Q the straightened orthonormal columns, R the small triangle that bridges them. That is A = QR.

Q's two columns are mutually perpendicular unit vectors, so applying Q to the plane doesn't distort the grid — it just turns the whole thing rigidly. Drag the angle slider. The cells neither stretch nor skew; a square stays a square, because both length and angle are preserved. Press the reflect button and it flips like a mirror, yet the columns are still orthonormal so the shape doesn't change. This is the very definition of an orthogonal matrix: QᵀQ = I. Watch QᵀQ below — at every angle it stays the identity. That is the proof that Q is nothing but a rotation or reflection.

Why is R always filled only in its upper triangle? Each entry is rij = qi · aj — the shadow of the j-th original column cast onto the i-th straightened direction. Toggle the cells to read the dot products. The key is the entries below the diagonal. r21 = q2 · a1, but q2 is exactly the part of a2 left over after removing a1's direction, so it is perpendicular to a1. Perpendicular means the dot product is 0, so r21 is 0. In general, a direction qi built later never enters an earlier column aj (j < i), because that column was already fully expressed by the earlier directions. So everything below the diagonal is 0, and R is upper triangular.

Now go the other way: take R in hand and rebuild A. Hold Q fixed as a 20-degree rotation, and as you move R's three entries with the sliders, A = QR redraws live. r11 sets the length of the first column a1 — raise it and a1 grows longer. r22 is how far the second direction reaches, the length of the orthogonal part. r12 is the shear: how much the second column tilts toward the first direction. These three numbers hold all the length and tilt, while Q merely turns the whole result by 20 degrees. You can see the division of labor: R carries the shape, Q the orientation.

Now to where QR really earns its keep: least squares. In the earlier projection lesson, the line that best fits the data was the one that makes the sum of squared residuals (the vertical dashes) smallest. Move the slope and intercept sliders to shrink the SSE. Textbooks usually say to form AᵀA and solve the normal equations. But AᵀA is sensitive to error and often numerically unstable. QR skips that step. Substitute A = QR and the normal equations collapse to R x = Qᵀ b, and since R is triangular one back-substitution finishes it. Press the button to jump to that QR solution, where the SSE is minimal. Getting the same answer more stably, without ever forming AᵀA, is what QR is good for.

In PracticeQR splits a matrix into A = QR, that is into a rotation and a triangle. Q is the Gram-Schmidt-straightened orthonormal columns, so it is a rotation (or reflection) and preserves length and angle with QᵀQ = I. R is upper triangular and each entry is rij = qi · aj, holding the columns' lengths and overlaps; the below-diagonal entries are 0 because later directions never enter earlier columns. QR's biggest use is least squares: without forming AᵀA, you solve R x = Qᵀ b by back-substitution and get the same answer more stably. So QR is the workhorse decomposition across numerical linear algebra, from linear regression and least squares to eigenvalue algorithms (the QR iteration).
Engineering Mathematics
Was this helpful? Support seegongsik