-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrixes.py
413 lines (319 loc) · 9.59 KB
/
Matrixes.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#not throwing any Errors so it's risky to use
#using * and / is not clever cos there are not precise
#can take off deepcopy form Ees for memory performance
from copy import deepcopy
class RowMatrix:
def __init__(self, rows, columns):
self.rows = rows
self.columns = columns
self.matrix = []
for i in range(rows):
tmp = []
for j in range(columns):
tmp.append(0)
self.matrix.append(tmp)
def GetRows(self, rows):
for i in range(self.rows):
self.matix[i] = rows[i]
def GetInPut(self):
for i in range(self.rows):
self.matrix[i] = list(map(int, input().split()))
def __repr__(self):
result = ""
for i in range(self.rows-1):
result += str(self.matrix[i][0])
for j in range(1,self.columns):
result += " "+ str(self.matrix[i][j])
result += "\n"
for i in range(self.rows - 1,self.rows ):
result += str(self.matrix[i][0])
for j in range(1,self.columns):
result += " "+ str(self.matrix[i][j])
return(result)
class ColumnMatrix:
def __init__(self, rows, columns):
self.rows = rows
self.columns = columns
self.matrix = []
for i in range(columns):
tmp = []
for j in range(rows):
tmp.append(0)
self.matrix.append(tmp)
def GetColumns(self, columns):
for i in range(len(columns)):
self.matrix[i] = columns[i]
def GetOneColumn(slef, column , place):
place = place-1
self.matrix[place] = column
def GetInPut(self):
ColumnsArray = []
for i in range(self.columns):
ColumnsArray.append([])
for i in range(self.rows):
lineArray = list(map(int, input().split()))
for j in range(self.columns):
ColumnsArray[j].append(lineArray[j])
self.GetColumns(ColumnsArray)
def __repr__(self):
result = ""
for i in range(self.rows-1):
result += str(self.matrix[0][i])
for j in range(1,self.columns):
result += " " + str(self.matrix[j][i])
result += "\n"
for i in range(self.rows - 1, self.rows ):
result += str(self.matrix[0][i])
for j in range(1,self.columns):
result += " " + str(self.matrix[j][i])
return result
def ConRow2Column(rmatrix):
result = ColumnMatrix(rmatrix.rows, rmatrix.columns)
for i in range(rmatrix.rows):
for j in range(rmatrix.columns):
result.matrix[j][i] = rmatrix.matrix[i][j]
return result
def ConColumn2Row(cmatrix):
result = RowMatrix(cmatrix.rows, cmatrix.columns)
for i in range(cmatrix.rows):
for j in range(cmatrix.columns):
result.matrix[i][j] = cmatrix.matrix[j][i]
return result
def plus2matrix(first, second):
x = deepcopy(first)
y = deepcopy(second)
if isinstance(x, RowMatrix):
x = ConRow2Column(x)
if isinstance(y, RowMatrix):
y = ConRow2Column(y)
result = ColumnMatrix(x.rows, y.columns)
for i in range(x.rows):
for j in range(x.columns):
result.matrix[j][i] = x.matrix[j][i] + y.matrix[j][i]
return result
def matrix2Cmulty(cmatrix, c):
x = deepcopy(cmatrix)
if isinstance(x, RowMatrix):
x = ConRow2Column(x)
result = ColumnMatrix(x.rows, x.columns)
for i in range(x.rows):
for j in range(x.columns):
result.matrix[j][i] = x.matrix[j][i] * c
return result
def arr2cmul(arr , c):
result = []
for i in range(len(arr)):
result.append(c*arr[i])
return result
def arr2plus(first, second):
result = []
for i in range(len(first)):
result.append(first[i] + second[i])
return result
def multiplication(first, second):
x = deepcopy(first)
y = deepcopy(second)
if isinstance(x, RowMatrix):
x = ConRow2Column(x)
if isinstance(y, RowMatrix):
y = ConRow2Column(y)
#Once more errors are not cheking in code
result = ColumnMatrix(x.rows, y.columns)
for i in range(y.columns):
tmp = arr2cmul(x.matrix[0], y.matrix[i][0])
for j in range(1, y.rows):
tmp = arr2plus(tmp , arr2cmul(x.matrix[j], y.matrix[i][j]))
result.matrix[i] = tmp
return result
def transpose(x):
tmp = []
if isinstance(x, RowMatrix):
tmp = ColumnMatrix(x.columns, x.rows)
for i in range(x.rows):
tmp.matrix[i] = x.matrix[i]
if isinstance(x, ColumnMatrix):
tmp = RowMatrix(x.columns, x.rows)
for i in range(x.columns):
tmp.matrix[i] = x.matrix[i]
if isinstance(tmp, RowMatrix):
tmp = ConRow2Column(tmp)
return tmp
class Ees:
def __init__(self, kind):
self.kind = kind
class ChangeRow(Ees):
def __init__(self, kind, first , second):
Ees.__init__(self, kind)
self.first = first -1
self.second = second -1
self.kind = "C"
def action(self, x):
tmp = deepcopy(x)
if isinstance(x, ColumnMatrix):
tmp = ConColumn2Row(tmp)
change1 = tmp.matrix[self.first]
change2 = tmp.matrix[self.second]
tmp.matrix[self.second] = change1
tmp.matrix[self.first] = change2
return tmp
def __repr__(self):
result = self.kind + str(self.first+1) + "," + str(self.second+1)
return result
class MultyRow(Ees):
def __init__(self, kind , row , c ):
Ees.__init__(self, kind)
self.row = row -1
self.c = c
def action(self, x):
tmp = deepcopy(x)
if isinstance(x, ColumnMatrix):
tmp = ConColumn2Row(tmp)
tmp.matrix[self.row] = arr2cmul(x.matrix[self.row] , self.c)
return tmp
def __repr__(self):
result = self.kind + str(self.row+1) + "*" + str(self.c)
return result
class PlusRow(Ees):
def __init__(self,kind, row1 , row2 , row2c):
Ees.__init__(self,kind)
self.row1 = row1 -1
self.row2 = row2 -1
self.row2c = row2c
self.kind = "P"
def action(self ,x):
tmp = deepcopy(x)
if isinstance(x, ColumnMatrix):
tmp = ConColumn2Row(tmp)
addrow = arr2cmul(x.matrix[self.row2], self.row2c)
tmp.matrix[self.row1] = arr2plus(x.matrix[self.row1], addrow)
return tmp
def __repr__(self):
result = self.kind + str(self.row1+1) + "," + str(self.row2+1) + "*" + str(self.row2c)
return result
class rref:
def __init__(self,matrix):
tmp = deepcopy(matrix)
if isinstance(tmp, ColumnMatrix):
tmp = ConColumn2Row(tmp)
self.matrix = tmp
self.rref = deepcopy(tmp)
self.seq = []
def procces(self):
currentrow = 0
for i in range(self.rref.columns):
if currentrow == self.rref.rows :
break
for j in range(currentrow,self.rref.rows):
if self.rref.matrix[j][i] != 0 :
if j <= currentrow :
for k in range(j):
if self.rref.matrix[k][i] != 0 :
t = (self.rref.matrix[k][i] / self.rref.matrix[j][i])* - 1
if t != 0 :
tt = PlusRow("P",k+1,j+1,t)
self.seq.append(tt)
self.rref = tt.action(self.rref)
for k in range(j+1,self.rref.rows):
if self.rref.matrix[k][i] != 0 :
t = (self.rref.matrix[k][i] / self.rref.matrix[j][i])* - 1
if t != 0 :
tt = PlusRow("P",k+1,j+1,t)
self.rref = tt.action(self.rref)
self.seq.append(tt)
if 1/self.rref.matrix[j][i] != 1 :
uu = MultyRow("M",currentrow+1, 1/self.rref.matrix[j][i])
self.seq.append(uu)
self.rref = uu.action(self.rref)
currentrow += 1
else :
c = ChangeRow("c",j+1, currentrow+1)
self.seq.append(c)
self.rref = c.action(self.rref)
for k in range(currentrow):
if self.rref.matrix[k][i] != 0 :
t = (self.rref.matrix[k][i] / self.rref.matrix[currentrow][i])* - 1
if t != 0 :
tt = PlusRow("P",k+1,currentrow+1,t)
self.seq.append(tt)
self.rref = tt.action(self.rref)
for k in range(currentrow+1,self.rref.rows):
if self.rref.matrix[k][i] != 0 :
t = (self.rref.matrix[k][i] / self.rref.matrix[currentrow][i])* - 1
if t != 0 :
tt = PlusRow("P",k+1,currentrow+1,t)
self.seq.append(tt)
self.rref = tt.action(self.rref)
if 1/self.rref.matrix[currentrow][i] != 1:
uu = MultyRow("M",currentrow+1, 1/self.rref.matrix[currentrow][i])
self.seq.append(uu)
self.rref = uu.action(self.rref)
currentrow += 1
return self.rref
class elimination:
def __init__(self, matrix):
tmp = deepcopy(matrix)
if isinstance(tmp, ColumnMatrix):
tmp = ConColumn2Row(tmp)
self.matrix = tmp
self.eliminate = deepcopy(tmp)
self.seq = []
self.can = True
self.done = False
def procces(self):
times = min(self.eliminate.rows, self.eliminate.columns)
for t in range(times):
if self.eliminate.matrix[t][t] == 0 :
chk = True
for k in range(t+1, self.eliminate.rows):
if self.eliminate.matrix[k][t] != 0 :
chk = False
tes = ChangeRow("C",t+1,k+1)
self.seq.append(tes)
self.eliminate = tes.action(self.eliminate)
if chk :
self.can = False
break
# adding else
for k in range(t+1, self.eliminate.rows):
if self.eliminate.matrix[k][t] != 0 :
cel = (self.eliminate.matrix[k][t] / self.eliminate.matrix[t][t]) * -1
if cel != 0 :
el = PlusRow("P",k+1,t+1, cel)
self.seq.append(el)
self.eliminate = el.action(self.eliminate)
else:
for k in range(t+1, self.eliminate.rows):
if self.eliminate.matrix[k][t] != 0 :
cel = (self.eliminate.matrix[k][t] / self.eliminate.matrix[t][t]) * -1
if cel != 0 :
el = PlusRow("P",k+1,t+1, cel)
self.seq.append(el)
self.eliminate = el.action(self.eliminate)
if self.can :
return(self.eliminate)
print(":(")
return ":("
def solveaxeqb(a,b):
tmpa = deepcopy(a)
tmpb = deepcopy(b)
if isinstance(tmpa , ColumnMatrix):
tmpa = ConColumn2Row(tmpa)
if isinstance(tmpb , ColumnMatrix):
tmpa = ConColumn2Row(tmpb)
eact = elimination(tmpa)
eact.procces()
if eact.can == False :
return "Can't be done"
refa = rref(tmpa)
refa.procces()
print(refa.seq)
print(refa.rref)
print("--------------------")
#Actions
for i in range(len(refa.seq)):
tmpb = refa.seq[i].action(tmpb)
for i in range(a.columns,tmpb.rows):
if tmpb.matrix[i][0] != 0:
#print ("Can't be done2")
return "Can't be done2"
print(tmpb)