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

��c�(�@s�dZddlmZddlZddlZddlmZddlZddl	m
Z
ddlmZGdd�de
�ZGd	d
�d
e�Zej�ed�Zej�ed�Zee
e�Zeee�Zefd
d�Zefdd�Zefdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)z]
Cache of computations of top xi evaluations and various evaluation of tautological classes.
�)�literal_evalN)�DOT_SAGE)�ADM_EVAL_CACHE)�XI_TOP_CACHEc@sFeZdZdZddd�Zdd�Zddd�Zd	d
�Zddd
�Zdd�Z	dS)�Cachea0
    Dictionary with synchronization in a ``.sobj`` file.

    TESTS:

    The synchronization files stores only diffs::

        sage: from admcycles.diffstrata.cache import Cache
        sage: filename = tmp_filename(ext='.sobj')
        sage: C = Cache({2: 'two'}, filename=filename)
        sage: C[3] = 'three'
        sage: D = Cache(filename=filename)
        sage: D
        {3: 'three'}

    An example without synchronization file::

        sage: C = Cache({2: 'two'})
        sage: C[3] = 'three'
        sage: C
        {2: 'two', 3: 'three'}
    NcCsF||_|duri|_t�|�n|��|_t�||�|�|j�dS�N)�	_filename�_default�dict�__init__�copy�update_from_sobj_file)�self�values�filename�r�>/home/user/Introduction lectures/admcycles/diffstrata/cache.pyr(s
zCache.__init__cCs|��|�|j�dS)a�
        Reset the content of the dictionary to the set of default values.

        TESTS::

            sage: from admcycles.diffstrata.cache import Cache
            sage: C = Cache({1: 'ein', 2: 'zwei'})
            sage: C[3] = 'drei'
            sage: sorted(C.keys())
            [1, 2, 3]
            sage: C.reset_default()
            sage: sorted(C.keys())
            [1, 2]
        N)�clear�updater	)rrrr�
reset_default2szCache.reset_defaultcCsh|dur|j}|dur,tj�|�r.t�|�jr0|�tjj	�
|��||jkr2|��dSdSdSdSdS)a�
        Update the values of this dictionary from a ``.sobj`` file.

        Missing or empty files are ignored. The cache file is synchronized.

        EXAMPLES::

            sage: from admcycles.diffstrata.cache import Cache
            sage: filename1 = tmp_filename(ext='.sobj')
            sage: filename2 = tmp_filename(ext='.sobj')
            sage: C1 = Cache({2: 'two'}, filename=filename1)
            sage: C1[3] = 'three'
            sage: C2 = Cache(filename=filename2)
            sage: C2
            {}
            sage: C2.update_from_sobj_file(filename1)
            sage: Cache(filename=filename2)
            {3: 'three'}
        N)r�os�path�isfile�stat�st_sizer�sage�misc�persist�load�save_to_sobj_file)rrrrrr
