Ë
    0Ò^j~<  ã                   óŽ   — d dl Z d dlZddlmZmZ ddlmZmZm	Z	 d dl
mc mZ dZdZdZdZd	Zd ZdZ	 	 dd
„Z	 dd„Zd„ Zd„ Zd„ Zy)é    Né   )Ú_RichResultÚ_call_callback_maybe_halt)Úarray_namespaceÚxp_sizeÚxp_result_typeéÿÿÿÿéþÿÿÿéýÿÿÿéüÿÿÿéûÿÿÿc           	      ó~  ‡— t        |«      }|€t        |Ž n|}|�5g |¢|j                  «       ¢­}t        |j	                  «       «      }|| dœd„} t        |d|dœŽ}	 |j                  g |¢|¢­Ž }
|
d| |
|d }}|D �cg c]  }|j                  ||	¬«      ‘Œ }}|D �cg c]  }|j                   | |g|¢­Ž «      ‘Œ }}|d   j                  }|d   j                  Š|r]|| dœˆfd	„
} t        j                  ‰|«      }|D �cg c]  }|j                  ||«      ‘Œ }}|D �cg c]  }|j                  ||«      ‘Œ }}d
}|�d|j                  «       › �}|D �cg c]  }|j                  |k(  ‘Œ }}t        |«      st        |«      ‚ |j                  |D �cg c]  }|j                   ‘Œ c}|	gz   Ž }|s|j#                  |d«      st        d«      ‚|D �cg c]  }|j                  ||d¬«      ‘Œ }}|D �cg c]  }|j                  ||d¬«      ‘Œ }}|D �cg c]  }|j%                  |d«      ‘Œ }}|D �cg c]  }|j%                  |d«      ‘Œ }}|D �cg c]%  }|j%                  |j                  |d¬«      d«      ‘Œ' }}| ||||||fS c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w )at  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`.
    kwargs : tuple, optional
        Additional keyword 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`.
    N)ÚkwnamesÚfuncc                ó‚   — t        |«      t        |«      z
  }t        t        |||d  «      «      } || g|d | ¢­i |¤|¤ŽS )N)ÚlenÚdictÚzip)Úxr   r   ÚargsÚkwargsÚnargsÚkwarrayss          ús/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/scipy/_lib/_elementwise_iterative_method.pyr   z_initialize.<locals>.funcW   sK   € Ü˜“I¤ G£Ñ,ˆEÜœC ¨¨e¨f¨Ó6Ó7ˆHÙ˜Ð?˜D  %˜LÒ?¨HÐ?¸Ñ?Ð?ó    T)Úforce_floatingÚxp©Údtyper   )Úshaper   c                óV   •— dt        ‰«      t        |«      z
  z  } || |   g|¢­i |¤ŽS )N)r   )r   )r   r    r   r   r   ÚiÚfshapes         €r   r   z_initialize.<locals>.funck   s3   ø€ Ø”c˜&“k¤C¨£JÑ.Ñ/ˆAÙ˜˜!™Ð.˜tÒ. vÑ.Ð.r   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   ÚvaluesÚtupleÚkeysr   Úbroadcast_arraysÚasarrayr    ÚnpÚbroadcast_shapesÚbroadcast_toÚlowerÚallÚ
ValueErrorÚresult_typer   ÚisdtypeÚreshape)r   Úxsr   r   Ú
complex_okÚpreserve_shaper   Únxr   ÚxatÚxasr   Úfsr    ÚargÚmessageÚfÚshapes_equalÚxfatr#   s                      @r   Ú_initializer@      sÓ  ø€ ôh 
ˆR‹€BØ!# Œ˜"Ñ	°€BàÐØ(�Ð(˜Ÿ™›Ñ(ˆÜ˜Ÿ™›Ó&ˆØ#*°ô 	@ô ˜"¨T°bÒ
9€CØ
ˆ"×
Ñ
Ð
)˜rÐ
) DÒ
)€CØ�3�Bˆx˜˜R˜S˜ˆ€BØ,.Ö	/ qˆ"�*‰*�Q˜cˆ*Õ
"Ð	/€BÐ	/Ø.0Ö	1¨ˆ"�*‰*‘T˜!�^˜d’^Õ
$Ð	1€BÐ	1Øˆq‰E�K‰K€EØ�‰U�[‰[€Fáà!&¨Tö 	/ô ×#Ñ# F¨EÓ2ˆØ13Ö4¨Aˆb�o‰o˜a Õ'Ð4ˆÐ4Ø7;Ö<°�—‘  UÕ+Ð<ˆÐ<ðD€GàÐ!Ø1°'·-±-³/Ð1BÐCˆØ.0Ö1¨�A—G‘G˜uÓ$Ð1€LÐ1Üˆ|ÔÜ˜Ó!Ð!ð
 ˆ2�>‰>¨bÖ1¨˜QŸW›WÒ1°S°EÑ9Ð;€DÙ˜bŸj™j¨¨Ô?ÜÐNÓOÐOØ8:Ö	;°1ˆ"�*‰*�Q˜d¨ˆ*Õ
.Ð	;€BÐ	;Ø8:Ö	;°1ˆ"�*‰*�Q˜d¨ˆ*Õ
.Ð	;€BÐ	;ð )+Ö	+ 1ˆ"�*‰*�Q˜Õ
Ð	+€BÐ	+Ø(*Ö	+ 1ˆ"�*‰*�Q˜Õ
Ð	+€BÐ	+ØEIÖJ¸cˆB�J‰J�r—z‘z #¨D�zÓ1°5Õ9ÐJ€DÐJØ��R˜˜u d¨BÐ.Ð.ùòI 
0ùÚ	1ùò 5ùÚ<ùò 2ùò 2ùò 
<ùÚ	;ùò 
,ùÚ	+ùÚJsB   Á>JÂJÄJÄ!JÅJÆJ!ÇJ&Ç4J+ÈJ0È4J5É*J:c           
      óú  — |€t        d«      ‚d}t        j                  |«      }|j                  |«      }|D ��ci c]  \  }}||j	                  ||¬«      “Œ }}}|j	                  ||j
                  ¬«      |d<   |j                  ||j                  t        «      |j                  ¬«      |d<   |j	                  ||j                  ¬«      |d<   |j	                  ||j                  ¬«      |d<   t        |«      }|| _        t        | ||||	||«      }|� t        | |||||||«      }t        ||«      rd	}| j                  |k  �rÞt!        |«      �rÒ|�sÏ|�rÌ || «      }| j                  rŒ| j                  d
   j"                  |j"                  k7  rfg }| j                  D ]N  }|j"                  |j"                  z
  }|j$                  d|z  z   }|j'                  |j)                  ||«      «       ŒP || _        |j$                  }|r|j)                  ||dz   «      } ||g| j                  ¢­Ž }|j                  ||¬«      }|r$|j)                  ||«      }|j)                  ||«      }| xj*                  |j"                  dk(  rdn|j$                  d   z  c_         |||| «       | xj                  dz  c_        t        | ||||	||«      }|�!t        | |||||||«      }t        ||«      rd	}n7t!        |«      d
k(  rn( |
| «       | j                  |k  rt!        |«      r|s|r�ŒÌt-        j.                  | j0                  «      dd j3                  |rt4        nt6        «      | _        t        | |||||||«      S c c}}w )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   rD   r   Úndimr    Úappendr3   rE   ÚxpxÚatrC   ÚsetÚ
_ECALLBACKÚ	_ECONVERR)ÚworkÚcallbackr    Úmaxiterr   r   r   Úpre_func_evalÚpost_func_evalÚcheck_terminationÚpost_termination_checkÚcustomize_resultÚres_work_pairsr   r6   Úcb_terminateÚ
n_elementsÚactiver"   ÚjÚres_dictÚresÚtempr   r;   Ú
n_new_dimsÚ	new_shapeÚx_shaper=   s                                r   Ú_looprk   ‹   sI  € ðR 
€zÜ!Ð"4Ó5Ð5à€Lô —‘˜5Ó!€JØ�Y‰Y�zÓ"€FØAO×P¹¸¸A��2—8‘8˜J¨e�8Ó4Ñ4ÐP€HÑPØŸ(™( :°R·W±W˜(Ó=€HˆYÑØŸ™ ¨R¯Z©Z¼Ó-EÈRÏXÉX˜ÓV€HˆXÑØ—h‘h˜z°·±�hÓ:€HˆU�OØ—x‘x 
°"·(±(�xÓ;€HˆVÑÜ
�hÓ
€CØ€D„Iä  c¨>¸6Ø 1°>À2óG€Fð ÐÜ˜t S¨.¸&À%Ø/°ÀóEˆä$ X¨tÔ4ØˆLà
�(‰(�WÓ
¤¨¥ºÊ*Ù˜$Óˆà�9Š9˜Ÿ™ 1™×*Ñ*¨a¯f©fÒ4ð ˆDØ—y‘yò 8�ØŸV™V c§h¡hÑ.�
ØŸI™I¨¨Z©Ñ7�	Ø—‘˜BŸJ™J s¨IÓ6Õ7ð8ð ˆDŒIà—'‘'ˆÙØ—
‘
˜1˜u u™}Ó.ˆAÙ�Ð�T—Y‘YÒˆØ�J‰J�q ˆJÓ&ˆÙØ—
‘
˜1˜gÓ&ˆAØ—
‘
˜1˜gÓ&ˆAØ�	Š	˜!Ÿ&™& Aš+‘Q¨1¯7©7°2©;Ñ6�	á�q˜!˜TÔ"à�Š�A‰�Ü# D¨#¨~¸vØ$5°~ÀróKˆð ÐÜ" 4¨¨n¸fÀeØ#3°^ÀRóIˆDä(¨°4Ô8Ø#�ØÜ�6‹?˜aÒØá˜tÔ$ðO �(‰(�WÒ
¤¨¤¹Ë*ôR —&‘&˜Ÿ™Ó%¡aÐ(×,Ñ,¹<­ZÌYÓW€D„KÜ˜4  n°f¸eØ+¨^¸RóAð Aùów Qs   »M7c           	      ó8  —  || «      }|j                  |«      r{t        | ||||||«       |r||   }| }||   }|sY| j                  «       D ]$  \  }	}
|	dv rŒt        |
dd«      dkD  r|
|   n|
| |	<   Œ& | j                  D �cg c]  }||   ‘Œ	 c}| _        |S c c}w )N>   Únr   rQ   r   )ÚanyÚ_update_activeÚitemsÚgetattrr   )rX   rf   r`   rc   r]   r6   r   ÚstopÚproceedÚkeyÚvalr;   s               r   rO   rO     sº   € ñ
 ˜TÓ"€Dà	‡v�vˆd„|ô 	�t˜S .°&¸$ÀÐPRÔSáØ˜‘<ˆDà�%ˆØ˜‘ˆáà ŸJ™J›Lò Q‘��Sà˜-Ñ'ØÜ,3°C¸ÀÓ,CÀaÒ,G˜C šLÈS��S’	ð	Qð
 26·±Ö;¨#˜˜W›Ò;ˆDŒIà€Mùò <s   ÂBc                 ó  — |D ��ci c]  \  }}|| |   “Œ }	}}| j                   dk(  |	d<   |�þ|rš|j                  |«      }
