Main Content

qr

QR decomposition of symbolic matrix

Description

example

R = qr(A) returns the R factor of the QR decomposition A = Q*R. Here, A is an m-by-n symbolic matrix, R is an m-by-n upper triangular symbolic matrix, and Q is an m-by-m unitary symbolic matrix.

example

[Q,R] = qr(A) returns an upper triangular matrix R and a unitary matrix Q such that A = Q*R.

example

[Q,R,P] = qr(A) also returns a permutation matrix P such that A*P = Q*R. If all elements of A can be approximated by floating-point numbers, then this syntax chooses the permutation matrix P so that abs(diag(R)) is decreasing. Otherwise, it returns P = eye(n).

example

[___] = qr(A,"econ") returns the economy-size decomposition using any of the previous output argument combinations. The sizes of the outputs depend on the size of m-by-n matrix A:

  • If m > n, then qr computes only the first n columns of Q and the first n rows of R.

  • If mn, then the economy-size decomposition is the same as the QR decomposition without the "econ" option.

By default, when you use "econ", qr returns the permutation information as a matrix P.

example

[Q,R,P] = qr(___,outputForm) specifies whether to return the permutation information P as a matrix or a vector. For example, if outputForm is "vector", then A(:,P) = Q*R. The default value of outputForm is "matrix" such that A*P = Q*R.

[___] = qr(A,0) is equivalent to qr(A,"econ","vector"). This syntax is not recommended. Use the "econ" option instead.

example

[C,R] = qr(A,B) returns an upper triangular matrix R and a matrix C such that C = Q'*B and A = Q*R. Here, A and B must have the same number of rows.

C and R represent the solution of the matrix equation A*X = B as X = R\C.

example

[C,R,P] = qr(A,B) also returns a permutation matrix P such that A*P = Q*R. If all elements of A can be approximated by floating-point numbers, then this syntax chooses the permutation matrix P so that abs(diag(R)) is decreasing. Otherwise, it returns P = eye(n).

C, R, and P represent the solution of the matrix equation A*X = B as X = P*(R\C).

[___] = qr(A,B,"econ") returns the economy-size decomposition using any of the previous output argument combinations. The sizes of the outputs depend on the size of m-by-n matrix A:

  • If m > n, then qr computes only the first n rows of C and R.

  • If mn, then the economy-size decomposition is the same as the QR decomposition without the "econ" option.

By default, when you use "econ", qr returns the permutation information as a matrix P.

example

[C,R,P] = qr(___,outputForm) specifies whether to return the permutation information P as a matrix or a vector. For example, if outputForm is "vector", then the solution of the matrix equation A*X = B is X(P,:) = R\C. The default value of outputForm is "matrix" such that the solution to A*X = B is X = P*(R\C).

[___] = qr(A,B,0) is equivalent to qr(A,B,"econ","vector"). This syntax is not recommended. Use the "econ" option instead.

example

___ = qr(___,"real") assumes that the input arguments and intermediate results are real, and therefore, suppresses calls to abs and conj. When you use this option, qr assumes that all symbolic variables represent real numbers. All numeric arguments should be real numbers for this syntax.

Use "real" to avoid complex conjugates in the result.

Examples

collapse all

Compute the upper triangular R factor of the QR decomposition of the 4-by-4 Wilkinson's eigenvalue test matrix.

Create the 4-by-4 Wilkinson's eigenvalue test matrix.

A = sym(wilkinson(4))
A = 

(32100112100112100132)

Return the R factor of the QR decomposition without returning the Q factor.

R = qr(A)
R = 

(13241313213130013532610135368921353530053381106172533812019300035381762)

Compute the QR decomposition of the 3-by-3 Pascal matrix.

Create the 3-by-3 Pascal matrix.

A = sym(pascal(3))
A = 

(111123136)

Find the Q and R matrices representing the QR decomposition of A.

[Q,R] = qr(A)
Q = 

(33-2266330-63332266)

R = 

(3231033025220066)

Verify that A = Q*R by using isAlways.

TF = isAlways(A == Q*R)
TF = 3x3 logical array

   1   1   1
   1   1   1
   1   1   1

Use permutations to improve the numerical stability of the QR decomposition for floating-point matrices. The qr function returns permutation information either as a matrix or as a vector.

