This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


QR factorization of a matrix

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


numeric::factorQR(A, options)


numeric::factorQR(A) returns a QR factorization A = QR of the matrix A.

The QR factorization of a real/complex m×n matrix is A = QR, where the m×m matrix Q is orthogonal/unitary and the m×n matrix R is upper triangular (i.e., Rij = 0 for j < i).

By default, a numerical factorization is computed. The matrix must not contain symbolic objects that cannot be converted to floats. Numerical symbolic expressions such as π, , etc. are accepted. They will be converted to floats, unless the option Symbolic is used.

The R factor is normalized such that its diagonal elements Rii with i = 1, …, min(m, n) are real and nonnegative.

If no return type is specified via the option ReturnType = d, the domain type of the factors Q and R depends on the type of the input matrix A:

  • The factors of an array are returned as arrays.

  • The factors of an hfarray are returned as hfarrays.

  • The factors of a dense matrix of type Dom::DenseMatrix() are dense matrices of type Dom::DenseMatrix() over the ring of expressions.

  • For all other matrices of categoryCat::Matrix, the factors are returned as matrices of type Dom::Matrix() over the ring of MuPAD® expressions. This includes input matrices A of type Dom::Matrix(...), Dom::SquareMatrix(...), Dom::MatrixGroup(...), etc.

Environment Interactions

Without the option Symbolic, the function is sensitive to the environment variable DIGITS, which determines the numerical working precision.


Example 1

We consider a quadratic matrix:

A := array(1..2, 1..2, [[1, 0] , [1, PI]]):

First, we compute a numerical factorization:

[Q1, R1] := numeric::factorQR(A)

Next, the symbolic factorization is computed:

[Q2, R2] := numeric::factorQR(A, Symbolic)

For further processing, the factors (of domain type DOM_ARRAY) are converted to elements of the matrix domain Dom::Matrix():

Q1 := matrix(Q1): R1 := matrix(R1):
Q2 := matrix(Q2): R2 := matrix(R2):

Now, the overloaded arithmetical operators +, *, ^ etc. can be used for further computations:

Q1*R1, Q2*R2

We finally verify the othogonality of the factors Q1 and Q2:

Q1 * linalg::transpose(Q1), Q2 * linalg::transpose(Q2)

delete A, Q1, R1, Q2, R2:

Example 2

We consider a non-square matrix of rank 1:

A := array(1..3, 1..2, [[0, 0], [I, 1], [I, 1]]):
numeric::factorQR(A, Symbolic)

In this case, the QR factorization is not unique. Note that the numerical factorization yields different factors:


delete A:

Example 3

We demonstrate the difference between hardware floats and software floats. For rank deficient matrices, the QR factorization is not unique. Depending on the options, different results are returned for the following matrix of rank 1:

A := matrix([[1, 1], [10^4, 10^4], [10^8, 10^8]]):
[Q1, R1] := float(numeric::factorQR(A, Symbolic))

[Q2, R2] := numeric::factorQR(A, SoftwareFloats)

[Q3, R3] := numeric::factorQR(A, HardwareFloats)

However, all factorizations satisfy A = QR numerically:

norm(A - Q1*R1), norm(A - Q2*R2), norm(A - Q3*R3)

delete A, Q1, R1, Q2, R2, Q3, R3:

Related Examples



An m×n matrix of domain type DOM_ARRAY, DOM_HFARRAY, or of category Cat::Matrix


Hard, HardwareFloats, Soft, SoftwareFloats

With Hard (or HardwareFloats), computations are done using fast hardware float arithmetic from within a MuPAD session. Hard and HardwareFloats are equivalent. With this option, the input data are converted to hardware floats and processed by compiled C code. The result is reconverted to MuPAD floats and returned to the MuPAD session.

With Soft (or SoftwareFloats) computations are dome using software float arithmetic provided by the MuPAD kernel. Soft and SoftwareFloats are equivalent. SoftwareFloats is used by default if the current value of DIGITS is larger than 15 and the input matrix A is not of domain type DOM_HFARRAY.

Compared to the SoftwareFloats used by the MuPAD kernel, the computation with HardwareFloats may be many times faster. Note, however, that the precision of hardware arithmetic is limited to about 15 digits. Further, the size of floating-point numbers may not be larger than approximately 10308 and not smaller than approximately 10- 308.

If no HardwareFloats or SoftwareFloats are requested explicitly, the following strategy is used: If the current value of DIGITS is smaller than 16 or if the matrix A is a hardware float array of domain type DOM_HFARRAY, then hardware arithmetic is tried. If this is successful, the result is returned.

If the result cannot be computed with hardware floats, software arithmetic by the MuPAD kernel is tried.

If the current value of DIGITS is larger than 15 and the input matrix A is not of domain type DOM_HFARRAY, or if one of the options Soft, SoftwareFloats or Symbolic is specified, MuPAD computes the result with its software arithmetic without trying to use hardware floats first.

There may be several reasons for hardware arithmetic to fail:

  • The current value of DIGITS is larger than 15.

  • The data contains symbolic objects.

  • The data contains numbers larger than 10308 or smaller than 10- 308 that cannot be represented by hardware floats.

If neither HardwareFloats nor SoftwareFloats is specified, the user is not informed whether hardware floats or software floats are used.

If HardwareFloats are specified but fail due to one of the reasons above, a warning is issued that the (much slower) software floating-point arithmetic of the MuPAD kernel is used.

Note that HardwareFloats can only be used if all input data can be converted to floating-point numbers.

The trailing digits in floating-point results computed with HardwareFloats and SoftwareFloats may differ.

    Note:   For ill conditioned matrices the results returned with HardwareFloats and SoftwareFloats may differ significantly! See Example 3.


Prevents the conversion of the input data to floating-point numbers. Exact arithmetic is used. This option overrides HardwareFloats and SoftwareFloats.

The usual arithmetic for MuPAD expressions is used. With this option, the matrix A may contain symbolic objects.


Suppresses warnings

If symbolic coefficients are found, numeric::factorQR automatically switches to the Symbolic mode with a warning. With this option, this warning is suppressed; the routine still uses the symbolic mode for symbolic coefficients, i.e., exact arithmetic without floating-point conversions is used.


Option, specified as ReturnType = d

Return the Cholesky factor as a matrix of domain type d. The following return types are available: DOM_ARRAY, DOM_HFARRAY, Dom::Matrix(), or Dom::DenseMatrix().

Return Values

List [Q, R] with matrices Q and R is returned. The domain type of the orthogonal/unitary m×m matrix Q and the upper triangular m×n matrix R depends on the type of the input matrix A. The components of Q and R are real or complex floats, unless the option Symbolic is used.


Householder transformations are used to compute the numerical factorization. With the option Symbolic, Gram-Schmidt orthonormalization of the columns of A is used.

For an invertible square matrix A, the QR factorization is unique up to scaling factors of modulus 1. The normalization of R to real positive diagonal elements determines the factorization uniquely. Consequently, the results obtained with/without the option Symbolic coincide for invertible square matrices.

For singular or non-square matrices, the factorization is not unique and the results obtained with/without the option Symbolic may differ. Cf. Example 2.

Was this topic helpful?