ó
<Zc           @   s]  d  d l  m Z m Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z
 d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m  Z  d e! f d „  ƒ  YZ" d e" e e e
 e e e e e e e e e e f d „  ƒ  YZ# d S(   iÿÿÿÿ(   t   gtt   lti   (   t   xrange(   t   SpecialFunctions(   t   RSCache(   t   QuadratureMethods(   t    LaplaceTransformInversionMethods(   t   CalculusMethods(   t   OptimizationMethods(   t
   ODEMethods(   t   MatrixMethods(   t   MatrixCalculusMethods(   t   LinearAlgebraMethods(   t   Eigen(   t   IdentificationMethods(   t   VisualizationMethods(   t   libmpt   Contextc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyR      s   t   StandardBaseContextc           B   sÐ  e  Z e j Z e j Z d  „  Z d „  Z e Z e Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z e e d „ Z d e d „ Z d „  Z d d „ Z d d „ Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z e  e j! ƒ Z" e  e j# ƒ Z# e  e j$ ƒ Z$ e  e j% ƒ Z% e  e j& ƒ Z& e  e j' ƒ Z( e  e j) ƒ Z* e  e j+ ƒ Z, e  e j- ƒ Z. d d „ Z/ d d „ Z0 d „  Z1 d „  Z2 d „  Z3 d „  Z4 RS(    c         C   s[   i  |  _  t j |  ƒ t j |  ƒ t j |  ƒ t j |  ƒ t j |  ƒ t j |  ƒ d  S(   N(   t   _aliasesR   t   __init__R   R   R   R   R
   (   t   ctx(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyR   *   s    	c         C   sU   xN |  j  j ƒ  D]= \ } } y t |  | t |  | ƒ ƒ Wq t k
 rL q Xq Wd  S(   N(   R   t   itemst   setattrt   getattrt   AttributeError(   R   t   aliast   value(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   _init_aliases4   s
    c         C   s   d | f GHd  S(   Ns   Warning:(    (   R   t   msg(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   warn@   s    c         C   s   t  | ƒ ‚ d  S(   N(   t
   ValueError(   R   R   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt
   bad_domainC   s    c         C   s   t  | d ƒ r | j S| S(   Nt   real(   t   hasattrR#   (   R   t   x(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   _reF   s    c         C   s   t  | d ƒ r | j S|  j S(   Nt   imag(   R$   R'   t   zero(   R   R%   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   _imK   s    c         C   s   | S(   N(    (   R   R%   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt
   _as_pointsP   s    c         K   s   |  j  | ƒ S(   N(   t   convert(   R   R%   t   kwargs(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   fnegS   s    c         K   s   |  j  | ƒ |  j  | ƒ S(   N(   R+   (   R   R%   t   yR,   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   faddV   s    c         K   s   |  j  | ƒ |  j  | ƒ S(   N(   R+   (   R   R%   R.   R,   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   fsubY   s    c         K   s   |  j  | ƒ |  j  | ƒ S(   N(   R+   (   R   R%   R.   R,   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   fmul\   s    c         K   s   |  j  | ƒ |  j  | ƒ S(   N(   R+   (   R   R%   R.   R,   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   fdiv_   s    c         C   sp   | r@ | r& t  d „  | Dƒ |  j ƒ St  d „  | Dƒ |  j ƒ S| r` t  d „  | Dƒ |  j ƒ St  | |  j ƒ S(   Nc         s   s   |  ] } t  | ƒ d  Vq d S(   i   N(   t   abs(   t   .0R%   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pys	   <genexpr>e   s    c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R3   (   R4   R%   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pys	   <genexpr>f   s    c         s   s   |  ] } | d  Vq d S(   i   N(    (   R4   R%   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pys	   <genexpr>h   s    (   t   sumR(   (   R   t   argst   absolutet   squared(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   fsumb   s    c            sk   | d  k	 r t | | ƒ } n  | rM |  j ‰  t ‡  f d †  | Dƒ |  j ƒ St d „  | Dƒ |  j ƒ Sd  S(   Nc         3   s%   |  ] \ } } | ˆ  | ƒ Vq d  S(   N(    (   R4   R%   R.   (   t   cf(    s.   lib/python2.7/site-packages/mpmath/ctx_base.pys	   <genexpr>p   s    c         s   s   |  ] \ } } | | Vq d  S(   N(    (   R4   R%   R.   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pys	   <genexpr>r   s    (   t   Nonet   zipt   conjR5   R(   (   R   t   xst   yst	   conjugate(    (   R:   s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   fdotk   s    	 c         C   s(   |  j  } x | D] } | | 9} q W| S(   N(   t   one(   R   R6   t   prodt   arg(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   fprodt   s    	i   c         K   s   |  j  | | |  GHd S(   s6   
        Equivalent to ``print(nstr(x, n))``.
        N(   t   nstr(   R   R%   t   nR,   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   nprintz   s    c            s2  ˆ d k r d ˆ  j ‰ n  y¡ ˆ  j | ƒ } t | ƒ } t | ƒ ˆ k  rS ˆ  j Sˆ  j | ƒ r¼ t ˆ | ˆ ƒ } t | j ƒ | k  r‘ | j St | j ƒ | k  r¼ ˆ  j	 d | j ƒ Sn  Wnn t
 k
 r-t | ˆ  j ƒ rø | j ‡  ‡ f d †  ƒ St | d ƒ r.g  | D] } ˆ  j | ˆ ƒ ^ qSn X| S(   sÖ  
        Chops off small real or imaginary parts, or converts
        numbers close to zero to exact zeros. The input can be a
        single number or an iterable::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> chop(5+1e-10j, tol=1e-9)
            mpf('5.0')
            >>> nprint(chop([1.0, 1e-20, 3+1e-18j, -4, 2]))
            [1.0, 0.0, 3.0, -4.0, 2.0]

        The tolerance defaults to ``100*eps``.
        id   i    c            s   ˆ  j  |  ˆ ƒ S(   N(   t   chop(   t   a(   R   t   tol(    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   <lambda>Ÿ   s    t   __iter__N(   R;   t   epsR+   R3   R(   t   _is_complex_typet   maxR'   R#   t   mpct	   TypeErrort
   isinstancet   matrixt   applyR$   RI   (   R   R%   RK   t   absxt   part_tolRJ   (    (   R   RK   s.   lib/python2.7/site-packages/mpmath/ctx_base.pyRI   €   s&    'c   	      C   s×   |  j  | ƒ } | d k rH | d k rH |  j d |  j d ƒ } } n  | d k r] | } n | d k rr | } n  t | | ƒ } | | k r’ t St | ƒ } t | ƒ } | | k  rÃ | | } n
 | | } | | k S(   sÖ  
        Determine whether the difference between `s` and `t` is smaller
        than a given epsilon, either relatively or absolutely.

        Both a maximum relative difference and a maximum difference
        ('epsilons') may be specified. The absolute difference is
        defined as `|s-t|` and the relative difference is defined
        as `|s-t|/\max(|s|, |t|)`.

        If only one epsilon is given, both are set to the same value.
        If none is given, both epsilons are set to `2^{-p+m}` where
        `p` is the current working precision and `m` is a small
        integer. The default setting typically allows :func:`~mpmath.almosteq`
        to be used to check for mathematical equality
        in the presence of small rounding errors.

        **Examples**

            >>> from mpmath import *
            >>> mp.dps = 15
            >>> almosteq(3.141592653589793, 3.141592653589790)
            True
            >>> almosteq(3.141592653589793, 3.141592653589700)
            False
            >>> almosteq(3.141592653589793, 3.141592653589700, 1e-10)
            True
            >>> almosteq(1e-20, 2e-20)
            True
            >>> almosteq(1e-20, 2e-20, rel_eps=0, abs_eps=0)
            False

        i   i   N(   R+   R;   t   ldexpt   precR3   t   True(	   R   t   st   tt   rel_epst   abs_epst   difft   absst   abstt   err(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   almosteq¤   s     !!		
c   	      G   s¤  t  | ƒ d k s+ t d t  | ƒ ƒ ‚ n  t  | ƒ d k sV t d t  | ƒ ƒ ‚ n  d } d } t  | ƒ d k r | d } n) t  | ƒ d k rª | d } | d } n  t  | ƒ d k rÉ | d } n  |  j | ƒ |  j | ƒ |  j | ƒ } } } | | | k st d ƒ ‚ | | k r9| d k r0g  St } n | d k  rIg  St } g  } d } | } x< | | | } | d 7} | | | ƒ r›| j | ƒ qdPqdW| S(   sa  
        This is a generalized version of Python's :func:`~mpmath.range` function
        that accepts fractional endpoints and step sizes and
        returns a list of ``mpf`` instances. Like :func:`~mpmath.range`,
        :func:`~mpmath.arange` can be called with 1, 2 or 3 arguments:

        ``arange(b)``
            `[0, 1, 2, \ldots, x]`
        ``arange(a, b)``
            `[a, a+1, a+2, \ldots, x]`
        ``arange(a, b, h)``
            `[a, a+h, a+h, \ldots, x]`

        where `b-1 \le x < b` (in the third case, `b-h \le x < b`).

        Like Python's :func:`~mpmath.range`, the endpoint is not included. To
        produce ranges where the endpoint is included, :func:`~mpmath.linspace`
        is more convenient.

        **Examples**

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> arange(4)
            [mpf('0.0'), mpf('1.0'), mpf('2.0'), mpf('3.0')]
            >>> arange(1, 2, 0.25)
            [mpf('1.0'), mpf('1.25'), mpf('1.5'), mpf('1.75')]
            >>> arange(1, -1, -0.75)
            [mpf('1.0'), mpf('0.25'), mpf('-0.5')]

        i   s+   arange expected at most 3 arguments, got %ii   s+   arange expected at least 1 argument, got %ii    i   s0   dt is too small and would cause an infinite loop(   t   lenRR   t   mpft   AssertionErrorR    R   t   append(	   R   R6   RJ   t   dtt   bt   opt   resultt   iR\   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   arange×   sD     
/	
c   	      O   s—  t  | ƒ d k rK |  j | d ƒ } |  j | d ƒ } t | d ƒ } nn t  | ƒ d k r£ t | d d ƒ sv t ‚ | d j } | d j } t | d ƒ } n t d t  | ƒ ƒ ‚ | d k  rÔ t d ƒ ‚ n  d | k sê | d rU| d k r|  j | ƒ g S| | |  j | d ƒ } g  t	 | ƒ D] } | | | ^ q.} | | d	 <n> | | |  j | ƒ } g  t	 | ƒ D] } | | | ^ qy} | S(
   sÄ  
        ``linspace(a, b, n)`` returns a list of `n` evenly spaced
        samples from `a` to `b`. The syntax ``linspace(mpi(a,b), n)``
        is also valid.

        This function is often more convenient than :func:`~mpmath.arange`
        for partitioning an interval into subintervals, since
        the endpoint is included::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = False
            >>> linspace(1, 4, 4)
            [mpf('1.0'), mpf('2.0'), mpf('3.0'), mpf('4.0')]

        You may also provide the keyword argument ``endpoint=False``::

            >>> linspace(1, 4, 4, endpoint=False)
            [mpf('1.0'), mpf('1.75'), mpf('2.5'), mpf('3.25')]

        i   i    i   i   t   _mpi_s*   linspace expected 2 or 3 arguments, got %is   n must be greater than 0t   endpointiÿÿÿÿ(
   Rd   Re   t   intR$   Rf   RJ   Ri   RR   R!   R   (	   R   R6   R,   RJ   Ri   RG   t   stepRl   R.   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   linspace   s,    ''c         K   s"   |  j  | |  |  j | |  f S(   N(   t   cost   sin(   R   t   zR,   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   cos_sinN  s    c         K   s"   |  j  | |  |  j | |  f S(   N(   t   cospit   sinpi(   R   Ru   R,   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   cospi_sinpiQ  s    c         C   s   t  d | d d | ƒ S(   Niè  g      Ð?i   (   Rp   (   R   t   p(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   _default_hyper_maxprecT  s    i   c         C   s!  |  j  } zd } xö | | d |  _  |  j } |  j } d } x} | ƒ  D]r } | | 7} | | r° | r° |  j | ƒ }	 t | |	 ƒ } |  j | ƒ }
 |
 |	 |  j  k r° Pq° n  | d 7} qH W| |
 } | | k rØ Pn  | | k  sí |  j rñ Pn  | t |  j  | ƒ 7} q W| SWd  | |  _  Xd  S(   Ni
   i   i    i   (   RY   t   ninfR(   t   magRP   t   _fixed_precisiont   min(   R   t   termst
   check_stepRY   t	   extraprect   max_magR[   t   kt   termt   term_magt   sum_magt   cancellation(    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   sum_accuratelya  s2    			

c         C   s+  |  j  } zd } x | | d |  _  |  j } |  j } | } d } x | ƒ  D]v }	 | |	 9} |	 | }
 | | sº |  j |
 ƒ } t | | ƒ } |  j | | ƒ } | |  j  k rº Pqº n  | d 7} qN W| | } | | k râ Pn  | | k  s÷ |  j rû Pn  | t |  j  | ƒ 7} q W| SWd  | |  _  Xd  S(   Ni
   i   i    i   (   RY   R|   RB   R}   RP   R~   R   (   R   t   factorsR   RY   R‚   Rƒ   RB   R[   R„   t   factorR…   R†   R‡   Rˆ   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   mul_accurately}  s6    			



c         C   s   |  j  | ƒ |  j  | ƒ S(   s  Converts `x` and `y` to mpmath numbers and evaluates
        `x^y = \exp(y \log(x))`::

            >>> from mpmath import *
            >>> mp.dps = 30; mp.pretty = True
            >>> power(2, 0.5)
            1.41421356237309504880168872421

        This shows the leading few digits of a large Mersenne prime
        (performing the exact calculation ``2**43112609-1`` and
        displaying the result in Python would be very slow)::

            >>> power(2, 43112609)-1
            3.16470269330255923143453723949e+12978188
        (   R+   (   R   R%   R.   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   power  s    c         C   s   |  j  | ƒ S(   N(   t   zeta(   R   RG   (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt	   _zeta_int¯  s    c            s%   d g ‰ ‡  ‡ ‡ ‡ f d †  } | S(   sù  
        Return a wrapped copy of *f* that raises ``NoConvergence`` when *f*
        has been called more than *N* times::

            >>> from mpmath import *
            >>> mp.dps = 15
            >>> f = maxcalls(sin, 10)
            >>> print(sum(f(n) for n in range(10)))
            1.95520948210738
            >>> f(10) # doctest: +IGNORE_EXCEPTION_DETAIL
            Traceback (most recent call last):
              ...
            NoConvergence: maxcalls: function evaluated 10 times

        i    c             sC   ˆ d c d 7<ˆ d ˆ  k r6 ˆ j  d ˆ  ƒ ‚ n  ˆ |  | Ž  S(   Ni    i   s%   maxcalls: function evaluated %i times(   t   NoConvergence(   R6   R,   (   t   Nt   counterR   t   f(    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   f_maxcalls_wrappedÃ  s    (    (   R   R“   R‘   R”   (    (   R‘   R’   R   R“   s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   maxcalls²  s    	c            s7   i  ‰ ‡  ‡ ‡ f d †  } ˆ j  | _  ˆ j | _ | S(   s™  
        Return a wrapped copy of *f* that caches computed values, i.e.
        a memoized copy of *f*. Values are only reused if the cached precision
        is equal to or higher than the working precision::

            >>> from mpmath import *
            >>> mp.dps = 15; mp.pretty = True
            >>> f = memoize(maxcalls(sin, 1))
            >>> f(2)
            0.909297426825682
            >>> f(2)
            0.909297426825682
            >>> mp.dps = 25
            >>> f(2) # doctest: +IGNORE_EXCEPTION_DETAIL
            Traceback (most recent call last):
              ...
            NoConvergence: maxcalls: function evaluated 1 times

        c             sƒ   | r! |  t  | j ƒ  ƒ f } n |  } ˆ  j } | ˆ k r` ˆ | \ } } | | k r` | 
Sn  ˆ |  | Ž  } | | f ˆ | <| S(   N(   t   tupleR   RY   (   R6   R,   t   keyRY   t   cprect   cvalueR   (   R   R“   t   f_cache(    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   f_cachedß  s    	(   R   t   __doc__(   R   R“   R›   (    (   R   R“   Rš   s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   memoizeÊ  s
    N(5   R   R   R   R   t   ComplexResultR   R   t   FalseR~   t   verboseR    R"   R&   R)   R*   R-   R/   R0   R1   R2   R9   R;   RA   RE   RH   RI   Rc   Rm   Rr   Rv   Ry   R{   t   staticmethodt   gcdt   _gcdt   list_primest   isprimet   bernfract   moebiust   ifact   _ifact   eulernumt	   _eulernumt	   stirling1t
   _stirling1t	   stirling2t
   _stirling2R‰   RŒ   R   R   R•   R   (    (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyR      sT   			
														$3	I	.			 			N($   t   operatorR    R   t   libmp.backendR   t   functions.functionsR   t   functions.rszetaR   t   calculus.quadratureR   t   calculus.inverselaplaceR   t   calculus.calculusR   t   calculus.optimizationR   t   calculus.odesR	   t   matrices.matricesR
   t   matrices.calculusR   t   matrices.linalgR   t   matrices.eigenR   t   identificationR   t   visualizationR   t    R   t   objectR   R   (    (    (    s.   lib/python2.7/site-packages/mpmath/ctx_base.pyt   <module>   s<   