a
    a                  
   @   s  d dl Z d dlmZ d dlZddlmZ d dlZdZdZ	de
ej Zg dZd Zd	Zd
ZdZdZdZdZdZdZdZeeeeeeeeeeiZG dd dZdd Zdd Zd;ddZdd  Zde	eeddfd!d"Zde	eeddfd#d$Z de	eeddfd%d&Z!de	eeddfd'd(Z"ee	fd)d*Z#d+d, Z$d-d. Z%d<d/d0Z&d1d2 Z'd3d4 Z(d5d6 Z)G d7d8 d8Z*dde	eeddfd9d:Z+dS )=    N)
namedtuple   )_zerosd   g-=   )newtonbisectridderbrentqbrenthtoms748RootResults	convergedz
sign errorzconvergence errorzvalue errorzNo errorc                   @   s    e Zd ZdZdd Zdd ZdS )r   a  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   sT   || _ || _|| _|tk| _d | _zt| | _W n tyN   d|f | _Y n0 d S )Nzunknown error %d)root
iterationsfunction_calls_ECONVERGEDr   flagflag_mapKeyError)selfr   r   r   r    r   d/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/optimize/zeros.py__init__3   s    
zRootResults.__init__c                    s4   g d}t tt|d  d fdd|D S )N)r   r   r   r   r   r   
c                    s(   g | ] }|  d  tt| qS )z: )rjustreprgetattr).0amr   r   r   
<listcomp>B   s   z(RootResults.__repr__.<locals>.<listcomp>)maxmaplenjoin)r   attrsr   r#   r   __repr__>   s
    zRootResults.__repr__N)__name__
