QR Call
produces the QR decomposition of a
matrix by Householder transformations
 CALL QR( q, r, piv, lindep, a<, ord><,
b>);
The QR subroutine returns the following values:
 q
 specifies an orthogonal matrix Q that is the product of
the Householder transformations applied to the m ×n
matrix A, if the b argument is not specified.
In this case, the min(m,n) Householder transformations
are applied, and q is an m ×m matrix.
If the b argument is specified, q is the m ×p
matrix Q' B that has the transposed
Householder transformations Q' applied
on the p columns of the argument matrix B.
 r
 specifies a min(m,n) ×n upper triangular
matrix R that is the upper part of the
m ×n upper triangular matrix
of the QR decomposition of the matrix A.
The matrix of the QR decomposition
can be obtained by vertical concatenation (using the
IML operator //) of the (m  min(m,n)) ×n
zero matrix to the result matrix R.
 piv
 specifies an n ×1 vector of permutations of the
columns of A; that is, on return, the QR decomposition
is computed, not of A, but of the permuted matrix
whose columns are [A_{piv[1]} ... A_{piv[n]}].
The vector piv corresponds to an
n ×n permutation matrix .
 lindep
 is the number of linearly dependent columns in matrix A
detected by applying the min(m,n) Householder transformations
in the order specified by the argument vector piv.
The inputs to the QR subroutine are as follows:
 a
 specifies an m ×n matrix A that is to be
decomposed into the product of the orthogonal matrix
Q and the upper triangular matrix .
 ord
 specifies an optional n ×1 vector that
specifies the order of Householder transformations
applied to matrix A, as follows:
 ord[j] > 0
 Column j of A is an initial column, meaning it has to
be processed at the start in increasing order of ord[j].
 ord[j] = 0
 Column j of A is allowed to be permuted in order
of decreasing residual Euclidean norm (pivoting).
 ord[j] < 0
 Column j of A is a final column, meaning it has to
be processed at the end in decreasing order of ord[j].
The default is ord[j] = j, in which case the
Householder transformations are done in the same order that
the columns are stored in matrix A (without pivoting).
 b
 specifies an optional m ×p matrix B that is to be
multiplied by the transposed m ×m matrix Q'.
If b is specified, the result q contains
the m ×p matrix Q' B.
If b is not specified, the result q
contains the m ×m matrix Q.
The QR subroutine decomposes an m ×n matrix A into
the product of an m ×m orthogonal matrix Q and an
m ×n upper triangular matrix , so that
by means of min(m,n) Householder transformations.
The m ×m orthogonal matrix Q is computed only
if the last argument b is not specified, as follows:
call qr(q,r,piv,lindep,a,ord);
In many applications, the number of rows, m, is very large.
In these cases, the explicit computation of the m ×m
matrix Q can require too much memory or time.
In the usual case where m > n,
where R is the result returned by the QR subroutine.
The n columns of matrix Q_{1} provide an
orthonormal basis for the n columns of A
and are called the range space of A.
Since the mn columns of Q_{2} are orthogonal to the n
columns of A, Q_{2}' A = 0, they provide
an orthonormal basis for the orthogonal complement of the
columns of A and are called the null space of A.
In the case where m < n,
Specifying the argument ord as an n vector lets
you specify a special order of the columns in matrix A
on which the Householder transformations are applied.
When you specify the ord argument, the columns
of A can be divided into the following groups:
 ord[j]>0: Column j of A is an
initial column, meaning it has to be processed
at the start in increasing order of ord[j].
This specification defines the first nl
columns of A that are to be processed.
 ord[j]=0: Column j of A is a
pivot column, meaning it is to be processed
in order of decreasing residual Euclidean norms.
The pivot columns of A are processed
after the nl initial columns
and before the nu final columns.
 ord[j]<0: Column j of A is a
final column, meaning it has to be processed
at the end in decreasing order of ord[j].
This specification defines the last nu
columns of A that are to be processed.
If n>m, some of these columns
will not be processed at all.
There are two special cases:
 If you do not specify the ord argument,
the default values ord[j] = j are used.
In this case, Householder transformations are
done in the same order in which the columns
are stored in A (without pivoting).
 If you set all components of ord to zero,
the Householder transformations are done in order of
decreasing Euclidean norms of the columns of A.
The resulting n ×1 vector piv specifies
the permutation of the columns of A on which the
Householder transformations are applied; that is,
on return, the QR decomposition is computed, not of
A, but of the matrix with columns that are permuted
in the order A_{piv[1]}, ... , A_{piv[n]}.
To check the QR decomposition, use the following statements
to compute the three residual sum of squares, represented by
the variables SS0, SS1, and SS2, which should be close to zero:
m = nrow(a); n = ncol(a);
call qr(q,r,piv,lindep,a,ord);
ss0 = ssq(a[ ,piv]  q[,1:n] * r);
ss1 = ssq(q * q`  i(m));
ss2 = ssq(q` * q  i(m));
If the QR subroutine detects linearly dependent columns
while processing matrix A, the column order given in
the result vector piv can differ from an explicitly
specified order in the argument vector ord.
If a column of A is found to be linearly
dependent on columns already processed, this
column is swapped to the end of matrix A.
The order of columns in the result matrix R
corresponds to the order of columns processed in A.
The swapping of a linearly dependent column of A
to the end of the matrix corresponds to the swapping
of the same column in R and leads to a zero row
at the end of the upper triangular matrix R.
The scalar result lindep counts the number of
linearly dependent columns that are detected in constructing
the first min(m,n) Householder transformations in
the order specified by the argument vector ord.
The test of linear dependence depends on the size of the
singularity criterion used; currently it is specified as 1E8.
Solving the linear system Rx = Q' b
with an upper triangular matrix R whose columns are
permuted corresponding to the result vector piv
leads to a solution x with permuted components.
You can reorder the components of x by using the index vector
piv at the lefthand side of an expression, as follows:
call qr(qtb,r,piv,lindep,a,ord,b);
x[piv] = inv(r) * qtb[1:n,1:p];
The following example solves the full
rank linear leastsquares problem.
Specify the argument b as an
m ×p matrix B, as follows:
call qr(q,r,piv,lindep,a,ord,b);
When you specify the b argument, the QR call computes the
matrix Q' B (instead of Q) as the result q.
Now you can compute the p leastsquares solutions
x_{k} of an overdetermined linear system with
an m ×n, m > n coefficient matrix A,
rank(A) = n, and p righthand sides b_{k}
stored as the columns of the m ×p matrix B:

min_{xk}  Ax_{k}  b_{k} ^{2}, k = 1, ... ,p
where  · is the Euclidean vector norm.
This is accomplished by solving the p upper
triangular systems with backsubstitution:
For most applications, m, the number of rows of
A, is much larger than n, the number of columns
of A, or p, the number of righthand sides.
In these cases, you are advised not to compute the large
m ×m matrix Q (which can consume too much memory
and time) if you can solve your problem by computing only
the smaller m ×p matrix Q' B implicitly.
For an example, use the first five columns
of the 6 ×6 Hilbert matrix A:
proc iml;
a= { 36 630 3360 7560 7560 2772,
630 14700 88200 211680 220500 83160,
3360 88200 564480 1411200 1512000 582120,
7560 211680 1411200 3628800 3969000 1552320,
7560 220500 1512000 3969000 4410000 1746360,
2772 83160 582120 1552320 1746360 698544 };
b= { 463, 13860, 97020, 258720, 291060, 116424};
n = 5; aa = a[,1:n];
call qr(qtb,r,piv,lindep,aa,,b);
if lindep=0 then x=inv(r)*qtb[1:n];
print x;
Note that you are using only the first
n rows, Q'_{1}B, of QTB.
The IFTHEN statement of the preceding code may be replaced
by the more efficient TRISOLV function, as follows:
if lindep=0 then x=TRISOLV(1,r,qtb[1:n],piv);
print x;
Both cases produce the following output:
X
1
0.5
0.3333333
0.25
0.2
For information on solving rankdeficient linear
leastsquares problems, see the RZLIND call.
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.