a
    ›¬<b(Á  ã                   @   s^  d Z dZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddl
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^mZ dd	l	m_Z_ dd
l	m`Z` eajbZce dd¡ZeedkrôddlfmgZh ddlfmi  mj  mkZl nddlmmnZh ddl	mmZl ddlmmoZompZpmqZq G dd„ deheƒZrG dd„ dƒZsetdkrZddluZueu v¡  dS )z[
This module defines the mpf, mpc classes, and standard functions for
operating with them.
Ú	plaintexté    Né   )ÚStandardBaseContext)Ú
basestringÚBACKEND)Úlibmp)UÚMPZÚMPZ_ZEROÚMPZ_ONEÚ	int_typesÚrepr_dpsÚround_floorÚround_ceilingÚdps_to_precÚround_nearestÚprec_to_dpsÚComplexResultÚto_pickableÚfrom_pickableÚ	normalizeÚfrom_intÚ
from_floatÚfrom_strÚto_intÚto_floatÚto_strÚfrom_rationalÚfrom_man_expÚfoneÚfzeroÚfinfÚfninfÚfnanÚmpf_absÚmpf_posÚmpf_negÚmpf_addÚmpf_subÚmpf_mulÚmpf_mul_intÚmpf_divÚmpf_rdiv_intÚmpf_pow_intÚmpf_modÚmpf_eqÚmpf_cmpÚmpf_ltÚmpf_gtÚmpf_leÚmpf_geÚmpf_hashÚmpf_randÚmpf_sumÚbitcountÚto_fixedÚ
mpc_to_strÚmpc_to_complexÚmpc_hashÚmpc_posÚmpc_is_nonzeroÚmpc_negÚmpc_conjugateÚmpc_absÚmpc_addÚmpc_add_mpfÚmpc_subÚmpc_sub_mpfÚmpc_mulÚmpc_mul_mpfÚmpc_mul_intÚmpc_divÚmpc_div_mpfÚmpc_powÚmpc_pow_mpfÚmpc_pow_intÚmpc_mpf_divÚmpf_powÚmpf_piÚ
mpf_degreeÚmpf_eÚmpf_phiÚmpf_ln2Úmpf_ln10Ú	mpf_eulerÚmpf_catalanÚ	mpf_aperyÚmpf_khinchinÚmpf_glaisherÚmpf_twinprimeÚmpf_mertensr   )Úfunction_docs)ÚrationalzX^\(?(?P<re>[\+\-]?\d*\.?\d*(e[\+\-]?\d+)?)??(?P<im>[\+\-]?\d*\.?\d*(e[\+\-]?\d+)?j)?\)?$Zsage)ÚContext)ÚPythonMPContext)Úctx_mp_python)Ú_mpfÚ_mpcÚ	mpnumericc                   @   sÀ  e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdmdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zed/d0„ ƒZed1d2„ ƒZdnd4d5„Zdod6d7„Zdpd8d9„Zdqd:d;„Z drd>d?„Z!dsdAdB„Z"dCdD„ Z#dEdF„ Z$dGdH„ Z%dIZ&dJZ'dtdLdM„Z(dNdO„ Z)dPdQ„ Z*dRdS„ Z+dTdU„ Z,dVdW„ Z-dXdY„ Z.dZd[„ Z/d\d]„ Z0d^d_„ Z1d`da„ Z2dbdc„ Z3ddde„ Z4dfdg„ Z5dhdi„ Z6djgd3fdkdl„Z7d<S )uÚ	MPContextzH
    Context for multiprecision arithmetic with a global precision.
    c                 C   sú   t  | ¡ d| _d| _| j| j| jg| _tj	| _
|  ¡  t | ¡ tj	| _	|  ¡  i | _|  ¡  z4tj| jj_tj| jj_tj| jj_tj| jj_W nB tyÖ   tj| jj_tj| jj_tj| jj_tj| jj_Y n0 tj| j_tj| j_tj| j_d S ©NF)ÚBaseMPContextÚ__init__Útrap_complexÚprettyÚmpfÚmpcÚconstantÚtypesr]   ÚmpqZ_mpqÚdefaultr   Úinit_builtinsÚhyp_summatorsZ_init_aliasesr\   Ú	bernoulliZim_funcZfunc_docZprimepiÚpsiÚatan2ÚAttributeErrorÚ__func__ÚdigammaÚcospiÚsinpi©Úctx© r|   ú]/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/mpmath/ctx_mp.pyrg   ?   s0    



zMPContext.__init__c                 C   s¢  | j }| j}|  t¡| _|  t¡| _|  ttf¡| _|  t	¡| _
|  t¡| _|  t¡| _|  dd„ dd¡}|| _|  tdd¡| _|  tdd¡| _|  tdd	¡| _|  td
d¡| _|  tdd¡| _|  tdd¡| _|  tdd¡| _|  tdd¡| _ |  t!dd¡| _"|  t#dd¡| _$|  t%dd¡| _&|  t'dd¡| _(|  t)dd¡| _*|  +t,j-t,j.¡| _/|  +t,j0t,j1¡| _2|  +t,j3t,j4¡| _5|  +t,j6t,j7¡| _8|  +t,j9t,j:¡| _;|  +t,j<t,j=¡| _>|  +t,j?t,j@¡| _A|  +t,jBt,jC¡| _D|  +t,jEt,jF¡| _G|  +t,jHt,jI¡| _J|  +t,jKt,jL¡| _M|  +t,jNt,jO¡| _P|  +t,jQt,jR¡| _S|  +t,jTt,jU¡| _V|  +t,jWt,jX¡| _Y|  +t,j6t,j7¡| _8|  +t,jZt,j[¡| _\|  +t,j]t,j^¡| __|  +t,j`t,ja¡| _b|  +t,jct,jd¡| _e|  +t,jft,jg¡| _h|  +t,jit,jj¡| _k|  +t,jlt,jm¡| _n|  +t,jot,jp¡| _q|  +t,jrt,js¡| _t|  +t,jut,jv¡ | _w| _x|  +t,jyt,jz¡| _{|  +t,j|t,j}¡| _~|  +t,jt,j€¡| _|  +t,j‚t,jƒ¡ | _„| _…|  +t,j†t,j‡¡| _ˆ|  +t,j‰t,jŠ¡| _‹|  +t,jŒt,j¡| _Ž|  +t,jt,j¡| _‘|  +t,j’t,j“¡| _”|  +t,j•t,j–¡| _—|  +t,j˜t,j™¡| _š|  +t,j›t,jœ¡| _|  +t,jžt,jŸ¡| _ |  +t,j¡d ¡| _¢|  +t,j£d ¡| _¤|  +t,j¥t,j¦¡| _§|  +t,j¨t,j©¡| _ªt«| d| j/ƒ| _/t«| d| j;ƒ| _;t«| d | j5ƒ| _5t«| d!| jGƒ| _Gt«| d"| jDƒ| _Dd S )#Nc                 S   s   dt d|  dfS )Nr   r   )r
   ©ÚprecZrndr|   r|   r}   Ú<lambda>m   ó    z)MPContext.init_builtins.<locals>.<lambda>zepsilon of working precisionÚepsÚpizln(2)Úln2zln(10)Úln10zGolden ratio phiÚphiz
