Orthogonalize columns and audit the factorization
QR Decomposition Calculator
Paste a real matrix with at least as many rows as columns. The calculator applies modified Gram–Schmidt, displays the thin Q and upper-triangular R factors, and checks both orthogonality and reconstruction instead of presenting unaudited matrices.
Enter matrix A
Scope: 2–8 rows, 1–6 columns, rows ≥ columns, and linearly independent columns.
Thin QR factorization
Q: orthonormal columns
[ 0.707107 0.408248 ] [ 0.707107 -0.408248 ] [ 0.000000 0.816497 ]
R: upper triangular
[ 1.414214 0.707107 ] [ 0.000000 1.224745 ]
QᵀQ: identity check
[ 1.000000 0.000000 ] [ 0.000000 1.000000 ]
QR: reconstructed A
[ 1.000000 1.000000 ] [ 1.000000 0.000000 ] [ 0.000000 1.000000 ]
Column projection ledger
Column 1: normalize a₁; r₁₁ = 1.414214. Column 2: remove r₁₂q₁ with r₁₂ = 0.707107; normalize the residual; r₂₂ = 1.224745.
What QR decomposition does
For a real m-by-n matrix A with m ≥ n and linearly independent columns, a thin QR decomposition writes A = QR. Q is m-by-n and its columns are orthonormal: each has Euclidean length one, and different columns have dot product zero. R is n-by-n and upper triangular. “Thin” or “reduced” means the calculator keeps only the n orthonormal columns needed to reconstruct A rather than extending Q to a full m-by-m orthogonal matrix.
The factorization separates geometry from coordinates. Q supplies an orthonormal basis for the column space of A. R records how the original columns are expressed in that basis. The diagonal entries of R reflect the length of each new residual direction as it is introduced; entries above the diagonal are projection coefficients onto earlier Q columns.
QR is not generally unique. Multiplying a column of Q by −1 and the matching row of R by −1 preserves QR. This calculator normalizes each residual using a positive Euclidean norm, which leads to nonnegative diagonal entries when the algorithm succeeds. Software using Householder reflections may return signs that differ while still producing a correct factorization.
Modified Gram–Schmidt, step by step
1. Take a column
Begin with column aj of A. Copy it into a working vector v. For the first column, there are no earlier basis directions to remove.
2. Project
For every earlier qi, calculate rij = qiTv. Subtract rijqi from v immediately. The modified order reduces numerical loss compared with the simplest classical presentation.
3. Normalize
After earlier components are removed, set rjj = ‖v‖2. Divide v by that norm to obtain qj. A near-zero norm signals a dependent or nearly dependent column.
4. Verify
Compute QTQ and QR. The first should be identity and the second should reproduce A within floating-point rounding. The calculator reports both diagnostic errors.
Classical Gram–Schmidt can be described as calculating all projections from the original column and subtracting them together. Modified Gram–Schmidt updates the working vector after each projection. In exact arithmetic they are equivalent, but the modified form is usually better for hand-sized numerical work. Production linear algebra libraries commonly use Householder transformations because they are more stable and efficient for broad matrix workloads.
Follow the default 3×2 example
The first column is a1 = [1, 1, 0]T. Its norm is √2 ≈ 1.414214, so r11 = √2 and q1 = [1/√2, 1/√2, 0]T. This creates the first unit direction.
The second column is a2 = [1, 0, 1]T. Its projection coefficient onto q1 is r12 = q1Ta2 = 1/√2 ≈ 0.707107. Subtracting r12q1 leaves [0.5, −0.5, 1]T. That residual has norm √1.5 ≈ 1.224745, which becomes r22. Normalization gives q2 ≈ [0.408248, −0.408248, 0.816497]T.
Place q1 and q2 beside each other to form Q. Place r11, r12, and r22 in the upper triangle of R, with the lower-left entry zero. Multiplication reproduces the original three rows. QTQ is the 2×2 identity to rounding, confirming unit lengths on the diagonal and a zero cross-dot-product off the diagonal.
How to read the diagnostics
The reconstruction residual displayed here is the Frobenius norm ‖A−QR‖F: square every elementwise reconstruction difference, sum those squares, and take the square root. It should be close to zero relative to the scale of A. A raw residual of 10−10 may be excellent for entries near one million but concerning for entries near 10−12, so numerical error should be interpreted relatively.
The orthogonality error is the largest absolute element of QTQ−I. It checks both unit column norms and pairwise orthogonality. Ordinary floating-point arithmetic rarely prints exact zeros, so values near machine precision are expected. Increasing the display precision reveals more digits but does not change the internal calculation or improve conditioning.
The projection ledger is an educational trace, not a complete symbolic proof. It records which prior q directions were removed from each new column and the resulting diagonal norm. Compare those coefficients with the upper triangle of R. A very small diagonal rjj indicates that column j adds little new direction beyond previous columns, which can make downstream calculations sensitive.
Rank deficiency and near dependence
If one column is an exact linear combination of earlier columns, its residual after projection is zero. The matrix does not have full column rank, and a thin Q with one independent unit column for every input column cannot be produced by this unpivoted routine. The calculator stops and identifies the affected column.
Near dependence is more subtle. R may have a tiny diagonal entry without reaching the fixed tolerance, and the computed basis can lose accuracy. Rescaling columns, using Householder QR, and applying column pivoting can improve diagnosis. LAPACK’s pivoted QR routines are designed for rank-revealing work. Do not interpret a successful small educational factorization as a condition-number guarantee.
Where QR decomposition is useful
Least squares
For an overdetermined system Ax ≈ b with full column rank, multiply by QT and solve the triangular system Rx = QTb. This avoids explicitly forming the normal-equation matrix ATA, which can worsen conditioning.
Orthonormal bases
The columns of Q span the same column space as A but are easier to project onto. The projection of a vector b into that space is QQTb when Q has orthonormal columns.
Eigenvalue algorithms
Repeated QR factorizations underpin the QR algorithm for eigenvalues. Practical implementations use shifts, reductions, and highly optimized transformations rather than this calculator’s small matrix loop.
Data workflows
Regression, signal processing, numerical optimization, and scientific computing use QR to manage bases and triangularize problems. Stable library routines are appropriate for consequential or large calculations.
For solving a system through row operations and examining pivots, visit the verified RREF matrix calculator. For cofactors and adjugates of a 3×3 matrix, the adjoint matrix calculator addresses a separate matrix operation. QR should usually be preferred over explicit inverse formation for least-squares computation.
Input rules and reporting
Every row must have the same number of finite entries. Separate values with spaces, commas, or tabs and place each matrix row on a new line. Fractions such as 1/3 are not parsed; enter their decimal values. The interface handles 2 through 8 rows and up to 6 columns, with rows at least equal to columns. These limits keep the displayed matrices and instructional ledger readable.
When documenting a result, state whether Q is thin or full, the algorithm or software used, whether pivoting occurred, and the numerical precision. If signs differ from another source, compare QR, QTQ, and the diagonal convention before declaring an error. For serious computation, also report conditioning or rank diagnostics and use a maintained numerical library. Preserve the original matrix so another analyst can reproduce every factor and diagnostic.
Frequently asked questions
Must A be square?
No. QR is especially useful for tall matrices with more rows than columns. This calculator returns thin Q and square R. It does not accept a wide matrix because the chosen educational factorization assumes m ≥ n and full column rank.
Why does my Q have different signs from a textbook?
QR sign choices are not unique. Flipping one Q column and the corresponding R row preserves the product. Verify orthogonality and reconstruction, and compare whether both methods enforce nonnegative R diagonal entries.
Is Q inverse equal to Q transpose?
For a square orthogonal Q, yes. For the thin rectangular Q here, QTQ = I, but QQT is generally a projection matrix rather than the full identity. A rectangular matrix has no ordinary two-sided inverse.
What happens with a zero column?
Its norm is zero, so it cannot be normalized into a unit basis vector. The input lacks full column rank and this routine reports an error at that column.
Does more displayed precision make QR more accurate?
No. It only changes formatting. Internal arithmetic uses JavaScript double precision. A reliable numerical library and a more stable algorithm matter more than printing additional decimals.
Is modified Gram–Schmidt the same as LAPACK DGEQRF?
No. They compute QR factorizations but use different algorithms. LAPACK’s DGEQRF uses Householder-based blocked routines suited to high-performance, stable numerical work. This calculator uses modified Gram–Schmidt because its projection steps are transparent.
References
Netlib’s LAPACK Users’ Guide explains QR factorization through Householder transformations, and the current LAPACK documentation lists the QR routine family, including GEQRF. These primary library references establish A = QR and the production approach; the interactive ledger uses modified Gram–Schmidt for small educational matrices.