
    (ph?`                         S SK r S SKrS SKJs  Jr  S SKJr  S SK	J
r
JrJr  SSSSS SSS.S jrS rSSSSSSSS.S	 jrg)
    N)_RichResult)xp_signxp_copyxp_take_along_axis )argsxatolxrtolfatolfrtolmaxitercallbackc                $  ^ [        XXEXgX5      n
U
u  ppEpgp[        R                  " XU4U5      nUu  pppmUu  nnUu  nnTR                  UTR	                  [        R
                  5      TR                  S9nSu  nnTR                  U5      nUc  SUR                  -  OUnUc  SUR                  -  OUnUc  UR                  OUnUTR                  TR                  U5      TR                  U5      5      -  nUcA  [        R                  " UR                  5      [        R                  " UR                  5      -
  OUn[        UUUUSSSXEXgUUUS9n/ SQnS nU4S	 jnU4S
 jnU4S jnU4S jn[        R                   " UXXX?UUUUUUTS9$ )a  Find the root of an elementwise function using Chandrupatla's algorithm.

For each element of the output of `func`, `chandrupatla` seeks the scalar
root that makes the element 0. This function allows for `a`, `b`, and the
output of `func` to be of any broadcastable shapes.

Parameters
----------
func : callable
    The function whose root is desired. The signature must be::

        func(x: ndarray, *args) -> ndarray

     where each element of ``x`` is a finite real and ``args`` is a tuple,
     which may contain an arbitrary number of components of any type(s).
     ``func`` must be an elementwise function: each element ``func(x)[i]``
     must equal ``func(x[i])`` for all indices ``i``. `_chandrupatla`
     seeks an array ``x`` such that ``func(x)`` is an array of zeros.
a, b : array_like
    The lower and upper bounds of the root of the function. Must be
    broadcastable with one another.
args : tuple, optional
    Additional positional arguments to be passed to `func`.
xatol, xrtol, fatol, frtol : float, optional
    Absolute and relative tolerances on the root and function value.
    See Notes for details.
maxiter : int, optional
    The maximum number of iterations of the algorithm to perform.
    The default is the maximum possible number of bisections within
    the (normal) floating point numbers of the relevant dtype.
callback : callable, optional
    An optional user-supplied function to be called before the first
    iteration and after each iteration.
    Called as ``callback(res)``, where ``res`` is a ``_RichResult``
    similar to that returned by `_chandrupatla` (but containing the current
    iterate's values of all variables). If `callback` raises a
    ``StopIteration``, the algorithm will terminate immediately and
    `_chandrupatla` will return a result.

Returns
-------
res : _RichResult
    An instance of `scipy._lib._util._RichResult` with the following
    attributes. The descriptions are written as though the values will be
    scalars; however, if `func` returns an array, the outputs will be
    arrays of the same shape.

    x : float
        The root of the function, if the algorithm terminated successfully.
    nfev : int
        The number of times the function was called to find the root.
    nit : int
        The number of iterations of Chandrupatla's algorithm performed.
    status : int
        An integer representing the exit status of the algorithm.
        ``0`` : The algorithm converged to the specified tolerances.
        ``-1`` : The algorithm encountered an invalid bracket.
        ``-2`` : The maximum number of iterations was reached.
        ``-3`` : A non-finite value was encountered.
        ``-4`` : Iteration was terminated by `callback`.
        ``1`` : The algorithm is proceeding normally (in `callback` only).
    success : bool
        ``True`` when the algorithm terminated successfully (status ``0``).
    fun : float
        The value of `func` evaluated at `x`.
    xl, xr : float
        The lower and upper ends of the bracket.
    fl, fr : float
        The function value at the lower and upper ends of the bracket.

Notes
-----
Implemented based on Chandrupatla's original paper [1]_.

If ``xl`` and ``xr`` are the left and right ends of the bracket,
``xmin = xl if abs(func(xl)) <= abs(func(xr)) else xr``,
and ``fmin0 = min(func(a), func(b))``, then the algorithm is considered to
have converged when ``abs(xr - xl) < xatol + abs(xmin) * xrtol`` or
``fun(xmin) <= fatol + abs(fmin0) * frtol``. This is equivalent to the
termination condition described in [1]_ with ``xrtol = 4e-10``,
``xatol = 1e-5``, and ``fatol = frtol = 0``. The default values are
``xatol = 4*tiny``, ``xrtol = 4*eps``, ``frtol = 0``, and ``fatol = tiny``,
where ``eps`` and ``tiny`` are the precision and smallest normal number
of the result ``dtype`` of function inputs and outputs.

References
----------

.. [1] Chandrupatla, Tirupathi R.
    "A new hybrid quadratic/bisection algorithm for finding the zero of a
    nonlinear function without using derivatives".
    Advances in Engineering Software, 28(3), 145-149.
    https://doi.org/10.1016/s0965-9978(96)00051-8

See Also
--------
brentq, brenth, ridder, bisect, newton

Examples
--------
>>> from scipy import optimize
>>> def f(x, c):
...     return x**3 - 2*x - c
>>> c = 5
>>> res = optimize._chandrupatla._chandrupatla(f, 0, 3, args=(c,))
>>> res.x
2.0945514818937463

>>> c = [3, 4, 5]
>>> res = optimize._chandrupatla._chandrupatla(f, 0, 3, args=(c,))
>>> res.x
array([1.8932892 , 2.        , 2.09455148])

dtype)r      N         ?)x1f1x2f2x3f3tr	   r
   r   r   nitnfevstatus)	r   r   )xxmin)funfminr   r   r   r   xlr   flr   )xrr   )frr   c                 l    U R                   U R                  U R                  U R                   -
  -  -   nU$ N)r   r   r   )workr    s     O/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_chandrupatla.pypre_func_eval$_chandrupatla.<locals>.pre_func_eval   s+    GGdff$'' 122    c                   > TR                  UR                  SS9TR                  UR                  SS9sUl        Ul        TR                  U5      TR                  UR                  5      :H  nU) nUR                  U   UR                  U   sUR                  U'   UR                  U'   UR                  U   UR                  U   sUR                  U'   UR                  U'   XsUl        Ul        g )NT)copy)asarrayr   r   r   r   signr   r   )r    fr.   jnjxps        r/   post_func_eval%_chandrupatla.<locals>.post_func_eval   s     JJtwwTJ:JJtwwTJ: 	GGAJ"''$''**R!%TWWQZ
DGGAJ#'772; TWWR[r2   c                   > TR                  U R                  5      TR                  U R                  5      :  nTR                  XR                  U R
                  5      U l        TR                  XR                  U R                  5      U l        TR                  U R                  TR                  S9nTR                  U R                  5      U R                  U R                  -   :*  n[        R                  U R                  U'   SX!'   [        U R                  5      [        U R                  5      :H  U) -  nTR!                  TR"                  U R                  R$                  S9nX3[        R&                  sU R                  U'   U R                  U'   U R                  U'   SX!'   TR)                  U R                  5      TR)                  U R
                  5      -  ) nTR+                  U R                  5      TR+                  U R                  5      -  nXE-  U) -  nX3[        R,                  sU R                  U'   U R                  U'   U R                  U'   SX!'   TR                  U R
                  U R                  -
  5      U l        TR                  U R                  5      U R0                  -  U R2                  -   U l        U R.                  U R4                  :  n[        R                  U R                  U'   SX!'   U$ )Nr   T)absr   r   wherer   r   r!   r#   
zeros_likeboolr   r   eim_ECONVERGEDr   r   r5   nanr   	_ESIGNERRisfiniteisnan
_EVALUEERRdxr
   r	   tol)r.   istopNaNx_nonfinitef_nanr:   s         r/   check_termination(_chandrupatla.<locals>.check_termination   s(   
 FF477ObffTWWo-HHQ1	HHQ1	}}TWWBGG}4 FF499djj!88A TWW!11dU:jjtyyj758s}}2		!diilDKKN DGG,r{{477/CCD!BHHTWW$55 TE)58s~~2		!diilDKKN &&477*+66$))$tzz1DJJ>GGdhhAr2   c                 N  > U R                   U R                  -
  U R                  U R                  -
  -  n[        R                  " SSS9   U R
                  U R                  -
  U R                  U R                  -
  -  nS S S 5        U R                  U R                   -
  U R                  U R                   -
  -  nSTR                  SU-
  5      -
  W:  UTR                  U5      :  -  nU R
                  U   U R                  U   U R                  U   X4   4u  pVpxTR                  UTR                  S5      5      n	XUU-
  -  U-  Xv-
  -  X-  Xu-
  -  U-  Xg-
  -  -
  X'   SU R                  -  U R                  -  n
TR                  XSU
-
  5      U l        g ! , (       d  f       GN= f)Nignore)divideinvalid   r   )r   r   r   nperrstater   r   r   sqrt	full_liker5   rJ   rI   clipr   )r.   xi1phi1alphar8   f1jf2jf3jalphajr   tlr:   s              r/   post_termination_check-_chandrupatla.<locals>.post_termination_check   sd   ww TWWtww%67[[(;GGdgg%$''DGG*;<D <477"tww'89"''!c'""d*tbggcl/BC $
DGGAJ
EH L#LL

30Sy!C'395,#),s2ci@A
 488^dgg%B' <;s   
4F
F$c                    > U S   U S   U S   U S   4u  p#pEU S   U S   :  nTR                  XbU5      U S'   TR                  XcU5      U S'   TR                  XdU5      U S'   TR                  XeU5      U S'   U$ Nr'   r*   r)   r+   r?   resshaper'   r*   r)   r+   rK   r:   s          r/   customize_result'_chandrupatla.<locals>.customize_result   s    TCIs4y#d)CID	!HHQB'D	HHQB'D	HHQB'D	HHQB'D	r2   r:   )_chandrupatla_ivrB   _initializerZ   r5   _EINPROGRESSint32finfosmallest_normalepsminimumr>   mathlog2maxr   _loop) funcabr   r	   r
   r   r   r   r   rj   tempxsfsrk   r   r   r   r   r   r   r   r   rs   r.   res_work_pairsr0   r;   rP   rd   rl   r:   s                                   @r/   _chandrupatlar      s   h 4u <C@C=DeG ??4Q.D+/(DbbFBFB\\"bjj)9)9: "  *FICHHUOE',}Ae###%E =AeiiKeE%*]E!!EBJJrvvbz266":66E/ yy#dii0E0E&FF'. "rdts"uT&:D@N
	 )V($ 99T8G4"N4E+-=~ r2   c                    [        U 5      (       d  [        S5      e[        R                  " U5      (       d  U4n[        R                  " Ub  UOSUb  UOSUb  UOSUb  UOS/5      n[        R
                  " UR                  [        R                  5      (       a]  [        R                  " US:  5      (       d?  [        R                  " [        R                  " U5      5      (       d  UR                  S:w  a  [        S5      eUb!  [        U5      n	Xi:w  d  US:  a  [        S5      eUb  [        U5      (       d  [        S5      eXX#XEXg4$ )Nz`func` must be callable.rV   r   )r   z(Tolerances must be non-negative scalars.z)`maxiter` must be a non-negative integer.z`callback` must be callable.)callable
ValueErrorrW   iterabler5   
issubdtyper   numberanyrG   rk   int)
r{   r   r	   r
   r   r   r   r   tolsmaxiter_ints
             r/   ro   ro      s    D>>344;;tw :: 1uq % 1uq % 1uq % 1uq: ;D MM$**bii00BFF4!84D4Dvvbhhtn%%t);CDD'l!Wq[HIIHX$6$6788uU7DDr2   d   c                  ^! [        XXVXxX5      nUu  ppVpxpXU4n[        R                  " XU5      nUu  ppnnm!Uu  pnUu  nnnT!R                  SUS9S   nT!R	                  UT!R                  [        R
                  5      T!R                  S9nSu  nnUc  T!R                  U5      R                  OUnUc  T!R                  U5      R                  OUnUc  T!R                  U5      R                  OUnUc/  [        R                  " T!R                  U5      R                  5      OUnT!R                  XU45      T!R                  UUU45      pT!R                  USS9n[        UUSS9u  pn[        UUSS9u  nnn[        U5      n[!        S0 SU_SU_S	U_S
U_SU_SU_SU_SU_SU_SU_SU_SU_SU_SU_SU_SU_6n/ SQnU!4S jnS nU!4S jnS nU!4S jn [        R"                  " UXXUUUUUUU UT!S9$ )a  Find the minimizer of an elementwise function.

For each element of the output of `func`, `_chandrupatla_minimize` seeks
the scalar minimizer that minimizes the element. This function allows for
`x1`, `x2`, `x3`, and the elements of `args` to be arrays of any
broadcastable shapes.

Parameters
----------
func : callable
    The function whose minimizer is desired. The signature must be::

        func(x: ndarray, *args) -> ndarray

     where each element of ``x`` is a finite real and ``args`` is a tuple,
     which may contain an arbitrary number of arrays that are broadcastable
     with `x`. ``func`` must be an elementwise function: each element
     ``func(x)[i]`` must equal ``func(x[i])`` for all indices ``i``.
     `_chandrupatla` seeks an array ``x`` such that ``func(x)`` is an array
     of minima.
x1, x2, x3 : array_like
    The abscissae of a standard scalar minimization bracket. A bracket is
    valid if ``x1 < x2 < x3`` and ``func(x1) > func(x2) <= func(x3)``.
    Must be broadcastable with one another and `args`.
args : tuple, optional
    Additional positional arguments to be passed to `func`.  Must be arrays
    broadcastable with `x1`, `x2`, and `x3`. If the callable to be
    differentiated requires arguments that are not broadcastable with `x`,
    wrap that callable with `func` such that `func` accepts only `x` and
    broadcastable arrays.
xatol, xrtol, fatol, frtol : float, optional
    Absolute and relative tolerances on the minimizer and function value.
    See Notes for details.
maxiter : int, optional
    The maximum number of iterations of the algorithm to perform.
callback : callable, optional
    An optional user-supplied function to be called before the first
    iteration and after each iteration.
    Called as ``callback(res)``, where ``res`` is a ``_RichResult``
    similar to that returned by `_chandrupatla_minimize` (but containing
    the current iterate's values of all variables). If `callback` raises a
    ``StopIteration``, the algorithm will terminate immediately and
    `_chandrupatla_minimize` will return a result.

Returns
-------
res : _RichResult
    An instance of `scipy._lib._util._RichResult` with the following
    attributes. (The descriptions are written as though the values will be
    scalars; however, if `func` returns an array, the outputs will be
    arrays of the same shape.)

    success : bool
        ``True`` when the algorithm terminated successfully (status ``0``).
    status : int
        An integer representing the exit status of the algorithm.
        ``0`` : The algorithm converged to the specified tolerances.
        ``-1`` : The algorithm encountered an invalid bracket.
        ``-2`` : The maximum number of iterations was reached.
        ``-3`` : A non-finite value was encountered.
        ``-4`` : Iteration was terminated by `callback`.
        ``1`` : The algorithm is proceeding normally (in `callback` only).
    x : float
        The minimizer of the function, if the algorithm terminated
        successfully.
    fun : float
        The value of `func` evaluated at `x`.
    nfev : int
        The number of points at which `func` was evaluated.
    nit : int
        The number of iterations of the algorithm that were performed.
    xl, xm, xr : float
        The final three-point bracket.
    fl, fm, fr : float
        The function value at the bracket points.

Notes
-----
Implemented based on Chandrupatla's original paper [1]_.

If ``x1 < x2 < x3`` are the points of the bracket and ``f1 > f2 <= f3``
are the values of ``func`` at those points, then the algorithm is
considered to have converged when ``x3 - x1 <= abs(x2)*xrtol + xatol``
or ``(f1 - 2*f2 + f3)/2 <= abs(f2)*frtol + fatol``. Note that first of
these differs from the termination conditions described in [1]_. The
default values of `xrtol` is the square root of the precision of the
appropriate dtype, and ``xatol = fatol = frtol`` is the smallest normal
number of the appropriate dtype.

References
----------
.. [1] Chandrupatla, Tirupathi R. (1998).
    "An efficient quadratic fit-sectioning algorithm for minimization
    without derivatives".
    Computer Methods in Applied Mechanics and Engineering, 152 (1-2),
    211-217. https://doi.org/10.1016/S0045-7825(97)00190-4

See Also
--------
golden, brent, bounded

Examples
--------
>>> from scipy.optimize._chandrupatla import _chandrupatla_minimize
>>> def f(x, args=1):
...     return (x - args)**2
>>> res = _chandrupatla_minimize(f, -5, 0, 5)
>>> res.x
1.0
>>> c = [1, 1.5, 2]
>>> res = _chandrupatla_minimize(f, -5, 0, 5, args=(c,))
>>> res.x
array([1. , 1.5, 2. ])
gw?r   r   )r      r   )axisr   r   r   r   r   r   phir	   r
   r   r   r   r   r   q0r   )r   )r    r   )r"   r   r$   r%   r&   )xmr   )r*   r   r(   )fmr   )r+   r   c                   > U R                   U R                  -
  nU R                  U R                   -
  nXR                  U R                  -
  -  nX R
                  U R                  -
  -  nX3U-   -  nSXPR                  U R                  -
  -  U R                   -   U R                  -   -  nTR                  X`R                  -
  5      STR                  U5      -  :  nXg   nTR                  Xg   U R                   U   -
  5      U R                  U   :*  n	U R                   U   U	   [        X'   U	   5      U R                  U   U	   -  -   X'   U R                   SU R                  -
  U-  -   n
XU'   X`l        U
$ )Nr   r   )r   r   r   r   r   r   r>   r   xtolr   r   )r.   x21x32ABCq1rK   xir8   r    r:   s              r/   r0   -_chandrupatla_minimize.<locals>.pre_func_eval  sK    gggg 77TWW$%77TWW$%QKAww()DGG3dgg=>
 FF2< 3#44U
 FF254771:%&$))A,6
