-
Notifications
You must be signed in to change notification settings - Fork 0
/
kontsevich_graph_series.py
319 lines (277 loc) · 10.1 KB
/
kontsevich_graph_series.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
r"""
Kontsevich graph series
"""
from sage.kontsevich_graph_series.kontsevich_graph import KontsevichGraph
from sage.kontsevich_graph_series.kontsevich_graph_sum import KontsevichGraphSum
from sage.structure.element import AlgebraElement
from sage.categories.associative_algebras import AssociativeAlgebras
from sage.rings.ring import Algebra
from sage.structure.parent import Parent
from sage.structure.nonexact import Nonexact
from sage.rings.infinity import infinity
from sage.combinat.permutation import Permutations
def fixed_length_partitions(n,k,l=1):
'''n is the integer to partition, k is the length of partitions, l is the min partition element size'''
if k < 1:
raise StopIteration
if k == 1:
if n >= l:
yield (n,)
raise StopIteration
for i in range(l,n+1):
for result in fixed_length_partitions(n-i,k-1,i):
yield (i,)+result
class KontsevichGraphSeries(AlgebraElement):
def __init__(self, parent, terms, prec=infinity):
"""
Kontsevich graph series.
Formal power series with graph sums as coefficients.
EXAMPLES::
sage: K = KontsevichGraphSums(QQ)
sage: star_product_terms = {0 : K([(1, KontsevichGraph({'F' : {}, \
....: 'G' : {}}, ground_vertices=('F','G'), immutable=True))])}
sage: S.<h> = KontsevichGraphSeriesRng(K, star_product_terms = \
....: star_product_terms, default_prec = 0)
sage: S(star_product_terms)
1*(Kontsevich graph with 0 vertices on 2 ground vertices) + O(h^1)
"""
AlgebraElement.__init__(self, parent)
self._terms = terms
self._prec = prec
def __hash__(self):
"""
Return the hash value.
"""
return hash((self._terms, self._prec))
def prec(self):
"""
Precision of the series (possibly infinite).
"""
return self._prec
def common_prec(self, other):
"""
Common precision of the two series (the minimum of the two).
"""
return min(self.prec(), other.prec())
def __getitem__(self, n):
"""
Coefficient of h^n in the series, where h is the formal variable.
"""
return self._terms[n] if n in self._terms \
else self.parent()._base_module(0)
def __setitem__(self, n, value):
"""
Assign coefficient of h^n in the series, where h is the formal variable.
"""
self._terms[n] = value
def __delitem__(self, n):
"""
Delete h^n term, where h is the formal variable.
"""
del self._terms[n]
def __contains__(self, n):
"""
Test if coefficient of h^n in the series, where h is the formal
variable, has been assigned.
"""
return n in self._terms
def __len__(self):
"""
Number of assigned coefficients.
"""
return len(self._terms)
def __iter__(self):
"""
Iterator over the numbers n such that the coefficient of h^n has
been assigned.
"""
return iter(self._terms)
def orders(self):
"""
Return the iterator from :meth:`.__iter__` as a list.
Used in :meth:`.reduce` for example.
"""
return self._terms.keys()
def degree(self):
"""
Maximum number n such that the coefficient of h^n has been assigned.
"""
return max(self)
def reduce(self):
"""
Reduce all the coefficients (graph sums).
"""
for n in self.orders():
self[n].reduce()
if self[n] == 0:
del self[n]
def __eq__(self, other):
"""
Test for equality.
"""
self.reduce()
other.reduce()
prec = self.common_prec(other)
relevant_orders = lambda s: map(lambda k: k <= prec, s)
if not set(relevant_orders(self)) == set(relevant_orders(other)):
return False
for n in relevant_orders(self):
if self[n] != other[n]:
return False
return True
def __ne__(self, other):
"""
Test for unequality, using :meth:`.__eq__`.
"""
return not self.__eq__(other)
def _repr_(self):
"""
Representation of the series as a string.
"""
self.reduce()
result = ''
if 0 in self:
result += str(self[0])
if len(filter(lambda k: k <= self.prec(), self)) > 1:
result += ' + '
result += ' + '.join('(%s)*%s^%d' % \
(self[n], self.parent()._generator, n) \
for n in self if n > 0 and n <= self.prec())
if not self._prec is infinity:
if len(self) > 0:
result += ' + '
result += 'O(%s^%d)' % (self.parent()._generator, self.prec() + 1)
return result
def _add_(self, other):
"""
Add two series.
"""
prec = self.common_prec(other)
relevant_orders = lambda s: filter(lambda k: k <= prec, s)
sum_keys = set(relevant_orders(self)) | set(relevant_orders(other))
sum_terms = {n: self[n] + other[n] for n in sum_keys}
return self.parent()(sum_terms, prec=prec)
def _rmul_(self, other):
"""
Scalar multiplication.
"""
if other in self.parent().base_ring():
return self.parent()({n : other*self[n] for n in self},
prec=self.prec())
def _mul_(self, other):
"""
Star product.
"""
return self.parent()._star_product_series.subs(self, other)
def subs(self, *args):
"""
Substitute series into the ground vertices of this series.
"""
prec = min(series.prec() for series in args)
N = self.parent().default_prec()
subs_terms = {}
for n in range(0, min(N, prec) + 1):
subs_terms[n] = 0
for y in fixed_length_partitions(n, len(args) + 1, 0):
for x in Permutations(y):
k = x[0]
x = x[1:]
subs_terms[n] += self[k].subs(*(args[l][t] for (l,t) in \
enumerate(x)))
return self.parent()(subs_terms, prec=prec)
def inverse(self):
"""
The formal power series inverse of this series.
Only support one ground vertex, for now.
"""
inverse_terms = {0 : self[0]}
for n in range(1, self.prec() + 1):
inverse_terms[n] = 0
for k in range(0,n):
inverse_terms[n] -= inverse_terms[k].subs(self[n-k])
return self.parent()(inverse_terms, prec=self.prec())
def gauge_transform(self, gauge):
"""
Return the gauge-transformed series.
Only support series with two ground vertices, i.e. star product series.
"""
inverse = gauge.inverse()
ground_vertices = list(self[0])[0][1].ground_vertices()
assert len(ground_vertices) == 2
ground_graph = lambda v: self.parent()({0 : self.parent().base_module()([(1,
KontsevichGraph({v : {}},
ground_vertices=tuple(v),
immutable=True))])},
prec=self.prec())
return inverse.subs(self.subs(*[gauge.subs(ground_graph(v))
for v in ground_vertices]))
class KontsevichGraphSeriesRng(Algebra, Nonexact):
Element = KontsevichGraphSeries
def __init__(self, base_module, names=None, name=None,
star_product_terms={}, default_prec=2):
"""
Kontsevich graph series rng (ring without identity).
EXAMPLES::
sage: K = KontsevichGraphSums(QQ)
sage: star_product_terms = {0 : K([(1, KontsevichGraph({'F' : {}, \
....: 'G' : {}}, ground_vertices=('F','G'), immutable=True))])}
sage: S.<h> = KontsevichGraphSeriesRng(K, star_product_terms = \
....: star_product_terms, default_prec = 0)
"""
Parent.__init__(self, base_module.base_ring(),
category=AssociativeAlgebras(base_module.base_ring()))
Nonexact.__init__(self, default_prec)
self._base_module = base_module
if name:
self._generator = name
elif names:
self._generator = names[0]
else:
raise ValueError('Must provide a name for the generator')
self._star_product_series = {}
self._star_product_series = self.element_class(self, star_product_terms,
prec=default_prec)
def base_module(self):
return self._base_module
def star_product_series(self):
return self._star_product_series
def _repr_(self):
"""
Representation of the rng as a string.
"""
return "Kontsevich graph series rng in %s over %s" % (self._generator,
self._base_module) + \
", with star product %s" % self._star_product_series
def _element_constructor_(self, terms, prec=None):
"""
Make a KontsevichGraphSeries in ``self`` from ``terms``.
"""
if isinstance(terms, KontsevichGraph):
terms = {0 : self.base_module()([(1, terms)])}
if isinstance(terms, KontsevichGraphSum):
terms = {0 : terms}
if isinstance(terms, KontsevichGraphSeries):
terms = terms._terms
prec = terms._prec
if prec is None:
prec = self.default_prec()
return self.element_class(self, terms, prec=prec)
# The following three methods make the generator notation
# S.<h> = KontsevichGraphSeriesRng(...) work.
def ngens(self):
"""
The number of generators (that is, 1).
"""
return 1
def gen(self, i=0):
"""
The ith generator.
"""
if i != 0:
raise ValueError('There is only one generator')
return self._generator
def gens(self):
"""
The generators.
"""
return (self._generator,)