# Program to Perform LU Decomposition of any Matrix
def LUdecomposition( a , l , u , n):
for i in range ( 0, n):
for j in range ( 0, n):
if (j < i):
l[j][i] = 0
else:
l[j][i] = a[j][i]
for k in range (0 , i ):
l[j][i] = l[j][i] - l[j][k] * u[k][i];
for j in range ( 0, n):
if (j < i):
u[i][j] = 0;
elif (j == i):
u[i][j] = 1
else:
u[i][j] = a[i][j] / l[i][i];
for k in range (0 , i ):
u[i][j] = u[i][j] - ((l[i][k] * u[k][j]) / l[i][i]);
#++++++++++++++++++++++++++++++++++
n=3
print("Enter the order of square matrix: ",n)
a =[ [-1.0 , 1.0 , 2.0],
[3.0 , -1.0 , 1.0],
[-1.0 , 3.0 , 4.0]]
l= [ [0.0 for i in range(n+1)] for j in range(n+1) ] #[n][n],
u= [ [0.0 for i in range(n+1)] for j in range(n+1) ] #[n][n],
for i in range(0 ,n):
for j in range(0,n):
print( "a[",i,"][",j,"]=",round(a[i][j],4),'\t',end='')
print("")
LUdecomposition(a, l, u, n);
print("\nL Decomposition is as follows...")
print("\n[L]: \n");
for i in range(0 ,n):
for j in range(0,n):
print( "L[",i,"][",j,"]=",round(l[i][j],4),'\t',end='')
print("")
print("\nU Decomposition is as follows...")
print("\n[U]: \n");
for i in range(0 ,n):
for j in range(0,n):
print( "U[",i,"][",j,"]=",round(u[i][j],4),'\t',end='')
print("")
======== RESTART: F:/2018-09勤益科大數值分析/數值分析/PYTHON/EX6-5-3.py ===========
Enter the order of square matrix: 3
a[ 0 ][ 0 ]= -1.0 a[ 0 ][ 1 ]= 1.0 a[ 0 ][ 2 ]= 2.0
a[ 1 ][ 0 ]= 3.0 a[ 1 ][ 1 ]= -1.0 a[ 1 ][ 2 ]= 1.0
a[ 2 ][ 0 ]= -1.0 a[ 2 ][ 1 ]= 3.0 a[ 2 ][ 2 ]= 4.0
L Decomposition is as follows...
[L]:
L[ 0 ][ 0 ]= -1.0 L[ 0 ][ 1 ]= 0 L[ 0 ][ 2 ]= 0
L[ 1 ][ 0 ]= 3.0 L[ 1 ][ 1 ]= 2.0 L[ 1 ][ 2 ]= 0
L[ 2 ][ 0 ]= -1.0 L[ 2 ][ 1 ]= 2.0 L[ 2 ][ 2 ]= -5.0
U Decomposition is as follows...
[U]:
U[ 0 ][ 0 ]= 1 U[ 0 ][ 1 ]= -1.0 U[ 0 ][ 2 ]= -2.0
U[ 1 ][ 0 ]= 0 U[ 1 ][ 1 ]= 1 U[ 1 ][ 2 ]= 3.5
U[ 2 ][ 0 ]= 0 U[ 2 ][ 1 ]= 0 U[ 2 ][ 2 ]= 1
>>>
沒有留言:
張貼留言