1q	 2TYYq\!_ DD GGq488|s**! r2   c                 P   [        XR                  -
  5      [        UR                  UR                  -
  5      :H  nX   UR                  U   UR                  U   UR                  U   4u  pEpgX   UR                  U   UR
                  U   UR                  U   4u  ppX:  nXL   X   sX|'   X'   U) nXl   X   XL   X   4u  X\'   X'   Xl'   X'   U) nX   UR                  U   UR                  U   UR                  U   4u  pnnX   UR                  U   UR
                  U   UR                  U   4u  nnnnUU:  nX   UU   sX'   UU'   U) nUU   UU   X   UU   4u  UU'   UU'   UU'   UU'   XVUsUR                  U'   UR                  U'   UR                  U'   XUsUR                  U'   UR
                  U'   UR                  U'   UUUsUR                  U'   UR                  U'   UR                  U'   UUUsUR                  U'   UR
                  U'   UR                  U'   g r-   )r   r   r   r   r   r   r   )r    r7   r.   rK   r   x1ix2ix3ifif1if2if3ir8   nixnix1nix2nix3nifnif1nif2nif3nis                         r/   r;   ._chandrupatla_minimize.<locals>.post_func_eval  s-   
 AK GDGGdgg,=$>>D$''!*dggaj$''!*ED$''!*dggaj$''!*DHB),)E&R !twwr{DGGBK M4 !twwr{DGGBK LT4$J63q6aB-1!Wd1gsvs1v-M*Qa$q'47-0s*
DGGAJ
-0s*
DGGAJ
04dD-TWWR[$''"+04dD-TWWR[$''"+r2   c                 D  > TR                  U R                  [        S9nU R                  U R                  :  U R                  U R
                  :  -  nTR                  TR                  sU R                  U'   U R                  U'   S[        R                  sX'   U R                  U'   TR                  U R                  U R                  -   U R                  -   U R                  -   U R                  -   U R
                  -   5      nX1-  ) nTR                  TR                  sU R                  U'   U R                  U'   S[        R                  sX'   U R                  U'   TR                  U R                  U R                  -
  5      TR                  U R                  U R                  -
  5      :  nU R                  U   nU R                  U   U R                  U'   X@R                  U'   U R                  U   nU R
                  U   U R                  U'   X@R
                  U'   TR                  U R                  5      U R                  -  U R                   -   U l        TR                  U R                  U R                  -
  5      SU R"                  -  :*  nTR                  U R                  5      U R$                  -  U R&                  -   nX R                  SU R                  -  -
  U R
                  -   SU-  :*  -  nX!) -  nS[        R(                  sX'   U R                  U'   U$ )Nr   Tr   )r@   r   rA   r   r   r   rD   r   rB   rE   r   rF   r   rH   r>   r
   r	   r   r   r   rC   )r.   rL   rK   finiter~   ftolr:   s         r/   rP   1_chandrupatla_minimize.<locals>.check_termination  sb   }}TWWD}1 ggDGGdgg$56!#
