o
    lhU                  	   @   s  d Z ddlZddlmZ ddlmZ ddlmZmZmZmZm	Z	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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: e
d	krd
Z;ndZ;dZ<e
d	krdZ=ndZ=dZ>i Z?dZ@i ZAdZBdZCi ZDdZEdZFdZGi ZHddgZIedeeBd D ]ZJeIeKdeJ eBd gdeJd   7 ZIqdd ZLdd ZMdd ZNdd ZOdd d!ZPeLd"d# ZQeLd$d% ZR	 ed&ZSed'ZTed(ZUed)ZVd*d+ ZWeLdd,d-ZXd.d/ ZYd0d1 ZZeLd2d3 Z[eLd4d5 Z\eMe\Z]eMeXZ^eMe[Z_eMeYZ`eMeQZaeMeRZbeLd6d7 ZceLd8d9 ZdeMedZeeMecZfefd:d;Zgd<d= Zhd>d? Ziefd@dAZjefdBdCZkddDdEZldFdG ZmdHdI ZnddJdKZodLdM ZpefdNdOZqdPdQ ZrdRdS ZsdTdU ZtdVdW ZudXdY ZvefdZd[Zwefd\d]Zxefd^d_Zyefd`daZzefdbdcZ{efdddeZ|efdfdgZ}efdhdiZ~ddjdkZdldm Zdndo Zdpdq ZefdrdsZedfdtduZdvdw ZeddfdxdyZefdzd{Zefd|d}Zefd~dZefddZefddZefddZefddZefddZefddZdddZdddZe
dkrtz*ddlm  m  mZ ej4Z4ejZejqZqejZejZejgZgejZejZejlZlW dS  eefys   ed Y dS w dS )a(  
This module implements computation of elementary transcendental
functions (powers, logarithms, trigonometric and hyperbolic
functions, inverse trigonometric and hyperbolic) for real
floating-point numbers.

For complex and interval implementations of the same functions,
see libmpc and libmpi.

    N)bisect   )xrange)MPZMPZ_ZEROMPZ_ONEMPZ_TWOMPZ_FIVEBACKEND)-round_floorround_ceiling
round_downround_upround_nearest
round_fastComplexResultbitcountbctablelshiftrshiftgiant_steps
sqrt_fixedfrom_intto_intfrom_man_expto_fixedto_float
from_floatfrom_rational	normalizefzerofonefnonefhalffinffninffnanmpf_cmpmpf_signmpf_absmpf_posmpf_negmpf_addmpf_submpf_mulmpf_div	mpf_shiftmpf_rdiv_intmpf_pow_intmpf_sqrtreciprocal_rndnegative_rndmpf_perturb
isqrt_fast)ifibpythonX  i  i        i  i	  	      i           c                    s,   d _ d _ fdd} j|_ j|_|S )z
    Decorator for caching computed values of mathematical
    constants. This decorator should be applied to a
    function taking a single argument prec as input and
    returning a fixed-point value with the given precision.
    Nc                    sR    j }| |kr j||  ? S t| d d } |fi | _| _  j||  ? S )Ng?
   )	memo_precmemo_valint)preckwargsrD   newprecf j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/mpmath/libmp/libelefun.pyg^   s   zconstant_memo.<locals>.g)rD   rE   __name____doc__)rK   rN   rL   rJ   rM   constant_memoU   s   rQ   c                    s   t f fdd	} j|_|S )z
    Create a function that computes the mpf value for a mathematical
    constant, given a function that computes the fixed-point value.

    Assumptions: the constant is positive and has magnitude ~= 1;
    the fixed-point function rounds to floor.
    c                    s<   | d } |}|t tfv r|d7 }td|| t|| |S )Nr>   r   r   )r   r   r   r   )rG   rndwpvfixedrL   rM   rK   r   s
   zdef_mpf_constant.<locals>.f)r   rP   )rV   rK   rL   rU   rM   def_mpf_constantj   s   rW   c                 C   s   || dkr't d| d }|s|d@ rt|| d  |fS t || d  |fS || d }t| |||\}}}t| |||\}	}
}|
| ||	  ||
 || fS )Nr   rA      )r   r   bsp_acot)qab
