-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdivars.py
307 lines (230 loc) · 7.21 KB
/
divars.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
from numbers import Real
from sympy import Add, Mul
from sys import modules
def mult_tuples(x, y):
ind = 0
ind0 = min(len(x), len(y))
while (ind < ind0) and (x[-ind-1] == y[ind]):
ind += 1
if ind == 0:
return x + y
else:
return x[:-ind] + y[ind:]
def chomp_empty(seq):
"""Return slice of sequence seq without trailing empty tuples."""
n = len(seq)
while (n > 0) and seq[n - 1] == ():
n -= 1
return seq[:n]
class Monomial:
def __init__(self, *args):
t = tuple(x if type(x) is tuple else (x,) for x in args)
self.vars = chomp_empty(t)
def __mul__(self, y):
if type(y) is Monomial:
t = tuple(map(mult_tuples, self.vars, y.vars))
ls, ly = len(self.vars), len(y.vars)
t += self.vars[ly:] if ls > ly else y.vars[ls:]
return Monomial(*t)
elif type(y) is Polynomial:
return (+self) * y
else:
return Polynomial({self: y} if y != 0 else {})
__rmul__ = __mul__
def __pos__(self):
return Polynomial({self: 1})
def __neg__(self):
return Polynomial({self: -1})
def __add__(self, y):
if type(y) is Monomial:
return Polynomial({self: 1, y: 1} if self != y else {self: 2})
else:
return y + self
def __sub__(self, y):
if type(y) is Monomial:
return Polynomial({self: 1, y: -1} if self != y else {})
else:
return self + (-y)
def conjugate(self):
return Monomial(*(tuple(reversed(ps)) for ps in self.vars))
def order(self):
return sum(map(len, self.vars))
def cmp(self, y):
ls, ly = self.order(), y.order()
if ls != ly:
return -1 if ls < ly else 1
for u, v in zip(self.vars, y.vars):
lu, lv = len(u), len(v)
if (lu != lv):
return -1 if lu > lv else 1
for u, v in zip(self.vars, y.vars):
if u != v:
return -1 if u < v else 1
return 0
def __eq__(self, y):
return self.cmp(y) == 0
def __neq__(self, y):
return self.cmp(y) != 0
def __lt__(self, y):
return self.cmp(y) < 0
def __le__(self, y):
return self.cmp(y) <= 0
def __gt__(self, y):
return self.cmp(y) > 0
def __ge__(self, y):
return self.cmp(y) >= 0
def __hash__(self):
return self.vars.__hash__()
def __repr__(self):
if self.order() != 0:
return ' '.join(filter(None,
(' '.join((chr(ord('A') + n) + str(x)
for x in p))
for n, p in enumerate(self.vars))))
else:
return 'Id'
def diop(site, x=1):
"""Make dichotomic variable from site and input number."""
t = ((),) * site + ((x,),)
return Monomial(*t)
def divar(name):
"""Make dichotomic variable from (string) name."""
name = name.upper()
if name == 'ID':
return Monomial()
else:
return diop(ord(name[0]) - ord('A'), int(name[1:]))
def divars(names):
"""Return a tuple of the dichotomic variables in the string names."""
return tuple(map(divar, names.split(' ')))
def bind_divars(names, module=modules['__main__']):
"""Create global variable bindings for dichotomic variables.
This creates the dichotomic variables in names (which should be a
space-separated string) and adds each name and corresponding
dichotomic variable in the symbol table of module (the top level
module named '__main__' by default)."""
bindings = vars(module)
for name in names.split(' '):
bindings[name] = divar(name)
class Polynomial(dict):
def apply(self, f):
for k in list(self.keys()):
new_val = f(self[k])
if new_val != 0:
self[k] = new_val
else:
del self[k]
def __iadd__(self, y):
if type(y) is Polynomial:
for k, v in y.items():
if k in self:
sk = self[k] + v
if sk != 0:
self[k] = sk
else:
del self[k]
else:
self[k] = v
else:
if y not in self:
self[y] = 1
else:
if self[y] != -1:
self[y] += 1
else:
del self[y]
return self
def __add__(self, y):
p = Polynomial(self.copy())
p += y
return p
def __neg__(self):
p = Polynomial()
for k, v in self.items():
p[k] = -v
return p
def __sub__(self, y):
return self + (-y)
def __mul__(self, y):
if type(y) is Polynomial:
p = Polynomial()
for ks, vs in self.items():
for ky, vy in y.items():
k, v = ks * ky, vs * vy
if k not in p:
p[k] = v
elif p[k] != -v:
p[k] += v
else:
del p[k]
return p
elif type(y) is Monomial:
return self * (+y)
elif y != 0:
p = Polynomial(self.copy())
for k in self.keys():
p[k] *= y
return p
else:
return Polynomial({})
__rmul__ = __mul__
def __pow__(self, k):
n = self
p = n if k % 2 == 1 else Monomial()
k >>= 1
while k > 0:
n = n * n
if k % 2 == 1:
p = p * n
k >>= 1
return p
def conjugate(self):
return Polynomial({m.conjugate(): cf.conjugate()
for m, cf in self.items()})
def __repr__(self):
keys = sorted(self.keys())
if len(keys) == 0:
return '0'
k, *keys = keys
x = self[k]
if x == 1:
rep = ''
elif x == -1:
rep = '-'
elif type(x) is Add:
rep = '(' + str(x) + ') '
else:
rep = str(x) + ' '
rep += str(k)
for k in keys:
x = self[k]
if x == 1:
rep += ' +'
elif x == -1:
rep += ' -'
elif isinstance(x, Real):
if x >= 0:
rep += ' + ' + str(x)
else:
rep += ' - ' + str(-x)
elif type(x) is Add:
rep += ' + (' + str(x) + ')'
elif type(x) is Mul and x.args[0].is_real \
and x.args[0].is_number and x.args[0] < 0:
rep += ' - ' + str(-x)
else:
rep += ' + ' + str(x)
rep += ' ' + str(k)
return rep
def conj(x):
"""Return conjugate of x."""
return x.conjugate()
def conjx(x, *args):
"""Return the conjugate of x multiplied by arguments *args."""
result = x.conjugate()
for y in args:
result *= y
return result
def sqr(x):
"""Return the conjugate of x multiplied by x."""
return x.conjugate() * x