e = exp(1)ÚezEuler's constantÚeulerzCatalan's constantÚcatalanzKhinchin's constantÚkhinchinzGlaisher's constantÚglaisherzApery's constantÚaperyz1 deg = pi / 180ÚdegreezTwin prime constantÚ	twinprimezMertens' constantÚmertensZ
_sage_sqrtZ	_sage_expZ_sage_lnZ	_sage_cosZ	_sage_sin)¬rj   rk   Úmake_mpfr   Úoner   ÚzeroÚmake_mpcÚjr    Úinfr!   Úninfr"   Únanrl   r‚   rO   rƒ   rS   r„   rT   r…   rR   r†   rQ   r‡   rU   rˆ   rV   r‰   rX   rŠ   rY   r‹   rW   rŒ   rP   r   rZ   rŽ   r[   r   Z_wrap_libmp_functionr   Zmpf_sqrtZmpc_sqrtÚsqrtZmpf_cbrtZmpc_cbrtZcbrtZmpf_logZmpc_logÚlnZmpf_atanZmpc_atanÚatanZmpf_expZmpc_expÚexpZmpf_expjZmpc_expjZexpjZ
mpf_expjpiZ
mpc_expjpiZexpjpiZmpf_sinZmpc_sinÚsinZmpf_cosZmpc_cosÚcosZmpf_tanZmpc_tanÚtanZmpf_sinhZmpc_sinhÚsinhZmpf_coshZmpc_coshÚcoshZmpf_tanhZmpc_tanhÚtanhZmpf_asinZmpc_asinÚasinZmpf_acosZmpc_acosÚacosZ	mpf_asinhZ	mpc_asinhÚasinhZ	mpf_acoshZ	mpc_acoshÚacoshZ	mpf_atanhZ	mpc_atanhÚatanhZ
mpf_sin_piZ
mpc_sin_piry   Z
mpf_cos_piZ
mpc_cos_pirx   Z	mpf_floorZ	mpc_floorÚfloorZmpf_ceilZmpc_ceilÚceilZmpf_nintZmpc_nintZnintZmpf_fracZmpc_fracÚfracZmpf_fibonacciZmpc_fibonacciZfibZ	fibonacciZ	mpf_gammaZ	mpc_gammaÚgammaZ
mpf_rgammaZ
mpc_rgammaZrgammaZmpf_loggammaZmpc_loggammaZloggammaZmpf_factorialZmpc_factorialZfacÚ	factorialZmpf_psi0Zmpc_psi0rw   Zmpf_harmonicZmpc_harmonicZharmonicZmpf_eiZmpc_eiÚeiZmpf_e1Zmpc_e1Úe1Zmpf_ciZmpc_ciZ_ciZmpf_siZmpc_siZ_siZ
mpf_ellipkZ
mpc_ellipkZellipkZ
mpf_ellipeZ
mpc_ellipeZ_ellipeZmpf_agm1Zmpc_agm1Zagm1Zmpf_erfZ_erfZmpf_erfcZ_erfcZmpf_zetaZmpc_zetaZ_zetaZmpf_altzetaZmpc_altzetaZ_altzetaÚgetattr)r{   rj   rk   r‚   r|   r|   r}   rp   `   s’    
ÿzMPContext.init_builtinsc                 C   s
   |  |¡S ©N)r8   )r{   Úxr   r|   r|   r}   r8   ¶   s    zMPContext.to_fixedc                 C   s4   |   |¡}|   |¡}|  tj|j|jg| j¢R Ž ¡S )z€
        Computes the Euclidean norm of the vector `(x, y)`, equal
        to `\sqrt{x^2 + y^2}`. Both `x` and `y` must be real.)Úconvertr   r   Z	mpf_hypotÚ_mpf_Ú_prec_rounding)r{   r°   Úyr|   r|   r}   Úhypot¹   s    

zMPContext.hypotc                 C   sv   t |  |¡ƒ}|dkr |  |¡S t|dƒs.t‚| j\}}tj||j||dd\}}|d u rd|  	|¡S |  
||f¡S d S )Nr   r²   T)rª   )ÚintÚ_rer­   ÚhasattrÚNotImplementedErrorr³   r   Ú
mpf_expintr²   r   r“   ©r{   ÚnÚzr   ÚroundingÚrealÚimagr|   r|   r}   Ú_gamma_upper_intÁ   s    



zMPContext._gamma_upper_intc                 C   sl   t |ƒ}|dkr|  |¡S t|dƒs(t‚| j\}}t ||j||¡\}}|d u rZ|  |¡S |  	||f¡S d S )Nr   r²   )
r¶   r­   r¸   r¹   r³   r   rº   r²   r   r“   r»   r|   r|   r}   Ú_expint_intÎ   s    



zMPContext._expint_intc                 C   sv   t |dƒrTz |  tj|j|g| j¢R Ž ¡W S  tyP   | jr@‚ |jtjf}Y qZ0 n|j	}|  
tj||g| j¢R Ž ¡S ©Nr²   )r¸   r   r   Zmpf_nthrootr²   r³   r   rh   r   Ú_mpc_r“   Zmpc_nthroot©r{   r°   r¼   r|   r|   r}   Ú_nthrootÛ   s    
 zMPContext._nthrootc                 C   sR   | j \}}t|dƒr,|  t ||j||¡¡S t|dƒrN|  t ||j||¡¡S d S ©Nr²   rÄ   )	r³   r¸   r   r   Zmpf_besseljnr²   r“   Zmpc_besseljnrÄ   )r{   r¼   r½   r   r¾   r|   r|   r}   Ú_besseljç   s
    


zMPContext._besseljr   c                 C   s¤   | j \}}t|dƒrRt|dƒrRz t |j|j||¡}|  |¡W S  tyP   Y n0 t|dƒrj|jtjf}n|j}t|dƒrˆ|jtjf}n|j}|  	t 
||||¡¡S rÃ   )r³   r¸   r   Zmpf_agmr²   r   r   r   rÄ   r“   Zmpc_agm)r{   ÚaÚbr   r¾   Úvr|   r|   r}   Ú_agmî   s    
zMPContext._agmc                 C   s   |   tjt|ƒg| j¢R Ž ¡S r¯   )r   r   Zmpf_bernoullir¶   r³   ©r{   r¼   r|   r|   r}   rr   ü   s    zMPContext.bernoullic                 C   s   |   tjt|ƒg| j¢R Ž ¡S r¯   )r   r   Zmpf_zeta_intr¶   r³   rÍ   r|   r|   r}   Ú	_zeta_intÿ   s    zMPContext._zeta_intc                 C   s4   |   |¡}|   |¡}|  tj|j|jg| j¢R Ž ¡S r¯   )r±   r   r   Z	mpf_atan2r²   r³   )r{   r´   r°   r|   r|   r}   rt     s    

zMPContext.atan2c                 C   s\   |   |¡}t|ƒ}|  |¡r:|  tj||jg| j¢R Ž ¡S |  tj	||j
g| j¢R Ž ¡S d S r¯   )r±   r¶   Ú_is_real_typer   r   Zmpf_psir²   r³   r“   Zmpc_psirÄ   )r{   Úmr½   r|   r|   r}   rs     s
    

zMPContext.psic                 K   s²   t |ƒ| jvr|  |¡}|  |¡\}}t|dƒrXt |j||¡\}}|  |¡|  |¡fS t|dƒrŠt 	|j
||¡\}}|  |¡|  |¡fS | j|fi |¤Ž| j|fi |¤ŽfS d S rÇ   )Útyperm   r±   Ú_parse_precr¸   r   Zmpf_cos_sinr²   r   Zmpc_cos_sinrÄ   r“   r   rœ   ©r{   r°   Úkwargsr   r¾   ÚcÚsr|   r|   r}   Úcos_sin  s    


zMPContext.cos_sinc                 K   s²   t |ƒ| jvr|  |¡}|  |¡\}}t|dƒrXt |j||¡\}}|  |¡|  |¡fS t|dƒrŠt 	|j
||¡\}}|  |¡|  |¡fS | j|fi |¤Ž| j|fi |¤ŽfS d S rÇ   )rÑ   rm   r±   rÒ   r¸   r   Zmpf_cos_sin_pir²   r   Zmpc_cos_sin_pirÄ   r“   r   rœ   rÓ   r|   r|   r}   Úcospi_sinpi  s    


zMPContext.cospi_sinpic                 C   s   |   ¡ }| j|_|S )zP
        Create a copy of the context, with the same working precision.
        )Ú	__class__r   )r{   rÉ   r|   r|   r}   Úclone)  s    zMPContext.clonec                 C   s   t |dƒst|ƒtu rdS dS )NrÄ   FT©r¸   rÑ   Úcomplex©r{   r°   r|   r|   r}   rÏ   4  s    zMPContext._is_real_typec                 C   s   t |dƒst|ƒtu rdS dS )NrÄ   TFrÛ   rÝ   r|   r|   r}   Ú_is_complex_type9  s    zMPContext._is_complex_typec                 C   sv   t |dƒr|jtkS t |dƒr(t|jv S t|tƒs>t|tjƒrBdS |  |¡}t |dƒs`t |dƒrj|  	|¡S t
