Skip to content

Commit a488369

Browse files
committed
LU decomposition
1 parent fe6e959 commit a488369

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: ArithmeticAnalysis/LUdecomposition.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import math
2+
import numpy
3+
4+
def LUDecompose (table): #table that contains our data
5+
#table has to be a square array so we need to check first
6+
rows,columns=numpy.shape(table)
7+
L=numpy.zeros((rows,columns))
8+
U=numpy.zeros((rows,columns))
9+
if rows!=columns:
10+
return
11+
for i in range (columns):
12+
for j in range(i-1):
13+
sum=0
14+
for k in range (j-1):
15+
sum+=L[i][k]*U[k][j]
16+
L[i][j]=(table[i][j]-sum)/U[j][j]
17+
L[i][i]=1
18+
for j in range(i-1,columns):
19+
sum1=0
20+
for k in range(i-1):
21+
sum1+=L[i][k]*U[k][j]
22+
U[i][j]=table[i][j]-sum1
23+
return L,U
24+
25+
26+
27+
28+
29+
30+
31+
matrix =numpy.array([[2,-2,1],[0,1,2],[5,3,1]])
32+
L,U = LUDecompose(matrix)
33+
print(L)
34+
print(U)

0 commit comments

Comments
 (0)