o
    tVa,                     @   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rd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r.ddluZueuv  dS dS )z[
This module defines the mpf, mpc classes, and standard functions for
operating with them.
Z	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)rationalz\^\(?(?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dnd6d7Zdnd8d9Zdnd:d;Z dod>d?Z!dpdAdBZ"dCdD Z#dEdF Z$dGdH Z%dIZ&dJZ'dqdLdM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 Z6	 djgd3fdkdlZ7d<S )r	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 | _|   ztj| jj_tj| jj_tj| jj_tj| jj_W n! tyk   tj| jj_tj| jj_tj| jj_tj| jj_Y nw tj| j_tj| j_tj| j_d S NF)BaseMPContext__init__trap_complexZ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 rz   //usr/lib/python3/dist-packages/mpmath/ctx_mp.pyrf   ?   s2   



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,jt,j | _| _| +t,jt,j| _| +t,jt,j| _| +t,jt,j| _| +t,jt,j| _| +t,jt,j| _| +t,jt,j| _| +t,jt,j| _| +t,jt,j| _| +t,jt,j| _| +t,jd | _| +t,jd | _| +t,jt,j| _| +t,jt,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rndrz   rz   r{   <lambda>m   s    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)rh   ri   make_mpfr   oner   zeromake_mpcjr   infr    ninfr!   nanrj   r   rN   r   rR   r   rS   r   rQ   r   rP   r   rT   r   rU   r   rW   r   rX   r   rV   r   rO   r   rY   r   rZ   r   Z_wrap_libmp_functionr   Zmpf_sqrtZmpc_sqrtZsqrtZmpf_cbrtZmpc_cbrtZcbrtZmpf_logZmpc_logZlnZmpf_atanZmpc_atanZ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_tanZtanZmpf_sinhZmpc_sinhZsinhZmpf_coshZmpc_coshZcoshZmpf_tanhZmpc_tanhZtanhZmpf_asinZmpc_asinZasinZmpf_acosZmpc_acosZacosZ	mpf_asinhZ	mpc_asinhZasinhZ	mpf_acoshZ	mpc_acoshZacoshZ	mpf_atanhZ	mpc_atanhZatanhZ
mpf_sin_piZ
mpc_sin_pirw   Z
mpf_cos_piZ
mpc_cos_pirv   Z	mpf_floorZ	mpc_floorZfloorZmpf_ceilZmpc_ceilZceilZmpf_nintZmpc_nintZnintZmpf_fracZmpc_fracZ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facZ	factorialZmpf_psi0Zmpc_psi0ru   Zmpf_harmonicZmpc_harmonicZharmonicZmpf_eiZmpc_eiZ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)ry   rh   ri   r   rz   rz   r{   rn   `   s   
zMPContext.init_builtinsc                 C   s
   | |S N)r7   )ry   xr}   rz   rz   r{   r7      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)ry   r   yrz   rz   r{   hypot   s   

 zMPContext.hypotc                 C   sr   t | |}|dkr| |S t|dst| j\}}tj||j||dd\}}|d u r2| 	|S | 
||fS )Nr   r   T)r   )int_rer   hasattrNotImplementedErrorr   r   
mpf_expintr   r   r   ry   nzr}   roundingrealimagrz   rz   r{   _gamma_upper_int   s   



zMPContext._gamma_upper_intc                 C   sh   t |}|dkr| |S t|dst| j\}}t||j||\}}|d u r-| |S | 	||fS )Nr   r   )
r   r   r   r   r   r   r   r   r   r   r   rz   rz   r{   _expint_int   s   



zMPContext._expint_intc                 C   st   t |dr)z| tj|j|g| jR  W S  ty(   | jr  |jtjf}Y nw |j	}| 
tj||g| jR  S Nr   )r   r   r   Zmpf_nthrootr   r   r   rg   r   _mpc_r   Zmpc_nthrootry   r   r   rz   rz   r{   _nthroot   s   
 zMPContext._nthrootc                 C   sR   | j \}}t|dr| t||j||S t|dr'| t||j||S d S Nr   r   )	r   r   r   r   Zmpf_besseljnr   r   Zmpc_besseljnr   )ry   r   r   r}   r   rz   rz   r{   _besselj   s   


zMPContext._besseljr   c                 C   s   | j \}}t|dr)t|dr)zt|j|j||}| |W S  ty(   Y nw t|dr5|jtjf}n|j}t|drD|jtjf}n|j}| 	t
||||S r   )r   r   r   Zmpf_agmr   r   r   r   r   r   Zmpc_agm)ry   abr}   r   vrz   rz   r{   _agm   s   
zMPContext._agmc                 C      |  tjt|g| jR  S r   )r   r   Zmpf_bernoullir   r   ry   r   rz   rz   r{   rp         zMPContext.bernoullic                 C   r   r   )r   r   Zmpf_zeta_intr   r   r   rz   rz   r{   	_zeta_int   r   zMPContext._zeta_intc                 C   s4   |  |}|  |}| tj|j|jg| jR  S r   )r   r   r   Z	mpf_atan2r   r   )ry   r   r   rz   rz   r{   rr     s   

 zMPContext.atan2c                 C   sX   |  |}t|}| |r| tj||jg| jR  S | tj	||j
g| jR  S r   )r   r   _is_real_typer   r   Zmpf_psir   r   r   Zmpc_psir   )ry   mr   rz   rz   r{   rq     s
   

zMPContext.psic                 K      t || jvr| |}| |\}}t|dr,t|j||\}}| || |fS t|drEt	|j
||\}}| || |fS | j|fi || j|fi |fS r   )typerk   r   _parse_precr   r   Zmpf_cos_sinr   r   Zmpc_cos_sinr   r   r   r   ry   r   kwargsr}   r   csrz   rz   r{   cos_sin     


$zMPContext.cos_sinc                 K   r   r   )r   rk   r   r   r   r   Zmpf_cos_sin_pir   r   Zmpc_cos_sin_pir   r   r   r   r   rz   rz   r{   cospi_sinpi  r   zMPContext.cospi_sinpic                 C   s   |   }| j|_|S )zP
        Create a copy of the context, with the same working precision.
        )	__class__r}   )ry   r   rz   rz   r{   clone)  s   zMPContext.clonec                 C      t |dst|tu rdS dS )Nr   FTr   r   complexry   r   rz   rz   r{   r   4     zMPContext._is_real_typec                 C   r   )Nr   TFr   r   rz   rz   r{   _is_complex_type9  r   zMPContext._is_complex_typec                 C   sr   t |dr
|jtkS t |drt|jv S t|tst|tjr!dS | |}t |ds0t |dr5| 	|S t
d)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 input)r   r   r!   r   
isinstancer
   r\   rl   r   isnan	TypeErrorr   rz   rz   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)Zisinfr   r   rz   rz   r{   isfiniteZ  s   zMPContext.isfinitec                 C   s   |sdS t |dr|j\}}}}|o|dkS t |dr%|j o$| |jS t|tv r/|dkS t|| jrF|j	\}}|s>dS |dkoE|dkS | | 
|S )z<
        Determine if *x* is a nonpositive integer.
        Tr   r   r   r   )r   r   r   isnpintr   r   r
   r   rl   _mpq_r   )ry   r   signmanr   bcpqrz   rz   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dpsrg   join)ry   linesrz   rz   r{   __str__  s   
zMPContext.__str__c                 C   s
   t | jS r   )r   _precrx   rz   rz   r{   _repr_digits  s   
zMPContext._repr_digitsc                 C   s   | j S r   )Z_dpsrx   rz   rz   r{   _str_digits  s   zMPContext._str_digitsFc                       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 r   rz   r   r   rz   r{   r~         z%MPContext.extraprec.<locals>.<lambda>NPrecisionManagerry   r   normalize_outputrz   r   r{   	extraprec  s   zMPContext.extraprecc                       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                    r   r   rz   dr   rz   r{   r~     r   z$MPContext.extradps.<locals>.<lambda>r   r   rz   r   r{   extradps     zMPContext.extradpsc                    r   )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 r   rz   r   r   rz   r{   r~         z$MPContext.workprec.<locals>.<lambda>Nr   r   rz   r   r{   workprec  s   zMPContext.workprecc                    r   )z
        This function is analogous to workprec (see documentation)
        but changes the decimal precision instead of the number of bits.
        Nc                    r   r   rz   r   r   rz   r{   r~     r   z#MPContext.workdps.<locals>.<lambda>r   r   rz   r   r{   workdps  r   zMPContext.workdpsNrz   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                     s2  j }d u r|}n}z|d _ z	| i |}W n  y*   j}Y nw |d }	 |_ z	| i |}W n  yH   j}Y nw ||krNn9|| | }|| k r`n.rltd||| f  |}||kryd| |t|d 7 }t||}q0W |_ |
 S W |_ |
 S |_ w )N
      r   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maxprec2Zv1Zprec2Zv2errcatchry   fmaxprecverboserz   r{   f_autoprec_wrapped  sZ   





z.MPContext.autoprec.<locals>.f_autoprec_wrappedrz   )ry   r  r  r  r  r  rz   r  r{   autoprec  s   D%zMPContext.autoprec   c                    s   t |trdd fdd|D  S t |tr*dd fdd|D  S t|dr9t|jfi S t|drLd	t|jfi  d
 S t |t	rUt
|S t | jrd|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   $    | ]} j |fi V  qd S r   nstr.0r   ry   r   r   rz   r{   	<genexpr>]     " z!MPContext.nstr.<locals>.<genexpr>z(%s)c                 3   r
  r   r  r  r  rz   r{   r  _  r  r   r   ())r   listr   tupler   r   r   r8   r   r   reprZmatrixZ__nstr__str)ry   r   r   r   rz   r  r{   r  4  s   
( 
 


zMPContext.nstrc                 C   s   |r7t |tr7d| v r7| dd}t|}|d}|s#d}|dd}| | 	|| 	|S t
|drN|j\}}||krJ| |S tdtd	t| )
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rstripri   r   r   r  r   
ValueErrorr   r  )ry   r   stringsr   r  r  r   r   rz   rz   r{   _convert_fallbackj  s   




zMPContext._convert_fallbackc                 O   s   | j |i |S r   )r   )ry   r   r   rz   rz   r{   	mpmathify|  s   zMPContext.mpmathifyc                 C   s   |rD| dr	dS | j\}}d|v r|d }d|v r-|d }|| jkr%dS t|}||fS d|v r@|d }|| jkr<dS t|}||fS | jS )Nexact)r   r  r   r}   r   )getr   r   r   r   )ry   r   r}   r   r   rz   rz   r{   r     s&   



zMPContext._parse_precz'the exact result does not fit in memoryzhypsum() 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r,t|d | j|< | j| }
| j}|d| |}d}d}i }d	}t	|D ]s\}}|| d
kr||kr|d	krd}t	|d | D ]\}}|| d
kry|d	kry||kryd}qe|st
dqI| |\}}t| }| }||kr|d	kr|dkr||v r||  |7  < n|||< t||| d }|t|7 }qI	 ||krt| j||| f || }|rtdd |D }ni }|
||	||||fi |\}}}| }d}||k r| D ]}|d u s	||k rd} nq||d d k p| }|r;|r!n'|d} | d ur;|| kr;|r8| d	S | jS |d9 }|d7 }|d7 }qt|tu r\|rW| |S | |S |S )Nr   Rr   Cr   r  2      r   ZFTzpole in hypergeometric series   <   c                 s   s    | ]}|d fV  qd S r   rz   )r  r   rz   rz   r{   r    s    z#MPContext.hypsum.<locals>.<genexpr>   zeroprecr   )r   r   r   ro   r   Zmake_hyp_summatorr}   r(  r   	enumerateZeroDivisionErrornint_distancer   maxabsr#  _hypsum_msgdictvaluesri   r   r   r  r   r   )!ry   r   r   flagsZcoeffsr   Zaccurate_smallr   keyr   Zsummatorr}   r  r   ZepsshiftZmagnitude_checkZmax_total_jumpir   okiiZccr   r   ZwpZmag_dictZzvZhave_complexZ	magnitudeZcancelZjumps_resolvedZaccurater1  rz   rz   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   rz   rz   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   )ry   r   r   r   rz   rz   r{   frexp   s   
zMPContext.frexpc                 K   s\   |  |\}}| |}t|dr| t|j||S t|dr*| t|j||S t	d)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 numbers)
r   r   r   r   r$   r   r   r=   r   r#  )ry   r   r   r}   r   rz   rz   r{   fneg  s   .


