§
    fŠtj§O  ã                   ó”   — d Z ddlZddlmZ ddlmZ dgZg d¢Z	 G d„ d	¦  «        Z
	 	 	 	 	 dd„Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ ZdS )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                   ó0   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ ZdS )Ú
MemoizeDeraœ  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                 ó>   — || _         d | _        d | _        d| _        d S ©Nr   )ÚfunÚvalsÚxÚn_calls)Úselfr   s     úY/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/optimize/_root_scalar.pyÚ__init__zMemoizeDer.__init__   s"   € ØˆŒØˆŒ	ØˆŒØˆŒˆˆó    c                 ó¨   — | j         �|| j        k    r4 | j        |g|¢R Ž }|| _        | xj        dz  c_        |dd…         | _         | j         d         S )z,Calculate f or use cached value if availableNr   r   )r   r   r   r   )r   r   ÚargsÚfgs       r   Ú__call__zMemoizeDer.__call__$   sa   € ð Œ9Ð  T¤V¢ Ø�”˜!Ð#˜dÐ#Ð#Ð#ˆBØˆDŒFØˆLŒL˜AÑˆLŒLØ˜1˜1˜1œˆDŒIØŒy˜Œ|Ðr   c                 óR   — | j         �|| j        k    r	 | |g|¢R Ž  | j         d         S )z/Calculate f' or use a cached value if availableNr   ©r   r   ©r   r   r   s      r   ÚfprimezMemoizeDer.fprime.   ó3   € àŒ9Ð  T¤V¢ ØˆD�ˆN�TˆNˆNˆNˆNØŒy˜Œ|Ðr   c                 óR   — | j         �|| j        k    r	 | |g|¢R Ž  | j         d         S )z0Calculate f'' or use a cached value if availableNé   r!   r"   s      r   Úfprime2zMemoizeDer.fprime24   r$   r   c                 ó   — | j         S )N)r   )r   s    r   ÚncallszMemoizeDer.ncalls:   s
   € ØŒ|Ðr   N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   r'   r)   © r   r   r   r      si   € € € € € ðð ðð ð ðð ð ðð ð ðð ð ðð ð ð ð r   r   r.   c           	      óð  ‡ — t          |t          ¦  «        s|f}|€i }d}|�@t          |¦  «        s1t          |¦  «        r t	          ‰ ¦  «        Š d}‰ j        }‰ j        }nd}|�9t          |¦  «        s*t          |¦  «        rt	          ‰ ¦  «        Š d}‰ j        }nd}i }dD ]*}t          ¦   «                              |¦  «        }|�|||<   Œ+|r| 	                    |¦  «         | 	                    dd¬¦  «         |s|�d}n|�|r|rd}n
d}n|�d	}nd}|st          d
¦  «        ‚|                     ¦   «         }dddœ}	 t          t          |                     ||¦  «        ¦  «        }n%# t          $ r}t          d|› �¦  «        |‚d}~ww xY w|dv rÀt          |t          t          z  t           j        z  ¦  «        st          d|› �¦  «        ‚|dd…         \  }}	  |‰ ||fd|i|¤Ž\  }}�nx# t          $ rW}t%          |d¦  «        r;t          j        |j        t           j        |j        t/          |¦  «        |¬¦  «        }n‚ Y d}~�nd}~ww xY w|dv rC|€t          d|› �¦  «        ‚d|v r|                     d¦  «        |d<    |‰ |f|dd|dœ|¤Ž\  }}nÍ|dv rI|€t          d|› �¦  «        ‚|sˆ fd„}d|v r|                     d¦  «        |d<    |‰ |f||ddœ|¤Ž\  }}n€|dv rj|€t          d|› �¦  «        ‚|st          d|› �¦  «        ‚|st          d|› �¦  «        ‚d|v r|                     d¦  «        |d<    |‰ |f|||dœ|¤Ž\  }}nt          d|› �¦  «        ‚|r‰ j        }||_        |S )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 r0   Útol)r   r#   r'   Úx1)r   c                 ó@   •— ˆfd„}t          || d|¬¦  «        d         S )Nc                 ó"   •—  ‰| d         g|¢R Ž S r   r.   )r   r   Úfs     €r   Ú	f_wrappedz.root_scalar.<locals>.fprime.<locals>.f_wrappedA  s   ø€ Ø˜1˜Q˜qœT˜> D˜>˜>˜>Ð)r   z2-point)r:   r   r   r   )r   r   r@   r?   s      €r   r#   zroot_scalar.<locals>.fprime9  s9   ø€ ð*ð *ð *ð *ð *å(¨°A¸iÈdÐSÑSÔSÐTUÔ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ÚRootResultsr5   ÚnanÚ_function_callsÚstrÚpopr   r8   )r?   r   r:   Úbracketr#   r'   Úx0r<   r0   r1   r2   ÚoptionsÚis_memoizedÚkwargsÚkÚvÚmethÚmap2underlyingÚmethodcÚeÚaÚbÚrÚsolr   s   `                        r   r   r   >   s  ø€ õr �d�EÑ"Ô"ð Øˆwˆà€Øˆð €KØÐ¥8¨GÑ#4Ô#4ÐÝ�‰=Œ=ð 	Ý˜1‘”ˆAØˆKØ”iˆGØ”XˆFˆFàˆGØÐ¥(¨6Ñ"2Ô"2ÐÝ�‰<Œ<ð 	Ý˜1‘”ˆAØˆKØ”XˆFˆFàˆFð €FØ(ð ð ˆÝ‰HŒH�LŠL˜‰OŒOˆØˆ=ØˆF�1‰Iøð ð Ø�Š�gÑÔÐð ‡M‚M˜d¨€MÑ/Ô/Ð/ð ð "ØÐØˆFˆFØˆ^Øð "Øð &Ø%�F�Fà%�F�FØ�Ø!��à!�Øð 9Ýð 8ñ 9ô 9ð 	9ð �<Š<‰>Œ>€DØ (°HÐ=Ð=€Nð:Ý�( N×$6Ò$6°t¸TÑ$BÔ$BÑCÔCˆˆøÝð :ð :ð :ÝÐ1¨4Ð1Ð1Ñ2Ô2¸Ð9øøøøð:øøøð ÐBÐBÐBÝ˜'¥4­%¡<µ"´*Ñ#<Ñ=Ô=ð 	=ÝÐ;°6Ð;Ð;Ñ<Ô<Ð<à�r˜�rŒ{‰ˆˆ1ð	Ø�W˜Q  1Ð:Ð:¨4Ð:°6Ð:Ð:‰FˆAˆs‰søÝð 	ð 	ð 	õ
 �q˜$ÑÔð ÝÔ*°´Ý68´fØ:;Ô:KÝ03°A±´¸vðGñ Gô G��ð
 ð ���‘øøøøð	øøøð 
