l.vandenberghe ece133a(spring2021) 7.linearequations

25
L. Vandenberghe ECE133A (Fall 2021) 7. Linear equations β€’ QR factorization method β€’ factor and solve β€’ LU factorization 7.1

Upload: others

Post on 18-Jan-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

L. Vandenberghe ECE133A (Fall 2021)

7. Linear equations

β€’ QR factorization method

β€’ factor and solve

β€’ LU factorization

7.1

QR factorization and matrix inverse

QR factorization of nonsingular matrix

every nonsingular 𝐴 ∈ R𝑛×𝑛 has a QR factorization

𝐴 = 𝑄𝑅

β€’ 𝑄 ∈ R𝑛×𝑛 is orthogonal (𝑄𝑇𝑄 = 𝑄𝑄𝑇 = 𝐼)

β€’ 𝑅 ∈ R𝑛×𝑛 is upper triangular with positive diagonal elements

Inverse from QR factorization: the inverse π΄βˆ’1 can be written as

π΄βˆ’1 = (𝑄𝑅)βˆ’1 = π‘…βˆ’1π‘„βˆ’1 = π‘…βˆ’1𝑄𝑇

Linear equations 7.2

Solving linear equations by QR factorization

Algorithm: to solve 𝐴π‘₯ = 𝑏 with nonsingular 𝐴 ∈ R𝑛×𝑛,

1. factor 𝐴 as 𝐴 = 𝑄𝑅

2. compute 𝑦 = 𝑄𝑇𝑏

3. solve 𝑅π‘₯ = 𝑦 by back substitution

Complexity: 2𝑛3 + 3𝑛2 β‰ˆ 2𝑛3 flops

β€’ QR factorization: 2𝑛3

β€’ matrix-vector multiplication: 2𝑛2

β€’ back substitution: 𝑛2

Linear equations 7.3

Multiple right-hand sides

consider π‘˜ sets of linear equations with the same coefficient matrix 𝐴:

𝐴π‘₯1 = 𝑏1, 𝐴π‘₯2 = 𝑏2, . . . , 𝐴π‘₯π‘˜ = π‘π‘˜

β€’ equivalently, solve 𝐴𝑋 = 𝐡 where 𝐡 is 𝑛 Γ— π‘˜ matrix with columns 𝑏1, . . . , π‘π‘˜

β€’ can be solved in 2𝑛3 + 3π‘˜π‘›2 flops if we reuse the factorization 𝐴 = 𝑄𝑅

β€’ for π‘˜ οΏ½ 𝑛, cost is roughly equal to cost of solving one equation (2𝑛3)

Application: to compute π΄βˆ’1, solve the matrix equation 𝐴𝑋 = 𝐼

β€’ equivalent to 𝑛 equations

𝑅π‘₯1 = 𝑄𝑇𝑒1, 𝑅π‘₯2 = 𝑄𝑇𝑒2, . . . , 𝑅π‘₯𝑛 = 𝑄𝑇𝑒𝑛

(π‘₯𝑖 is column 𝑖 of 𝑋 and 𝑄𝑇𝑒𝑖 is transpose of 𝑖th row of 𝑄)

β€’ complexity is 2𝑛3 + 𝑛3 = 3𝑛3 (here the 2nd term 𝑛3 is not negligible)

Linear equations 7.4

Outline

β€’ QR factorization method

β€’ factor and solve

β€’ LU factorization

Factor–solve approach

to solve 𝐴π‘₯ = 𝑏, first write 𝐴 as a product of β€œsimple” matrices

𝐴 = 𝐴1𝐴2 Β· Β· Β· π΄π‘˜

then solve (𝐴1𝐴2 Β· Β· Β· π΄π‘˜)π‘₯ = 𝑏 by solving π‘˜ equations

𝐴1𝑧1 = 𝑏, 𝐴2𝑧2 = 𝑧1, . . . , π΄π‘˜βˆ’1π‘§π‘˜βˆ’1 = π‘§π‘˜βˆ’2, π΄π‘˜π‘₯ = π‘§π‘˜βˆ’1

Examples

β€’ QR factorization: π‘˜ = 2, 𝐴 = 𝑄𝑅

β€’ LU factorization (this lecture)

β€’ Cholesky factorization (later)

Linear equations 7.5

Complexity of factor–solve method

#flops = 𝑓 + 𝑠

