
    "`q>                        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   G d de!      Z" G d de"eee
eeeeeeeeee      Z#y)    )gtlt   )xrange)SpecialFunctions)RSCache)QuadratureMethods) LaplaceTransformInversionMethods)CalculusMethods)OptimizationMethods)
ODEMethods)MatrixMethods)MatrixCalculusMethods)LinearAlgebraMethods)Eigen)IdentificationMethods)VisualizationMethods)libmpc                       e Zd Zy)ContextN)__name__
__module____qualname__     1/usr/lib/python3/dist-packages/mpmath/ctx_base.pyr   r      s    r   r   c                   D   e Zd Zej                  Zej
                  Zd Zd ZdZdZ	d Z
d Zd Zd Zd Zd	 Zd
 Zd Zd Zd Zd dZd!dZd Zd"dZd#dZd$dZd Zd Zd Zd Zd Z eej@                        Z! eejD                        Z" eejF                        Z# eejH                        Z$ eejJ                        Z% eejL                        Z' eejP                        Z) eejT                        Z+ eejX                        Z-d%dZ.d%dZ/d Z0d Z1d Z2d Z3y)&StandardBaseContextc                    i | _         t        j                  |        t        j                  |        t	        j                  |        t        j                  |        t        j                  |        t        j                  |        y N)_aliasesr   __init__r   r	   r
   r   r   )ctxs    r   r"   zStandardBaseContext.__init__*   s]    !!#&""3'(11#6  %s#r   c           	          | j                   j                         D ]  \  }}	 t        | |t        | |              y # t        $ r Y ,w xY wr    )r!   itemssetattrgetattrAttributeError)r#   aliasvalues      r   _init_aliasesz!StandardBaseContext._init_aliases4   sN    LL..0 	LE5UGC$78	 " s   <	AAFc                     t        d|       y )NzWarning:)printr#   msgs     r   warnzStandardBaseContext.warn@   s    j#r   c                     t        |      r    )
ValueErrorr.   s     r   
bad_domainzStandardBaseContext.bad_domainC   s    or   c                 6    t        |d      r|j                  S |S )Nreal)hasattrr5   r#   xs     r   _rezStandardBaseContext._reF   s    1f66Mr   c                 J    t        |d      r|j                  S | j                  S )Nimag)r6   r;   zeror7   s     r   _imzStandardBaseContext._imK   s    1f66Mxxr   c                     |S r    r   r7   s     r   
_as_pointszStandardBaseContext._as_pointsP   s    r   c                 &    | j                  |       S r    convert)r#   r8   kwargss      r   fnegzStandardBaseContext.fnegS   s    Ar   c                 H    | j                  |      | j                  |      z   S r    rA   r#   r8   yrC   s       r   faddzStandardBaseContext.faddV       {{1~ckk!n,,r   c                 H    | j                  |      | j                  |      z
  S r    rA   rF   s       r   fsubzStandardBaseContext.fsubY   rI   r   c                 H    | j                  |      | j                  |      z  S r    rA   rF   s       r   fmulzStandardBaseContext.fmul\   rI   r   c                 H    | j                  |      | j                  |      z  S r    rA   rF   s       r   fdivzStandardBaseContext.fdiv_   rI   r   c                     |r<|rt        d |D        | j                        S t        d |D        | j                        S |rt        d |D        | j                        S t        || j                        S )Nc              3   8   K   | ]  }t        |      d z    yw   Nabs.0r8   s     r   	<genexpr>z+StandardBaseContext.fsum.<locals>.<genexpr>e   s     4!CFAI4s   c              3   2   K   | ]  }t        |        y wr    rT   rV   s     r   rX   z+StandardBaseContext.fsum.<locals>.<genexpr>f   s     -1A-s   c              3   &   K   | ]	  }|d z    ywrR   r   rV   s     r   rX   z+StandardBaseContext.fsum.<locals>.<genexpr>h   s     +1+s   )sumr<   )r#   argsabsolutesquareds       r   fsumzStandardBaseContext.fsumb   sa    4t4chh??--sxx88+d+SXX664""r   Nc                     |t        ||      }|r+| j                  t        fd|D        | j                        S t        d |D        | j                        S )Nc              3   :   K   | ]  \  }}| |      z    y wr    r   )rW   r8   rG   cfs      r   rX   z+StandardBaseContext.fdot.<locals>.<genexpr>p   s     0EQq"Q%0s   c              3   ,   K   | ]  \  }}||z    y wr    r   )rW   r8   rG   s      r   rX   z+StandardBaseContext.fdot.<locals>.<genexpr>r   s     ,1!,s   )zipconjr[   r<   )r#   xsys	conjugaterb   s       @r   fdotzStandardBaseContext.fdotk   sM    >RBB0R0#((;;,,chh77r   c                 6    | j                   }|D ]  }||z  }	 |S r    )one)r#   r\   prodargs       r   fprodzStandardBaseContext.fprodt   s(    ww 	CCKD	r   c                 >    t         | j                  ||fi |       y)z6
        Equivalent to ``print(nstr(x, n))``.
        N)r-   nstr)r#   r8   nrC   s       r   nprintzStandardBaseContext.nprintz   s     	hchhq!&v&'r   c                 l    d j                   z  	  j                  |      }t        |      }t        |      k  r j                  S  j	                  |      rgt        |z        }t        |j                        |k  r|j                  S t        |j                        |k  r j                  d|j                        S |S # t        $ re t        | j                        r|j                   fd      cY S t        |d      r(|D cg c]  } j                  |       nc c}w c}cY S Y |S w xY w)a  
        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``.
        d   r   c                 (    j                  |       S r    )chop)ar#   tols    r   <lambda>z*StandardBaseContext.chop.<locals>.<lambda>   s    !S)9 r   __iter__)epsrB   rU   r<   _is_complex_typemaxr;   r5   mpc	TypeError
