QR Factorization: Triangular Matrices QR Factorization Gram-Schmidt Algorithm Householder Algorithm
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
𝐴11 0 ··· 0 0
0 0
𝐴21 𝐴22 ···
𝐴 = .. .. ... 0 0
0
𝐴𝑛−1,1
𝐴𝑛−1,2 ··· 𝐴𝑛−1,𝑛−1
𝐴𝑛1
𝐴𝑛2 ··· 𝐴𝑛,𝑛−1 𝐴𝑛𝑛
QR factorization 6.2
Forward substitution
Algorithm
𝑥1 = 𝑏 1/𝐴11
𝑥2 = (𝑏 2 − 𝐴21𝑥1)/𝐴22
𝑥3 = (𝑏 3 − 𝐴31𝑥1 − 𝐴32𝑥2)/𝐴33
..
QR factorization 6.3
Back substitution
Algorithm
𝑥 𝑛 = 𝑏 𝑛 /𝐴𝑛𝑛
𝑥 𝑛−1 = (𝑏 𝑛−1 − 𝐴𝑛−1,𝑛 𝑥 𝑛 )/𝐴𝑛−1,𝑛−1
𝑥 𝑛−2 = (𝑏 𝑛−2 − 𝐴𝑛−2,𝑛−1𝑥 𝑛−1 − 𝐴𝑛−2,𝑛 𝑥 𝑛 )/𝐴𝑛−2,𝑛−2
..
Complexity: 𝑛2 flops
QR factorization 6.4
Inverse of triangular matrix
𝐴𝑥 = 0 =⇒ 𝑥=0
= (𝑥𝑖 is column 𝑖 of 𝑋 )
𝐴 𝑥1 𝑥2 · · · 𝑥𝑛 𝑒1 𝑒2 · · · 𝑒𝑛
𝑛2 + (𝑛 − 1) 2 + · · · + 1 ≈ 13 𝑛3 flops
QR factorization 6.5
Outline
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• Householder algorithm
QR factorization
k𝑞𝑖 k = 1, 𝑞𝑇𝑖 𝑞 𝑗 = 0 if 𝑖 ≠ 𝑗
QR factorization 6.6
QR factorization in matrix notation
𝐴 = 𝑄𝑅
Q-factor
R-factor
QR factorization 6.7
Example
= 𝑄𝑅
QR factorization 6.8
Full QR factorization
QR factorization 6.9
Applications
• linear equations
• least squares problems
• constrained least squares problems
QR factorization 6.10
QR factorization and (pseudo-)inverse
𝐴† = ( 𝐴𝑇 𝐴) −1 𝐴𝑇
QR factorization 6.11
Range
range( 𝐴) = {𝐴𝑥 | 𝑥 ∈ R𝑛 }
QR factorization 6.12
Projection on range
note the order of the product in 𝐴𝐴† and the difference with 𝐴† 𝐴 = 𝐼
• recall (from page 5.17) that 𝑄𝑄𝑇 𝑥 is the projection of 𝑥 on the range of 𝑄
𝑥
𝐴𝐴†𝑥 = 𝑄𝑄𝑇 𝑥
range( 𝐴) = range(𝑄)
QR factorization 6.13
QR factorization of complex matrices
𝐴 = 𝑄𝑅
QR factorization 6.14
Algorithms for QR factorization
in the rest of the course we will take 2𝑚𝑛2 for the complexity of QR factorization
QR factorization 6.15
Outline
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• Householder algorithm
Gram–Schmidt algorithm
QR factorization 6.16
Computing the 𝑘 th columns of 𝑄 and 𝑅
suppose we have the partial factorization for the first 𝑘 − 1 columns of 𝑄 and 𝑅
QR factorization 6.17
Gram–Schmidt algorithm
Algorithm
for 𝑘 = 1 to 𝑛
𝑅1𝑘 = 𝑞𝑇1 𝑎 𝑘
𝑅2𝑘 = 𝑞𝑇2 𝑎 𝑘
..
𝑅 𝑘−1,𝑘 = 𝑞𝑇𝑘−1 𝑎 𝑘
𝑞˜ 𝑘 = 𝑎 𝑘 − (𝑅1𝑘 𝑞 1 + 𝑅2𝑘 𝑞 2 + · · · + 𝑅 𝑘−1,𝑘 𝑞 𝑘−1)
𝑅𝑘 𝑘 = k 𝑞˜ 𝑘 k
1
𝑞𝑘 = 𝑞˜ 𝑘
𝑅𝑘 𝑘
QR factorization 6.18
Example
−1 −1 1
1 3 3
=
𝑎1 𝑎2 𝑎3 −1 −1 5
1 3 7
𝑅11 𝑅12 𝑅13
𝑞 3 0
=
𝑞1 𝑞2 𝑅22 𝑅23
0 0
𝑅33
−1 −1/2
1 1 1/2
𝑞˜ 1 = 𝑎 1 = 𝑅11 = k 𝑞˜ 1 k = 2, 𝑞˜ 1 =
, 𝑞1 =
−1 𝑅11 −1/2
1 1/2
QR factorization 6.19
Example
1/2
1 1/2
𝑅22 = k 𝑞˜ 2 k = 2, 𝑞˜ 2 =
𝑞2 =
1/2
𝑅22
1/2
QR factorization 6.20
Example
1 −1/2 1/2 −2
3 1/2 1/2 −2
𝑞˜ 3 = 𝑎 3 − 𝑅13 𝑞 1 − 𝑅23 𝑞 2 = −2
−1/2 − 8 1/2 = 2
5
7 1/2 1/2 2
• normalize to get
−1/2
1
−1/2
𝑅33 = k 𝑞˜ 3 k = 4, 𝑞˜ 3 =
𝑞3 =
1/2
𝑅33
1/2
QR factorization 6.21
Example
Final result
−1 −1 1
1 3 3 𝑅11 𝑅12 𝑅13
𝑞 3 0
=
−1 −1 5
𝑞1 𝑞2 𝑅22 𝑅23
0 0
1 𝑅33
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.22
Complexity
𝑛
𝑛(𝑛 − 1)
((4𝑚 − 1) (𝑘 − 1) + 3𝑚) (4𝑚 − 1) + 3𝑚𝑛
X
=
𝑘=1 2
≈ 2𝑚𝑛2 flops
QR factorization 6.23
Numerical experiment
𝑆𝑖𝑖 = 10−10(𝑖−1)/(𝑛−1) , 𝑖 = 1, . . . , 𝑛
QR factorization 6.24
Numerical experiment
𝑒 𝑘 = max |𝑞𝑇𝑖 𝑞 𝑘 |, 𝑘 = 2, . . . , 𝑛
1≤𝑖<𝑘
0.8
0.6
𝑒𝑘
0.4
0.2
0
0 10 20 30 40 50
𝑘
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• Householder algorithm
Householder algorithm
• the most widely used algorithm for QR factorization (qr in MATLAB and Julia)
• less sensitive to rounding error than Gram–Schmidt algorithm
• computes a “full” QR factorization (QR decomposition)
𝑅
𝐴= 𝑄˜ 𝑄˜ orthogonal
𝑄 , 𝑄
0
𝑄˜ = 𝐻1 𝐻2 · · · 𝐻 𝑛
𝑄
QR factorization 6.26
Reflector
𝐻𝑥 = 𝑥 − 2(𝑣𝑇 𝑥)𝑣
QR factorization 6.27
Reflection to multiple of unit vector
𝑦 1 + sign(𝑦 1) k𝑦k
1
𝑦2
𝑤 = .. , 𝑣= 𝑤
k𝑤k
𝑦𝑝
• we define sign(0) = 1
• vector 𝑤 satisfies
k𝑤k 2 = 2 (𝑤𝑇 𝑦) = 2k𝑦k (k𝑦k + |𝑦 1 |)
2(𝑤𝑇 𝑦)
𝐻𝑦 = 𝑦 − 2
𝑤 = 𝑦 − 𝑤 = −sign(𝑦 1)k𝑦k𝑒 1
k𝑤k
QR factorization 6.28
Geometry
𝑤
𝑦
hyperplane {𝑥 | 𝑤𝑇 𝑥 = 0}
𝑤 = 𝑦 + sign(𝑦 1) k𝑦k𝑒 1
QR factorization 6.29
Householder triangularization
𝑚−𝑘
𝑘 𝑛−𝑘
QR factorization 6.30
Householder algorithm
𝑅
the following algorithm overwrites 𝐴 with
0
Algorithm: for 𝑘 = 1 to 𝑛,
1
𝑤 = 𝑦 + sign(𝑦 1) k𝑦k𝑒 1, 𝑣𝑘 = 𝑤
k𝑤k
QR factorization 6.31
Comments
QR factorization 6.32
Example
−1 −1 1
1 3 3
𝑅
𝐴 = = 𝐻 𝐻 𝐻
1 2 3
−1 −1 5 0
1 3 7
QR factorization 6.33
Example
First column of 𝑅
−1 −3 −3
1 1 1 1 1
𝑦 = , 𝑤 = 𝑦 − k𝑦k𝑒 1 = , 𝑣1 = 𝑤= √
−1 −1 k𝑤k 2 3 −1
1 1 1
2 4 2
0 4/3 8/3
𝐴 := (𝐼 − 2𝑣 1 𝑣 1 ) 𝐴 =
𝑇
0 2/3 16/3
0 4/3 20/3
QR factorization 6.34
Example
Second column of 𝑅
4/3 10/3 5
1 1
𝑦 = 2/3 , 𝑤 = 𝑦 + k𝑦k𝑒 1 = 2/3 , 𝑤=√ 1
𝑣2 =
4/3 4/3 k𝑤k 30 2
2 4 2
1 0 0 −2
−8
𝐴 :=
𝐴 =
0 𝐼 − 2𝑣 2 𝑣𝑇2 0 0 16/5
0 0 12/5
QR factorization 6.35
Example
Third column of 𝑅
16/5 36/5 1 1 3
𝑦= , 𝑤 = 𝑦 + k𝑦k𝑒 1 = , 𝑣3 = 𝑤=√
12/5 12/5 k𝑤k 10 1
2 4 2
0 0 −2 −8
𝐼
𝐴 :=
𝐴 =
0 𝐼 − 2𝑣 3 𝑣𝑇3 0 0 −4
0 0 0
QR factorization 6.36
Example
Final result
0 1 0
𝐼
𝐻3 𝐻2 𝐻1 𝐴 = (𝐼 − 2𝑣 1 𝑣𝑇1 ) 𝐴
0 𝐼 − 2𝑣 3 𝑣𝑇3 0 𝐼 − 2𝑣 2 𝑣𝑇2
2 4 2
0 1 0 0 4/3 8/3
𝐼
=
0 𝐼 − 2𝑣 3 𝑣𝑇3 0 𝐼 − 2𝑣 2 𝑣𝑇2 0 2/3 16/3
0 4/3 20/3
2 4 2
0 0 −2
𝐼 −8
=
0 𝐼 − 2𝑣 3 𝑣𝑇3 0 0 16/5
0 0 12/5
2 4 2
0 −2 −8
=
0 0 −4
0 0 0
QR factorization 6.37
Complexity
Complexity in cycle 𝑘 (of algorithm on page 6.31): the dominant terms are
𝑛 ∫ 𝑛
4(𝑚 − 𝑘 + 1) (𝑛 − 𝑘 + 1) 4(𝑚 − 𝑡)(𝑛 − 𝑡)𝑑𝑡
X
≈
𝑘=1 0
2
= 2𝑚𝑛2 − 𝑛3 flops
3
QR factorization 6.38
Q-factor
𝑄˜ = 𝐻1 𝐻2 · · · 𝐻 𝑛
𝑄
𝑄˜ 𝑥 = 𝐻1 𝐻2 · · · 𝐻 𝑛 𝑥
𝑄
𝑄˜
𝑇
𝑦 = 𝐻𝑛 𝐻𝑛−1 · · · 𝐻1 𝑦
𝑄
QR factorization 6.39
Multiplication with Q-factor
0
𝐼 𝑥1:𝑘−1 𝑥1:𝑘−1
𝐻𝑘 𝑥 = =
0 𝐼 − 2𝑣 𝑘 𝑣𝑇𝑘 𝑥 𝑘:𝑚 𝑥 𝑘:𝑚 − 2(𝑣𝑇𝑘 𝑥 𝑘:𝑚 )𝑣 𝑘
𝑛
4(𝑚 − 𝑘 + 1) ≈ 4𝑚𝑛 − 2𝑛2 flops
X
𝑘=1
QR factorization 6.40