-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterativos.py
141 lines (122 loc) · 2.74 KB
/
iterativos.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import time, threading, sys
def Seidel(M,b,presicion):
sol = Array(len(M),0)
anterior = Array(len(M),0)
error = Array(len(M),8000000000)
iteraciones = 0
b = B(M)
r = b/(1-b)
t = r*Norma(error)
print("b de M es ",b)
while(t > presicion):
print(iteraciones)
mult = MultSeidel(M,anterior)
sol = Suma(mult,b)
error = Resta(sol,anterior)
ne = Norma(error)
t = r*ne
print("solucion ",sol)
Copiar(sol,anterior)
iteraciones += 1
print("Seidel ",iteraciones," iteraciones\n")
return sol
def MultSeidel(M,mult):
for i in range(0,len(M)):
mult[i] = 0
for j in range(0,len(M)):
mult[i] += M[i][j]*mult[j]
return mult
def B(M):
p = P(M)
q = Q(M)
b = -1
for i in range(0,len(M)):
c = q[i]/(1 - p[i])
if(c > b): b = c
return b
def P(M):
p = Array(len(M),0)
for i in range(0,len(M)):
for j in range(0,i):
if(M[i][j]>0):
p[i]+=M[i][j]
else:
p[i]-=M[i][j]
return p
def Q(M):
q = Array(len(M),0)
for i in range(0,len(M)):
for j in range(i + 1,len(M)):
if(M[i][j]>0):
q[i]+=M[i][j]
else:
q[i]-=M[i][j]
return q
def Jacobi(M,b,presicion):
sol = Array(len(M),0)
anterior = Array(len(M),0)
error = Array(len(M),8000000000)
iteraciones = 0
a = NormaMatriz(M)
r = a/(1-a)
t = r*Norma(error)
print("La norma de M es ",a)
v = 8
while(t > presicion):
print(iteraciones)
mult = Multiplicacion(M,anterior)
sol = Suma(mult,b)
error = Resta(sol,anterior)
ne = Norma(error)
t = r*ne
print("solucion ",sol)
Copiar(sol,anterior)
iteraciones += 1
print("Jacobi ",iteraciones," iteraciones\n")
return sol
def Copiar(a,b):
for i in range(0,len(a)):
b[i] = a[i]
return 0
def Norma(x):
max = -1
for i in range(0,len(x)):
if(x[i]>max):
max = x[i]
return max
def NormaMatriz(M):
max = -1
for i in range(0,len(M)):
suma = 0
for j in range(0,len(M)):
if(M[i][j] > 0):
suma += M[i][j]
else:
suma -= M[i][j]
if(suma > max): max = suma
return max
def Suma(a,b):
suma = Array(len(a),0)
for i in range(0,len(a)):
suma[i] = a[i] + b[i]
return suma
def Resta(a,b):
suma = Array(len(a),0)
for i in range(0,len(a)):
suma[i] = a[i] - b[i]
return suma
def Multiplicacion(M,x):
mult = Array(len(M),0)
for i in range(0,len(M)):
for j in range(0,len(M)):
mult[i] += M[i][j]*x[j]
return mult
def Array(a,valor):
sol = []
while a:
sol.append(valor)
a = a-1
return sol
M = [[0,1/10,-1/5],[1/5,0,1/5],[-1/4,1/8,0]]
b = [3/5,2,-1]
print(Jacobi(M,b,0.01))