QR Decomposition Calculator

Compute QR decomposition via Gram-Schmidt. View orthogonal Q and upper triangular R with step-by-step process, verification, and application reference.

Matrix A
Verify A = QR
✓ Passed
Maximum entry-wise difference between A and Q·R
Q Orthogonality
Error: 1.00e+0
QᵀQ should equal the identity matrix
det(A) via R
0.000000
Product of R's diagonal entries (since det(Q) = ±1)
R Diagonal
0.0000, 0.0000, 0.0000
Diagonal entries of the upper triangular matrix R
Q Column Norms
0.0000, 0.0000, 0.0000
Each column of Q should have unit norm (= 1)
R Zero Pattern
3 sub-diagonal zeros
R is upper triangular — all entries below the diagonal are zero
Q (orthogonal matrix)
0.00000.00000.0000
0.00000.00000.0000
0.00000.00000.0000
R (upper triangular matrix)
0.00000.00000.0000
0.00000.00000.0000
0.00000.00000.0000
Verification: Q · R (should equal A)
0.00000.00000.0000
0.00000.00000.0000
0.00000.00000.0000

Gram-Schmidt Process Steps

Column 1: a1 = [0.0000, 0.0000, 0.0000]
‖u1‖ = 0.000000, e1 = [0.0000, 0.0000, 0.0000]
Column 2: a2 = [0.0000, 0.0000, 0.0000]
Subtract proj onto e1: proj = 0.0000
‖u2‖ = 0.000000, e2 = [0.0000, 0.0000, 0.0000]
Column 3: a3 = [0.0000, 0.0000, 0.0000]
Subtract proj onto e1: proj = 0.0000
Subtract proj onto e2: proj = 0.0000
‖u3‖ = 0.000000, e3 = [0.0000, 0.0000, 0.0000]

QR Properties

PropertyQR
Shape3×33×3
TypeOrthogonal (QᵀQ = I)Upper triangular
det±10.0000
InverseQ⁻¹ = QᵀBack-substitution
Column normsAll = 1N/A

Applications of QR Decomposition

ApplicationHow QR is Used
Solving Ax = bQᵀb = Rx, then back-substitute in R
Least SquaresMinimizes ‖Ax − b‖₂ for overdetermined systems
Eigenvalue AlgorithmQR iteration converges to Schur form
Determinantdet(A) = det(R) = product of diagonal of R
Orthogonal BasisColumns of Q form an orthonormal basis for col(A)

R Diagonal Magnitude

r11
0.0000
r22
0.0000
r33
0.0000
Planning notes, formulas, and examples

About the QR Decomposition Calculator

The QR decomposition factors a square matrix A into the product A = QR, where Q is an orthogonal matrix (its columns are orthonormal) and R is upper triangular. This factorization is one of the most important tools in numerical linear algebra, underpinning efficient algorithms for solving linear systems, computing eigenvalues, and performing least-squares regression.

The classical approach uses the Gram-Schmidt process: starting with the columns of A, each column is orthogonalized against all previous columns to build Q, and the projection coefficients form R. The result is a set of orthonormal vectors that span the same column space as A, paired with an upper triangular matrix that encodes the transformation.

This calculator performs Gram-Schmidt QR decomposition for 2×2 and 3×3 matrices. It shows the complete step-by-step process including each projection and normalization, verifies A = QR and QᵀQ = I, displays properties of both Q and R, and provides an application reference table showing how QR is used in practice. Load one of the presets to see a worked example.

When This Page Helps

The Gram-Schmidt process involves repeated projections and normalizations that are prone to arithmetic mistakes when done by hand. Even a small error in an early step propagates through all subsequent orthogonalizations. This calculator performs the entire process with full numerical precision, shows every intermediate step, and automatically verifies the result satisfies A = QR and QᵀQ = I. It is ideal for homework, exam preparation, and quick verification of hand computations.

How to Use the Inputs

  1. Select the matrix size (2×2 or 3×3)
  2. Enter entries for matrix A or click a preset to load an example
  3. View Q (orthogonal) and R (upper triangular) in the result matrices
  4. Check the verification outputs to confirm A = QR and QᵀQ = I
  5. Read the Gram-Schmidt steps to understand each projection
  6. Use the R diagonal bars to visualize the magnitude of diagonal entries
  7. Refer to the applications table for real-world uses of QR
Formula used
QR Decomposition: A = QR where Q is orthogonal (QᵀQ = I) and R is upper triangular. Gram-Schmidt: eⱼ = (aⱼ − Σᵢ₌₁ʲ⁻¹ ⟨aⱼ, eᵢ⟩eᵢ) / ‖...‖, Rᵢⱼ = ⟨eᵢ, aⱼ⟩.

Example Calculation

Result: Q ≈ [[0.857,−0.394,0.331],[0.429,0.903,0.034],[−0.286,0.171,0.943]], R ≈ [[14,21,−14],[0,175,−70],[0,0,−35]]

Gram-Schmidt orthogonalizes the three columns of A. The resulting Q has orthonormal columns (each has unit length, and columns are perpendicular), while R captures the projection coefficients.

Tips & Best Practices

  • The determinant of A equals the product of R's diagonal entries (det(Q) = ±1)
  • If any diagonal entry of R is zero, A is rank-deficient
  • Modified Gram-Schmidt is more numerically stable than classical Gram-Schmidt
  • Householder reflections are preferred in practice for large matrices
  • QR is the engine behind the QR eigenvalue algorithm used in LAPACK
  • For least squares: Ax ≈ b becomes Rx = Qᵀb, solvable by back-substitution

The Gram-Schmidt Process in Detail

Starting with columns a₁, a₂,..., aₙ of matrix A, the Gram-Schmidt process constructs orthonormal vectors e₁, e₂,..., eₙ. First, e₁ = a₁/‖a₁‖. Then for each subsequent column aⱼ, compute uⱼ = aⱼ − Σᵢ₌₁ʲ⁻¹ ⟨aⱼ, eᵢ⟩eᵢ (subtract projections) and normalize: eⱼ = uⱼ/‖uⱼ‖. The matrix Q = [e₁ | e₂ |... | eₙ] has orthonormal columns, and R is upper triangular with Rᵢⱼ = ⟨eᵢ, aⱼ⟩ for i ≤ j. Classical Gram-Schmidt can lose orthogonality due to floating-point errors; the modified version recomputes projections against the already-updated vectors.

QR in Numerical Linear Algebra

QR decomposition is a workhorse: it is used for solving linear systems (more stable than LU for ill-conditioned problems), least-squares regression (the standard algorithm in R's lm() and Python's numpy.linalg.lstsq), eigenvalue computation (QR algorithm), and condition number estimation. The Householder variant requires about 2n³/3 flops and is backward stable, making it the default implementation in LAPACK (dgeqrf). Givens rotations are an alternative that zeros one element at a time, useful for sparse or banded matrices.

From QR to Least Squares

For an overdetermined system Ax ≈ b (more equations than unknowns), the least-squares solution minimizes ‖Ax − b‖₂. Using QR: write A = QR, then ‖Ax − b‖₂ = ‖Rx − Qᵀb‖₂ because Q preserves norms. Minimizing this reduces to solving the triangular system Rx = Qᵀb via back-substitution. This is cheaper and more stable than forming the normal equations AᵀAx = Aᵀb, which squares the condition number.

Sources & Methodology

Last updated:

Frequently Asked Questions

  • QR decomposition factors a matrix A as A = QR, where Q has orthonormal columns (QᵀQ = I) and R is upper triangular (all entries below the diagonal are zero). It exists for every square matrix and extends to rectangular matrices.