[TOC]

qr

QR decomposition

Introduction

Let be an matrix. It can be factorised such that , where is an orthogonal matrix and is such that

In the above equation,

Permutation

The error in and can be improved by permuting columns of before factorisation. The permutation is done by multiplying with a given matrix , i.e.,

where the permuted matrix is defined as . The matrix is given such that diagonal elements of are arranged in decreasing order of their absolute values (or moduli if A is complex).

The matrix can also be constructed from a given permutation vector such that

for all and .

Thin Factorisation

Suppose that . Let , where is . We can perform the thin QR factorisation:

Note that and are sub-matrices of and , respectively. Hence, the thin factorisation is potentially more economical in terms of computation.

The error in and can be improved by permuting columns of before factorisation:

The matrix is constructed by

for all and , where is a given permutation vector.

Usages

R = qr(A)

R = qr(A, 0)

R = qr(A, 'vector')

R = qr(A, 'matrix')

[Q,R] = qr(A)

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

[Q,Rp,e] = qr(A, 'vector')

[Q,Rp,P] = qr(A, 'matrix')

[Q1,R1] = qr(A, 0)

[Q1,Rp1,e] = qr(A, 0)

Output argumentsVariables in the equationsSizesMeaning
Q
Q1 if Thin
Q1 if
R
RpPermuted
R1 if Thin
R1 if (no thinning possible)
Rp1 if Permuted thin
Rp1 if (no thinning possible)Permuted
PPermutation matrix
ePermutation vector

Examples

Input
A = rand(3,5);
[Q,R] = qr(A);
% m < n. Therefore, R has the form [R2 R3]
% where R2 is upper triangular
R
% Q*R and A are the same
% (ignoring rounding errors)
Q*R-A

B = rand(5,3);
[Q,R] = qr(B);
% m > n. Therefore, R has the form [R1;0] 
% where R1 is upper triangular
R
% Q*R and B are the same
% (ignoring rounding errors)
Q*R-B
Output
R = 
-1.2683  -0.2718  -0.7296  -0.4461  -0.9179
 0.0000  -0.7927  -0.0660   0.0097  -0.6946
 0.0000   0.0000   0.4922   0.3550   0.2499

ans = 1e-16 × 
 0.0000   0.1388   0.5551   0.5551   2.7756
-0.2776   0.0000  -0.2776   0.1388   0.0000
 0.0000  -0.1388   0.0000   0.0000   2.2204

R = 
-1.4719  -0.5101  -1.2602
 0.0000  -0.2651  -0.6190
 0.0000   0.0000  -0.3916
 0.0000   0.0000   0.0000
 0.0000   0.0000   0.0000

ans = 1e-16 × 
 0.0000   0.0000   2.7756
-1.1102   0.0000  -1.1102
 0.0000   0.0000   1.1102
 0.0000   0.5551  -1.1102
 0.0000   0.5551   1.1102
Input
A = rand(5,3);
[Q,R] = qr(A);
% R has the form [R1;0]
% where R1 is upper triangular
R

% With the second input argument being 0,
% qr() returns the upper triangular matrix
% without giving the zero matrix
[Q,R1] = qr(A,0);
R1
Output
R = 
-1.5226  -0.4083  -0.8611
 0.0000   0.7716   0.7349
 0.0000   0.0000  -0.4714
 0.0000   0.0000   0.0000
 0.0000   0.0000   0.0000

R1 = 
-1.5226  -0.4083  -0.8611
 0.0000   0.7716   0.7349
 0.0000   0.0000  -0.4714
Input
A = rand(3,5);
[Q,R,P] = qr(A,'matrix');
% A*E and Q*R are the same
% (ignoring rounding errors)
A*P-Q*R
Output
ans = 1e-16 × 
 1.1102   1.1102   0.8327   0.5551   0.0000
 0.0000   1.1102   0.9714   0.0000   0.6939
 0.0000   3.3307   0.5551  -0.1388   0.0000
Input
% Randomly generated matrix.
a = randi(10,4,5);

% Vector e contains permutation information.
[Q,R,e] = qr(a,'vector');

% Construct a permuation matrix P using e.
P = zeros(size(a,2));
for r = 1:size(a,2)
    P(e(r),r) = 1;
end

% Permutation matrix P2 given by qr()
% with second input argument being 'matrix'
[Q,R,P2] = qr(a,'matrix');

% Permutation matrices P and P2 are the same.
P
P2
Output
P = 
 0.0000   0.0000   0.0000   1.0000   0.0000
 0.0000   1.0000   0.0000   0.0000   0.0000
 0.0000   0.0000   0.0000   0.0000   1.0000
 1.0000   0.0000   0.0000   0.0000   0.0000
 0.0000   0.0000   1.0000   0.0000   0.0000

P2 = 
 0.0000   0.0000   0.0000   1.0000   0.0000
 0.0000   1.0000   0.0000   0.0000   0.0000
 0.0000   0.0000   0.0000   0.0000   1.0000
 1.0000   0.0000   0.0000   0.0000   0.0000
 0.0000   0.0000   1.0000   0.0000   0.0000