hyperbolica1mp1q1r1p2q2r2rL   rL   rM   rY   {   s   rY   c                 C   sB   t d| t|  d }t| d||\}}}|| |> ||   S )z
    Compute acot(a) or acoth(a) for an integer a with binary splitting; see
    http://numbers.computation.free.fr/Constants/Algorithms/splitting.html
    ffffff?r>   r   )rF   mathlogrY   )r[   rG   r]   NprZ   rrL   rL   rM   
acot_fixed   s   rl   Fc                 C   s>   d}t }| D ]\}}|t|tt||| | 7 }q||? S )z
    Evaluate a Machin-like formula, i.e., a linear combination of
    acot(n) or acoth(n) for specific integer values of n, using fixed-
    point arithmetic. The input should be a list [(c, n), ...], giving
    c*acot[h](n) + ...
    rC   )r   r   rl   )coefsrG   r]   	extraprecsr[   r\   rL   rL   rM   machin   s
   "rp   c                 C      t g d| dS )zz
    Computes ln(2). This is done with a hyperbolic Machin-type formula,
    with binary splitting at high precision.
    ))      )i  )r<   i-"  Trp   rG   rL   rL   rM   	ln2_fixed   s   rw   c                 C   rq   )zN
    Computes ln(10). This is done with a hyperbolic Machin-type formula.
    )).      )"   1   )r>      Tru   rv   rL   rL   rM   
ln10_fixed   s   r}   iqc i-~ i@	    c                 C   s   ||  dkr1t d| d d| d  d| d  }|d td  d }d| | tt|   }n:|r=|dk r=td	| | | | d }t| ||d |\}}	}
t|||d |\}}}|	| }|| }|
| ||  }|||fS )
z
    Computes the sum from a to b of the series in the Chudnovsky
    formula. Returns g, p, q where p/q is the sum as an exact
    fraction and g is a temporary value used to save work
    for recursive calls.
    r         rA   rX      rB      z  binary splitting)r   CHUD_CCHUD_ACHUD_Bprintbs_chudnovsky)r[   r\   levelverboserN   rj   rZ   midg1r`   ra   g2rc   rd   rL   rL   rM   r      s   (
r   c           	      C   sf   t | d d d }|rtd| td|d|\}}}ttd|  > }|t | |t|  t  }|S )z
    Compute floor(pi * 2**prec) as a big integer.

    This is done using Chudnovsky's series (see comments in
    libelefun.py for details).
    gv	O
@g bi ],@rA   zbinary splitting with N =r   )rF   r   r   r7   r   r   CHUD_D)	rG   r   verbose_baseri   rN   rj   rZ   sqrtCrT   rL   rL   rM   pi_fixed   s   	
r   c                 C   s   t | d S )N   )r   rv   rL   rL   rM   degree_fixed   s   r   c                 C   sT   ||  dkrt t|fS | | d }t| |\}}t||\}}|| | || fS )ze
    Sum series for exp(1)-1 between a, b, returning the result
    as an exact fraction (p, q).
    r   rA   )r   r   bspe)r[   r\   r_   r`   ra   rc   rd   rL   rL   rM   r      s   r   c                 C   s8   t d|  t|  d }td|\}}|| | > | S )z
    Computes exp(1). This is done using the ordinary Taylor series for
    exp, with binary splitting. For a description of the algorithm,
    see:

        http://numbers.computation.free.fr/Constants/
            Algorithms/splitting.html
    g?r>   r   )rF   rg   rh   r   )rG   ri   rj   rZ   rL   rL   rM   e_fixed	  s   r   c                 C   s(   | d7 } t td|  > t| >  }|d? S )z2
    Computes the golden ratio, (1+sqrt(5))/2
    rC   rA      )r7   r	   r   )rG   r[   rL   rL   rM   	phi_fixed  s   r   c                 C   s&   | d }t ttt|d|| d S )NrC   r   )r   mpf_logr0   mpf_pi)rG   rS   rL   rL   rM   ln_sqrt2pi_fixed*  s   r   c                 C   s   t t| | S N)r   r   rv   rL   rL   rM   sqrtpi_fixed0     r   c                 C   s   | \}}}}|\}}	}
}|r|
dk rt d|
dkr't| d| |	|
>  ||S |
dkre|	dkrF|r@ttt| |d t| ||S t| ||S |rXtt| |d t| |	 ||S tt| |d ||	||S t| |d |}tt||||S )zV
    Compute s**t. Raises ComplexResult if s is negative and t is
    fractional.
    r   z,negative number raised to a fractional powerrB   r   rC   )	r   r2   r/   r!   r3   r4   r   mpf_expr.   )ro   trG   rR   ssignsmansexpsbctsigntmantexptbccrL   rL   rM   mpf_pow>  s0   r   c           
      C   s  |dkr
