2. 분산시스템 연구실
Contents
• LU Factorization from Gaussian
Elimination
• LU Factorization of Tridiagonal
Matrices
• LU Factorization with Pivoting
• Direct LU Factorization
• Application of LU Factorization
• Matlab’s Method
3. 분산시스템 연구실
Example. Circuit Analysis Application
Solve by LU Factorization of coefficient matrix
0V1 =
0V2 =
200V3 =
20R1 = 25R3 =
30R5 =10R2 =
10R4 =
1i
2i
3i
200)(10)(1030
0)(20)(1025
0)(10)(20
13233
12322
3121
=-+-+
=-+-+
=-+-
iiiii
Flow around lower loop
iiiii
Flow around upper loop
iiii
Flow around the left loop
6. 분산시스템 연구실
LU Factorization from Gaussian
Elimination
- Example 6.2
Example 6.2 Four-By-Four System
=
1415113
202092
91871
48124
A
Step 1: Row 1 is unchanged, and rows 2-4 are modified to give
4
1
11
21
21 ==
a
a
l
2
1
11
31
31 ==
a
a
l
4
3
11
41
31 ==
a
a
l
=
11920
181630
81640
48124
U
=
1000
0100
0010
0001
L
=
1415113
202092
91871
48124
U
=
1415113
202092
91871
48124
A
=
11500
0100
0014/1
0001
L
−−−
=
1415113
202092
19218370
48124
U
=
1415113
202092
91871
48124
A
=
11500
0102/1
0014/1
0001
L
=
1415113
181630
81640
48124
U
=
1415113
202092
91871
48124
A
=
11504/3
0102/1
0014/1
0001
L
7. 분산시스템 연구실
LU Factorization from Gaussian
Elimination
- Example 6.2
Step 2: Row 1 and 2 are unchanged, row 3 and 4 are transformed, yielding
Example 6.2 Four-By-Four System
4
3
22
32
32 ==
a
a
l
2
1
22
42
42 ==
a
a
l
=
7100
12400
81640
48124
U
Step 3: The fourth row is modified to complete the forward elimination stage:
4
1
33
43
43 ==
a
a
l
=
4000
12400
81640
48124
U
=
102/14/3
014/32/1
0014/1
0001
L
=
1415113
202092
91871
48124
A
=
14/12/14/3
014/32/1
0014/1
0001
L
=
1415113
202092
91871
48124
A
8. 분산시스템 연구실
LU Factorization from Gaussian
Elimination
- Example 6.2
Multiply L by U to verify the result:
Example 6.2 Four-By-Four System
1
4
1
2
1
4
3
01
4
3
2
1
001
4
1
0001
4000
12400
81640
48124
.
1415113
202092
91871
48124
=
L . U = A
9. 분산시스템 연구실
LU Factorization from Gaussian
Elimination
- MATLAB Function for LU Factorization
6.1.1 MATLAB Function for LU Factorization
Using Gaussian Elimination
function [L, U] = LU_Factor(A)
% LU factorization of matrix A
% using Gaussian elimination without pivoting
% Input : A n-by-n matrix
% Output L ( lower triangular ) and
% U (upper triangular )
[n, m] = size(A);
L = eye(n); % initialize matrices
U = A;
for j = 1 : n
for I = j + 1 : n
L( i , j ) = U( i, j ) / U( j, j );
U( i , : ) = U( i, j ) / L( j, j ) * U( j, : );
end
end
% display L and U
L
U
% verify results
B = L * U
A
15. 분산시스템 연구실
LU Factorization of Tridiagonal Matrix
• 6.2.1 Matlab function for LU Factorization of
a Tridiagonal Matrix
LU Factorization of a tridiagonal matrix T
• less computation
• less computer memory
16. 분산시스템 연구실
LU Factorization of Tridiagonal Matrix
function [dd, bb] = LU_tridiag(a, d, b)
% LU factorization of a tridiagonal matrix T
% Input
% a vector of elements above main diagonal, a(n) = 0
% d diagonal of matrix T
% b vector of elements below main diagonal, b(1) = 0
% The factorization of T consists of
% Lower bidiagonal matrix,
% 1’s on main diagonal; lower diagonal is bb
% Upper bidiagonal matrix,
% main diagonal is dd; upper diagonal is a
N = length(d)
bb(1) = 0;
dd(1) = d(1);
for i = 2 : n
bb(i) = b(i) / dd(i-1);
dd(i) = d(i) – bb(i) * a(i-1);
end
22. 분산시스템 연구실
6.3.2 Theory for pivoting 1/2
• 한 쪽에 A 행렬 , 다른 한쪽에 I 행렬을 놓는
다 .
(A: LU 분해 할 NxN 행렬 , I: NxN 단위행
렬 )
• A 행렬을 가우스 소거법을 이용해 상삼각 행
렬로 변환하면서 A 행렬에 가하는 행렬 연산
을 기록한다 . (Nx: 소거 , Px: 교환 )
ULAP
UNNNAPPP
UAPPPNNN
UAPNPNPN
nnn
nn
nn
⋅=⋅
⋅⋅⋅⋅=⋅⋅⋅⋅
=⋅⋅⋅⋅⋅⋅⋅⋅
=⋅⋅⋅⋅⋅⋅⋅
−
−
−
−
−
−
−−
−−
1
1
1
2
1
1121
121121
112211
'''
'''
27. 분산시스템 연구실
6.3.5 pivoting code
Function [L, U, P] = LU_pivot(A)
[n, n1] = size(A);
L=eye(n); P=eye(n); U=A;
For j = 1:n
[pivot m] = max(abs(U(j:n, j))); m = m+j-1;
if m ~= j
% interchange rows m and j in U
% interchange rows m and j in P
if j >= 2
% interchange rows m and j in columns 1:j-1 of L
end
end
for i = j+1:n
L(i, j) = U(i, j) / U(j, j);
U(i, :) = U(i, :) – L(i, j)*U(j, :);
end
end
30. 분산시스템 연구실
6.4.2 Type of Direct LU factorization
• 조건의 수 = N^2
변수의 수 = N(N+1)/2 + N(N+1)/2 =
N^2+N
• Doolittle 방식
L 의 대각 원소가 1
• Crout 방식
U 의 대각 원소가 1
• Cholesky 방식
L 과 U 의 같은 위치 대각 원소끼리 같
음
34. 분산시스템 연구실
6.4.6 Cholesky code
Function [L, U] = Cholesky(A)
% A is assumed to be symmetric.
% L is computed, and U = L’
[n, m] = size(A);
L = zeros(n, n);
for k = 1:n
L(k, k) = sqrt( A(k,k) – L(k, 1:k-1)*L(k, 1:k-1)’ );
for i = k+1:n
L(i, k) = (A(i,k) – L(i, 1:k-1)*L(k, 1:k-1)’)/L(k,
k);
end
end
36. 분산시스템 연구실
Contents
• 6.5 Applications of LU Factorization
– Linear Equations
– Tridiagonal System
– Determinant of a Matrix
– Inverse of a Matrix
• 6.6 MATLAB’s Methods
– lu, chol, det, inv
37. 분산시스템 연구실
6.5.1 Solving Systems of Linear Equations
• Ax = b → LUx = b → Ly = b
– A = LU, Ux = y
• Solve the system Ly = b for y
– Forward substitution
– y1 → y2 → …… yn
• Solve the system Ux = y for x
– Backward substitution
– xn → xn-1 → …… x1
• If pivoting has been used
– Ax=b → PAx=Pb, where PA=LU, Pb=c
39. 분산시스템 연구실
Ex 6.8 Solving Electrical Circuit for Several Voltages
11
22
33
3
2
1
0.8044/2510(4/5)]-(56/25)(1/30)[-20
2.2456/255)40/3)(3/12(80
0.804/532/40
onsubstitutibackwardBy
32
80
0
4000
3/503/1250
1020-30
ix
ix
ix
x
x
x
←===
←==+=
←===
=
−
−
⇒= yUx
40. 분산시스템 연구실
MATLAB Function to Solve the Linear System LUx=b
function x = LU_Solve(L, U, b)
% Function to solve the equation L U x = b
% L --> Lower triangular matrix (with 1's on diagonal)
% U --> Upper triangular matrix
% b --> Right-hand side vector
[n m] = size(L); z = zeros(n,1); x = zeros(n,1);
% Solve L z = b using forward substitution
z(1) = b(1);
for i = 2:n
z(i) = b(i) - L(i, 1:i-1) * z(1:i-1);
end
% Solve U x = z using back substitution
x(n) = z(n) / U(n, n);
for i = n-1 : -1 : 1
x(i) = (z(i) - U(i,i+1:n) * x(i+1:n)) / U(i, i);
end
42. 분산시스템 연구실
MATLAB Function for Solving a Tridiagonal System
function x = LU_tridiag_solve(a, d, b, r)
% Function to solve the eqquation A x = r
% LU factorization of A is expressed as
% Lower bidiagonal matrix:
% diagonal is 1s, lower diagonal is b; b(1) = 0.
% Upper bidiagonal matrix: diagonal is d, upper diagonal is a
% Right-hand-side vector is r
% Solve L z = r using forward substitution
n = length(d);
z(1) = r(1);
for i = 2:n
z(i) = r(i) - b(i) * z(i-1);
end
% Solve U x = z using back substitution
x(n) = z(n) / d(n);
for i = n-1 : -1 : 1
x(i) = (z(i) - a(i) * x(i+1)) / d(i);
end
43. 분산시스템 연구실
6.5.3 Determinant of a Matrix
ionfactorizatLUtheoccurredthat
esinterchangrowofnumbertheiswhere
)1()det(
thatsoused,ispivotingIf
)det(then,If
11
11
k
ul
ul
n
i
ii
n
i
ii
k
n
i
ii
n
i
ii
∏∏
∏∏
==
==
−=
=
==
A
LUPA
ALUA
-1
8)8)(1)(1()det(
800
510
211
111
012
001
121
112
211
332211 −=−==
−
−
=
−−
−=⇒
−
−
−
=
uuuA
ULA
Ex 6.10
44. 분산시스템 연구실
6.5.4 Inverse of a Matrix
• The inverse of an n-by-n matrix A
Axi=ei (i=1, … ,n)
– ei=[0 0 … 1 … 0 0]’, where the 1 appears
in the ith position
– X whose columns are the solution vectors
x1, … , xn is A-1
47. 분산시스템 연구실
MATLAB Function
function x = LU_Solve_Gen(L, U, B)
% Function to solve the equation L U x = B
% L --> Lower triangular matrix (1's on diagonal)
% U --> Upper triangular matrix
% B --> Right-hand-side matrix
[n n2] = size(L); [m1 m] = size(B);
% Solve L z = B using forward substitution
for j = 1:m
z(1, j) = b(1, j);
for i = 2 : n
z(i,j) = B(i,j) - L(i, 1:i-1) * z(1:i-1, j);
end
end
% Solve U x = z using back substitution
for j = 1:m
x(n, j) = z(n, j) / U(n, n);
for i = n-1 : -1 : 1
x(i,j) = (z(i,j)-U(i,i+1:n)*x(i+1:n,j)) / U(i,i);
end
end
49. 분산시스템 연구실
6.6 MATLAB’s Method
• 4 built-in functions
– LU decomposition of a square matrix A
• [L, U] = lu(A), [L, U, P] = lu(A)
– Cholesky factorization
• chol
– Determinant of a matrix
• det
– Inverse of a matrix
• inv