The QR decomposition calculator above writes a matrix A as the product of a matrix Q with orthonormal columns and an upper triangular matrix R. It uses Householder reflections rather than the classical Gram-Schmidt process, because Householder is numerically stable and classical Gram-Schmidt is not. It prints both factors, and it prints two error figures that let you check the answer yourself instead of taking it on trust.
Arb Digital publishes it because QR is the factorisation that quietly underpins most of applied numerical linear algebra. It is how least-squares problems are solved in practice, how eigenvalues are found by the QR algorithm, and how an orthonormal basis is built for a subspace. If you want to see the orthonormalisation itself carried out vector by vector, our Gram-Schmidt calculator does that; this page returns the finished factorisation, computed by a different and better-behaved algorithm. For the symmetric positive definite special case use the Cholesky decomposition calculator, and for a general square system use the LU decomposition calculator.
What This QR Decomposition Calculator Does
It accepts any real matrix with at least as many rows as columns, up to eight by eight, and returns the reduced factorisation: Q has the same shape as A with orthonormal columns, and R is square and upper triangular with side length equal to the number of columns. That is the form most useful in practice, because the discarded part of the full Q spans the orthogonal complement of the column space and is rarely needed.
Three numbers come back alongside the factors. The first is the product of the magnitudes of R's diagonal entries. For a square matrix that is exactly the absolute value of the determinant, because the determinant of an orthogonal matrix is plus or minus one. For a tall matrix it is the volume of the parallelepiped spanned by the columns, which is a useful measure of how close to dependent they are.
The second is the numerical rank: the count of diagonal entries of R whose magnitude exceeds a tolerance scaled to the size of the matrix. The third and fourth are the two error figures, and they are the point of the page. A tool that shows you Q and R and nothing else is asking you to believe it. This one shows you the residuals of both defining properties.
How to Use It
- Type the matrix one row per line. Separate the entries with spaces, commas or tabs. Every row must have the same number of entries, and there must be at least as many rows as columns.
- Choose the sign convention. The default forces R's diagonal to be non-negative, which is the convention under which QR is unique for a full-rank matrix and the one you will find in textbooks. The raw setting shows what the Householder algorithm produces before any sign fixing.
- Read Q and R. They are printed row by row underneath the summary grid, rounded to the number of decimals you asked for.
- Check the two error figures. Both should be tiny. The bar display shows the magnitude of each diagonal entry of R, which is where rank deficiency becomes visible.
- Use the example buttons to load a classic textbook matrix, a tall matrix of the kind least-squares fitting produces, or a deliberately rank-deficient matrix so you can see what the tool says when the columns are dependent.
The Formula and How It Is Calculated
A Householder reflection is the matrix H = I − 2vvᵀ/(vᵀv), built from a single vector v. Geometrically it reflects space in the hyperplane perpendicular to v. It is symmetric and orthogonal, and applying it costs a dot product and a rank-one update rather than a full matrix multiplication.
The algorithm takes the first column of A, chooses v so that reflecting that column lands it on the first coordinate axis, and applies the reflection to the whole matrix. That zeroes everything below the first diagonal entry. It then repeats on the trailing submatrix, one column at a time. After n steps the matrix has been reduced to upper triangular form, and that is R. The product of the reflections, in reverse, is Q.
The one subtlety is a sign. The target vector could be plus or minus the norm times the first axis vector, and the standard choice takes the sign that makes v longest, which is the opposite sign to the leading entry of the column. Choosing the other sign risks catastrophic cancellation when the column is already nearly aligned with the axis, and that single decision is most of what makes the method stable. It is described in the MathWorld entry on the Householder matrix.
Worked example, using the classic matrix loaded by default. Its first column is (12, 6, −4), whose norm is the square root of 144 plus 36 plus 16, which is the square root of 196, which is 14. So the first diagonal entry of R is 14 under the non-negative convention. Carrying the process through gives R with diagonal 14, 175, 35, and the product of those three is 85,750. The determinant of the matrix, computed independently by cofactor expansion, is −85,750, and the magnitudes agree exactly as they must.
Why Householder Instead of Gram-Schmidt
Classical Gram-Schmidt produces the same factorisation on paper and a materially worse one in floating point. It builds each column of Q by subtracting from a column of A its projections onto the columns of Q already built. When the columns of A are nearly dependent, that subtraction cancels most of the significant digits, and the vector that survives is dominated by rounding error. The result is that Q transpose times Q drifts away from the identity, sometimes badly, on matrices that are not even especially ill conditioned.
Modified Gram-Schmidt repairs much of this by subtracting the projections one at a time from the running remainder rather than all at once from the original column. It is mathematically identical and numerically far better, and it is the honest minimum if you want a Gram-Schmidt method. Householder is better still: its orthogonality error is bounded by machine epsilon times a modest factor regardless of conditioning, because Q is never formed by subtraction at all. It is assembled from reflections, each of which is exactly orthogonal to working precision.
This matters more than it sounds. If you use QR to solve a least-squares problem and Q is not orthogonal, the normal equations you are implicitly solving are the wrong ones, and the error does not announce itself. That is exactly why this page reports the orthogonality error as a headline figure. Try the rank-deficient example and watch what a genuinely dependent column does to the diagonal of R.
What the Two Error Figures Actually Tell You
The reconstruction error answers one question: does Q times R give back A? It is the largest absolute difference across all entries. On a well-scaled matrix it should be about machine epsilon times the largest entry of A, so for entries in the hundreds you expect something around ten to the minus thirteen. A reconstruction error of one part in a thousand means the algorithm has failed, not that the matrix is difficult.
The orthogonality error answers the other: are Q's columns really orthonormal? It is the largest absolute difference between Q transpose times Q and the identity matrix. This is the figure that separates a good algorithm from a bad one, because a broken Gram-Schmidt implementation can have a small reconstruction error and a terrible orthogonality error at the same time. The product still reassembles A; it just does not do it with an orthogonal factor.
Neither figure is a measure of how well conditioned your matrix is. A matrix can have a condition number of ten to the twelfth and still factor with both errors at rounding level. What the condition number governs is how much the answer to a downstream problem moves when the input moves, which is a separate question. Our matrix rank calculator and eigenvalue calculator address different parts of that picture.
QR and the Least-Squares Problem
The reason QR is computed far more often than LU in statistical work is least squares. To fit a linear model you want the vector x minimising the length of Ax minus b, where A is tall. The textbook route forms the normal equations by multiplying through by A transpose, and that is a mistake in floating point: it squares the condition number, so a problem that was borderline becomes hopeless.
The QR route substitutes A equals QR and uses the fact that multiplying by an orthogonal matrix does not change length. The problem becomes the minimisation of the length of Rx minus Q transpose b, and because R is triangular that is solved by back substitution in a single pass. No squaring, no cancellation, and the residual falls out for free. This is what the LAPACK users' guide section on orthogonal factorizations and linear least squares problems documents, and it is why the tall example is included here. If you want the fitted line itself rather than the factorisation, our linear regression calculator does that directly.
When R Has a Zero on the Diagonal
A zero diagonal entry in R means the corresponding column of A lies in the span of the columns before it. The matrix is rank deficient, and the factorisation is no longer unique: Q's corresponding column can be any unit vector orthogonal to those before it, and different algorithms will pick different ones. Nothing has gone wrong, but the factorisation has stopped carrying the information you probably wanted.
In exact arithmetic the entry would be exactly zero. In floating point it will be a small number of no particular size, which is why the rank figure here uses a tolerance rather than an equality test. The tolerance is the largest diagonal magnitude multiplied by the matrix dimension and by machine epsilon, which is the usual convention, but it is a convention and not a fact. A matrix with diagonal magnitudes 1, 1 and 10 to the minus nine is ambiguous and no tolerance resolves it honestly.
Plain QR is also not the right tool for detecting rank reliably, because the diagonal of R is not guaranteed to reveal it. Column-pivoted QR, which reorders the columns to push the largest remaining norm to the front at each step, is much better at it, and the singular value decomposition is better still. Use the SVD calculator when rank is the actual question.
Where QR Shows Up Outside Class
The QR algorithm for eigenvalues repeatedly factors a matrix and multiplies the factors back in the other order. Under mild conditions the sequence converges to a triangular matrix whose diagonal holds the eigenvalues. That algorithm, with shifts and a preliminary reduction to Hessenberg form, is how essentially every numerical eigenvalue routine in production works, and it is named after this factorisation.
Kalman filters use QR-based square-root formulations to keep the covariance matrix positive definite through many update steps, where the naive recursion drifts. Signal processing uses it for adaptive filtering. Computer graphics uses it to recover a rotation from a matrix that has accumulated numerical drift, by discarding R and keeping Q. In each case the value is the same: an orthogonal matrix preserves lengths and angles, so it can be applied repeatedly without amplifying error.
Arb Digital builds reporting and attribution systems where the arithmetic is auditable rather than assumed.
Browse All Free Tools Talk To Our TeamCommon Mistakes to Avoid
- Comparing your Q against someone else's sign for sign — QR is unique only up to the signs of R's diagonal, so two correct answers can differ by a column of minus signs. Fix the convention before you compare.
- Assuming a small reconstruction error means a good factorisation — it does not. An almost-orthogonal Q still multiplies back to A. Check the orthogonality error too.
- Using classical Gram-Schmidt on nearly dependent columns — it is the one situation where it fails, and it is also the situation where you are most likely to be in it without noticing.
- Reading the diagonal of R as singular values — they are not, and they can be badly misleading about rank. Singular values come from the SVD.
- Forming the normal equations anyway — computing A transpose A and inverting it throws away half your precision for no gain when QR is available.
Related Free Tools From Arb Digital
See the orthonormalisation carried out vector by vector with the Gram-Schmidt calculator, get singular values and a rank you can rely on from the SVD calculator, factor a symmetric positive definite matrix with the Cholesky decomposition calculator, invert a matrix with the matrix inverse calculator, or compute the determinant directly with the determinant calculator. The full free online tools hub lists every linear algebra tool we publish.
Frequently Asked Questions
Householder reflections. Classical Gram-Schmidt gives the same factorisation in exact arithmetic but loses orthogonality badly in floating point, so it is not an honest choice for a general tool.
For a matrix with full column rank it is unique once you fix the sign convention on R's diagonal. Without that convention any column of Q can be negated with the matching row of R, and both answers are correct.
No. Any matrix with at least as many rows as columns can be factored, and the tall case is the one that matters most because it is what least-squares fitting produces.
Full QR makes Q square by adding columns spanning the orthogonal complement of the column space, and pads R with zero rows. Reduced QR, which this page returns, keeps only the columns that carry information.
Because they fail independently. Reconstruction error checks that Q times R equals A. Orthogonality error checks that Q's columns are really orthonormal. A poor implementation can pass the first and fail the second.
For a square matrix, yes. The determinant of A is plus or minus the product of R's diagonal entries, because every Householder reflection has determinant minus one and the number of them fixes the sign.
A diagonal entry of R comes out at rounding level and the factorisation stops being unique. The tool reports a reduced numerical rank, but plain QR is not a reliable rank detector and the SVD should be used when rank is the question.
Forming A transpose A squares the condition number, so a problem that was merely awkward becomes numerically hopeless. QR avoids the product entirely and solves a triangular system instead.
This page explains a numerical linear algebra method for educational purposes. All arithmetic is double precision, so a matrix that is nearly rank deficient may factor cleanly while still being unreliable to use in a downstream calculation.