-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.py
316 lines (242 loc) · 6.88 KB
/
element.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
import math
import threading
from abc import ABC, abstractmethod
__all__ = [
'Element', 'Expr', 'Op1Expr', 'Op2Expr',
'Algebra', 'Number',
'Neg', 'Add', 'Sub', 'Mul', 'Div'
]
class Element(ABC):
@staticmethod
def from_obj(obj):
if isinstance(obj, Element):
return obj
if isinstance(obj, (int, float)):
return Number(obj)
if isinstance(obj, str):
return Algebra(obj)
raise ValueError(f'Unsupported type: {type(obj)}')
@property
@abstractmethod
def value(self) -> float:
raise NotImplementedError()
def round(self) -> int:
val = self.value
return math.ceil(val) if val % 1 >= 0.5 else math.floor(val)
def floor(self) -> int:
return math.floor(self.value)
def ceil(self) -> int:
return math.ceil(self.value)
def __float__(self) -> float:
return self.value
def __int__(self) -> int:
return int(self.value)
def __repr__(self) -> str:
return f'<{self.__class__.__name__}>'
__str__ = __repr__
@abstractmethod
def eq(self, other) -> bool:
raise NotImplementedError()
def veq(self, other) -> bool:
return (isinstance(other, self.__class__) and self.eq(other)) or self.value == other.value
def lt(self, other) -> bool:
return self.value < other.value
def __eq__(self, other) -> bool:
return isinstance(other, self.__class__) and self.eq(other)
def __ne__(self, other) -> bool:
return not isinstance(other, self.__class__) or not self.eq(other)
def __lt__(self, other) -> bool:
return self.lt(other)
def __le__(self, other) -> bool:
return self.veq(other) or self.lt(other)
def __gt__(self, other) -> bool:
return not self.veq(other) and not self.lt(other)
def __ge__(self, other) -> bool:
return not self.lt(other)
def __neg__(self):
return Neg(self)
def __add__(self, other):
return Add(self, other)
def __sub__(self, other):
return Sub(self, other)
def __mul__(self, other):
return Mul(self, other)
def __div__(self, other):
return Div(self, other)
def calc(self):
return self
class Algebra(Element):
ATTR_NAME = '__equation_algebra_values'
def __init__(self, name: str):
assert Algebra.check_name(name)
self._name = name
@staticmethod
def check_name(name: str) -> bool:
num = False
for c in name:
if '0' <= c and c <= '9':
if not num:
num = True
continue
if num:
return False
if (c < 'a' or 'z' < c) and (c < 'A' or 'Z' < c):
return False
return True
@property
def name(self) -> str:
return self._name
@property
def value(self) -> float:
curthr = threading.current_thread()
values = getattr(curthr, Algebra.ATTR_NAME, None)
if values is None:
raise RuntimeError('Not in an algebra context')
v = values.get(self.name, None)
if v is None:
raise RuntimeError('Undefined algebraic value')
return v
def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
return f'<Algebra name={str(self.name)}>'
def eq(self, other) -> bool:
return self.name == other.name
Algebra.A = Algebra('a')
Algebra.B = Algebra('b')
Algebra.C = Algebra('c')
Algebra.D = Algebra('d')
Algebra.W = Algebra('w')
Algebra.X = Algebra('x')
Algebra.Y = Algebra('y')
Algebra.Z = Algebra('z')
class Number(Element):
def __new__(cls, n):
return super().__new__(cls)
def __init__(self, n):
self.__v = float(n)
@property
def value(self) -> float:
return self.__v
def __str__(self) -> str:
return str(self.__v)
def __repr__(self) -> str:
return f'<Number {str(self.__v)}>'
def eq(self, other) -> bool:
return self.__v == other.__v
def lt(self, other) -> bool:
return self.__v < other.__v
Number.ZERO = Number(0)
Number.ONE = Number(1)
class Expr(Element):
pass
class Op1Expr(Expr):
def __init__(self, a: Element):
self.__a = a if isinstance(a, Element) else Element.from_obj(a)
@property
def op1(self):
return self.__a
def __repr__(self) -> str:
return f'<{self.__class__.__name__} {repr(self.__a)}>'
def calc(self):
a = self.__a.calc()
obj = self.__class__(a)
if isinstance(a, Number):
return Number(obj.value)
return obj
class Op2Expr(Expr):
def __init__(self, a: Element, b: Element):
self.__a = a if isinstance(a, Element) else Element.from_obj(a)
self.__b = b if isinstance(b, Element) else Element.from_obj(b)
@property
def op1(self):
return self.__a
@property
def op2(self):
return self.__b
def __repr__(self) -> str:
return f'<{self.__class__.__name__} {repr(self.__a)}, {repr(self.__b)}>'
def eq(self, other) -> bool:
return self.__a == other.__a and self.__b == other.__b
def calc(self):
a, b = self.__a.calc(), self.__b.calc()
obj = self.__class__(a, b)
if isinstance(a, Number) and isinstance(b, Number):
return Number(obj.value)
return obj
class Neg(Op1Expr):
def __new__(cls, a: Element):
return super().__new__(cls)
@property
def value(self) -> float:
return -self.op1.value
def __str__(self) -> str:
return f'-{str(self.op1)}'
def eq(self, other) -> bool:
return self.op1 == other.op1
def lt(self, other) -> bool:
return self.op1 > other.op1
def calc(self):
self = super().calc()
a = self.op1
if isinstance(a, Number):
return a.__class__(-a.value)
if isinstance(a, Neg):
return a.op1
return self
class Add(Op2Expr):
@property
def value(self) -> float:
return self.op1.value + self.op2.value
def __str__(self) -> str:
return f'({str(self.op1)} + {str(self.op2)})'
def eq(self, other) -> bool:
return (self.op1 == other.op1 and self.op2 == other.op2) or \
(self.op1 == other.op2 and self.op2 == other.op1)
def calc(self):
self = super().calc()
o1, o2 = self.op1, self.op2
if o1 == Neg(o2).calc():
return Number.ZERO
return self
class Sub(Op2Expr):
@property
def value(self) -> float:
return self.op1.value - self.op2.value
def __str__(self) -> str:
return f'({str(self.op1)} - {str(self.op2)})'
def eq(self, other) -> bool:
return (self.op1 == other.op1 and self.op2 == other.op2) or \
(self.op1 == -other.op2 and self.op2 == -other.op1)
def calc(self):
self = super().calc()
o1, o2 = self.op1, self.op2
if o1 == o2:
return Number.ZERO
return self
class Mul(Op2Expr):
@property
def value(self) -> float:
return self.op1.value * self.op2.value
def __str__(self) -> str:
return f'{str(self.op1)} * {str(self.op2)}'
def calc(self):
cls = self.__class__
self = super().calc()
if isinstance(self.op1, (Add, Sub)):
return self.op1.__class__(cls(self.op1.op1, self.op2).calc(), cls(self.op1.op2, self.op2).calc())
if isinstance(self.op2, (Add, Sub)):
return self.op2.__class__(cls(self.op1, self.op2.op1).calc(), cls(self.op1, self.op2.op2).calc())
return self
class Div(Op2Expr):
@property
def value(self) -> float:
return self.op1.value / self.op2.value
def __str__(self) -> str:
return f'{str(self.op1)} / {str(self.op2)}'
def calc(self):
cls = self.__class__
self = super().calc()
if isinstance(self.op1, (Add, Sub)):
return self.op1.__class__(cls(self.op1.op1, self.op2).calc(), cls(self.op1.op2, self.op2).calc())
return self