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


Recommended