-
Notifications
You must be signed in to change notification settings - Fork 0
/
freenum.py
353 lines (290 loc) · 8.98 KB
/
freenum.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
"""
Free numbers provide a convenient way to form and solve linear systems.
>>> n1 = FreeFloat()
>>> n2 = FreeFloat()
>>> solve(n1 + n2)
>>> solve(n1 - 2 * n2, 12)
>>> n1.value
4.0
>>> n2.value
-4.0
>>> n3 = FreeComplex()
>>> solve(n3 * (1-2j), -3-4j)
>>> n3.refine().value
(1-2j)
"""
from collections import OrderedDict
from functools import total_ordering
from numbers import Real, Complex
__all__ = ['FreeComplex', 'FreeFloat', 'solve']
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
class UndefinedError(ValueError):
pass
class OverdefinedError(ValueError):
pass
class FreeComplex:
"""
Base class for free numbers.
Never instantiated. Calling constructor returns sum of two
FreeFloat's.
"""
__slots__ = []
def __new__(cls):
if cls is FreeComplex:
return FreeFloat() + FreeFloat() * 1j
return super().__new__(cls)
def __bool__(self):
return not self.defined or self.value != 0
def __add__(self, other):
return SumComplex(nums=(self, other), weights=(1, 1))
__radd__ = __add__
def __sub__(self, other):
return SumComplex(nums=(self, other), weights=(1, -1))
def __rsub__(self, other):
return SumComplex(nums=(self, other), weights=(-1, 1))
def __mul__(self, other):
if isinstance(other, FreeComplex):
if not other.defined:
return other * self.value
other = other.value
elif isinstance(other, Real):
other = float(other)
elif isinstance(other, Complex):
other = complex(other)
return SumComplex(nums=(self,), weights=(other,))
__rmul__ = __mul__
def __truediv__(self, other):
return self * (1.0 / other)
def __rtruediv__(self, other):
return other / self.value
def __neg__(self):
return SumComplex(nums=(self,), weights=(-1,))
def __pos__(self):
return self
class FreeFloat(FreeComplex):
__slots__ = ['_value']
def __init__(self):
self._value = None
@property
def value(self):
if self._value is None:
raise UndefinedError
return self._value
@property
def defined(self):
return self._value is not None
def define(self, value):
if self._value is not None:
raise OverdefinedError
if not isinstance(value, Real):
raise TypeError(value)
logger.info('defined something')
self._value = float(value)
@property
def real(self):
return self
@property
def imag(self):
return 0.0
class SumComplex(FreeComplex):
__slots__ = ['nums', 'weights', 'absolute']
def __new__(cls, *, nums=(), weights=(), absolute=0.0):
self = super().__new__(cls)
self.nums = []
self.weights = []
self.absolute = 0.0
return self
def __init__(self, *, nums=(), weights=(), absolute=0.0):
if (nums, weights, absolute) == ((), (), 0.0):
return
self.nums.extend(nums)
self.weights.extend(weights)
self.absolute += absolute
self.refine()
@classmethod
def normalize_nums_weights(cls, nums, weights, absolute):
the_nums = {}
the_weights = {}
for num, weight in cls.flatten_nums_weights(nums, weights):
if isinstance(num, (float, complex)):
absolute += num * weight
continue;
assert not isinstance(num, SumComplex)
assert isinstance(num, FreeFloat)
num_id = id(num)
if num_id not in the_nums:
the_nums[num_id] = num
the_weights[num_id] = weight
else:
the_weights[num_id] += weight
nums = []; weights = [];
for num_id in the_nums:
num = the_nums[num_id]
weight = the_weights[num_id]
if not weight:
continue;
nums.append(num)
weights.append(weight)
return nums, weights, absolute
@classmethod
def flatten_nums_weights(cls, nums, weights):
nums = list(nums); weights = list(weights)
if len(nums) != len(weights):
raise ValueError
for num, weight in zip(nums, weights):
if isinstance(weight, Real):
weight = float(weight)
elif isinstance(weight, Complex):
weight = complex(weight)
else:
raise TypeError(weight)
if weight == 0:
continue
if isinstance(num, Real):
yield float(num), weight
continue;
elif isinstance(num, Complex):
yield complex(num), weight
continue;
if not isinstance(num, FreeComplex):
raise TypeError(num)
if num.defined:
yield num.value, weight
continue;
if isinstance(num, SumComplex):
for subnum, subweight in num:
yield subnum, subweight * weight
continue;
yield num, weight
def __iter__(self):
yield from self.flatten_nums_weights(self.nums, self.weights)
yield self.absolute, 1.0
def refine(self):
self.nums, self.weights, self.absolute = self.normalize_nums_weights(
self.nums, self.weights, self.absolute)
return self
@property
def defined(self):
return not self.nums
def define(self, value):
raise TypeError("Can't define {!r} directly".format(self))
@property
def value(self):
if self.nums:
raise UndefinedError
return self.absolute
@property
def real(self):
return type(self)(
nums=self.nums,
weights=(weight.real for weight in self.weights),
absolute=self.absolute.real)
@property
def imag(self):
return type(self)(
nums=self.nums,
weights=(weight.imag for weight in self.weights),
absolute=self.absolute.imag)
def get_weight(self, num):
nums = self.nums
try:
index = nums.index(num)
except ValueError:
return 0.0
return self.weights[index]
def __repr__(self):
return (
'{self.__class__.__name__}(\n'
'\tnums={self.nums!r},\n'
'\tweights={self.weights!r},\n'
'\tabsolute={self.absolute} )'
.format(self=self) )
class Equation:
__slots__ = ['zero']
zeros = []
_defined_counter = 0
def __init__(self, lhs, rhs=0):
self.zero = zero = lhs - rhs
if not isinstance(zero, SumComplex):
raise TypeError(lhs, rhs)
def __bool__(self):
"""
Return True if self.zero is, well, zero.
"""
return self.zero.defined and self.zero.value == 0.0
def solve(self):
self.add_zero(self.zero)
@classmethod
def add_zero(cls, zero):
zero, imag = zero.real, zero.imag
if cls.trivial_add(zero):
return
zero = cls.substitute(zero)
if cls.trivial_add(zero):
return
zero /= zero.weights[0]
cls.zeros.append(zero)
cls.back_substitute()
if imag:
cls.add_zero(imag)
@classmethod
def trivial_add(cls, zero):
if zero.defined:
if zero.value:
raise OverdefinedError("Inconsistent equation")
else:
raise OverdefinedError("Redundant equation")
if len(zero.nums) == 1:
num, = zero.nums
weight, = zero.weights
num.define(- zero.absolute / weight)
zero.refine()
cls.refine()
return True
return False
@classmethod
def substitute(cls, zero):
zero.refine()
for z in cls.zeros:
num, *nums = z.nums
weight = zero.get_weight(num)
if not weight:
continue
zero -= weight * z
return zero
@classmethod
def back_substitute(cls):
zeros = []
trivial_zeros = []
zero = cls.zeros[-1]
num, *nums = zero.nums
needs_refine = False
for z in cls.zeros[:-1]:
weight = z.get_weight(num)
if weight:
z = z - weight * zero
if len(z.nums) > 1:
zeros.append(z)
else:
trivial_zeros.append(z)
zeros.append(zero)
cls.zeros = zeros
for z in trivial_zeros:
if not cls.trivial_add(z):
raise AssertionError
@classmethod
def refine(cls):
zeros = cls.zeros
if not zeros:
return
zeros = [zero.refine() for zero in zeros]
cls.zeros = []
for zero in zeros:
cls.add_zero(zero)
def solve(lhs, rhs=0):
Equation(lhs, rhs).solve()
if __name__ == '__main__':
import doctest
doctest.testmod()