QR Decomposition Calculator

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.

? Classic 3x3 (Householder demo)
? Hilbert 3x3
? 2x2 [[1,2],[3,4]]
? 4x4 integer test
? 5x5 random (demo)
Note on uniqueness: QR decomposition is not unique – signs of rows/columns can vary. Our Modified Gram–Schmidt algorithm produces an upper triangular R with positive diagonal entries. Other methods (Householder) may give different signs, but A = Q·R always holds.
Privacy first: All calculations run locally in your browser. No matrix data is transmitted.

What is QR Decomposition?

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.

The Modified Gram–Schmidt Algorithm

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.

Numerical Stability Comparison
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).

  • Step 1: Initialize Q with a copy of A.
  • Step 2: For each column j, compute its norm → Rjj; if non-zero, normalize the column.
  • Step 3: For all later columns i>j, compute Rji = QjT·Qi and subtract the component.
  • Result: Q orthonormal, R upper triangular, and A = Q·R up to machine precision.

Applications & Real-World Relevance

Least Squares Fitting

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.

Numerical Linear Algebra

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.

Signal Processing & Statistics

QR decomposition appears in recursive least squares (RLS) filters, principal component analysis (PCA) variants, and Kalman filters for state estimation.

Engineering Mechanics

Finite element methods and structural analysis use QR to solve ill-conditioned systems arising from constraints and rigid body modes.

Step-by-Step Usage

  1. Select matrix size: 2x2, 3x3, or 4x4 from the dropdown.
  2. Enter numeric values into the matrix input grid (real numbers supported).
  3. Click Compute QR Decomposition to factorize.
  4. Inspect orthogonal matrix Q and upper triangular matrix R.
  5. Check validation metrics: orthogonality error ||QTQ - I||F and reconstruction error ||A - QR||F.
  6. Use preset examples to explore known factorizations.

Numerical Examples & Verified Results

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
Case Study: Solving Overdetermined Systems

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.

Frequently Asked Questions

For full-rank square matrices, QR factorization is unique if we require the diagonal entries of R to be positive. Otherwise, signs may differ. Our algorithm returns R with positive diagonal entries when possible.

The algorithm still produces Q and R, but some diagonal entries of R may be near zero. The orthogonality of Q holds mathematically, but the factorization may have reduced rank. A warning is displayed if R has zero pivot.

This version supports square matrices up to 4x4. For rectangular matrices, the underlying method extends (economy-size QR), but we focus on the classic square case for educational clarity.

Double-precision arithmetic ensures relative errors around 1e-15 for well-conditioned matrices. Validation metrics show both orthogonality and reconstruction errors.

Gram-Schmidt (MGS) is intuitive and straightforward; Householder transformations are more numerically stable for large matrices but more complex. For small matrices (≤4x4), MGS provides excellent accuracy and transparency.

QR is more stable for ill‑conditioned matrices and is preferred for least‑squares problems. LU decomposition is faster for square systems with moderate conditioning (typically 2×–3× speedup), but can fail or become unstable for near‑singular matrices. Use QR when robustness is critical.

That indicates the original matrix is rank‑deficient (linearly dependent columns). The algorithm still produces a factorization, but Q is no longer fully orthogonal – the zero column corresponds to a zero pivot in R. Check the reconstruction error; if it is small, the factorization is still valid for some purposes, but the orthogonality property is lost.

Specialization: matrix decompositions and iterative methods. Last updated: April 2026. This tool is actively maintained and validated against standard references (Golub & Van Loan, Trefethen & Bau).

Authoritative references: Wolfram MathWorld – QR Decomposition; Golub, G. H. & Van Loan, C. F. (2013). "Matrix Computations"; Trefethen, L. N. & Bau, D. (1997). "Numerical Linear Algebra". This tool is designed by numerical analysts and reviewed for educational integrity.