Set the number of significant decimal digits, used for variable-precision arithmetic, to 10. Approximate the 3-by-3 symbolic Hilbert matrix with floating-point numbers.

previoussetting = digits(10);
A = vpa(hilb(3))
A = 

(1.00.50.33333333330.50.33333333330.250.33333333330.250.2)

First, compute the QR decomposition of A without permutations.

[Q,R] = qr(A)
Q = 

(0.8571428571-0.50160491660.11704114720.42857142860.5684855721-0.70224688320.28571428570.65208639150.7022468832)

R = 

(1.1666666670.64285714290.4500.10171433030.1053370325000.003901371573)

Compute the difference between A and Q*R. The computed Q and R matrices do not strictly satisfy the equality A = Q*R because of round-off errors.

E = A - Q*R
E = 

(-3.469446952e-18-6.938893904e-18-4.33680869e-180-1.734723476e-18-8.67361738e-190-8.67361738e-19-4.33680869e-19)

To increase numerical stability of the QR decomposition, use permutations by specifying the syntax with three output arguments. For matrices that do not contain symbolic variables, expressions, or functions, this syntax triggers pivoting so that abs(diag(R)) in the returned matrix R is decreasing.

[Q,R,P] = qr(A)
Q = 

(0.8571428571-0.4969293466-0.13552618540.42857142860.54210474170.72280632230.28571428570.6776309272-0.6776309272)

R = 

(1.1666666670.450.642857142900.10540925530.1016446391000.003764616262)

P = 3×3

     1     0     0
     0     0     1
     0     1     0

Check the equality A*P = Q*R. QR decomposition with permutations results in smaller round-off errors.

E = A*P - Q*R
E = 

(-3.469446952e-18-4.33680869e-18-6.938893904e-180-8.67361738e-19-1.734723476e-180-4.33680869e-19-1.734723476e-18)

Now, return the permutation information as a vector by using the "vector" argument.

[Q,R,p] = qr(A,"vector")
Q = 

(0.8571428571-0.4969293466-0.13552618540.42857142860.54210474170.72280632230.28571428570.6776309272-0.6776309272)

R = 

(1.1666666670.450.642857142900.10540925530.1016446391000.003764616262)

p = 1×3

     1     3     2

Verify that A(:,p) = Q*R.

E = A(:,p) - Q*R
E = 

(-3.469446952e-18-4.33680869e-18-6.938893904e-180-8.67361738e-19-1.734723476e-180-4.33680869e-19-1.734723476e-18)

Exact symbolic computations do not result in round-off errors.

A = sym(hilb(3));
[Q,R] = qr(A);
E = A - Q*R
E = 

(000000000)

Restore the number of significant decimal digits to its previous setting.

digits(previoussetting)

You can solve systems of equations in matrix form by using qr.

Solve the system of equations A*X = b, where A is a 5-by-5 symbolic matrix and b is a 5-by-1 symbolic vector.

A = sym(invhilb(5))
A = 

(25-3001050-1400630-3004800-1890026880-126001050-1890079380-11760056700-140026880-117600179200-88200630-1260056700-8820044100)

