ó
Ê½÷Xc           @` s\  d  d l  m Z m Z m Z d  d l Z d d l m Z d  d l m Z m	 Z	 m
 Z
 d Z d Z d e e ƒ j Z d	 d
 d d d g Z d Z d Z d Z i e d  6e d 6e d 6Z d e f d „  ƒ  YZ d „  Z d d d d d d „ Z d e e e e e d „ Z d e e e e e d „ Z d  e e e e e d „ Z d! e e e e e d „ Z d S("   i    (   t   divisiont   print_functiont   absolute_importNi   (   t   _zeros(   t   finfot   signt   sqrtid   gê-™—=i   t   newtont   bisectt   riddert   brentqt   brentht	   convergeds
   sign errors   convergence erroriÿÿÿÿiþÿÿÿt   RootResultsc           B` s    e  Z d  Z d „  Z d „  Z RS(   sŠ   Represents the root finding result.
    Attributes
    ----------
    root : float
        Estimated root location.
    iterations : int
        Number of iterations needed to find the root.
    function_calls : int
        Number of times the function was called.
    converged : bool
        True if the routine converged.
    flag : str
        Description of the cause of termination.
    c         C` sc   | |  _  | |  _ | |  _ | d k |  _ y t | |  _ Wn! t k
 r^ d | f |  _ n Xd  S(   Ni    s   unknown error %d(   t   roott
   iterationst   function_callsR   t   flag_mapt   flagt   KeyError(   t   selfR   R   R   R   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyt   __init__#   s    			c         C` sn   d d d d d g } t  t t | ƒ ƒ d } d j g  | D], } | j | ƒ d t t |  | ƒ ƒ ^ q; ƒ S(	   NR   R   R   R   R   i   s   
s   : (   t   maxt   mapt   lent   joint   rjustt   reprt   getattr(   R   t   attrst   mt   a(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyt   __repr__-   s
    		(   t   __name__t
   __module__t   __doc__R   R    (    (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyR      s   	
c      	   C` sK   |  rC | \ } } } } t  d | d | d | d | ƒ } | | f S| Sd  S(   NR   R   R   R   (   R   (   t   full_outputt   rt   xt   funcallsR   R   t   results(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyt	   results_c5   s    	
g`sáÓbÈO>i2   c         C` s˜  | d k r t  d | ƒ ‚ n  | d k  r: t  d ƒ ‚ n  | d k	 rld | } d } xt | ƒ D]}	 | f | }
 | |
 Œ  } | d k r¨ d } t j | t ƒ | S|  |
 Œ  } | d k	 rÏ | |
 Œ  } n  | d k rì | | | } nY | d d | | } | d k  r| | | } n& | d | | t | ƒ t | ƒ } t | | ƒ | k  r_| S| } qc Wn| } | d k r| d d } n | d d } |  | f | Œ  } |  | f | Œ  } x² t | ƒ D]¤ }	 | | k r| | k rd	 | | } t j | t ƒ n  | | d
 S| | | | | | } t | | ƒ | k  rO| S| } | } | } |  | f | Œ  } qÐWd | | f } t	 | ƒ ‚ d S(   s{	  
    Find a zero using the Newton-Raphson or secant method.

    Find a zero of the function `func` given a nearby starting point `x0`.
    The Newton-Raphson method is used if the derivative `fprime` of `func`
    is provided, otherwise the secant method is used.  If the second order
    derivate `fprime2` of `func` is provided, parabolic Halley's method
    is used.

    Parameters
    ----------
    func : function
        The function whose zero is wanted. It must be a function of a
        single variable of the form f(x,a,b,c...), where a,b,c... are extra
        arguments that can be passed in the `args` parameter.
    x0 : float
        An initial estimate of the zero that should be somewhere near the
        actual zero.
    fprime : function, optional
        The derivative of the function when available and convenient. If it
        is None (default), then the secant method is used.
    args : tuple, optional
        Extra arguments to be used in the function call.
    tol : float, optional
        The allowable error of the zero value.
    maxiter : int, optional
        Maximum number of iterations.
    fprime2 : function, optional
        The second order derivative of the function when available and
        convenient. If it is None (default), then the normal Newton-Raphson
        or the secant method is used. If it is given, parabolic Halley's
        method is used.

    Returns
    -------
    zero : float
        Estimated location where function is zero.

    See Also
    --------
    brentq, brenth, ridder, bisect
    fsolve : find zeroes in n dimensions.

    Notes
    -----
    The convergence rate of the Newton-Raphson method is quadratic,
    the Halley method is cubic, and the secant method is
    sub-quadratic.  This means that if the function is well behaved
    the actual error in the estimated zero is approximately the square
    (cube for Halley) of the requested tolerance up to roundoff
    error. However, the stopping criterion used here is the step size
    and there is no guarantee that a zero has been found. Consequently
    the result should be verified. Safer algorithms are brentq,
    brenth, ridder, and bisect, but they all require that the root
    first be bracketed in an interval where the function changes
    sign. The brentq algorithm is recommended for general use in one
    dimensional problems when such an interval has been found.

    i    s   tol too small (%g <= 0)i   s   maxiter must be greater than 0g      ð?s   derivative was zero.i   g-Cëâ6?s   Tolerance of %s reachedg       @s3   Failed to converge after %d iterations, value is %sNgq¬‹Ûh ð?gq¬‹Ûh ð?(
   t
   ValueErrort   Nonet   ranget   warningst   warnt   RuntimeWarningR   R   t   abst   RuntimeError(   t   funct   x0t   fprimet   argst   tolt   maxitert   fprime2t   p0t   fder2t   itert   myargst   fdert   msgt   fvalt   pt   discrt   p1t   q0t   q1(    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyR   B   s^    =
&c	   
   
   C` s“   t  | t ƒ s | f } n  | d k r: t d | ƒ ‚ n  | t k  r_ t d | t f ƒ ‚ n  t j |  | | | | | | | | ƒ	 }	 t | |	 ƒ S(   sÉ  
    Find root of a function within an interval.

    Basic bisection routine to find a zero of the function `f` between the
    arguments `a` and `b`. `f(a)` and `f(b)` cannot have the same signs.
    Slow but sure.

    Parameters
    ----------
    f : function
        Python function returning a number.  `f` must be continuous, and
        f(a) and f(b) must have opposite signs.
    a : number
        One end of the bracketing interval [a,b].
    b : number
        The other end of the bracketing interval [a,b].
    xtol : number, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
        parameter must be nonnegative.
    rtol : number, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
        parameter cannot be smaller than its default value of
        ``4*np.finfo(float).eps``.
    maxiter : number, optional
        if convergence is not achieved in `maxiter` iterations, an error is
        raised.  Must be >= 0.
    args : tuple, optional
        containing extra arguments for the function `f`.
        `f` is called by ``apply(f, (x)+args)``.
    full_output : bool, optional
        If `full_output` is False, the root is returned.  If `full_output` is
        True, the return value is ``(x, r)``, where x is the root, and r is
        a `RootResults` object.
    disp : bool, optional
        If True, raise RuntimeError if the algorithm didn't converge.

    Returns
    -------
    x0 : float
        Zero of `f` between `a` and `b`.
    r : RootResults (present if ``full_output = True``)
        Object containing information about the convergence.  In particular,
        ``r.converged`` is True if the routine converged.

    See Also
    --------
    brentq, brenth, bisect, newton
    fixed_point : scalar fixed-point finder
    fsolve : n-dimensional root-finding

    i    s   xtol too small (%g <= 0)s   rtol too small (%g < %g)(   t
   isinstancet   tupleR*   t   _rtolR   t   _bisectR)   (
   t   fR   t   bR5   t   xtolt   rtolR7   R$   t   dispR%   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyR   »   s    8'c	   
   
   C` s“   t  | t ƒ s | f } n  | d k r: t d | ƒ ‚ n  | t k  r_ t d | t f ƒ ‚ n  t j |  | | | | | | | | ƒ	 }	 t | |	 ƒ S(   sç	  
    Find a root of a function in an interval.

    Parameters
    ----------
    f : function
        Python function returning a number.  f must be continuous, and f(a) and
        f(b) must have opposite signs.
    a : number
        One end of the bracketing interval [a,b].
    b : number
        The other end of the bracketing interval [a,b].
    xtol : number, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
        parameter must be nonnegative.
    rtol : number, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
        parameter cannot be smaller than its default value of
        ``4*np.finfo(float).eps``.
    maxiter : number, optional
        if convergence is not achieved in maxiter iterations, an error is
        raised.  Must be >= 0.
    args : tuple, optional
        containing extra arguments for the function `f`.
        `f` is called by ``apply(f, (x)+args)``.
    full_output : bool, optional
        If `full_output` is False, the root is returned.  If `full_output` is
        True, the return value is ``(x, r)``, where `x` is the root, and `r` is
        a RootResults object.
    disp : bool, optional
        If True, raise RuntimeError if the algorithm didn't converge.

    Returns
    -------
    x0 : float
        Zero of `f` between `a` and `b`.
    r : RootResults (present if ``full_output = True``)
        Object containing information about the convergence.
        In particular, ``r.converged`` is True if the routine converged.

    See Also
    --------
    brentq, brenth, bisect, newton : one-dimensional root-finding
    fixed_point : scalar fixed-point finder

    Notes
    -----
    Uses [Ridders1979]_ method to find a zero of the function `f` between the
    arguments `a` and `b`. Ridders' method is faster than bisection, but not
    generally as fast as the Brent rountines. [Ridders1979]_ provides the
    classic description and source of the algorithm. A description can also be
    found in any recent edition of Numerical Recipes.

    The routine used here diverges slightly from standard presentations in
    order to be a bit more careful of tolerance.

    References
    ----------
    .. [Ridders1979]
       Ridders, C. F. J. "A New Algorithm for Computing a
       Single Root of a Real Continuous Function."
       IEEE Trans. Circuits Systems 26, 979-980, 1979.

    i    s   xtol too small (%g <= 0)s   rtol too small (%g < %g)(   RE   RF   R*   RG   R   t   _ridderR)   (
   RI   R   RJ   R5   RK   RL   R7   R$   RM   R%   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyR	   ý   s    E'c	   
   
   C` s“   t  | t ƒ s | f } n  | d k r: t d | ƒ ‚ n  | t k  r_ t d | t f ƒ ‚ n  t j |  | | | | | | | | ƒ	 }	 t | |	 ƒ S(   s  
    Find a root of a function in a bracketing interval using Brent's method.

    Uses the classic Brent's method to find a zero of the function `f` on
    the sign changing interval [a , b].  Generally considered the best of the
    rootfinding routines here.  It is a safe version of the secant method that
    uses inverse quadratic extrapolation.  Brent's method combines root
    bracketing, interval bisection, and inverse quadratic interpolation.  It is
    sometimes known as the van Wijngaarden-Dekker-Brent method.  Brent (1973)
    claims convergence is guaranteed for functions computable within [a,b].

    [Brent1973]_ provides the classic description of the algorithm.  Another
    description can be found in a recent edition of Numerical Recipes, including
    [PressEtal1992]_.  Another description is at
    http://mathworld.wolfram.com/BrentsMethod.html.  It should be easy to
    understand the algorithm just by reading our code.  Our code diverges a bit
    from standard presentations: we choose a different formula for the
    extrapolation step.

    Parameters
    ----------
    f : function
        Python function returning a number.  The function :math:`f`
        must be continuous, and :math:`f(a)` and :math:`f(b)` must
        have opposite signs.
    a : number
        One end of the bracketing interval :math:`[a, b]`.
    b : number
        The other end of the bracketing interval :math:`[a, b]`.
    xtol : number, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
        parameter must be nonnegative. For nice functions, Brent's
        method will often satisfy the above condition will ``xtol/2``
        and ``rtol/2``. [Brent1973]_
    rtol : number, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
        parameter cannot be smaller than its default value of
        ``4*np.finfo(float).eps``. For nice functions, Brent's
        method will often satisfy the above condition will ``xtol/2``
        and ``rtol/2``. [Brent1973]_
    maxiter : number, optional
        if convergence is not achieved in maxiter iterations, an error is
        raised.  Must be >= 0.
    args : tuple, optional
        containing extra arguments for the function `f`.
        `f` is called by ``apply(f, (x)+args)``.
    full_output : bool, optional
        If `full_output` is False, the root is returned.  If `full_output` is
        True, the return value is ``(x, r)``, where `x` is the root, and `r` is
        a RootResults object.
    disp : bool, optional
        If True, raise RuntimeError if the algorithm didn't converge.

    Returns
    -------
    x0 : float
        Zero of `f` between `a` and `b`.
    r : RootResults (present if ``full_output = True``)
        Object containing information about the convergence.  In particular,
        ``r.converged`` is True if the routine converged.

    See Also
    --------
    multivariate local optimizers
      `fmin`, `fmin_powell`, `fmin_cg`, `fmin_bfgs`, `fmin_ncg`
    nonlinear least squares minimizer
      `leastsq`
    constrained multivariate optimizers
      `fmin_l_bfgs_b`, `fmin_tnc`, `fmin_cobyla`
    global optimizers
      `basinhopping`, `brute`, `differential_evolution`
    local scalar minimizers
      `fminbound`, `brent`, `golden`, `bracket`
    n-dimensional root-finding
      `fsolve`
    one-dimensional root-finding
      `brenth`, `ridder`, `bisect`, `newton`
    scalar fixed-point finder
      `fixed_point`

    Notes
    -----
    `f` must be continuous.  f(a) and f(b) must have opposite signs.


    References
    ----------
    .. [Brent1973]
       Brent, R. P.,
       *Algorithms for Minimization Without Derivatives*.
       Englewood Cliffs, NJ: Prentice-Hall, 1973. Ch. 3-4.

    .. [PressEtal1992]
       Press, W. H.; Flannery, B. P.; Teukolsky, S. A.; and Vetterling, W. T.
       *Numerical Recipes in FORTRAN: The Art of Scientific Computing*, 2nd ed.
       Cambridge, England: Cambridge University Press, pp. 352-355, 1992.
       Section 9.3:  "Van Wijngaarden-Dekker-Brent Method."

    i    s   xtol too small (%g <= 0)s   rtol too small (%g < %g)(   RE   RF   R*   RG   R   t   _brentqR)   (
   RI   R   RJ   R5   RK   RL   R7   R$   RM   R%   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyR
   L  s    h'c	   
   
   C` s“   t  | t ƒ s | f } n  | d k r: t d | ƒ ‚ n  | t k  r_ t d | t f ƒ ‚ n  t j |  | | | | | | | | ƒ	 }	 t | |	 ƒ S(   s†  Find root of f in [a,b].

    A variation on the classic Brent routine to find a zero of the function f
    between the arguments a and b that uses hyperbolic extrapolation instead of
    inverse quadratic extrapolation. There was a paper back in the 1980's ...
    f(a) and f(b) cannot have the same signs. Generally on a par with the
    brent routine, but not as heavily tested.  It is a safe version of the
    secant method that uses hyperbolic extrapolation. The version here is by
    Chuck Harris.

    Parameters
    ----------
    f : function
        Python function returning a number.  f must be continuous, and f(a) and
        f(b) must have opposite signs.
    a : number
        One end of the bracketing interval [a,b].
    b : number
        The other end of the bracketing interval [a,b].
    xtol : number, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
        parameter must be nonnegative. As with `brentq`, for nice
        functions the method will often satisfy the above condition
        will ``xtol/2`` and ``rtol/2``.
    rtol : number, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
        parameter cannot be smaller than its default value of
        ``4*np.finfo(float).eps``. As with `brentq`, for nice functions
        the method will often satisfy the above condition will
        ``xtol/2`` and ``rtol/2``.
    maxiter : number, optional
        if convergence is not achieved in maxiter iterations, an error is
        raised.  Must be >= 0.
    args : tuple, optional
        containing extra arguments for the function `f`.
        `f` is called by ``apply(f, (x)+args)``.
    full_output : bool, optional
        If `full_output` is False, the root is returned.  If `full_output` is
        True, the return value is ``(x, r)``, where `x` is the root, and `r` is
        a RootResults object.
    disp : bool, optional
        If True, raise RuntimeError if the algorithm didn't converge.

    Returns
    -------
    x0 : float
        Zero of `f` between `a` and `b`.
    r : RootResults (present if ``full_output = True``)
        Object containing information about the convergence.  In particular,
        ``r.converged`` is True if the routine converged.

    See Also
    --------
    fmin, fmin_powell, fmin_cg,
           fmin_bfgs, fmin_ncg : multivariate local optimizers

    leastsq : nonlinear least squares minimizer

    fmin_l_bfgs_b, fmin_tnc, fmin_cobyla : constrained multivariate optimizers

    basinhopping, differential_evolution, brute : global optimizers

    fminbound, brent, golden, bracket : local scalar minimizers

    fsolve : n-dimensional root-finding

    brentq, brenth, ridder, bisect, newton : one-dimensional root-finding

    fixed_point : scalar fixed-point finder

    i    s   xtol too small (%g <= 0)s   rtol too small (%g < %g)(   RE   RF   R*   RG   R   t   _brenthR)   (
   RI   R   RJ   R5   RK   RL   R7   R$   RM   R%   (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyR   ¾  s    L'(    (    (    (    (    (    t
   __future__R    R   R   R-   t    R   t   numpyR   R   R   t   _itert   _xtolt   floatt   epsRG   t   __all__t	   CONVERGEDt   SIGNERRt   CONVERRR   t   objectR   R)   R+   R   t   Falset   TrueR   R	   R
   R   (    (    (    s3   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/zeros.pyt   <module>   s6   !	x	@	M	p	