Professional Documents
Culture Documents
Vandenberghe
6. QR factorization
triangular matrices
QR factorization
Gram-Schmidt algorithm
Householder algorithm
6-1
Triangular matrix
a square matrix A is lower triangular if Aij = 0 for j > i
A11
A21
..
.
0
A22
..
.
..
.
A=
An1,1 An1,2
An1
An2
0
0
0
0
0
0
Ann
An1,n1
An,n1
QR factorization
6-2
Forward substitution
solve Ax = b when A is lower triangular with nonzero diagonal elements
Algorithm
x1 = b1/A11
x2 = (b2 A21x1)/A22
6-3
Back substitution
solve Ax = b when A is upper triangular with nonzero diagonal elements
Algorithm
xn = bn/Ann
xn1 = (bn1 An1,nxn)/An1,n1
Complexity: n2 flops
QR factorization
6-4
x=0
x 1 x2
xn
e1 e2
en
(xi is column i of X)
QR factorization
6-5
Outline
triangular matrices
QR factorization
Gram-Schmidt algorithm
Householder algorithm
QR factorization
if A Rmn has linearly independent columns then it can be
factored as
Rnn
qiT qj = 0 if i 6= j
6-6
Q-factor
Q is m n with orthonormal columns (QT Q = I)
if A is square (m = n), then Q is orthogonal (QT Q = QQT = I)
R-factor
R is n n, upper triangular, with nonzero diagonal elements
R is nonsingular (diagonal elements are nonzero)
QR factorization
6-7
Example
1 1 1
1/2 1/2 1/2
1
0 2 8
1 1 5 = 1/2 1/2
1/2
0 0 4
1
3 7
1/2 1/2
1/2
q1 q2 q3
= QR
QR factorization
6-8
Applications
in the following lectures, we will use the QR factorization to solve
linear equations
least squares problems
constrained least squares problems
here, we show that it gives useful simple formulas for
the pseudo-inverse of a matrix with linearly independent columns
the inverse of a nonsingular matrix
projection on the range of a matrix with linearly independent columns
QR factorization
6-9
(QT Q = I)
= R1RT RT QT
(R is nonsingular)
= R1QT
for square nonsingular A this is the inverse:
A1 = (QR)1 = R1QT
QR factorization
6-10
Range
recall definition of range of a matrix A Rmn (page 5-14):
range(A) = {Ax | x Rn}
suppose A has linearly independent columns with QR factors Q, R
Q has the same range as A:
y range(A) y = Ax for some x
y = Qz for some z
(take z = Rx)
y range(Q)
6-11
Projection on range
b
QQT b
range(A) = range(Q)
QR factorization
6-12
QR factorization
6-13
6-14
Outline
triangular matrices
QR factorization
Gram-Schmidt algorithm
Householder algorithm
Gram-Schmidt algorithm
Gram-Schmidt QR algorithm computes Q and R column by column
after k steps we have a partial QR factorization
a1 a2
ak
q1 q2
qk
R11 R12
0
R22
..
..
..
.
.
.
0
0
R1k
R2k
..
.
Rkk
QR factorization
6-15
Computing column k
suppose we have completed the factorization for the first k 1 columns
column k of the equation A = QR reads
ak = R1k q1 + R2k q2 + + Rk1,k qk1 + Rkk qk
regardless of the choice of R1k , . . . , Rk1,k , the vector
qk = ak R1k q1 R2k q2 Rk1,k qk1
will be nonzero: a1, a2, . . . , ak are linearly independent and therefore
ak 6 span{a1, . . . , ak1} = span{q1, . . . , qk1}
qk is qk normalized: choose Rkk = k
qk k and qk = (1/Rkk )
qk
R2k = q2T ak ,
...,
T
Rk1,k = qk1
ak
6-16
Gram-Schmidt algorithm
Given: m n matrix A with linearly independent columns a1, . . . , an
Algorithm
for k = 1 to n
R1k
= q1T ak
R2k
= q2T ak
..
.
Rk1,k
qk
Rkk
qk
QR factorization
T
= qk1
ak
= k
qk k
1
qk
=
Rkk
6-17
Example
example on page 6-8:
a1 a2
1 1 1
1
3
3
a3
=
1 1 5
1
3 7
R11 R12 R13
R22 R23
q1 q2 q3 0
=
0
0
R33
1
1
,
q1 = a1 =
R11 = k
q1k = 2,
1
1
QR factorization
1/2
1/2
1
q1 =
q1 =
1/2
R11
1/2
6-18
Example
Second column of Q and R
compute R12 = q1T a2 = 4
compute
1
1/2
1
1/2 1
3
q2 = a2 R12q1 =
1 4 1/2 = 1
1
1/2
3
normalize to get
R22 = k
q2k = 2,
QR factorization
1/2
1/2
1
q2 =
q2 =
1/2
R22
1/2
6-19
Example
Third column of Q and R
compute R13 = q1T a3 = 2 and R23 = q2T a3 = 8
compute
2
1/2
1/2
1
1/2 2
1/2
3
q3 = a3 R13q1 R23q2 =
5 2 1/2 8 1/2 = 2
2
1/2
1/2
7
normalize to get
R33 = k
q3k = 4,
QR factorization
1/2
1/2
1
q3 =
q3 =
1/2
R33
1/2
6-20
Example
Final result
1 1 1
1
3
3
=
q1 q2
1 1 5
1
3 7
1/2
1/2
=
1/2
1/2
QR factorization
q3
1/2 1/2
2 4 2
1/2 1/2
0 2 8
1/2
1/2
0 0 4
1/2
1/2
6-21
Complexity
Complexity of cycle k (algorithm on page 6-17)
k 1 inner products with ak : (k 1)(2m 1) flops
computation of qk : 2(k 1)m flops
computing Rkk and qk : 3m flops
total for cycle k: (4m 1)(k 1) + 3m flops
Complexity for m n factorization:
n
X
k=1
n(n 1)
+ 3mn
2
2mn2 flops
QR factorization
6-22
Numerical experiment
we use the following MATLAB code
[m,
Q =
R =
for
n] = size(A);
zeros(m,n);
zeros(n,n);
k = 1:n
R(1:k-1,k) = Q(:,1:k-1) * A(:,k);
v = A(:,k) - Q(:,1:k-1) * R(1:k-1,k);
R(k,k) = norm(v);
Q(:,k) = v / R(k,k);
end;
we apply this to a square matrix A of size m = n = 50
A is constructed as A = U SV with U , V orthogonal, S diagonal with
Sii = 1010(i1)/(n1),
QR factorization
i = 1, . . . , n
6-23
Numerical experiment
plot shows deviation from orthogonality between qk and previous columns
ek = max |qiT qk |,
k = 2, . . . , n
1i<k
1
0.8
ek
0.6
0.4
0.2
0
0
10
20
30
40
50
6-24
Outline
triangular matrices
QR factorization
Gram-Schmidt algorithm
Householder algorithm
Householder algorithm
the most widely used algorithm for QR factorization (qr in MATLAB)
less sensitive to rounding error than Gram-Schmidt algorithm
computes a full QR factorization
A=
Q Q
R
0
,
Q Q
orthogonal
Q Q
= H1H2 Hn
6-25
Reflector
H = I 2vv T
with kvk = 1
QR factorization
6-26
w=
y1 + sign(y1)kyk
y2
..
.
yp
v=
1
w
kwk
we take sign(0) = 1
vector w satisfies kwk2 = 2 (wT y) = 2kyk (kyk + |y1|)
reflector H = I 2vv T maps y to multiple of e1 = (1, 0, . . . , 0):
2(wT y)
w = y w = sign(y1)kyke1
Hy = y
2
kwk
QR factorization
6-27
Geometry
w
sign(y1)kyke1
hyperplane {x | wT x = 0}
6-28
Householder triangularization
computes reflectors H1, . . . , Hn that reduce A to triangular form:
HnHn1 H1A =
R
0
Hk Hk1 H1A =
mk
k
nk
6-29
Householder algorithm
the following algorithm overwrites A with
R
0
Algorithm: for k = 1 to n,
1. define y = Ak:m,k and compute (m k + 1)-vector vk :
w = y + sign(y1)kyke1,
1
vk =
w
kwk
6-30
Comments
in step 2 we multiply Ak:m,k:n with the reflector I 2vk vkT :
(I 2vk vkT )Ak:m,k:n = Ak:m,k:n 2vk (vkT Ak:m,k:n)
this is equivalent to multiplying A with m m reflector
Hk =
I
0
0 I 2vk vkT
=I 2
0
vk
0
vk
T
R
0
6-31
Example
example on page 6-8:
1 1 1
1
3 3
R
A=
= H1H2H3
1 1 5
0
1
3 7
0
R
R
22
23
H3H2H1A =
0
0
R33
0
0
0
QR factorization
6-32
Example
First column of R
compute reflector that maps first column of A to multiple of e1:
1
1
y=
1 ,
1
3
1
w = ykyke1 =
1 ,
1
3
1
1
1
v1 =
w=
kwk
2 3 1
1
2 4
2
0 4/3 8/3
T
A := (I 2v1v1 )A =
0 2/3 16/3
0 4/3 20/3
QR factorization
6-33
Example
Second column of R
compute reflector that maps A2:4,2 to multiple of e1:
4/3
y = 2/3 ,
4/3
10/3
w = y+kyke1 = 2/3 ,
4/3
5
1
1
1
v2 =
w=
kwk
30 2
A :=
QR factorization
1
0
0 I 2v2v2T
2
4
2
0 2
8
A=
0
0 16/5
0
0 12/5
6-34
Example
Third column of R
compute reflector that maps A3:4,3 to multiple of e1:
y=
16/5
12/5
,
w = y+kyke1 =
36/5
12/5
,
v3 =
1
1
w=
kwk
10
3
1
A :=
QR factorization
I
0
0 I 2v3v3T
2
4
2
0 2 8
A=
0
0 4
0
0
0
6-35
Example
Final result
H3H2H1A =
=
I
0
0 I 2v3v3T
I
0
0 I 2v3v3T
=
I
0
0 I 2v3v3T
1
0
0 I 2v2v2T
1
0
0 I 2v2v2T
(I 2v1v1T )A
2 4
2
0 4/3 8/3
0 2/3 16/3
0 4/3 20/3
2
4
2
0 2
8
0
0 16/5
0
0 12/5
2
4
2
0 2 8
=
0
0 4
0
0
0
QR factorization
6-36
Complexity
Complexity in cycle k (algorithm on page 6-30): the dominant terms are
(2(m k + 1) 1)(n k + 1) flops for product vkT (Ak:m,k:n)
(m k + 1)(n k + 1) flops for outer product with vk
(m k + 1)(n k + 1) flops for subtraction from Ak:m,k:n
sum is roughly 4(m k + 1)(n k + 1) flops
Total for computing R and vectors v1, . . . , vn:
n
X
k=1
4(m k + 1)(n k + 1)
Z
0
2 3
= 2mn n
3
2
QR factorization
flops
6-37
Q-factor
the Householder algorithm returns the vectors v1, . . . , vn that define
Q Q
= H1H2 Hn
] explicitly
usually there is no need to compute the matrix [ Q Q
]
the vectors v1, . . . , vn are an economical representation of [ Q Q
] or its transpose can be computed as
products with [ Q Q
QR factorization
Q Q
Q Q
T
x = H1H2 Hnx
y = HnHn1 H1y
6-38
I
0
0 I 2vk vkT
x1:k1
xk:m
=
x1:k1
xk:m 2(vkT xk:m)vk
QR factorization
6-39