dƒ‚dS )a¢  
        Return *True* if *x* is a NaN (not-a-number), or for a complex
        number, whether either the real or complex part is NaN;
        otherwise return *False*::

            >>> from mpmath import *
            >>> isnan(3.14)
            False
            >>> isnan(nan)
            True
            >>> isnan(mpc(3.14,2.72))
            False
            >>> isnan(mpc(3.14,nan))
            True

        r²   rÄ   Fzisnan() needs a number as inputN)r¸   r²   r"   rÄ   Ú
isinstancer   r]   rn   r±   ÚisnanÚ	TypeErrorrÝ   r|   r|   r}   rà   >  s    





zMPContext.isnanc                 C   s   |   |¡s|  |¡rdS dS )aè  
        Return *True* if *x* is a finite number, i.e. neither
        an infinity or a NaN.

            >>> from mpmath import *
            >>> isfinite(inf)
            False
            >>> isfinite(-inf)
            False
            >>> isfinite(3)
            True
            >>> isfinite(nan)
            False
            >>> isfinite(3+4j)
            True
            >>> isfinite(mpc(3,inf))
            False
            >>> isfinite(mpc(nan,3))
            False

        FT)Úisinfrà   rÝ   r|   r|   r}   ÚisfiniteZ  s    zMPContext.isfinitec                 C   sœ   |sdS t |dƒr,|j\}}}}|o*|dkS t |dƒrJ|j oH|  |j¡S t|ƒtv r^|dkS t|| jƒrŒ|j	\}}|s|dS |dkoŠ|dkS |  |  
|¡¡S )z<
        Determine if *x* is a nonpositive integer.
        Tr²   r   rÄ   r   )r¸   r²   rÀ   Úisnpintr¿   rÑ   r   rß   rn   Ú_mpq_r±   )r{   r°   ÚsignÚmanr›   ÚbcÚpÚqr|   r|   r}   rä   t  s    