b = sym([1:5]')
b = 

(12345)

Find matrices C and R such that C = Q'*b and A = Q*R.

[C,R] = qr(A,b);

Compute the solution X.

X = R\C
X = 

(57120197706572801271630)

Verify that X is the solution of the system A*X = b by using isAlways.

tf = isAlways(A*X == b)
tf = 5x1 logical array

   1
   1
   1
   1
   1

When solving systems of equations that contain floating-point numbers, use QR decomposition with a permutation matrix or vector.

Solve the system of equations A*X = b, where A is a 3-by-3 symbolic matrix with variable precision and b is a 3-by-1 symbolic vector with variable precision. Find matrices C and R such that C = Q'*b and A = Q*R.

previoussetting = digits(10);
A = vpa([2 -3 -1; 1 1 -1; 0 1 -1]);
b = vpa([2; 0; -1]);
[C,R,P] = qr(A,b)
C = 

(-2.110579412-0.21320071640.7071067812)

R = 

(3.316624790.3015113446-1.50755672301.705605731-1.492405014000.7071067812)

P = 3×3

     0     0     1
     1     0     0
     0     1     0

Compute the solution X.

X = P*(R\C)
X = 

(1.0-0.250.75)

Alternatively, return the permutation information as a vector.

[C,R,p] = qr(A,b,"vector")
C = 

(-2.110579412-0.21320071640.7071067812)

R = 

(3.316624790.3015113446-1.50755672301.705605731-1.492405014000.7071067812)

p = 1×3

     2     3     1

Compute the solution X with permutation vector p.

X(p,:) = R\C
X = 

(1.0-0.250.75)

Restore the number of significant decimal digits to its previous setting.

digits(previoussetting)

Compute the economy-size QR decomposition by using the "econ" option.

Create a matrix that consists of the first two columns of the 4-by-4 Pascal matrix.

A = sym(pascal(4));
A = A(:,1:2)
A = 

(11121314)

Compute the QR decomposition for this matrix.

[Q,R] = qr(A)
Q = 

(12-σ131010012-510-2310156612510-31030-6312σ13101566)where  σ1=3510

R = 

(25050000)

Now, compute the economy-size QR decomposition for this matrix. Because the number of rows is greater than the number of columns in A, qr computes only the first 2 columns of Q and the first 2 rows of R.

[Q,R] = qr(A,"econ")
Q = 

(12-351012-51012510123510)

R = 

(2505)

You can avoid complex conjugates in the result of QR decomposition by using the "real" option.

Create a matrix that includes a symbolic variable as an element.

syms x
A = [1 2; 3 x]
A = 

(123x)

Compute the QR decomposition of this matrix. By default, qr assumes that x represents a complex number, and therefore, the result contains expressions with the abs function.

[Q,R] = qr(A)
Q = 

(1010-3x10-95σ131010x10-35σ1)where  σ1=|x10-35|2+|3x10-95|2

R = 

(10103x+2100|x10-35|2+|3x10-95|2)

When you use the "real" option, qr assumes that all symbolic variables represent real numbers and can return shorter results.

[Q,R] = qr(A,"real")
Q = 

(1010-3x10-95x210-6x5+18531010x10-35x210-6x5+185)

R = 

(10103x+2100x210-6x5+185)

Input Arguments

collapse all

Symbolic input, specified as an m-by-n symbolic matrix.

Symbolic input, specified as a symbolic vector or matrix. B must have the same number of rows as A.

Shape of the permutation output, specified as "matrix" or "vector". This option controls whether the permutation output P is returned as a permutation matrix or vector. You must specify three output arguments to qr to use this option.

  • If outputForm is "matrix", then P is a permutation matrix that satisfies A*P = Q*R.

  • If outputForm is "vector", then P is a permutation vector that satisfies A(:,P) = Q*R.

Output Arguments

collapse all

R factor of the QR decomposition, returned as an upper triangular symbolic matrix that satisfies A = Q*R for an m-by-n matrix A.

Q factor of the QR decomposition, returned as a unitary symbolic matrix that satisfies A = Q*R for an m-by-n matrix A.

  • For full decomposition, qr(A) returns Q as an m-by-m unitary matrix.

  • For rectangular matrix A with m > n, the economy-size decomposition qr(A,"econ") computes only the first n columns of Q and first n rows of R.

Permutation information, returned as a matrix or vector of double-precision values. The shape of P depends on the value of outputForm.

Linear system factor, returned as a symbolic matrix representing the solution of the matrix equation A*X = B, such that C = Q'*B and X = R\C. If you specify the permutation output P, then the solution is either X = P*(R\C) when outputForm is "matrix" or X(P,:) = R\C when outputForm is "vector".

More About

collapse all

QR Decomposition of Matrix

The QR decomposition, also known as the QR factorization, expresses an m-by-n matrix A as A = Q*R. For the full decomposition, Q is an m-by-m unitary matrix, and R is an m-by-n upper triangular matrix. If the components of A are real numbers, then Q is an orthogonal matrix.

Tips

  • The upper triangular matrix R satisfies the condition R = chol(A'*A).

  • The arguments "econ" and 0 affect only the shape of the returned matrices.

  • Calling qr for numeric matrices that are not symbolic objects (not created by sym, syms, or vpa) invokes the MATLAB® qr function.

  • Matrix computations involving many symbolic variables can be slow. To increase the computational speed, reduce the number of symbolic variables by substituting the given values for some variables.

Version History

Introduced in R2014a

expand all

See Also

| | |