Understanding the QR decomposition calculator — concept and principle
This calculator factors a matrix with independent columns as A = Q·R, where Q has orthonormal columns and R is upper triangular with a positive diagonal. It also shows the sizes and |det A| for square matrices. Use it for linear algebra courses, solving least-squares problems stably, and as the basic step of eigenvalue algorithms.
How it is calculated
The calculator applies Gram–Schmidt to the columns a₁, …, aₙ of A:
- q₁ = a₁ ÷ ‖a₁‖.
- For each next column, subtract its projections onto the previous q's, then divide by the length: q_k = (a_k − Σ (q_j·a_k) q_j) ÷ ‖…‖.
- R = QᵀA, so r_jk = q_j · a_k for j ≤ k and 0 below the diagonal.
The thin (reduced) form is returned: Q is m × n and R is n × n. Projections are repeated once to improve numerical accuracy. For a square matrix, |det A| = product of R's diagonal.
The principle
Gram–Schmidt turns any set of independent vectors into an orthonormal set spanning the same space, one vector at a time. Each original column can then be rebuilt from the q's using only the q's that came before it, which is why R is upper triangular. Orthonormal columns make QᵀQ = I, so solving Ax = b in the least-squares sense reduces to Rx = Qᵀb, solved by back-substitution. Because Q preserves lengths, this route avoids the loss of accuracy that comes with forming AᵀA.
Worked example
Default: A = [12 −51 4; 6 167 −68; −4 24 −41].
- ‖a₁‖ = √(144 + 36 + 16) = 14, so q₁ = (6/7, 3/7, −2/7) ≈ (0.857143, 0.428571, −0.285714).
- r₁₂ = q₁·a₂ = 21. a₂ − 21q₁ = (−69, 158, 30), length 175, so q₂ ≈ (−0.394286, 0.902857, 0.171429).
- r₁₃ = −14 and r₂₃ = −70; what is left of a₃ has length 35, giving q₃ ≈ (−0.331429, 0.0342857, −0.942857).
- R = [14 21 −14; 0 175 −70; 0 0 35].
- |det A| = 14 × 175 × 35 = 85,750.
How to read the result
The diagonal entries of R are the lengths of the parts of each column that are new compared with the earlier columns. A very small diagonal entry means that column is nearly a combination of the earlier ones. QᵀQ should be the identity; you can check by multiplying. The factorisation is unique once R's diagonal is required to be positive.
Common mistakes
- Using a matrix with dependent columns; Gram–Schmidt then produces a zero vector.
- Forgetting to normalise each q.
- Expecting the full square Q when only the thin form is given.
- Reading a sign difference with other software as an error; other tools may allow negative diagonals.
Frequently asked questions
What is QR decomposition used for?
Stable least-squares solving, the QR eigenvalue algorithm, and building orthonormal bases.
Is QR unique?
Yes, for independent columns, once the diagonal of R is required to be positive.
How does it relate to Gram–Schmidt?
Q contains the Gram–Schmidt vectors and R records the coefficients used to build them.
Sources: Coordinated Linear Algebra (University of Nebraska–Lincoln), §7.6 QR Factorization and Least Square Approximations