zMPContext.isnpintc                 C   sF   dd| j   d¡d d| j  d¡d d| j  d¡d g}d	 |¡S )
NzMpmath settings:z  mp.prec = %sé   z[default: 53]z  mp.dps = %sz[default: 15]z  mp.trap_complex = %sz[default: False]Ú
)r   ÚljustÚdpsrh   Újoin)r{   Úlinesr|   r|   r}   Ú__str__ˆ  s    ýzMPContext.__str__c                 C   s
   t | jƒS r¯   )r   Ú_precrz   r|   r|   r}   Ú_repr_digits  s    zMPContext._repr_digitsc                 C   s   | j S r¯   )Z_dpsrz   r|   r|   r}   Ú_str_digits”  s    zMPContext._str_digitsFc                    s   t | ‡ fdd„d|ƒS )aÛ  
        The block

            with extraprec(n):
                <code>

        increases the precision n bits, executes <code>, and then
        restores the precision.

        extraprec(n)(f) returns a decorated version of the function f
        that increases the working precision by n bits before execution,
        and restores the parent precision afterwards. With
        normalize_output=True, it rounds the return value to the parent
        precision.
        c                    s   | ˆ  S r¯   r|   ©ré   ©r¼   r|   r}   r€   ¨  r   z%MPContext.extraprec.<locals>.<lambda>N©ÚPrecisionManager©r{   r¼   Únormalize_outputr|   rö   r}   Ú	extraprec˜  s    zMPContext.extraprecc                    s   t | d‡ fdd„|ƒS )z–
        This function is analogous to extraprec (see documentation)
        but changes the decimal precision instead of the number of bits.
        Nc                    s   | ˆ  S r¯   r|   ©Údrö   r|   r}   r€   ¯  r   z$MPContext.extradps.<locals>.<lambda>r÷   rù   r|   rö   r}   Úextradpsª  s    zMPContext.extradpsc                    s   t | ‡ fdd„d|ƒS )a»  
        The block

            with workprec(n):
                <code>

        sets the precision to n bits, executes <code>, and then restores
        the precision.

        workprec(n)(f) returns a decorated version of the function f
        that sets the precision to n bits before execution,
        and restores the precision afterwards. With normalize_output=True,
        it rounds the return value to the parent precision.
        c                    s   ˆ S r¯   r|   rõ   rö   r|   r}   r€   À  r   z$MPContext.workprec.<locals>.<lambda>Nr÷   rù   r|   rö   r}   Úworkprec±  s    zMPContext.workprecc                    s   t | d‡ fdd„|ƒS )z•
        This function is analogous to workprec (see documentation)
        but changes the decimal precision instead of the number of bits.
        Nc                    s   ˆ S r¯   r|   rü   rö   r|   r}   r€   Ç  r   z#MPContext.workdps.<locals>.<lambda>r÷   rù   r|   rö   r}   ÚworkdpsÂ  s    zMPContext.workdpsNr|   c                    s   ‡ ‡‡‡‡fdd„}|S )a‹
  
        Return a wrapped copy of *f* that repeatedly evaluates *f*
        with increasing precision until the result converges to the
        full precision used at the point of the call.

        This heuristically protects against rounding errors, at the cost of
        roughly a 2x slowdown compared to manually setting the optimal
        precision. This method can, however, easily be fooled if the results
        from *f* depend "discontinuously" on the precision, for instance
        if catastrophic cancellation can occur. Therefore, :func:`~mpmath.autoprec`
        should be used judiciously.

        **Examples**

        Many functions are sensitive to perturbations of the input arguments.
        If the arguments are decimal numbers, they may have to be converted
        to binary at a much higher precision. If the amount of required
        extra precision is unknown, :func:`~mpmath.autoprec` is convenient::

            >>> from mpmath import *
            >>> mp.dps = 15
            >>> mp.pretty = True
            >>> besselj(5, 125 * 10**28)    # Exact input
            -8.03284785591801e-17
            >>> besselj(5, '1.25e30')   # Bad
            7.12954868316652e-16
            >>> autoprec(besselj)(5, '1.25e30')   # Good
            -8.03284785591801e-17

        The following fails to converge because `\sin(\pi) = 0` whereas all
        finite-precision approximations of `\pi` give nonzero values::

            >>> autoprec(sin)(pi) # doctest: +IGNORE_EXCEPTION_DETAIL
            Traceback (most recent call last):
              ...
            NoConvergence: autoprec: prec increased to 2910 without convergence

        As the following example shows, :func:`~mpmath.autoprec` can protect against
        cancellation, but is fooled by too severe cancellation::

            >>> x = 1e-10
            >>> exp(x)-1; expm1(x); autoprec(lambda t: exp(t)-1)(x)
            1.00000008274037e-10
            1.00000000005e-10
            1.00000000005e-10
            >>> x = 1e-50
            >>> exp(x)-1; expm1(x); autoprec(lambda t: exp(t)-1)(x)
            0.0
            1.0e-50
            0.0

        With *catch*, an exception or list of exceptions to intercept
        may be specified. The raised exception is interpreted
        as signaling insufficient precision. This permits, for example,
        evaluating a function where a too low precision results in a
        division by zero::

            >>> f = lambda x: 1/(exp(x)-1)
            >>> f(1e-30)
            Traceback (most recent call last):
              ...
            ZeroDivisionError
            >>> autoprec(f, catch=ZeroDivisionError)(1e-30)
            1.0e+30


        c                     s(  ˆj }ˆd u rˆ |¡}nˆ}zú|d ˆ_ zˆ| i |¤Ž}W n ˆ yT   ˆj}Y n0 |d }|ˆ_ zˆ| i |¤Ž}W n ˆ yŽ   ˆj}Y n0 ||krœqˆ || ¡ˆ |¡ }|| k rÂqˆrÚtd||| f ƒ |}||krôˆ d| ¡‚|t|d ƒ7 }t||ƒ}q^W |ˆ_ n|ˆ_ 0 |
 S )Né
   é   z)autoprec: target=%s, prec=%s, accuracy=%sz2autoprec: prec increased to %i without convergenceé   )r   Ú_default_hyper_maxprecr—   ÚmagÚprintZNoConvergencer¶   Úmin)ÚargsrÔ   r   Zmaxprec2Úv1Zprec2Úv2Úerr©Úcatchr{   ÚfÚmaxprecÚverboser|   r}   Úf_autoprec_wrapped  sH    


ÿÿÿz.MPContext.autoprec.<locals>.f_autoprec_wrappedr|   )r{   r  r  r  r  r  r|   r  r}   ÚautoprecÉ  s    D%zMPContext.autoprecé   c                    sÐ   t |tƒr*dd ‡ ‡‡fdd„|D ƒ¡ S t |tƒrTdd ‡ ‡‡fdd„|D ƒ¡ S t|dƒrrt|jˆfi ˆ¤ŽS t|dƒr˜d	t|jˆfi ˆ¤Ž d
 S t |t	ƒrªt