| |  dfS t | }d}d|dt |  d  }t\}}}}		 |d@ r\||  }|| }|	|d 7 }	|	tt||	?   }	|	|krQ||	| ? }||	| 7 }|}	|d8 }|s\	 ||fS | |  } || }|| d }|tt| |?   }||kr| || ? } ||| 7 }|}|d }q#)zn-th power of a fixed point number with precision prec

       Returns the power in the form man, exp,
       man * 2**exp ~= y**n
    rA   r   r   r   )r   r!   r   rF   )
ynrG   bcexpworkprec_pmpepbcrL   rL   rM   int_pow_fixedZ  s<   
r   c                 C   s  d}zt | |||  }tt|d|  }W n" ty9   t||}t|}td||}t|||}t|}Y nw d}|}	|}
t||| D ]9}t	||d |
\}}t ||d |
 | | |	 }t
| d| | |	 | }||d t
|||
   | }|}
qG|S )N2   g      ?r   rC   rA   )r   r   rF   OverflowErrorr   r1   r   r   r   r   r   )r   r   rG   exp1starty1rk   fnextraextra1prevprj   r   r   re   BrL   rL   rM   nthroot_fixed  s*   
r   c                 C   sl  | \}}}}|rt d|s2| tkrtS | tkr&|dkrtS |dkr$tS tS |s*tS |dk r0tS tS d}|dk rb|dkr>tS |dkrHt| ||S |dkrStt| ||S t| }d}d}	||	7 }| }|d	kr|d
ksv|tdd|d   k r|d }
t	|}t
d||
}t| ||
|}t|d |d |d |d ||} |rtt| ||	 |S | S |d|  ||  }
|dkr|
|
d 7 }
|
|
|  }
||
 }d}|| }|dk rd}| }|r||| 7 }n||| 8 }t||}d}|| |d |
  | | }d}|r|dks	|dkrd}n|dks|dkrd}t|| ||
|}t||||} |r4tt| ||	 |S | S )zanth-root of a positive number

    Use the Newton method when faster, otherwise use x**(1/n)
    znth root of a negative numberr   FrA   r   rB   Tr   r>   i N     gL<@gףp=
