-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaussNewJacobi.py
239 lines (205 loc) · 6.55 KB
/
GaussNewJacobi.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf-8 -*-
"""
Created on Tue May 30 23:21:57 2017
@author: FALALU
"""
import math
m = []
def matrix_L(m):
if(isSquare(m)):
zeros_diagonal = zeros_matrix(len(m), len(m))
for i in range(1, len(m), 1):
for j in range(0, i):
zeros_diagonal[i][j] = - m[i][j]
return zeros_diagonal
else:
print ("Must be a square matrix")
def matrix_U(m):
if(isSquare(m)):
zeros_diagonal = zeros_matrix(len(m), len(m))
for i in range(0, len(m)):
for j in range(i+1, len(m)):
zeros_diagonal[i][j]= -m[i][j]
return zeros_diagonal
else:
print ("Must be a square matrix")
def diagonial_matrix(m):
if(isSquare(m)):
zeros_diagonal = zeros_matrix(len(m), len(m))
for i in range(len(m)):
for j in range(len(m)):
zeros_diagonal[i][i] = m[i][i]
return zeros_diagonal
else:
print ("Must be a square matrix")
def vector_vector_mult(v1, v2):
result = 0
for i in range(len(v1)):
result = result + v1[i]*v2[i]
return result
def vector_vector_add(v1, v2):
result = zeros_array(len(v1))
for i in range(len(v1)):
result[i] = v1[i]+v2[i]
return result
def matrix_inverse(m):
return matrix_scalar_mult(matrix_adjunct(m), (1/matrix_determinant(m)))
def matrix_scalar_mult(m, s):
for i in range(len(m)):
for j in range(len(m[0])):
m[i][j] = m[i][j] * s
return m
def matrix_cofactor(m):
result = []
for i in range(len(m)):
aRow=[]
for j in range(len(m)):
x=((-1)**(i+j))*matrix_determinant(adjunct_sub_matrix(m,i,j))
aRow.append(x)
result.append(aRow)
return result
def isSquare(m):
return all(len(row) == len(m) for row in m)
def isList(a):
return isinstance(a,list)
def dot_product(a1, a2):
if (isList(a1) and isList(a2)):
if (len(a1)==len(a2)):
sum = 0
for i in range(len(a1)):
sum = sum + a1[i]*a2[i]
return sum
else:
raise ValueError('a1 and a2 must be arrays of same length')
else:
raise ValueError('a1 and a2 must be must lists')
def matrix_vector_mult(m, v):
result = []
for row in m:
aProduct = dot_product(row, v)
result.append(aProduct)
return result
def matrix_transpose(m):
return [[x[i] for x in m] for i in range(len(m[0]))]
def matrix_adjunct(m):
if(isSquare(m)):
return matrix_transpose(matrix_cofactor(m))
else:
raise ValueError('Must be a square matrix')
def matrix_determinant(m):
return sum(recursion(m))
def zeros_array(size):
result=[]
for i in range(size):
result.append(0)
return result
def zeros_matrix(dim1, dim2):
result=[]
for i in range(dim1):
aRow = []
for j in range(dim2):
aRow.append(0)
result.append(aRow)
return result
def adjunct_sub_matrix(m, exclu_row, exclu_col):
sub_matrix=[]
for i in range(len(m)):
if(i == exclu_row):
continue
else:
aRow =[]
for j in range(len(m[i])):
if(j == exclu_col):
continue
else:
aRow.append(m[i][j])
sub_matrix.append(aRow)
return sub_matrix
def det_2_by_2(matrix):
return matrix[0][0]*matrix[1][1]-matrix[1][0]*matrix[0][1]
def recursion(matrix,somme=None,prod=1):
if(somme==None):
somme=[]
if(len(matrix)==1):
somme.append(matrix[0][0])
elif(len(matrix)==2):
somme.append(det_2_by_2(matrix)*prod)
else:
for index, elmt in enumerate(matrix[0]):
transposee = [list(a) for a in zip(*matrix[1:])]
del transposee[index]
mineur = [list(a) for a in zip(*transposee)]
somme = recursion(mineur,somme,prod*matrix[0][index]*(-1)**(index+2))
return somme
def matrix_matrix_addition(A, B):
if(isSquare(A) and isSquare(B)):
result = zeros_matrix(len(A), len(A))
for i in range(len(A)):
for j in range(len(A)):
result[i][j] = A[i][j] + B[i][j]
return result
else:
print ("Must be square matrices")
def norm(m):
sum = 0
for i in range(len(m)):
for j in range(len(m)):
sum = sum +m[i][j]**2
return math.sqrt(sum)
def matrix_matrix_subtraction(A, B):
if(isSquare(A) and isSquare(B)):
result = zeros_matrix(len(A), len(A))
for i in range(len(A)):
for j in range(len(A)):
result[i][j] = A[i][j] - B[i][j]
return result
else:
print ("Must be square matrices")
def matrix_matrix_mult(m1, m2):
if(len(m1[0])==len(m2)):
result = zeros_matrix(len(m1), len(m2))
for i in range(len(m1)):
for j in range(len(m2[0])):
for k in range(len(m1)):
result[i][j] += m1[i][k] * m2[k][j]
return result
def L_U_Siedel(A, b, x_0, iterations = 20):
result = []
result.append(x_0) # append x_0
L = matrix_L(A)
U = matrix_U(A)
D = diagonial_matrix(A)
D_min_L = matrix_matrix_subtraction(D, L)
TEMP = matrix_inverse(D_min_L)
C = matrix_matrix_mult(TEMP, U)
if (norm(C)< 1):
for i in range(iterations):
tm1 = matrix_vector_mult(TEMP, b)
tm2 = matrix_vector_mult(C, result[i])
x_k1 = vector_vector_add(tm1,tm2)
result.append(x_k1)
for index in range(len(result[i])):
print("X%s = %s"%(index, result[i][index]))
else:
print ("Non - convergence")
def L_U_Jacobi(A, b, x_0, iterations = 20):
result = []
result.append(x_0) # append x_0
L = matrix_L(A)
U = matrix_U(A)
D = diagonial_matrix(A)
D_inverse = matrix_inverse(D)
TEMP = matrix_matrix_addition(L, U)
C = matrix_matrix_mult(D_inverse, TEMP)
if (norm(C)< 1):
for i in range(iterations):
tm1 = matrix_vector_mult(D_inverse, b)
tm2 = matrix_vector_mult(C, result[i])
x_k1 = vector_vector_add(tm1,tm2)
result.append(x_k1)
for index in range(len(result[i])):
print("X%s = %s"%(index, result[i][index]))
else:
print ("Non - convergence")
if __name__ == "__main__":
L_U_Jacobi([[10,1,2,-1],[3,-25,-2,1],[2,-1,-6,2],[2,-3,-0.5,-8]],[1,0,-4,3],[0,0,0,0],20)