isinstancematrixapplyr6   rv   )r#   r8   rx   absxpart_tolrw   s   ` `   r   rv   zStandardBaseContext.chop   s    ;cgg+C	5AAq6D1v|xx##A&sDH-qvv;)66Mqvv;)771aff--   	5!SZZ(ww9::q*%234QC(444 &	5s0   5C AC 3C 4D3;D3D%$	D32D3c                    | j                  |      }|$|"| j                  d| j                   dz         x}}||}n||}t        ||z
        }||k  ryt        |      }t        |      }||k  r
||z  }||k  S ||z  }||k  S )a  
        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

        r      T)rB   ldexpprecrU   )	r#   strel_epsabs_epsdiffabssabsterrs	            r   almosteqzStandardBaseContext.almosteq   s    B KKN?w #		!chhYq[ 99Gg?G_G1Q3x7?1v1v$;t)C g~ t)Cg~r   c                 F   t        |      dk  st        dt        |      z        t        |      dk\  st        dt        |      z        d}d}t        |      dk(  r|d   }nt        |      dk\  r
|d   }|d   }t        |      dk(  r|d   }| j                  |      | j                        | j                  |      }}}||z   |k7  sJ d       ||kD  r|dkD  rg S t        }n|dk  rg S t        }g }d}|}	 |||z  z   }|dz  } |||      r|j                  |       n	 |S ,)aa  
        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')]

           z+arange expected at most 3 arguments, got %ir   z+arange expected at least 1 argument, got %ir   rS   z0dt is too small and would cause an infinite loop)lenr   mpfr   r   append)	r#   r\   rw   dtbopresultir   s	            r   arangezStandardBaseContext.arange   sR   @ 4yA~I!$i( ) )4yA~I!$i( ) ) t9>QAY!^QAQAt9>aB771:swwqz3772;b12v{NNN{q5Av	BAv	BBqDAFA!Qxa  r   c                    t        |      dk(  r7| j                  |d         }| j                  |d         }t        |d         }nct        |      dk(  r>t        |d   d      sJ |d   j                  }|d   j
                  }t        |d         }nt        dt        |      z        |dk  rt        d      d|vs|d   rV|dk(  r| j                  |      gS ||z
  | j                  |dz
        z  }t        |      D cg c]
  }||z  |z    }}||d	<   |S ||z
  | j                  |      z  }t        |      D cg c]
  }||z  |z    }}|S c c}w c c}w )
a  
        ``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')]

        r   r   r   rS   _mpi_z*linspace expected 2 or 3 arguments, got %izn must be greater than 0endpoint)	r   r   intr6   rw   r   r   r2   r   )	r#   r\   rC   rw   r   rq   stepr   rG   s	            r   linspacezStandardBaseContext.linspace   sj   * t9>Q AQ ADGAY!^47G,,,Q		AQ		ADGAH!$i( ) )q5788V#vj'9Av