β€’ 𝑓 is complexity of factoring 𝐴 as 𝐴 = 𝐴1𝐴2 Β· Β· Β· π΄π‘˜ (factorization step)

β€’ 𝑠 is complexity of solving the π‘˜ equations for 𝑧1, 𝑧2, . . . π‘§π‘˜βˆ’1, π‘₯ (solve step)

β€’ usually 𝑓 οΏ½ 𝑠

Example: solving linear equations using the QR factorization

𝑓 = 2𝑛3, 𝑠 = 3𝑛2

Linear equations 7.6

Outline

β€’ QR factorization method

β€’ factor and solve

β€’ LU factorization

LU factorization

LU factorization without pivoting

𝐴 = πΏπ‘ˆ

β€’ 𝐿 unit lower triangular, π‘ˆ upper triangular

β€’ does not always exist (even if 𝐴 is nonsingular)

LU factorization (with row pivoting)

𝐴 = π‘ƒπΏπ‘ˆ

β€’ 𝑃 permutation matrix, 𝐿 unit lower triangular, π‘ˆ upper triangular

β€’ exists if and only if 𝐴 is nonsingular (see later)

Complexity: (2/3)𝑛3 if 𝐴 is 𝑛 Γ— 𝑛

Linear equations 7.7

Solving linear equations by LU factorization

Algorithm: to solve 𝐴π‘₯ = 𝑏 with nonsingular 𝐴 of size 𝑛 Γ— 𝑛

1. factor 𝐴 as 𝐴 = π‘ƒπΏπ‘ˆ ((2/3)𝑛3 flops)

2. solve (π‘ƒπΏπ‘ˆ)π‘₯ = 𝑏 in three steps

(a) permutation: 𝑧1 = 𝑃𝑇𝑏 (0 flops)(b) forward substitution: solve 𝐿𝑧2 = 𝑧1 (𝑛2 flops)(c) back substitution: solve π‘ˆπ‘₯ = 𝑧2 (𝑛2 flops)

Complexity: (2/3)𝑛3 + 2𝑛2 β‰ˆ (2/3)𝑛3 flops

this is the standard method for solving 𝐴π‘₯ = 𝑏

Linear equations 7.8

Multiple right-hand sides

two equations with the same matrix 𝐴 (nonsingular and 𝑛 Γ— 𝑛):

𝐴π‘₯ = 𝑏, 𝐴π‘₯ = οΏ½ΜƒοΏ½

β€’ factor 𝐴 once

β€’ forward/back substitution to get π‘₯

β€’ forward/back substitution to get π‘₯

complexity: (2/3)𝑛3 + 4𝑛2 β‰ˆ (2/3)𝑛3

Exercise: propose an efficient method for solving

𝐴π‘₯ = 𝑏, 𝐴𝑇π‘₯ = οΏ½ΜƒοΏ½

Linear equations 7.9

LU factorization and matrix inverse

suppose 𝐴 is nonsingular and 𝑛 Γ— 𝑛, with LU factorization

𝐴 = π‘ƒπΏπ‘ˆ

β€’ inverse from LU factorization

π΄βˆ’1 = (π‘ƒπΏπ‘ˆ)βˆ’1 = π‘ˆβˆ’1πΏβˆ’1𝑃𝑇

β€’ gives interpretation of solve step: we evaluate

π‘₯ = π΄βˆ’1𝑏 = π‘ˆβˆ’1πΏβˆ’1𝑃𝑇𝑏

in three steps𝑧1 = 𝑃𝑇𝑏, 𝑧2 = πΏβˆ’1𝑧1, π‘₯ = π‘ˆβˆ’1𝑧2

Linear equations 7.10

Computing the inverse

solve 𝐴𝑋 = 𝐼 column by column

β€’ one LU factorization of 𝐴: 2𝑛3/3 flops

β€’ 𝑛 solve steps: 2𝑛3 flops

β€’ total: (8/3)𝑛3 flops

slightly faster methods exist that exploit structure in right-hand side 𝐼

Conclusion: do not solve 𝐴π‘₯ = 𝑏 by multiplying π΄βˆ’1 with 𝑏

Linear equations 7.11

LU factorization without pivoting

[𝐴11 𝐴1,2:𝑛𝐴2:𝑛,1 𝐴2:𝑛,2:𝑛

]=

[1 0

𝐿2:𝑛,1 𝐿2:𝑛,2:𝑛

] [π‘ˆ11 π‘ˆ1,2:𝑛0 π‘ˆ2:𝑛,2:𝑛

]=

