yields the QR decomposition as the pair {q,r}, where q is a unitary matrix and r is an upper‐triangular matrix.
QRDecomposition[m,Full]
returns the full QR decomposition even when m is not square.
QRDecomposition
yields the QR decomposition as the pair {q,r}, where q is a unitary matrix and r is an upper‐triangular matrix.
QRDecomposition[m,Full]
returns the full QR decomposition even when m is not square.
Details and Options
- The original matrix m is equal to ConjugateTranspose[q].r. This can be simplified to Transpose[q].r if m is real. »
- The matrix q is unitary. If the input is real, then q is additionally real orthogonal. »
- The number of columns in
is always equal to the number of rows in
, while
and
always have the same number of columns. » - When using QRDecomposition[m], the number of rows in q and r both equal MatrixRank[m]. »
- When using QRDecomposition[m,Full], the matrix q is always square, and r has the same dimensions as m. »
- QRDecomposition takes the following options:
-
Pivoting False whether to use pivoting TargetStructure Automatic the structure of the returned matrix - QRDecomposition[m,Pivoting->True] yields a list {q,r,p} where p is a permutation matrix such that m.p is equal to ConjugateTranspose[q].r. »
- Possible settings for TargetStructure include:
-
Automatic automatic settings "Dense" return all matrices as lists of lists "Sparse" return all matrices as SparseArray objects "Structured" return r as an UpperTriangularMatrix object, q as a UnitaryMatrix or OrthogonalMatrix object and p as a PermutationMatrix object - When using the setting TargetStructure"Structured", whether q is returned as a UnitaryMatrix object or an OrthogonalMatrix object depends on whether the input was real.
Examples
open all close allBasic Examples (3)
The decomposition of a 2×2 matrix into a unitary (orthogonal) matrix
and upper triangular matrix
:
m = (| | |
| - | - |
| 1 | 2 |
| 3 | 4 |);
{q, r} = QRDecomposition[m];
{q//MatrixForm, r//MatrixForm}m == ConjugateTranspose[q].rCompute the QR decomposition for a 3×2 matrix with exact values:
{q, r} = QRDecomposition[(| | |
| - | - |
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |)];
{q//MatrixForm, r//MatrixForm}Transpose[q].r//MatrixFormCompute the QR decomposition for a 2×3 matrix with approximate numerical values:
{q, r} = QRDecomposition[(| | | |
| -- | -- | -- |
| 1. | 2. | 3. |
| 4. | 5. | 6. |)];
{q//MatrixForm, r//MatrixForm}Transpose[q].r//MatrixFormScope (13)
Basic Uses (6)
Find the QR decomposition of a machine-precision matrix:
QRDecomposition[{{1.2, 2.3, 3.4}, {2.3, 4.5, 5.6}, {3.2, 7.6, 6.5}}]Map[MatrixForm, %]QR decomposition for a complex matrix:
m = (| | | |
| -------------- | -------------- | -------------- |
| 0.18 + 0.26 I | 0.22 + 0.19 I | 0.4 + 0.26 I |
| 0.26 + 0.54 I | 0.79 + 0.43 I | 0.14 + 0.26 I |
| 0.63 + 0.64 I | 0.33 + 0.42 I | 0.52 + 0.12 I |);
{q, r} = QRDecomposition[m];
MatrixForm /@ %Verify the decomposition using ConjugateTranspose:
ConjugateTranspose[q].r == mUse QRDecomposition for an exact matrix:
MatrixForm /@ QRDecomposition[{{3, 1, 5}, {2, 4, 6}, {8, 7, 9}}]QR decomposition for an arbitrary-precision matrix:
MatrixForm /@ QRDecomposition[RandomReal[5, {3, 3}, WorkingPrecision -> 10]]Use QRDecomposition with a symbolic matrix:
FullSimplify[QRDecomposition[{{a, b}, {c, d}}], {a, b, c, d}∈ℝ]The QR decomposition for a large numerical matrix is computed efficiently:
mat = RandomReal[{0, 9}, {1000, 1000}];AbsoluteTiming[QRDecomposition[mat];]The Full Decomposition (3)
A rank deficient, square matrix:
m = (| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |);
MatrixRank[m]QRDecomposition[m] gives a rectangular matrix:
{qt, rt} = QRDecomposition[m];
MatrixForm /@ %QRDecomposition[m,Full] adds additional rows, q square and r the same shape as m:
{qf, rf} = QRDecomposition[m, Full];
MatrixForm /@ %Both pairs are valid decompositions:
qt.rt == m == qf.rfThe leading rows of the two decompositions are the same:
qt == qf[[1 ;; 2]] && rt == rf[[1 ;; 2]]The r matrix is always padded with zero rows:
rf[[3 ;; ]]The full decomposition always adds rows when the input is tall:
m = (| | |
| - | - |
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |);
MatrixForm /@ QRDecomposition[m]MatrixForm /@ QRDecomposition[(| | |
| - | - |
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |), Full]This is because a tall matrix can never have full row rank:
MatrixRank[m] < Length[m]For wide matrix m, the full and thin decompositions coincide:
m = (| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |);
QRDecomposition[m] == QRDecomposition[m, Full]This is because it has full row rank:
MatrixRank[m] == Length[m]For the wide matrix d, the two decompositions are not the same:
d = (| | | |
| - | - | - |
| 1 | 2 | 3 |
| 2 | 4 | 6 |);
QRDecomposition[d] == QRDecomposition[d, Full]This is because d does not have full row rank:
MatrixRank[d] < Length[d]Special Matrices (4)
Find the QR decomposition for a sparse matrix:
SparseArray[{{1, 3} -> 1, {2, 2} -> 2, {3, 1} -> 3}, {3, 3}]QRDecomposition[%]SparseArray[{{x_, y_} /; Abs[x - y] < 3 -> 3}, {5, 5}]Map[MatrixForm, QRDecomposition[%]]QR decompositions of structured matrices:
SymmetrizedArray[{{1, 1} -> 2, {1, 2} -> 1}, {2, 2}, Symmetric[All]]QRDecomposition[%]Use with a QuantityArray structured matrix that has consistent units:
QuantityArray[{{1, 4}, {3, 5}}, "Meters"]The
matrix is dimensionless; the
matrix gets the units:
QRDecomposition[%]QRDecomposition[m] when m is an identity matrix consists of two identity matrices:
QRDecomposition[IdentityMatrix[3]]QRDecomposition[IdentityMatrix[{3, 4}]]QR decomposition of HilbertMatrix:
Map[MatrixForm, QRDecomposition[HilbertMatrix[3]]]Options (4)
Pivoting (1)
Compute the QR decomposition using machine arithmetic with pivoting:
m = (| | | | |
| :- | :- | :- | :- |
| 1 | 2 | 3 | 4 |
| 1 | 4 | 9 | 16 |
| 1 | 8 | 27 | 64 |);
{q, r, p} = QRDecomposition[N[m], Pivoting -> True]The elements along the diagonal of r are in order of decreasing magnitude:
MatrixForm[r]The matrix p is a permutation matrix:
MatrixForm[p]QRDecomposition satisfies m.p==ConjugateTranspose[q].r:
ConjugateTranspose[q].r - m.p//ChopTargetStructure (3)
With default settings, QRDecomposition returns a list of two dense matrices for real input:
ma = RandomReal[1, {5, 3}];
QRDecomposition[ma]With TargetStructure->"Structured", the result is a pair of OrthogonalMatrix and UpperTriangularMatrix objects:
QRDecomposition[ma, TargetStructure -> "Structured"]With the default setting of TargetStructure, the pivot matrix is returned as a SparseArray object:
ma = RandomReal[1, {5, 3}];
QRDecomposition[ma, Pivoting -> True]Use TargetStructure"Dense" to get all three matrices as ordinary lists of lists:
QRDecomposition[ma, Pivoting -> True, TargetStructure -> "Dense"]With the setting TargetStructure->"Structured", the result for real input is a triple of OrthogonalMatrix, UpperTriangularMatrix and PermutationMatrix objects:
QRDecomposition[ma, Pivoting -> True, TargetStructure -> "Structured"]For complex input and TargetStructure->"Structured", QRDecomposition returns a pair of UnitaryMatrix and UpperTriangularMatrix objects:
ma = RandomComplex[1 + I, {6, 4}];
QRDecomposition[ma, TargetStructure -> "Structured"]Get the result as sparse matrices instead:
QRDecomposition[ma, TargetStructure -> "Sparse"]Applications (8)
Geometry of QRDecomposition (4)
Find an orthonormal basis for the column space of the following matrix
, and then use that basis to find a QR factorization of
:
a = (| | | |
| -- | -- | -- |
| 1 | 3 | 7 |
| -1 | 2 | -3 |
| -1 | 2 | -9 |
| 1 | -4 | 7 |
| 1 | 2 | 1 |);{n, m} = Dimensions[a]Define
as the
column of
and
as the
element of the corresponding Gram–Schmidt basis:
Table[v[k] = a[[All, k]], {k, m}];Table[e[k] = Normalize[v[k] - Underoverscript[∑, j = 1, k - 1]Projection[v[k], e[j]]], {k, m}]//MatrixFormLet
be the matrix whose rows are the
:
q = %;Let
be the matrix whose elements are the components of
along the
basis vector:
(r = Table[e[i].v[j], {i, m}, {j, m}])//MatrixFormTranspose[q].r == aThis is the same result as given by QRDecomposition:
{q, r} == QRDecomposition[a]Compare QR decompositions found using Orthogonalize and QRDecomposition for the following matrix
:
a = (| | | |
| - | - | - |
| 7 | 1 | 1 |
| 2 | 0 | 0 |
| 9 | 4 | 1 |);Let
be the result of applying Orthogonalize to the columns of
:
MatrixForm[q = Orthogonalize[Transpose[a]]](r = q.a)//MatrixFormTranspose[q].r == aThis is the same result as given by QRDecomposition:
{q, r} == QRDecomposition[a]Compare QR decompositions found using Orthogonalize and QRDecomposition for the following matrix
:
a = (| | | |
| ------------------------------------------ | ------------------------------------------- | ------------------------------------------ |
| 0.3777175008628273 + 0.7554306035890215 I | 0.042448606227943886 + 0.6597841389667563 I | 0.5294306313261885 + 0.9489845876390817 I |
| 0.06926665232206441 + 0.7454105710412986 I | 0.3752984378635884 + 0.07592515378172049 I | 0.934180626643051 + 0.6965119371223651 I |
| 0.3003335850354458 + 0.8656874330066835 I | 0.5891726583893067 + 0.4589922313534083 I | 0.8285542124029186 + 0.32894896452410505 I |);Let
be the result of applying Orthogonalize to the complex-conjugated columns of
:
MatrixForm[q = Orthogonalize[ConjugateTranspose[a]]](r = q.a//Chop)//MatrixFormConjugateTranspose[q].r == aUp to sign, this is the same result as given by QRDecomposition:
MatrixForm /@ QRDecomposition[a]For some applications, it use useful to compute a so-called full QR decomposition, in which the
is square (and thus unitary) and
has the same dimensions as the input matrix. Compute the full QR decomposition for the following matrix
:
m = (| | | |
| ----- | ----- | ----- |
| 1 | 2 | 3 |
| 4 I | 5 I | 6 I |
| -7 | -8 | -9 |
| -10 I | -11 I | -12 I |);There are only two linearly independent columns, so
and
each have only two rows:
{q, r} = QRDecomposition[m];
{q//MatrixForm, r//MatrixForm}Use NullSpace to find vectors outside the span of the rows of
, then orthogonalize the complete set:
MatrixForm[qF = Orthogonalize[Join[q, NullSpace[q]]]]UnitaryMatrixQ[qF]Simply pad the
matrix with zeros to make it the same shape as
:
MatrixForm[rF = PadRight[r, Dimensions[m]]]Verify that this is also a valid QR decomposition:
ConjugateTranspose[qF].rF == mLeast Squares and Curve Fitting (4)
Use the QR decomposition to find the
that minimizes
for the following matrix
and vector
:
m = (| | | |
| -- | -- | -- |
| 2 | -2 | -2 |
| -2 | 2 | 0 |
| 2 | -2 | 0 |
| 2 | 2 | 0 |
| 2 | 1 | -1 |);
b = {6, 1, 4, 9, 0};{q, r} = QRDecomposition[m];
{q//MatrixForm, r//MatrixForm}Since
,
, and the normal equations
can be recast as
:
Transpose[m].m == Transpose[r].rAs
is invertible (because the columns of
are linearly independent), the solution is
:
Inverse[r].q.bConfirm the result using LeastSquares:
% == LeastSquares[m, b]Use the QR decomposition to solve
for the following matrix
and vector
:
m = (| | | | | |
| -- | -- | -- | -- | -- |
| 0 | 0 | -1 | -1 | 1 |
| -2 | -1 | -1 | 1 | 0 |
| 0 | -2 | -2 | 0 | -1 |);
b = {7, 9, 0};Compute the QR decomposition of
, which gives an invertible
, as
has linearly independent rows:
{q, r} = QRDecomposition[Transpose[m]];
{q//MatrixForm, r//MatrixForm}Let
as if solving the least-squares problem:
x = Transpose[q].Inverse[Transpose[r]].b//FullSimplifyAs the columns of
span
,
must be a solution of the equation:
m.x == bQRDecomposition can be used to find a best-fit curve to data. Consider the following data:
data = IconizedObject[«Data»];
ListPlot[data]Extract the
and
coordinates from the data:
{x, y} = Extract[data, {{All, 1}, {All, 2}}];Let
have the columns
and
, so that minimizing
will be fitting to a line
:
m = Map[x |-> {1, x}, x];As the columns of
are linearly independent, the coefficients for a linear least‐squares fit are
:
{q, r} = QRDecomposition[m];
{a, b} = Inverse[r].q.yVerify the coefficients using Fit:
Fit[data, {1, s}, s]Plot the best-fit curve along with the data:
Show[Plot[a + b x, {x, 0, 1}], ListPlot[data]]Find the best-fit parabola to the following data:
data = IconizedObject[«Data»];
ListPlot[data]Extract the
and
coordinates from the data:
{x, y} = Extract[data, {{All, 1}, {All, 2}}];Let
have the columns
,
and
, so that minimizing
will be fitting to
:
m = Map[x |-> {1, x, x ^ 2}, x];As the columns of
are linearly independent, the coefficients for a least‐squares fit are
:
{q, r} = QRDecomposition[m];
{a, b, c} = Inverse[r].q.yVerify the coefficients using Fit:
Fit[data, {1, s, s ^ 2}, s]Plot the best-fit curve along with the data:
Show[Plot[a + b x + c x ^ 2, {x, 0, 3}], ListPlot[data]]Properties & Relations (12)
The original matrix is equal to ConjugateTranspose[q].r:
mℂ = RandomComplex[1 + I, {3, 4}];
{qℂ, rℂ} = QRDecomposition[mℂ];
Chop[mℂ - ConjugateTranspose[qℂ].rℂ]This can be simplified to Transpose[q].r if the input is real:
mℝ = RandomReal[1, {3, 4}];
{qℝ, rℝ} = QRDecomposition[mℝ];
Chop[mℝ - Transpose[qℝ].rℝ]The q matrix in the decomposition is unitary:
{qℂ, rℂ} = QRDecomposition[RandomComplex[1 + I, {3, 4}]];
UnitaryMatrixQ[q]If the input is real, then q is additionally real orthogonal:
{qℝ, rℝ} = QRDecomposition[RandomReal[1, {3, 4}]];
OrthogonalMatrixQ[q] && Element[q, Reals]The r matrix of the decomposition is always upper triangular:
UpperTriangularMatrixQ[rℂ] && UpperTriangularMatrixQ[rℝ]If
is an
matrix, the
matrix will have
columns and the
matrix
columns:
a = RandomReal[1, {3, 5}];
{n, m} = Dimensions[a];
{q, r} = QRDecomposition[a];
n == Last[Dimensions[q]] && m == Last[Dimensions[r]]QRDecomposition[m] computes the thin decomposition, where
and
have MatrixRank[m] rows:
m = (| | | | |
| - | -- | -- | -- |
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 |);
{q, r} = QRDecomposition[m];
MatrixRank[m] == Length[q] == Length[r]{q//MatrixForm, r//MatrixForm}QRDecomposition[m,Full] gives an r matrix whose dimensions are the same as m:
m = (| | | | |
| - | -- | -- | -- |
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 |);
{q, r} = QRDecomposition[m, Full];
Dimensions[r] == Dimensions[m]The matrix q is square, with dimensions equal to the number of rows in m:
SquareMatrixQ[q] && Length[q] == Length[m]{q//MatrixForm, r//MatrixForm}If m is real-valued and invertible, the
matrix of its QR decomposition is square and orthogonal:
m = RandomReal[1, {5, 5}];
MatrixRank[m] == 5{q, r} = QRDecomposition[m];
SquareMatrixQ[q] && OrthogonalMatrixQ[q]If m is invertible, the
matrix of its QR decomposition is square and unitary:
m = RandomComplex[1 + I, {5, 5}];
MatrixRank[m] == 5{q, r} = QRDecomposition[m];
SquareMatrixQ[q] && UnitaryMatrixQ[q]If a is an
matrix and MatrixRank[a]==n, the
matrix of its QR decomposition is square and unitary:
a = RandomComplex[1 + I, {3, 5}];
{n, m} = Dimensions[a];
MatrixRank[a] == n{q, r} = QRDecomposition[a];
SquareMatrixQ[q] && UnitaryMatrixQ[q]If a is an
matrix and MatrixRank[a]==m, the
matrix of its QR decomposition is invertible:
a = RandomReal[1, {5, 3}];
{n, m} = Dimensions[a];
MatrixRank[a] == m{q, r} = QRDecomposition[a];
Det[r] != 0Moreover, PseudoInverse[a]==Inverse[r].q:
PseudoInverse[a] - Inverse[r].q//ChopOrthogonalize can be used to compute a QR decomposition:
m = RandomComplex[1 + I, {5, 5}];
q = Orthogonalize[ConjugateTranspose[m]];
r = q.m;
m == ConjugateTranspose[q].r && UnitaryMatrixQ[q] && UpperTriangularMatrixQ[r]For an approximate matrix, it is typically different from the one found by QRDecomposition:
{q, r} == QRDecomposition[m]LeastSquares and QRDecomposition can both be used to solve the least-squares problem:
m = {{1, 2}, {3, 4}, {1, 1}};
b = {1, 2, 3};
{q, r} = QRDecomposition[m];LeastSquares[m, b] == Inverse[r].q.bThe Cholesky decomposition of
coincides with
's QR decomposition up to phase:
m = RandomComplex[1 + I, {6, 3}];Compute CholeskyDecomposition[ConjugateTranspose[m].]m:
u = CholeskyDecomposition[ConjugateTranspose[m].m];Find the QR decomposition of
:
{q, r} = QRDecomposition[m];
is the same as
except for the choice of phase for each row:
Chop[r Sign[Tr[r, List]] - u]Related Guides
History
Introduced in 1991 (2.0) | Updated in 2026 (15.0)
Text
Wolfram Research (1991), QRDecomposition, Wolfram Language function, https://reference.wolfram.com/language/ref/QRDecomposition.html (updated 2026).
CMS
Wolfram Language. 1991. "QRDecomposition." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2026. https://reference.wolfram.com/language/ref/QRDecomposition.html.
APA
Wolfram Language. (1991). QRDecomposition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/QRDecomposition.html
BibTeX
@misc{reference.wolfram_2026_qrdecomposition, author="Wolfram Research", title="{QRDecomposition}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/QRDecomposition.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_qrdecomposition, organization={Wolfram Research}, title={QRDecomposition}, year={2026}, url={https://reference.wolfram.com/language/ref/QRDecomposition.html}, note=[Accessed: 13-June-2026]}