-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvartypes.py
300 lines (203 loc) · 8.95 KB
/
vartypes.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
import copy
import functools
import math
from abc import ABCMeta
from typing import List
from common import EvaluationException, operations
from matrix import MatrixTransformer, DynamicVector
def raise_exception(tpe, op):
raise EvaluationException('{} does not have operation {}'.format(tpe, op))
class Value(metaclass=ABCMeta):
__slots__ = ('type', 'value')
def __init__(self):
self.type = self.__class__.__name__
for op in operations:
if not hasattr(self, op):
setattr(self, op, functools.partial(raise_exception, tpe=self.type, op=op))
def __str__(self):
return str(self.value)
class VariableValue(Value):
def __init__(self, data):
super().__init__()
if isinstance(data, list):
self.value = data[0].value
else:
self.value = data
class NumberValue(Value):
def __init__(self, data):
super().__init__()
if isinstance(data, list):
self.value = float(data[0].value)
else:
self.value = data
def pos(self):
return NumberValue(self.value)
def neg(self):
return NumberValue(-self.value)
def add(self, other):
if isinstance(other, NumberValue):
return NumberValue(self.value + other.value)
else:
raise EvaluationException('Cannot add {} and {}'.format(self.type, other.type))
def sub(self, other):
if isinstance(other, NumberValue):
return NumberValue(self.value - other.value)
else:
raise EvaluationException('Cannot sub {} and {}'.format(self.type, other.type))
def mul(self, other):
if isinstance(other, NumberValue):
return NumberValue(self.value * other.value)
elif isinstance(other, MatrixValue):
return other.mul(self)
else:
raise EvaluationException('Cannot mul {} and {}'.format(self.type, other.type))
def div(self, other):
if isinstance(other, NumberValue):
return NumberValue(self.value / other.value)
else:
raise EvaluationException('Cannot div {} and {}'.format(self.type, other.type))
def mod(self, other):
if isinstance(other, NumberValue):
return NumberValue(self.value % other.value)
else:
raise EvaluationException('Cannot mod {} and {}'.format(self.type, other.type))
def pow(self, other):
if isinstance(other, NumberValue):
return NumberValue(self.value ** other.value)
else:
raise EvaluationException('Cannot pow {} and {}'.format(self.type, other.type))
def sqrt(self):
return NumberValue(math.sqrt(self.value))
def exp(self):
return NumberValue(math.exp(self.value))
def identity(self):
return MatrixValue([[1 if col is row else 0 for col in range(int(self.value))] for row in range(int(self.value))])
def zeroes(self, other):
return MatrixValue([[0 for _ in range(int(other.value))] for _ in range(int(self.value))])
class MatrixValue(Value):
def __init__(self, data):
super().__init__()
if isinstance(data[0], Value):
self.value = list(map(lambda t: t.value, data))
else:
self.value = data
self._rref_cache = None
def __str__(self):
return '[\n' + '\n'.join(['[' + ', '.join(map(lambda cell: str(round(cell, 5)), row)) + ']' for row in self.value]) + '\n]'
def sub(self, other):
if isinstance(other, MatrixValue):
if len(self.value) != len(other.value) or len(self.value[0]) != len(other.value[0]):
raise EvaluationException('Attempted to subtract two matrices of different dimensions')
return MatrixValue([[self.value[row][col] - other.value[row][col] for col in range(len(self.value[row]))] for row in range(len(self.value))])
raise EvaluationException('Cannot sub {} and {}'.format(self.type, other.type))
def mul(self, other):
if isinstance(other, NumberValue):
# Matrix * Number
return MatrixValue([[cell * other.value for cell in row] for row in self.value])
elif isinstance(other, MatrixValue):
# Matrix * Matrix
if len(self.value[0]) != len(other.value):
raise EvaluationException('Cannot multiply matrices of dimensions {} and {}'.format((len(self.value), len(self.value[0])), (len(other.value), len(other.value[0]))))
result = [[0 for _ in range(len(other.value[0]))] for _ in range(len(self.value))]
for i in range(len(self.value)):
for j in range(len(other.value[0])):
for k in range(len(other.value)):
result[i][j] += self.value[i][k] * other.value[k][j]
return MatrixValue(result)
else:
raise EvaluationException('Cannot mul {} and {}'.format(self.type, other.type))
def div(self, other):
if isinstance(other, NumberValue):
# Matrix / Number
return MatrixValue([[cell / other.value for cell in row] for row in self.value])
else:
raise EvaluationException('Cannot div {} and {}'.format(self.type, other.type))
def det(self):
return NumberValue(self._det(self.value))
@staticmethod
def _det(matrix: List[List[float]]) -> float:
if len(matrix) is 1:
return matrix[0][0]
cofactors = []
for col in range(len(matrix)):
cofactors.append(MatrixValue._det([matrix[row][0:col] + matrix[row][col + 1:] for row in range(1, len(matrix))]) * matrix[0][col] * (1 if col % 2 is 0 else -1))
return sum(cofactors)
def trans(self):
return MatrixValue(list(map(list, zip(*self.value))))
def cof(self):
cofactor_matrix = []
for row in range(len(self.value)):
cofactor_matrix.append([])
for col in range(len(self.value[row])):
minor = copy.deepcopy(self.value)
del minor[row]
for r in minor:
del r[col]
cofactor_matrix[row].append(self._det(minor) * (1 if (row + col) % 2 is 0 else -1))
return MatrixValue(cofactor_matrix)
def adj(self):
return self.cof().trans()
def inv(self):
det = self._det(self.value)
if det == 0:
raise EvaluationException('Cannot invert matrix with determinant of 0.')
multiplier = 1 / det
return MatrixValue([[cell * multiplier for cell in row] for row in self.adj().value])
def _rref(self):
if not self._rref_cache:
self._rref_cache = MatrixTransformer(copy.deepcopy(self.value)).rref()
return self._rref_cache
def rref(self):
return MatrixValue(self._rref()[0])
def trnsform(self):
return MatrixValue(self._rref()[1])
def solve(self, other):
return DynamicVectorValue(MatrixTransformer(copy.deepcopy(self.value)).rref(other.value[0])[2])
def ls(self, other):
return (self.trans().mul(self)).inv().mul(self.trans()).mul(other)
def norm(self):
return NumberValue(math.sqrt(sum([sum([col * col for col in row]) for row in self.value])))
def squeeze(self):
return MatrixValue([[cell for row in self.value for cell in row]])
def _col(self, col):
""" Isolates an individual column from the matrix """
return MatrixValue([[row[col]] for row in self.value])
def qr(self):
m = NumberValue(len(self.value))
n = NumberValue(len(self.value[0]))
Q = m.zeroes(m).value
R = n.zeroes(n).value
for j in range(n.value):
v = self._col(j)
for i in range(j):
R[i][j] = MatrixValue(Q)._col(i).trans().mul(self._col(j)).value[0][0]
v = v.squeeze().sub(MatrixValue(Q)._col(i).trans().mul(NumberValue(R[i][j])))
R[j][j] = v.norm().value
val = v.div(NumberValue(R[j][j])).squeeze().value[0]
for row in range(len(Q)):
Q[row][j] = val[row]
return TupleValue([MatrixValue(Q), MatrixValue(R)])
class MatrixRowValue(Value):
def __init__(self, data):
super().__init__()
if isinstance(data[0], Value):
self.value = list(map(lambda t: t.value, data))
else:
self.value = data
class TupleValue(Value):
def __init__(self, args: List):
super().__init__()
self.value = args
class DynamicVectorValue(Value):
def __init__(self, dvec: DynamicVector):
super().__init__()
self.value = dvec
def eval(self, *args):
dvec = copy.deepcopy(self.value)
ans = dvec.vectors.pop('const')
i = 0
for _, free_var in dvec.vectors.items():
row = [x * int(args[i].value) for x in free_var]
ans = [ans[i] + row[i] for i in range(len(ans))]
i += 1
return MatrixValue([ans])