?rC   rX   ur   drK   )r   r&   r    r!   r$   r*   r/   r4   rF   r   r1   r   r   r   r   r   )ro   r   rG   rR   signmanr   r   flag_inverseextra_inverseprec2r   nthrk   shiftsign1esr   r   	rnd_shiftrL   rL   rM   mpf_nthroot  s   ("
r   c                 C   s   t | d||S )zcubic root of a positive numberrX   )r   )ro   rG   rR   rL   rL   rM   mpf_cbrt  r   r   c           	      C   s   | t v rt |  \}}||kr||| ? S |d }|tkr8|du r$t|}t| }| || > }t||||  }nttt| |d |}| tk rN||ft | < ||| ? S )z`
    Fast computation of log(n), caching the value for small n,
    intended for zeta sums.
    rC   Nr   )	log_int_cacheLOG_TAYLOR_SHIFTrw   r   log_taylor_cachedr   r   r   MAX_LOG_INT_CACHE)	r   rG   ln2valuevprecrS   rk   xrT   rL   rL   rM   log_int_fixed  s   r   c                 C   sH   d}	 | | d? }|dkrt | | dk r| S t| | }|} |d7 }q)z^
    Fixed-point computation of agm(a,b), assuming
    a, b both close to unit magnitude.
    r   r   r   r<   )absr7   )r[   r\   rG   ianewrL   rL   rM   	agm_fixed  s   r   c                 C   s   | |  |? }| } }}|r || |? }|| |? }||7 }|s|t |> 7 }|| |d ? }|t| |>  |? }|  } }}|rR|| |? }|| |? }||7 }|s@t |> |d>  }|| |? }t|||}t||> | S )a*  
    Fixed-point computation of -log(x) = log(1/x), suitable
    for large precision. It is required that 0 < x < 1. The
    algorithm used is the Sasaki-Kanada formula

        -log(x) = pi/agm(theta2(x)^2,theta3(x)^2). [1]

    For faster convergence in the theta functions, x should
    be chosen closer to 0.

    Guard bits must be added by the caller.

    HYPOTHESIS: if x = 2^(-n), n bits need to be added to
    account for the truncation to a fixed-point number,
    and this is the only significant cancellation error.

    The number of bits lost to roundoff is small and can be
    considered constant.

    [1] Richard P. Brent, "Fast Algorithms for High-Precision
        Computation of Elementary Functions (extended abstract)",
        http://wwwmaths.anu.edu.au/~brent/pd/RNC7-Brent.pdf

    rA   r   )r   r7   r   r   )r   rG   x2ro   r[   r\   r   rj   rL   rL   rM   log_agm)  s(   r   c                 C   s   t |D ]}t| |> } qt|> }| | |> | |  }|dk }|r$| }|| |? }|| |? }|}	|d }
|| |? }d}|r\|	|| 7 }	|d7 }|
|| 7 }
|| |? }|d7 }|s@|
| |? }
|	|
 d| > }|ro| S |S )a:  
    Fixed-point calculation of log(x). It is assumed that x is close
    enough to 1 for the Taylor series to converge quickly. Convergence
    can be improved by specifying r > 0 to compute
    log(x^(1/2^r))*2^r, at the cost of performing r square roots.

    The caller must provide sufficient guard bits.
    r   rX   r   rA   r   )r   r7   r   )r   rG   rk   r   onerT   r   v2v4s0s1kro   rL   rL   rM   
log_taylorX  s2   	r   c                 C   s&  | |t  ? }t| }|| }||ftv rt||f \}}n||t  > }t||d}||ft||f< ||L }||L }| | |> | }||> t|> |  }|| |? }	|	|	 |? }
|}|d }||
 |? }d}|r||| 7 }|d7 }||| 7 }||
 |? }|d7 }|sg||	 |? }|| d> }|| S )zd
    Fixed-point computation of log(x), assuming x in (0.5, 2)
    and prec <= LOG_TAYLOR_PREC.
    r<   rX   r   rA   r   )r   cache_prec_stepslog_taylor_cacher   r   )r   rG   r   cached_precdprecr[   log_ar   rT   r   r   r   r   r   ro   rL   rL   rM   r   z  s8   r   c                 C   s  | \}}}}|s| t krtS | tkrtS | tkrtS |r td|d }|dkr8|s,t S t|t| | ||S || }t|}	|	dkr{d|	 }
|
rQt|> | }n|t|d >  }t	|}|| }||krwt
|
||	| ||d}t||
||S ||7 }|	dkrt	|	|krt|t| | ||S |tkrtt||| |}|r||t| 7 }n$| t }|| }t| |} || 7 }tt| || }||t| 8 }t|| ||S )zj
    Compute the natural logarithm of the mpf value x. If x is negative,
    ComplexResult is raised.
    zlogarithm of a negative numberr>   r   r   i'  )r    r%   r$   r&   r   r   rw   r   r   r   r   r6   LOG_TAYLOR_PRECr   r   LOG_AGM_MAG_PREC_RATIOr0   r   r   )r   rG   rR   r   r   r   r   rS   magabs_magr   r   r   cancellationr   r_   optimal_magr   rL   rL   rM   r     sP   


r   c           
      C   s  |d s	|| } }| d s;|d s'| |  krt krtS  t| |fv r%tS tS | t kr3tt|||S | tkr9tS tS t| | }t||}d}t|||| }t|td}|d |d  }	|t ksh|	| d k ryt|||| t	|d |d  }t
t|||dS )z1
    Computes log(sqrt(a^2+b^2)) accurately.
    r   r>   rC   rA   rX   rB   )r    r%   r&   r$   r   r)   r.   r,   r"   minr0   )
r[   r\   rG   rR   a2b2r   h2	cancelledmag_cancelledrL   rL   rM   mpf_log_hypot  s0   


"r   c           
      C   s   |dkrt t| |d ? d }nt t| d|  }d}tt|d d| ? }d}t||D ]4}||7 }||| > }t||\}}||> | }|t| ||  |> t|> |d |?   }	||	 }|}q2t||| S )Nd   5   g      @Cg       @r   rA   )rg   atanrF   r   r   cos_sin_fixedr   r   )
r   rG   rk   r   extra_prS   cossintanr[   rL   rL   rM   atan_newton  s   *r   c                 C   sp   dt |d > d }|| }| |ftv rt| |f \}}n| |t > }t||}||ft| |f< ||? ||? fS )Nr   r>   )r   atan_taylor_cacheATAN_TAYLOR_SHIFTr   )r   rG   r   r   r[   atan_arL   rL   rM   atan_taylor_get_cached"  s   
r  c                 C   s   | |t  ? }t||\}}| | }||> |d |? || |?  t|>    }}|d |? }|| |? }	|d }
||	 |? }d}|r]||| 7 }|d7 }|
|| 7 }
||	 |? }|d7 }|sA|
| |? }
||
 }|| S )NrA   rX   r   )r   r  r   )r   rG   r   r[   r   r   r   rT   r   r   r   r   ro   rL   rL   rM   atan_taylor1  s&   ,r  c                 C   s,   | s
t t||dS tt t|t| dS )NrB   )r0   r   r+   r5   )r   rG   rR   rL   rL   rM   atan_infE  s   r  c                 C   s  | \}}}}|s$| t krt S | tkrtd||S | tkr"td||S tS || }||d kr4t|||S | |d krDt| d| ||S |d t| }|dkrYtd| |} d}	nd}	t| |}
|re|
 }
|t	k rot
|
|}nt|
|}|	rt|d? d | }|r| }t|| ||S )Nr   r   r>      rA   TF)r    r$   r  r%   r&   r6   r   r1   r   ATAN_TAYLOR_PRECr  r   r   r   )r   rG   rR   r   r   r   r   r   rS   
reciprocalr   r[   rL   rL   rM   mpf_atanJ  s6   

r  c                 C   s>  |\}}}}| \}}	}
}|	sK| t kr#|tkr#t|dkrt S t||S | ttfv rI|ttfv r1tS | tkr=tt||dS ttt|t| dS tS |rZtt	t| ||t| S |s|tkrbtS |tkrht S |tkrqt||S | t krwt S tt||dS t
t| ||d |d }|rtt|d |||S t|||S )Nr   rB   r   )r    r&   r(   r   r$   r%   r0   r+   r5   	mpf_atan2r  r/   r,   r*   )r   r   rG   rR   xsignxmanxexpxbcysignymanyexpybctquorL   rL   rM   r  m  s<   

r  c                 C   sv   | \}}}}|| dkr| t tfvrtd|d }t| | }tt ttt ||||}	t| |	|}
tt	|
||dS )Nr   z%asin(x) is real only for -1 <= x <= 1   r   )
r!   r"   r   r.   r,   r3   r-   r/   r0   r  r   rG   rR   r   r   r   r   rS   r[   r\   r   rL   rL   rM   mpf_asin  s   
r  c                 C   s   | \}}}}|| dkr| t tfvrtd| tkrt||S |d }t| | }ttt |||}	t|	tt | ||}
t	t
|
||dS )Nr   z%acos(x) is real only for -1 <= x <= 1r  r   )r!   r"   r   r   r.   r3   r-   r/   r,   r0   r  r  rL   rL   rM   mpf_acos  s   

r  c           
      C   s   |d }| \}}}}|| }|dk r%|| k r t | d| ||S || 7 }ttt| | t||}	tt| |	|}	|rEtt|	|t| S t|	||S )Nr>   r   )	r6   r3   r,   r.   r!   r)   r+   r   r5   )
r   rG   rR   rS   r   r   r   r   r   rZ   rL   rL   rM   	mpf_asinh  s   

r  c                 C   sJ   |d }t | tdkrtdttt| | t||}tt| ||||S )Nr  rB   z acosh(x) is real only for x >= 1)r'   r!   r   r3   r,   r.   r"   r   )r   rG   rR   rS   rZ   rL   rL   rM   	mpf_acosh  s
   r  c                 C   s   | \}}}}|s|r| t tfv r| S td|| }|dkr0|dkr,|dkr,ttg| S td|d }|dk rI|| k rDt| |||S || 7 }t| t|}	tt| |}
t	t
t|	|
|||dS )Nz&atanh(x) is real only for -1 <= x <= 1r   r   r  r  rB   )r    r&   r   r$   r%   r6   r,   r!   r-   r0   r   r/   )r   rG   rR   r   r   r   r   r   rS   r[   r\   rL   rL   rM   	mpf_atanh  s$   

r  c                 C   s   | \}}}}|s| t krtS | S t|| }|dkr.|dk s$|t|kr.ttt| ||S || d }t|}	tt	|	dt
|}
t|	| |}t| |}t|||}t|||}t||
||}|S )Nr   rC   r>   r   )r%   r&   r   r   r   r8   r   mpf_phir,   r0   r"   r   
mpf_cos_pir/   r-   )r   rG   rR   r   r   r   r   sizerS   r[   r\   r   rT   rL   rL   rM   mpf_fibonacci  s$   
r  c                 C   s  | dk r
|  } d}nd}t d|d  }t| | }td|| }ddt||   }|| }| || K } t|> }|dk}	|tk r| |  |?  }
}|
|
 |? }t }}d}|r||d |  }||7 }|d7 }||d |  }||7 }|d7 }|| |? }|sW|
| |? }|	r|| | }n|| | }n~t d|d  }| |  |?  }
}||
g}td|D ]}||d |
 |?  qtg| }d}|rt|D ]%}||d |  }|	r|d@ r||  |8  < n||  |7  < |d7 }q||d  |? }|std|D ]}|| ||  |? ||< qt|| }|dkr@t	|| ||>  }|r*|| }n|| }t|D ]	}|| |? }q2||? S |d }t|D ]}|| |? | }qHt	t
||> ||  }|rf| }||? ||? fS )	z
    Taylor series for cosh/sinh or cos/sin.

    type = 0 -- returns exp(x)  (slightly faster than cosh+sinh)
    type = 1 -- returns (cosh(x), sinh(x))
    type = 2 -- returns (cos(x), sin(x))
    r   r         ?rC   rA   g333333?rf   rB   )rF   r   maxr   EXP_SERIES_U_CUTOFFr   r   appendsumr7   r   )r   rG   typer   rk   xmagr   rS   r   altr   r[   x4r   r   r   r   r   xpowersr   sumsro   rT   pshiftrL   rL   rM   exponential_series  sv     



r*  c           
      C   s   |t kr
t| |dS t|d }||7 }t|>  }}d}| |  |?  }}|rF|| }||7 }|d7 }|| }||7 }|d7 }|| |? }|s&||  |? }|| }|}	|r`|| |? }|d8 }|sT||	? S )z
    Compute exp(x) as a fixed-point number. Works for any x,
    but for speed should have |x| < 1. For an arbitrary number,
    use exp(x) = exp(x-m*log(2)) * 2^m where m = floor(x/log(2)).
    r   r  rA   r   )EXP_COSH_CUTOFFr*  rF   r   )
