
~9\c           @  s  d  Z  d d l m Z m Z d d l Z d d l j Z d d l m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z d d l m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& m' Z' m( Z( m) Z) m* Z* m+ Z+ m, Z, m- Z- m. Z. m/ Z/ m0 Z0 d d l m1 Z2 d d l3 m4 Z4 d d	 l5 m6 Z6 d d
 l7 m8 Z8 m9 Z9 d d l: m; Z; d d l< m= Z= m> Z> d d l? m? Z? d d l@ mA ZA d d lB mC ZC e jD d d  ZE e. ZF d   Z1 eG e  ZH eG e  ZI d ZJ d eK f d     YZL d   ZM eN d  ZO d d  ZP eN d  ZQ d   ZR d   ZS d   ZT d   ZU d   ZV d    ZW d!   ZX d"   ZY d#   ZZ eN d$  Z[ d%   Z\ d&   Z] d'   Z^ d(   Z_ d)   Z` d*   Za d+   Zb d,   Zc d-   Zd d.   Ze d/   Zf d0   Zg d1   Zh d2   Zi d3   Zj d4   Zk d5   Zl d6   Zm d7   Zn d8   Zo d aq d9   Zr d:   Zs d; et f d<     YZu d= d>  Zv d S(?   s^   
Adaptive numerical evaluation of SymPy expressions, using mpmath
for mathematical functions.
i(   t   print_functiont   divisionN(	   t   make_mpct   make_mpft   mpt   mpct   mpft   nsumt   quadtst   quadosct   workprec(   t   inf(   t   from_intt   from_man_expt   from_rationalt   fhalft   fnant   fnonet   fonet   fzerot   mpf_abst   mpf_addt   mpf_atant	   mpf_atan2t   mpf_cmpt   mpf_cost   mpf_et   mpf_expt   mpf_logt   mpf_ltt   mpf_mult   mpf_negt   mpf_pit   mpf_powt   mpf_pow_intt	   mpf_shiftt   mpf_sint   mpf_sqrtt	   normalizet   round_nearestt   to_intt   to_str(   t   bitcount(   t   MPZ(   t	   _infs_nan(   t   dps_to_prect   prec_to_dps(   t   mpf_bernoullii   (   t
   SYMPY_INTSt   range(   t   sympify(   t   S(   t   is_sequencei
   i   c         C  s   t  t t |     S(   s8   Return smallest integer, b, such that |n|/2**b < 1.
    (   t   mpmath_bitcountt   abst   int(   t   n(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyR*   "   s    iM  t   PrecisionExhaustedc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyR9   1   s   c         C  s'   |  s |  t  k r t S|  d |  d S(   sD  Fast approximation of log2(x) for an mpf value tuple x.

    Notes: Calculated as exponent + width of mantissa. This is an
    approximation for two reasons: 1) it gives the ceil(log2(abs(x)))
    value and 2) it is too high by 1 in the case that x is an exact
    power of 2. Although this is easy to remedy by testing to see if
    the odd mpf mantissa is 1 (indicating that one was dealing with
    an exact power of 2) that would decrease the speed and is not
    necessary as this is only being used as an approximation for the
    number of bits in x. The correct return value could be written as
    "x[2] + (x[3] if x[1] != 1 else 0)".
        Since mpf tuples always have an odd mantissa, no check is done
    to see if the mantissa is a multiple of 2 (in which case the
    result would be too large by 1).

    Examples
    ========

    >>> from sympy import log
    >>> from sympy.core.evalf import fastlog, bitcount
    >>> s, m, e = 0, 5, 1
    >>> bc = bitcount(m)
    >>> n = [1, -1][s]*m*2**e
    >>> n, (log(n)/log(2)).evalf(2), fastlog((s, m, e, bc))
    (10, 3.3, 4)
    i   i   (   R   t	   MINUS_INF(   t   x(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   fastlogQ   s    c         C  s[   |  j    \ } } | s, | r( | | f Sd S| j   \ } } | t j k rW | | f Sd S(   s  Return a and b if v matches a + I*b where b is not zero and
    a and b are Numbers, else None. If `or_real` is True then 0 will
    be returned for `b` if `v` is a real number.

    >>> from sympy.core.evalf import pure_complex
    >>> from sympy import sqrt, I, S
    >>> a, b, surd = S(2), S(3), sqrt(2)
    >>> pure_complex(a)
    >>> pure_complex(a, or_real=True)
    (2, 0)
    >>> pure_complex(surd)
    >>> pure_complex(a + b*I)
    (2, 3)
    >>> pure_complex(I)
    (0, 1)
    N(   t   as_coeff_Addt   as_coeff_MulR3   t   ImaginaryUnit(   t   vt   or_realt   ht   tt   ct   i(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   pure_complexr   s    
c         C  s   t  |   t k rM t |   d k rM t |  d t rM |  d d f |  d St |  t  r | d	 k rw t d   n  t t	 |   d } } | d k r d n d } | g f | d } | | f St d   d S(
   sl  Return an mpf representing a power of two with magnitude ``mag``
    and -1 for precision. Or, if ``mag`` is a scaled_zero tuple, then just
    remove the sign from within the list that it was initially wrapped
    in.

    Examples
    ========

    >>> from sympy.core.evalf import scaled_zero
    >>> from sympy import Float
    >>> z, p = scaled_zero(100)
    >>> z, p
    (([0], 1, 100, 1), -1)
    >>> ok = scaled_zero(z)
    >>> ok
    (0, 1, 100, 1)
    >>> Float(ok)
    1.26765060022823e+30
    >>> Float(ok, p)
    0.e+30
    >>> ok, p = scaled_zero(100, -1)
    >>> Float(scaled_zero(ok), p)
    -0.e+30
    i   t   scaledi    i   is   sign must be +/-1s-   scaled zero expects int or scaled_zero tuple.N(   ii   (
   t   typet   tuplet   lent   iszerot   Truet
   isinstanceR0   t
   ValueErrorR#   R   (   t   magt   signt   rvt   pt   s(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   scaled_zero   s    6
c         C  s]   | s! |  p  |  d o  |  d S|  o\ t  |  d  t k o\ |  d |  d k oZ d k SS(   Ni   ii    (   RJ   t   list(   R   RI   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRM      s    c   	      C  sw   |  \ } } } } | s& | s" t  S| S| s0 | St |  } t |  } t | | | |  } | t | |  } | S(   s  
    Returns relative accuracy of a complex number with given accuracies
    for the real and imaginary parts. The relative accuracy is defined
    in the complex norm sense as ||z|+|error|| / |z| where error
    is equal to (real absolute error) + (imag absolute error)*i.

    The full expression for the (logarithmic) error can be approximated
    easily by using the max norm to approximate the complex norm.

    In the worst case (re and im equal), this is wrong by a factor
    sqrt(2), or by log2(sqrt(2)) = 0.5 bit.
    (   t   INFR>   t   max(	   t   resultt   ret   imt   re_acct   im_acct   re_sizet   im_sizet   absolute_errort   relative_error(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   complex_accuracy   s    c   
      C  s  t  |  | d |  \ } } } } | sI | | | | f \ } } } } n  | r |  j r t  t t |  | d   | d |  \ } } }	 } | d  |	 d  f Sd | k r t j | | f |  d  | d  f St |   d  | d  f Sn  | r t |  d  | d  f Sd Sd  S(   Ni   t   subs(   NNNN(   t   evalft	   is_numberR6   t   Nt   Nonet   libmpt   mpc_absR   (
   t   exprt   prect   optionsR[   R\   R]   R^   t   abs_exprt   _t   acc(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   get_abs   s    "!	"c   	      C  s   | } d } x t  |  | |  } | | d d  \ } } | s^ | | k s^ | d | k rn | d | d f S| t d d |  7} | d 7} q Wd S(   s/   no = 0 for real part, no = 1 for imaginary parti    Ni   i   i   (   Re   Rh   RY   (	   Rk   t   noRl   Rm   R
   RG   t   rest   valuet   accuracy(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   get_complex_part   s    $c         C  s   t  |  j d | |  S(   Ni    (   Rq   t   args(   Rk   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt	   evalf_abs   s    c         C  s   t  |  j d d | |  S(   Ni    (   Rv   Rw   (   Rk   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   evalf_re   s    c         C  s   t  |  j d d | |  S(   Ni    i   (   Rv   Rw   (   Rk   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   evalf_im   s    c         C  s   |  t  k r' | t  k r' t d   n8 |  t  k rC d  | d  | f S| t  k r_ |  d  | d  f St |   } t |  } | | k r | } | t | | d  } n | } | t | | d  } |  | | | f S(   Ns&   got complex zero with unknown accuracyi    (   R   RP   Rh   R>   t   min(   R[   R\   Rl   t   size_ret   size_imR]   R^   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   finalize_complex  s    c         C  s  |  \ } } } } | rJ | t  k rJ t |  | d k  rJ d \ } } n  | r | t  k r t |  | d k  r d \ } } n  | r| rt |  t |  } | d k  r | | | d k r d \ } } n  | d k  r| | | d k rd \ } } qn  | | | | f S(   s.   
    Chop off tiny real or complex parts.
    i   i   N(   NN(   NN(   NN(   NN(   R,   R>   Rh   (   Rt   Rl   R[   R\   R]   R^   t   delta(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt
   chop_parts  s    ))! c         C  s/   t  |  } | | k  r+ t d |    n  d  S(   Ns   Failed to distinguish the expression: 

%s

from zero. Try simplifying the input, using chop=True, or providing a higher maxn for evalf(   Rc   R9   (   Rk   RZ   Rl   t   a(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   check_target)  s    c           s  d d l  m } m } d } t |  |   \ } } }	 }
 | rl | rl t t |  |	 t |  |
  } n6 | r t |  |	 } n | r t |  |
 } n d Sd } | | k r | | |  t |     \ } } }	 }
 n |      f d   } d	 \ } } } } | r;| | |  d t |  \ } } n  | re| | |  d t |  \ } } n  | rt t	 | pzt
   t t	 | pt
   f S| | | | f S(
   s   
    With no = 1, computes ceiling(expr)
    With no = -1, computes floor(expr)

    Note: this function either gives the exact result or signals failure.
    i(   R[   R\   i   i
   c           s  d d l  m } | \ } } } } | d k } t t | t   } | r t |  | d   \ }	 }
 } } |
 sx t  t |	  d } |  k r t |  |   \ }	 }
 } } |
 s t  |	 } | \ } } } } | d k } t t | t   } q n  | s j d t	  } | rt
 } d d l m } x | j   D] } y | | d t	 Wq@t k
 ry0 g  | j   D] } | | d t	 ^ qzw@Wqt t f k
 rt	 } PqXq@Xq@W| r|  j |  }  qn  | |  | d	 t	 }  t |  d   \ } } } } y  t |  | d  | d  f d
  Wn/ t k
 rc|  j d  sZt  n  t } n X| t   t | pyt t    k  7} n  t |  } | t f S(   Ni(   t   Addi    i
   i   Rd   (   t   as_intt   strictt   evaluatei   (   t   sympy.core.addR   R7   R(   t   rndRe   t   AssertionErrorR>   t   gett   FalseRN   t   sympy.core.compatibilityR   t   valuesRP   t   as_real_imagt   AttributeErrorRd   R   Rh   R9   t   equalsR   R   R   RX   (   t   re_imt   nexprR   R8   RF   RT   t   bt   is_intt   nintt   iret   iimt   ire_acct   iim_acct   sizeRU   t   doitR   RB   RG   R=   Ro   t   x_acc(   Rr   Rm   Rl   (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt	   calc_partU  sZ    ) 	
,R   N(   NNNN(   NNNN(   t$   sympy.functions.elementary.complexesR[   R\   Re   RY   R>   Rh   R   R7   R(   R   (   Rk   Rr   Rm   t   return_intsR[   R\   t   assumed_sizeR   R   R   R   t   gapt   marginR   t   re_t   im_R]   R^   (    (   Rr   Rm   Rl   s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   get_integer_part1  s2    &8$$.c         C  s   t  |  j d d |  S(   Ni    i   (   R   Rw   (   Rk   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   evalf_ceiling  s    c         C  s   t  |  j d d |  S(   Ni    i(   R   Rw   (   Rk   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   evalf_floor  s    c         C  s~  g  |  D] } t  | d  s | ^ q }  |  s3 d	 St |   d k rM |  d Sg  } d d l m } xL |  D]D } | j | d d  } | t j k s | j rj | j	 |  qj qj W| r d d l
 m } t | |   | d i   } | d | d f Sd | }	 d d t }
 } } x |  D] \ } } | \ } } } } | rI| } n  t | | | |  } | | } | | k r| |	 k r|
 s| t t |
   |	 k r| }
 | } q	|
 | | >7}
 q| } | | |	 k r|
 s	| | }
 } q	q|
 | >| }
 | } qW|
 st |  S|
 d k  r9d } |
 }
 n d } t |
  } | | | } t | |
 | | | t  | f } | S(
   s%  
    Helper for evalf_add. Adds a list of (mpfval, accuracy) terms.

    Returns
    -------

    - None, None if there are no non-zero terms;
    - terms[0] if there is only 1 term;
    - scaled_zero if the sum of the terms produces a zero by cancellation
      e.g. mpfs representing 1 and -1 would produce a scaled zero which need
      special handling since they are not actually zero and they are purposely
      malformed to ensure that they can't be used in anything but accuracy
      calculations;
    - a tuple that is scaled to target_prec that corresponds to the
      sum of the terms.

    The returned mpf tuple will be normalized to target_prec; the input
    prec is used to define the working precision.

    XXX explain why this is needed and why one can't just loop using mpf_add
    i    i   i(   t   Float(   R   i   i   N(   NN(   RM   Rh   RL   t   sympy.core.numbersR   t   _newR3   t   NaNt   is_infinitet   appendR   R   Re   R<   RY   R*   R6   RV   R&   R   (   t   termsRl   t   target_precRE   t   specialR   t   argR   RS   t   working_prect   sum_mant   sum_expRa   R=   Ru   RR   t   mant   expt   bcR   t   sum_signt   sum_bct   sum_accuracyt   r(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt	   add_terms  s^    )


	


c         C  sb  t  |   } | rj | \ } } t | | |  \ } } } } t | | |  \ }	 } }
 } | |	 | |
 f S| j d t  } d } | } x{t | d |  | d <g  |  j D] } t | | d |  ^ q } t g  | D]# } | d r | d d  d  ^ q | |  \ } } t g  | D]# } | d r| d d  d  ^ q| |  \ }	 }
 t | |	 | |
 f  } | | k r| j d  rt d | d | |
  n  Pq | | | d k rPn  | t	 d d | | |  } | d 7} | j d  r t d	 |  q q W| | d <t
 | d
 t r1t |  } n  t
 |	 d
 t rRt |	  }	 n  | |	 | |
 f S(   Nt   maxpreci    i   i
   i   t   verboses   ADD: wanteds   accurate bits, gots   ADD: restarting with precRI   (   RH   Re   R   t   DEFAULT_MAXPRECR{   Rw   R   Rc   t   printRY   RM   RN   RV   (   RB   Rl   Rm   Rs   RD   RF   R[   Ro   R]   R\   R^   t
   oldmaxprecRG   R   R   R   R   Rp   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt	   evalf_add  sB    ,??

c   )      C  s   t  |   } | rL | \ } } t | | |  \ } } } } d  | d  | f St |  j  } g  }	 d d l m }
 xt | D]l } t | | |  } | d d  k r qx n  |
 j | d d  } | t j	 k s | j
 rx |	 j |  qx qx W|	 rd d l m } | |	   }	 t |	 | d i   S| } | t |  d } t d  d d f } \ } } } t |  } d } | j t j  g  } xnt |  D]`\ } } | | k rt  |  r| d | j   | d <qn! | | k r| t j k rqn  t | | |  \ } } } } | r;| r;| j | | | | f  qnR | r]| | \ } } } } } n0 | r| | \ } } } } } | d 7} n d S| d | 7} | | 9} | | 7} | | 7} | d	 | k r| | L} | | 7} n  t | |  } qW| d @d ?} | sUt | | | t |  | t  }  | d @rBd  |  d  | f S|  d  | d  f Sn| | | f | k r| | | t |  f d t d  d d f } } d } nI | d \ }  }! }" }# t | t |  |! |" |# f   } |  } |! } d } x | | D] \ }  }! }" }# t | t |  |! |" |# f   } | }$ t | |  |$  }% t t |  |! |$  }& t | |! |$  }' t | |  |$  }( t |% |& |$  } t |' |( |$  } qW| j d
  rt d | d |  n  | d @rt |  | } } n  | | | | f Sd  S(   Ni(   R   i    i   (   t   Muli   i   i   i   R   s   MUL: wanteds   accurate bits, got(   NNNN(   RH   Re   Rh   RW   Rw   R   R   R   R3   R   R   R   t   sympy.core.mulR   RL   R+   t   Onet	   enumeratet   expandR{   R&   R*   R   Rc   R   R   R   R   R   ()   RB   Rl   Rm   Rs   Ro   RD   R\   R^   Rw   R   R   R   R   Rp   R   t   startR   R   R   t   lastt	   directiont   complex_factorsRG   R[   R]   RU   t   mt   eR   t   w_accRR   t   i0t   wret   wimt   wre_acct   wim_acct   use_prect   At   Bt   Ct   D(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt	   evalf_mul$  s    "



!
1	
c         C  sP  | } |  j  \ } } | j r| j } | s= t d  | d  f S| t t j t |  d   7} t	 | | d |  \ } } }	 }
 | r | r t
 | | |  d  | d  f S| rR| rRt
 | | |  } | d } | d k r | d  | d  f S| d k rd  | d  | f S| d k r-t |  d  | d  f S| d k rRd  t |  d  | f Sn  | s\d St j | | f | |  \ } } t | | |  S| t j k rIt	 | | d |  \ } } } } | rt j | pt | f |  \ } } t | | |  S| sd	 St | t  r0d  t t |  |  d  | f St | |  d  | d  f S| d 7} t	 | | |  \ } } } } | pz| st d  | d  f St |  } | d k r| | 7} t	 | | |  \ } } } } n  | t j k r2| rt j | pt | f |  \ } } t | | |  St | |  d  | d  f St	 | | d |  \ } } } } | p]| sdd
 S| rt j | pyt | pt f | pt | f |  \ } } t | | |  S| rt j | pt | f | |  \ } } t | | |  St | t  r0t j | t f | |  \ } } t | | |  St | | |  d  | d  f Sd  S(   Ni   i   i   i    i   i   i
   (   NNNN(   NNNN(   NNNN(   Rw   t
   is_IntegerRT   R   Rh   R7   t   matht   logR6   Re   R"   R   Ri   t   mpc_pow_intR~   R3   t   Halft   mpc_sqrtR   R   R%   R>   t   Exp1t   mpc_expR   t   mpc_powt   mpc_pow_mpfR!   (   RB   Rl   Rm   R   t   baseR   RT   R[   R\   R]   R^   t   zt   caset   xret   ximRo   t   yret   yimt   ysize(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt	   evalf_pow  s~    		""
!"$

!$"$'!c         C  s\  d d l  m } m } t |  |  r. t } n t |  |  rF t } n t  |  j d } | d } t | | |  \ } }	 }
 } |	 r d | k r |  j	 | d  }  n  t |  j
 |  | |  S| st |  |  r t d | d f St |  |  r d St  n  t |  } | d k  r7| | | t  d | d f S| d k rn| | } t | | |  \ } }	 }
 } n  x | | | t  } t |  } | } | | | } | | k  rD| j d  rt d	 | d
 | d |  t t | d   n  | | j d t  k r| d | d f S| | 7} t | | |  \ } }	 }
 } qqqq| d | d f SqqWd S(   sy   
    This function handles sin and cos of complex arguments.

    TODO: should also handle tan of complex arguments.
    i(   t   cost   sini    i   Rd   i   i
   R   s   SIN/COSt   wantedR   R   N(   NNNN(   t   sympyR   R   RO   R   R$   t   NotImplementedErrorRw   Re   Rd   t   _eval_evalfR   Rh   R>   R   R   R   R)   R   (   RB   Rl   Rm   R   R   t   funcR   t   xprecR[   R\   R]   R^   t   xsizet   yR   R   Ru   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt
   evalf_trig   sP    		
	
!
c         C  s  d d l  m } m } m } t |  j  d k rM |  j   }  t |  | |  S|  j d } | d } t | | |  \ } }	 }
 } |	 r t | | | d t	 d t	 | |  } t
 |	 | p t |  } | d | | d | f St | t  d k  } t t |  | t  } t |  } | | | k r| t k r| t j | d t	 } t | | |  \ } }	 } } | t |  } t t t | t |   | t  } n  | } | r| t |  | | f S| d  | d  f Sd  S(   Ni(   t   AbsR   R   i   i    i
   R   i   (   R   R   R   R   RL   Rw   R   Re   t	   evalf_logR   R   R   R   R   R   R   R>   R3   t   NegativeOneR   R   R   R    Rh   (   Rk   Rl   Rm   R   R   R   R   R
   R   R   t   xaccRo   R[   R\   t   imaginary_termR   t   prec2R]   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyR   =  s0    
''c         C  sz   |  j  d } t | | d |  \ } } } } | | k oF d  k n rO d S| r^ t  n  t | | t  d  | d  f S(   Ni    i   i   (   N(   NNNN(   Rw   Re   Rh   R   R   R   (   RB   Rl   Rm   R   R   R   t   reacct   imacc(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt
   evalf_atana  s    "	c         C  sX   i  } xK | j    D]= \ } } t |  } | j rF | j |   } n  | | | <q W| S(   s<    Change all Float entries in `subs` to have precision prec. (   t   itemsR3   t   is_FloatR   (   Rl   Rd   t   newsubsR   R   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt
   evalf_subsk  s    	c         C  s   d d l  m } m } d | k r |  j t | | d   }  | j   } | d =t |  d  rp t |  | |  St |   t	 k r t | |   | |  St |   t
 k r t | |   | |  Sn  t  d  S(   Ni(   R   t   IntegerRd   R   (   R   R   R  Rd   R  t   copyt   hasattrRe   RJ   t   floatR7   R   (   Rk   Rl   Rm   R   R  t   newopts(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   evalf_piecewisev  s    c         C  sc   |  j  d } | j s% t d   n  t |  } t | | t  } | t k rS d S| d  | d  f S(   Ni    s)   Bernoulli number index must be an integer(   NNNN(   Rw   R   RP   R7   R/   R   R   Rh   (   Rk   Rl   Rm   R   R8   R   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   evalf_bernoulli  s    	c   	      C  s   d d l  m } m } m } t |   }  t |  |  sC |  d k rM t d  St |  |  rf t d  St |  |  r t d  St |  | |  \ } } } } | r t | p t	 |  St |  S(   Ni(   t   Infinityt   NegativeInfinityt   Zeroi    R   s   -inf(
   R   R  R  R  R2   RO   R   Re   R   R   (	   R=   Rl   Rm   R  R  R  R[   R\   Ro   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt	   as_mpmath  s    


c      	     s  |  j  d   |  j  d \  } } | | k r< d } } nH    j k r | j | j @r | | } | j r d | } } q q n  | j d t  } t | d |  | d <t | d  t | | d |  } t | | d |  } d d l m	 } m
 } m }	 t t g  t g  t g        f d	   }
 | j d
  d k r3|	 d d  g } |	 d d  g } |	 d  }   j | |  |  |  } | s  j | |  |  |  } n  | st d   n  t d t j | | | d |  } t |
 | | g d | } t } n0 t |
 | | g d d \ } } t | j  } Wd  QX| | d < d r| j j } | t k rt t |  d |   \ } } t |  } qt  d t |  | |  } n d \ } }  d ru| j j } | t k rPt t |  d |   \ } } t |  } qt  d t |  | |  } n d \ } } | | | | f } | S(   Ni    i   R   i   i   i   i(   R   R   t   Wildc           s   t    t j i i |   6d 6 \ } } } } | p<  d  d <| pP  d  d <t  d t |    d <t  d t |    d <| r t | p t |  St | p t  S(   NRd   i    i   (   Re   R   Rl   RY   R>   R   R   R   (   RE   R[   R\   R]   R^   (   R   t	   have_partt   max_imag_termt   max_real_termR=   (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   f  s    /t   quadt   oscR   t   excludeR   R   sb   An integrand of the form sin(A*x+B)*f(x) or cos(A*x+B)*f(x) is required for oscillatory quadraturet   periodt   error(   NN(   NN(   Rw   t   free_symbolsRf   R   R   R{   R
   R  R   R   R   R  R   R<   t   matchRP   R3   t   PiR	   R   R>   t   _mpf_t   realR   RV   RY   Rh   t   imag(   Rk   Rl   Rm   t   xlowt   xhight   diffR   R   R   R  R  R   R   R   R   R  RZ   t   quadrature_errorR[   R]   R\   R^   (    (   R   R  R  R  R=   s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   do_integral  sl    
			!$%	!

"
"c   	      C  s   |  j  } t |  d k s1 t | d  d k r: t  n  | } d } | j d t  } x t |  | |  } t |  } | | k r Pn  | | k r Pn  | d k r | d 9} n | t | d |  7} t | |  } | d 7} q[ W| S(   Ni   i    i   R   ii   (	   t   limitsRL   R   R   RX   R$  Rc   RY   R{   (	   Rk   Rl   Rm   R%  R
   RG   R   RZ   Ru   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   evalf_integral  s&    	(	c         C  s  d d l  m } | |  |  } | | |  } | j   } | j   } | | } | rc | d d f S| j   | j   }	 t |	  d k r | |	 d f S| j   | j   k o d k n r | |	 d f S| j   d }
 | j   d } | |	 | |
 | j   f S(   s8  
    Returns (h, g, p) where
    -- h is:
        > 0 for convergence of rate 1/factorial(n)**h
        < 0 for divergence of rate factorial(n)**(-h)
        = 0 for geometric or polynomial convergence or divergence

    -- abs(g) is:
        > 1 for geometric convergence of rate 1/h**n
        < 1 for geometric divergence of rate h**n
        = 1 for polynomial convergence or divergence

        (g < 0 indicates an alternating series)

    -- p is:
        > 1 for polynomial convergence of rate 1/n**h
        <= 1 for polynomial divergence of rate n**(-h)

    i(   t   Polyi   i    N(   R   R'  t   degreeRh   t   LCR6   t
   all_coeffs(   t   numert   denomR8   R'  t   npolt   dpolRT   t   qt   ratet   constantt   pct   qc(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   check_convergence!  s     
(c      	     s  d d l  m } m } m } | t d  k r= t d   n  | r\ |  j | | |  }  n  | |  |  } | d k r t d   n  | j   \ } }	 | | |    | | |	   t	 | |	 |  \ }
 } } |
 d k  r t
 d |
   n  |  j | d  } | j st d   n  |
 d k sE|
 d k rt |  d	 k rt | j  | >| j } | } d	 } x^ t |  d
 k r| t   | d	   9} | t  | d	   } | | 7} | d	 7} qnWt | |  S| d k  } t |  d	 k  rt
 d t d	 |    n  | d	 k  s4| d	 k rH| rHt
 d |   n  d } t |  } x t rd |  t | j   >| j } | g     f d  } t |    t | d t g d d } Wd QX| | |  } | d k	 r| | k rPn  | | 7} | } q]W| j Sd S(   s   
    Sum a rapidly convergent infinite hypergeometric series with
    given general term, e.g. e = hypsum(1/factorial(n), n). The
    quotient between successive terms must be a quotient of integer
    polynomials.
    i(   R   t	   hypersimpt   lambdifyR   s   does not support inf precs#   a hypergeometric series is requiredi    s   Sum diverges like (n!)^%is3   Non rational term functionality is not implemented.i   i   s   Sum diverges like (%i)^ns   Sum diverges like n^%ii   c           sm   |  rU t  |   }  | d c t   |  d   9<| d c t  |  d   <n  t t | d    S(   Ni    i   (   R7   R+   R   R   (   t   kt   _term(   t   func1t   func2R   (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   summand  s
     #t   methodt
   richardsonN(   R   R   R5  R6  R  R   Rd   Rh   t   as_numer_denomR4  RP   t   is_RationalR6   R+   RT   R/  R   R.   RN   R
   R   t
   mpmath_infR  (   Rk   R8   R   Rl   R   R5  R6  t   hst   numt   denRD   t   gRT   t   termRU   R7  t   altt   voldt   ndigt   term0R;  RB   t   vf(    (   R9  R:  R   s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   hypsumG  s\    	*
	
!

c         C  s   d d l  m } t d   |  j D  rV t |  j   d | d | \ } } } } n- t |  j |  d | d | \ } } } } | | | | f S(   Ni(   t   Sumc         s  s$   |  ] } | d  | d j  Vq d S(   i   i   N(   R   (   t   .0t   l(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pys	   <genexpr>  s    Rl   Rm   (   R   RL  t   allR%  Re   R   t   rewrite(   Rk   Rl   Rm   RL  R[   R\   R]   R^   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt
   evalf_prod  s
    --c      
   C  sm  d d l  m } d | k r2 |  j | d  }  n  |  j } |  j } t |  d k sl t | d  d k ru t  n  | t j k r t	 d  d  d  d  f S| d } y | d \ } } }	 |	 t j k s | t |  k r t  n  t | | t |  |  }
 | t |
  } t |
  d k  r?t | | t |  |  }
 n  |
 d  t | |  d  f SWnt k
 rh| d	  | } xo t d d
  D]^ } d | | } } |  j d | d | d | d t  \ } } | j   } | | k rPqqWt t t |  d |  d  } t | | |  \ } } } } | d  k rB| } n  | d  k rX| } n  | | | | f SXd  S(   Ni(   R   Rd   i   i    i   i
   ig       @i   i   R   R8   t   epst   eval_integrali   (   R   R   Rd   t   functionR%  RL   R   R3   R  R   Rh   R  R7   RK  R>   R{   R1   t   euler_maclaurinR   Re   R6   (   Rk   Rl   Rm   R   R   R%  R   R8   R   R   RB   R   RR  RG   R   RU   t   errR[   R\   R]   R^   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt	   evalf_sum  sF    		(	
!	"

c         C  s   | d |  } t  | t  r: | s' d S| j d  | d  f Sd | k rS i  | d <n  | d } | j |  d  t f  \ } } | | k r | St t |  | |  } | | f | |  <| Sd  S(   NRd   t   _cache(   NNNN(   RO   R   Rh   R  R   R<   Re   R2   (   R=   Rl   Rm   t   valt   cachet   cachedt   cached_precRB   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   evalf_symbol  s    
c            sS  d d l  m }  d d l m } d d l m } d d l m } d d l m	 } d d l
 m } m } m } m } m }	 m }
 m } m } m } m } m } d d l m   d d	 l m } m } d d
 l m } m } m } d d l m  } m! } d d l" m# } m$ } d d l% m& } d d l' m( } m) } m* } d d l+ m, } i t- | 6t- | 6d   | 6d   | 6d   |	 6d   | 6d   | 6d   | 6d   | 6d   | 6d   | 6d   | 6d   |
 6  f d   | 6t. | 6t. | 6t/ | 6t0 | 6t1   6t2 | 6t3 | 6t4 | 6t5 | 6t6 | 6t7 | 6t8 | 6t9 | 6t: | 6t; | 6t< | 6t= |  6a> d  S(   Ni(   t	   bernoulli(   t   Product(   RL  (   R   (   R   (   R   R   R   RA   R  R   R   R   R  t   RationalR  (   t   Pow(   t   Dummyt   Symbol(   R   R\   R[   (   R   R   (   t   ceilingt   floor(   t	   Piecewise(   t   atanR   R   (   t   Integralc         S  s   |  j  d  | d  f S(   N(   R  Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   <lambda>  t    c         S  s"   t  |  j |  j |  d  | d  f S(   N(   R   RT   R/  Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   t  |  j |  d  | d  f S(   N(   R   RT   Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   d  d  | d  f S(   N(   Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   t  d  | d  f S(   N(   R   Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   t  d  | d  f S(   N(   R   Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   t  |  d  | d  f S(   N(   R    Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   t  |  d  | d  f S(   N(   R   Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   d  t d  | f S(   N(   Rh   R   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   t  d  | d  f S(   N(   R   Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c         S  s   t  d  | d  f S(   N(   R   Rh   (   R=   Rl   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    Rj  c           s)   t    t j |  j d d t | |  S(   Ni    R   (   R   R3   R   Rw   R   (   R=   Rl   Rm   (   Ra  (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRi    s   (?   t%   sympy.functions.combinatorial.numbersR^  t   sympy.concrete.productsR_  t   sympy.concrete.summationsRL  R   R   R   R   R   R   R   R   RA   R  R   R   R   R  R`  R  t   sympy.core.powerRa  t   sympy.core.symbolRb  Rc  R   R   R\   R[   t&   sympy.functions.elementary.exponentialR   R   t#   sympy.functions.elementary.integersRd  Re  t$   sympy.functions.elementary.piecewiseRf  t(   sympy.functions.elementary.trigonometricRg  R   R   t   sympy.integrals.integralsRh  R]  R   R   R   R   R   R   Rx   Ry   Rz   R   R   R&  RW  RQ  R
  R  t   evalf_table(   R^  R_  RL  R   R   R   R   R   RA   R  R   R   R   R  R`  R  Rb  Rc  R   R\   R[   R   R   Rd  Re  Rf  Rg  R   R   Rh  (    (   Ra  s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   _create_evalf_table  s\    L










c         C  s  d d l  m } m } y# t |  j } | |  | |  } Wnet k
 rd | k rt |  j t | | d   }  n  |  j |  } | d  k r t
  n  t | d d   } | d  k r t
  n  |   \ }	 }
 |	 j |  s |
 j |  r t
  n  |	 d k rd  }	 d  } n0 |	 j r:|	 j | d t j }	 | } n t
  |
 d k r[d  }
 d  } n0 |
 j r|
 j | d t j }
 | } n t
  |	 |
 | | f } n X| j d  rt d |   t d	 t | d pt d
   t d |  t   n  | j d t  } | ro| t k r!| } n< t t d t j |  d   } | d k r]| d 8} n  t | |  } n  | j d  rt |  | |  n  | S(   Ni(   R[   R\   Rd   R   i    t
   allow_intsR   s	   ### inputs
   ### outputi2   s   ### rawt   chopg rh
g      @i   i   R   (   R   R[   R\   Ru  R   t   KeyErrorRd   R  R   Rh   R   t   getattrt   hasRf   t
   _to_mpmathR   R  R   R   R)   R   RN   R7   t   roundR   t   log10R   R   (   R=   Rl   Rm   R   R   t   rfR   t   xeR   R[   R\   t   reprect   imprecRx  t	   chop_prec(    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRe     s^    									 
	#t
   EvalfMixinc           B  sV   e  Z d  Z g  Z d d d e e d e d  Z e Z d   Z d   Z	 e
 d  Z RS(   s%   Mixin class adding evalf capabililty.i   id   c         C  sN  d d l  m } m }	 | d k	 r( | n d } | rO t |  rO t d   n  | d k r t |  |	  r d d l m }
 |  j	 d | | | | | |  } |
 |  } | j
 d |  } | St s t   n  t |  } i t | t | t   d 6| d	 6| d
 6| d 6} | d k	 r(| | d <n  | d k	 rA| | d <n  y t	 |  | d |  } Wn[ t k
 r|  j |  } | d k r|  Sy t	 | | |  } Wqt k
 r| SXn X| \ } } } } | rt t | |  d  } | j | |  } n	 t j } | rFt t | |  d  } | j | |  } | | t j S| Sd S(   s  
        Evaluate the given formula to an accuracy of n digits.
        Optional keyword arguments:

            subs=<dict>
                Substitute numerical values for symbols, e.g.
                subs={x:3, y:1+pi}. The substitutions must be given as a
                dictionary.

            maxn=<integer>
                Allow a maximum temporary working precision of maxn digits
                (default=100)

            chop=<bool>
                Replace tiny real or imaginary parts in subresults
                by exact zeros (default=False)

            strict=<bool>
                Raise PrecisionExhausted if any subresult fails to evaluate
                to full accuracy, given the available maxprec
                (default=False)

            quad=<str>
                Choose algorithm for numerical quadrature. By default,
                tanh-sinh quadrature is used. For oscillatory
                integrals on an infinite interval, try quad='osc'.

            verbose=<bool>
                Print debug information (default=False)

        Notes
        =====

        When Floats are naively substituted into an expression, precision errors
        may adversely affect the result. For example, adding 1e16 (a Float) to 1
        will truncate to 1e16; if 1e16 is then subtracted, the result will be 0.
        That is exactly what happens in the following:

        >>> from sympy.abc import x, y, z
        >>> values = {x: 1e16, y: 1, z: 1e16}
        >>> (x + y - z).subs(values)
        0

        Using the subs argument for evalf is the accurate way to evaluate such an
        expression:

        >>> (x + y - z).evalf(subs=values)
        1.00000000000000
        i(   R   t   Numberi   s"   subs must be given as a dictionaryi   (   t   _magi   R   Rx  R   R   Rd   R  i   N(   R   R   R  Rh   R4   t	   TypeErrorRO   t   sympy.core.exprR  Re   R}  Ru  Rv  R-   RY   R7   t   LG10R   R   R{   R   R3   R  RA   (   t   selfR8   Rd   t   maxnRx  R   R  R   R   R  R  RS   R   Rl   Rm   RZ   RB   R[   R\   R]   R^   RT   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRe   X  sN    2!
$		c         C  s(   |  j  |  } | d k r$ |  } n  | S(   s@   Helper for evalf. Does the same thing but takes binary precisionN(   R   Rh   (   R  Rl   R   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   _evalf  s    	c         C  s   d  S(   N(    (   R  Rl   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyR     s    c         C  s  d } | r |  j  r |  j St |  d  r> t |  j |   Sya t |  | i   \ } } } } | r | st t } n  t | | f  S| r t |  St t  SWn t k
 r|  j	 |  } | d  k r t |   n  | j r t | j  S| j   \ } } | r"| j  r"t | j  } n! | j r7| j } n t |   | rd| j  rdt | j  } n! | j ry| j } n t |   t | | f  SXd  S(   Ns   cannot convert to mpmath numbert   _as_mpf_val(   R   RT   R  R   R  Re   R   R   R   R   Rh   RP   R  R  R   R   (   R  Rl   Rw  t   errmsgR[   R\   Ro   RB   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyR|    s@    	
			N(   R:   R;   t   __doc__t	   __slots__Rh   R   Re   R8   R  R   RN   R|  (    (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyR  S  s   c		i   c         K  s   t  |   j | |  S(   s}  
    Calls x.evalf(n, \*\*options).

    Both .n() and N() are equivalent to .evalf(); use the one that you like better.
    See also the docstring of .evalf() for information on the options.

    Examples
    ========

    >>> from sympy import Sum, oo, N
    >>> from sympy.abc import k
    >>> Sum(1/k**k, (k, 1, oo))
    Sum(k**(-k), (k, 1, oo))
    >>> N(_, 4)
    1.291

    (   R2   Re   (   R=   R8   Rm   (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyRg     s    (w   R  t
   __future__R    R   R   t   mpmath.libmpRi   t   mpmathR   R   R   R   R   R   R   R	   R
   R   R@  R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R    R!   R"   R#   R$   R%   R&   R'   R(   R)   R*   R5   t   mpmath.libmp.backendR+   t   mpmath.libmp.libmpcR,   t   mpmath.libmp.libmpfR-   R.   t   mpmath.libmp.gammazetaR/   t   compatibilityR0   R1   R2   t	   singletonR3   t   sympy.utilities.iterablesR4   R   R  R   R  RX   R<   R   t   ArithmeticErrorR9   R>   R   RH   RV   RM   Rc   Rq   Rv   Rx   Ry   Rz   R~   R   R   R   R   R   R   R   R   R   R   R   R   R  R
  R  R  R$  R&  R4  RK  RQ  RW  R]  Rh   Ru  Rv  Re   t   objectR  Rg   (    (    (    s/   lib/python2.7/site-packages/sympy/core/evalf.pyt   <module>   sz   @	 	!&									h		
	S	*	r	j	=	$	
					_		&	L			.		;	: