QR Factorization
QR Factorization
6. QR factorization
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• modified 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 ∼ 31 𝑛3 flops
QR factorization 6.5
Outline
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• modified Gram–Schmidt algorithm
• Householder algorithm
QR factorization
∥𝑞𝑖 ∥ = 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
• modified 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
Interpretation
QR factorization 6.18
Gram–Schmidt algorithm
Algorithm
for 𝑘 = 1 to 𝑛
𝑅1𝑘 = 𝑞𝑇1 𝑎 𝑘
𝑅2𝑘 = 𝑞𝑇2 𝑎 𝑘
..
𝑅 𝑘−1,𝑘 = 𝑞𝑇𝑘−1 𝑎 𝑘
𝑞˜ 𝑘 = 𝑎 𝑘 − (𝑅1𝑘 𝑞 1 + 𝑅2𝑘 𝑞 2 + · · · + 𝑅 𝑘−1,𝑘 𝑞 𝑘−1)
𝑅𝑘 𝑘 = ∥ 𝑞˜ 𝑘 ∥
1
𝑞𝑘 = 𝑞˜ 𝑘
𝑅𝑘 𝑘
QR factorization 6.19
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 = ∥ 𝑞˜ 1 ∥ = 2, 𝑞˜ 1 =
, 𝑞1 =
−1 𝑅11 −1/2
1 1/2
QR factorization 6.20
Example
1/2
1 1/2
𝑅22 = ∥ 𝑞˜ 2 ∥ = 2, 𝑞˜ 2 =
𝑞2 =
1/2
𝑅22
1/2
QR factorization 6.21
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 = ∥ 𝑞˜ 3 ∥ = 4, 𝑞˜ 3 =
𝑞3 =
1/2
𝑅33
1/2
QR factorization 6.22
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
2 4 2
1/2 1/2 −1/2
0 2 8
= −1/2 1/2 1/2
0 0 4
1/2 1/2 1/2
QR factorization 6.23
Complexity
𝑛
𝑛(𝑛 − 1)
((4𝑚 − 1) (𝑘 − 1) + 3𝑚) (4𝑚 − 1) + 3𝑚𝑛
∑︁
=
𝑘=1 2
∼ 2𝑚𝑛2 flops
QR factorization 6.24
Numerical experiment
𝑆𝑖𝑖 = 10−10(𝑖−1)/(𝑛−1) , 𝑖 = 1, . . . , 𝑛
QR factorization 6.25
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
• modified Gram–Schmidt algorithm
• Householder algorithm
Modified Gram–Schmidt algorithm
QR factorization 6.27
Modified Gram–Schmidt algorithm
𝐴 = 𝑎 1 · · · 𝑎 𝑘−1 𝑎 𝑘 · · · 𝑎𝑛
QR factorization 6.28
Modified Gram–Schmidt update
˜ ˜
partition 𝑄 𝑘 as 𝑄 𝑘 = 𝑞˜ 𝑘 𝐵 with 𝑞˜ 𝑘 the first column and 𝐵 of size 𝑚 × (𝑛 − 𝑘) :
𝑞˜ 𝑘 = 𝑞 𝑘 𝑅 𝑘 𝑘 , 𝐵 = 𝑞 𝑘 𝑅 𝑘,(𝑘+1):𝑛 + 𝑄˜ 𝑘+1
• from the first equation, and the required properties ∥𝑞 𝑘 ∥ = 1 and 𝑅 𝑘 𝑘 > 0:
1
𝑅 𝑘 𝑘 = ∥ 𝑞˜ 𝑘 ∥, 𝑞𝑘 = 𝑞˜ 𝑘
𝑅𝑘 𝑘
• from the second equation, and the requirement that 𝑞𝑇𝑘 𝑄˜ 𝑘+1 = 0:
QR factorization 6.29
Summary: modified Gram–Schmidt algorithm
define 𝑄˜ 1 = 𝐴; for 𝑘 = 1 to 𝑛,
• compute 𝑅 𝑘 𝑘 = ∥ 𝑞˜ 𝑘 ∥ and 𝑞 𝑘 = (1/𝑅 𝑘 𝑘 ) 𝑞˜ 𝑘 where 𝑞˜ 𝑘 is the first column of 𝑄˜ 𝑘
• compute
𝑅 𝑘,𝑘+1 · · · 𝑅 𝑘𝑛 = 𝑞𝑇𝑘 𝐵, ˜
𝑄 𝑘+1 = 𝐵 − 𝑞 𝑘 𝑅 𝑘,𝑘+1 · · · 𝑅 𝑘𝑛
−1 −1 1
1 3 3
𝑎3 =
𝑎1 𝑎2
−1 −1 5
1 3 7
QR factorization 6.31
Example
𝑛
𝑛(𝑛 − 1)
((4𝑚 − 1) (𝑛 − 𝑘) + 3𝑚) (4𝑚 − 1) + 3𝑚𝑛
∑︁
=
𝑘=1 2
∼ 2𝑚𝑛2 flops
QR factorization 6.33
Outline
• triangular matrices
• QR factorization
• Gram–Schmidt algorithm
• modified 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.34
Reflector
𝐻𝑥 = 𝑥 − 2(𝑣𝑇 𝑥)𝑣
QR factorization 6.35
Reflection to multiple of unit vector
𝑦 1 + sign(𝑦 1) ∥𝑦∥
1
𝑦2
𝑤 = .. , 𝑣= 𝑤
∥𝑤∥
𝑦𝑝
• we define sign(0) = 1
• vector 𝑤 satisfies
∥𝑤∥ 2 = 2 (𝑤𝑇 𝑦) = 2∥𝑦∥ (∥𝑦∥ + |𝑦 1 |)
2(𝑤𝑇 𝑦)
𝐻𝑦 = 𝑦 − 𝑤 = 𝑦 − 𝑤 = −sign(𝑦 1)∥𝑦∥𝑒 1
∥𝑤∥ 2
QR factorization 6.36
Geometry
𝑤
𝑦
hyperplane {𝑥 | 𝑤𝑇 𝑥 = 0}
𝑤 = 𝑦 + sign(𝑦 1) ∥𝑦∥𝑒 1
QR factorization 6.37
Householder triangularization
𝑚−𝑘
𝑘 𝑛−𝑘
QR factorization 6.38
Householder algorithm
𝑅
the following algorithm overwrites 𝐴 with
0
Algorithm: for 𝑘 = 1 to 𝑛,
1
𝑤 = 𝑦 + sign(𝑦 1) ∥𝑦∥𝑒 1, 𝑣𝑘 = 𝑤
∥𝑤∥
QR factorization 6.39
Comments
QR factorization 6.40
Example
−1 −1 1
1 3 3
𝑅
𝐴 = = 𝐻 𝐻 𝐻
1 2 3
−1 −1 5 0
1 3 7
QR factorization 6.41
Example
First column of 𝑅
−1 −3 −3
1 1 1 1 1
𝑦 = , 𝑤 = 𝑦 − ∥𝑦∥𝑒 1 = , 𝑣1 = 𝑤= √
−1 −1 ∥𝑤∥ 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.42
Example
Second column of 𝑅
4/3 10/3 5
1 1
𝑦 = 2/3 , 𝑤 = 𝑦 + ∥𝑦∥𝑒 1 = 2/3 , 𝑤=√ 1
𝑣2 =
4/3 4/3 ∥𝑤∥ 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.43
Example
Third column of 𝑅
16/5 36/5 1 1 3
𝑦= , 𝑤 = 𝑦 + ∥𝑦∥𝑒 1 = , 𝑣3 = 𝑤=√
12/5 12/5 ∥𝑤∥ 10 1
2 4 2
0 0 −2 −8
𝐼
𝐴 :=
𝐴 =
0 𝐼 − 2𝑣 3 𝑣𝑇3 0 0 −4
0 0 0
QR factorization 6.44
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.45
Complexity
Complexity in cycle 𝑘 (of algorithm on page 6.39): the dominant terms are
𝑛 ∫ 𝑛
4(𝑚 − 𝑘 + 1) (𝑛 − 𝑘 + 1) 4(𝑚 − 𝑡)(𝑛 − 𝑡)𝑑𝑡
∑︁
∼
𝑘=1 0
2 3 2
= 2𝑚𝑛 − 𝑛 flops
3
QR factorization 6.46
Q-factor
𝑄˜ = 𝐻1 𝐻2 · · · 𝐻 𝑛
𝑄
𝑄˜ 𝑥 = 𝐻1 𝐻2 · · · 𝐻 𝑛 𝑥
𝑄
𝑄˜
𝑇
𝑦 = 𝐻𝑛 𝐻𝑛−1 · · · 𝐻1 𝑦
𝑄
QR factorization 6.47
Multiplication with Q-factor
0
𝐼 𝑥1:𝑘−1 𝑥1:𝑘−1
𝐻𝑘 𝑥 = =
0 𝐼 − 2𝑣 𝑘 𝑣𝑇𝑘 𝑥 𝑘:𝑚 𝑥 𝑘:𝑚 − 2(𝑣𝑇𝑘 𝑥 𝑘:𝑚 )𝑣 𝑘
𝑛
4(𝑚 − 𝑘 + 1) ∼ 4𝑚𝑛 − 2𝑛2 flops
∑︁
𝑘=1
QR factorization 6.48