|#ESWWQU^+D%+AY/4!/A/AbE  ESWWQZ'D%+AY/4!/A/ 0 0s    E<Ec                 N     | j                   |fi | | j                  |fi |fS r    )cossinr#   zrC   s      r   cos_sinzStandardBaseContext.cos_sinN  s-    swwq#F#WSWWQ%9&%999r   c                 N     | j                   |fi | | j                  |fi |fS r    )cospisinpir   s      r   cospi_sinpizStandardBaseContext.cospi_sinpiQ  s-    syy%f%ysyy'=f'===r   c                 0    t        d|dz  z  d|z  z         S )Ni  g      ?r   )r   )r#   ps     r   _default_hyper_maxprecz*StandardBaseContext._default_hyper_maxprecT  s    4!T'>AaC'((r   c                    | j                   }	 d}	 ||z   dz   | _         | j                  }| j                  }d} |       D ]U  }||z  }||z  sD|rB| j                  |      }	t	        ||	      }| j                  |      }
|
|	z
  | j                   kD  r n|dz  }W |
z
  }||k7  rn,||k  s| j
                  rn|t        | j                   |      z  }||| _         S # || _         w xY wN
   r      r   )r   ninfr<   magr}   _fixed_precisionmin)r#   terms
check_stepr   	extraprecmax_magr   ktermterm_magsum_magcancellations               r   sum_accuratelyz"StandardBaseContext.sum_accuratelya  s    xx	I)+a/((HH!G DIA
N#&774="%gx"8"%''!*"X-8!FA  '0</)+s/C/CS<88	' ( CHtCHs   CC 	C c                    | j                   }	 d}	 ||z   dz   | _         | j                  }| j                  }|}d} |       D ]Y  }	||	z  }|	|z
  }
||z  sC| j                  |
      }t	        ||      }| j                  ||z
        }| | j                   kD  r n|dz  }[ |z
  }||k7  rn,||k  s| j
                  rn|t        | j                   |      z  }||| _         S # || _         w xY wr   )r   r   rk   r   r}   r   r   )r#   factorsr   r   r   r   rk   r   r   factorr   r   r   r   s                 r   mul_accuratelyz"StandardBaseContext.mul_accurately}  s   xx	I)+a/((gg%i FKA!C<D
N#&774="%gx"8"%''!C%. %9sxx/!FA  '0</)+s/C/CS<88	/ 0 CHtCHs   CC 	C&c                 H    | j                  |      | j                  |      z  S )a  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
        rA   )r#   r8   rG   s      r   powerzStandardBaseContext.power  s      {{1~Q//r   c                 $    | j                  |      S r    )zeta)r#   rq   s     r   	_zeta_intzStandardBaseContext._zeta_int  s    xx{r   c                 $     dg fd}|S )a  
        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

        r   c                  f    dxx   dz  cc<   d   kD  rj                  dz         | i |S )Nr   r   z%maxcalls: function evaluated %i times)NoConvergence)r\   rC   Ncounterr#   fs     r   f_maxcalls_wrappedz8StandardBaseContext.maxcalls.<locals>.f_maxcalls_wrapped  sC    AJ!OJqzA~''(ORS(STTd%f%%r   r   )r#   r   r   r   r   s   ``` @r   maxcallszStandardBaseContext.maxcalls  s      #	&
 "!r   c                 b     i  fd}j                   |_         j                  |_        |S )a  
        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                      |r| t        |j                               f}n| }j                  }|	v r	|   \  }}||k\  r|S  | i |}||f	|<   |S r    )tupler%   r   )
r\   rC   keyr   cpreccvaluer*   r#   r   f_caches
          r   f_cachedz-StandardBaseContext.memoize.<locals>.f_cached  sl    E&,,.1188Dg~ 'vD="7Nt&v&E %=GCLLr   )r   __doc__)r#   r   r   r   s   `` @r   memoizezStandardBaseContext.memoize  s.    ( 	 JJ99r   )FF)NF)   r    )NN)r   )4r   r   r   r   r   ComplexResultr"   r+   r   verboser0   r3   r9   r=   r?   rD   rH   rK   rM   rO   r_   ri   rn   rr   rv   r   r   r   r   r   r   staticmethodgcd_gcdlist_primesisprimebernfracmoebiusifac_ifaceulernum	_eulernum	stirling1
_stirling1	stirling2
_stirling2r   r   r   r   r   r   r   r   r   r   r      sB    ''M''M$  G

----#8("H1fGR,\:>) 		"Du001K5==)GENN+H5==)G$EU^^,Ieoo.Jeoo.J8@0$"0$r   r   N)$operatorr   r   libmp.backendr   functions.functionsr   functions.rszetar   calculus.quadraturer	   calculus.inverselaplacer
   calculus.calculusr   calculus.optimizationr   calculus.odesr   matrices.matricesr   matrices.calculusr   matrices.linalgr   matrices.eigenr   identificationr   visualizationr    r   objectr   r   r   r   r   <module>r     sv     ! 1 % 2 E . 6 % , 4 1 ! 1 / 	f 	V'$	Vr   