LU decomposition is a common method to solve polynomial A x = b Ax=b A x = b , and this method is consist of three steps:
(1) LU factorization: Gaussian elimination on matrix A A A , factorize A A A to L , U L,U L , U such that P A = L U PA=LU P A = L U
(2) Forward substitution: Solve lower triangular system L d = P b Ld = Pb L d = P b
(3) Backward substitution: Solve upper triangular system U x = d Ux = d U x = d
In the following part, I would use the example of A A A and b b b to explain the calculation of LU decomposition.
A = ( 1 2 3 2 5 10 3 10 10 ) , b = ( 3 7 13 ) A=\left(\begin{array}{ccc}
1 & 2 & 3 \\
2 & 5 & 10 \\
3 & 10 & 10
\end{array}\right), \quad b=\left(\begin{array}{c}
3 \\
7 \\
13
\end{array}\right)
A = ⎝ ⎛ 1 2 3 2 5 1 0 3 1 0 1 0 ⎠ ⎞ , b = ⎝ ⎛ 3 7 1 3 ⎠ ⎞
(1) LU factorization
In LU factorization, we need to factorize A A A into L L L and U U U , and let L U = P A LU=PA L U = P A
L = ( a 1 0 0 a 2 a 3 0 a 4 a 5 a 6 ) , U = ( b 1 b 2 b 3 0 b 4 b 5 0 0 b 6 ) L=\left(\begin{array}{ccc}
a_1 & 0 & 0 \\
a_2 & a_3 & 0 \\
a_4 & a_5 & a_6
\end{array}\right), \quad U=\left(\begin{array}{ccc}
b_1 & b_2 & b_3 \\
0 & b_4 & b_5 \\
0 & 0 & b_6
\end{array}\right)
L = ⎝ ⎛ a 1 a 2 a 4 0 a 3 a 5 0 0 a 6 ⎠ ⎞ , U = ⎝ ⎛ b 1 0 0 b 2 b 4 0 b 3 b 5 b 6 ⎠ ⎞
Sometimes, we can get the right result through the decomposition of L U = A LU=A L U = A , but this decomposition isn’t stable that might cause a large error in the calculation. Therefore, we need to do the “pivoting” procedure (P P P ) during the factorization step. “pivoting” means that we should keep the value of each non-zero element of each row to be larger the other elements of the same column below this one, and we can achieve this goal by row exchange.
U U U is obtained from Gaussian elimination of A A A , and L L L is computed from U U U . P P P is the identity matrix at first, and if we exchange row during the Gaussian elimination, the same exchange need to be applied on P P P . And A , P , L , U A, P, L, U A , P , L , U all have the same size.
The calculation below is LU factorization example:
A = ( 1 2 3 2 5 10 3 10 16 ) , P = ( 1 0 0 0 1 0 0 0 1 ) ⟶ R 1 ↔ R 3 ( 3 10 16 2 5 10 1 2 3 ) , P = ( 0 0 1 0 1 0 1 0 0 ) ⟶ R 2 − 2 3 R 1 ( 3 10 10 0 − 5 3 − 2 3 0 − 4 3 − 1 3 ) , P = ( 0 0 1 0 1 0 1 0 0 ) ⟶ R 3 − 1 5 R 1 R 2 ( 3 10 16 0 − 5 3 − 2 3 0 0 − 5 5 ) = U , P = ( 0 0 1 0 1 0 1 0 0 ) \begin{aligned}
&A=\left(\begin{array}{ccc}
1 & 2 & 3 \\
2 & 5 & 10 \\
3 & 10 & 16
\end{array}\right), \quad P=\left(\begin{array}{lll}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{array}\right) \\
&\stackrel{R_{1} \leftrightarrow R_{3}}{\longrightarrow}\left(\begin{array}{ccc}
3 & 10 & 16 \\
2 & 5 & 10 \\
1 & 2 & 3
\end{array}\right), \quad P=\left(\begin{array}{lll}
0 & 0 & 1 \\
0 & 1 & 0 \\
1 & 0 & 0
\end{array}\right) \\
&\stackrel{R_{2}-\frac{2}{3} R_{1}}{\longrightarrow}\left(\begin{array}{ccc}
3 & 10 & 10 \\
0 & -\frac{5}{3} & -\frac{2}{3} \\
0 & -\frac{4}{3} & -\frac{1}{3}
\end{array}\right), \quad P=\left(\begin{array}{lll}
0 & 0 & 1 \\
0 & 1 & 0 \\
1 & 0 & 0
\end{array}\right) \\
&\stackrel{R_{3}-\frac{1}{5} R_{1} R_{2}}{\longrightarrow}\left(\begin{array}{ccc}
3 & 10 & 16 \\
0 & -\frac{5}{3} & -\frac{2}{3} \\
0 & 0 & -\frac{5}{5}
\end{array}\right)=U, \quad P=\left(\begin{array}{lll}
0 & 0 & 1 \\
0 & 1 & 0 \\
1 & 0 & 0
\end{array}\right)
\end{aligned}
A = ⎝ ⎛ 1 2 3 2 5 1 0 3 1 0 1 6 ⎠ ⎞ , P = ⎝ ⎛ 1 0 0 0 1 0 0 0 1 ⎠ ⎞ ⟶ R 1 ↔ R 3 ⎝ ⎛ 3 2 1 1 0 5 2 1 6 1 0 3 ⎠ ⎞ , P = ⎝ ⎛ 0 0 1 0 1 0 1 0 0 ⎠ ⎞ ⟶ R 2 − 3 2 R 1 ⎝ ⎛ 3 0 0 1 0 − 3 5 − 3 4 1 0 − 3 2 − 3 1 ⎠ ⎞ , P = ⎝ ⎛ 0 0 1 0 1 0 1 0 0 ⎠ ⎞ ⟶ R 3 − 5 1 R 1 R 2 ⎝ ⎛ 3 0 0 1 0 − 3 5 0 1 6 − 3 2 − 5 5 ⎠ ⎞ = U , P = ⎝ ⎛ 0 0 1 0 1 0 1 0 0 ⎠ ⎞
L = ( ( 3 2 1 ) / 3 , ( 0 − 5 3 − 4 3 ) / − 5 3 , ( 0 0 − 9 5 ) / − 9 5 ) = ( 1 0 0 2 3 1 0 1 3 4 5 1 ) \begin{aligned}
L&=\left(\left(\begin{array}{l}
3 \\
2 \\
1
\end{array}\right) / 3,\left(\begin{array}{c}
0 \\
-\frac{5}{3} \\
-\frac{4}{3}
\end{array}\right) / -\frac{5}{3},\left(\begin{array}{c}
0 \\
0 \\
-\frac{9}{5}
\end{array}\right) /-\frac{9}{5}\right) \\
&=\left(\begin{array}{ccc}
1 & 0 & 0 \\
\frac{2}{3} & 1 & 0 \\
\frac{1}{3} & \frac{4}{5} & 1
\end{array}\right)
\end{aligned}
L = ⎝ ⎛ ⎝ ⎛ 3 2 1 ⎠ ⎞ / 3 , ⎝ ⎛ 0 − 3 5 − 3 4 ⎠ ⎞ / − 3 5 , ⎝ ⎛ 0 0 − 5 9 ⎠ ⎞ / − 5 9 ⎠ ⎞ = ⎝ ⎛ 1 3 2 3 1 0 1 5 4 0 0 1 ⎠ ⎞
(2) Forward and Backward substitution
Forward and Backward substitution is used to calculate the x x x and y y y in the following functions.
{ A x = b L U = P A U x = y → { L y = P b U x = y \begin{aligned}
&\left\{\begin{array} { l }
{ A x = b } \\
{ L U = P A } \\
{ U x = y }
\end{array} \rightarrow \left\{\begin{array}{l}
L y=P b \\
U x=y
\end{array}\right.\right.\\
\end{aligned}
⎩ ⎨ ⎧ A x = b L U = P A U x = y → { L y = P b U x = y
Forward substitution:
P b = ( 0 0 1 0 1 0 1 0 0 ) ⋅ ( 3 7 13 ) = ( 13 7 3 ) L y = ( 1 0 0 2 3 1 0 1 3 4 5 1 ) ⋅ ( y 1 y 2 y 3 ) = ( 13 7 3 ) → { y 1 = 13 2 3 y 1 + y 2 = 1 1 3 y 1 + 4 5 y 2 + y 3 = 3 → { y 1 = 13 y 2 = − 5 3 y 3 = 0 → y = ( 13 − 5 3 0 ) \begin{aligned}
&P b=\left(\begin{array}{lll}
0 & 0 & 1 \\
0 & 1 & 0 \\
1 & 0 & 0
\end{array}\right) \cdot\left(\begin{array}{l}
3 \\
7 \\
13
\end{array}\right)=\left(\begin{array}{c}
13 \\
7 \\
3
\end{array}\right)\\
&L y=\left(\begin{array}{ccc}
1 & 0 & 0 \\
\frac{2}{3} & 1 & 0 \\
\frac{1}{3} & \frac{4}{5} & 1
\end{array}\right) \cdot\left(\begin{array}{l}
y_{1} \\
y_{2} \\
y_{3}
\end{array}\right)=\left(\begin{array}{c}
13 \\
7 \\
3
\end{array}\right)\\
&\rightarrow\left\{\begin{array} { l }
{ y _ { 1 } = 1 3 } \\
{ \frac { 2 } { 3 } y _ { 1 } + y _ { 2 } = 1 } \\
{ \frac { 1 } { 3 } y _ { 1 } + \frac { 4 } { 5 } y _ { 2 } + y _ { 3 } = 3 }
\end{array} \rightarrow \left\{\begin{array}{l}
y_{1}=13 \\
y_{2}=-\frac{5}{3} \\
y_{3}=0
\end{array} \quad \rightarrow y=\left(\begin{array}{c}
13 \\
-\frac{5}{3} \\
0
\end{array}\right)\right.\right.\\
\end{aligned}
P b = ⎝ ⎛ 0 0 1 0 1 0 1 0 0 ⎠ ⎞ ⋅ ⎝ ⎛ 3 7 1 3 ⎠ ⎞ = ⎝ ⎛ 1 3 7 3 ⎠ ⎞ L y = ⎝ ⎛ 1 3 2 3 1 0 1 5 4 0 0 1 ⎠ ⎞ ⋅ ⎝ ⎛ y 1 y 2 y 3 ⎠ ⎞ = ⎝ ⎛ 1 3 7 3 ⎠ ⎞ → ⎩ ⎨ ⎧ y 1 = 1 3 3 2 y 1 + y 2 = 1 3 1 y 1 + 5 4 y 2 + y 3 = 3 → ⎩ ⎨ ⎧ y 1 = 1 3 y 2 = − 3 5 y 3 = 0 → y = ⎝ ⎛ 1 3 − 3 5 0 ⎠ ⎞
Backward substitution:
U x = ( 3 10 16 0 − 5 3 − 2 3 0 0 − 9 5 ) ⋅ ( x 1 x 2 x 3 ) = ( 13 − 5 3 0 ) → { 3 x 1 + 10 x 2 + 16 x 3 = 13 − 5 3 x 2 − 2 3 x 3 = − 5 3 − 9 5 x 3 = 0 → { x 1 = 1 x 2 = 1 x 3 = 0 → x = ( 1 1 0 ) \begin{aligned}
&Ux=\left(\begin{array}{ccc}
3 & 10 & 16 \\
0 & -\frac{5}{3} & -\frac{2}{3} \\
0 & 0 & -\frac{9}{5}
\end{array}\right) \cdot\left(\begin{array}{l}
x_{1} \\
x_{2} \\
x_{3}
\end{array}\right)=\left(\begin{array}{c}
13 \\
-\frac{5}{3} \\
0
\end{array}\right)\\
&\rightarrow\left\{\begin{array} { c }
{ 3 x _ { 1 } + 1 0 x _ { 2 } + 1 6 x _ { 3 } = 1 3 } \\
{ - \frac { 5 } { 3 } x _ { 2 } - \frac { 2 } { 3 } x _ { 3 } = - \frac { 5 } { 3 } } \\
{ - \frac { 9 } { 5 } x _ { 3 } = 0 }
\end{array} \rightarrow \left\{\begin{array}{l}
x_{1}=1 \\
x_{2}=1 \\
x_{3}=0
\end{array} \rightarrow x=\left(\begin{array}{l}
1 \\
1 \\
0
\end{array}\right)\right.\right.
\end{aligned}
U x = ⎝ ⎛ 3 0 0 1 0 − 3 5 0 1 6 − 3 2 − 5 9 ⎠ ⎞ ⋅ ⎝ ⎛ x 1 x 2 x 3 ⎠ ⎞ = ⎝ ⎛ 1 3 − 3 5 0 ⎠ ⎞ → ⎩ ⎨ ⎧ 3 x 1 + 1 0 x 2 + 1 6 x 3 = 1 3 − 3 5 x 2 − 3 2 x 3 = − 3 5 − 5 9 x 3 = 0 → ⎩ ⎨ ⎧ x 1 = 1 x 2 = 1 x 3 = 0 → x = ⎝ ⎛ 1 1 0 ⎠ ⎞
(3) LU decomposition in Python
1 2 3 4 5 6 7 8 9 10 11 12 import numpy as npfrom scipy.linalg import lufrom scipy.linalg import solve_triangularA =np.array([[3 -1 2],[1 0 -1],[4 2 -3]]) P, L, U =lu(A)b =np.array([[8],[-1],[-4]]) b = P.T @ b # Row changes in bd =solve_triangular(L, b, lower =True )x =solve_triangular(U, y) x2 = np.linalg.solve(A,b)