r   rG   rk   r   r   r   r[   r   ro   r   rL   rL   rM   exp_basecase>  s(   r,  c                 C   sJ   |t krt| |d\}}|| || fS t| |}t|| > | }||fS )z(
    Computation of exp(x), exp(-x)
    r   )r+  r*  r,  r   )r   rG   coshsinhr[   r\   rL   rL   rM   exp_expneg_basecaseW  s   
r/  c                 C   s4  |t kr
t| |dS |t }| |? }t|}|tvr6|dt  t > }t|dt  d\}}|d? |d? ft|< t| \}}t | }||L }||L }| ||> 8 } t|> }	| }
d}| |  |?  }|r|| }|	|7 }	|d7 }||  |? }|| }|
|7 }
|d7 }||  |?  }|s_|	| |
|  |? |
| |	|  |? fS )z
    Compute cos(x), sin(x) as fixed-point numbers, assuming x
    in [0, pi/2). For an arbitrary number, use x' = x - m*(pi/2)
    where m = floor(x/(pi/2)) along with quarter-period symmetries.
    rA   rC   r   )COS_SIN_CACHE_PRECr*  COS_SIN_CACHE_STEPrF   cos_sin_cacher   )r   rG   precsr   r   wcos_tsin_toffsetr   r   r   r[   rL   rL   rM   cos_sin_basecaseb  s.   $&(r8  c                 C   s0  | \}}}}|r|| }|d }|r| }|dkr0|dkr0t |td|  }	t|	||> ||S || k r<tt|||S |dkrj|| }
