
n5[c           @   s  d  Z  d Z d d l Z d d l m Z d d l Z d a 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 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" e d k rd	   Z# n	 d
   Z# d   Z$ d e% f d     YZ& y e' Wn e( k
 rNd   Z' n Xe' d  Z) e' d  Z* e' d  Z+ e' d  Z, e' d  Z- e- Z. d   Z/ d   Z0 d   Z1 d e
 d d f Z2 d e
 d d f Z3 d e d d f Z4 d e d d f Z5 d e d d f Z6 d e d d f Z7 d e d d f Z8 d e
 d d f Z9 d e
 d d f Z: d e
 d d f Z; dg Z< d   Z= d  dh d!     YZ> d g g  e? d d"  D] Z@ e e@ d >d ^ qZA e>   eA g ZB i di e* 6dj e+ 6dk e- 6dl e, 6ZC d#   ZD d$   ZE y eF eG f ZH Wn e( k
 r&eF f ZH n Xd%   ZI d&   ZJ e d' k rld( eK e  k rle jL ZD e jL ZE n  e d k re jM ZD ZE n  e reI ZM eJ ZN n eD ZM eE ZN d e. d)  ZO eP d*   e? d+ d,  D  ZQ e d' k rd- eK e  k re jR ZO n  e d k re jO ZO n  d e. d.  ZS d/   ZT d d0  ZU d1   ZV d e. d2  ZW d e. d3  ZX d e. d4  ZY d e. d5  ZZ d6 e. d7  Z[ d8 e. d9  Z\ d e. d:  Z] e^ e. d;  Z_ e. d<  Z` d=   Za d>   Zb d?   Zc d@   Zd dA   Ze dB   Zf dC   Zg dD   Zh dE   Zi dF   Zj dG   Zk d e. dH  Zl d e. dI  Zm d e. dJ  Zn dK   Zo d e. d dL  Zp d e. dM  Zq d e. e^ dN  Zr d e. dO  Zs e. dP  Zt d e. dQ  Zu e. dR  Zv e d' k res Zw et Zx n eu Zw ev Zx dS   Zy dT   Zz e. dU  Z{ e. dV  Z| e. dW  Z} i e, e- 6e- e, 6e+ e* 6e* e+ 6e) e) 6Z~ i e- e- 6e, e, 6e+ e* 6e* e+ 6e) e) 6Z e. dX  Z dY   Z dZ   Z e d d e^ d[  Z d\ d]  Z i e: d^ 6e: d_ 6e; d` 6e9 da 6Z e. db  Z dc   Z dd   Z e. de  Z e. df  Z e d k r{yF d d l j j j Z e jp Zp e jq Zq e jw Zw e j{ Z{ e j Z Wq{e k
 rwq{Xn  d S(m   sH   
Low-level functions for arbitrary-precision floating-point arithmetic.
t	   plaintextiN(   t   bisecti   (   t   MPZt   MPZ_TYPEt   MPZ_ZEROt   MPZ_ONEt   MPZ_TWOt   MPZ_FIVEt   BACKENDt   STRICTt   HASH_MODULUSt	   HASH_BITSt   gmpyt   saget
   sage_utils(   t   giant_stepst
   trailtablet   bctablet   lshiftt   rshiftt   bitcountt   trailingt
   sqrt_fixedt   numeralt   isqrtt
   isqrt_fastt   sqrtremt   bin_to_radixR   c         C   s(   |  \ } } } } | t  |  | | f S(   N(   t   hex(   t   xt   signt   mant   expt   bc(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   to_pickable   s    c         C   s,   |  \ } } } } | t  |  d | | f S(   Ni   (   R   (   R   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyR"   #   s    c         C   s+   |  \ } } } } | t  | d  | | f S(   Ni   (   R   (   R   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   from_pickable'   s    t   ComplexResultc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyR$   +   s   c         C   s   |  S(   N(    (   R   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   <lambda>1   s    t   nt   ft   ct   ut   dc         C   s'   t  d t t t |   d  d   S(   sZ   Return number of accurate decimals that can be represented
    with a precision of n bits.i   gry	O
@(   t   maxt   intt   round(   R(   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   prec_to_dps;   s    c         C   s'   t  d t t t |   d d    S(   sJ   Return the number of bits required to represent n decimals
    accurately.i   gry	O
@(   R-   R.   R/   (   R(   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   dps_to_prec@   s    c         C   s$   t  |   } | d k r d S| d S(   s   Return the number of decimal digits required to represent
    a number with n-bit precision so that it can be uniquely
    reconstructed from the representation.i   i   i   (   R0   (   R(   t   dps(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   repr_dpsE   s    i    i   ii8iiigu <7~c         C   s   | t  k r~ |  d k ri |  | d ?} | d @r^ | d @sR |  t | d k  | @r^ | d ?d S| d ?Sq~ t |  | |  Sn  | t k r |  | ?S| t k r |  | ?S| t k r |  d k r |  | ?S|  | ?S| t k r |  d k r |  | ?S|  | ?Sd  S(   Ni    i   i   i,  (   t   round_nearestt   h_maskt	   round_intt   round_floort   round_ceilingt
   round_downt   round_up(   R   R(   t   rndt   t(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyR6   k   s&    ,


t
   h_mask_bigc           B   s   e  Z d    Z RS(   c         C   s   t  | d >d S(   Ni   (   R   (   t   selfR(   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   __getitem__   s    (   R%   R&   R?   (    (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyR=      s   i,  c         C   sn  | s
 t  S| | } | d k r | t k r | | d ?} | d @rw | d @sf | t | d k  | @rw | d ?d } q | d ?} n' t | |  r | | L} n | | ?} | | 7} | } n  | d @sIt t | d @ } | s(x, | d @s| d L} | d 7} | d 8} q Wt t | d @ } n  | | L} | | 7} | | 8} n  | d k r^d } n  |  | | | f S(   s  
    Create a raw mpf tuple with value (-1)**sign * man * 2**exp and
    normalized mantissa. The mantissa is rounded in the specified
    direction if its size exceeds the precision. Trailing zero bits
    are also stripped from the mantissa to ensure that the
    representation is canonical.

    Conditions on the input:
    * The input must represent a regular (finite) number
    * The sign bit must be 0 or 1
    * The mantissa must be positive
    * The exponent must be an integer
    * The bitcount must be exact

    If these conditions are not met, use from_man_exp, mpf_pos, or any
    of the conversion functions to create normalized raw mpf tuples.
    i    i   i   i,  i   i   (   t   fzeroR4   R5   t   shifts_downR   R.   (   R   R   R    R!   t   precR;   R(   R<   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt
   _normalize   s8    
,
	




	c         C   s{  | s
 t  S| | k r& |  | | | f S| | } | t k r | | d ?} | d @r | d @sv | t | d k  | @r | d ?d } q | d ?} n' t | |  r | | L} n | | ?} | | 7} | } | d @sVt t | d @ } | s5x, | d @s| d L} | d 7} | d 8} q Wt t | d @ } n  | | L} | | 7} | | 8} n  | d k rkd } n  |  | | | f S(   sS   same as normalize, but with the added condition that
       man is odd or zero
    i   i   i,  i   i   (   R@   R4   R5   RA   R   R.   (   R   R   R    R!   RB   R;   R(   R<   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   _normalize1   s:    
,





	c         C   sy   t  |  t k s t  t  |  t k s0 t  t  |  t k sH t  | t |  k s` t  t |  | | | | |  S(   sz   Additional checks on the components of an mpf. Enable tests by setting
       the environment variable MPMATH_STRICT to Y.(   t   typeR   t   AssertionErrort
   _exp_typesR   RC   (   R   R   R    R!   RB   R;   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   strict_normalize   s
    c         C   s   t  |  t k s t  t  |  t k s0 t  t  |  t k sH t  | t |  k s` t  | sw | d @sw t  t |  | | | | |  S(   sz   Additional checks on the components of an mpf. Enable tests by setting
       the environment variable MPMATH_STRICT to Y.i   (   RE   R   RF   RG   R   RD   (   R   R   R    R!   RB   R;   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   strict_normalize1  s    R   t   _mpmath_normalizec         C   sC  t  |   }  d } |  d k  r. d } |  }  n  |  d k  rM t t |   } n t |   } | s*|  si t S|  d @s|  d @r | |  d ?| d | d f St t |  d @ } | s x, |  d @s |  d L}  | d 7} | d 8} q Wt t |  d @ } n  |  | L}  | | 7} | | 8} n  | |  | | f St | |  | | | |  S(   s   Create raw mpf from (man, exp) pair. The mantissa may be signed.
    If no precision is specified, the mantissa is stored exactly.i    i   i   i   i   i   (   R   R   R.   R   R@   R   t	   normalize(   R   R    RB   R;   R   R!   R<   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   from_man_exp#  s4    






c         c   s$   |  ] } | t  | d   f Vq d S(   i    N(   RL   (   t   .0R(   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pys	   <genexpr>B  s    ii  t   _mpmath_createc         C   s0   | s |  t  k r t  |  Sn  t |  d | |  S(   sc   Create a raw mpf from an integer. If no precision is specified,
    the mantissa is stored exactly.i    (   t	   int_cacheRL   (   R(   RB   R;   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   from_intJ  s    c         C   s<   |  \ } } } } | r2 | r2 t  d |   n  | | f S(   s:   Return (man, exp) of a raw mpf. Raise an error if inf/nan.s*   mantissa and exponent are undefined for %s(   t
   ValueError(   t   sR   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt
   to_man_expR  s    c         C   s   |  \ } } } } | r. | r. t  d   n  | d k rQ | rI | | >S| | >S| ss | rg | | ?S| | ?Sn  | r t | | |  St | | |  Sd S(   s   Convert a raw mpf to the nearest int. Rounding is done down by
    default (same as int(float) in Python), but can be changed. If the
    input is inf/nan, an exception is raised.s    cannot convert inf or nan to inti    N(   RQ   R6   (   RR   R;   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   to_intY  s    	
c         C   s   |  \ } } } } | r# | r# |  S| d k r3 |  S| | } | d k  r | t  k rf | r_ t St Sq | t k r | r| t St Sq | t k r | d k  s | t k r t S| r t St Sq t  n  t |  t	 | |  |  S(   Ni    i   (
   R8   R@   t   foneR7   t   fnoneR4   R   t   NotImplementedErrort   mpf_post   min(   RR   R;   R   R   R    R!   t   mag(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_round_into  s.    
    	c         C   s.   t  |  t  } | r* t | | |  } n  | S(   N(   R[   R7   RX   (   RR   RB   R;   t   v(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt	   mpf_floor  s    c         C   s.   t  |  t  } | r* t | | |  } n  | S(   N(   R[   R8   RX   (   RR   RB   R;   R\   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_ceil  s    c         C   s.   t  |  t  } | r* t | | |  } n  | S(   N(   R[   R4   RX   (   RR   RB   R;   R\   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_nint  s    c         C   s   t  |  t |   | |  S(   N(   t   mpf_subR]   (   RR   RB   R;   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_frac  s    i5   c         C   s   |  |  k r t  Sy t j |   \ } } Wn) |  t k r? t S|  t k rP t St  SX|  t k re t S|  t k rv t St t | d  | d | |  S(   s   Create a raw mpf from a Python float, rounding if necessary.
    If prec >= 53, the result is guaranteed to represent exactly the
    same number as the input. If prec is not specified, use prec=53.i   i5   I        (   t   fnant   matht   frexpt   math_float_inft   finft   fninfRL   R.   (   R   RB   R;   t   mt   e(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt
   from_float  s        iq   c         C   s   t  |   } |  | k r( t | | |  Sd d l } | j |   r | j |   \ } } t t | j | d   t | d  | |  S| j |   r t	 S| j
 |   r t St S(   s   Create a raw mpf from a numpy float, rounding if necessary.
    If prec >= 113, the result is guaranteed to represent exactly the
    same number as the input. If prec is not specified, use prec=113.iNiq   (   t   floatRj   t   numpyt   isfiniteRd   RL   R.   t   ldexpt   isposinfRf   t   isneginfRg   Rb   (   R   RB   R;   t   yt   npRh   Ri   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   from_npfloat  s    /  c         C   su   |  j    r t S|  j   r0 |  j   r, t St S| d k r_ t t |  j	   d  d  } n  t
 t |   | |  S(   s   Create a raw mpf from a Decimal, rounding if necessary.
    If prec is not specified, use the equivalent bit precision
    of the number of significant digits in x.i   gry	O
@N(   t   is_nanRb   t   is_infinitet	   is_signedRg   Rf   t   NoneR.   t   lent   as_tuplet   from_strt   str(   R   RB   R;   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   from_Decimal  s      #c         C   s   |  \ } } } } | sQ |  t  k r( d S|  t k r8 t S|  t k rI t St t S| d k r t | | | | d |  \ } } } } n  | r | } n  y t j | |  SWn@ t k
 r | r   n  | | d k r | r t St Sn  d SXd S(   s  
    Convert a raw mpf to a Python float. The result is exact if the
    bitcount of s is <= 53 and no underflow/overflow occurs.

    If the number is too large or too small to represent as a regular
    float, it will be converted to inf or 0.0. Setting strict=True
    forces an OverflowError to be raised instead.

    Warning: with a directed rounding mode, the correct nearest representable
    floating-point number in the specified direction might not be computed
    in case of overflow or (gradual) underflow.
    g        i5   i    N(   R@   Rf   Re   Rg   t
   normalize1Rc   Rn   t   OverflowError(   RR   t   strictR;   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   to_float  s.       *
c         C   s   t  t |   t |  | |  S(   sD   Create a raw mpf from a rational number p/q, round if
    necessary.(   t   mpf_divRP   (   t   pt   qRB   R;   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   from_rational  s    c         C   sr   |  \ } } } } | r" | } n  | d k rA t  d |   n  | d k r_ | d | >d f S| d | >f Sd S(   s]   Convert a raw mpf to a rational number. Return integers (p, q)
    such that s = p/q exactly.is&   cannot convert %s to a rational numberi    i   N(   RQ   (   RR   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   to_rational  s    
c         C   se   |  \ } } } } | | } | rD | d k r7 | | >S| | ?Sn | d k rX | | >S| | ?Sd S(   s.   Convert a raw mpf to a fixed-point big integeri    N(    (   RR   RB   R   R   R    R!   t   offset(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   to_fixed  s    
 	 c         C   s8   t  s d d l } | j  a  n  t t  |   |  |  t  S(   sQ   Return a raw mpf chosen randomly from [0, 1), with prec bits
    in the mantissa.iN(   t   getrandbitst   randomRL   R7   (   RB   R   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_rand  s    c         C   s?   |  d s | d r5 |  t  k s. | t  k r5 t Sn  |  | k S(   s   Test equality of two raw mpfs. This is simply tuple comparison
    unless either number is nan, in which case the result is False.i   (   Rb   t   False(   RR   R<   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_eq  s    c         C   s   t  j d k r |  \ } } } } | sm |  t k r= t  j j S|  t k rS t  j j S|  t k rm t  j j Sn  | t } | d k r | t	 } n t	 d d | t	 } | | >t } | r | } n  | d k r | d k n  t
 |  Sy t t |  d d  SWn t k
 rt |   SXd  S(   Ns   3.2i    i   iiR   (   t   syst   versionRb   t	   hash_infot   nanRf   t   infRg   R
   R   R.   t   hashR   R~   (   RR   t   ssignt   smant   sexpt   sbct   h(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_hash!  s.     
 
 
 
 
c         C   s  |  \ } } } } | \ } } } }	 | s2 | r |  t  k rI t |  S| t  k r_ t |   S|  | k ro d S| t k r d S|  t k r d S| t k r d Sd S| | k r | s d Sd S| | k r| | k r d S| | k r | r d Sd Sq| r d Sd Sn  | | }
 |	 | } | rD|
 | k  r1d S|
 | k rdd Sn  |
 | k  rTd S|
 | k rdd St |  | d t  } | d rd Sd S(   s   Compare the raw mpfs s and t. Return -1 if s < t, 0 if s == t,
    and 1 if s > t. (Same convention as Python's cmp() function.)i    i   ii   (   R@   t   mpf_signRb   Rf   Rg   R`   R7   (   RR   R<   R   R   R   R   t   tsignt   tmant   texpt   tbct   at   bt   delta(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_cmp>  sZ      
       

    
c         C   s/   |  t  k s | t  k r t St |  |  d k  S(   Ni    (   Rb   R   R   (   RR   R<   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_ltr  s    c         C   s/   |  t  k s | t  k r t St |  |  d k S(   Ni    (   Rb   R   R   (   RR   R<   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_lew  s    c         C   s/   |  t  k s | t  k r t St |  |  d k S(   Ni    (   Rb   R   R   (   RR   R<   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_gt|  s    c         C   s/   |  t  k s | t  k r t St |  |  d k S(   Ni    (   Rb   R   R   (   RR   R<   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_ge  s    c         C   s]   |  d } } xB |  d D]6 } t  | |  r7 | } n  t | |  r | } q q W| | f S(   Ni    i   (   R   R   (   t   seqRY   R-   R   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_min_max  s     	 c         C   sF   | rB |  \ } } } } | r) | r) |  St  | | | | | |  S|  S(   sP   Calculate 0+s for a raw mpf (i.e., just round s to the specified
    precision).(   R}   (   RR   RB   R;   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyRX     s    c         C   s|   |  \ } } } } | sE | rA |  t  k r. t S|  t k rA t  Sn  |  S| s_ d | | | | f St d | | | | | |  S(   s   Negate a raw mpf (return -s), rounding the result to the
    specified precision. The prec argument can be omitted to do the
    operation exactly.i   (   Rf   Rg   R}   (   RR   RB   R;   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_neg  s      c         C   sl   |  \ } } } } | r3 | r3 |  t  k r/ t S|  S| sS | rO d | | | f S|  St d | | | | |  S(   s   Return abs(s) of the raw mpf s, rounded to the specified
    precision. The prec argument can be omitted to generate an
    exact result.i    (   Rg   Rf   R}   (   RR   RB   R;   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_abs  s    c         C   sD   |  \ } } } } | s< |  t  k r( d S|  t k r8 d Sd Sd | S(   s   Return -1, 0, or 1 (as a Python int, not a raw mpf) depending on
    whether s is negative, zero, or positive. (Nan is taken to give 0.)i   ii    (   Rf   Rg   (   RR   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyR     s      c         C   s  |  \ } } } } | \ }	 }
 } } |	 | N}	 | r+|
 r+| | } | r| d k rv| d k r | r | | | | } | | d k r | d } | | K} |	 | k r | d 7} n
 | d 8} t  | | | | t |  | |  Sn  | |	 k r|
 | | >} nG | r|
 | | >} n | | >|
 } | d k r>d } n | } d } t |  } t  | | | | | po| |  S| d k  r| d k  r| r| | | | } | | d k r| d } |
 | K}
 | |	 k r|
 d 7}
 n
 |
 d 8}
 t  |	 |
 | | t |
  | |  Sn  | |	 k r1| |
 | >} nI |	 rI| |
 | >} n |
 | >| } | d k rmd } n | } d } t |  } t  | | | | | p| |  Sn  | |	 k r|
 | } n? | r|
 | } n
 | |
 } | d k rd } n | } d } t |  } t | | | | | p$| |  S| r@t |  } n  | s| rm|  | k se|
 se| ri|  St S|
 rt  |	 |
 | | | p| |  S| S| r| S| rt  | | | | | p| |  S|  S(   s   
    Add the two raw mpf values s and t.

    With prec=0, no rounding is performed. Note that this can
    produce a very large mantissa (potentially too large to fit
    in memory) if exponents are far apart.
    i    id   i   i   i(   R}   R   RK   R   Rb   (   RR   R<   RB   R;   t   _subR   R   R   R   R   R   R   R   R   R   R   R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_add  s    



 
 	

 
 	" 
	c         C   s   t  |  | | | d  S(   sx   Return the difference of two raw mpfs, s-t. This function is
    simply a wrapper of mpf_add that changes the sign of t.i   (   R   (   RR   R<   RB   R;   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyR`     s    c         C   sa  d } d } | d p d } d } x|  D]} | \ }	 }
 } } |
 r
|	 r^ | r^ |
 }
 n  | | } | | k r | | k r | s | t t |   | k r |
 } | } q| |
 | >7} q@| } | | | k r | s|
 | } } qq@| | >|
 } | } q) | r) | r%t |  } n  t | p1t | d  } q) q) W| rN| St | | | |  S(   s>  
    Sum a list of mpf values efficiently and accurately
    (typically no temporary roundoff occurs). If prec=0,
    the final result will not be rounded either.

    There may be roundoff error or cancellation if extremely
    large exponent differences occur.

    With absolute=True, sums the absolute values.
    i    i   i@B i   N(   Rw   R   t   absR   R   R@   RL   (   t   xsRB   R;   t   absoluteR   R    t   max_extra_prect   specialR   t   xsignt   xmant   xexpt   xbcR   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_sum"  s:    

#		c         C   s  |  \ } } } } | \ } }	 }
 } | | A} | |	 } | r t  |  } | rm t | | | |
 | | |  S| | | |
 | f Sn  | o | } |	 o |
 } | r | r t St |  | f k r t S|	 r |
 r | |  }  } n  | t k r t Si t d 6t d 6t |   t |  S(   s   Multiply two raw mpfsi   i(   R   R}   R@   Rb   Rf   Rg   R   (   RR   R<   RB   R;   R   R   R   R   R   R   R   R   R   R   R!   t	   s_specialt	   t_special(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   gmpy_mpf_mulR  s(    

   c         C   s   |  \ } } } } | s1 t  |  t |  | |  S| s; t S| d k  r[ | d N} | } n  | | 9} t | | | t |  | |  S(   s   Multiply by a Python integer.i    i   (   t   mpf_mulRP   R@   RK   R   (   RR   R(   RB   R;   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   gmpy_mpf_mul_intg  s    


c         C   s/  |  \ } } } } | \ } }	 }
 } | | A} | |	 } | r | | d } | t  | | ? 7} | r t | | | |
 | | |  S| | | |
 | f Sn  | o | } |	 o |
 } | r | r t St |  | f k r t S|	 r |
 r | |  }  } n  | t k r	t Si t d 6t d 6t |   t |  S(   s   Multiply two raw mpfsi   i(   R.   R}   R@   Rb   Rf   Rg   R   (   RR   R<   RB   R;   R   R   R   R   R   R   R   R   R   R   R!   R   R   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   python_mpf_mult  s*    

   c         C   s   |  \ } } } } | s1 t  |  t |  | |  S| s; t S| d k  r[ | d N} | } n  | | 9} | d k  r | t t |  d 7} n | t |  d 7} | t | | ? 7} t | | | | | |  S(   s   Multiply by a Python integer.i    i   i   (   R   RP   R@   R   R.   R   RK   (   RR   R(   RB   R;   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   python_mpf_mul_int  s    


c         C   s0   |  \ } } } } | s |  S| | | | | f S(   s8   Quickly multiply the raw mpf s by 2**n without rounding.(    (   RR   R(   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt	   mpf_shift  s    c         C   sS   |  \ } } } } | s7 |  t  k r. t  d f St  n  t |  | |  | | f S(   s?   Convert x = y*2**n to (y, n) with abs(y) in [0.5, 1) if nonzeroi    (   R@   RQ   R   (   R   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt	   mpf_frexp  s    
	c         C   s  |  \ } } } } | \ } }	 }
 } | s2 |	 r|  t  k rg | t  k rS t  n  | t k rc t St  S| t  k r| t  n  | o | } |	 o |
 } | r | r t S|  t k s | t k r t S| s | t  k r t Si t d 6t d 6t |   t |  St  S| | A} |	 d k r5t | | | |
 | | |  S| | | d } | d k  r\d } n  t | | >|	  \ } } | r| d >d } | d 7} t | | | |
 | t |  | |  St	 | | | |
 | t |  | |  S(   s   Floating-point divisioni   ii   (
   R@   t   ZeroDivisionErrorRb   Rf   Rg   R   R}   t   divmodR   RK   (   RR   R<   RB   R;   R   R   R   R   R   R   R   R   R   R   R   t   extrat   quott   rem(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyR     sD     	 	&
	
'c         C   s   | \ } } } } |  s  | r9 t  t |   | | |  S|  d k  rY | d N} |  }  n  | | d } t |  | >|  \ }	 }
 |
 r |	 d >d }	 | d 7} t | |	 | | t |	  | |  St | |	 | | t |	  | |  S(   s>   Floating-point division n/t with a Python integer as numeratori    i   i   (   R   RP   R   R}   R   RK   (   R(   R<   RB   R;   R   R   R    R!   R   R   R   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_rdiv_int  s    


$c         C   s  |  \ } } } } | \ } }	 }
 } | r1 | s> |	 rB |
 rB t  S| | k rb |
 | | k rb |  S|	 d k r | |
 | k r t St | |
  } d | | } d | |	 }	 | | | >|	 |
 | >} | d k r d } n | } d } t | | | t |  | |  S(   Ni   ii    (   Rb   R@   RY   RK   R   (   RR   R<   RB   R;   R   R   R   R   R   R   R   R   t   baseR   R   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_mod  s"    	c         C   s  |  \ } } } } | r | r |  t  k rO | d k r; |  S| d k rK t St S|  t k r | d k ry t  t g | d @S| d k r t St St St |  } | d k r t S| d k r t |  | |  S| d k r^|  \ } } } } | s t S| | } | d k rd t | | d f S| | d } | t t | | ? 7} t	 d | | | | | |  S| d k r}t
 t |  | |  S| d k  rt |  | | d t |  }	 t
 t |	 | |  S| | @}
 | d k r|
 t | | d f S| | d k  r!| | C} t	 |
 | | | t |  | |  S| t k p8t | |
 } | d t |  d } t \ } } } } x7| d @r| | } | | } | | d 7} | t t | | ? } | | k r| r| | | ?} n | | | ?} | | | 7} | } n  | d 8} | sPqn  | | } | | } | | d } | t t | | ? } | | k r| rj| | | ?} n | | | ?} | | | 7} | } n  | d } qhWt |
 | | | | |  S(   s=   Compute s**n, where s is a raw mpf and n is a Python integer.i    i   i   ii   i  i   (   Rf   Rb   R@   Rg   R.   RU   RX   R   R   R}   R   t   mpf_pow_intt   reciprocal_rndR   R4   RA   RK   (   RR   R(   RB   R;   R   R   R    R!   t   _t   inverset   result_signt   rounds_downt   workprect   pmt   pet   pbc(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyR     s          
 

#


	


	c   
      C   s   | t  k r t |  | |  S|  \ } } } } | t | | | d d f } | rk | t t f k | A}	 n | t t f k | A}	 |	 r t |  | | |  St |  | |  Sd S(   s  
    For nonzero x, calculate x + eps with directed rounding, where
    eps < prec relatively and eps has the given sign (0 for
    positive, 1 for negative).

    With rounding to nearest, this is taken to simply normalize
    x to the given precision.
    i   N(   R4   RX   R   R9   R8   R:   R   (
   R   t   eps_signRB   R;   R   R   R    R!   t   epst   away(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_perturbf  s    	c         C   s  |  d r d } t  |   }  n d } |  \ } } } } | sA d St | t j d d   d } | | } t |  d k r'd d	 l m }	 m }
 t t |   d
 } t	 |  } t
 | |	 |   } t | |
 |  |  } t |  } t |  t t | |  |  }  |  \ } } } } | } n d } t | | | d  } t | t j d d  d  } t |  |  } t | | d |  } t | d d d | } | t |  | d 7} | | | f S(   s0  Helper function for representing the floating-point number s as
    a decimal with dps digits. Returns (sign, string, exponent) where
    sign is '' or '-', string is the digit string, and exponent is
    the decimal exponent as an int.

    If inexact, the decimal representation is rounded toward zero.i    t   -t    t   0i
   i   i  i   (   t   mpf_ln2t   mpf_ln10i   g      ?R   t   size(   R   R   i    (   R   R.   Rc   t   logR   t	   libelefunR   R   R   RP   R   R   RT   R   t   ftenR-   R   R   R   Rx   (   RR   R2   R   t   _signR   R    R!   t   bitprect
   exp_from_1R   R   t   expprect   tmpR   t   exponentt   fixprect   fixdpst   sft   sdt   digits(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   to_digits_exp  s6    	
 
	 c         C   s  |  d s{ |  t  k rB | r% d } n d } | r> | d 7} n  | S|  t k rR d S|  t k rb d S|  t k rr d St  n  | d k r t | d d	  } n  | d k r | } n  t |  | d  \ } } }	 | s | d
 d k r |	 d 7}	 n  d } nt |  | k r| | d k r| |  } | d }
 x* |
 d
 k r]| |
 d k r]|
 d 8}
 q4W|
 d
 k r| |
  t	 t
 | |
  d  d | |
 d } qd d | d } |	 d 7}	 n
 | |  } | |	 k  o| k  n rB|	 d
 k  rd t
 |	  | } d } n+ |	 d } | | k r9| d | | 7} n  d
 }	 n d } | |  d | | } | r| j d  } | d d k r| d 7} qn  |	 d
 k r| r| r| | S|	 d
 k r| | d t	 |	  S|	 d
 k  r| | d t	 |	  Sd S(   s{  
    Convert a raw mpf to a decimal floating-point literal with at
    most `dps` decimal digits in the mantissa (not counting extra zeros
    that may be inserted for visual purposes).

    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.

    The literal is formatted so that it can be parsed back to a number
    by to_str, float() or Decimal().
    i   s   0.0s   .0s   e+0s   +infs   -infR   i   ii    t   56789t   9R   t   1t   .is   e+Ri   N(   R@   Rf   Rg   Rb   RQ   Rw   RY   R   Rx   R{   R.   t   rstrip(   RR   R2   t   strip_zerost	   min_fixedt	   max_fixedt   show_zero_exponentR<   R   R   R   t   it   split(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   to_str  sl    
 	   	  		"

5
	
	   i
   c         C   s   |  j    j d  }  t |   |  j d  } t |  d k rI d } n | d }  t | d  } |  j d  } t |  d k r | d | d j d  } } | t |  8} | | }  n  t t |  |   }  |  | f S(   s   Helper function for from_str.t   lRi   i   i    R   i   R   (   t   lowerR   Rk   R   Rx   R.   R   (   R   R   t   partsR    R   R   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   str_to_man_exp
  s    
	
R   s   +infs   -infR   c         C   s1  |  j    j   }  |  t k r& t |  Sd |  k r |  j d  \ } } | j d  | j d  } } t t |  t |  | |  St |  d d \ } } t |  d k r t	 | | d  } t
 | t t | | d  | |  } nC | d k rt	 | d | | |  } n t | d | | |  } | S(   sV  Create a raw mpf from a decimal literal, rounding in the
    specified direction if the input number cannot be represented
    exactly as a binary floating-point number with the given number of
    bits. The literal syntax accepted is the same as for Python
    floats.

    TODO: the rounding does not work properly for large exponents.
    t   /R   R   i
   i  i    (   R   t   stript   special_strR   R   R   R.   R   R   RP   R   R   R   (   R   RB   R;   R   R   R   R    RR   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyRz   !  s    	(c         C   sk   t  |  d d \ } } t |  } d } | d k  rF | } d } n  t |  } t | | | | | t  S(   NR   i   i    i   (   R   R   R   RK   R7   (   R   R   R    R   R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt	   from_bstrD  s    	c         C   sD   |  \ } } } } d d g | t  | d t |  d d d | S(   NR   R   R   R   i   s   e%i(   R   R   (   R   R   R   R    R!   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   to_bstrN  s    c   	      C   s  |  \ } } } } | r' t  d   n  | s1 |  S| d @r\ | d 8} | d K} | d 7} n) | d k r t | | | d | | |  St d d | | d  } | | d @7} | d k r t | | > } n7 t | | > \ } } | r| d >d } | d 7} n  t | | | d | |  S(   sb   
    Compute the square root of a nonnegative mpf value. The
    result is correctly rounded.
    s    square root of a negative numberi   i   i   t   fd(   R$   R}   R-   R   R   RL   (	   RR   RB   R;   R   R   R    R!   t   shiftR   (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   mpf_sqrtX  s(    


c         C   sp   | t  k r t |  | |  S|  t  k r8 t | | |  St t |  |   t | |  | d  } t | | |  S(   sI   Compute the Euclidean norm sqrt(x**2 + y**2) of two raw mpfs
    x and y.i   (   R@   R   R   R   R  (   R   Rq   RB   R;   t   hypot2(    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt	   mpf_hypott  s      (g      (    (   i   i    (   i    i   (   i   i   (   i    i    (   t   __doc__t   __docformat__Rc   R   R   Rw   R   t   backendR   R   R   R   R   R   R   R	   R
   R   R   R   R   t
   libintmathR   R   R   R   R   R   R   R   R   R   R   R   R   R"   R#   RQ   R$   t   internt	   NameErrorR4   R7   R8   R:   R9   t
   round_fastR0   R1   R3   R@   t   fnzeroRU   RV   t   ftwoR   t   fhalfRb   Rf   Rg   Re   R6   R=   t   rangeR   t   h_mask_smallR5   RA   RC   RD   R.   t   longRG   RH   RI   t   dirRJ   RK   R}   RL   t   dictRO   RN   RP   RS   RT   R[   R]   R^   R_   Ra   Rj   Rs   R|   R   R   R   R   R   R   R   R   R   R   R   R   R   R   RX   R   R   R   R   R`   R   R   R   R   R   R   t   mpf_mul_intR   R   R   R   R   R   t   negative_rndR   R   R   t   TrueR   R   R   Rz   R   R   R  R  t   sage.libs.mpmath.ext_libmpt   libst   mpmatht	   ext_libmpt   ext_libt   ImportError(    (    (    s2   lib/python2.7/site-packages/mpmath/libmp/libmpf.pyt   <module>   s"  XX
							5
	7	)			
				
%							4					
	
_0			
%

T		4	T"#	
	
					