forked from lazzarigioele/chlorocarfitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmatrix.py
282 lines (177 loc) · 6.91 KB
/
libmatrix.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
import copy
class ObjMatrix(object):
def __init__(self, rows = [[0]]):
self.r = len(rows)
self.c = len(rows[0])
# Validity check
if any([len(row) != self.c for row in rows[1:]]):
print("inconsistent row length")
self.rows = rows
def getString(self):
s='\n'.join([' '.join([str(item) for item in row]) for row in self.rows])
return s + '\n'
def getMax(self):
current_max = self.rows[0][0]
for i in range(self.r):
for j in range(self.c):
if self.rows[i][j] > current_max:
current_max = self.rows[i][j]
return current_max
def getMin(self):
current_min = self.rows[0][0]
for i in range(self.r):
for j in range(self.c):
if self.rows[i][j] < current_min:
current_min = self.rows[i][j]
return current_min
def isEqual(self, mat):
return (mat.rows == self.rows)
def transpose(self):
rows = [list(item) for item in zip(*self.rows)]
return ObjMatrix(rows)
def summate(self, mat):
if (self.r != mat.r or self.c != mat.c):
print("Trying to summate ObjMatrixes of varying rank!")
result = ObjMatrix.makeZero(self.r, self.c)
for i in range(self.r):
for j in range(self.c):
result.rows[i][j] = self.rows[i][j] + mat.rows[i][j]
return result
def subtract(self, mat):
if (self.r != mat.r or self.c != mat.c):
print("Trying to subtract ObjMatrixes of varying rank!")
result = ObjMatrix.makeZero(self.r, self.c)
for i in range(self.r):
for j in range(self.c):
result.rows[i][j] = self.rows[i][j] - mat.rows[i][j]
return result
def multiply(self, mat):
if (self.c != mat.r):
print("Matrices cannot be multipled!")
result = ObjMatrix.makeZero(self.r, mat.c)
for i in range(self.r):
for j in range(mat.c):
for k in range(mat.r):
result.rows[i][j] += self.rows[i][k] * mat.rows[k][j]
return result
def multiplyScalar(self, scalar):
listol = []
for i in range(self.r):
row = []
for j in range(self.c):
row.append(scalar * self.rows[i][j])
listol.append(row)
return ObjMatrix(listol)
def makeZero(r, c):
rows = [[0]*c for x in range(r)]
return ObjMatrix(rows)
def makeIdentity(r):
rows = [[0]*r for x in range(r)]
idx = 0
for row in rows:
row[idx] = 1
idx += 1
return ObjMatrix(rows)
def __minor(matrix, i):
minor = copy.deepcopy(matrix)
del minor[0] # Delete first row
for b in range(len(matrix)-1): # Delete column i
del minor[b][i]
return minor
def __det(rows):
if len(rows) == 1: # End of recursion
return rows[0][0]
else:
result = 0
for x in list(range(len(rows))):
result += rows[0][x] * (-1)**(2+x) * ObjMatrix.__det(ObjMatrix.__minor(rows,x))
return result
def determinant(self):
if (self.r != self.c):
print("Can't calculate determinant if the matrix is non squared!")
return
rows = copy.deepcopy(self.rows)
return ObjMatrix.__det(rows)
def __inv(rows):
rows = [rows[i]+[int(i==j) for j in range(len(rows))] for i in range(len(rows))]
for i in range(len(rows)):
rows[i:] = sorted(rows[i:], key=lambda r: -abs(r[i]))
rows[i] = [rows[i][j]/rows[i][i] for j in range(len(rows)*2)]
rows = [[rows[j][k] if i==j else rows[j][k]-rows[i][k]*rows[j][i] for k in range(len(rows)*2)] for j in range(len(rows))]
return [rows[i][-len(rows):] for i in range(len(rows))]
def inverse(self):
if self.r != self.c:
print("Requested the inverse of non-squared matrix. Are you shure?")
rows = copy.deepcopy(self.rows)
return ObjMatrix(ObjMatrix.__inv(rows))
def extractColumns(self, indexes):
new = []
for i in range(self.r):
# iterate through columns
new_row = []
for j in range(self.c):
if j in indexes:
new_row.append(self.rows[i][j])
new.append(new_row)
return ObjMatrix(new)
def extractRows(self, indexes):
listol = []
for i in range(self.r):
if i in indexes:
listol.append(self.rows[i])
print("new: " + str(listol))
return ObjMatrix(listol)
def getArgMax(self): # (of the flattened array)
current_index = 0
current_max = self.rows[0][0]
for i in range(self.r):
for j in range(self.c):
if self.rows[i][j] > current_max:
current_max = self.rows[i][j]
current_index = j + self.c * i
return current_index
def getNormL1(self):
partials = [0 for i in range(self.c)]
for i in range(self.r):
for j in range(self.c):
partials[j] += abs(self.rows[i][j])
return max(partials)
if __name__ == "__main__":
# getString
a = ObjMatrix([[4, 6, 3], [7, 5, 2], [6, 1, 0]])
print(a.getString())
# transpose
b = a.transpose()
print(b.getString())
# makeIdentity
d = ObjMatrix.makeIdentity(5)
print(d.getString())
# makeZero
e = ObjMatrix.makeZero(5,3)
print(e.getString())
# multiply
o1 = ObjMatrix([[5,1] ,[2, 0]])
o2 = ObjMatrix([[2,1,0],[4,3,-1]])
o3 = o1.multiply(o2)
print(o3.getString())
# subtract
o1 = ObjMatrix([[5,1, -2] ,[0,4,1]])
o2 = ObjMatrix([[0,-4,15],[8,0,0]])
o3 = o1.subtract(o2)
print(o3.getString())
# determinant
data = ObjMatrix([[1, 2, 3], [-5, 0, 4], [2, 4, 6]])
data = ObjMatrix([[2]])
data = ObjMatrix([[3,-1,0], [-2,1,1], [4,5,-4]])
print(data.determinant())
# inverse
last = ObjMatrix([[3,0,2],[2,0,-2],[0,1,1]])
print(last.inverse().getString())
# getArgMax
am = ObjMatrix([[0, 1, 2, 3],
[4, 5, 6, 7],
[100, 9, 10, 11]])
print(am.getArgMax())
print(am.getString())
# extractColumns
print(am.extractColumns([1,3]).getString())