||
 }|dkrQ||> }n|| ? }t|
}t||\}}t|}||L }n|| }|dkrw||> }n|| ? }d}t||}t||| ||S |stS | t	krt
S | S )N   r:   r   g333333?r   )mpf_erF   r2   r6   r!   rw   divmodr,  r   r%   r    )r   rG   rR   r   r   r   r   r   rS   ewpmodr7  r   lg2r   rL   rL   rM   r     sB   






r   c                 C   s<  | \}}}}|s.|r.|r| t krtS | tkrtS tS | t kr"t t fS | tkr*t tfS ttfS || }|d }	|dk ra||	 k r\|rJt| d| ||S ttd||}
t| |||}|
|fS |	| 7 }	|dkrdd|d >  |	kr|r~tttg| d| ||S ttt| ||d }}|rt	|}||fS |dkr|	| }|| }|dkr||> }n|| ? }t
|}t||\}}t|}||L }n||	 }|dkr||> }n|| ? }d}t||	\}}||d| ?  }
||d| ?  }|r| }|r||	> |
 }t||	 ||S t|
||	 d ||}
t|||	 d ||}|
|fS )	z4Simultaneously compute (cosh(x), sinh(x)) for real xr9  r   r   rC   rX   rB   rA   )r$   r!   r%   r"   r&   r6   r0   r   r)   r+   rw   r;  rF   r/  r   )r   rG   rR   tanhr   r   r   r   r   rS   r-  r.  r   ro   r=  r7  r   r>  r   r[   r\   rL   rL   rM   mpf_cosh_sinh  sj   






