Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
181 views
unlisted
ubuntu2004
1
r"""
2
Mixcellaneous functions
3
"""
4
from sage.rings.integer_ring import ZZ
5
from sage.rings.rational_field import QQ
6
from sage.combinat.subset import Subsets
7
from sage.combinat.combination import Combinations
8
9
10
ENABLE_DPRINT = False
11
ENABLE_DSAVE = False
12
13
A_list = [ZZ(6 * n).factorial() / (ZZ(3 * n).factorial() * ZZ(2 * n).factorial())
14
for n in range(100)]
15
B_list = [ZZ(6 * n + 1).factorial() / ((6 * n - 1) * ZZ(3 * n).factorial() * ZZ(2 * n).factorial())
16
for n in range(100)]
17
18
19
def get_memory_usage():
20
"""
21
Return the memory usage of the current process in megabytes.
22
23
This function was part of sage.misc.getusage but the module was
24
removed in sage 9.5
25
26
OUTPUT: a float representing the number of megabytes used.
27
28
EXAMPLES::
29
30
sage: from admcycles.DR.utils import get_memory_usage
31
sage: t = get_memory_usage(); t # random
32
873.98046875
33
sage: type(t)
34
<... 'float'>
35
"""
36
import psutil
37
return psutil.Process().memory_info().vms / float(1048576)
38
39
40
def dprint(string, *args):
41
if ENABLE_DPRINT:
42
print(string % args)
43
44
45
def dsave(string, *args):
46
if ENABLE_DSAVE:
47
from sage.misc.persist import save
48
save(0, string % args)
49
50
51
def aut(L):
52
"""
53
Return the cardinality of the automorphism group of the list ``L``.
54
55
EXAMPLES::
56
57
sage: from admcycles.DR.utils import aut
58
sage: aut([])
59
1
60
sage: aut([4,1,3,2])
61
1
62
sage: aut([4,5,6,5,4,4,6])
63
24
64
"""
65
if not L:
66
return ZZ.one()
67
L.sort()
68
total = ZZ.one()
69
n = 1
70
last = L[0]
71
for l in L[1:]:
72
if l == last:
73
n += 1
74
total *= n
75
else:
76
n = 1
77
last = l
78
return total
79
80
81
def remove_duplicates(L):
82
"""
83
Remove duplicate elements in a list ``L``.
84
85
One cannot use ``set(L)`` because the elements of ``L`` are not hashable.
86
87
INPUT:
88
89
- ``L`` -- a list
90
91
OUTPUT:
92
93
a list
94
95
EXAMPLES::
96
97
sage: from admcycles.DR.utils import remove_duplicates
98
sage: remove_duplicates([4,7,6,4,3,3,4,2,2,1])
99
[1, 2, 3, 4, 6, 7]
100
"""
101
if not L:
102
return L
103
L.sort()
104
LL = [L[0]]
105
for i, Li in enumerate(L[1:]):
106
if Li != L[i]:
107
LL.append(Li)
108
return LL
109
110
111
def subsequences(n, l, symm):
112
"""
113
Return all subsequences of length ``l`` of ``n`` points with symmetry in the first ``symm`` points.
114
115
EXAMPLES::
116
117
sage: from admcycles.DR.utils import subsequences
118
sage: subsequences(5,2,2)
119
[[2, 3], [2, 4], [3, 4], [1, 2], [1, 3], [1, 4], [1, 1]]
120
"""
121
sym = max(symm, 1)
122
answer = []
123
for ones in range(min(l, sym) + 1):
124
for others in Subsets(tuple(range(2, n - sym + 2)), l - ones):
125
answer.append([1 for _ in range(ones)] + sorted(list(others)))
126
return answer
127
128
129
def interpolate(A, B, var='x'):
130
r"""
131
Univariate Lagrange interpolation over the rationals.
132
133
EXAMPLES::
134
135
sage: from admcycles.DR.utils import interpolate
136
sage: p = interpolate([1/2, -2, 3], [4/5, 2/3, -7/6])
137
sage: p(1/2)
138
4/5
139
sage: p(-2)
140
2/3
141
sage: p(3)
142
-7/6
143
144
TESTS::
145
146
sage: from admcycles.DR.utils import interpolate
147
sage: parent(interpolate([], []))
148
Univariate Polynomial Ring in x over Rational Field
149
sage: parent(interpolate([], [], 'r'))
150
Univariate Polynomial Ring in r over Rational Field
151
"""
152
if len(A) != len(B):
153
raise ValueError
154
return QQ[var].lagrange_polynomial(zip(A, B))
155
156
157
def simplify_sparse(vec):
158
"""
159
Collect coefficients in a list of pairs (index, coefficient).
160
161
This also sorts the indices and removes indices with zero coefficient.
162
163
EXAMPLES::
164
165
sage: from admcycles.DR.utils import simplify_sparse
166
sage: simplify_sparse([('b',6),('a',1),('c',2),('a',-1),('b',5)])
167
[['b', 11], ['c', 2]]
168
"""
169
vec.sort()
170
vec2 = []
171
last_index = None
172
for index, coeff in vec:
173
if index == last_index:
174
if vec2[-1][1] == -coeff:
175
vec2.pop()
176
last_index = None
177
else:
178
vec2[-1][1] += coeff
179
else:
180
vec2.append([index, coeff])
181
last_index = index
182
return vec2
183
184
185
def setparts_recur(symlist, progress):
186
if not symlist:
187
yield progress
188
return
189
for i in Combinations(symlist[1:]):
190
j = [symlist[0]] + i
191
if progress and j < progress[-1]:
192
continue
193
cur = 0
194
new_symlist = []
195
for k in range(len(symlist)):
196
if cur < len(j) and symlist[k] == j[cur]:
197
cur += 1
198
else:
199
new_symlist.append(symlist[k])
200
yield from setparts_recur(new_symlist, progress + [j])
201
202
203
def setparts_with_auts(symlist):
204
r"""
205
Iterate through the pairs ``(part, aut)`` where ``part`` is a set
206
partition of ``symlist`` and ``aut`` is its number of
207
automorphisms.
208
209
EXAMPLES::
210
211
sage: from admcycles.DR.utils import setparts_with_auts
212
213
sage: list(setparts_with_auts(symlist=[1]))
214
[([[1]], 1)]
215
sage: list(setparts_with_auts(symlist=[2]))
216
[([[2]], 1)]
217
sage: list(setparts_with_auts(symlist=[1, 1]))
218
[([[1], [1]], 1), ([[1, 1]], 1)]
219
sage: list(setparts_with_auts(symlist=[3]))
220
[([[3]], 1)]
221
sage: list(setparts_with_auts(symlist=[1, 2]))
222
[([[1], [2]], 1), ([[1, 2]], 1)]
223
sage: list(setparts_with_auts(symlist=[1, 1, 1]))
224
[([[1], [1], [1]], 1), ([[1], [1, 1]], 3), ([[1, 1, 1]], 1)]
225
"""
226
a = aut(symlist)
227
for i in setparts_recur(symlist, []):
228
b = aut(i)
229
for j in i:
230
b *= aut(j)
231
yield (i, a // b)
232
233