Ds�
�
�zCache.update_from_sobj_filecCs|��}|jD]}||=q|Sr)rr	)r�cache�keyrrr�diff`s
z
Cache.diffTcCsJ|dur|j}|dur!|r|��n|��}|r#tjj�||�dSdSdS)zI
        Save the values of this dictionary to a ``.sobj`` file.
        N)rr"rrrr�save)rrZ	only_diffr rrrrfs�zCache.save_to_sobj_filecCst�|||�|��dSr)r
�__setitem__r�rr!�valuerrrr$qszCache.__setitem__)NNr)NT)
�__name__�
__module__�__qualname__�__doc__rrr
r"rr$rrrrrs



rc@seZdZdZdd�ZdS)�	FakeCachea
    A cache where ``__setitem__`` does not do anything.

    This class is intended for benchmarking without caching.

    TESTS::

        sage: from admcycles.diffstrata.cache import FakeCache
        sage: C = FakeCache()
        sage: C[3] = 'trois'
        sage: C
        {}
    cCsdSrrr%rrrr$�szFakeCache.__setitem__N)r'r(r)r*r$rrrrr+vsr+zadm_evals.sobjztop_xis.sobjccs,�t|�D]}|\}}||||fVqdS)aM
    A generator for decyphering LevelStratum.dict_key

    By default, the xi cache is used, alternatively a different
    cache dictionary may be specified.

    Args:
        xi_dict (dict, optional): cache dictionary. Defaults to ``TOP_XIS``.

    Yields:
        tuple: signature list, residue conditions, top xi evaluation
    N)�sorted)�xi_dictr!�sig_list�rcsrrr�list_top_xis�s
�
�r0c	Cs
d}d}d}tdjd|d�ddjd|d�ddjd	|d��td
|||�t|�D]S\}}}t|�dkr?|d}nt|�}|sIt�}nt|�dkrVt|d�}nd
d�|D�}tdjt|�|d�ddjt|�|dd�ddjt|�|dd��q/dS)a�
    The top xi powers in the cache are printed in human-readable form.

    Alternatively, any valid xi cache dictionary may be specified.

    The dimensions of the table might have to be adapted to console size and
    complexity of the involved strata.

    Args:
        xi_dict (dict, optional): xi cache dictionary. Defaults to ``TOP_XIS``.

    EXAMPLES::

        sage: from admcycles.diffstrata import print_top_xis
        sage: from admcycles.diffstrata.cache import TOP_XIS
        sage: TOP_XIS.reset_default()
        sage: print_top_xis()
        Stratum                                 | Residue Conditions                          | xi^dim
        --------------------------------------------------------------------------------------------------
        (-8, -2, 8)                             | [(0, 0), (0, 1)]                            | 1
        (-8, 0, 6)                              | [(0, 0)]                                    | 1
        (-8, 1, 1, 2, 2)                        | [(0, 0)]                                    | 49
        ...
        (3, 3)                                  | ()                                          | 0
        (4,)                                    | ()                                          | 305/580608
        (6,)                                    | ()                                          | -87983/199065600
        (8,)                                    | ()                                          | 339849/504627200
    �(�-�
�{:<{width}}�Stratum��width�|z Residue Conditionsz xi^dim�-�rcSsg|]}t|��qSr)�list)�.0�crrr�
<listcomp>�sz!print_top_xis.<locals>.<listcomp>�| N)�print�formatr0�lenr;�tuple�str)	r-�LEFT�MIDDLE�RIGHTr.�	res_conds�xi�sigr/rrr�
print_top_xis�s8���
����rKc	Cs�d}d}d}tdjd|d�ddjd|d�ddjd	|d��td
|||�t|�D]3}|\}}||}t|�}tdjt|�|d�ddjt|�|dd�ddjt|�|dd��q/d
S)a'
    The cached evaluations are printed in human-readable form.

    Alternatively, any valid adm cache dictionary may be specified (see load_adm_evals
    for details on the format).

    The dimensions of the table might have to be adapted to console size and
    complexity of the involved strata.

    Args:
        adm_dict (dict, optional): evaluations cache dictionary. Defaults to None.

    EXAMPLES::

        sage: from admcycles.diffstrata import print_adm_evals
        sage: from admcycles.diffstrata.cache import ADM_EVALS
        sage: ADM_EVALS.reset_default()
        sage: print_adm_evals()
        Stratum                                 | Psis                                        | eval
        --------------------------------------------------------------------------------------------------
        ...
        (2,)                                    | {1: 3}                                      | 1/1920
        (2, 2)                                  | {1: 1, 2: 5}                                | 71/322560
        (2, 2)                                  | {1: 2, 2: 4}                                | 13/53760
        (2, 2)                                  | {1: 3, 2: 3}                                | 13/53760
        (2, 2)                                  | {1: 6}                                      | 43/322560
        (4,)                                    | {1: 5}                                      | 13/580608
        (6,)                                    | {1: 7}                                      | 281/199065600
    r1r2r3r4r5r6r8z Psisz evalr9r?r:N)r@rAr,r
rD)	Zadm_dictrErFrGr!rJ�psisr&Zdpsisrrr�print_adm_evals�s.�������rMcC�&i}|��D]
\}}||t|�<q|Sr)�itemsrD)�d�	json_dict�k�vrrr�jsonify_dict�rTcCrNr)rOr)rQZ
restored_dictrRrSrrr�unjsonify_dictrUrVcC�t�|�dS)z}
    Import a dictionary of the form adm_key : value into the cache.

    Args:
        filename (String): sobj filename
    N)�	ADM_EVALSr
�rrrr�import_adm_evals�rZcCrW)z�
    Import a dictionary of the form LevelStratum.dict_key : value into the cache.

    Args:
        filename (String): sobj filename
    N)�TOP_XISr
rYrrr�import_top_xisr[r]cC�tS)a
    The cache dictionary for the admcycles evaluations (via GeneralisedStratum.adm_evaluate)

    The cache dictionary is of the form:

    adm_key -> GeneralisedStratum.adm_evaluate(adm_key)  (a rational number)

    Returns:
        dict: cache dictionary
    )rXrrrr�load_adm_evals'sr_cCr^)z�
    The cache dictionary for the top xi powers (evaluated)

    The cache dictionary is of the form:

    L.dict_key -> (L.xi)^L.dim().evaluate()  (a rational number)

    for a LevelStratum L.

    Returns:
        dict: cache dictionary
    )r\rrrr�load_xis5s
r`)r*�astrr�os.path�sage.envr�sage.misc.persistrZ#admcycles.diffstrata.adm_eval_cacherZadmcycles.diffstrata.xi_cacherr
rr+r�joinZADM_EVALS_FILENAMEZTOP_XIS_FILENAMErXr\r0rKrMrTrVrZr]r_r`rrrr�<module>s.f

5.