zMPContext.fnegc              	   K      |  |\}}| |}| |}z\t|dr;t|dr)| t|j|j||W S t|dr;| t|j|j||W S t|drdt|drR| t|j|j||W S t|dri| t	|j|j||W S W t
dW t
d t
tfy{   t| jw )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   rB  )r   r   r   r   r%   r   r   rA   r   r@   r#  OverflowError_exact_overflow_msgry   r   r   r   r}   r   rz   rz   r{   faddF  s*   8








zMPContext.faddc              	   K   s   |  |\}}| |}| |}z^t|dr=t|dr)| t|j|j||W S t|dr=| t|jtf|j	||W S t|drft|drT| t
|j	|j||W S t|drk| t|j	|j	||W S W tdW td ttfy}   t| jw )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   rB  )r   r   r   r   r&   r   r   rB   r   r   rC   r#  rE  rF  rG  rz   rz   r{   fsub  s*   0








zMPContext.fsubc              	   K   rD  )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   rB  )r   r   r   r   r'   r   r   rE   r   rD   r#  rE  rF  rG  rz   rz   r{   fmul  s*   3








zMPContext.fmulc                 K   s   |  |\}}|std| |}| |}t|dr@t|dr-| t|j|j||S t|dr@| t|jt	f|j
||S t|drgt|drV| t|j
|j||S t|drg| t|j
|j
||S td)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   rB  )r   r#  r   r   r   r)   r   r   rG   r   r   rH   rG  rz   rz   r{   fdiv  s    1