__module____qualname____doc__r   r+   r   r   r   r   r   !   s   r   c                 C   s0   | r(|\}}}}t ||||d}||fS |S d S )Nr   r   r   r   r   full_outputrxfuncallsr   r   resultsr   r   r   	results_cF   s    r8   c                 C   s,   |\}}}}| r(t ||||d}||fS |S )z:Select from a tuple of (root, funccalls, iterations, flag)r0   r1   r2   r   r   r   _results_selectR   s    r9   r   `sbO>2           FTc              	   C   s  |dkrt d| t|}|dk r.t dt|dkrRt| |||||||	S d| }d}|durt|D ]2}| |g|R  }|d7 }|dkrt|	|||tf  S ||g|R  }|d7 }|dkrd}|
r|d|d |f 7 }t	|t
|t t|	|||d tf  S || }|rj||g|R  }|d7 }|| | d	 }t|dk rj|d|  }|| }tj||||d
rt|	|||d tf  S |}qpn|dur||krt d|}n(d}|d|  }||dkr|n| 7 }| |g|R  }|d7 }| |g|R  }|d7 }t|t|k rJ||||f\}}}}t|D ]}||kr||krd||  }|
r|d|d |f 7 }t	|t
|t || d }t|	|||d tf  S t|t|kr| | | | d||   }n| | | | d||   }tj||||d
rJt|	|||d tf  S || }}|}| |g|R  }|d7 }qR|
rd|d |f }t	|t|	|||d tfS )a}  
    Find a zero of a real or complex function using the Newton-Raphson
    (or secant or Halley's) 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
    derivative `fprime2` of `func` is also provided, then Halley's method is
    used.

    If `x0` is a sequence with more than one item, then `newton` returns an
    array, and `func` must be vectorized and return a sequence or array of the
    same shape as its first argument. If `fprime` or `fprime2` is given, then
    its return must also have the same shape.

    Parameters
    ----------
    func : callable
        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, sequence, or ndarray
        An initial estimate of the zero that should be somewhere near the
        actual zero. If not scalar, then `func` must be vectorized and return
        a sequence or array of the same shape as its first argument.
    fprime : callable, 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. If `func` is complex-valued,
        a larger `tol` is recommended as both the real and imaginary parts
        of `x` contribute to ``|x - x0|``.
    maxiter : int, optional
        Maximum number of iterations.
    fprime2 : callable, 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 not None, then Halley's method
        is used.
    x1 : float, optional
        Another estimate of the zero that should be somewhere near the
        actual zero. Used if `fprime` is not provided.
    rtol : float, optional
        Tolerance (relative) for termination.
    full_output : bool, optional
        If `full_output` is False (default), the root is returned.
        If True and `x0` is scalar, the return value is ``(x, r)``, where ``x``
        is the root and ``r`` is a `RootResults` object.
        If True and `x0` is non-scalar, the return value is ``(x, converged,
        zero_der)`` (see Returns section for details).
    disp : bool, optional
        If True, raise a RuntimeError if the algorithm didn't converge, with
        the error message containing the number of iterations and current
        function value. Otherwise, the convergence status is recorded in a
        `RootResults` return object.
        Ignored if `x0` is not scalar.
        *Note: this has little to do with displaying, however,
        the `disp` keyword cannot be renamed for backwards compatibility.*

    Returns
    -------
    root : float, sequence, or ndarray
        Estimated location where function is zero.
    r : `RootResults`, optional
        Present if ``full_output=True`` and `x0` is scalar.
        Object containing information about the convergence. In particular,
        ``r.converged`` is True if the routine converged.
    converged : ndarray of bool, optional
        Present if ``full_output=True`` and `x0` is non-scalar.
        For vector functions, indicates which elements converged successfully.
    zero_der : ndarray of bool, optional
        Present if ``full_output=True`` and `x0` is non-scalar.
        For vector functions, indicates which elements had a zero derivative.

    See Also
    --------
    brentq, brenth, ridder, bisect
    fsolve : find zeros 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 after the nth iteration
    is approximately the square (cube for Halley) of the error
    after the (n-1)th step. 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.

    When `newton` is used with arrays, it is best suited for the following
    types of problems:

    * The initial guesses, `x0`, are all relatively the same distance from
      the roots.
    * Some or all of the extra arguments, `args`, are also arrays so that a
      class of similar problems can be solved together.
    * The size of the initial guesses, `x0`, is larger than O(100) elements.
      Otherwise, a naive loop may perform as well or better than a vector.

    Examples
    --------
    >>> from scipy import optimize
    >>> import matplotlib.pyplot as plt

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

    ``fprime`` is not provided, use the secant method:

    >>> root = optimize.newton(f, 1.5)
    >>> root
    1.0000000000000016
    >>> root = optimize.newton(f, 1.5, fprime2=lambda x: 6 * x)
    >>> root
    1.0000000000000016

    Only ``fprime`` is provided, use the Newton-Raphson method:

    >>> root = optimize.newton(f, 1.5, fprime=lambda x: 3 * x**2)
    >>> root
    1.0

    Both ``fprime2`` and ``fprime`` are provided, use Halley's method:

    >>> root = optimize.newton(f, 1.5, fprime=lambda x: 3 * x**2,
    ...                        fprime2=lambda x: 6 * x)
    >>> root
    1.0

    When we want to find zeros for a set of related starting values and/or
    function parameters, we can provide both of those as an array of inputs:

    >>> f = lambda x, a: x**3 - a
    >>> fder = lambda x, a: 3 * x**2
    >>> rng = np.random.default_rng()
    >>> x = rng.standard_normal(100)
    >>> a = np.arange(-50, 50)
    >>> vec_res = optimize.newton(f, x, fprime=fder, args=(a, ), maxiter=200)

    The above is the equivalent of solving for each value in ``(x, a)``
    separately in a for-loop, just faster:

    >>> loop_res = [optimize.newton(f, x0, fprime=fder, args=(a0,))
    ...             for x0, a0 in zip(x, a)]
    >>> np.allclose(vec_res, loop_res)
    True

    Plot the results found for all values of ``a``:

    >>> analytical_result = np.sign(a) * np.abs(a)**(1/3)
    >>> fig = plt.figure()
    >>> ax = fig.add_subplot(111)
    >>> ax.plot(a, analytical_result, 'o')
    >>> ax.plot(a, vec_res, '.')
    >>> ax.set_xlabel('$a$')
    >>> ax.set_ylabel('$x$ where $f(x, a)=0$')
    >>> plt.show()

    r   ztol too small (%g <= 0)r   maxiter must be greater than 0      ?NzDerivative was zero.z5 Failed to converge after %d iterations, value is %s.   rtolatolzx1 and x0 must be differentg-C6?zTolerance of %s reached.       @z4Failed to converge after %d iterations, value is %s.)
ValueErroroperatorindexnpsize_array_newtonranger9   r   RuntimeErrorwarningswarnRuntimeWarning	_ECONVERRabsisclose)funcx0fprimeargstolmaxiterfprime2x1rA   r3   dispZp0r6   itrfvalfdermsgZnewton_stepfder2adjpp1epsq0q1r   r   r   r   ^   s     +









 

r   c              	   C   s  t j|dd}t j|td}	t |	}
|dur8t|D ]}t | |g|R  }| sj|t}	 qt ||g|R  }|dk}
|
 s q||
 ||
  }|durt ||g|R  }|dd| ||
  ||
    }t j|t ||t j	d}||
  |8  < t 
||k|	|
< |	|
  s8 qq8nVt tjd }|d	|  t |dk||  }t | |g|R  }t | |g|R  }t j|td}t|D ]}||k}
|
 s|| d
 } q|||  |
 || |
  }t j|t |||t j	d}||
 | ||
< |
 |@ }|| | d
 ||< ||
M }t 
||k|	|
< |	|
  sf q|| }}|}t | |g|R  }q|
 |	@ }| r"|du r||k}||@ }| r t t|| ||  d }td|t n(| rdnd}d|}t|t nF|	 rh|	 r:dnd}d||}|	 r\t|t|t |rtdd}|||	 |}|S )z
    A vectorized version of Newton, Halley, and secant methods for arrays.

    Do not use this method directly. This method is called from `newton`
    when ``np.size(x0) > 1`` is ``True``. For docstring, see `newton`.
    T)copy)ZdtypeNr   r>         ?gQ?r   rC   r?   zRMS of {:g} reachedallZsomez{:s} derivatives were zeroz/{0:s} failed to converge after {1:d} iterationsresult)r   r   zero_der)rG   arrayZ	ones_likeboolrJ   asarrayanyZastypeZresult_typeZfloat64rP   finfofloatrc   wheresqrtsumrL   rM   formatrN   rh   rK   r   )rR   rS   rT   rU   rV   rW   rX   r3   ra   ZfailuresZnz_der	iterationr\   r]   Zdpr_   Zdxrb   rd   re   activeZactive_zero_derrj   Z
nonzero_dpZzero_der_nz_dpZrmsZall_or_somer^   ri   r   r   r   rI   n  s    	


  











rI   c	           
      C   sj   t |ts|f}t|}|dkr.td| |tk rFtd|tf t| ||||||||	}	t||	S )a#	  
    Find root of a function within an interval using bisection.

    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 : scalar
        One end of the bracketing interval [a,b].
    b : scalar
        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 : int, 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.
        Otherwise, the convergence status is recorded in a `RootResults`
        return object.

    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.

    Examples
    --------

    >>> def f(x):
    ...     return (x**2 - 1)

    >>> from scipy import optimize

    >>> root = optimize.bisect(f, 0, 2)
    >>> root
    1.0

    >>> root = optimize.bisect(f, -2, 0)
    >>> root
    -1.0

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

    r   xtol too small (%g <= 0)rtol too small (%g < %g))	
isinstancetuplerE   rF   rD   _rtolr   _bisectr8   
fr"   brU   xtolrA   rW   r3   rZ   r4   r   r   r   r     s    J

r   c	           
      C   sj   t |ts|f}t|}|dkr.td| |tk rFtd|tf t| ||||||||	}	t||	S )a?  
    Find a root of a function in an interval using Ridder's method.

    Parameters
    ----------
    f : function
        Python function returning a number. f must be continuous, and f(a) and
        f(b) must have opposite signs.
    a : scalar
        One end of the bracketing interval [a,b].
    b : scalar
        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 : int, 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.
        Otherwise, the convergence status is recorded in any `RootResults`
        return object.

    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 : 1-D 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 routines. [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.

    Examples
    --------

    >>> def f(x):
    ...     return (x**2 - 1)

    >>> from scipy import optimize

    >>> root = optimize.ridder(f, 0, 2)
    >>> root
    1.0

    >>> root = optimize.ridder(f, -2, 0)
    >>> root
    -1.0
    r   rw   rx   )	ry   rz   rE   rF   rD   r{   r   Z_ridderr8   r}   r   r   r   r	   )  s    V

r	   c	           
      C   sj   t |ts|f}t|}|dkr.td| |tk rFtd|tf t| ||||||||	}	t||	S )a  
    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]_. A third 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 : scalar
        One end of the bracketing interval :math:`[a, b]`.
    b : scalar
        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 with ``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 with ``xtol/2``
        and ``rtol/2``. [Brent1973]_
    maxiter : int, 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.
        Otherwise, the convergence status is recorded in any `RootResults`
        return object.

    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.

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

    Related functions fall into several classes:

    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-D root-finding
      `fsolve`
    1-D root-finding
      `brenth`, `ridder`, `bisect`, `newton`
    scalar fixed-point finder
      `fixed_point`

    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."

    Examples
    --------
    >>> def f(x):
    ...     return (x**2 - 1)

    >>> from scipy import optimize

    >>> root = optimize.brentq(f, -2, 0)
    >>> root
    -1.0

    >>> root = optimize.brentq(f, 0, 2)
    >>> root
    1.0
    r   rw   rx   )	ry   rz   rE   rF   rD   r{   r   Z_brentqr8   r}   r   r   r   r
     s    w

r
   c	           
      C   sj   t |ts|f}t|}|dkr.td| |tk rFtd|tf t| ||||||||	}	t||	S )a  Find a root of a function in a bracketing interval using Brent's
    method with hyperbolic extrapolation.

    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 : scalar
        One end of the bracketing interval [a,b].
    b : scalar
        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
        with ``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 with
        ``xtol/2`` and ``rtol/2``.
    maxiter : int, 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.
        Otherwise, the convergence status is recorded in any `RootResults`
        return object.

    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.

    Examples
    --------
    >>> def f(x):
    ...     return (x**2 - 1)

    >>> from scipy import optimize

    >>> root = optimize.brenth(f, -2, 0)
    >>> root
    -1.0

    >>> root = optimize.brenth(f, 0, 2)
    >>> root
    1.0

    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-D root-finding

    brentq, brenth, ridder, bisect, newton : 1-D root-finding

    fixed_point : scalar fixed-point finder

    r   rw   rx   )	ry   rz   rE   rF   rD   r{   r   Z_brenthr8   r}   r   r   r   r     s    ^

r   c                    sB   t o<t to<t fddtd d D  }|S )Nc                 3   s4   | ],\}}t tj||d  d  dV  qdS )r   Nr@   )rn   rG   rQ   )r!   iZ_frB   fsrA   r   r   	<genexpr>  s   z_notclose.<locals>.<genexpr>r   )rh   rG   isfinitern   	enumerate)r   rA   rB   Znotclosefvalsr   r   r   	_notclose{  s    r   c                 C   s   | dd \}}|dd \}}||kr.t jS t |t |krb| | | | d||   }n| | | | d||   }|S )z+Perform a secant step, taking a little careNr?   r   )rG   nanrP   )xvalsfvalsrS   rY   Zf0f1Zx2r   r   r   _secant  s     r   c           	      C   sR   |\}}t |t | dkr$dnd}| | ||  }}|||< || |< ||fS )z?Update a bracket given (c, fc), return the discarded endpoints.r   r   )rG   sign)	abfabcfcfafbidxrxZrfxr   r   r   _update_bracket  s     r   c                 C   sd  |r|rt | } nt | ddd } t| }|du r<|nt||}t ||g}|dd |dddf< td|D ]J}t ||d d|d f | |d | d||    ||d|f< qv|S t | } t |}t |}	|rdnd}
||
 |d< tdt| D ]T}| ||t|	 d  | dt|	d   }t |	dd | }	|	|
 ||< q
|S )a  Return a matrix of divided differences for the xvals, fvals pairs

    DD[i, j] = f[x_{i-j}, ..., x_i] for 0 <= j <= i

    If full is False, just return the main diagonal(or last row):
      f[a], f[a, b] and f[a, b, c].
    If forward is False, return f[c], f[b, c], f[a, b, c].Nr   r   r   )rG   rm   rk   r(   minzerosrJ   diff)r   r   NfullforwardMZDDr   ddrowZidx2UseZdenomr   r   r   _compute_divided_differences  s.    	


,r   c           	      C   s$  t | } t| }t ||g}t ||g}|dd |dddf< |dd |dddf< td|D ]}||d|d f ||d |d |d f  }| d||  | ||  }| |d | | | ||d|f< | d||  | | | ||d|f< qht |dddf |d  S )zCompute p(x) for the polynomial passing through the specified locations.

    Use Neville's algorithm to compute p(x) where p is the minimal degree
    polynomial passing through the points xvals, fvalsNr   r   r   )r   r   )rG   rm   r(   r   rJ   rs   )	r   r   r5   r   QDkalphaZdiffikr   r   r   _interpolated_poly  s    
0$*r   c                 C   s   t ||||g| |||gdS )zInverse cubic interpolation f-values -> x-values

    Given four points (fa, a), (fb, b), (fc, c), (fd, d) with
    fa, fb, fc, fd all distinct, find poly IP(y) through the 4 points
    and compute x=IP(0).
    r   )r   )r"   r   r   dr   r   r   fdr   r   r   _inverse_poly_zero  s    r   c                    s  | \|\}t |g||gddd\}  fdd} dkr\  }n t t dkrxn}t|D ]}	||| d|       }
| d |
  k r| d k sn | d |  k r| d k rn n|  S t| d	 } q|
}q|S )
zApply Newton-Raphson like steps, using divided differences to approximate f'

    ab is a real interval [a, b] containing a root,
    fab holds the real values of f(a), f(b)
    d is a real number outside [ab, b]
    k is the number of steps to apply
    TFr   r   c                    s    |    |    S Nr   )r5   ABr"   r   r   r   r   _P  s    z_newton_quadratic.<locals>._Pr   r?   r   rC   )r   rG   r   rJ   rs   )r   r   r   r   r   r   _r   r4   r   Zr1r   r   r   _newton_quadratic  s$     $  r   c                   @   sz   e Zd ZdZdZdZdZdd Zdd Zdd
dZ	e
fddZdd ZdddZdd Zdd Zdeeded	fddZdS )TOMS748SolverzGSolve f(x, *args) == 0 using Algorithm748 of Alefeld, Potro & Shi.
    rg   r   r   c                 C   sn   d | _ d | _d| _d| _d| _tjtjg| _tjtjg| _d | _	d | _
d | _d | _d| _t| _t| _t| _d S )Nr   r?   F)r~   rU   r   r   r   rG   r   r   r   r   r   eferZ   _xtolr   r{   rA   _iterrW   )r   r   r   r   r     s    zTOMS748Solver.__init__c                 C   sT   || _ || _|| _|| _t|| j| _| j| jkrPd| j }t	|t
 | j| _d S )Nztoms748: Overriding k: ->%d)rZ   r   rA   rW   r&   _K_MINr   _K_MAXrL   rM   rN   )r   r   rA   rW   rZ   r   r^   r   r   r   	configure  s    
zTOMS748Solver.configureTc                 C   sD   | j |g| jR  }|  jd7  _t|s@|r@td||f |S )z4Call the user-supplied function, update book-keepingr   $Invalid function value: f(%f) -> %s )r~   rU   r   rG   r   rD   )r   r5   errorZfxr   r   r   _callf,  s
    zTOMS748Solver._callfc                 C   s   || j | j|fS )z/Package the result and statistics into a tuple.)r   r   )r   r5   r   r   r   r   
get_result4  s    zTOMS748Solver.get_resultc                 C   s   t | j| j||S r   )r   r   r   )r   r   r   r   r   r   r   8  s    zTOMS748Solver._update_bracketr   c                 C   sH  d| _ d| _|| _|| _||g| jdd< t|rBt|dkrNtd| t|rft|dkrrtd| | 	|}t|rt|dkrtd||f |dkrt
|fS | 	|}t|rt|dkrtd||f |dkrt
|fS t|t| dkr$td||||f ||g| jdd< tt| jd fS )zPrepare for the iterations.r   NzInvalid x value: %s r   z,a, b must bracket a root f(%e)=%e, f(%e)=%e rC   )r   r   r~   rU   r   rG   r   imagrD   r   r   r   r   _EINPROGRESSrs   )r   r~   r"   r   rU   r   r   r   r   r   start;  s2    


zTOMS748Solver.startc                 C   sj   | j dd \}}tj||| j| jdr:tt| j d fS | j| jkrXt	t| j d fS t
t| j d fS )zDetermine the current status.Nr?   r@   rC   )r   rG   rQ   rA   r   r   rs   r   rW   rO   r   )r   r"   r   r   r   r   
get_statusZ  s    zTOMS748Solver.get_statusc              
   C   sf  |  j d7  _ ttj}| j| j| j| jf\}}}}| j	d | j	d  }d}t
d| jd D ]}t| j||g dd| drt| j	d | j	d ||| jd | jd ||}	| j	d |	  k r| j	d k rn n|	}|du rt| j	| j|||}| |}
|
dkrt|f  S || }}| ||
\}}q^t| jd t| jd k rRdnd}| j	| | j|  }}t| j	| j|dkdd\}}|d| |  }t|| d	| j	d | j	d   krt| j	d
 }ntj|||ddrt| jd }|| |d|  d k r0d| j	|  | j	d|   d }n8|dkr>dnd}|t| | j || j  }|| }| j	d |  k r| j	d k sn t| j	d
 }| |}
|
dkrt|fS || }}| ||
\}}| j	d | j	d  | j| kr6|| }}t| j	d
 }| |}|dkr&t|fS | ||\}}|| | _| _|| | _| _|  \}}||fS )zkPerform one step in the algorithm.

        Implements Algorithm 4.1(k=1) or 4.2(k=2) in [APS1995]
        r   r   Nr?       r@   Fr   rg   rC   r;      r   )r   rG   ro   rp   rc   r   r   r   r   r   rJ   r   r   r   r   r   r   r   r   rP   r   rs   rQ   frexprA   r   _MUr   )r   rc   r   r   r   r   Zab_widthr   ZnstepsZc0r   ZuixuZfur   r   Zfrsmmr`   zZfzstatusxnr   r   r   iteratec  sh    $


