-
Notifications
You must be signed in to change notification settings - Fork 2
/
LDLt.py
48 lines (45 loc) · 1.33 KB
/
LDLt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import math
import funciones
def LDLt(A,B,n):
Lt = [[0.0]*n]*n
for i in range(n):
suma = A[i][i]
for k in range(i):
suma = suma - A[k][i]**2
if suma < 0: #no es definida positiva
return ["NULL","NULL"]
A[i][i] = math.sqrt(suma)
for j in range(i+1, n):
suma = A[i][j]
for k in range(i):
suma = suma - A[k][i]*A[k][j]
A[i][j] = suma / A[i][i]
for j in range(n):
for i in range(n):
if(i > j):
A[i][j] = 0.0
Lt = A
L = funciones.transMatriz(Lt,n)
D = [[float(i == j) for j in range(n)] for i in range(n)]
for i in range(n):
D[i][i] = float(L[i][i])
for j in range(i+1):
L[i][j] = L[i][j]/D[j][j]
for i in range(n):
for j in range(i,n):
Lt[i][j] = Lt[i][j]/D[i][i]
for i in range(n):
D[i][i] = D[i][i]**2
print "\n Resolucion por Algoritmo LDLt "
print " -------------------------------------------------"
print " Matriz L Triangular Inferior"
funciones.imprimeMatriz(L)
print " Matriz D Diagonal"
funciones.imprimeMatriz(D)
print " Matriz Lt Triangular Superior"
funciones.imprimeMatriz(Lt)
print "Para hallar el resultado de LDLt.x=b requerimos 3 ecuaciones:"
z = funciones.solucionL(L,B,n) #Lz = b "obtenemos z"
y = funciones.solucionL(D,z,n) #Dy = z "obtenemos y"
x = funciones.solucionU(Lt,y,n) #Ltx = y "obtenemos x"
return z,y,x