Lesson 3 of 15

QR Decomposition

QR Decomposition

Every matrix AA can be factored as A=QRA = QR where:

  • QQ has orthonormal columns (QTQ=IQ^T Q = I)
  • RR is upper triangular with positive diagonal entries

QR decomposition is the foundation for numerically stable least-squares, eigenvalue algorithms, and more.

Algorithm via Gram-Schmidt

Process each column of AA left-to-right:

  • Apply Gram-Schmidt to produce orthonormal column qj\mathbf{q}_{j}
  • Rij=ajqiR_{ij} = \mathbf{a}_{j} \cdot \mathbf{q}_{i} for i<ji < j (how much of each previous q\mathbf{q} is in aj\mathbf{a}_{j})
  • Rjj=wjR_{jj} = \|\mathbf{w}_{j}\| (the norm of the residual)

Example

A=(3212),Q=(0.94870.31620.31620.9487),R=(3.16232.529801.2649)A = \begin{pmatrix} 3 & 2 \\ 1 & 2 \end{pmatrix}, \quad Q = \begin{pmatrix} 0.9487 & -0.3162 \\ 0.3162 & 0.9487 \end{pmatrix}, \quad R = \begin{pmatrix} 3.1623 & 2.5298 \\ 0 & 1.2649 \end{pmatrix}

Verify: QTQ=IQ^T Q = I and Q×R=AQ \times R = A.

Your Task

Implement qr_decompose(A) returning (Q, R). Use Gram-Schmidt on the columns of A.

Python runtime loading...
Loading...
Click "Run" to execute your code.