�Ð	Ð	Øˆ:ÝÐ@¸Ð@Ð@ÑAÔAÐAØ�VÐÐØ"ŸJšJ vÑ.Ô.ˆF�5‰MØ�˜˜Bð * T°$ÀØð*ð *Ø"(ð*ð *‰ˆˆ3ˆ3à	�Ð	Ð	Øˆ:ÝÐ@¸Ð@Ð@ÑAÔAÐAØð 	Wð
Wð 
Wð 
Wð 
Wð 
Wð �VÐÐØ"ŸJšJ vÑ.Ô.ˆF�5‰MØ�˜˜Bð # T°&À$ð #ð #Ø!ð#ð #‰ˆˆ3ˆ3à	�Ð	Ð	Øˆ:ÝÐ@¸Ð@Ð@ÑAÔAÐAØð 	GÝÐE¸VÐEÐEÑFÔFÐFØð 	HÝÐF¸fÐFÐFÑGÔGÐGØ�VÐÐØ"ŸJšJ vÑ.Ô.ˆF�5‰MØ�˜˜BÐT T°&À'ÐTÐTÈVÐTÐT‰ˆˆ3ˆ3åÐ3¨6Ð3Ð3Ñ4Ô4Ð4àð %ð ”)ˆØ$ˆÔà€Js1   Ä?)E) Å)
FÅ3FÆFÇG. Ç.
IÇ8AI
É
Ic                  ó   — dS )aA  
    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

    Nr.   r.   r   r   Ú_root_scalar_brentq_docrf   _  ó	   € ð& 	€Dr   c                  ó   — dS ©aB  
    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.

    Nr.   r.   r   r   Ú_root_scalar_brenth_docrj   u  rg   r   c                  ó   — dS ri   r.   r.   r   r   Ú_root_scalar_toms748_docrl   Š  rg   r   c                  ó   — dS )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.

    Nr.   r.   r   r   Ú_root_scalar_secant_docrn      s	   € ð* 	€Dr   c                  ó   — dS )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.

    Nr.   r.   r   r   Ú_root_scalar_newton_docrp   ¸  s	   € ð. 	€Dr   c                  ó   — dS )ar  
    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.

    Nr.   r.   r   r   Ú_root_scalar_halley_docrr   Ò  s	   € ð8 	€Dr   c                  ó   — dS ri   r.   r.   r   r   Ú_root_scalar_ridder_docrt   ñ  rg   r   c                  ó   — dS ri   r.   r.   r   r   Ú_root_scalar_bisect_docrv     rg   r   )r.   NNNNNNNNNN)r-   ÚnumpyrN   Ú r   rK   Ú_numdiffr   Ú__all__ÚROOT_SCALAR_METHODSr   r   rf   rj   rl   rn   rp   rr   rt   rv   r.   r   r   ú<module>r|      s/  ððð ð Ð Ð Ð à #Ð #Ð #Ð #Ð #Ð #Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'àˆ/€ð5ð 5ð 5Ð ð'ð 'ð 'ð 'ð 'ñ 'ô 'ð 'ðT 26Ø%)Ø Ø.2Øð	^ð ^ð ^ð ^ðB		ð 	ð 	ð,	ð 	ð 	ð*	ð 	ð 	ð,	ð 	ð 	ð0	ð 	ð 	ð4	ð 	ð 	ð>	ð 	ð 	ð,	ð 	ð 	ð 	ð 	r   