Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
181 views
unlisted
ubuntu2004
o

��c��@s�dZddlmZddlmZddlmZddlmZdZ	dZ
dd�ed	�D�Zd
d�ed	�D�Z
dd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zd!dd�Zdd�Zdd�Zdd�Zd S)"z
Mixcellaneous functions
�)�ZZ)�QQ)�Subsets)�CombinationsFcCs<g|]}td|���td|���td|����qS)����r�	factorial��.0�n�r�6/home/user/Introduction lectures/admcycles/DR/utils.py�
<listcomp>
s4�r�dcCsLg|]"}td|d���d|dtd|���td|����qS)r�rrr	rrrrrsD�cCsddl}|����jtd�S)a�
    Return the memory usage of the current process in megabytes.

    This function was part of sage.misc.getusage but the module was
    removed in sage 9.5

    OUTPUT: a float representing the number of megabytes used.

    EXAMPLES::

        sage: from admcycles.DR.utils import get_memory_usage
        sage: t = get_memory_usage(); t  # random
        873.98046875
        sage: type(t)
        <... 'float'>
    rNi)�psutil�ProcessZmemory_infoZvms�float)rrrr�get_memory_usagesrcGstr
t||�dSdS)N)�
ENABLE_DPRINT�print)�string�argsrrr�dprint(s�rcGs&trddlm}|d||�dSdS)Nr)�save)�ENABLE_DSAVE�sage.misc.persistr)rrrrrr�dsave-s�rcCs`|st��S|��t��}d}|d}|dd�D]}||kr)|d7}||9}qd}|}q|S)z�
    Return the cardinality of the automorphism group of the list ``L``.

    EXAMPLES::

       sage: from admcycles.DR.utils import aut
       sage: aut([])
       1
       sage: aut([4,1,3,2])
       1
       sage: aut([4,5,6,5,4,4,6])
       24
    rrN)r�one�sort)�L�totalr
�last�lrrr�aut3s
r&cCsN|s|S|��|dg}t|dd��D]\}}|||kr$|�|�q|S)a^
    Remove duplicate elements in a list ``L``.

    One cannot use ``set(L)`` because the elements of ``L`` are not hashable.

    INPUT:

    - ``L`` -- a list

    OUTPUT:

    a list

    EXAMPLES::

       sage: from admcycles.DR.utils import remove_duplicates
       sage: remove_duplicates([4,7,6,4,3,3,4,2,2,1])
       [1, 2, 3, 4, 6, 7]
    rrN)r!�	enumerate�append)r"�LL�i�Lirrr�remove_duplicatesQs

�r,cCstt|d�}g}tt||�d�D]'}tttd||d��||�D]}|�dd�t|�D�tt|���q"q|S)a
    Return all subsequences of length ``l`` of ``n`` points with symmetry in the first ``symm`` points.

    EXAMPLES::

        sage: from admcycles.DR.utils import subsequences
        sage: subsequences(5,2,2)
        [[2, 3], [2, 4], [3, 4], [1, 2], [1, 3], [1, 4], [1, 1]]
    rrcSsg|]}d�qS)rr)r�_rrrr}sz subsequences.<locals>.<listcomp>)�max�range�minr�tupler(�sorted�list)r
r%�symm�sym�answer�ones�othersrrr�subsequencesos

$&�r9�xcCs(t|�t|�kr
t�t|�t||��S)a@
    Univariate Lagrange interpolation over the rationals.

    EXAMPLES::

        sage: from admcycles.DR.utils import interpolate
        sage: p = interpolate([1/2, -2, 3], [4/5, 2/3, -7/6])
        sage: p(1/2)
        4/5
        sage: p(-2)
        2/3
        sage: p(3)
        -7/6

    TESTS::

        sage: from admcycles.DR.utils import interpolate
        sage: parent(interpolate([], []))
        Univariate Polynomial Ring in x over Rational Field
        sage: parent(interpolate([], [], 'r'))
        Univariate Polynomial Ring in r over Rational Field
    )�len�
ValueErrorr�lagrange_polynomial�zip)�A�B�varrrr�interpolate�srBcCsr|��g}d}|D],\}}||kr-|dd|kr"|��d}q
|dd|7<q
|�||g�|}q
|S)aI
    Collect coefficients in a list of pairs (index, coefficient).

    This also sorts the indices and removes indices with zero coefficient.

    EXAMPLES::

        sage: from admcycles.DR.utils import simplify_sparse
        sage: simplify_sparse([('b',6),('a',1),('c',2),('a',-1),('b',5)])
        [['b', 11], ['c', 2]]
    N�����r)r!�popr()�vec�vec2�
last_index�index�coeffrrr�simplify_sparse�srJccs��|s|VdSt|dd��D]D}|dg|}|r"||dkr"qd}g}tt|��D]}|t|�krA||||krA|d7}q,|�||�q,t|||g�EdHqdS)NrrrC)rr/r;r(�setparts_recur)�symlist�progressr*�j�curZnew_symlist�krrrrK�s �
�rKccsJ�t|�}t|g�D]}t|�}|D]}|t|�9}q|||fVq
dS)a
    Iterate through the pairs ``(part, aut)`` where ``part`` is a set
    partition of ``symlist`` and ``aut`` is its number of
    automorphisms.

    EXAMPLES::

        sage: from admcycles.DR.utils import setparts_with_auts

        sage: list(setparts_with_auts(symlist=[1]))
        [([[1]], 1)]
        sage: list(setparts_with_auts(symlist=[2]))
        [([[2]], 1)]
        sage: list(setparts_with_auts(symlist=[1, 1]))
        [([[1], [1]], 1), ([[1, 1]], 1)]
        sage: list(setparts_with_auts(symlist=[3]))
        [([[3]], 1)]
        sage: list(setparts_with_auts(symlist=[1, 2]))
        [([[1], [2]], 1), ([[1, 2]], 1)]
        sage: list(setparts_with_auts(symlist=[1, 1, 1]))
        [([[1], [1], [1]], 1), ([[1], [1, 1]], 3), ([[1, 1, 1]], 1)]
    N)r&rK)rL�ar*�brNrrr�setparts_with_auts�s��rSN)r:)�__doc__�sage.rings.integer_ringr�sage.rings.rational_fieldr�sage.combinat.subsetr�sage.combinat.combinationrrrr/ZA_listZB_listrrrr&r,r9rBrJrKrSrrrr�<module>s.��