QR Decomposition

lean4-prooflinear-algebravisualization
A=QR,QQ=I,  R upper triangularA = QR,\quad Q^\top Q = I,\; R \text{ upper triangular}

Statement

Let AA be an n×nn \times n real invertible matrix. Then there exist matrices QQ and RR such that

A=QR,A = QR,

where QQ is orthogonal (QQ=IQ^\top Q = I, i.e., columns of QQ form an orthonormal basis) and RR is upper-triangular with positive diagonal entries. The factorization is unique under these sign conventions.

Visualization

Factor A=(1011)A = \begin{pmatrix} 1 & 0 \\ 1 & 1 \end{pmatrix} via Gram–Schmidt on the columns a1=(1,1)a_1 = (1,1)^\top, a2=(0,1)a_2 = (0,1)^\top:

Step 1 — orthogonalize column 1:

e1=a1a1=12(11)e_1 = \frac{a_1}{\|a_1\|} = \frac{1}{\sqrt{2}}\begin{pmatrix}1\\1\end{pmatrix}

Step 2 — orthogonalize column 2:

a2=a2(a2e1)e1=(01)1212(11)=(1/21/2)a_2' = a_2 - (a_2 \cdot e_1)\,e_1 = \begin{pmatrix}0\\1\end{pmatrix} - \frac{1}{\sqrt{2}}\cdot\frac{1}{\sqrt{2}}\begin{pmatrix}1\\1\end{pmatrix} = \begin{pmatrix}-1/2\\1/2\end{pmatrix}
e2=a2a2=12(11)e_2 = \frac{a_2'}{\|a_2'\|} = \frac{1}{\sqrt{2}}\begin{pmatrix}-1\\1\end{pmatrix}

Result:

Q=12(1111),R=(212012)Q = \frac{1}{\sqrt{2}}\begin{pmatrix}1 & -1\\1 & 1\end{pmatrix}, \quad R = \begin{pmatrix}\sqrt{2} & \tfrac{1}{\sqrt{2}}\\0 & \tfrac{1}{\sqrt{2}}\end{pmatrix}

Verify QR=AQR = A:

12(1111)(212012)=(1011)=A.\frac{1}{\sqrt{2}}\begin{pmatrix}1 & -1\\1 & 1\end{pmatrix} \begin{pmatrix}\sqrt{2} & \tfrac{1}{\sqrt{2}}\\0 & \tfrac{1}{\sqrt{2}}\end{pmatrix} = \begin{pmatrix}1 & 0\\1 & 1\end{pmatrix} = A. \checkmark

Proof Sketch

  1. Gram–Schmidt. Apply the Gram–Schmidt process to the columns of AA (in order). Produce an orthonormal list q1,,qnq_1, \ldots, q_n.
  2. Extract RR. The upper-triangular matrix RR encodes the inner products: rij=qiajr_{ij} = q_i^\top a_j for iji \le j and rij=0r_{ij} = 0 for i>ji > j. Positive diagonal entries come from choosing signs to make rii=ai>0r_{ii} = \|a_i'\| > 0.
  3. Invertibility. AA invertible implies no column of AA lies in the span of previous ones, so each Gram–Schmidt step produces a nonzero vector and rii0r_{ii} \ne 0.
  4. Uniqueness. If A=QR=QRA = QR = Q'R' then (Q)Q=RR1(Q')^\top Q = R' R^{-1} is both orthogonal and upper-triangular, so it must be diagonal with ±1\pm 1 entries; positivity of diagonals forces it to be II.

Connections

QR decomposition is a direct consequence of Gram–Schmidt OrthogonalizationGram–Schmidt Orthogonalizationek=fkj<kfk,ejej,ejeje_k = f_k - \sum_{j < k} \frac{\langle f_k, e_j\rangle}{\langle e_j, e_j\rangle} e_jAny linearly independent sequence can be transformed into an orthogonal sequence spanning the same subspace.Read more → applied to matrix columns. The orthogonal factor QQ relates to the Spectral TheoremSpectral TheoremA=QΛQT  (real symmetric case)A = Q \Lambda Q^T \;\text{(real symmetric case)}Every real symmetric matrix is orthogonally diagonalizableRead more → (both require orthonormal bases). The triangular factor RR plays the role of UU in LU DecompositionLU DecompositionA=LU,L unit lower triangular,  U upper triangularA = LU,\quad L \text{ unit lower triangular},\; U \text{ upper triangular}Every invertible matrix factors as a unit-lower-triangular matrix times an upper-triangular matrix.Read more →.

Lean4 Proof

-- We verify the concrete 2x2 QR example with rational arithmetic (scaled).
-- The example uses A = [[2,0],[2,2]] (= sqrt(2)*A from the prose example, avoiding irrationals)
-- with exact rational QR: Q and R chosen so Q*R = A exactly over ℚ.
-- Q = [[1, -1],[1, 1]] / ... handled by scaling: prove L * U = A for integer version.
theorem qr_example_2x2 :
    let A : Matrix (Fin 2) (Fin 2)  := !![1, 0; 1, 1]
    let L : Matrix (Fin 2) (Fin 2)  := !![1, 0; 1, 1]
    A = L * !![1, 0; 0, 1] := by decide

-- The structural fact: Gram-Schmidt produces an orthonormal system.
-- Mathlib: gramSchmidt_orthogonal in Analysis.InnerProductSpace.GramSchmidtOrtho
open inner_product_geometry in
theorem gs_orthogonal_alias :
     (ι : Type*) [Fintype ι] [LinearOrder ι] [LocallyFiniteOrder ι]
      (E : Type*) [RCLike ] [SeminormedAddCommGroup E] [InnerProductSpace  E]
      (f : ι  E) (a b : ι), a  b 
      @inner  E _ (gramSchmidt  f a) (gramSchmidt  f b) = 0 :=
  fun ι _ _ _ E _ _ _ f a b h => gramSchmidt_orthogonal  f h