rA  c                 C   s   |dkrZd}	 d|> }|| | }t |d }|d? }|| }	|	dkr(| |	> }
n| |	 ? }
t|
|\}}||kr=|| }n|}||| d ? rTt|}||? }
|| }n|d7 }qn|| 7 }|| }	|	dkrl| |	> }
n| |	 ? }
d}|
||fS )Nr   r   r>   rC   )r   r;  rF   )r   r   r   rS   r   cancellation_precr=  pi2pi4r7  r   r   r   smallrL   rL   rM   mod_pi2  s<   






rF  c                 C   s  | \}}}}|s/|rt t }	}
ntt}	}
|dkr|	|
fS |dkr#|	S |dkr)|
S |dkr/|
S || }|d }|dk rx|| k rx|rIt| t|} ttd||}	t| d| ||}
|dkra|	|
fS |dkrg|	S |dkrm|
S |dkrxt| |||S |r|dkr|dkrt}	ttft|d@ |A  }
n|dkrtt}	}
ntt}	}
|dkr|	|
fS |dkr|	S |dkr|
S |dkrt|
|	||S || d ? d d? }||| d >  }t	|| }|d | }|| }|dkr||> }n|| ? }|t
| |? }n
t||||\}}}t||\}	}
|d@ }|dkr|
 |	}	}
