
    -i:                         S SK r S SKrSSKJrJr  SSKJrJrJ	r	  S SK
Js  Jr  SrSrSrSrS	rS rSrSS
 jr SS jrS rS rS rg)    N   )_RichResult_call_callback_maybe_halt)array_namespacexp_sizexp_result_typec           
      F  ^ [        U5      nUc  [        U6 OUn[        USUS.6nUR                  " / UQUQ76 nUSU XS p!U V	s/ s H  oR	                  XS9PM     nn	U V	s/ s H  oR	                  U " U	/UQ76 5      PM     n
n	US   R
                  nU
S   R
                  mU(       aa  XS.U4S jjn [        R                  " TU5      nU V	s/ s H  oR                  X5      PM     nn	U Vs/ s H  oR                  X5      PM     nnSnUb  S	UR                  5        3nU
 Vs/ s H  oR
                  U:H  PM     nn[        U5      (       d  [        U5      eUR                  " U
 Vs/ s H  oR                  PM     snU/-   6 nU(       d"  UR                  US
5      (       d  [        S5      eU V	s/ s H  oR	                  U	USS9PM     nn	U
 Vs/ s H  oR	                  UUSS9PM     n
nU V	s/ s H  oR                  U	S5      PM     nn	U
 Vs/ s H  oR                  US5      PM     n
nU Vs/ s H"  oR                  UR	                  USS9S5      PM$     nnXXUUU4$ s  sn	f s  sn	f s  sn	f s  snf s  snf s  snf s  sn	f s  snf s  sn	f s  snf s  snf )av  Initialize abscissa, function, and args arrays for elementwise function

Parameters
----------
func : callable
    An elementwise function with signature

        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``.
xs : tuple of arrays
    Finite real abscissa arrays. Must be broadcastable.
args : tuple, optional
    Additional positional arguments to be passed to `func`.
preserve_shape : bool, default:False
    When ``preserve_shape=False`` (default), `func` may be passed
    arguments of any shape; `_scalar_optimization_loop` is permitted
    to reshape and compress arguments at will. When
    ``preserve_shape=False``, arguments passed to `func` must have shape
    `shape` or ``shape + (n,)``, where ``n`` is any integer.
xp : namespace
    Namespace of array arguments in `xs`.

Returns
-------
xs, fs, args : tuple of arrays
    Broadcasted, writeable, 1D abscissa and function value arrays (or
    NumPy floats, if appropriate). The dtypes of the `xs` and `fs` are
    `xfat`; the dtype of the `args` are unchanged.
shape : tuple of ints
    Original shape of broadcasted arrays.
xfat : NumPy dtype
    Result dtype of abscissae, function values, and args determined using
    `np.result_type`, except integer types are promoted to `np.float64`.

Raises
------
ValueError
    If the result dtype is not that of a real scalar

Notes
-----
Useful for initializing the input of SciPy functions that accept
an elementwise callable, abscissae, and arguments; e.g.
`scipy.optimize._chandrupatla`.
NT)force_floatingxpdtyper   )shapefuncc                T   > S[        T5      [        U5      -
  -  nU" X   /UQ70 UD6$ )N)r   )len)xr   r   argskwargsifshapes         [/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/_elementwise_iterative_method.pyr   _initialize.<locals>.func`   s1    c&kCJ./A.t.v..    zpThe shape of the array returned by `func` must be the same as the broadcasted shape of `x` and all other `args`.zWhen `preserve_shape=False`, zreal floatingz3Abscissae and function output must be real numbers.)r   copyr	   )r   )r   r   r   broadcast_arraysasarrayr   npbroadcast_shapesbroadcast_tolowerall
ValueErrorresult_typer   isdtypereshape)r   xsr   
complex_okpreserve_shaper   nxxatxasr   fsr   argmessagefshapes_equalxfatr   s                    @r   _initializer8      s   b 
RB!#"	B "Tb
9C



)r
)D
)C3BxS,.	/Bq**Q*
"BB	/.0	1b**T!^d^
$bB	1qEKKEU[[F!& 	/ 	/ ##FE2134Aooa'47;<t+t<DG!1'--/1BC.01bGGu$bL1|!!
 >>b1bWWb1SE9;Dbjj??NOO8:	;1**Qd*
.B	;8:	;1**Qd*
.B	; )+	+1**Q
B	+(*	+1**Q
B	+EIJTcJJrzz#Dz159TDJRudB..I 
0	1 5< 2 2 
<	; 
,	+JsB   I,$!I1I61I;)J .J8J
J8JJ:)Jc           
      `   Uc  [        S5      eSn[        R                  " U5      nUR                  U5      nU VVs0 s H  u  nnUUR	                  UUS9_M     nnnUR	                  UUR
                  S9US'   UR                  UUR                  [        5      UR                  S9US'   UR	                  UUR                  S9US'   UR	                  UUR                  S9US'   [        U5      nXPl        [        U UUUXU5      nUb$  [        U UUUUXU5      n[        UU5      (       a  S	nU R                  U:  Ga  [!        U5      (       Ga  U(       Gd  U(       Ga  U" U 5      nU R                  (       a  U R                  S
   R"                  UR"                  :w  ag  / nU R                   HO  nUR"                  UR"                  -
  nUR$                  SU-  -   nUR'                  UR)                  UU5      5        MQ     XPl        UR$                  nU(       a  UR)                  UUS-   5      nU" U/U R                  Q76 nUR                  UUS9nU(       a$  UR)                  UU5      nUR)                  UU5      nU =R*                  UR"                  S:X  a  SOUR$                  S   -  sl        U" UUU 5        U =R                  S-  sl        [        U UUUXU5      nUb%  [        U UUUUXU5      n[        UU5      (       a  S	nOI[!        U5      S
:X  a  O9U
" U 5        U R                  U:  a!  [!        U5      (       a  U(       d
  U(       a  GM  [,        R.                  " U R0                  5      SS R3                  U(       a  [4        O[6        5      U l        [        U UUUUXU5      $ s  snnf )a  Main loop of a vectorized scalar optimization algorithm

Parameters
----------
work : _RichResult
    All variables that need to be retained between iterations. Must
    contain attributes `nit`, `nfev`, and `success`. All arrays are
    subject to being "compressed" if `preserve_shape is False`; nest
    arrays that should not be compressed inside another object (e.g.
    `dict` or `_RichResult`).
callback : callable
    User-specified callback function
shape : tuple of ints
    The shape of all output arrays
maxiter :
    Maximum number of iterations of the algorithm
func : callable
    The user-specified callable that is being optimized or solved
args : tuple
    Additional positional arguments to be passed to `func`.
dtype : NumPy dtype
    The common dtype of all abscissae and function values
pre_func_eval : callable
    A function that accepts `work` and returns `x`, the active elements
    of `x` at which `func` will be evaluated. May modify attributes
    of `work` with any algorithmic steps that need to happen
     at the beginning of an iteration, before `func` is evaluated,
post_func_eval : callable
    A function that accepts `x`, `func(x)`, and `work`. May modify
    attributes of `work` with any algorithmic steps that need to happen
     in the middle of an iteration, after `func` is evaluated but before
     the termination check.
check_termination : callable
    A function that accepts `work` and returns `stop`, a boolean array
    indicating which of the active elements have met a termination
    condition.
post_termination_check : callable
    A function that accepts `work`. May modify `work` with any algorithmic
    steps that need to happen after the termination check and before the
    end of the iteration.
customize_result : callable
    A function that accepts `res` and `shape` and returns `shape`. May
    modify `res` (in-place) according to preferences (e.g. rearrange
    elements between attributes) and modify `shape` if needed.
res_work_pairs : list of (str, str)
    Identifies correspondence between attributes of `res` and attributes
    of `work`; i.e., attributes of active elements of `work` will be
    copied to the appropriate indices of `res` when appropriate. The order
    determines the order in which _RichResult attributes will be
    pretty-printed.
preserve_shape : bool, default: False
    Whether to compress the attributes of `work` (to avoid unnecessary
    computation on elements that have already converged).

Returns
-------
res : _RichResult
    The final result object

Notes
-----
Besides providing structure, this framework provides several important
services for a vectorized optimization algorithm.

- It handles common tasks involving iteration count, function evaluation
  count, a user-specified callback, and associated termination conditions.
- It compresses the attributes of `work` to eliminate unnecessary
  computation on elements that have already converged.

NzMust provide xp.Fr   successstatusnitnfevTr   )r   r    r   r	   )NotImplementedErrormathprodarangezerosboolfullr"   _EINPROGRESSint32r   r   _check_termination_prepare_resultr   r<   r   ndimr   appendr+   r=   xpxatr;   set
_ECALLBACK	_ECONVERR)workcallbackr   maxiterr   r   r   pre_func_evalpost_func_evalcheck_terminationpost_termination_checkcustomize_resultres_work_pairsr   r.   cb_terminate
n_elementsactiver   jres_dictrestempr   r3   
n_new_dims	new_shapex_shaper5   s                                r   _looprc      sC   R 
z!"455L 5!JYYz"FAOPA288Je844HP((:RWW(=HYRZZ-ERXXVHXhhzh:HUOxx
"((x;HV
h
CIc>6 12GF tS.&%/E$Xt44L
((W
*$991**aff4 DyyVVchh.
IIZ7	BJJsI67 ! I''

1uu}.ATYYJJqJ&

1g&A

1g&A		!&&A+Q1772;6	q!T"A#D#~v$5rK "4nfe#3RID(488#6?at$O ((W
**R &&%a(,,<ZYWDK4nfe+RA Aw Qs   N*c           	      V   U" U 5      nUR                  U5      (       a  [        XX#XuU5        U(       a  Xs   nU) nX8   nU(       d^  U R                  5        H&  u  pU	S;   a  M  [        U
SS5      S:  a  X   OU
X	'   M(     U R                   Vs/ s H  oU   PM	     snU l        U$ s  snf )N>   nr   rI   r   )any_update_activeitemsgetattrr   )rP   r^   rX   r[   rU   r.   r   stopproceedkeyvalr3   s               r   rG   rG     s    
 T"D	vvd|| 	t.$PRS<D% JJL-',3C,Ca,GCLS		 )
 26;#W;DIM <s   B&c                 4   U VVs0 s H
  u  pxXpU   _M     n	nnU R                   S:H  U	S'   UGb  U(       a  UR                  U5      n
[        R                  " U
5      U   R	                  S5      n
X-  n
U	R                  5        HH  u  p[        USS5      S:  a  X   OUn[        R                  " X   5      U
   R	                  U5      X'   MJ     g X4   n
U	R                  5        HH  u  p[        USS5      S:  a  X   OUn[        R                  " X   5      U
   R	                  U5      X'   MJ     g U	R                  5        HM  u  pU(       a  [        USS5      S:  a  X   n[        R                  " X   5      U   R	                  U5      X'   MO     g s  snnf )Nr   r:   TrI   )r;   
zeros_likerK   rL   rM   rh   ri   )rP   r^   rX   r[   maskr.   r   key1key2update_dictactive_maskrl   rm   s                r   rg   rg   .  su   
 7EEn
4d#nKE![[A-K	---K&&-f599$?K%,K'--/*1#vq*AA*Ec&366#(+K8<<SA 0 !,K'--/#*3#:Q#>ciC66#(+K8<<SA 0 $))+HC'#vq"9A"=kvvch'/33C8CH ,# Fs   Fc           	      2   UR                  5       n[        XX#S Xg5        U" X5      nUR                  5        H/  u  pUR                  X5      n
U
R                  S:X  a  U
S   OU
X'   M1     S/U VVs/ s H  u  pUPM	     snn-   US'   [        S0 UD6$ s  snnf )Nr    r:   _order_keys)r   rg   rh   r+   rI   r   )rP   r^   rX   r[   r   rW   r.   r   rl   rm   r_   r   r\   s                r   rH   rH   J  s    
 ((*C4ndNOS(EIIKzz#%#yyA~484  
 $n'Endan'EEC (Fs   1B)FNN)F)r?   numpyr#   _utilr   r   
_array_apir   r   r   scipy._lib.array_api_extra_libarray_api_extrarK   	_ESIGNERRrO   
_EVALUEERRrN   
_EINPUTERR_ECONVERGEDrE   r8   rc   rG   rg   rH   rv   r   r   <module>r      sd      9 @ @ ( (		


`/J @EMA`<98r   