|ƒS t |ˆ jƒrÈ|jˆfi ˆ¤ŽS t|ƒS )a3  
        Convert an ``mpf`` or ``mpc`` to a decimal string literal with *n*
        significant digits. The small default value for *n* is chosen to
        make this function useful for printing collections of numbers
        (lists, matrices, etc).

        If *x* is a list or tuple, :func:`~mpmath.nstr` is applied recursively
        to each element. For unrecognized classes, :func:`~mpmath.nstr`
        simply returns ``str(x)``.

        The companion function :func:`~mpmath.nprint` prints the result
        instead of returning it.

        The keyword arguments *strip_zeros*, *min_fixed*, *max_fixed*
        and *show_zero_exponent* are forwarded to :func:`~mpmath.libmp.to_str`.

        The number will be printed in fixed-point format if the position
        of the leading digit is strictly between min_fixed
        (default = min(-dps/3,-5)) and max_fixed (default = dps).

        To force fixed-point format always, set min_fixed = -inf,
        max_fixed = +inf. To force floating-point format, set
        min_fixed >= max_fixed.

            >>> from mpmath import *
            >>> nstr([+pi, ldexp(1,-500)])
            '[3.14159, 3.05494e-151]'
            >>> nprint([+pi, ldexp(1,-500)])
            [3.14159, 3.05494e-151]
            >>> nstr(mpf("5e-10"), 5)
            '5.0e-10'
            >>> nstr(mpf("5e-10"), 5, strip_zeros=False)
            '5.0000e-10'
            >>> nstr(mpf("5e-10"), 5, strip_zeros=False, min_fixed=-11)
            '0.00000000050000'
            >>> nstr(mpf(0), 5, show_zero_exponent=True)
            '0.0e+0'

        z[%s]z, c                 3   s"   | ]}ˆ j |ˆfi ˆ¤ŽV  qd S r¯   ©Únstr©Ú.0rÕ   ©r{   rÔ   r¼   r|   r}   Ú	<genexpr>]  r   z!MPContext.nstr.<locals>.<genexpr>z(%s)c                 3   s"   | ]}ˆ j |ˆfi ˆ¤ŽV  qd S r¯   r  r  r  r|   r}   r  _  r   r²   rÄ   ú(ú))rß   Úlistrï   Útupler¸   r   r²   r9   rÄ   r   ÚreprZmatrixZ__nstr__Ústr)r{   r°   r¼   rÔ   r|   r  r}   r  4  s    (
 
 


zMPContext.nstrc                 C   s°   |rnt |tƒrnd| ¡ v rn| ¡  dd¡}t |¡}| d¡}|sFd}| d¡ d¡}|  |  	|¡|  	|¡¡S t
|dƒrœ|j\}}||kr”|  |¡S tdƒ‚td	t|ƒ ƒ‚d S )
Nr”   ú Ú Úrer   ÚimÚ_mpi_z,can only create mpf from zero-width intervalzcannot create mpf from )rß   r   ÚlowerÚreplaceÚget_complexÚmatchÚgroupÚrstriprk   r±   r¸   r$  r   Ú
ValueErrorrá   r  )r{   r°   Ústringsr(  r"  r#  rÉ   rÊ   r|   r|   r}   Ú_convert_fallbackj  s    




zMPContext._convert_fallbackc                 O   s   | j |i |¤ŽS r¯   )r±   )r{   r  rÔ   r|   r|   r}   Ú	mpmathify|  s    zMPContext.mpmathifyc                 C   sˆ   |r‚|  d¡rdS | j\}}d|v r,|d }d|v rT|d }|| jkrJdS t|ƒ}n&d|v rz|d }|| jkrrdS t|ƒ}||fS | jS )NÚexact)r   r  r¾   r   rî   )Úgetr³   r•   r¶   r   )r{   rÔ   r   r¾   rî   r|   r|   r}   rÒ     s$    




zMPContext._parse_precz'the exact result does not fit in memoryzœhypsum() failed to converge to the requested %i bits of accuracy
using a working precision of %i bits. Try with a higher maxprec,
maxterms, or set zeroprec.Tc           !      K   sÖ  t |dƒr|||df}|j}	nt |dƒr:|||df}|j}	|| jvrXt |¡d | j|< | j| }
| j}| d|  |¡¡}d}d}i }d	}t	|ƒD ]ð\}}|| d
kr||kr’|d	kr’d}t	|d |… ƒD ](\}}|| d
krÌ|d	krÌ||krÌd}qÌ|s’t
dƒ‚q’|  |¡\}}t|ƒ }| }||krv|d	krv|dkrv||v r\||  |7  < n|||< t||| d ƒ}|t|ƒ7 }q’||kr¤t| j||| f ƒ‚|| }|rÆtdd„ |D ƒƒ}ni }|
||	||||fi |¤Ž\}}}| }d}||k r,| ¡ D ]$}|d u s||k rd} q,q||d d k pB| }|rˆ|rTq¤| d¡} | d urˆ|| krˆ|r‚|  d	¡S | jS |d9 }|d7 }|d7 }q„t|ƒtu rÎ|rÂ|  |¡S |  |¡S n|S d S )Nr²   ÚRrÄ   ÚCr   r  é2   é   r   ÚZFTzpole in hypergeometric seriesé   é<   c                 s   s   | ]}|d fV  qd S r¯   r|   )r  r¼   r|   r|   r}   r  Ç  r   z#MPContext.hypsum.<locals>.<genexpr>é   Úzeroprecr  )r¸   r²   rÄ   rq   r   Zmake_hyp_summatorr   r0  r  Ú	enumerateÚZeroDivisionErrorÚnint_distancer¶   ÚmaxÚabsr+  Ú_hypsum_msgÚdictÚvaluesrk   r’   rÑ   r  r“   r   )!r{   ré   rê   ÚflagsZcoeffsr½   Zaccurate_smallrÔ   ÚkeyrË   Zsummatorr   r  rû   ZepsshiftZmagnitude_checkZmax_total_jumpÚirÕ   ÚokÚiiÚccr¼   rý   ZwpZmag_dictZzvZhave_complexZ	magnitudeÚcancelZjumps_resolvedZaccurater9  r|   r|   r}   Úhypsumš  sŒ    







ÿÿ






zMPContext.hypsumc                 C   s   |   |¡}|  t |j|¡¡S )a–  
        Computes `x 2^n` efficiently. No rounding is performed.
        The argument `x` must be a real floating-point number (or
        possible to convert into one) and `n` must be a Python ``int``.

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> ldexp(1, 10)
            mpf('1024.0')
            >>> ldexp(1, -3)
            mpf('0.125')

        )r±   r   r   Z	mpf_shiftr²   rÅ   r|   r|   r}   Úldexpï  s    
zMPContext.ldexpc                 C   s(   |   |¡}t |j¡\}}|  |¡|fS )a=  
        Given a real number `x`, returns `(y, n)` with `y \in [0.5, 1)`,
        `n` a Python integer, and such that `x = y 2^n`. No rounding is
        performed.

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> frexp(7.5)
            (mpf('0.9375'), 3)

        )r±   r   Z	mpf_frexpr²   r   )r{   r°   r´   r¼   r|   r|   r}   Úfrexp   s    
