unlisted
ubuntu2004r"""1Mixcellaneous functions2"""3from sage.rings.integer_ring import ZZ4from sage.rings.rational_field import QQ5from sage.combinat.subset import Subsets6from sage.combinat.combination import Combinations789ENABLE_DPRINT = False10ENABLE_DSAVE = False1112A_list = [ZZ(6 * n).factorial() / (ZZ(3 * n).factorial() * ZZ(2 * n).factorial())13for n in range(100)]14B_list = [ZZ(6 * n + 1).factorial() / ((6 * n - 1) * ZZ(3 * n).factorial() * ZZ(2 * n).factorial())15for n in range(100)]161718def get_memory_usage():19"""20Return the memory usage of the current process in megabytes.2122This function was part of sage.misc.getusage but the module was23removed in sage 9.52425OUTPUT: a float representing the number of megabytes used.2627EXAMPLES::2829sage: from admcycles.DR.utils import get_memory_usage30sage: t = get_memory_usage(); t # random31873.9804687532sage: type(t)33<... 'float'>34"""35import psutil36return psutil.Process().memory_info().vms / float(1048576)373839def dprint(string, *args):40if ENABLE_DPRINT:41print(string % args)424344def dsave(string, *args):45if ENABLE_DSAVE:46from sage.misc.persist import save47save(0, string % args)484950def aut(L):51"""52Return the cardinality of the automorphism group of the list ``L``.5354EXAMPLES::5556sage: from admcycles.DR.utils import aut57sage: aut([])58159sage: aut([4,1,3,2])60161sage: aut([4,5,6,5,4,4,6])622463"""64if not L:65return ZZ.one()66L.sort()67total = ZZ.one()68n = 169last = L[0]70for l in L[1:]:71if l == last:72n += 173total *= n74else:75n = 176last = l77return total787980def remove_duplicates(L):81"""82Remove duplicate elements in a list ``L``.8384One cannot use ``set(L)`` because the elements of ``L`` are not hashable.8586INPUT:8788- ``L`` -- a list8990OUTPUT:9192a list9394EXAMPLES::9596sage: from admcycles.DR.utils import remove_duplicates97sage: remove_duplicates([4,7,6,4,3,3,4,2,2,1])98[1, 2, 3, 4, 6, 7]99"""100if not L:101return L102L.sort()103LL = [L[0]]104for i, Li in enumerate(L[1:]):105if Li != L[i]:106LL.append(Li)107return LL108109110def subsequences(n, l, symm):111"""112Return all subsequences of length ``l`` of ``n`` points with symmetry in the first ``symm`` points.113114EXAMPLES::115116sage: from admcycles.DR.utils import subsequences117sage: subsequences(5,2,2)118[[2, 3], [2, 4], [3, 4], [1, 2], [1, 3], [1, 4], [1, 1]]119"""120sym = max(symm, 1)121answer = []122for ones in range(min(l, sym) + 1):123for others in Subsets(tuple(range(2, n - sym + 2)), l - ones):124answer.append([1 for _ in range(ones)] + sorted(list(others)))125return answer126127128def interpolate(A, B, var='x'):129r"""130Univariate Lagrange interpolation over the rationals.131132EXAMPLES::133134sage: from admcycles.DR.utils import interpolate135sage: p = interpolate([1/2, -2, 3], [4/5, 2/3, -7/6])136sage: p(1/2)1374/5138sage: p(-2)1392/3140sage: p(3)141-7/6142143TESTS::144145sage: from admcycles.DR.utils import interpolate146sage: parent(interpolate([], []))147Univariate Polynomial Ring in x over Rational Field148sage: parent(interpolate([], [], 'r'))149Univariate Polynomial Ring in r over Rational Field150"""151if len(A) != len(B):152raise ValueError153return QQ[var].lagrange_polynomial(zip(A, B))154155156def simplify_sparse(vec):157"""158Collect coefficients in a list of pairs (index, coefficient).159160This also sorts the indices and removes indices with zero coefficient.161162EXAMPLES::163164sage: from admcycles.DR.utils import simplify_sparse165sage: simplify_sparse([('b',6),('a',1),('c',2),('a',-1),('b',5)])166[['b', 11], ['c', 2]]167"""168vec.sort()169vec2 = []170last_index = None171for index, coeff in vec:172if index == last_index:173if vec2[-1][1] == -coeff:174vec2.pop()175last_index = None176else:177vec2[-1][1] += coeff178else:179vec2.append([index, coeff])180last_index = index181return vec2182183184def setparts_recur(symlist, progress):185if not symlist:186yield progress187return188for i in Combinations(symlist[1:]):189j = [symlist[0]] + i190if progress and j < progress[-1]:191continue192cur = 0193new_symlist = []194for k in range(len(symlist)):195if cur < len(j) and symlist[k] == j[cur]:196cur += 1197else:198new_symlist.append(symlist[k])199yield from setparts_recur(new_symlist, progress + [j])200201202def setparts_with_auts(symlist):203r"""204Iterate through the pairs ``(part, aut)`` where ``part`` is a set205partition of ``symlist`` and ``aut`` is its number of206automorphisms.207208EXAMPLES::209210sage: from admcycles.DR.utils import setparts_with_auts211212sage: list(setparts_with_auts(symlist=[1]))213[([[1]], 1)]214sage: list(setparts_with_auts(symlist=[2]))215[([[2]], 1)]216sage: list(setparts_with_auts(symlist=[1, 1]))217[([[1], [1]], 1), ([[1, 1]], 1)]218sage: list(setparts_with_auts(symlist=[3]))219[([[3]], 1)]220sage: list(setparts_with_auts(symlist=[1, 2]))221[([[1], [2]], 1), ([[1, 2]], 1)]222sage: list(setparts_with_auts(symlist=[1, 1, 1]))223[([[1], [1], [1]], 1), ([[1], [1, 1]], 3), ([[1, 1, 1]], 1)]224"""225a = aut(symlist)226for i in setparts_recur(symlist, []):227b = aut(i)228for j in i:229b *= aut(j)230yield (i, a // b)231232233