Compute the QR factorization of a square matrix (2×2, 3×3, 4×4, or 5×5) using Modified Gram–Schmidt orthogonalization. Get orthogonal matrix Q, upper triangular matrix R, orthogonality check, and reconstruction error.
QR decomposition (QR factorization) factorizes a matrix A into a product A = Q R, where Q is an orthogonal matrix (QT Q = I) and R is an upper triangular matrix. This decomposition is fundamental in numerical linear algebra, used for solving linear least squares problems, eigenvalue algorithms (QR algorithm), and computing matrix inverses with enhanced stability.
For any square matrix A ∈ ℝn×n with full rank, there exists a QR factorization:
A = Q · R where QTQ = I, R is upper triangular.
Historical note: The QR decomposition was independently discovered by several mathematicians in the 20th century. The modern algorithm using Householder reflections was proposed by Alston Householder in 1958, while the Gram–Schmidt process dates back to Laplace (1816) and was refined by Erhard Schmidt (1907). The QR algorithm for eigenvalues (1961) by Francis and Kublanovskaya remains one of the most influential numerical methods in history.
Our calculator implements the Modified Gram–Schmidt (MGS) process, which is numerically more stable than the classical version. The algorithm orthogonalizes each column of A sequentially, producing orthonormal columns in Q and storing the projection coefficients in R. For each column j, we subtract projections onto all previous orthonormal vectors, then normalize. This yields a robust QR factorization even for moderately ill-conditioned matrices.
| Method | Orthogonality loss | Best for |
|---|---|---|
| Classical Gram–Schmidt | Severe (rapid loss) | Teaching / conceptual understanding |
| Modified Gram–Schmidt (MGS) | Minor (near-perfect for n≤4) | Education & small-to-medium matrices |
| Householder reflections | Excellent (backward stable) | Production code, large matrices |
Our calculator uses MGS – optimal for interactive learning and small matrices (2×2 to 4×4).
QR decomposition provides a direct and stable solution to the least squares problem min ||Ax - b||2. Instead of normal equations, we solve R x = QTb via back-substitution – avoiding condition number squaring.
The QR algorithm is the workhorse for computing eigenvalues and eigenvectors of dense matrices (used in MATLAB, R, and Python's NumPy). It forms the basis for modern eigensolvers.
QR decomposition appears in recursive least squares (RLS) filters, principal component analysis (PCA) variants, and Kalman filters for state estimation.
Finite element methods and structural analysis use QR to solve ill-conditioned systems arising from constraints and rigid body modes.
| Matrix A | Q (orthogonal) | R (upper triangular) | Error (Frobenius) |
|---|---|---|---|
| [[1,2],[3,4]] | [[-0.3162,-0.9487],[-0.9487,0.3162]] | [[-3.1623,-4.4272],[0,-0.6325]] | < 1e-14 |
| [[12,-51,4],[6,167,-68],[-4,24,-41]] | [[-0.8571,0.3943,0.3314],[-0.4286,-0.9029,0.0343],[0.2857,-0.1714,0.9429]] | [[-14.0,-21.0,14.0],[0,-175.0,70.0],[0,0,-35.0]] | ~0 |
| Hilbert 3x3 (Hij=1/(i+j-1)) | orthogonal within tolerance | ill-conditioned but valid | low |
In linear regression with more equations than unknowns, QR decomposition provides a numerically robust solution. For a dataset with 5 points and a quadratic model (3 parameters), the design matrix A (5×3) yields a QR factorization where the least-squares solution is obtained by solving R x = QT b. This method outperforms the normal equation approach when A is ill-conditioned, delivering high-precision coefficient estimates used in econometrics and machine learning.