zMPContext.frexpc                 K   s`   |   |¡\}}|  |¡}t|dƒr6|  t|j||ƒ¡S t|dƒrT|  t|j||ƒ¡S t	dƒ‚dS )a  
        Negates the number *x*, giving a floating-point result, optionally
        using a custom precision and rounding mode.

        See the documentation of :func:`~mpmath.fadd` for a detailed description
        of how to specify precision and rounding.

        **Examples**

        An mpmath number is returned::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fneg(2.5)
            mpf('-2.5')
            >>> fneg(-5+2j)
            mpc(real='5.0', imag='-2.0')

        Precise control over rounding is possible::

            >>> x = fadd(2, 1e-100, exact=True)
            >>> fneg(x)
            mpf('-2.0')
            >>> fneg(x, rounding='f')
            mpf('-2.0000000000000004')

        Negating with and without roundoff::

            >>> n = 200000000000000000000001
            >>> print(int(-mpf(n)))
            -200000000000000016777216
            >>> print(int(fneg(n)))
            -200000000000000016777216
            >>> print(int(fneg(n, prec=log(n,2)+1)))
            -200000000000000000000001
            >>> print(int(fneg(n, dps=log(n,10)+1)))
            -200000000000000000000001
            >>> print(int(fneg(n, prec=inf)))
            -200000000000000000000001
            >>> print(int(fneg(n, dps=inf)))
            -200000000000000000000001
            >>> print(int(fneg(n, exact=True)))
            -200000000000000000000001

        r²   rÄ   ú2Arguments need to be mpf or mpc compatible numbersN)
rÒ   r±   r¸   r   r%   r²   r“   r>   rÄ   r+  )r{   r°   rÔ   r   r¾   r|   r|   r}   Úfneg  s    .


zMPContext.fnegc              	   K   sø   |   |¡\}}|  |¡}|  |¡}z¨t|dƒrvt|dƒrR|  t|j|j||ƒ¡W S t|dƒrv|  t|j|j||ƒ¡W S t|dƒrÈt|dƒr¤|  t|j|j||ƒ¡W S t|dƒrÈ|  t	|j|j||ƒ¡W S W n  t
tfyê   t| jƒ‚Y n0 t
dƒ‚dS )a“  
        Adds the numbers *x* and *y*, giving a floating-point result,
        optionally using a custom precision and rounding mode.

        The default precision is the working precision of the context.
        You can specify a custom precision in bits by passing the *prec* keyword
        argument, or by providing an equivalent decimal precision with the *dps*
        keyword argument. If the precision is set to ``+inf``, or if the flag
        *exact=True* is passed, an exact addition with no rounding is performed.

        When the precision is finite, the optional *rounding* keyword argument
        specifies the direction of rounding. Valid options are ``'n'`` for
        nearest (default), ``'f'`` for floor, ``'c'`` for ceiling, ``'d'``
        for down, ``'u'`` for up.

        **Examples**

        Using :func:`~mpmath.fadd` with precision and rounding control::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fadd(2, 1e-20)
            mpf('2.0')
            >>> fadd(2, 1e-20, rounding='u')
            mpf('2.0000000000000004')
            >>> nprint(fadd(2, 1e-20, prec=100), 25)
            2.00000000000000000001
            >>> nprint(fadd(2, 1e-20, dps=15), 25)
            2.0
            >>> nprint(fadd(2, 1e-20, dps=25), 25)
            2.00000000000000000001
            >>> nprint(fadd(2, 1e-20, exact=True), 25)
            2.00000000000000000001

        Exact addition avoids cancellation errors, enforcing familiar laws
        of numbers such as `x+y-x = y`, which don't hold in floating-point
        arithmetic with finite precision::

            >>> x, y = mpf(2), mpf('1e-1000')
            >>> print(x + y - x)
            0.0
            >>> print(fadd(x, y, prec=inf) - x)
            1.0e-1000
            >>> print(fadd(x, y, exact=True) - x)
            1.0e-1000

        Exact addition can be inefficient and may be impossible to perform
        with large magnitude differences::

            >>> fadd(1, '1e-100000000000000000000', prec=inf)
            Traceback (most recent call last):
              ...
            OverflowError: the exact result does not fit in memory

        r²   rÄ   rL  N)rÒ   r±   r¸   r   r&   r²   r“   rB   rÄ   rA   r+  ÚOverflowErrorÚ_exact_overflow_msg©r{   r°   r´   rÔ   r   r¾   r|   r|   r}   ÚfaddF  s"    8







zMPContext.faddc              	   K   sü   |   |¡\}}|  |¡}|  |¡}z¬t|dƒrzt|dƒrR|  t|j|j||ƒ¡W S t|dƒrz|  t|jtf|j	||ƒ¡W S t|dƒrÌt|dƒr¨|  t
|j	|j||ƒ¡W S t|dƒrÌ|  t|j	|j	||ƒ¡W S W n  ttfyî   t| jƒ‚Y n0 tdƒ‚dS )a  
        Subtracts the numbers *x* and *y*, giving a floating-point result,
        optionally using a custom precision and rounding mode.

        See the documentation of :func:`~mpmath.fadd` for a detailed description
        of how to specify precision and rounding.

        **Examples**

        Using :func:`~mpmath.fsub` with precision and rounding control::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fsub(2, 1e-20)
            mpf('2.0')
            >>> fsub(2, 1e-20, rounding='d')
            mpf('1.9999999999999998')
            >>> nprint(fsub(2, 1e-20, prec=100), 25)
            1.99999999999999999999
            >>> nprint(fsub(2, 1e-20, dps=15), 25)
            2.0
            >>> nprint(fsub(2, 1e-20, dps=25), 25)
            1.99999999999999999999
            >>> nprint(fsub(2, 1e-20, exact=True), 25)
            1.99999999999999999999

        Exact subtraction avoids cancellation errors, enforcing familiar laws
        of numbers such as `x-y+y = x`, which don't hold in floating-point
        arithmetic with finite precision::

            >>> x, y = mpf(2), mpf('1e1000')
            >>> print(x - y + y)
            0.0
            >>> print(fsub(x, y, prec=inf) + y)
            2.0
            >>> print(fsub(x, y, exact=True) + y)
            2.0

        Exact addition can be inefficient and may be impossible to perform
        with large magnitude differences::

            >>> fsub(1, '1e-100000000000000000000', prec=inf)
            Traceback (most recent call last):
              ...
            OverflowError: the exact result does not fit in memory

        r²   rÄ   rL  N)rÒ   r±   r¸   r   r'   r²   r“   rC   r   rÄ   rD   r+  rN  rO  rP  r|   r|   r}   Úfsub  s"    0







zMPContext.fsubc              	   K   sø   |   |¡\}}|  |¡}|  |¡}z¨t|dƒrvt|dƒrR|  t|j|j||ƒ¡W S t|dƒrv|  t|j|j||ƒ¡W S t|dƒrÈt|dƒr¤|  t|j|j||ƒ¡W S t|dƒrÈ|  t	|j|j||ƒ¡W S W n  t
tfyê   t| jƒ‚Y n0 t
dƒ‚dS )a¥  
        Multiplies the numbers *x* and *y*, giving a floating-point result,
        optionally using a custom precision and rounding mode.

        See the documentation of :func:`~mpmath.fadd` for a detailed description
        of how to specify precision and rounding.

        **Examples**

        The result is an mpmath number::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fmul(2, 5.0)
            mpf('10.0')
            >>> fmul(0.5j, 0.5)
            mpc(real='0.0', imag='0.25')

        Avoiding roundoff::

            >>> x, y = 10**10+1, 10**15+1
            >>> print(x*y)
            10000000001000010000000001
            >>> print(mpf(x) * mpf(y))
            1.0000000001e+25
            >>> print(int(mpf(x) * mpf(y)))
            10000000001000011026399232
            >>> print(int(fmul(x, y)))
            10000000001000011026399232
            >>> print(int(fmul(x, y, dps=25)))
            10000000001000010000000001
            >>> print(int(fmul(x, y, exact=True)))
            10000000001000010000000001

        Exact multiplication with complex numbers can be inefficient and may
        be impossible to perform with large magnitude differences between
        real and imaginary parts::

            >>> x = 1+2j
            >>> y = mpc(2, '1e-100000000000000000000')
            >>> fmul(x, y)
            mpc(real='2.0', imag='4.0')
            >>> fmul(x, y, rounding='u')
            mpc(real='2.0', imag='4.0000000000000009')
            >>> fmul(x, y, exact=True)
            Traceback (most recent call last):
              ...
            OverflowError: the exact result does not fit in memory

        r²   rÄ   rL  N)rÒ   r±   r¸   r   r(   r²   r“   rF   rÄ   rE   r+  rN  rO  rP  r|   r|   r}   ÚfmulÒ  s"    3







zMPContext.fmulc                 K   sÚ   |   |¡\}}|stdƒ‚|  |¡}|  |¡}t|dƒr€t|dƒrZ|  t|j|j||ƒ¡S t|dƒr€|  t|jt	f|j
||ƒ¡S t|dƒrÎt|dƒr¬|  t|j
|j||ƒ¡S t|dƒrÎ|  t|j
|j
||ƒ¡S tdƒ‚dS )a©  
        Divides the numbers *x* and *y*, giving a floating-point result,
        optionally using a custom precision and rounding mode.

        See the documentation of :func:`~mpmath.fadd` for a detailed description
        of how to specify precision and rounding.

        **Examples**

        The result is an mpmath number::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fdiv(3, 2)
            mpf('1.5')
            >>> fdiv(2, 3)
            mpf('0.66666666666666663')
            >>> fdiv(2+4j, 0.5)
            mpc(real='4.0', imag='8.0')

        The rounding direction and precision can be controlled::

            >>> fdiv(2, 3, dps=3)    # Should be accurate to at least 3 digits
            mpf('0.6666259765625')
            >>> fdiv(2, 3, rounding='d')
            mpf('0.66666666666666663')
            >>> fdiv(2, 3, prec=60)
            mpf('0.66666666666666667')
            >>> fdiv(2, 3, rounding='u')
            mpf('0.66666666666666674')

        Checking the error of a division by performing it at higher precision::

            >>> fdiv(2, 3) - fdiv(2, 3, prec=100)
            mpf('-3.7007434154172148e-17')

        Unlike :func:`~mpmath.fadd`, :func:`~mpmath.fmul`, etc., exact division is not
        allowed since the quotient of two floating-point numbers generally
        does not have an exact floating-point representation. (In the
        future this might be changed to allow the case where the division
        is actually exact.)

            >>> fdiv(2, 3, exact=True)
            Traceback (most recent call last):
              ...
            ValueError: division is not an exact operation

        z"division is not an exact operationr²   rÄ   rL  N)rÒ   r+  r±   r¸   r   r*   r²   r“   rH   r   rÄ   rI   rP  r|   r|   r}   Úfdiv  s     1







zMPContext.fdivc                 C   s  t |ƒ}|tv rt|ƒ| jfS |tju rˆ|j\}}t||ƒ\}}d| |krV|d7 }n|sd|| jfS tt	|||  ƒƒt|ƒ }||fS t
|dƒr |j}| j}	n|t
|dƒrè|j\}}
|
\}}}}|rÎ|| }	n|
tkrÞ| j}	ntdƒ‚n4|  |¡}t
|dƒs
t
|dƒr|  |¡S tdƒ‚|\}}}}|| }|dk rDd}|}nº|rà|dkrd||> }| j}nn|dkr€|d? d }d}nR| d }||? }|d@ r²|d7 }||> | }n|||> 8 }|d? }|t|ƒ }|rþ| }n|tkrö| j}d}ntdƒ‚|t||	ƒfS )	aº  
        Return `(n,d)` where `n` is the nearest integer to `x` and `d` is
        an estimate of `\log_2(|x-n|)`. If `d < 0`, `-d` gives the precision
        (measured in bits) lost to cancellation when computing `x-n`.

            >>> from mpmath import *
            >>> n, d = nint_distance(5)
            >>> print(n); print(d)
            5
            -inf
            >>> n, d = nint_distance(mpf(5))
            >>> print(n); print(d)
            5
            -inf
            >>> n, d = nint_distance(mpf(5.00000001))
            >>> print(n); print(d)
            5
            -26
            >>> n, d = nint_distance(mpf(4.99999999))
            >>> print(n); print(d)
            5
            -26
            >>> n, d = nint_distance(mpc(5,10))
            >>> print(n); print(d)
            5
            4
            >>> n, d = nint_distance(mpc(5,0.000001))
            >>> print(n); print(d)
            5
            -19

        r  r   r²   rÄ   zrequires a finite numberzrequires an mpf/mpcr   éÿÿÿÿ)rÑ   r   r¶   r–   r]   rn   rå   Údivmodr7   r>  r¸   r²   rÄ   r   r+  r±   r<  rá   r=  )r{   r°   Ztypxré   rê   r¼   Úrrý   r"  Zim_distr#  ZisignZimanZiexpZibcræ   rç   r›   rè   r  Zre_distÚtr|   r|   r}   r<  Y  sl    !
















zMPContext.nint_distancec                 C   s8   | j }z"| j}|D ]}||9 }qW || _ n|| _ 0 |
 S )aT  
        Calculates a product containing a finite number of factors (for
        infinite products, see :func:`~mpmath.nprod`). The factors will be
        converted to mpmath numbers.

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> fprod([1, 2, 0.5, 7])
            mpf('7.0')

        )r   r‘   )r{   ZfactorsÚorigrË   ré   r|   r|   r}   Úfprod»  s    zMPContext.fprodc                 C   s   |   t| jƒ¡S )z·
        Returns an ``mpf`` with value chosen randomly from `[0, 1)`.
        The number of randomly generated bits in the mantissa is equal
        to the working precision.
        )r   r5   rò   rz   r|   r|   r}   ÚrandÐ  s    zMPContext.randc                    s   |   ‡ ‡fdd„dˆ ˆf ¡S )a  
        Given Python integers `(p, q)`, returns a lazy ``mpf`` representing
        the fraction `p/q`. The value is updated with the precision.

            >>> from mpmath import *
            >>> mp.dps = 15
            >>> a = fraction(1,100)
            >>> b = mpf(1)/100
            >>> print(a); print(b)
            0.01
            0.01
            >>> mp.dps = 30
            >>> print(a); print(b)      # a will be accurate
            0.01
            0.0100000000000000002081668171172
            >>> mp.dps = 15
        c                    s   t ˆ ˆ| |ƒS r¯   )r   r~   ©ré   rê   r|   r}   r€   ê  r   z$MPContext.fraction.<locals>.<lambda>z%s/%s)rl   )r{   ré   rê   r|   r\  r}   ÚfractionØ  s    
ÿzMPContext.fractionc                 C   s   t |  |¡ƒS r¯   ©r>  r±   rÝ   r|   r|   r}   Úabsminí  s    zMPContext.absminc                 C   s   t |  |¡ƒS r¯   r^  rÝ   r|   r|   r}   Úabsmaxð  s    zMPContext.absmaxc                 C   s,   t |dƒr(|j\}}|  |¡|  |¡gS |S )Nr$  )r¸   r$  r   )r{   r°   rÉ   rÊ   r|   r|   r}   Ú
_as_pointsó  s    

zMPContext._as_pointsr   c           	         sl   ˆ   |¡rt|dƒst‚t|ƒ}ˆ j}t |j|||||¡\}}‡ fdd„|D ƒ}‡ fdd„|D ƒ}||fS )NrÄ   c                    s   g | ]}ˆ   |¡‘qS r|   ©r“   )r  r°   rz   r|   r}   Ú
<listcomp>  r   z+MPContext._zetasum_fast.<locals>.<listcomp>c                    s   g | ]}ˆ   |¡‘qS r|   rb  )r  r´   rz   r|   r}   rc    r   )Zisintr¸   r¹   r¶   rò   r   Zmpc_zetasumrÄ   )	r{   rÖ   rÉ   r¼   ZderivativesZreflectr   ÚxsZysr|   rz   r}   Ú_zetasum_fast  s    zMPContext._zetasum_fast)r   )F)F)F)F)Nr|   F)r  )T)8Ú__name__Ú
__module__Ú__qualname__Ú__doc__rg   rp   r8   rµ   rÁ   rÂ   rÆ   rÈ   rÌ   rr   rÎ   rt   rs   r×   rØ   rÚ   rÏ   rÞ   rà   rã   rä   rñ   Úpropertyró   rô   rû   rþ   rÿ   r   r  r  r-  r.  rÒ   rO  r?  rI  rJ  rK  rM  rQ  rR  rS  rT  r<  rZ  r[  r]  r_  r`  ra  re  r|   r|   r|   r}   rd   :   sl   !V







