# QR

## Purpose:

Calculates the QR decomposition of a matrix.

## Syntax:

(q, r) = QR(a, mode)

(q, r, p) = QR(a, mode)

a

-

A square or rectangular matrix.

mode

-

Optional. An integer, the form of the output matrices:

 0: Compact form 1: Standard form (default)

## Alternate Syntax:

QR(a, mode)

a

-

A square or rectangular matrix.

mode

-

Optional. An integer, the type of matrix to return:

 01: R, the upper triangular matrix. 10: Q, the orthogonal matrix. 11: Combined orthogonal and upper matrix (default).

## Returns:

(q, r) = QR(a, mode) returns separate q and r components such that q *^ r == a.

(q, r, p) = QR(a, mode) returns separate q and r components and a permutation matrix p such that q *^ r == a *^ p

QR(a, mode) returns a single component or the combined upper and orthogonal matrix.

## Example:

A = {{1, 4, 7},

{2, 5, 8},

{3, 6, 9}}

(q, r) = qr(A)

q == {{-0.267261,  0.872872,  0.408248},

{-0.534522,  0.218218, -0.816497},

{-0.801784, -0.436436,  0.408248}}

r == {{-3.741657, -8.552360, -13.363062},

{ 0.000000,  1.963961,   3.927922},

{ 0.000000,  0.000000,   0.000000}}

q' *^ q == q *^ q' == {{1, 0, 0},

{0, 1, 0},

{0, 0, 1}}

q *^ r  == {{1, 4, 7},

{2, 5, 8},

{3, 6, 9}}

The input matrix A is decomposed into a orthogonal matrix q and an upper triangular matrix r such that q *^ r == A, q' *^ q is a unitary diagonal matrix and the diagonal elements of r are ordered by decreasing magnitude.

## Example:

A = {{1, 4, 7},

{2, 5, 8},

{3, 6, 9}}

(qq, rr, p) = qr(A)

qq ==  {{-0.502571,  0.762073,  0.408248},

{-0.574367,  0.058621, -0.816497},

{-0.646162, -0.644831,  0.408248}}

rr == {{-13.9284, -3.58979, -8.75909},

{  0,      -1.05518, -0.527589},

{  0,       0,        1.11022e-015}}

p == {{0, 1, 0},

{0, 0, 1},

{1, 0, 0}}

qq *^ rr == {{7, 1, 4},

{8, 2, 5},

{9, 3, 6}}

A *^ p ==   {{7, 1, 4},

{8, 2, 5},

{9, 3, 6}}

For this case, qq is a unitary matrix, rr is an upper triangular matrix with diagonal elements of decreasing magnitude and p is the permutation matrix such that:

qq *^ rr == a *^ p

## Example:

A = {{1, 2, 3},

{4, 5, 6},

{7, 8, 10}}

b = {14, 32, 53}

(q, r) = qr(A)

v = q' *^ b

w = r \^ v

x = A \^ b

w == x == {1, 2, 3}

A *^ w == A *^ x == {14, 32, 53}

Using QR decomposition, the solution x to A *^ x = b can be computed with the factorization  v = q' *^ b and x = r \^ v.

## Example:

W1: {{1, 4, 7},

{2, 5, 8},

{3, 6, 9}}

W2: qr(W1, 01)

{{-3.74, -8.55, -13.36},

{ 0.00,  1.96,   3.93},

{ 0.00,  0.00,   0.00}}

W3: qr(W1, 10)

{{-0.27,  0.87,  0.41},

{-0.53,  0.22, -0.82},

{-0.80, -0.44,  0.41}}

W4: W3 *^ W2

{{1, 4, 7},

{2, 5, 8},

{3, 6, 9}}

## Remarks:

The input matrix A is decomposed into an orthogonal matrix q and an upper triangle matrix r such that A = q *^ r.

For a non-square MxN matrix where M > N

(q, r) = qr(A, 0)

returns the components in compact form where q is size MxN and r is size NxN. If M <= N, the compact form is identical to the standard form.

For an over determined or under determined system of equations of the form A *^ x = b, The \^ operator automatically uses QR decomposition such that:

A \^ b

returns the best solution for x in the least squares sense.

See LINFIT to fit linear combination of arbitrary basis functions to a series using the method of least squares with QR decomposition.

See DADiSP/MatrixXL to significantly optimize QR.

CHOLESKY