
    (phLz                         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rSrS rSSSSSSS	.S
 jjrS rSSSSSSSS.S jrg)    N)_RichResult)array_namespacexp_ravelxp_default_dtype   c           	      j   [        U 5      (       d  [        S5      e[        R                  " U5      (       d  U4n[	        U5      nUR                  U5      S   nUR                  UR                  S5      (       a!  UR                  UR                  S5      (       a  [        S5      eUR                  UR                  S5      (       d  UR                  U[        U5      S9nSn	Uc  UR                  nS	n	Uc  UR                  * OUnUc  UR                  OUnUc  S
OUnUR                  XR                  U5      UR                  U5      UR                  U5      UR                  U5      5      u  pp4nUR                  UR                  S5      (       a!  UR                  UR                  S5      (       a  [        S5      eUR                  UR                  S5      (       a!  UR                  UR                  S5      (       a  [        S5      eUR                  UR                  S5      (       a!  UR                  UR                  S5      (       a  [        S5      eUR                  UR                  S5      (       a!  UR                  UR                  S5      (       a  [        S5      eUR                  US:  5      (       d  [        S5      eU	(       aB  XR                  XA-
  S-  UR                  S5      5      -   nUR                  X!R                  SS9nUR                  U5      nSn
UR                  UR                  S5      (       a9  UR                  [!        5       :w  d!  UR                  UR                  S5      (       a  [        U
5      e[#        US   5      nX{:X  a  US:  a  [        U
5      eXX#XEXgU4	$ )N`func` must be callable. numericcomplex floating`xl0` must be numeric and real.real floatingdtypeFT       @`xr0` must be numeric and real. `xmin` must be numeric and real. `xmax` must be numeric and real."`factor` must be numeric and real.   0All elements of `factor` must be greater than 1.   g      ?copy)`maxiter` must be a non-negative integer.r   )callable
ValueErrornpiterabler   asarrayisdtyper   r   naninfbroadcast_arraysallminimumastypeshapetupleint)funcxl0xr0xminxmaxfactorargsmaxiterxpxr0_not_suppliedmessagemaxiter_ints               J/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_bracket.py_bracket_root_ivr9   	   s   D>>344;;tw		B
**S/"
CJJsyy),,::cii!344:;;::cii11jj$4R$8j9
 
{fflBFF7D\266tD>RvF#%#6#6ZZ_bjj.

40@"**VBT$V Cd& JJsyy),,::cii!344:;;JJtzz9--::djj"455;<<JJtzz9--::djj"455;<<JJv||Y//::fll$677=>>66&1*KLL JJ
Q

3@@iiYYUi3jj!G9GJJw}}i00GMMUW4Lzz'--);<<!!gbk"K!Wq[!!ctb@@    r   i  )r/   r0   r1   r2   r3   c                  ^% Sn[        XX#XEXg5      n	U	u	  pp#pEpgm%X4n
[        R                  " X
U5      n	U	u  p
ppm%U
u  p[        T%R	                  T%R                  X<5      USS9T%S9n[        T%R	                  T%R                  XL5      USS9T%S9nX1:*  X:  -  X$:*  -  ) nT%R                  U
5      nT%R                  U5      nT%R                  X45      nUR                  S   S-  nT%R                  UUS USU 45      nT%R                  UUS USU 45      nUnT%R                  X445      n[        T%R                  X\5      T%S9nT%R	                  X]SS9nT%R                  XU45      nT%R                  SU-  5      nU Vs/ s H  nT%R                  UU45      PM     nnUS-   nT%R                  U5      nU) nT%R                  U5      nUU   UU   -
  UU'   UU   UU   -
  UU'   T%R                  U[        R                  T%R                  S9n[        R                  UU'   S	u  n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T%R                   _ST%R                   _ST%R                   _ST%R                   _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XXpXmU U!U"U#U$UT%5      $ s  snf )#al  Bracket the root of a monotonic scalar function of one variable

This function works elementwise when `xl0`, `xr0`, `xmin`, `xmax`, `factor`, and
the elements of `args` are broadcastable arrays.

Parameters
----------
func : callable
    The function for which the root is to be bracketed.
    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``.
xl0, xr0: float array_like
    Starting guess of bracket, which need not contain a root. If `xr0` is
    not provided, ``xr0 = xl0 + 1``. Must be broadcastable with one another.
xmin, xmax : float array_like, optional
    Minimum and maximum allowable endpoints of the bracket, inclusive. Must
    be broadcastable with `xl0` and `xr0`.
factor : float array_like, default: 2
    The factor used to grow the bracket. See notes for details.
args : tuple, optional
    Additional positional arguments to be passed to `func`.  Must be arrays
    broadcastable with `xl0`, `xr0`, `xmin`, and `xmax`. If the callable to be
    bracketed requires arguments that are not broadcastable with these
    arrays, wrap that callable with `func` such that `func` accepts
    only `x` and broadcastable arrays.
maxiter : int, optional
    The maximum number of iterations of the algorithm to perform.

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.

    xl, xr : float
        The lower and upper ends of the bracket, if the algorithm
        terminated successfully.
    fl, fr : float
        The function value at the lower and upper ends of the bracket.
    nfev : int
        The number of function evaluations required to find the bracket.
        This is distinct from the number of times `func` is *called*
        because the function may evaluated at multiple points in a single
        call.
    nit : int
        The number of iterations of the algorithm that were performed.
    status : int
        An integer representing the exit status of the algorithm.

        - ``0`` : The algorithm produced a valid bracket.
        - ``-1`` : The bracket expanded to the allowable limits without finding a bracket.
        - ``-2`` : The maximum number of iterations was reached.
        - ``-3`` : A non-finite value was encountered.
        - ``-4`` : Iteration was terminated by `callback`.
        - ``-5``: The initial bracket does not satisfy `xmin <= xl0 < xr0 < xmax`.
        - ``1`` : The algorithm is proceeding normally (in `callback` only).
        - ``2`` : A bracket was found in the opposite search direction (in `callback` only).

    success : bool
        ``True`` when the algorithm terminated successfully (status ``0``).

Notes
-----
This function generalizes an algorithm found in pieces throughout
`scipy.stats`. The strategy is to iteratively grow the bracket ``(l, r)``
 until ``func(l) < 0 < func(r)``. The bracket grows to the left as follows.

- If `xmin` is not provided, the distance between `xl0` and `l` is iteratively
  increased by `factor`.
- If `xmin` is provided, the distance between `xmin` and `l` is iteratively
  decreased by `factor`. Note that this also *increases* the bracket size.

Growth of the bracket to the right is analogous.

Growth of the bracket in one direction stops when the endpoint is no longer
finite, the function value at the endpoint is no longer finite, or the
endpoint reaches its limiting value (`xmin` or `xmax`). Iteration terminates
when the bracket stops growing in both directions, the bracket surrounds
the root, or a root is found (accidentally).

If two brackets are found - that is, a bracket is found on both sides in
the same iteration, the smaller of the two is returned.
If roots of the function are found, both `l` and `r` are set to the
leftmost root.

NFr   r4   r   r   )r   r   )r   r   xx0flimitr1   activedx_lastf_lastnitnfevstatusr2   xlxrflfrn)rG   rG   rH   rH   rI   rI   rE   rE   rF   rF   rJ   rJ   rK   rK   )r=   r=   )r?   r?   )rC   rC   )rD   rD   c                   > TR                  U R                  5      nTR                  U R                  5      nU R                  U==   U R
                  U   -  ss'   U R                  U   U R                  U   -   X'   U) nU R                  U==   U R
                  U   -  ss'   U R                  U   U R                  U   -
  X'   U$ N)
zeros_liker=   isinfr@   rB   r1   r>   )workr=   inir4   s       r8   pre_func_eval$_bracket_root.<locals>.pre_func_eval   s    MM$&&! HHTZZ q	T[[^#	wwqzDFF1I% Rr
dkk"o%


2+r:   c                 `    UR                   Ul        UR                  Ul        Xl         Xl        g rU   )r=   rC   r?   rD   r=   r?   rX   s      r8   post_func_eval%_bracket_root.<locals>.post_func_eval  s$     ffffr:   c                   > U R                   [        R                  :H  nTR                  U R                  5      nTR                  U R
                  5      nX2* :H  US:H  -  US:H  -  U) -  n[        R                  U R                   U'   SX'   U R                  U   U R                  -   SU R                  -  -  nTR                  U R                  U5      nX`R                  R                  S   :  nXg   XW   pVXeU R                  U   :H     nTR                  U5      nSXF'   XA) -  n[        U R                   U'   SX'   U R                  U R                  :H  U) -  n[        U R                   U'   SX'   TR!                  U R                  5      TR!                  U R                  5      -  ) U) -  n[        R"                  U R                   U'   SX'   U$ )Nr   Tr   )rG   eim
_EINPUTERRsignr?   rD   _ECONVERGEDrA   rL   searchsortedr)   rV   _ESTOPONESIDEr=   r@   _ELIMITSisfinite
_EVALUEERR)	rX   stopsfsf_lastrY   	also_stopjmaskr4   s	           r8   check_termination(_bracket_root.<locals>.check_termination  s   s~~- WWTVV_''$++&nA."':teCA" [[^dff,466:	 OODKK3
 ;;$$Q'' w	94;;q>)*MM$I&A VVtzz!dU*!A kk$&&!BKK$778D5@Ar:   c                     g rU   r   rX   s    r8   post_termination_check-_bracket_root.<locals>.post_termination_checkO      r:   c                   > U S   R                   S   S-  nU S   S U nU S   S U nU S   US  nU S   US  nU S   S U nU S   S U nU S   US  n	U S   US  n
TR                  USS9nTR                  USS9nTR                  USS9nTR                  U
SS9nU S	   S U nU S	   US  nXC-
  nXe-
  nUU:*  US:H  -  US:H  US:g  -  -  nUU:*  US:H  -  US:H  US:g  -  -  nUU   UU'   UU   UU'   UU   UU'   U	U   UU'   XS
'   XS'   XS'   XS'   TR                  U S   S U U S   US  5      U S'   U S   S U U S   US  -   U S'   TR                  US:H  UU5      U S	'   U S	   S:H  U S'   U S	 U S	 U S	 U S	 US S $ )Nr=   r   r   rC   r?   rD   Tr   rG   rH   rI   rJ   rK   rE   rF   successr   )r)   r!   maximumwhere)resr)   rL   xalxarxblxbrfalfarfblfbrrH   rJ   rI   rK   sasbdadbi1i2r4   s                        r8   customize_result'_bracket_root.<locals>.customize_resultR  sL   HNN1" #hrl(mBQ(mAB#hqrl#hrl(mBQ(mAB#hqrl ZZ$Z'ZZ$Z'ZZ$Z'ZZ$Z' ]2A]12YYRxB!G$"'bAg)>?RxB!G$"'bAg)>?R2R2R2R2 D	D	D	D	ZZE
2AE
12?E
&k"1oFAB7F q"b1Hh-1,IHHMMSbzr:   r   )r9   rb   _initializer   r(   broadcast_toconcatr)   arangerW   rV   	full_like_EINPROGRESSint32rc   r   r#   _loop)&r,   r-   r.   r/   r0   r1   r2   r3   callbacktempxsfsr)   r   invalid_bracketr=   r?   rL   rC   rD   r>   r@   rA   argrY   rZ   rB   rG   rE   rF   rX   res_work_pairsr[   r_   rq   ru   r   r4   s&                                        @r8   _bracket_rootr   K   sa   J HDs$ND<@9Ds$r
B??4T*D+/(DbbHCBIIbood:EINSUVDBIIbood:EINSUVD3s{CDO 			"A
		"Aii BCO	
aA
 YY!"q!u~&FYY!"q!u~&F	B IItl#Eboof4<FYYv5Y1FYY'(FYYqs^F-12TcBIIsCj!TD2 DLE 	A
B
aAQ4"Q%<AaD"I"AbE\\!S--RXX\>F!nnF?IC H Hr HQ He HF H$H()H28HAGHH%)H28H?CH &&H &(VVH 13H <>66H FGHDBN
$=~JX 99T8G4"N4E+-=~ O 3s   =Kc	           
         [        U 5      (       d  [        S5      e[        R                  " U5      (       d  U4n[	        U5      n	U	R                  U5      S   nU	R                  UR                  S5      (       a!  U	R                  UR                  S5      (       a  [        S5      eU	R                  UR                  S5      (       d  U	R                  U[        U	5      S9nUc  U	R                  * OUnUc  U	R                  OUnSn
Uc  U	R                  nS	n
SnUc  U	R                  nS	nUc  S
OUnU	R                  U	R                  U5      XR                  U5      U	R                  U5      U	R                  U5      U	R                  U5      5      u  p!p4pVU	R                  UR                  S5      (       a!  U	R                  UR                  S5      (       a  [        S5      eU	R                  UR                  S5      (       a!  U	R                  UR                  S5      (       a  [        S5      eU	R                  UR                  S5      (       a!  U	R                  UR                  S5      (       a  [        S5      eU	R                  UR                  S5      (       a!  U	R                  UR                  S5      (       a  [        S5      eU	R                  UR                  S5      (       a!  U	R                  UR                  S5      (       a  [        S5      eU	R                  US:  5      (       d  [        S5      eU
(       aB  XR                  X-
  S-  U	R                  S5      5      -
  nU	R                  X!R                  SS9nU(       aB  XR                  XQ-
  S-  U	R                  S5      5      -   nU	R                  X1R                  SS9nU	R                  U5      nSnU	R                  UR                  S5      (       a9  UR                  [!        5       :w  d!  U	R                  UR                  S5      (       a  [        U5      e[#        US   5      nX:X  a  US:  a  [        U5      eXX#XEXgX4
$ )Nr
   r   r   r   z`xm0` must be numeric and real.r   r   FTr   r   r   r   r   r   r   r      g      ?r   r   r   )r   r   r   r    r   r!   r"   r   r   r$   r#   r%   r&   r'   r(   r)   r*   r+   )r,   xm0r-   r.   r/   r0   r1   r2   r3   r4   xl0_not_suppliedr5   r6   r7   s                 r8   _bracket_minimum_ivr     s   D>>344;;tw		B
**S/"
CJJsyy),,::cii!344:;;::cii11jj$4R$8j9lBFF7D\266tD
 
{ff
{ffNSF(*(;(;


3jjorzz$/?


4"**V,)%Cc
 JJsyy),,::cii!344:;;JJsyy),,::cii!344:;;JJtzz9--::djj"455;<<JJtzz9--::djj"455;<<JJv||Y//::fll$677=>>66&1*KLL
 JJ
B

3@@iiYYUi3JJ
B

3@@iiYYUi3jj!G9GJJw}}i00GMMUW4Lzz'--);<<!!gbk"K!Wq[!!cF'EEr:   )r-   r.   r/   r0   r1   r2   r3   c                  ^" Sn	[        XX#XEXgU5	      n
U
u
  pp#pEpgnm"X!U4n[        R                  " XU5      n
U
u  pppm"Uu  p!nUu  nnnT"R                  T"R	                  XM5      USS9n[        UT"S9nT"R                  T"R	                  X]5      USS9n[        UT"S9nXB:*  X!:  -  X:  -  X5:*  -  ) nT"R                  T"R	                  Xm5      USS9n[        U5      nUU:  nUU   UU   sUU'   UU'   UU   UU   sUU'   UU'   T"R                  UXE5      nT"R                  U5      nU) nT"R                  U5      nUU   UU   -
  UU'   UU   UU   -
  UU'   SUU   -  UU'   T"R                  U[        R                  T"R                  S9n[        R                  UU'   Su  nn[        X!X3UUUUUUUUUUUS	9n/ S
QnU"4S jnS nU"4S jnS n S n![        R                  " UXXX~UUUU U!UT"5      $ )a  Bracket the minimum of a unimodal scalar function of one variable

This function works elementwise when `xm0`, `xl0`, `xr0`, `xmin`, `xmax`,
and the elements of `args` are broadcastable arrays.

Parameters
----------
func : callable
    The function for which the minimum is to be bracketed.
    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`.
xm0: float array_like
    Starting guess for middle point of bracket.
xl0, xr0: float array_like, optional
    Starting guesses for left and right endpoints of the bracket. Must be
    broadcastable with one another and with `xm0`.
xmin, xmax : float array_like, optional
    Minimum and maximum allowable endpoints of the bracket, inclusive. Must
    be broadcastable with `xl0`, `xm0`, and `xr0`.
factor : float array_like, optional
    Controls expansion of bracket endpoint in downhill direction. Works
    differently in the cases where a limit is set in the downhill direction
    with `xmax` or `xmin`. See Notes.
args : tuple, optional
    Additional positional arguments to be passed to `func`.  Must be arrays
    broadcastable with `xl0`, `xm0`, `xr0`, `xmin`, and `xmax`. If the
    callable to be bracketed requires arguments that are not broadcastable
    with these arrays, wrap that callable with `func` such that `func`
    accepts only ``x`` and broadcastable arrays.
maxiter : int, optional
    The maximum number of iterations of the algorithm to perform. The number
    of function evaluations is three greater than the number of iterations.

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.

    xl, xm, xr : float
        The left, middle, and right points of the bracket, if the algorithm
        terminated successfully.
    fl, fm, fr : float
        The function value at the left, middle, and right points of the bracket.
    nfev : int
        The number of function evaluations required to find the bracket.
    nit : int
        The number of iterations of the algorithm that were performed.
    status : int
        An integer representing the exit status of the algorithm.

        - ``0`` : The algorithm produced a valid bracket.
        - ``-1`` : The bracket expanded to the allowable limits. Assuming
                   unimodality, this implies the endpoint at the limit is a
                   minimizer.
        - ``-2`` : The maximum number of iterations was reached.
        - ``-3`` : A non-finite value was encountered.
        - ``-4`` : ``None`` shall pass.
        - ``-5`` : The initial bracket does not satisfy
                   `xmin <= xl0 < xm0 < xr0 <= xmax`.

    success : bool
        ``True`` when the algorithm terminated successfully (status ``0``).

Notes
-----
Similar to `scipy.optimize.bracket`, this function seeks to find real
points ``xl < xm < xr`` such that ``f(xl) >= f(xm)`` and ``f(xr) >= f(xm)``,
where at least one of the inequalities is strict. Unlike `scipy.optimize.bracket`,
this function can operate in a vectorized manner on array input, so long as
the input arrays are broadcastable with each other. Also unlike
`scipy.optimize.bracket`, users may specify minimum and maximum endpoints
for the desired bracket.

Given an initial trio of points ``xl = xl0``, ``xm = xm0``, ``xr = xr0``,
the algorithm checks if these points already give a valid bracket. If not,
a new endpoint, ``w`` is chosen in the "downhill" direction, ``xm`` becomes the new
opposite endpoint, and either `xl` or `xr` becomes the new middle point,
depending on which direction is downhill. The algorithm repeats from here.

The new endpoint `w` is chosen differently depending on whether or not a
boundary `xmin` or `xmax` has been set in the downhill direction. Without
loss of generality, suppose the downhill direction is to the right, so that
``f(xl) > f(xm) > f(xr)``. If there is no boundary to the right, then `w`
is chosen to be ``xr + factor * (xr - xm)`` where `factor` is controlled by
the user (defaults to 2.0) so that step sizes increase in geometric proportion.
If there is a boundary, `xmax` in this case, then `w` is chosen to be
``xmax - (xmax - xr)/factor``, with steps slowing to a stop at
`xmax`. This cautious approach ensures that a minimum near but distinct from
the boundary isn't missed while also detecting whether or not the `xmax` is
a minimizer when `xmax` is reached after a finite number of steps.
NFr   r<   Tr   r   )r      )rH   xmrI   r.   rJ   fmrK   stepr@   limitedr1   rE   rF   rG   r2   )	rM   rN   )r   r   rO   rP   rQ   rR   )r   r   rS   c                 N  > U =R                   U R                  -  sl         TR                  U R                  5      nU R                  U R
                  )    U R                   U R
                  )    -   XR
                  ) '   U R                  U R
                     U R                   U R
                     -
  XR
                  '   TR                  XR
                     U R                  U R
                     :H  U R                  U R
                     XR
                     5      XR
                  '   U$ rU   )r   r1   
empty_likerI   r.   r   r@   r{   )rX   r=   r4   s     r8   r[   '_bracket_minimum.<locals>.pre_func_eval  s    		T[[ 	MM$''"88T\\M2TYY}5MM<<-**T\\2TYYt||5LL,,
 ((llOtwwt||44JJt||$llO
,,
 r:   c                     UR                   UR                  U sUl        Ul         Ul        UR                  UR                  UsUl        Ul        Ul        g rU   )r   rI   rH   r   rK   rJ   r^   s      r8   r_   (_bracket_minimum.<locals>.post_func_eval  s>    $(GGTWWa!$'$(GGTWWa!$'r:   c                 |  > U R                   [        R                  :H  nU R                  U R                  :  U R
                  U R                  :  -  U R                  U R                  :  U R
                  U R                  :  -  -  U) -  n[        R                  U R                   U'   SX'   U R                  U R                  :H  U) -  n[        U R                   U'   SX'   TR                  U R                  5      TR                  U R
                  5      -  ) U) -  n[        R                  U R                   U'   SX'   U$ )NT)rG   rb   rc   rJ   r   rK   re   rI   r@   rh   ri   rj   )rX   rk   rY   r4   s      r8   rq   +_bracket_minimum.<locals>.check_termination  s   s~~- WWDGGdgg$56ww TWW%789E A WW

"te+!A kk$''"R[[%99:dUBAr:   c                     g rU   r   rt   s    r8   ru   0_bracket_minimum.<locals>.post_termination_check  rw   r:   c                     U S   U S   :  nU S   U   U S   U   sU S   U'   U S   U'   U S   U   U S   U   sU S   U'   U S   U'   U$ )NrH   rI   rK   rJ   r   )r|   r)   comps      r8   r   *_bracket_minimum.<locals>.customize_result  sq    4y3t9$+.t9T?CIdO(D	$T4+.t9T?CIdO(D	$T4r:   )r   rb   r   r(   r   r   r{   rW   r   r   r   r   rc   r   r   )#r,   r   r-   r.   r/   r0   r1   r2   r3   r   r   r   r   r)   r   fl0fm0fr0r   r   r@   	unlimitedr   r   rG   rE   rF   rX   r   r[   r_   rq   ru   r   r4   s#                                     @r8   _bracket_minimumr     sc   L Ht#DgVDAE>DsV7B
CB??4T*D+/(DbbMCcMCc99R__T15u9EDDR D99R__T15u9EDDR D3syAS[QRO YYrv5u4YHFfF 9Dt9c$iCIs4yt9c$iCIs4yHHT4&EIjG==D9~I6DO7^c'l2DM &/)F7O\\#s//rxx\@F!nnF?IC##333 wvSV d<D$N 80 99T8D"N&(>%~r	; ;r:   rU   )numpyr   (scipy._lib._elementwise_iterative_method_lib_elementwise_iterative_methodrb   scipy._lib._utilr   scipy._lib._array_apir   r   r   rh   rg   r9   r   r   r   r   r:   r8   <module>r      s`     6 6 ( M M?ADVt$t4Vr
OFd (,Dt r4S;r:   