[π‘ˆ11 π‘ˆ1,2:𝑛

π‘ˆ11𝐿2:𝑛,1 𝐿2:𝑛,1π‘ˆ1,2:𝑛 + 𝐿2:𝑛,2:π‘›π‘ˆ2:𝑛,2:𝑛

]Recursive algorithm

β€’ determine first row of π‘ˆ and first column of 𝐿

π‘ˆ11 = 𝐴11, π‘ˆ1,2:𝑛 = 𝐴1,2:𝑛, 𝐿2:𝑛,1 =1𝐴11

𝐴2:𝑛,1

β€’ factor the (𝑛 βˆ’ 1) Γ— (𝑛 βˆ’ 1)-matrix 𝐴2:𝑛,2:𝑛 βˆ’ 𝐿2:𝑛,1π‘ˆ1,2:𝑛 as

𝐴2:𝑛,2:𝑛 βˆ’ 𝐿2:𝑛,1π‘ˆ1,2:𝑛 = 𝐿2:𝑛,2:π‘›π‘ˆ2:𝑛,2:𝑛

this is an LU factorization (without pivoting) of size (𝑛 βˆ’ 1) Γ— (𝑛 βˆ’ 1)

Linear equations 7.12

Example

LU factorization (without pivoting) of

𝐴 =

8 2 94 9 46 7 9

write as 𝐴 = πΏπ‘ˆ with 𝐿 unit lower triangular, π‘ˆ upper triangular

𝐴 =

8 2 94 9 46 7 9

=

1 0 0𝐿21 1 0𝐿31 𝐿32 1

π‘ˆ11 π‘ˆ12 π‘ˆ130 π‘ˆ22 π‘ˆ230 0 π‘ˆ33

Linear equations 7.13

Example

β€’ first row of π‘ˆ, first column of 𝐿:8 2 94 9 46 7 9

=

1 0 01/2 1 03/4 𝐿32 1

8 2 90 π‘ˆ22 π‘ˆ230 0 π‘ˆ33

β€’ second row of π‘ˆ, second column of 𝐿:[

9 47 9

]βˆ’[

1/23/4

] [2 9

]=

[1 0𝐿32 1

] [π‘ˆ22 π‘ˆ230 π‘ˆ33

][

8 βˆ’1/211/2 9/4

]=

[1 0

11/16 1

] [8 βˆ’1/20 π‘ˆ33

]β€’ third row of π‘ˆ: π‘ˆ33 = 9/4 + 11/32 = 83/32

Conclusion

𝐴 =

8 2 94 9 46 7 9

=

1 0 01/2 1 03/4 11/16 1

8 2 90 8 βˆ’1/20 0 83/32

Linear equations 7.14

Not every nonsingular 𝐴 can be factored as 𝐴 = πΏπ‘ˆ

𝐴 =

1 0 00 0 20 1 βˆ’1

=

1 0 0𝐿21 1 0𝐿31 𝐿32 1

π‘ˆ11 π‘ˆ12 π‘ˆ130 π‘ˆ22 π‘ˆ230 0 π‘ˆ33

β€’ first row of π‘ˆ, first column of 𝐿:

1 0 00 0 20 1 βˆ’1

=

1 0 00 1 00 𝐿32 1

1 0 00 π‘ˆ22 π‘ˆ230 0 π‘ˆ33

β€’ second row of π‘ˆ, second column of 𝐿:[

0 21 βˆ’1

]=

[1 0𝐿32 1

] [π‘ˆ22 π‘ˆ230 π‘ˆ33

]π‘ˆ22 = 0, π‘ˆ23 = 2, 𝐿32 Β· 0 = 1 ?

Linear equations 7.15

LU factorization (with row pivoting)

if 𝐴 is 𝑛 Γ— 𝑛 and nonsingular, then it can be factored as

𝐴 = π‘ƒπΏπ‘ˆ

𝑃 is a permutation matrix, 𝐿 is unit lower triangular, π‘ˆ is upper triangular

β€’ not unique; there may be several possible choices for 𝑃, 𝐿, π‘ˆ

β€’ interpretation: permute the rows of 𝐴 and factor 𝑃𝑇𝐴 as 𝑃𝑇𝐴 = πΏπ‘ˆ

β€’ also known as Gaussian elimination with partial pivoting (GEPP)

β€’ complexity: (2/3)𝑛3 flops

we skip the details of calculating 𝑃, 𝐿, π‘ˆ

