LU Decomposition

LU decomposition is a common method to solve polynomial Ax=bAx=b, and this method is consist of three steps:

(1) LU factorization: Gaussian elimination on matrix AA, factorize AA to L,UL,U such that PA=LUPA=LU

(2) Forward substitution: Solve lower triangular system Ld=PbLd = Pb

(3) Backward substitution: Solve upper triangular system Ux=dUx = d

In the following part, I would use the example of AA and bb to explain the calculation of LU decomposition.

A=(123251031010),b=(3713)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)

(1) LU factorization

In LU factorization, we need to factorize AA into LL and UU, and let LU=PALU=PA

L=(a100a2a30a4a5a6),U=(b1b2b30b4b500b6)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)

Sometimes, we can get the right result through the decomposition of LU=ALU=A, but this decomposition isn’t stable that might cause a large error in the calculation. Therefore, we need to do the “pivoting” procedure (PP) 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.

UU is obtained from Gaussian elimination of AA, and LL is computed from UU. PP is the identity matrix at first, and if we exchange row during the Gaussian elimination, the same exchange need to be applied on PP. And A,P,L,UA, P, L, U all have the same size.

The calculation below is LU factorization example:

A=(123251031016),P=(100010001)R1R3(310162510123),P=(001010100)R223R1(310100532304313),P=(001010100)R315R1R2(31016053230055)=U,P=(001010100)\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}

L=((321)/3,(05343)/53,(0095)/95)=(100231013451)\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}

(2) Forward and Backward substitution

Forward and Backward substitution is used to calculate the xx and yy in the following functions.

{Ax=bLU=PAUx=y{Ly=PbUx=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}

Forward substitution:

Pb=(001010100)(3713)=(1373)Ly=(100231013451)(y1y2y3)=(1373){y1=1323y1+y2=113y1+45y2+y3=3{y1=13y2=53y3=0y=(13530)\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}

Backward substitution:

Ux=(31016053230095)(x1x2x3)=(13530){3x1+10x2+16x3=1353x223x3=5395x3=0{x1=1x2=1x3=0x=(110)\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}

(3) LU decomposition in Python

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
from scipy.linalg import lu
from scipy.linalg import solve_triangular
A=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 b
# Forward and backward substitution
d=solve_triangular(L, b, lower=True)
x=solve_triangular(U, y)
# Check if the result from LU-decomposition is the same as direct computation
x2 = np.linalg.solve(A,b)

All articles in this blog adopt the CC BY-SA 4.0 agreement except for special statements. Please indicate the source for reprinting!