L ixddlZddlmcmZddlmZddlm Z m Z m Z dZ dZ dZddddddd d Zd Zdddddddd d Zy)N) _RichResult)array_namespacexp_ravel xp_promotec rt|s tdtj|s|f}t |||||g|}d} ||j }d} | |j n|}| |j n|}|dn|}t|||||dd|\}}}}}|j|jds td|j|jdr|j|jd r td |j|jdr|j|jd r td |j|jdr|j|jd r td |j|jdr|j|jd r td |j|dkDs td| r9||j||z dz dz}|j||jd}|j|}d} |j|jdr3|jtk7s|j|jd r t| t!|d} || k(r|dkr t| |||||||||f S)N`func` must be callable.FT@ broadcastforce_floatingxpintegralz real floating`xl0` must be numeric and real.numericcomplex floating`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 ValueErrornpiterablernaninfrisdtypedtypeallminimumastypeasarrayshapetupleint) funcxl0xr0xminxmaxfactorargsmaxiterrxr0_not_suppliedmessage maxiter_ints ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_bracket.py_bracket_root_ivr; sx D>344 ;;t w c4v = =B  {fflBFF7D\266tD>RvF#- S$fTb$R CdD& ::cii!> ?:;; JJsyy) , ::cii!3 4:;; JJtzz9 - ::djj"4 5;<< JJtzz9 - ::djj"4 5;<< JJv||Y / ::fll$6 7=>> 66&1* KLLBJJs Q44iiSYYUi3jj!G9G JJw}}i 0GMMUW4Lzz'--);<!!gbk"K k !Wq[!! c4vtWb @@ri)r2r3r4r5r6c%d}t||||||||} | \ }}}}}}}}%||f} tj|| |} | \}} } }} } %| \}}t%j %j || | d%}t%j %j || | d%}||k||kz||kz}%j | }%j | }%j ||f}|jddz}%j ||d|d|f}%j ||d|d|f}|}%j ||f}t%j || %}%j || d}%j ||f}%jd|z}|Dcgc]}%j ||f}}| dz} %j|}|}%j|}||||z ||<||||z ||<%j|tj%j}tj||<d \}}td"id |d |d |d |d|d|d|d|d|d|d|d|d|d%j d%j d%j d%j d|}gd}%fd} d}!%fd}"d }#%fd!}$tj"||| |||| | |!|"|#|$|%Scc}w)#aBracket 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. NFrrrr)rr')rrxx0flimitr4activedx_lastf_lastnitnfevstatusr5xlxrflfrn) rJrJrKrKrLrLrHrHrIrIrMrMrNrN)r@r@)rBrB)rFrF)rGrGcj|j}j|j}|j|xx|j |zcc<|j ||j|z||<|}|j|xx|j |zcc<|j||j|z ||<|SN) zeros_liker@isinfrCrEr4rA)workr@inirs r: pre_func_evalz$_bracket_root..pre_func_evals MM$&& ! HHTZZ  q T[[^# wwqzDFF1I%!R r dkk"o%  2+"r<cd|j|_|j|_||_||_yrX)r@rFrBrGr@rBr[s r:post_func_evalz%_bracket_root..post_func_evals(ff ff r<c|jtjk(}j|j}j|j }|| k(|dk(z|dk(z|z}tj |j|<d||<|j||jzd|jzz}j|j|}||jjdk}||||}}|||j|k(}j|}d||<||z}t|j|<d||<|j|jk(|z}t|j|<d||<j!|jj!|jz|z}tj"|j|<d||<|S)NrTr)rJeim _EINPUTERRsignrBrG _ECONVERGEDrDrO searchsortedr,rY _ESTOPONESIDEr@rC_ELIMITSisfinite _EVALUEERR) r[stopsfsf_lastr\ also_stopjmaskrs r:check_terminationz(_bracket_root..check_termination s s~~-WWTVV_''$++&"nA ."' :te C AQ"[[^dff,466:  OODKK 3 4;;$$Q''w $9 i4;;q>) * MM$ ! I& AQVVtzz !dU *! AQkk$&&!BKK$77 8D5 @ AQ r<cyrXrr[s r:post_termination_checkz-_bracket_root..post_termination_checkL r<c|djddz}|dd|}|dd|}|d|d}|d|d}|dd|}|dd|}|d|d} |d|d} j|d} j|d} j|d} j| d}|d d|}|d |d}||z }||z }||k|dk(z|dk(|dk7zz}||k|dk(z|dk(|dk7zz}||| |<||||<||| |<| || |<| |d <| |d <| |d <||d <j|dd||d|d|d<|dd||d|dz|d<j|dk(|||d <|d dk(|d<|d=|d=|d=|d=|ddS)Nr@rrrFrBrGTrrJrKrLrMrNrHrIsuccessr)r,r+maximumwhere)resr,rOxalxarxblxbrfalfarfblfbrrKrMrLrNsasbdadbi1i2rs r:customize_resultz'_bracket_root..customize_resultOsX HNN1  "#hrl(mBQ(mAB#hqrl#hrl(mBQ(mAB#hqrlZZ$Z ' ZZ$Z ' ZZ$Z ' ZZ$Z ']2A  ]12  3Y 3YRxB!G$"'bAg)> ?RxB!G$"'bAg)> ?R2R2R2R2D D D D ZZE 2AE 12?E &k"1oF AB7F q"b1H h-1,I H H M MSbzr<r)r;rc _initializerr* broadcast_toconcatr,arangerZrY full_like _EINPROGRESSint32rdrr$_loop)&r/r0r1r2r3r4r5r6callbacktempxsfsr,r'invalid_bracketr@rBrOrFrGrArCrDargr\r]rErJrHrIr[res_work_pairsr^rarrrurrs& @r: _bracket_rootrHsJH D#sD$g ND<@9D#sD$gr sB ??4T *D+/(D"b$ubHC BIIboodE:EINSU VD BIIboodE:EINSU VDs3sd{CDO " A " Aii/ BCO  aA YY!"q!u~ &F YY!"q!u~ &F B IItTl #E boofe4 F!nnF?IC  H Hr HQ He HF H$ H() H28 HAG H H%) H28 H?C H&& H&(VV H13 H<>66 HFG HDBN $=~ JX 99T8UGT4"N4E+-=~ O 3s&K.c t|s tdtj|s|f}t ||||||g|} | | j n|}| | j n|}d} || j }d} d} || j }d} |dn|}t||||||dd|  \}}}}}}| j|jds td| j|jdr| j|jd r td | j|jdr| j|jd r td | j|jdr| j|jd r td | j|jdr| j|jd r td | j|jdr| j|jd r td| j|dkDs td| r9|| j||z dz dz }| j||jd}| r9|| j||z dz dz}| j||jd}| j|}d} | j|jdr3|jtk7s| j|jd r t| t!|d} || k(r|dkr t| |||||||||| f S)Nr FTr r rz`xm0` must be numeric and real.rrrrrrrrrg?rrrr)r r!r"r#rr%r$rr&r'r(r)r*r+r,r-r.)r/xm0r0r1r2r3r4r5r6rxl0_not_suppliedr7r8r9s r:_bracket_minimum_ivrs D>344 ;;t w c3dF BT BBlBFF7D\266tD  {ff {ffNSF(2 S#tT6T$SU)W%Cc4v ::cii!> ?:;; JJsyy) , ::cii!3 4:;; JJsyy) , ::cii!3 4:;; JJtzz9 - ::djj"4 5;<< JJtzz9 - ::djj"4 5;<< JJv||Y / ::fll$6 7=>> 66&1* KLL BJJd B44iiSYYUi3BJJs B44iiSYYUi3jj!G9G JJw}}i 0GMMUW4Lzz'--);<!!gbk"K k !Wq[!! c3dFD'2 EEr<)r0r1r2r3r4r5r6c8"d} t||||||||| } | \ }}}}}}}}}"|||f} tj|| |} | \}} } }} }"| \}}}| \}}}"j"j || |d}t |"}"j"j || |d}t |"}||k||kz||kz||kz}"j"j || |d}t |}||k}||||c||<||<||||c||<||<"j |||}"j|}|}"j|}||||z ||<||||z ||<d||z ||<"j|tj"j}tj||<d\}}t||||||||||||||| }gd }"fd }d }"fd }d} d}!tj|| | |||||||| |!|"S)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. NFrr>Trr?)r)rKxmrLr1rMfmrNsteprClimitedr4rHrIrJr5) rPrQ)rrrRrSrTrU)rrrVcX|xj|jzc_j|j}|j|j |j|j z||j <|j |j |j|j z ||j <j||j |j|j k(|j |j ||j ||j <|SrX)rr4 empty_likerLr1rrCrz)r[r@rs r:r^z'_bracket_minimum..pre_func_evals T[[ MM$'' "88T\\M2TYY }5MM4<<-**T\\2TYYt||5LL$,, (( dllOtwwt||4 4 JJt|| $ dllO $,, r<c|j|j|c|_|_|_|j|j|c|_|_|_yrX)rrLrKrrNrMr`s r:raz(_bracket_minimum..post_func_evals>$(GGTWWa!$'$(GGTWWa!$'r<c|jtjk(}|j|jk\|j |jkDz|j|jkD|j |jk\zz|z}tj |j|<d||<|j|jk(|z}t|j|<d||<j|jj|j z|z}tj|j|<d||<|S)NT) rJrcrdrMrrNrfrLrCrirjrk)r[rlr\rs r:rrz+_bracket_minimum..check_terminations s~~-WW DGGdgg$5 6ww TWW%78 9 E  AQWW "te +! AQkk$''"R[[%99 :dU B AQ r<cyrXrrts r:ruz0_bracket_minimum..post_termination_checkrvr<c|d|dkD}|d||d|c|d|<|d|<|d||d|c|d|<|d|<|S)NrKrLrNrMr)r{r,comps r:rz*_bracket_minimum..customize_resultsq4y3t9$+.t9T?CIdO(D $T4+.t9T?CIdO(D $T4 r<)rrcrr*rrrzrZrrrrrdrr)#r/rr0r1r2r3r4r5r6rrrrr,r'fl0fm0fr0rrrC unlimitedrrrJrHrIr[rr^rarrrurrs# @r:_bracket_minimumrsLH tS#sD$g VDAE>D#sCtVT7B sCB ??4T *D+/(D"b$ubMCcMCc 99R__T515u9 ED DR D 99R__T515u9 ED DR Ds3sSyASD[QROYYrvu5u4Y HF f F 9Dt9c$iCIs4yt9c$iCIs4y HHT4 &EIjG == D9~I6DO7^c'l2DM&/)F7O \\#s//rxx\ @F!nnF?IC ###3333 wvSV d %~r  ;;r<rX)numpyr"(scipy._lib._elementwise_iterative_method_lib_elementwise_iterative_methodrcscipy._lib._utilrscipy._lib._array_apirrrrirhr;rrrrr<r:rs`66(GG  <A~Vt$t4Vr KF\(,Dt r4S;r<