
    (phO                         S r SSKrSSKJr  SSKJr  S/r/ SQr	 " S S	5      r
     SS
 jrS rS rS rS rS rS rS rS rg)z
Unified interfaces to root finding algorithms for real or complex
scalar functions.

Functions
---------
- root : find a root of a scalar function.
    N   )	_zeros_pyapprox_derivativeroot_scalar)bisectbrentqbrenthriddertoms748newtonsecanthalleyc                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)

MemoizeDer   a  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                 :    Xl         S U l        S U l        SU l        g Nr   )funvalsxn_calls)selfr   s     N/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_root_scalar.py__init__MemoizeDer.__init__   s    	    c                     U R                   b  XR                  :w  a8  U R                  " U/UQ76 nXl        U =R                  S-  sl        USS U l         U R                   S   $ )z,Calculate f or use cached value if availableNr   r   )r   r   r   r   )r   r   argsfgs       r   __call__MemoizeDer.__call__$   sU     99VV!#d#BFLLAL1DIyy|r   c                 j    U R                   b  XR                  :w  a	  U " U/UQ76   U R                   S   $ )z/Calculate f' or use a cached value if availabler   r   r   r   r   r   s      r   fprimeMemoizeDer.fprime.   -    99VVNTNyy|r   c                 j    U R                   b  XR                  :w  a	  U " U/UQ76   U R                   S   $ )z0Calculate f'' or use a cached value if available   r$   r%   s      r   fprime2MemoizeDer.fprime24   r(   r   c                     U R                   $ )N)r   )r   s    r   ncallsMemoizeDer.ncalls:   s    ||r   )r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   r!   r&   r+   r.   __static_attributes__ r   r   r   r      s     r   r   c           	        ^  [        U[        5      (       d  U4nUc  0 nSnUbH  [        U5      (       d8  [        U5      (       a&  [	        T 5      m SnT R
                  nT R                  nOSnUb<  [        U5      (       d,  [        U5      (       a  [	        T 5      m SnT R                  nOSn0 nS H%  n[        5       R                  U5      nUc  M!  XU'   M'     U(       a  UR                  U5        UR                  SSS9  U(       d%  Ub  SnOUb  U(       a  U(       a  SnOSnOUb  S	nOSnU(       d  [        S
5      eUR                  5       nSSS.n [        [        UR                  UU5      5      nUS;   aS  [        U[        [        [         R"                  45      (       d  [        SU 35      eUSS u  nn U" T UU4SU0UD6u  nnGOUS;   a>  Uc  [        SU 35      eSU;   a  UR1                  S5      US'   U" T U4USSUS.UD6u  nnOUS;   aI  Uc  [        SU 35      eU(       d  U 4S jnSU;   a  UR1                  S5      US'   U" T U4XSS.UD6u  nnOzUS;   af  Uc  [        SU 35      eU(       d  [        SU 35      eU(       d  [        SU 35      eSU;   a  UR1                  S5      US'   U" T U4XUS.UD6u  nnO[        SU 35      eU(       a  T R2                  nUUl        U$ ! [         a  n[        SU 35      UeSnAff = f! [         a_  n[%        US5      (       aH  [        R&                  " UR(                  [         R*                  UR,                  [/        U5      US9n SnANe SnAff = f)a  
Find a root of a scalar function.

Parameters
----------
f : callable
    A function to find a root of.

    Suppose the callable has signature ``f0(x, *my_args, **my_kwargs)``, where
    ``my_args`` and ``my_kwargs`` are required positional and keyword arguments.
    Rather than passing ``f0`` as the callable, wrap it to accept
    only ``x``; e.g., pass ``fun=lambda x: f0(x, *my_args, **my_kwargs)`` as the
    callable, where ``my_args`` (tuple) and ``my_kwargs`` (dict) have been
    gathered before invoking this function.
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 the objective function and of the derivative.
    `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 the objective function and of the
    first and second derivatives.
    `fprime2` can also be a callable returning the second 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.

Arguments for each method are as follows (x=required, o=optional).

+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+
|                    method                     | f | args | bracket | x0 | x1 | fprime | fprime2 | xtol | rtol | maxiter | options |
+===============================================+===+======+=========+====+====+========+=========+======+======+=========+=========+
| :ref:`bisect <optimize.root_scalar-bisect>`   | x |  o   |    x    |    |    |        |         |  o   |  o   |    o    |   o     |
+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+
| :ref:`brentq <optimize.root_scalar-brentq>`   | x |  o   |    x    |    |    |        |         |  o   |  o   |    o    |   o     |
+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+
| :ref:`brenth <optimize.root_scalar-brenth>`   | x |  o   |    x    |    |    |        |         |  o   |  o   |    o    |   o     |
+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+
| :ref:`ridder <optimize.root_scalar-ridder>`   | x |  o   |    x    |    |    |        |         |  o   |  o   |    o    |   o     |
+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+
| :ref:`toms748 <optimize.root_scalar-toms748>` | x |  o   |    x    |    |    |        |         |  o   |  o   |    o    |   o     |
+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+
| :ref:`secant <optimize.root_scalar-secant>`   | x |  o   |         | x  | o  |        |         |  o   |  o   |    o    |   o     |
+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+
| :ref:`newton <optimize.root_scalar-newton>`   | x |  o   |         | x  |    |   o    |         |  o   |  o   |    o    |   o     |
+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+
| :ref:`halley <optimize.root_scalar-halley>`   | x |  o   |         | x  |    |   x    |    x    |  o   |  o   |    o    |   o     |
+-----------------------------------------------+---+------+---------+----+----+--------+---------+------+------+---------+---------+

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)


NFT)xtolrtolmaxiter)full_outputdispr	   r   r   r   zIUnable to select a solver as neither bracket nor starting point provided.)r   r   zUnknown solver )r   r   r	   r
   r   zBracket needed for r*   r   _x)root
iterationsfunction_callsflagmethod)r   zx0 must not be None for r8   tol)r   r&   r+   x1)r   c                 ,   > U4S jn[        X SUS9S   $ )Nc                    > T" U S   /UQ76 $ r   r6   )r   r   fs     r   	f_wrapped.root_scalar.<locals>.fprime.<locals>.f_wrappedA  s    QqT>D>)r   z2-point)rB   r   r   r   )r   r   rH   rG   s      r   r&   root_scalar.<locals>.fprime9  s    *(idSTUVVr   )r   r&   r+   )r   zfprime must be specified for zfprime2 must be specified for )
isinstancetuplecallableboolr   r+   r&   localsgetupdate
ValueErrorlowergetattroptzerosAttributeErrorlistnpndarrayhasattrRootResultsr=   nan_function_callsstrpopr   r@   )rG   r   rB   bracketr&   r+   x0rD   r8   r9   r:   optionsis_memoizedkwargskvmethmap2underlyingmethodceabrsolr   s   `                        r   r   r   >   s   r dE""w K8G#4#4==1AKiiGXXFG(6"2"2<<1AKXXFF F(HLLO=1I ) g MMdM/ F^%F%F!! 8 9 	9 <<>D (H=N:(N$6$6tT$BC BB'D%#<==26(;<<r{1	Q1:4:6:FAs 
	:7x@AAV"JJv.F5MB *T$*"(*3		:7x@AA
W V"JJv.F5MB #T$ #!#3		:7x@AA<VHEFF=fXFGGV"JJv.F5MBTT'TVT3?6(344 ))$JO  :?4&129:  	
 q$**68ff:;:K:K03AvG
 	s7    K6 ;L 6
L LL
N !AM;:M;;N c                      g)a  
Options
-------
args : tuple, optional
    Extra arguments passed to the objective function.
bracket: A sequence of 2 floats, optional
    An interval bracketing a root.  ``f(x, *args)`` must have different
    signs at the two endpoints.
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

Nr6   r6   r   r   _root_scalar_brentq_docrp   _      & 	r   c                      ga  
Options
-------
args : tuple, optional
    Extra arguments passed to the objective function.
bracket: A sequence of 2 floats, optional
    An interval bracketing a root.  ``f(x, *args)`` must have different
    signs at the two endpoints.
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.

Nr6   r6   r   r   _root_scalar_brenth_docrt   u  rq   r   c                      grs   r6   r6   r   r   _root_scalar_toms748_docrv     rq   r   c                      g)a  
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, optional
    A second guess. Must be different from `x0`. If not specified,
    a value near `x0` will be chosen.
options: dict, optional
    Specifies any method-specific options not covered above.

Nr6   r6   r   r   _root_scalar_secant_docrx     s    * 	r   c                      g)a  
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.

Nr6   r6   r   r   _root_scalar_newton_docrz     s    . 	r   c                      g)a  
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.

Nr6   r6   r   r   _root_scalar_halley_docr|     s    8 	r   c                      grs   r6   r6   r   r   _root_scalar_ridder_docr~     rq   r   c                      grs   r6   r6   r   r   _root_scalar_bisect_docr     rq   r   )r6   NNNNNNNNNN)r4   numpyrX    r   rU   _numdiffr   __all__ROOT_SCALAR_METHODSr   r   rp   rt   rv   rx   rz   r|   r~   r   r6   r   r   <module>r      so     # '/5 ' 'T 26%) .2	^B		,	*	,	0	4	>	,	r   