ó
î&]\c           @` sã   d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l	 m
 Z d g Z d e f d	 „  ƒ  YZ d d d d d d d d d d d d
 „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s•   
Unified interfaces to root finding algorithms for real or complex
scalar functions.

Functions
---------
- root : find a root of a scalar function.
i    (   t   divisiont   print_functiont   absolute_importN(   t   callablei   (   t   zerost   root_scalart
   MemoizeDerc           B` s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sš  Decorator that caches the value and derivative(s) of function each
    time it is called.

    This is a simplistic memoizer that calls and caches a single value
    of `f(x, *args)`.
    It assumes that `args` does not change between invocations.
    It supports the use case of a root-finder where `args` is fixed,
    `x` changes, and only rarely, if at all, does x assume the same value
    more than once.c         C` s(   | |  _  d  |  _ d  |  _ d |  _ d  S(   Ni    (   t   funt   Nonet   valst   xt   n_calls(   t   selfR   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   __init__   s    			c         G` s`   |  j  d k s | |  j k rU |  j | | Œ } | |  _ |  j d 7_ | |  _  n  |  j  d S(   s,   Calculate f or use cached value if availablei   i    N(   R	   R   R
   R   R   (   R   R
   t   argst   fg(    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   __call__#   s    	c         G` s9   |  j  d k s | |  j k r. |  | | Œ n  |  j  d S(   s/   Calculate f' or use a cached value if availablei   N(   R	   R   R
   (   R   R
   R   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   fprime-   s    c         G` s9   |  j  d k s | |  j k r. |  | | Œ n  |  j  d S(   s0   Calculate f'' or use a cached value if availablei   N(   R	   R   R
   (   R   R
   R   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   fprime23   s    c         C` s   |  j  S(   N(   R   (   R   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   ncalls9   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyR      s   			
		c         C` sŒ  t  | t ƒ s | f } n  | d k r0 i  } n  t } | d k	 r‹ t | ƒ r‹ t | ƒ r‚ t |  ƒ }  t } |  j } |  j	 } q‹ d } n  | d k	 r× t | ƒ r× t | ƒ rÎ t |  ƒ }  t } |  j	 } q× d } n  i  } xB d d d g D]1 } t
 ƒ  j | ƒ } | d k	 rí | | | <qí qí W| r8| j | ƒ n  | j d t d t ƒ | s™| rcd } q™| d k	 r™| r| r„d } q“d } q–d	 } q™n  | s®t d
 ƒ ‚ n  | j ƒ  } i d d 6d d	 6} y t t | j | | ƒ ƒ } Wn! t k
 rt d | ƒ ‚ n X| d k rt  | t t t j f ƒ sKt d | ƒ ‚ n  | d  \ } } | |  | | d | | \ } } nî| d k r| d k rªt d | ƒ ‚ n  | d k rÉt d | ƒ ‚ n  d | k rë| j d ƒ | d <n  | |  | d | d d d d d | | \ } } nO| d k r±| d k rIt d | ƒ ‚ n  | sbt d | ƒ ‚ n  d | k r„| j d ƒ | d <n  | |  | d | d | d d | \ } } n¼ | d k r]| d k rÜt d | ƒ ‚ n  | sõt d | ƒ ‚ n  | st d | ƒ ‚ n  d | k r0| j d ƒ | d <n  | |  | d | d | d | | \ } } n t d | ƒ ‚ | rˆ|  j } | | _ n  | S(    sŽ  
    Find a root of a scalar function.

    Parameters
    ----------
    f : callable
        A function to find a root of.
    args : tuple, optional
        Extra arguments passed to the objective function and its derivative(s).
    method : str, optional
        Type of solver.  Should be one of

            - 'bisect'    :ref:`(see here) <optimize.root_scalar-bisect>`
            - 'brentq'    :ref:`(see here) <optimize.root_scalar-brentq>`
            - 'brenth'    :ref:`(see here) <optimize.root_scalar-brenth>`
            - 'ridder'    :ref:`(see here) <optimize.root_scalar-ridder>`
            - 'toms748'    :ref:`(see here) <optimize.root_scalar-toms748>`
            - 'newton'    :ref:`(see here) <optimize.root_scalar-newton>`
            - 'secant'    :ref:`(see here) <optimize.root_scalar-secant>`
            - 'halley'    :ref:`(see here) <optimize.root_scalar-halley>`

    bracket: A sequence of 2 floats, optional
        An interval bracketing a root.  `f(x, *args)` must have different
        signs at the two endpoints.
    x0 : float, optional
        Initial guess.
    x1 : float, optional
        A second guess.
    fprime : bool or callable, optional
        If `fprime` is a boolean and is True, `f` is assumed to return the
        value of derivative along with the objective function.
        `fprime` can also be a callable returning the derivative of `f`. In
        this case, it must accept the same arguments as `f`.
    fprime2 : bool or callable, optional
        If `fprime2` is a boolean and is True, `f` is assumed to return the
        value of 1st and 2nd derivatives along with the objective function.
        `fprime2` can also be a callable returning the 2nd derivative of `f`.
        In this case, it must accept the same arguments as `f`.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    options : dict, optional
        A dictionary of solver options. E.g. ``k``, see
        :obj:`show_options()` for details.

    Returns
    -------
    sol : RootResults
        The solution represented as a ``RootResults`` object.
        Important attributes are: ``root`` the solution , ``converged`` a
        boolean flag indicating if the algorithm exited successfully and
        ``flag`` which describes the cause of the termination. See
        `RootResults` for a description of other attributes.

    See also
    --------
    show_options : Additional options accepted by the solvers
    root : Find a root of a vector function.

    Notes
    -----
    This section describes the available solvers that can be selected by the
    'method' parameter.

    The default is to use the best method available for the situation
    presented.
    If a bracket is provided, it may use one of the bracketing methods.
    If a derivative and an initial value are specified, it may
    select one of the derivative-based methods.
    If no method is judged applicable, it will raise an Exception.


    Examples
    --------

    Find the root of a simple cubic

    >>> from scipy import optimize
    >>> def f(x):
    ...     return (x**3 - 1)  # only one real root at x = 1

    >>> def fprime(x):
    ...     return 3*x**2

    The `brentq` method takes as input a bracket

    >>> sol = optimize.root_scalar(f, bracket=[0, 3], method='brentq')
    >>> sol.root, sol.iterations, sol.function_calls
    (1.0, 10, 11)

    The `newton` method takes as input a single point and uses the derivative(s)

    >>> sol = optimize.root_scalar(f, x0=0.2, fprime=fprime, method='newton')
    >>> sol.root, sol.iterations, sol.function_calls
    (1.0, 11, 22)

    The function can provide the value and derivative(s) in a single call.

    >>> def f_p_pp(x):
    ...     return (x**3 - 1), 3*x**2, 6*x

    >>> sol = optimize.root_scalar(f_p_pp, x0=0.2, fprime=True, method='newton')
    >>> sol.root, sol.iterations, sol.function_calls
    (1.0, 11, 11)

    >>> sol = optimize.root_scalar(f_p_pp, x0=0.2, fprime=True, fprime2=True, method='halley')
    >>> sol.root, sol.iterations, sol.function_calls
    (1.0, 7, 8)


    t   xtolt   rtolt   maxitert   full_outputt   dispt   brentqt   halleyt   newtont   secantsI   Unable to select a solver as neither bracket nor starting point provided.s   Unknown solver %st   bisectt   riddert   brentht   toms748s   Bracket needed for %si   R   s   x0 must not be None for %ss   x1 must not be None for %st   tolR   R   t   x1s   fprime must be specified for %ss    fprime2 must be specified for %sN(   R    R!   R   R"   R#   (   R   (   R   (   R   (   t
   isinstancet   tupleR   t   FalseR   t   boolR   t   TrueR   R   t   localst   gett   updatet
   ValueErrort   lowert   getattrt   optzerost   AttributeErrort   listt   npt   ndarrayt   popR   t   function_calls(   t   fR   t   methodt   bracketR   R   t   x0R%   R   R   R   t   optionst   is_memoizedt   kwargst   kt   vt   metht   map2underlyingt   methodct   at   bt   rt   solR   (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyR   =   s¢    w							$-	c           C` s   d S(   s£  
    Options
    -------
    args : tuple, optional
        Extra arguments passed to the objective function.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    options: dict, optional
        Specifies any method-specific options not covered above

    N(    (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   _root_scalar_brentq_doc!  s    c           C` s   d S(   s£  
    Options
    -------
    args : tuple, optional
        Extra arguments passed to the objective function.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    options: dict, optional
        Specifies any method-specific options not covered above

    N(    (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   _root_scalar_brenth_doc4  s    c           C` s   d S(   s£  
    Options
    -------
    args : tuple, optional
        Extra arguments passed to the objective function.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    options: dict, optional
        Specifies any method-specific options not covered above

    N(    (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   _root_scalar_toms748_docF  s    c           C` s   d S(   s  
    Options
    -------
    args : tuple, optional
        Extra arguments passed to the objective function.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    x0 : float, required
        Initial guess.
    x1 : float, required
        A second guess.
    options: dict, optional
        Specifies any method-specific options not covered above

    N(    (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   _root_scalar_secant_docY  s    c           C` s   d S(   s!  
    Options
    -------
    args : tuple, optional
        Extra arguments passed to the objective function and its derivative.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    x0 : float, required
        Initial guess.
    fprime : bool or callable, optional
        If `fprime` is a boolean and is True, `f` is assumed to return the
        value of derivative along with the objective function.
        `fprime` can also be a callable returning the derivative of `f`. In
        this case, it must accept the same arguments as `f`.
    options: dict, optional
        Specifies any method-specific options not covered above

    N(    (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   _root_scalar_newton_docp  s    c           C` s   d S(   sq  
    Options
    -------
    args : tuple, optional
        Extra arguments passed to the objective function and its derivatives.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    x0 : float, required
        Initial guess.
    fprime : bool or callable, required
        If `fprime` is a boolean and is True, `f` is assumed to return the
        value of derivative along with the objective function.
        `fprime` can also be a callable returning the derivative of `f`. In
        this case, it must accept the same arguments as `f`.
    fprime2 : bool or callable, required
        If `fprime2` is a boolean and is True, `f` is assumed to return the
        value of 1st and 2nd derivatives along with the objective function.
        `fprime2` can also be a callable returning the 2nd derivative of `f`.
        In this case, it must accept the same arguments as `f`.
    options: dict, optional
        Specifies any method-specific options not covered above

    N(    (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   _root_scalar_halley_docŠ  s    c           C` s   d S(   s£  
    Options
    -------
    args : tuple, optional
        Extra arguments passed to the objective function.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    options: dict, optional
        Specifies any method-specific options not covered above

    N(    (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   _root_scalar_ridder_doc©  s    c           C` s   d S(   s£  
    Options
    -------
    args : tuple, optional
        Extra arguments passed to the objective function.
    xtol : float, optional
        Tolerance (absolute) for termination.
    rtol : float, optional
        Tolerance (relative) for termination.
    maxiter : int, optional
        Maximum number of iterations.
    options: dict, optional
        Specifies any method-specific options not covered above

    N(    (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   _root_scalar_bisect_doc¼  s    (    (   R   t
   __future__R    R   R   t   numpyR4   t   scipy._lib.sixR   t    R   R1   t   __all__t   objectR   R   R   RH   RI   RJ   RK   RL   RM   RN   RO   (    (    (    s:   lib/python2.7/site-packages/scipy/optimize/_root_scalar.pyt   <module>   s&   	*		à							