
'k[c           @   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/ m0 Z0 m1 Z1 m2 Z2 m3 Z3 m4 Z4 m5 Z5 m6 Z6 m7 Z7 m8 Z8 d d l9 m: Z: e
 d	 k rd
 Z; n d Z; d Z< e
 d	 k rd Z= n d Z= d Z> i  Z? d Z@ i  ZA d ZB d ZC i  ZD d ZE d ZF d ZG i  ZH d d g ZI xK e d e eB  d  D]0 ZJ eI eK d eJ eB  d g d eJ d 7ZI q,Wd   ZL d   ZM d   ZN d   ZO eP d  ZQ eL d    ZR eL d    ZS e d  ZT e d  ZU e d   ZV e d!  ZW d"   ZX eL eP eY d#   ZZ d$   Z[ d%   Z\ eL d&    Z] eL d'    Z^ eM e^  Z_ eM eZ  Z` eM e]  Za eM e[  Zb eM eR  Zc eM eS  Zd eL d(    Ze eL d)    Zf eM ef  Zg eM ee  Zh e d*  Zi d+   Zj d,   Zk e d-  Zl e d.  Zm eY d/  Zn d0   Zo d1   Zp d2 d3  Zq d4   Zr e d5  Zs d6   Zt d7   Zu d8   Zv d9   Zw d:   Zx e d;  Zy e d<  Zz e d=  Z{ e d>  Z| e d?  Z} e d@  Z~ e dA  Z e dB  Z d2 dC  Z dD   Z dE   Z dF   Z e dG  Z e d2 dH  Z dI   Z e d2 eP dJ  Z e dK  Z e dL  Z e dM  Z e dN  Z e dO  Z e dP  Z e dQ  Z e dR  Z e dS  Z eY dT  Z eY dU  Z e
 dV k r%yj d d l j j j Z e j4 Z4 e j Z e js Zs e j Z e j Z e ji Zi e j Z e j Z e jn Zn Wn e e f k
 r!dW GHn Xn  d S(X   s(  
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.

iN(   t   bisecti   (   t   xrange(   t   MPZt   MPZ_ZEROt   MPZ_ONEt   MPZ_TWOt   MPZ_FIVEt   BACKEND(-   t   round_floort   round_ceilingt
   round_downt   round_upt   round_nearestt
   round_fastt   ComplexResultt   bitcountt   bctablet   lshiftt   rshiftt   giant_stepst
   sqrt_fixedt   from_intt   to_intt   from_man_expt   to_fixedt   to_floatt
   from_floatt   from_rationalt	   normalizet   fzerot   fonet   fnonet   fhalft   finft   fninft   fnant   mpf_cmpt   mpf_signt   mpf_abst   mpf_post   mpf_negt   mpf_addt   mpf_subt   mpf_mult   mpf_divt	   mpf_shiftt   mpf_rdiv_intt   mpf_pow_intt   mpf_sqrtt   reciprocal_rndt   negative_rndt   mpf_perturbt
   isqrt_fast(   t   ifibt   pythoniX  i  i  i   i   i  i	  i	   i   i  i   i   i   c            s=   d   _  d   _   f d   }   j | _   j | _ | S(   s   
    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.
    ic            sb     j  } |  | k r$   j | |  ?St |  d d  }   | |    _ |   _    j | |  ?S(   Ng?i
   (   t	   memo_prect   memo_valt   int(   t   prect   kwargsR7   t   newprec(   t   f(    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   g^   s    		N(   R7   t   NoneR8   t   __name__t   __doc__(   R=   R>   (    (   R=   s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   constant_memoU   s    		c            s"   t    f d  }   j | _ | S(   s   
    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            sU   |  d }   |  } | t  t f k r5 | d 7} n  t d | | t |  |  |  S(   Ni   i   i    (   R   R	   R   R   (   R:   t   rndt   wpt   v(   t   fixed(    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyR=   r   s
    
(   R   RA   (   RF   R=   (    (   RF   s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   def_mpf_constantj   s    c         C   s   | | d k rb t  d | d  } | s4 | d @rI t | |  d | f St | |  d | f Sn  | | d } t |  | | |  \ } } } t |  | | |  \ }	 }
 } |
 | | |	 | |
 | | f S(   Ni   i   i   (   R   R   t   bsp_acot(   t   qt   at   bt
   hyperbolict   a1t   mt   p1t   q1t   r1t   p2t   q2t   r2(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyRH   {   s    c         C   sS   t  d | t j |   d  } t |  d | |  \ } } } | | | >| |  S(   s   
    Compute acot(a) or acoth(a) for an integer a with binary splitting; see
    http://numbers.computation.free.fr/Constants/Algorithms/splitting.html
    gffffff?i   i    (   R9   t   matht   logRH   (   RJ   R:   RL   t   Nt   pRI   t   r(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt
   acot_fixed   s    !c         C   sU   d } t  } x> |  D]6 \ } } | t |  t t |  | | |  7} q W| | ?S(   s   
    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) + ...
    i
   (   R   R   RZ   (   t   coefsR:   RL   t	   extraprect   sRJ   RK   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   machin   s
    .c         C   s   t  d d d	 g |  t  S(
   sz   
    Computes ln(2). This is done with a hyperbolic Machin-type formula,
    with binary splitting at high precision.
    i   i   ii  i   i-"  (   i   i   (   ii  (   i   i-"  (   R^   t   True(   R:   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt	   ln2_fixed   s    c         C   s   t  d d d	 g |  t  S(
   sN   
    Computes ln(10). This is done with a hyperbolic Machin-type formula.
    i.   i   i"   i1   i   i   (   i.   i   (   i"   i1   (   i   i   (   R^   R_   (   R:   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt
   ln10_fixed   s    iqc i-~ i@	 i   c         C   s  | |  d k ro t  d | d d | d d | d  } | d t d d } d | | t t | } n | r | d k  r d	 |  | f GHn  |  | d } t |  | | d |  \ } }	 }
 t | | | d |  \ } } } |	 | } | | } |
 | | | } | | | f S(
   s   
    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.
    i   i   i   i   i   i   ii   s     binary splitting(   R   t   CHUD_Ct   CHUD_At   CHUD_Bt   bs_chudnovsky(   RJ   RK   t   levelt   verboseR>   RX   RI   t   midt   g1RO   RP   t   g2RR   RS   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyRe      s    ,""

c   	      C   s   t  |  d d d  } | r, d | f GHn  t d | d |  \ } } } t t d |  > } | t | | t | t } | S(   s   
    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 ],@i   s   binary splitting with N =i    (   R9   Re   R4   Rb   Rc   t   CHUD_D(	   R:   Rg   t   verbose_baseRW   R>   RX   RI   t   sqrtCRE   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   pi_fixed   s    	c         C   s   t  |   d S(   Ni   (   Rn   (   R:   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   degree_fixed   s    c         C   sn   | |  d k r  t  t |  f S|  | d } t |  |  \ } } t | |  \ } } | | | | | f S(   se   
    Sum series for exp(1)-1 between a, b, returning the result
    as an exact fraction (p, q).
    i   i   (   R   R   t   bspe(   RJ   RK   RN   RO   RP   RR   RS   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyRp      s    c         C   sF   t  d |  t j |   d  } t d |  \ } } | | |  >| S(   s   
    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?i   i    (   R9   RU   RV   Rp   (   R:   RW   RX   RI   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   e_fixed	  s    !c         C   s.   |  d 7}  t  t d |  > t |  >} | d ?S(   s2   
    Computes the golden ratio, (1+sqrt(5))/2
    i
   i   i   (   R4   R   R   (   R:   RJ   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt	   phi_fixed  s    
c         C   s3   |  d } t  t t t |  d  |  |  d  S(   Ni
   i   (   R   t   mpf_logR-   t   mpf_pi(   R:   RD   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   ln_sqrt2pi_fixed*  s    
c         C   s   t  t |   |   S(   N(   R   Rn   (   R:   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   sqrtpi_fixed0  s    c         C   sH  |  \ } } } } | \ } }	 }
 } | rE |
 d k  rE t  d   n  |
 d k rp t |  d | |	 |
 >| |  S|
 d k r|	 d k r | r t t t |  | d t |  | |  St |  | |  S| r t t |  | d t |  |	 | |  St t |  | d |  |	 | |  Sn  t |  | d |  } t t | |  | |  S(   sV   
    Compute s**t. Raises ComplexResult if s is negative and t is
    fractional.
    i    s,   negative number raised to a fractional powerii   i
   (	   R   R/   R,   R   R0   R1   Rs   t   mpf_expR+   (   R]   t   tR:   RC   t   ssignt   smant   sexpt   sbct   tsignt   tmant   texpt   tbct   c(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_pow>  s$    &c   
      C   sl  | d k r |  |  d f St  |   } d } d | d t  |  d } t \ } } } }	 x| d @r | |  } | | } |	 | d 7}	 |	 t t | |	 ? }	 |	 | k r | |	 | ?} | |	 | 7} | }	 n  | d 8} | s Pq n  |  |  }  | | } | | d } | t t |  | ? } | | k rT|  | | ?}  | | | 7} | } n  | d } q] W| | f S(   s   n-th power of a fixed point number with precision prec

       Returns the power in the form man, exp,
       man * 2**exp ~= y**n
    i   i    i   i   (   R   R   R   R9   (
   t   yt   nR:   t   bct   expt   workprect   _t   pmt   pet   pbc(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   int_pow_fixedZ  s:    


	


	c         C   sU  d } y5 t  |  | | |  } t t | d |   } Wn\ t k
 r t | |  } t |  } t d | |  } t | | |  } t |  } n Xd } | }	 | }
 x t | | |  D] } t	 | | d |
  \ } } t  | | d |
 | | |	  } t
 |  d | | |	  | } | | d t
 | | |
  | } | }
 q W| S(   Ni2   g      ?i   i
   i   (   R   R   R9   t   OverflowErrorR   R.   R   R   R   R   R   (   R   R   R:   t   exp1t   startt   y1RY   t   fnt   extrat   extra1t   prevpRX   R   R   RT   t   B(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   nthroot_fixed  s(    ##
c         C   sG  |  \ } } } } | r' t  d   n  | s |  t k r= t S|  t k rm | d k rY t S| d k ri t St S| sw t S| d k  r t St St } | d k  r| d k r t S| d k r t |  | |  S| d k r t t |  | |  St | } t	 } d }	 | |	 7} | } n  | d k r| d k sH| t
 d	 d
 | d  k  r| d }
 t |  } t d | |
  } t |  | |
 |  } t | d | d | d | d | |  }  | rt t |  | |	 |  S|  Sn  | d | | | }
 | d k r|
 |
 d 7}
 |
 |
 | }
 n  | |
 } d } | | } | d k  rKd } | } n  | rb| | | 7} n | | | 8} t | |  } d } | | | d |
 | | } d } | r| d k s| d k rd } qn! | d k s| d k rd } n  t | | | |
 |  } t | | | |  }  | r?t t |  | |	 |  S|  Sd S(   sa   nth-root of a positive number

    Use the Newton method when faster, otherwise use x**(1/n)
    s   nth root of a negative numberi    i   i   ii   i   i N  i   gL<@gףp=
?i
   i   t   uR   t   dR=   N(   R   R#   R   R   R!   t   FalseR'   R,   R1   R_   R9   R   R.   R   R   R   R   R   (   R]   R   R:   RC   t   signt   manR   R   t   flag_inverset   extra_inverset   prec2R   t   nthRY   t   shiftt   sign1t   esR   R   t	   rnd_shift(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_nthroot  s    


6
+


	c         C   s   t  |  d | |  S(   s   cubic root of a positive numberi   (   R   (   R]   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_cbrt  s    c   	      C   s   |  t  k r7 t  |  \ } } | | k r7 | | | ?Sn  | d } | t k r | d k rh t |  } n  t |   } |  | | >} t | |  | | } n" t t t |   | d  |  } |  t	 k  r | | f t  |  <n  | | | ?S(   s`   
    Fast computation of log(n), caching the value for small n,
    intended for zeta sums.
    i
   i   N(
   t   log_int_cachet   LOG_TAYLOR_SHIFTR?   R`   R   t   log_taylor_cachedR   Rs   R   t   MAX_LOG_INT_CACHE(	   R   R:   t   ln2t   valuet   vprecRD   RY   t   xRE   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   log_int_fixed  s    
"c         C   se   d } xX |  | d ?} | d k r= t  |  |  d k  r= |  St |  |  } | }  | d 7} q	 W|  S(   s^   
    Fixed-point computation of agm(a,b), assuming
    a, b both close to unit magnitude.
    i    i   i   i   (   t   absR4   (   RJ   RK   R:   t   it   anew(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt	   agm_fixed  s    "c         C   s  |  |  | ?} | } } } x0 | rN | | | ?} | | | ?} | | 7} q W| t  | >7} | | | d ?} | t |  | > | ?} |  } } } x0 | r | | | ?} | | | ?} | | 7} q Wt  | >| d >} | | | ?} t | | |  } t |  | >| S(   s*  
    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

    i   i   (   R   R4   R   Rn   (   R   R:   t   x2R]   RJ   RK   Rx   RX   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   log_agm)  s$    		i    c         C   s  x$ t  |  D] } t |  | > }  q Wt | >} |  | | >|  | } | d k  } | rc | } n  | | | ?} | | | ?} | }	 | d }
 | | | ?} d } xH | r |	 | | 7}	 | d 7} |
 | | 7}
 | | | ?} | d 7} q W|
 | | ?}
 |	 |
 d | >} | r| S| S(   s:  
    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.
    i    i   i   i   i   (   R   R4   R   (   R   R:   RY   R   t   oneRE   R   t   v2t   v4t   s0t   s1t   kR]   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt
   log_taylorX  s0    	


	
c         C   sn  |  | t  ?} t | } | | } | | f t k rM t | | f \ } } n6 | | t  >} t | | d  } | | f t | | f <| | L} | | L} |  | | >| } | | >t | >| } | | | ?}	 |	 |	 | ?}
 | } | d } | |
 | ?} d } xH | rI| | | 7} | d 7} | | | 7} | |
 | ?} | d 7} qW| |	 | ?} | | d >} | | S(   sd   
    Fixed-point computation of log(x), assuming x in (0.5, 2)
    and prec <= LOG_TAYLOR_PREC.
    i   i   i   i   i   (   R   t   cache_prec_stepst   log_taylor_cacheR   R   (   R   R:   R   t   cached_prect   dprecRJ   t   log_aR   RE   R   R   R   R   R   R]   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyR   z  s6    




	
c         C   sK  |  \ } } } } | sK |  t  k r( t S|  t k r8 t S|  t k rK t Sn  | r` t d   n  | d } | d k r | s t  St | t |  | | |  S| | } t |  }	 |	 d k rTd |	 }
 |
 r t | >| } n | t | d >} t	 |  } | | } | | k rGt
 |
 | |	 | | | d  } t | |
 | |  S| | 7} n  |	 d k rt	 |	  | k rt | t |  | | |  Sn  | t k rt t | | |  |  } | r7| | t |  7} q7n\ | t } | | } t |  |  }  | | 7} t t |  |  |  } | | t |  8} t | | | |  S(   sj   
    Compute the natural logarithm of the mpf value x. If x is negative,
    ComplexResult is raised.
    s   logarithm of a negative numberi   i   R   i'  (   R   R"   R!   R#   R   R   R`   R   R   R   R   R3   t   LOG_TAYLOR_PRECR   R   t   LOG_AGM_MAG_PREC_RATIOR-   R   R   (   R   R:   RC   R   R   R   R   RD   t   magt   abs_magR}   R~   R   t   cancellationRx   RN   t   optimal_magR   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyRs     sT       



!
c   
      C   s`  | d s | |  }  } n  |  d s | d sh |  | k oE t  k n rN t St |  | f k rd t St S|  t  k r t t |  | |  S|  t k r t St St |  |   } t | |  } d } t | | | |  } t | t d  } | d | d }	 | t  k s|	 | d k  rGt | | | | t	 | d | d   } n  t
 t | | |  d  S(   s1   
    Computes log(sqrt(a^2+b^2)) accurately.
    i   i   i
   i   i   i(   R   R"   R#   R!   Rs   R&   R+   R)   R   t   minR-   (
   RJ   RK   R:   RC   t   a2t   b2R   t   h2t	   cancelledt   mag_cancelled(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_log_hypot  s.    


.c   
      C   s  | d k r0 t  j t |  | d ? d  } n t  j t |   d |  } d } t t | d  d | ? } d } x t | |  D] } | | 7} | | | >} t | |  \ } } | | >| } | t |  | |  | >t | >| d | ?}	 | |	 } | } q Wt | | |  S(   Nid   i5   g       @i2   i   g      @Cg      @C(   RU   t   atanR9   R   R   t   cos_sin_fixedR   R   (
   R   R:   RY   R   t   extra_pRD   t   cost   sint   tanRJ   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   atan_newton  s    $
/

c         C   s   d t  | d  >d } | | } |  | f t k rM t |  | f \ } } n3 |  | t >} t | |  } | | f t |  | f <| | ?| | ?f S(   Ni   i   (   R   t   atan_taylor_cachet   ATAN_TAYLOR_SHIFTR   (   R   R:   R   R   RJ   t   atan_a(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   atan_taylor_get_cached"  s    
c         C   s   |  | t  ?} t | |  \ } } |  | } | | >| d | ?| | | ?t | >} } | d | ?} | | | ?}	 | d }
 | |	 | ?} d } xH | r | | | 7} | d 7} |
 | | 7}
 | |	 | ?} | d 7} q W|
 | | ?}
 | |
 } | | S(   Ni   i   i   (   R   R   R   (   R   R:   R   RJ   R   R   R   RE   R   R   R   R   R]   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   atan_taylor1  s$    
.
	

c         C   s<   |  s t  t | |  d  St t  t | t |  d   S(   Ni(   R-   Rt   R(   R2   (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   atan_infE  s    c         C   s  |  \ } } } } | sd |  t  k r( t  S|  t k rD t d | |  S|  t k r` t d | |  St S| | } | | d k r t | | |  S| | d k r t |  d | | |  S| d t |  } | d k r t d |  |  }  t }	 n t	 }	 t
 |  |  }
 | r|
 }
 n  | t k  r4t |
 |  } n t |
 |  } |	 rdt |  d ?d | } n  | rt| } n  t | | | |  S(   Ni    i   i   i   i   (   R   R!   R   R"   R#   R3   R   R.   R_   R   R   t   ATAN_TAYLOR_PRECR   R   Rn   R   (   R   R:   RC   R   R   R   R   R   RD   t
   reciprocalRx   RJ   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_atanJ  s<       
	

c         C   s  | \ } } } } |  \ } }	 }
 } |	 s |  t  k re | t k re t |  d k rX t  St | |  S|  t t f k r | t t f k r t S|  t k r t t | |  d  St t t | t |  d   St S| r t t	 t |   | | t |   S| sa| t k rt S| t k r"t  S| t k r;t | |  S|  t  k rKt  St t | |  d  St
 t |  | | d  | d  } | rt t | d  | | |  St | | |  Sd  S(   Ni    ii   (   R   R#   R%   Rt   R!   R"   R-   R(   R2   t	   mpf_atan2R   R,   R)   R'   (   R   R   R:   RC   t   xsignt   xmant   xexpt   xbct   ysignt   ymant   yexpt   ybct   tquo(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyR   m  s<     ##c         C   s   |  \ } } } } | | d k rC |  t  t f k rC t d   n  | d } t |  |   } t t  t t t  | |  |  |  }	 t |  |	 |  }
 t t	 |
 | |  d  S(   Ni    s%   asin(x) is real only for -1 <= x <= 1i   i   (
   R   R   R   R+   R)   R0   R*   R,   R-   R   (   R   R:   RC   R   R   R   R   RD   RJ   RK   R   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_asin  s    "
'c         C   s   |  \ } } } } | | d k r_ |  t  t f k rC t d   n  |  t k r_ t | |  Sn  | d } t |  |   } t t t  | |  |  }	 t |	 t t  |  |  |  }
 t	 t
 |
 | |  d  S(   Ni    s%   acos(x) is real only for -1 <= x <= 1i   i   (   R   R   R   Rt   R+   R0   R*   R,   R)   R-   R   (   R   R:   RC   R   R   R   R   RD   RJ   RK   R   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_acos  s    
c   
      C   s   | d } |  \ } } } } | | } | d k  rd | | k  rV t  |  d | | |  S| | 7} n  t t t |  |   t |  |  }	 t t |   |	 |  }	 | r t t |	 | t |   St |	 | |  Sd  S(   Ni   ii   (	   R3   R0   R)   R+   R   R&   R(   Rs   R2   (
   R   R:   RC   RD   R   R   R   R   R   RI   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt	   mpf_asinh  s    

$c         C   sn   | d } t  |  t  d k r. t d   n  t t t |  |   t |  |  } t t |  | |  | |  S(   Ni   is    acosh(x) is real only for x >= 1(   R$   R   R   R0   R)   R+   R   Rs   (   R   R:   RC   RD   RI   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt	   mpf_acosh  s
    
$c         C   s  |  \ } } } } | rD | rD |  t  t f k r5 |  St d   n  | | } | d k r | d k r | d k r t t g | St d   n  | d } | d k  r | | k  r t |  | | |  S| | 7} n  t |  t |  }	 t t |  |  }
 t	 t
 t |	 |
 |  | |  d  S(   Ns&   atanh(x) is real only for -1 <= x <= 1i    i   i   ii(   R   R#   R   R!   R"   R3   R)   R   R*   R-   Rs   R,   (   R   R:   RC   R   R   R   R   R   RD   RJ   RK   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt	   mpf_atanh  s$    

c         C   s  |  \ } } } } | s, |  t  k r( t S|  St | |  } | d k r | d k  sf | t |  k r t t t |    | |  Sn  | | d } t |  }	 t t	 |	 d  t
 |  }
 t |	 |  |  } t |  |  } t | | |  } t | | |  } t | |
 | |  } | S(   Ni    i
   i   i   (   R"   R#   R   R   R   R5   R   t   mpf_phiR)   R-   R   R   t
   mpf_cos_piR,   R*   (   R   R:   RC   R   R   R   R   t   sizeRD   RJ   RK   R   RE   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_fibonacci  s$    c         C   s  |  d k  r |  }  d } n d } t  d | d  } t |   | } t d | |  } d d t | |  } | | } |  | | K}  t | >} | d k }	 | t k  rx|  |  | ?}
 } |
 |
 | ?} t } } d } xd | rA| | d | } | | 7} | d 7} | | d | } | | 7} | d 7} | | | ?} q W|
 | | ?} |	 rg| | | } q| | | } nAt  d | d  } |  |  | ?}
 } | |
 g } x0 t d |  D] } | j | d |
 | ? qWt g | } d } x | rtxc t |  D]U } | | d | } |	 rA| d @rA| | c | 8<n | | c | 7<| d 7} qW| | d | ?} qWx1 t d |  D]  } | | | | | ?| | <qWt |  | } | d k r't	 | | | | > } | r| | } n
 | | } x" t |  D] } | | | ?} qW| | ?S| d } x& t |  D] } | | | ?| } q>Wt	 t
 | | >| |   } | r| } n  | | ?| | ?f Sd	 S(
   s   
    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))
    i    i   g      ?i
   i   g333333?gffffff?iN(   R9   R   t   maxR   t   EXP_SERIES_U_CUTOFFR   R   t   appendt   sumR4   R   (   R   R:   t   typeR   RY   t   xmagR   RD   R   t   altR   RJ   t   x4R   R   R   R   R   t   xpowersR   t   sumsR]   RE   t   pshift(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   exponential_series  s|    	


	 
 
 
 
	 


c   
      C   s   | t  k r t |  | d  St | d  } | | 7} t | >} } d } |  |  | ?} } xT | r | | } | | 7} | d 7} | | } | | 7} | d 7} | | | ?} q_ W| |  | ?} | | } | }	 x" | r | | | ?} | d 8} q W| |	 ?S(   s   
    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)).
    i    g      ?i   i   (   t   EXP_COSH_CUTOFFR   R9   R   (
   R   R:   RY   R   R   R   RJ   R   R]   R   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   exp_basecase>  s,    
	
 
 

 
 

	c         C   sa   | t  k r6 t |  | d  \ } } | | | | f St |  |  } t | | >| } | | f S(   s(   
    Computation of exp(x), exp(-x)
    i   (   R   R   R   R   (   R   R:   t   cosht   sinhRJ   RK   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   exp_expneg_basecaseW  s    c         C   s  | t  k r t |  | d  S| t } |  | ?} t |  } | t k r | d t  t >} t | d t  d  \ } } | d ?| d ?f t | <n  t | \ } } t  | } | | L} | | L} |  | | >8}  t | >}	 |  }
 d } |  |  | ?} xc | rW| | } |	 | 7}	 | d 7} | |  | ?} | | } |
 | 7}
 | d 7} | |  | ?} q W|	 | |
 | | ?|
 | |	 | | ?f S(   s   
    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.
    i   i
   i   (   t   COS_SIN_CACHE_PRECR   t   COS_SIN_CACHE_STEPR9   t   cos_sin_cacheR   (   R   R:   t   precsRx   R   t   wt   cos_tt   sin_tt   offsetR   R   R   RJ   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   cos_sin_basecaseb  s8    





	
 
 
 
 
 
 c         C   s  |  \ } } } } | r}| | } | d } | r< | } n  | d k r | d k r t  | t d |   }	 t |	 | | >| |  S| | k  r t t | | |  S| d k r#| | }
 | |
 } | d k r | | >} n | | ?} t |
  } t | |  \ } } t |  } | | L} n4 | | } | d k rF| | >} n | | ?} d } t | |  } t | | | | |  S| st S|  t	 k rt
 S|  S(   Ni   iX  i    g333333?i   (   t   mpf_eR9   R/   R3   R   R`   t   divmodR   R   R"   R   (   R   R:   RC   R   R   R   R   R   RD   t   et   wpmodR	  Rx   t   lg2R   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyRw     sB    





c         C   s  |  \ } } } } | r | r | rI |  t  k r5 t S|  t k rE t St S|  t  k r_ t  t  f S|  t k ru t  t f St t f S| | } | d }	 | d k  r| |	 k  r | r t |  d | | |  St t d | |  }
 t |  | | |  } |
 | f S|	 | 7}	 n  | d k rd d | d >|	 k r| rVt t t g | d | | |  St t t |   | |  d  } } | rt	 |  } n  | | f Sn  | d k r|	 | } | | } | d k r| | >} n | | ?} t
 |  } t | |  \ } } t |  } | | L} n4 | |	 } | d k r>| | >} n | | ?} d } t | |	  \ } } | | d | ?}
 | | d | ?} | r| } n  | r| |	 >|
 } t | |	 | |  St |
 | |	 d | |  }
 t | | |	 d | |  } |
 | f Sd	 S(
   s4   Simultaneously compute (cosh(x), sinh(x)) for real xi   ii   i    i
   i   ii   N(   R!   R   R"   R   R#   R3   R-   Rw   R&   R(   R`   R  R9   R  R   (   R   R:   RC   t   tanhR   R   R   R   R   RD   R   R   R   R]   R  R	  Rx   R  R   RJ   RK   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_cosh_sinh  sr       
 




!%



c         C   s<  | d k r d } xd | >} | | | } t  | d  } | d ?} | | }	 |	 d k rj |  |	 >}
 n |  |	 ?}
 t |
 |  \ } } | | k r | | } n | } | | | d ?r t |  } | | ?}
 | | } Pn  | d 7} q Wn? | | 7} | | }	 |	 d k r|  |	 >}
 n |  |	 ?}
 d } |
 | | f S(   Ni    i   i   i
   (   Rn   R  R9   (   R   R   R   RD   R   t   cancellation_precR  t   pi2t   pi4R	  Rx   R   R   t   small(    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mod_pi2  s8    





c         C   s  |  \ } } } } | s | r. t  t  }	 }
 n t t }	 }
 | d k rQ |	 |
 f S| d k ra |	 S| d k rq |
 S| d k r |
 Sn  | | } | d } | d k  rX| | k  rX| r t |  t |   }  n  t t d | |  }	 t |  d | | |  }
 | d k r|	 |
 f S| d k r#|	 S| d k r3|
 S| d k rUt |  | | |  SqXn  | r| d k r| d k rt }	 t t f t | d @ | A}
 n) | d k rt t }	 }
 n t t }	 }
 | d k r|	 |
 f S| d k r|	 S| d k r|
 S| d k rt |
 |	 | |  Sn  | | 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 r@|
 |	 }	 }
 n  | rP|
 }
 n  | d k rt |	 | | |  }	 t |
 | | |  }
 |	 |
 f S| d k rt |	 | | |  S| d k rt |
 | | |  S| d k rt |
 |	 | |  Sd S(   s   
    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
    i    i   i   i   i
   iN(   R#   R   R   R+   Rt   R3   R   t   boolR,   R   Rn   R  R
  R   R   (   R   R:   RC   t   whicht   piR   R   R   R   R   R]   R   RD   R   t   mag2R	  Rx   RN   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_cos_sin  s    
 
   

 
   ! 
   

   

c         C   s   t  |  | | d  S(   Ni   (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_cosb  s    c         C   s   t  |  | | d  S(   Ni   (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_sinc  s    c         C   s   t  |  | | d  S(   Ni   (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_tand  s    c         C   s   t  |  | | d d  S(   Ni    i   (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_cos_sin_pie  s    c         C   s   t  |  | | d d  S(   Ni   (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyR   f  s    c         C   s   t  |  | | d d  S(   Ni   i   (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt
   mpf_sin_pig  s    c         C   s   t  |  | |  d S(   Ni    (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_coshh  s    c         C   s   t  |  | |  d S(   Ni   (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_sinhi  s    c         C   s   t  |  | | d d S(   NR  i   (   R  (   R   R:   RC   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   mpf_tanhj  s    c         C   s   | d  k r t | d  } n  t |  |  \ } } t |  } t | |  \ } } | d @} | d k ru | | f S| d k r | | f S| d k r | | f S| d k r | | f Sd  S(   Ni   i   i    i   (   R?   Rn   R  R9   R
  (   R   R:   R  R   Rx   R   R]   RN   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyR   o  s    
 
   c         C   sl   | d  k r t |  } n  t |  |  \ } } t |  } t | |  } | d k r_ | | >S| | ?Sd  S(   Ni    (   R?   R`   R  R9   R   (   R   R:   R   R   Rx   RE   (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt	   exp_fixed{  s    t   sages)   Warning: Sage imports in libelefun failed(   RA   RU   R    t   backendR   R   R   R   R   R   R   t   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,   R-   R.   R/   R0   R1   R2   R3   R4   t
   libintmathR5   R   R   R  R  R  R   R   R   R   R   R   R   R   R   R   R   R   RB   RG   RH   RZ   R   R^   R`   Ra   Rc   Rd   Rb   Rk   Re   R?   Rn   Ro   Rp   Rq   Rr   R   Rt   R  t
   mpf_degreet   mpf_ln2t   mpf_ln10Ru   Rv   t
   mpf_sqrtpit   mpf_ln_sqrt2piR   R   R   R   R   R   R   R   R   R   Rs   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R
  Rw   R  R  R  R  R  R  R  R   R   R!  R"  R#  R   R$  t   sage.libs.mpmath.ext_libmpt   libst   mpmatht	   ext_libmpt   _lbmpt   ImportErrort   AttributeError(    (    (    s5   lib/python2.7/site-packages/mpmath/libmp/libelefun.pyt   <module>
   s   . 
		 .						$						6	S		/"	"H	,				##K			-C	$O								