Linear equations 7.16

Example

0 5 52 9 06 8 8

=

0 0 10 1 01 0 0

1 0 01/3 1 00 15/19 1

6 8 80 19/3 βˆ’8/30 0 135/19

the factorization is not unique; the same matrix can be factored as

0 5 52 9 06 8 8

=

0 1 01 0 00 0 1

1 0 00 1 03 βˆ’19/5 1

2 9 00 5 50 0 27

Linear equations 7.17

Effect of rounding error

[10βˆ’5 1

1 1

] [π‘₯1π‘₯2

]=

[10

]

solution:π‘₯1 =

βˆ’11 βˆ’ 10βˆ’5 , π‘₯2 =

11 βˆ’ 10βˆ’5

β€’ let us solve using LU factorization for the two possible permutations:

𝑃 =

[1 00 1

]or 𝑃 =

[0 11 0

]β€’ we round intermediate results to four significant decimal digits

Linear equations 7.18

First choice: 𝑃 = 𝐼 (no pivoting)[10βˆ’5 1

1 1

]=

[1 0

105 1

] [10βˆ’5 1

0 1 βˆ’ 105

]β€’ 𝐿, π‘ˆ rounded to 4 significant decimal digits

𝐿 =

[1 0

105 1

], π‘ˆ =

[10βˆ’5 1

0 βˆ’105

]β€’ forward substitution[

1 0105 1

] [𝑧1𝑧2

]=

[10

]=β‡’ 𝑧1 = 1, 𝑧2 = βˆ’105

β€’ back substitution[10βˆ’5 1

0 βˆ’105

] [π‘₯1π‘₯2

]=

[1

βˆ’105

]=β‡’ π‘₯1 = 0, π‘₯2 = 1

error in π‘₯1 is 100%Linear equations 7.19

Second choice: interchange rows[1 1

10βˆ’5 1

]=

[1 0

10βˆ’5 1

] [1 10 1 βˆ’ 10βˆ’5

]β€’ 𝐿, π‘ˆ rounded to 4 significant decimal digits

𝐿 =

[1 0

10βˆ’5 1

], π‘ˆ =

[1 10 1

]β€’ forward substitution[

1 010βˆ’5 1

] [𝑧1𝑧2

]=

[01

]=β‡’ 𝑧1 = 0, 𝑧2 = 1

β€’ backward substitution[1 10 1

] [π‘₯1π‘₯2

]=

[01

]=β‡’ π‘₯1 = βˆ’1, π‘₯2 = 1

error in π‘₯1, π‘₯2 is about 10βˆ’5

Linear equations 7.20

Conclusion: rounding error and LU factorization

β€’ for some choices of 𝑃, small errors in the algorithm can cause very large errorsin the solution

β€’ this is called numerical instability: for the first choice of 𝑃 in the example, thealgorithm is unstable; for the second choice of 𝑃, it is stable

β€’ from numerical analysis: there is a simple rule for selecting a good permutation

(we skip the details, since we skipped the details of the factorization)

Linear equations 7.21

Sparse linear equations

if 𝐴 is sparse, it is usually factored as

𝐴 = 𝑃1πΏπ‘ˆπ‘ƒ2

𝑃1 and 𝑃2 are permutation matrices

β€’ interpretation: permute rows and columns of 𝐴 and factor οΏ½ΜƒοΏ½ = 𝑃𝑇1 𝐴𝑃𝑇2

οΏ½ΜƒοΏ½ = πΏπ‘ˆ

β€’ choice of 𝑃1 and 𝑃2 greatly affects the sparsity of 𝐿 and π‘ˆ: several heuristicmethods exist for selecting good permutations

β€’ in practice: #flops οΏ½ (2/3)𝑛3; exact value depends on 𝑛, number of nonzeroelements, sparsity pattern

Linear equations 7.22

Conclusion

different levels of detail in understanding how linear equation solvers work

Highest level

β€’ x = A \ b costs (2/3)𝑛3

β€’ more efficient than x = inv(A) * b

Intermediate level: factorization step 𝐴 = π‘ƒπΏπ‘ˆ followed by solve step

Lowest level: details of factorization 𝐴 = π‘ƒπΏπ‘ˆ

β€’ for most applications, level 1 is sufficient

β€’ in some situations (e.g., multiple right-hand sides) level 2 is useful

β€’ level 3 is important for experts who write numerical libraries

Linear equations 7.23