QR Factorization: Triangular Matrices QR Factorization Gram-Schmidt Algorithm Householder Algorithm
QR Factorization: Triangular Matrices QR Factorization Gram-Schmidt Algorithm Householder Algorithm
6. QR factorization
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• Householder algorithm
6.1
Triangular matrix
A11 0 ··· 0 0
A21 A22 0 0
···
A =
.. .. ... 0 0
An−1,1 An−1,2 An−1,n−1 0
···
An1 An2 An,n−1 Ann
···
QR factorization 6.2
Forward substitution
Algorithm
x1 = b1/A11
x2 = (b2 − A21 x1)/A22
x3 = (b3 − A31 x1 − A32 x2)/A33
..
QR factorization 6.3
Back substitution
Algorithm
xn = bn/Ann
xn−1 = (bn−1 − An−1,n xn)/An−1,n−1
xn−2 = (bn−2 − An−2,n−1 xn−1 − An−2,n xn)/An−2,n−2
..
Complexity: n2 flops
QR factorization 6.4
Inverse of a triangular matrix
Ax = 0 =⇒ x=0
A x1 x2 · · · xn e1 e2 · · · en ( xi is column i of X )
=
2 2 1 3
n + (n − 1) + · · · + 1 ≈ n flops
3
QR factorization 6.5
Outline
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• Householder algorithm
QR factorization
kqi k = 1, qiT q j = 0 if i , j
• if Rii < 0, we can switch the signs of Rii , . . . , Rin, and the vector qi
QR factorization 6.6
QR factorization in matrix notation
A = QR
Q-factor
R-factor
QR factorization 6.7
Example
= QR
QR factorization 6.8
Applications
• linear equations
• least squares problems
• constrained least squares problems
QR factorization 6.9
QR factorization and (pseudo-)inverse
A† = (AT A)−1 AT
A† = ((QR)T (QR))−1(QR)T
= (RT QT QR)−1 RT QT
= (RT R)−1 RT QT (QT Q = I )
= R−1 R−T RT QT ( R is nonsingular)
= R−1QT
QR factorization 6.10
Range
range(A) = { Ax | x ∈ Rn }
QR factorization 6.11
Projection on range
note the order of the product in AA† and the difference with A† A = I
• recall (from page 5.17) that QQT x is the projection of x on the range of Q
x
AA† x = QQT x
range(A) = range(Q)
QR factorization 6.12
QR factorization of complex matrices
A = QR
QR factorization 6.13
Algorithms for QR factorization
in the rest of the course we will take 2mn2 for the complexity of QR factorization
QR factorization 6.14
Outline
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• Householder algorithm
Gram–Schmidt algorithm
• columns q1, . . . , qk have the same span as a1, . . . , a k (see page 6.11)
QR factorization 6.15
Computing column k
QR factorization 6.16
Gram–Schmidt algorithm
Algorithm
for k = 1 to n
R1k = q1T a k
R2k = q2T a k
..
Rk−1,k = qTk−1 a k
q̃k = a k − (R1k q1 + R2k q2 + · · · + Rk−1,k qk−1)
Rk k = k q̃k k
1
qk = q̃k
Rk k
QR factorization 6.17
Example
−1 −1 1
1 3 3
a1 a2 a3
= −1 −1 5
1 3 7
R11 R12 R13
q1 q2 q3 0 R22 R23
=
0 0 R33
−1 −1/2
1 1 1/2
q̃1 = a1 = R11 = k q̃1 k = 2, q1 = q̃1 =
,
−1 R11 −1/2
1 1/2
QR factorization 6.18
Example
• compute
−1 −1/2 1
3 1/2 1
q̃2 = a2 − R12 q1 = −4 =
−1 −1/2 1
3 1/2 1
• normalize to get
1/2
1 1/2
R22 = k q̃2 k = 2, q2 = q̃2 =
1/2
R22
1/2
QR factorization 6.19
Example
• compute
1 −1/2 1/2 −2
3 1/2 1/2 −2
−2
q̃3 = a3 − R13 q1 − R23 q2 = −1/2 − 8 1/2 = 2
5
7 1/2 1/2 2
• normalize to get
−1/2
1
−1/2
R33 = k q̃3 k = 4, q3 = q̃3 =
1/2
R33
1/2
QR factorization 6.20
Example
Final result
−1 −1 1
1 3 3 R11 R12 R13
q1 q2 q3 0 R22 R23
−1 −1 5
=
0 0 R33
1 3 7
−1/2 1/2 −1/2
1/2 1/2 −1/2 2 4 2
= 0 2 8
−1/2 1/2 1/2
0 0 4
1/2 1/2 1/2
QR factorization 6.21
Complexity
n
n(n − 1)
((4m − 1)(k − 1) + 3m) = (4m − 1) + 3mn
X
k=1 2
≈ 2mn2 flops
QR factorization 6.22
Numerical experiment
Sii = 10−10(i−1)/(n−1), i = 1, . . . , n
QR factorization 6.23
Numerical experiment
e k = max |qiT qk |, k = 2, . . . , n
1≤i<k
0.8
0.6
ek
0.4
0.2
0
0 10 20 30 40 50
k
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• Householder algorithm
Householder algorithm
• the most widely used algorithm for QR factorization (qr in MATLAB and Julia)
R
A= Q Q̃ Q Q̃ orthogonal
,
0
Q Q̃ = H1 H2 · · · Hn
QR factorization 6.25
Reflector
• H is symmetric
• H is orthogonal
H x = x − 2(vT x)v
QR factorization 6.26
Reflection to multiple of unit vector
y1 + sign(y1)k yk
1
y2
,
w = .. v= w
kwk
yp
• we define sign(0) = 1
• vector w satisfies
2(wT y)
Hy = y − 2
w = y − w = −sign(y1)k yke1
kwk
QR factorization 6.27
Geometry
w
y
hyperplane {x | wT x = 0}
w = y + sign(y1)k yke1
QR factorization 6.28
Householder triangularization
R
Hn Hn−1 · · · H1 A =
0
m−k
k n−k
QR factorization 6.29
Householder algorithm
R
the following algorithm overwrites A with
0
Algorithm: for k = 1 to n,
1
w = y + sign(y1)k yke1, vk = w
kwk
QR factorization 6.30
Comments
QR factorization 6.31
Example
−1 −1 1
1 3 3 R
A = H H H
= 1 2 3
−1 −1 5 0
1 3 7
QR factorization 6.32
Example
First column of R
−1 −3 −3
1 1 1 1 1
y = , w = y − k yke1 = , v1 = w= √
−1 −1 kwk 2 3 −1
1 1 1
2 4 2
0 4/3 8/3
T
A := (I − 2v1 v1 )A =
0 2/3 16/3
0 4/3 20/3
QR factorization 6.33
Example
Second column of R
4/3 10/3 5
1 1
y = 2/3 , w = y + k yke1 = 2/3 , w=√ 1
v2 =
4/3 4/3 kwk 30 2
2 4 2
1 0 0 −2
−8
A := A =
0 I − 2v2 v2T 0 0 16/5
0 0 12/5
QR factorization 6.34
Example
Third column of R
16/5 36/5 1 1 3
y= , w = y + k yke1 = , v3 = w=√
12/5 12/5 kwk 10 1
2 4 2
I 0 0 −2 −8
A := A =
0 I − 2v3 v3T 0 0 −4
0 0 0
QR factorization 6.35
Example
Final result
I 0 1 0
H3 H2 H1 A = (I − 2v1 v1T )A
0 I − 2v3 v3T 0 I − 2v2 v2T
2 4 2
I 0 1 0 0 4/3 8/3
=
0 I − 2v3 v3T 0 I − 2v2 v2T 0 2/3 16/3
0 4/3 20/3
2 4 2
I 0 0 −2
−8
=
0 I − 2v3 v3T 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 (of algorithm on page 6.30): the dominant terms are
n ∫ n
4(m − k + 1)(n − k + 1) ≈ 4(m − t)(n − t)dt
X
k=1 0
2
= 2mn2 − n3 flops
3
QR factorization 6.37
Q-factor
Q Q̃ = H1 H2 · · · Hn
Q Q̃ x = H1 H2 · · · Hn x
T
Q Q̃ y = Hn Hn−1 · · · H1 y
QR factorization 6.38
Multiplication with Q-factor
I 0 x1:k−1 x1:k−1
Hk x = =
0 I − 2v k vTk x k:m x k:m − 2(vTk x k:m )v k
n
4(m − k + 1) ≈ 4mn − 2n2 flops
X
k=1
QR factorization 6.39