t        j                  |
«      |   j	                  d«      }
|
|z  }
|	j                  «       D ]I  \  }}t        |dd«      dkD  r||
   n|}t        j                  ||   «      |
   j	                  |«      ||<   ŒK y ||   }
|	j                  «       D ]I  \  }}t        |dd«      dkD  r||   n|}t        j                  ||   «      |
   j	                  |«      ||<   ŒK y |	j                  «       D ]I  \  }}|rt        |dd«      dkD  r||   }t        j                  ||   «      |   j	                  |«      ||<   ŒK y c c}}w )Nr   rB   TrQ   )rC   Ú
zeros_likerS   rT   rU   rp   rq   )rX   rf   r`   rc   Úmaskr6   r   Úkey1Úkey2Úupdate_dictÚactive_maskrt   ru   s                r   ro   ro   9  s§  € ð
 7E×E©
¨¨d�4˜˜d™Ñ#ÐE€KÑEØ!Ÿ[™[¨AÑ-€K�	ÑàÐÙØŸ-™-¨Ó-ˆKÜŸ&™& Ó-¨fÑ5×9Ñ9¸$Ó?ˆKØ%¨Ñ,ˆKØ'×-Ñ-Ó/ò B‘��SÜ*1°#°v¸qÓ*AÀAÒ*E�c˜+Ò&È3�ÜŸ6™6 # c¡(Ó+¨KÑ8×<Ñ<¸SÓA��C’ñBð ! ™,ˆKØ'×-Ñ-Ó/ò B‘��SÜ#*¨3°¸Ó#:¸QÒ#>�c˜$’iÀC�ÜŸ6™6 # c¡(Ó+¨KÑ8×<Ñ<¸SÓA��C’ñBð $×)Ñ)Ó+ò 	9‰HˆC�Ù¤'¨#¨v°qÓ"9¸AÒ"=Ø˜&‘k�Ü—v‘v˜c #™hÓ'¨Ñ/×3Ñ3°CÓ8ˆC�ŠHñ	9ùó# Fs   †Fc           	      ó6  — |j                  «       }t        | |||d ||«        |||«      }|j                  «       D ]0  \  }}	|j                  |	|«      }
|
j                  dk(  r|
d   n|
||<   Œ2 dg|D ��cg c]  \  }}|‘Œ	 c}}z   |d<   t        di |¤ŽS c c}}w )Nr   © rB   Ú_order_keys)r$   ro   rp   r3   rQ   r   )rX   rf   r`   rc   r    r_   r6   r   rt   ru   rg   r"   rd   s                r   rP   rP   U  s§   € ð
 �(‰(‹*€CÜ�4˜˜n¨f°d¸NÈBÔOá˜S %Ó(€Eà—I‘I“Kò 8‰ˆˆSà�z‰z˜#˜uÓ%ˆØ#Ÿy™y¨Aš~�4˜’8°4ˆˆCŠð8ð
 $˜°n×'E©d¨a°ªÓ'EÑE€CˆÑÜÑ˜ÑÐùó (Fs   Á5B)NFNN)F)rG   Únumpyr+   Ú_utilr   r   Ú
_array_apir   r   r   Úscipy._external.array_api_extraÚ	_externalÚarray_api_extrarS   Ú	_ESIGNERRrW   Ú
_EVALUEERRrV   Ú
_EINPUTERRÚ_ECONVERGEDrM   r@   rk   rO   ro   rP   r~   r   r   ú<module>rŠ      sj   ðó Û ß 9ß @Ñ @ß -Ð -à€	Ø€	Ø€
Ø€
Ø€
Ø€Ø€à'+Ø:>ók/ð` @EóMAò`ò<9ó8r   