zMPContext.fdivc                 C   s  t |}|tv rt|| jfS |tju rD|j\}}t||\}}d| |kr+|d7 }n|s2|| jfS tt	|||  t| }||fS t
|drP|j}| j}	n;t
|drs|j\}}
|
\}}}}|rg|| }	n$|
tkro| j}	ntd| |}t
|dst
|dr| |S td|\}}}}|| }|dk rd}|}nW|r|dkr||> }| j}n5|dkr|d? d }d}n(| 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\   rl   r   divmodr6   r6  r   r   r   r   r#  r   r4  r   r5  )ry   r   Ztypxr   r   r   rr   r  Zim_distr  ZisignZimanZiexpZibcr   r   r   r   r   Zre_disttrz   rz   r{   r4  Y  sn   !










zMPContext.nint_distancec                 C   s6   | j }z| j}|D ]}||9 }q	W || _ |
 S || _ w )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   )ry   Zfactorsorigr   r   rz   rz   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   r4   r   rx   rz   rz   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   rz   r{   r~     s    z$MPContext.fraction.<locals>.<lambda>z%s/%s)rj   )ry   r   r   rz   rS  r{   fraction  s   
zMPContext.fractionc                 C      t | |S r   r6  r   r   rz   rz   r{   absmin     zMPContext.absminc                 C   rU  r   rV  r   rz   rz   r{   absmax  rX  zMPContext.absmaxc                 C   s,   t |dr|j\}}| || |gS |S )Nr  )r   r  r   )ry   r   r   r   rz   rz   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                       g | ]}  |qS rz   r   )r  r   rx   rz   r{   
<listcomp>      z+MPContext._zetasum_fast.<locals>.<listcomp>c                    r[  rz   r\  )r  r   rx   rz   r{   r]    r^  )Zisintr   r   r   r   r   Zmpc_zetasumr   )	ry   r   r   r   ZderivativesZreflectr}   ZxsZysrz   rx   r{   _zetasum_fast  s   zMPContext._zetasum_fast)r   F)Nrz   F)r	  )T)8__name__
__module____qualname____doc__rf   rn   r7   r   r   r   r   r   r   rp   r   rr   rq   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r  r  r%  r&  r   rF  r7  r?  r@  rA  rC  rH  rI  rJ  rK  r4  rQ  rR  rT  rW  rY  rZ  r_  rz   rz   rz   r{   rc   :   sp    !V








k6
U6JBEBbrc   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   )ry   precfundpsfunr   )selfry   rf  rg  r   rz   rz   r{   rf     s   
zPrecisionManager.__init__c                    s   t   fdd}|S )Nc                     s   j j}zHjrj jj _n	j jj _jrA | i |}t|tu r9tdd |D W |j _S |
 W |j _S  | i |W |j _S |j _w )Nc                 S   s   g | ]}|
 qS rz   rz   )r  r   rz   rz   r{   r]  '  s    z8PrecisionManager.__call__.<locals>.g.<locals>.<listcomp>)ry   r}   rf  rg  r   r   r   r  )r   r   rP  r   r  rh  rz   r{   g  s   

z$PrecisionManager.__call__.<locals>.g)	functoolswraps)rh  r  rj  rz   ri  r{   __call__  s   zPrecisionManager.__call__c                 C   s<   | j j| _| jr| | j j| j _d S | | j j| j _d S r   )ry   r}   origprf  rg  r   )rh  rz   rz   r{   	__enter__.  s   
zPrecisionManager.__enter__c                 C   s   | j | j_dS rd   )rn  ry   r}   )rh  exc_typeZexc_valexc_tbrz   rz   r{   __exit__4  s   
zPrecisionManager.__exit__Nr`  )ra  rb  rc  rf   rm  ro  rr  rz   rz   rz   r{   r     s
    
r   __main__)wrd  Z__docformat__rk  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.   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\   object__new__newcompiler  Zsage.libs.mpmath.ext_mainr]   re   ZlibsZmpmathZext_mainZ_mpf_moduler_   r^   r`   ra   rb   rc   r   ra  ZdoctestZtestmodrz   rz   rz   r{   <module>   sD    ^ 
         d
$