QR Decomposition Calculator
Factorize a matrix into the product A = QR, where Q has orthonormal columns and R is upper triangular. QR decomposition is commonly used in least-squares problems, numerical linear algebra, and eigenvalue algorithms.
A
Learn About QR Decomposition
In numerical linear algebra, QR decomposition factorizes an $m \times n$ matrix $\mathbf{A}$ into an orthogonal matrix $\mathbf{Q}$ and an upper triangular matrix $\mathbf{R}$:
QR decomposition is the standard method for solving linear least-squares problems, computing pseudoinverses, and finding eigenvalues via the iterative QR algorithm.
Reduced (Thin) vs. Full QR Decomposition
- Reduced (Thin) QR Decomposition ($m \ge n$):
- $\mathbf{Q} \in \mathbb{R}^{m \times n}$ has orthonormal columns ($\mathbf{Q}^T \mathbf{Q} = \mathbf{I}_n$).
- $\mathbf{R} \in \mathbb{R}^{n \times n}$ is square upper triangular.
This is the form computed by GammaCalculator. Its columns span $\operatorname{Col}(\mathbf{A})$.
- Full QR Decomposition:
- $\mathbf{Q} \in \mathbb{R}^{m \times m}$ is square orthogonal ($\mathbf{Q}^T \mathbf{Q} = \mathbf{Q} \mathbf{Q}^T = \mathbf{I}_m$).
- $\mathbf{R} \in \mathbb{R}^{m \times n}$ has an $n \times n$ upper triangular block atop $m - n$ rows of zeros.
Gram-Schmidt Construction
For column vectors $\mathbf{a}_1, \dots, \mathbf{a}_n$ of $\mathbf{A}$:
- Columns $\mathbf{q}_1, \dots, \mathbf{q}_n$ of $\mathbf{Q}$ are the normalized orthogonal vectors:
- Entries of $\mathbf{R}$ record projection lengths and norms:
All subdiagonal entries ($i > j$) are zero: $r_{ij} = 0$.
Implementation Conditions and Limitations
GammaCalculator requires:
- Tall or Square Matrices ($m \ge n$): Number of rows must equal or exceed columns.
- Full Column Rank: Columns of $\mathbf{A}$ must be linearly independent ($\operatorname{rank}(\mathbf{A}) = n$). If columns are dependent, diagonal pivots in $\mathbf{R}$ vanish ($r_{jj} = 0$).
Worked Numerical Example
Compute the thin QR decomposition of:
- First Column ($\mathbf{a}_1$):
- Second Column ($\mathbf{a}_2$):
- $r_{12} = \mathbf{q}_1 \cdot \mathbf{a}_2 = \frac{1}{\sqrt{2}}(1) + 0 + 0 = \frac{1}{\sqrt{2}}$.
- Orthogonal component:
- Norm: $\|\mathbf{u}_2\| = \sqrt{(1/2)^2 + (-1/2)^2 + 1^2} = \frac{\sqrt{6}}{2} \implies r_{22} = \frac{\sqrt{6}}{2}$.
- Normalize:
- Resulting Factors:
Verification: $\mathbf{Q}^T\mathbf{Q} = \mathbf{I}_2$ and $\mathbf{Q}\mathbf{R} = \mathbf{A}$.
How is QR decomposition used to solve linear least-squares problems?
For an overdetermined system $\mathbf{A}\mathbf{x} \approx \mathbf{b}$ ($m > n$), substituting $\mathbf{A} = \mathbf{Q}\mathbf{R}$ into the normal equations $\mathbf{A}^T\mathbf{A}\mathbf{x} = \mathbf{A}^T\mathbf{b}$ yields:
This simplifies the problem to triangular back-substitution without computing $\mathbf{A}^T\mathbf{A}$.
Why is QR decomposition preferred over normal equations?
Forming $\mathbf{A}^T\mathbf{A}$ squares the numerical condition number: $\kappa(\mathbf{A}^T\mathbf{A}) = (\kappa(\mathbf{A}))^2$. If $\kappa(\mathbf{A}) = 10^4$, then $\kappa(\mathbf{A}^T\mathbf{A}) = 10^8$, discarding half of double-precision digits. Operating on $\mathbf{R}$ preserves the condition number $\kappa(\mathbf{R}) = \kappa(\mathbf{A})$, ensuring superior stability.
Can the diagonal entries of R be negative?
By mathematical convention, the diagonal entries of $\mathbf{R}$ are strictly positive ($r_{ii} > 0$). If an elimination produces a negative entry, multiplying column $\mathbf{q}_i$ and row $\mathbf{r}_i$ by $-1$ restores positivity while leaving $\mathbf{Q}\mathbf{R}$ unchanged. Under $r_{ii} > 0$, thin QR decomposition is strictly unique.
How does Householder reflection differ from Gram-Schmidt in computing QR?
While Gram-Schmidt orthogonalizes columns sequentially, Householder reflections apply unitary reflections that zero out subdiagonal columns simultaneously. Householder reflections are unconditionally stable and preserve exact orthogonality ($\mathbf{Q}^T\mathbf{Q} = \mathbf{I}$) to machine precision.
- The QR Algorithm for Eigenvalues: In 1961, John G. F. Francis and Vera Kublanovskaya independently discovered the QR algorithm: iteratively decomposing $\mathbf{A}_k = \mathbf{Q}_k \mathbf{R}_k$ and multiplying in reverse $\mathbf{A}_{k+1} = \mathbf{R}_k \mathbf{Q}_k$. The matrices converge to triangular form, revealing all eigenvalues on the diagonal.
- Determinant Identity: For a square matrix $\mathbf{A} \in \mathbb{R}^{n \times n}$, because $\det(\mathbf{Q}) = \pm 1$ and $\mathbf{R}$ is triangular, the determinant magnitude is the product of diagonal elements of $\mathbf{R}$: