QR Decomposition Calculator
Decompose any matrix A into an orthogonal matrix Q and an upper-triangular matrix R such that A = QR. Essential for solving least-squares problems, computing eigenvalues, and numerical linear algebra.
About this calculator
QR decomposition factors a matrix A into A = Q · R, where Q is an orthogonal matrix (Q^T Q = I) and R is upper-triangular. The columns of Q form an orthonormal basis for the column space of A, obtained via the Gram-Schmidt process or Householder reflections. For a 2×2 matrix with columns a₁ and a₂, the first column of Q is q₁ = a₁ / ‖a₁‖, and r₁₁ = ‖a₁‖. The second column is q₂ = (a₂ − (a₂·q₁)q₁) / ‖a₂ − (a₂·q₁)q₁‖, with r₁₂ = a₂·q₁ and r₂₂ = ‖a₂ − r₁₂ q₁‖. The R entries on and above the diagonal store the projection lengths. QR decomposition is numerically stable and is the basis of efficient least-squares solvers and the QR eigenvalue algorithm.
How to use
Decompose A = [[2, 1], [0, 3]]. Column a₁ = (2, 0): r₁₁ = ‖a₁‖ = √(4+0) = 2; q₁ = (1, 0). Column a₂ = (1, 3): projection onto q₁: r₁₂ = a₂·q₁ = 1·1 + 3·0 = 1. Subtract: v₂ = (1 − 1·1, 3 − 1·0) = (0, 3). r₂₂ = ‖v₂‖ = 3; q₂ = (0, 1). So Q = [[1, 0], [0, 1]] and R = [[2, 1], [0, 3]]. Verify: Q·R = [[2, 1], [0, 3]] = A ✓.
Frequently asked questions
What is QR decomposition and why is it used in numerical computing?
QR decomposition expresses a matrix A as the product of an orthogonal matrix Q and an upper-triangular matrix R. Because orthogonal matrices preserve vector lengths and angles, Q^T is easy to compute (it equals Q⁻¹), making QR ideal for numerically stable computation. It is the standard algorithm behind least-squares regression solvers, and the iterative QR algorithm is the most widely used method for computing all eigenvalues of a matrix. Software libraries like LAPACK use QR routinely because it avoids the numerical pitfalls of computing A^T A directly.
How does the Gram-Schmidt method differ from Householder reflections for QR decomposition?
Classical Gram-Schmidt orthogonalizes columns one at a time by subtracting projections, but it can lose orthogonality rapidly due to floating-point cancellation for nearly dependent columns. Modified Gram-Schmidt improves stability by updating vectors incrementally but still struggles for ill-conditioned matrices. Householder reflections instead apply a sequence of carefully constructed orthogonal reflections that zero out subdiagonal entries one column at a time; this approach is far more numerically stable and is the method of choice in production linear algebra software. For small, well-conditioned matrices the differences are negligible.
How can QR decomposition be used to solve a least-squares problem?
For an overdetermined system Ax = b (more equations than unknowns), the least-squares solution minimizes ‖Ax − b‖². After factoring A = QR, multiply both sides by Q^T: Q^T Ax = Q^T b simplifies to Rx = Q^T b, because Q^T Q = I. Since R is upper-triangular, this reduced system is solved by simple back-substitution in O(n²) operations. This approach is more stable than forming and solving the normal equations A^T A x = A^T b, which squares the condition number and magnifies numerical errors.