Linear algebra · orthogonal
QR Factorization: Gram–Schmidt, Householder and Least Squares
QR factorization writes a matrix A with m rows and n columns (m ≥ n) as A = QR. The columns of Q are orthonormal, and R is an n × n upper triangular matrix. Orthonormal columns preserve lengths, so working with them adds no amplification of rounding error. That is why QR is the standard way to solve least squares problems and to extract an orthonormal basis from a set of columns.
Gram–Schmidt on the columns
The classical construction processes the columns of A one at a time. Take a₁, divide by its length to get q₁. For a₂, subtract its component along q₁ and normalize what is left. The number removed, r₁₂ = q₁ · a₂, becomes an entry of R, and the length of the remainder becomes the diagonal entry r₂₂. Each column of A is thus a combination of the q vectors found so far, which is precisely the statement that R is upper triangular.
Take A = [1 1; 1 0; 0 1]. The first column is a₁ = (1, 1, 0), with length √2, so r₁₁ = √2 and q₁ = (1, 1, 0)/√2. Next, r₁₂ = q₁ · a₂ = 1/√2. Subtracting gives (1, 0, 1) − (1/2, 1/2, 0) = (1/2, −1/2, 1), whose length is √(1/4 + 1/4 + 1) = √6/2 ≈ 1.2247. So r₂₂ = √6/2 and q₂ = (1, −1, 2)/√6. A quick check: q₁ · q₂ = (1 − 1 + 0)/√12 = 0.
Least squares through QR
Minimizing the length of Ax − b is the same as minimizing the length of Rx − Qᵀb, because multiplying by an orthogonal matrix changes no lengths. For a full-rank A the minimizer solves the triangular system Rx = Qᵀb, which needs only back substitution.
Use b = (1, 2, 3) with the matrix above. Then q₁ · b = 3/√2 and q₂ · b = (1 − 2 + 6)/√6 = 5/√6. The second row of the system reads (√6/2)x₂ = 5/√6, so x₂ = 10/6 = 5/3. The first row gives √2·x₁ + (1/√2)(5/3) = 3/√2; multiplying by √2 turns it into 2x₁ + 5/3 = 3, so x₁ = 2/3.
The normal equations confirm this: AᵀA = [2 1; 1 2] and Aᵀb = (3, 4). Then 2(2/3) + 5/3 = 3 and 2/3 + 2(5/3) = 4. The residual b − Ax = (−4/3, 4/3, 4/3) is orthogonal to both columns, as the theory requires: its dot product with column 1 is −4/3 + 4/3 = 0, and with column 2 it is −4/3 + 4/3 = 0. The same problem, solved by the operator on the left division and least squares page, uses exactly this route for tall matrices.
Householder reflections
Classical Gram–Schmidt loses orthogonality when columns are nearly parallel; the computed q vectors can drift far from perpendicular. Householder QR avoids this by applying a sequence of reflections, each of the form H = I − 2vvᵀ/(vᵀv), that map a column onto a multiple of the first unit vector. For x = (1, 1, 0), choosing v = x + ‖x‖e₁ = (1 + √2, 1, 0) reflects x to (−√2, 0, 0). Applying such reflections column by column leaves R, and the product of the reflections is Q.
The cost is about 2mn² − 2n³/3 operations, more than forming normal equations but far more reliable. Solving through AᵀA squares the condition number, so a problem with condition number 10⁸ behaves as if it were 10¹⁶, beyond double precision’s reach. QR keeps the original number.
Related tools
The columns of Q from a full-rank A span the same space as the columns of A, which is the idea behind the orthonormal basis for the range. When A is square, QR is an alternative to LU factorization, costing about twice as much but needing no pivoting. If A may be rank-deficient, column pivoting helps, and the singular value decomposition settles the question outright. For symmetric positive definite matrices, Cholesky factorization is cheaper. The linear algebra section lists the neighbors.