n|dkr(|	 |
 }	}
n|dkr3|
|	 }	}
|r9|
 }
|dkrRt|	| ||}	t|
| ||}
|	|
fS |dkr_t|	| ||S |dkrlt|
| ||S |dkrxt|
|	||S dS )z
    which:
    0 -- return cos(x), sin(x)
    1 -- return cos(x)
    2 -- return sin(x)
    3 -- return tan(x)

    if pi=True, compute for pi*x
    r   r   rA   rX   rC   rB   N)r&   r!   r    r.   r   r6   r"   boolr/   r   r   rF  r8  r   r   )r   rG   rR   whichpir   r   r   r   r   ro   r   rS   r   mag2r7  r   r_   rL   rL   rM   mpf_cos_sin  sz   









rK  c                 C      t | ||dS Nr   rK  r   rG   rR   rL   rL   rM   mpf_cosb      rP  c                 C   rL  )NrA   rN  rO  rL   rL   rM   mpf_sinc  rQ  rR  c                 C   rL  )NrX   rN  rO  rL   rL   rM   mpf_tand  rQ  rS  c                 C      t | ||ddS )Nr   r   rN  rO  rL   rL   rM   mpf_cos_sin_pie      rU  c                 C   s   t | ||ddS rM  rN  rO  rL   rL   rM   r  f  rV  r  c                 C   rT  )NrA   r   rN  rO  rL   rL   rM   
mpf_sin_pig  rV  rW  c                 C      t | ||d S Nr   rA  rO  rL   rL   rM   mpf_coshh  rV  r[  c                 C   rX  rM  rZ  rO  rL   rL   rM   mpf_sinhi  rV  r\  c                 C   s   t | ||ddS )Nr   )r@  rZ  rO  rL   rL   rM   mpf_tanhj  rV  r]  c                 C   s   |d u r
t |d }t| |\}}t|}t||\}}|d@ }|dkr(||fS |dkr1| |fS |dkr;| | fS |dkrD|| fS d S )Nr   rX   r   rA   )r   r;  rF   r8  )r   rG   rC  r   r   r   ro   r_   rL   rL   rM   r   o  s   r   c                 C   sJ   |d u rt |}t| |\}}t|}t||}|dkr ||> S || ? S rY  )rw   r;  rF   r,  )r   rG   r   r   r   rT   rL   rL   rM   	exp_fixed{  s   

r^  sagez)Warning: Sage imports in libelefun failed)F)FNr   )r   )rP   rg   r   backendr   r   r   r   r   r	   r
   libmpfr   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   
libintmathr8   r+  r   r0  r1  r2  r   r   r   r   r   r   r  r   r   r   r   r   rQ   rW   rY   rl   rp   rw   r}   r   r   r   r   r   r   r   r   r   r   r  r   r:  
mpf_degreempf_ln2mpf_ln10r   r   
mpf_sqrtpimpf_ln_sqrt2pir   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/  r8  r   rA  rF  rK  rP  rR  rS  rU  r  rW  r[  r\  r]  r   r^  sage.libs.mpmath.ext_libmplibsmpmath	ext_libmp_lbmpImportErrorAttributeErrorr   rL   rL   rL   rM   <module>   s     
&	
	





	6S

/""H,##
K-C$O