k
6
U6JBEBbrd   c                   @   s.   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
S )rø   Fc                 C   s   || _ || _|| _|| _d S r¯   )r{   ÚprecfunÚdpsfunrú   )Úselfr{   rk  rl  rú   r|   r|   r}   rg     s    zPrecisionManager.__init__c                    s   t  ˆ ¡‡ ‡fdd„ƒ}|S )Nc                     s´   ˆj j}zœˆjr$ˆ ˆj j¡ˆj _nˆ ˆj j¡ˆj _ˆjr‚ˆ | i |¤Ž}t|ƒtu rrtdd„ |D ƒƒW |ˆj _S |
 W |ˆj _S ˆ | i |¤ŽW |ˆj _S W |ˆj _n
|ˆj _0 d S )Nc                 S   s   g | ]
}|
 ‘qS r|   r|   )r  rÉ   r|   r|   r}   rc  '  r   z8PrecisionManager.__call__.<locals>.g.<locals>.<listcomp>)r{   r   rk  rl  rî   rú   rÑ   r  )r  rÔ   rY  rË   ©r  rm  r|   r}   Úg  s$    ûüþz$PrecisionManager.__call__.<locals>.g)Ú	functoolsÚwraps)rm  r  ro  r|   rn  r}   Ú__call__  s    zPrecisionManager.__call__c                 C   s:   | j j| _| jr$|  | j j¡| j _n|  | j j¡| j _d S r¯   )r{   r   Úorigprk  rl  rî   )rm  r|   r|   r}   Ú	__enter__.  s    
zPrecisionManager.__enter__c                 C   s   | j | j_dS re   )rs  r{   r   )rm  Úexc_typeÚexc_valÚexc_tbr|   r|   r}   Ú__exit__4  s    
zPrecisionManager.__exit__N)F)rf  rg  rh  rg   rr  rt  rx  r|   r|   r|   r}   rø     s   
rø   Ú__main__)wri  Z__docformat__rp  r"  Zctx_baser   Zlibmp.backendr   r   r!  r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   ÚobjectÚ__new__ÚnewÚcompiler'  Zsage.libs.mpmath.ext_mainr^   rf   ZlibsZmpmathZext_mainZ_mpf_moduler`   r_   ra   rb   rc   rd   rø   rf  ÚdoctestÚtestmodr|   r|   r|   r}   Ú<module>   s@   ÿ ]

         d$