DGGAJ"&Q TWWTWW_TWW4TWW<TWWDTWWLMm!#
DGGAJ"&Q FF477TWW$%tww/@(AAwwqzWWQZ

wwqzWWQZ

 FF477Odjj04::=	 FF477TWW$%TYY6 vvdgg+djj8 	
ggDGG#dgg-!D&88	U
"&Qr2   c                     g r-   r   )r.   s    r/   rd   6_chandrupatla_minimize.<locals>.post_termination_check  s    r2   c                    > U S   U S   U S   U S   4u  p#pEU S   U S   :  nTR                  XcU5      U S'   TR                  XbU5      U S'   TR                  XeU5      U S'   TR                  XdU5      U S'   U$ rg   rh   ri   s          r/   rl   0_chandrupatla_minimize.<locals>.customize_result  s    TCIs4y#d)CIT"HHQB'D	HHQB'D	HHQB'D	HHQB'D	r2   rn   )ro   rB   rp   r5   rZ   rq   rr   rs   rt   rw   rY   ru   stackargsortr   r   r   rz   )"r{   r   r   r   r   r	   r
   r   r   r   r   rj   r   r~   r   rk   r   r   r   r   r   r   r   r   rK   r   r.   r   r0   r;   rP   rd   rl   r:   s"                                    @r/   _chandrupatla_minimizer     so   j 4u <C@C=DeG "B??4T*D+/(DbubJBBJBB
**%U*
3B
7C\\"bjj)9)9: "  *FIC/4}BHHUO++%E/4}BHHUO++%E/4}BHHUO++%E.3mDIIbhhuo))*E XXrrl#RXXr2rl%;


2A
A#B2JBB#B2JBB	B L" L L Lr Lb LR LS L"L*/L7<LDILL%)L28L=?LFJLD@N#JA4,\ 99T8G4"N4E+-=~ r2   )rw   numpyrW   (scipy._lib._elementwise_iterative_method_lib_elementwise_iterative_methodrB   scipy._lib._utilr   scipy._lib._array_apir   r   r   r   ro   r   r   r2   r/   <module>r      sR      6 6 ( F F ')DAtdgTE< 68t!%Ts$(Tr2   