QR Decomposition Calculator

Decompose a square matrix into A = QR with orthogonal Q and upper-triangular R using Gram-Schmidt.
Works for 2x2, 3x3, and 4x4 matrices.

QR Decomposition

QR decomposition factors a matrix A into A = QR, where Q has orthonormal columns (Q^T Q = I, the identity) and R is upper triangular. It is the workhorse of least-squares regression and the engine behind the QR algorithm for finding eigenvalues.

This calculator uses classical Gram-Schmidt orthogonalization on the columns of A:

For each column j: take the original column, subtract its projections onto all previously orthonormalized columns, then normalize. The lengths of the original-minus-projections become the diagonal of R, and the projection coefficients fill in the off-diagonal entries of R.

Once you have Q and R, solving Ax = b becomes Rx = Q^T b — a triangular system, fast to back-substitute. For least-squares (overdetermined Ax ≈ b), the same idea: R x = Q^T b gives the least-squares solution directly, no normal equations needed. Numerical analysts strongly prefer QR over solving A^T A x = A^T b because the normal equations square the condition number and can blow up for nearly singular A.

Modified Gram-Schmidt and Householder reflections give better numerical stability than classical Gram-Schmidt, but the classical version is easiest to implement and shows the algorithm clearly. For the small (up to 4x4) matrices supported here, classical Gram-Schmidt is fine.

Worked example: A = [[12,-51,4],[6,167,-68],[-4,24,-41]]. The first column has length sqrt(196) = 14. After orthonormalizing all three columns and forming R from the projections, you get an orthogonal Q and an upper-triangular R whose product equals A.

R inherits a useful property: |det(A)| = |det(R)| = product of |diagonal entries of R|. Since R is upper triangular, this is just the product of its diagonal. Q has determinant ±1 (orthogonal matrices preserve volume).

If A is singular or has linearly dependent columns, classical Gram-Schmidt fails when one of the column lengths after subtraction comes out to zero. The calculator returns an error in that case.


How we build and check this calculator

This calculator runs entirely in your browser, so the numbers you enter stay on your device. The math behind it is written by hand and tested against worked examples and standard references before the page goes live.

SuperGlobalCalculator is independently built and maintained. See how we build and verify our calculators.

Embed This Calculator

Copy the code below and paste it into your website or blog.
The calculator will work directly on your page.