*

("&


 


zTOMS748Solver.iterater?   c
                 C   s   | j ||||	|d | ||||\}
}|
tkr:| |S t| j| j}| jd |  k rh| jd k sxn t| jd }| |}|dkr| |S | 	||\| _
| _d\| _| _|  jd7  _|  \}
}|
tkr| |S |
tkrd}|	r|| jd | jf }t|| |tS qdS )z3Solve f(x) = 0 given an interval containing a zero.)r   rA   rW   rZ   r   r   r   rC   )NNz5Failed to converge after %d iterations, bracket is %sN)r   r   r   r   r   r   r   rs   r   r   r   r   r   r   r   r   rO   rK   )r   r~   r"   r   rU   r   rA   r   rW   rZ   r   r   r   r   fmtr^   r   r   r   solve  s,    
"


zTOMS748Solver.solveN)T)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     s   

	Q
r   c
                 C   s   |dkrt d| |td k r0t d|tf t|}|dk rJt dt|s`t d| t|svt d| ||krt d	|||dkst d
| t|ts|f}t	 }
|
j
| ||||||||	d	}|\}}}}t|||||fS )a  
    Find a zero using TOMS Algorithm 748 method.

    Implements the Algorithm 748 method of Alefeld, Potro and Shi to find a
    zero of the function `f` on the interval `[a , b]`, where `f(a)` and
    `f(b)` must have opposite signs.

    It uses a mixture of inverse cubic interpolation and
    "Newton-quadratic" steps. [APS1995].

    Parameters
    ----------
    f : function
        Python function returning a scalar. The function :math:`f`
        must be continuous, and :math:`f(a)` and :math:`f(b)`
        have opposite signs.
    a : scalar,
        lower boundary of the search interval
    b : scalar,
        upper boundary of the search interval
    args : tuple, optional
        containing extra arguments for the function `f`.
        `f` is called by ``f(x, *args)``.
    k : int, optional
        The number of Newton quadratic steps to perform each
        iteration. ``k>=1``.
    xtol : scalar, 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 : scalar, optional
        The computed root ``x0`` will satisfy ``np.allclose(x, x0,
        atol=xtol, rtol=rtol)``, where ``x`` is the exact root.
    maxiter : int, optional
        If convergence is not achieved in `maxiter` iterations, an error is
        raised. Must be >= 0.
    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.
        Otherwise, the convergence status is recorded in the `RootResults`
        return object.

    Returns
    -------
    x0 : float
        Approximate Zero of `f`
    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, ridder, bisect, newton
    fsolve : find zeroes in N dimensions.

    Notes
    -----
    `f` must be continuous.
    Algorithm 748 with ``k=2`` is asymptotically the most efficient
    algorithm known for finding roots of a four times continuously
    differentiable function.
    In contrast with Brent's algorithm, which may only decrease the length of
    the enclosing bracket on the last step, Algorithm 748 decreases it each
    iteration with the same asymptotic efficiency as it finds the root.

    For easy statement of efficiency indices, assume that `f` has 4
    continuouous deriviatives.
    For ``k=1``, the convergence order is at least 2.7, and with about
    asymptotically 2 function evaluations per iteration, the efficiency
    index is approximately 1.65.
    For ``k=2``, the order is about 4.6 with asymptotically 3 function
    evaluations per iteration, and the efficiency index 1.66.
    For higher values of `k`, the efficiency index approaches
    the kth root of ``(3k-2)``, hence ``k=1`` or ``k=2`` are
    usually appropriate.

    References
    ----------
    .. [APS1995]
       Alefeld, G. E. and Potra, F. A. and Shi, Yixun,
       *Algorithm 748: Enclosing Zeros of Continuous Functions*,
       ACM Trans. Math. Softw. Volume 221(1995)
       doi = {10.1145/210089.210111}

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

    >>> from scipy import optimize
    >>> root, results = optimize.toms748(f, 0, 2, full_output=True)
    >>> root
    1.0
    >>> results
          converged: True
               flag: 'converged'
     function_calls: 11
         iterations: 5
               root: 1.0
    r   rw   r   rx   r   r=   za is not finite %szb is not finite %sz$a and b are not an interval [{}, {}]zk too small (%s < 1))rU   r   r   rA   rW   rZ   )rD   r{   rE   rF   rG   r   rt   ry   rz   r   r   r9   )r~   r"   r   rU   r   r   rA   rW   r3   rZ   Zsolverri   r5   r   r   r   r   r   r   r     s.    j



r   )	Nr   r:   r;   NNr<   FT)NTT),rL   collectionsr   rE    r   ZnumpyrG   r   r   ro   rp   rc   r{   __all__r   Z	_ESIGNERRrO   Z
_EVALUEERRr   Z	CONVERGEDZSIGNERRZCONVERRZVALUEERRZ
INPROGRESSr   r   r8   r9   r   rI   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s~   %   
  f
U
a
 
o	
  
#
# P