§
    "�’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dS )é    Né   )Ú_RichResultÚ_call_callback_maybe_halt)Úarray_namespaceÚxp_sizeÚxp_result_typeéÿÿÿÿéþÿÿÿéýÿÿÿéüÿÿÿéûÿÿÿFc                 óF  ‡ ‡‡‡‡‡‡— t          |¦  «        }‰€	t          |Ž n‰Št          |d‰dœŽŠ ‰j        g |¢‰¢R Ž }|d|…         ||d…         c}Šˆˆfd„|D ¦   «         }ˆˆ ˆfd„|D ¦   «         }|d         j        Š|d         j        Š|r<‰‰ dœˆfd„
Š t          j        ‰‰¦  «        Šˆˆfd	„|D ¦   «         }ˆˆfd
„‰D ¦   «         Šd}	|�d|	                     ¦   «         › �}	ˆfd„|D ¦   «         }
t          |
¦  «        st          |	¦  «        ‚ ‰j
        d„ |D ¦   «         ‰gz   Ž Š|s%‰                     ‰d¦  «        st          d¦  «        ‚ˆˆfd„|D ¦   «         }ˆˆfd„|D ¦   «         }ˆfd„|D ¦   «         }ˆfd„|D ¦   «         }ˆfd„‰D ¦   «         Š‰ ||‰‰‰‰fS )a  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Úxpc                 ó>   •— g | ]}‰                      |‰¬ ¦  «        ‘ŒS ©©Údtype©Úasarray)Ú.0ÚxÚxatr   s     €€úVC:\Projects\DSCons\.venv\Lib\site-packages\scipy\_lib\_elementwise_iterative_method.pyú
<listcomp>z_initialize.<locals>.<listcomp>Y   s)   ø€ Ð	/Ð	/Ð	/ qˆ"�*Š*�Q˜cˆ*Ñ
"Ô
"Ð	/Ð	/Ð	/ó    c                 óH   •— g | ]}‰                       ‰|g‰¢R Ž ¦  «        ‘ŒS © r   )r   r   ÚargsÚfuncr   s     €€€r   r   z_initialize.<locals>.<listcomp>Z   s3   ø€ Ð	1Ð	1Ð	1¨ˆ"�*Š*�T�T˜!�^˜d�^�^�^Ñ
$Ô
$Ð	1Ð	1Ð	1r   r   )Úshaper    c                ól   •— dt          ‰¦  «        t          |¦  «        z
  z  } || |         g|¢R i |¤ŽS )N)r   )Úlen)r   r!   r    r   ÚkwargsÚiÚfshapes         €r   r    z_initialize.<locals>.func`   sB   ø€ Ø•c˜&‘k”k¥C¨¡J¤JÑ.Ñ/ˆAØ�4˜˜!œÐ.˜tÐ.Ð.Ð. vÐ.Ð.Ð.r   c                 ó<   •— g | ]}‰                      |‰¦  «        ‘ŒS r   ©Úbroadcast_to)r   r   r!   r   s     €€r   r   z_initialize.<locals>.<listcomp>d   s'   ø€ Ð4Ð4Ð4¨Aˆb�oŠo˜a Ñ'Ô'Ð4Ð4Ð4r   c                 ó<   •— g | ]}‰                      |‰¦  «        ‘ŒS r   r(   )r   Úargr!   r   s     €€r   r   z_initialize.<locals>.<listcomp>e   s'   ø€ Ð<Ð<Ð<°�—’  UÑ+Ô+Ð<Ð<Ð<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`, c                 ó&   •— g | ]}|j         ‰k    ‘ŒS r   )r!   )r   Úfr!   s     €r   r   z_initialize.<locals>.<listcomp>k   s    ø€ Ð1Ð1Ð1¨�A”G˜uÒ$Ð1Ð1Ð1r   c                 ó   — g | ]	}|j         ‘Œ
S r   r   )r   r-   s     r   r   z_initialize.<locals>.<listcomp>r   s   € Ð1Ð1Ð1¨˜QœWÐ1Ð1Ð1r   zreal floatingz3Abscissae and function output must be real numbers.c                 ó@   •— g | ]}‰                      |‰d ¬¦  «        ‘ŒS ©T)r   Úcopyr   )r   r   Úxfatr   s     €€r   r   z_initialize.<locals>.<listcomp>u   ó+   ø€ Ð	;Ð	;Ð	;°1ˆ"�*Š*�Q˜d¨ˆ*Ñ
.Ô
.Ð	;Ð	;Ð	;r   c                 ó@   •— g | ]}‰                      |‰d ¬¦  «        ‘ŒS r0   r   )r   r-   r2   r   s     €€r   r   z_initialize.<locals>.<listcomp>v   r3   r   c                 ó<   •— g | ]}‰                      |d ¦  «        ‘ŒS ©©r	   ©Úreshape)r   r   r   s     €r   r   z_initialize.<locals>.<listcomp>z   ó'   ø€ Ð	+Ð	+Ð	+ 1ˆ"�*Š*�Q˜Ñ
Ô
Ð	+Ð	+Ð	+r   c                 ó<   •— g | ]}‰                      |d ¦  «        ‘ŒS r6   r8   )r   r-   r   s     €r   r   z_initialize.<locals>.<listcomp>{   r:   r   c                 óf   •— g | ]-}‰                      ‰                     |d ¬¦  «        d¦  «        ‘Œ.S )T)r1   r7   )r9   r   )r   r+   r   s     €r   r   z_initialize.<locals>.<listcomp>|   s7   ø€ ÐJÐJÐJ¸cˆB�JŠJ�r—z’z #¨D�zÑ1Ô1°5Ñ9Ô9ÐJÐJÐJr   )r#   r   r   Úbroadcast_arraysr!   ÚnpÚbroadcast_shapesÚlowerÚallÚ
ValueErrorÚresult_typeÚisdtype)r    Úxsr   Ú
complex_okÚpreserve_shaper   ÚnxÚxasÚfsÚmessageÚshapes_equalr&   r!   r   r2   s   ` `  `     @@@@r   Ú_initializerM      s�  øøøøøøø€ õb 
ˆR‰Œ€BØ!# �˜"Ð	Ð	°€Bõ ˜"¨T°bÐ
9Ð
9Ð
9€CØ
ˆ"Ô
Ð
)˜rÐ
) DÐ
)Ð
)Ð
)€CØ�3�B�3Œx˜˜R˜S˜Sœ€H€BˆØ	/Ð	/Ð	/Ð	/Ð	/¨BÐ	/Ñ	/Ô	/€BØ	1Ð	1Ð	1Ð	1Ð	1Ð	1¨bÐ	1Ñ	1Ô	1€BØˆqŒEŒK€EØ�ŒUŒ[€Fàð =à!&¨Tð 	/ð 	/ð 	/ð 	/ð 	/ð 	/ð 	/õ Ô# F¨EÑ2Ô2ˆØ4Ð4Ð4Ð4Ð4°Ð4Ñ4Ô4ˆØ<Ð<Ð<Ð<Ð<°tÐ<Ñ<Ô<ˆðD€GàÐ!ØC°'·-²-±/´/ÐCÐCˆØ1Ð1Ð1Ð1¨bÐ1Ñ1Ô1€LÝˆ|ÑÔð "Ý˜Ñ!Ô!Ð!ð
 ˆ2Œ>Ð1Ð1¨bÐ1Ñ1Ô1°S°EÑ9Ð;€DØð P˜bŸjšj¨¨Ñ?Ô?ð PÝÐNÑOÔOÐOØ	;Ð	;Ð	;Ð	;Ð	;¸Ð	;Ñ	;Ô	;€BØ	;Ð	;Ð	;Ð	;Ð	;¸Ð	;Ñ	;Ô	;€Bð 
,Ð	+Ð	+Ð	+¨Ð	+Ñ	+Ô	+€BØ	+Ð	+Ð	+Ð	+¨Ð	+Ñ	+Ô	+€BØJÐJÐJÐJÀTÐJÑJÔJ€DØ��R˜˜u d¨BÐ.Ð.r   c           
      óì  ‡‡‡— ‰€t          d¦  «        ‚d}t          j        |¦  «        Š‰                     ‰¦  «        }ˆˆˆfd„|D ¦   «         }‰                     ‰‰j        ¬¦  «        |d<   ‰                     ‰‰                     t          ¦  «        ‰j	        ¬¦  «        |d<   ‰                     ‰‰j	        ¬¦  «        |d<   ‰                     ‰‰j	        ¬¦  «        |d	<   t          |¦  «        }|| _        t          | ||||	|‰¦  «        }|�(t          | ||||||‰¦  «        }t          ||¦  «        rd
}| j        |k     �rÓt!          |¦  «        �rÃ|�sÀ‰�r½ || ¦  «        }| j        rs| j        d         j        |j        k    rXg }| j        D ]G}|j        |j        z
  }|j        d|z  z   }|                     ‰                     ||¦  «        ¦  «         ŒH|| _        |j        }|r‰                     ||dz   ¦  «        } ||g| j        ¢R Ž }‰                     |‰¬¦  «        }|r,‰                     ||¦  «        }‰                     ||¦  «        }| xj        |j        dk    rdn|j        d         z  c_         |||| ¦  «         | xj        dz  c_        t          | ||||	|‰¦  «        }|�)t          | ||||||‰¦  «        }t          ||¦  «        rd
}n>t!          |¦  «        dk    rn* |
| ¦  «         | j        |k     rt!          |¦  «        r|s‰�°½t-          j        | j        ¦  «        dd…                              |rt4          nt6          ¦  «        | _        t          | ||||||‰¦  «        S )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.Fc                 óF   •— i | ]\  }}|‰                      ‰‰¬ ¦  «        “ŒS r   )Úzeros)r   r%   Újr   Ú
n_elementsr   s      €€€r   ú
<dictcomp>z_loop.<locals>.<dictcomp>Ñ   s/   ø€ ÐPÐPÐP¹¸¸A��2—8’8˜J¨e�8Ñ4Ô4ÐPÐPÐPr   r   ÚsuccessÚstatusÚnitÚnfevTr   )r   r7   r   r	   )ÚNotImplementedErrorÚmathÚprodÚarangerP   ÚboolÚfullr   Ú_EINPROGRESSÚint32r   r   Ú_check_terminationÚ_prepare_resultr   rV   r   Úndimr!   Úappendr9   rW   ÚxpxÚatrU   ÚsetÚ
_ECALLBACKÚ	_ECONVERR)ÚworkÚcallbackr!   Úmaxiterr    r   r   Úpre_func_evalÚpost_func_evalÚcheck_terminationÚpost_termination_checkÚcustomize_resultÚres_work_pairsr   rG   Úcb_terminateÚactiveÚres_dictÚresÚtempr   r+   Ú
n_new_dimsÚ	new_shapeÚx_shaper-   rR   s         `      `            @r   Ú_looprz   €   s×  øøø€ ðR 
€zÝ!Ð"4Ñ5Ô5Ð5à€Lõ ”˜5Ñ!Ô!€JØ�YŠY�zÑ"Ô"€FØPÐPÐPÐPÐPÐPÀÐPÑPÔP€HØŸ(š( :°R´W˜(Ñ=Ô=€HˆYÑØŸš ¨R¯ZªZ½Ñ-EÔ-EÈRÌX˜ÑVÔV€HˆXÑØ—h’h˜z°´�hÑ:Ô:€HˆU�OØ—x’x 
°"´(�xÑ;Ô;€HˆVÑÝ
�hÑ
Ô
€CØ€D„Iå  c¨>¸6Ø 1°>À2ñGô G€Fð ÐÝ˜t S¨.¸&À%Ø/°ÀñEô Eˆå$ X¨tÑ4Ô4ð 	 ØˆLà
Œ(�WÒ
Ñ
¥¨¡¤Ñ
¸Ñ
È*Ñ
ØˆM˜$ÑÔˆàŒ9ð 		˜œ 1œÔ*¨a¬fÒ4Ð4ð ˆDØ”yð 8ð 8�ØœV c¤hÑ.�
ØœI¨¨Z©Ñ7�	Ø—’˜BŸJšJ s¨IÑ6Ô6Ñ7Ô7Ð7Ð7ØˆDŒIà”'ˆØð 	/Ø—
’
˜1˜u u™}Ñ.Ô.ˆAØˆD�Ð�T”YÐÐÐˆØ�JŠJ�q ˆJÑ&Ô&ˆØð 	'Ø—
’
˜1˜gÑ&Ô&ˆAØ—
’
˜1˜gÑ&Ô&ˆAØˆ	Œ	˜!œ& Aš+˜+�Q�Q¨1¬7°2¬;Ñ6ˆ	Œ	àˆ�q˜!˜TÑ"Ô"Ð"àˆŒ�A‰ˆŒÝ# D¨#¨~¸vØ$5°~ÀrñKô Kˆð ÐÝ" 4¨¨n¸fÀeØ#3°^ÀRñIô IˆDå(¨°4Ñ8Ô8ð Ø#�ØÝ�6‰?Œ?˜aÒÐØàÐ˜tÑ$Ô$Ð$ðO Œ(�WÒ
Ð
¥¨¡¤Ð
¸Ð
È*Ñ
õR ”&˜œÑ%Ô% a a aÔ(×,Ò,¸<Ð-V­Z¨ZÍYÑWÔW€D„KÝ˜4  n°f¸eØ+¨^¸RñAô Að Ar   c           	      óR  ‡
—  || ¦  «        }|                      |¦  «        r…t          | ||||||¦  «         |r||         }| Š
|‰
         }|sY|                      ¦   «         D ],\  }}	|dv rŒ
t          |	dd¦  «        dk    r|	‰
         n|	| |<   Œ-ˆ
fd„| j        D ¦   «         | _        |S )N>   Únr   rb   r   c                 ó    •— g | ]
}|‰         ‘ŒS r   r   )r   r+   Úproceeds     €r   r   z&_check_termination.<locals>.<listcomp>)  s   ø€ Ð;Ð;Ð;¨#˜˜WœÐ;Ð;Ð;r   )ÚanyÚ_update_activeÚitemsÚgetattrr   )ri   ru   rq   rs   rn   rG   r   ÚstopÚkeyÚvalr~   s             @r   r`   r`     sã   ø€ ð
 Ð˜TÑ"Ô"€Dà	‡v‚vˆd�|„|ð <õ 	�t˜S .°&¸$ÀÐPRÑSÔSÐSàð 	 Ø˜”<ˆDà�%ˆØ˜”ˆàð 	<à ŸJšJ™LœLð Qð Q‘��Sà˜-Ð'Ð'ØÝ,3°C¸ÀÑ,CÔ,CÀaÒ,GÐ,G˜C œL˜LÈS��S‘	�	Ø;Ð;Ð;Ð;°´Ð;Ñ;Ô;ˆDŒIà€Mr   c                 óŠ  ‡ — ˆ fd„|D ¦   «         }‰ j         dk    |d<   |��3|r¸|                     |¦  «        }t          j        |¦  «        |                              d¦  «        }||z  }|                     ¦   «         D ]Z\  }	}
t          |
dd¦  «        dk    r|
|         n|
}
t          j        ||	         ¦  «        |                              |
¦  «        ||	<   Œ[d S ||         }|                     ¦   «         D ]Z\  }	}
t          |
dd¦  «        dk    r|
|         n|
}
t          j        ||	         ¦  «        |                              |
¦  «        ||	<   Œ[d S |                     ¦   «         D ]Z\  }	}
|rt          |
dd¦  «        dk    r|
|         }
t          j        ||	         ¦  «        |                              |
¦  «        ||	<   Œ[d S )Nc                 ó(   •— i | ]\  }}|‰|         “ŒS r   r   )r   Úkey1Úkey2ri   s      €r   rS   z"_update_active.<locals>.<dictcomp>3  s#   ø€ ÐEÐEÐE©
¨¨d�4˜˜dœÐEÐEÐEr   r   rT   Trb   )rU   Ú
zeros_likerd   re   rf   r�   r‚   )ri   ru   rq   rs   ÚmaskrG   r   Úupdate_dictÚactive_maskr„   r…   s   `          r   r€   r€   .  sÿ  ø€ ð
 FÐEÐEÐE°nÐEÑEÔE€KØ!œ[¨AÒ-€K�	ÑàÑØð 	BØŸ-š-¨Ñ-Ô-ˆKÝœ& Ñ-Ô-¨fÔ5×9Ò9¸$Ñ?Ô?ˆKØ%¨Ñ,ˆKØ'×-Ò-Ñ/Ô/ð Bð B‘��SÝ*1°#°v¸qÑ*AÔ*AÀAÒ*EÐ*E�c˜+Ô&Ð&È3�Ýœ6 # c¤(Ñ+Ô+¨KÔ8×<Ò<¸SÑAÔA��C‘�ðBð Bð ! œ,ˆKØ'×-Ò-Ñ/Ô/ð Bð B‘��SÝ#*¨3°¸Ñ#:Ô#:¸QÒ#>Ð#>�c˜$”i�iÀC�Ýœ6 # c¤(Ñ+Ô+¨KÔ8×<Ò<¸SÑAÔA��C‘�ðBð Bð $×)Ò)Ñ+Ô+ð 	9ð 	9‰HˆC�Øð "¥'¨#¨v°qÑ"9Ô"9¸AÒ"=Ð"=Ø˜&”k�Ý”v˜c #œhÑ'Ô'¨Ô/×3Ò3°CÑ8Ô8ˆC�‰HˆHð	9ð 	9r   c           	      ó:  — |                      ¦   «         }t          | |||d ||¦  «          |||¦  «        }|                     ¦   «         D ]3\  }}	|                     |	|¦  «        }
|
j        dk    r|
d         n|
||<   Œ4dgd„ |D ¦   «         z   |d<   t          di |¤ŽS )Nr   r   rT   c                 ó   — g | ]\  }}|‘ŒS r   r   )r   r%   rQ   s      r   r   z#_prepare_result.<locals>.<listcomp>Y  s   € Ð'EÐ'EÐ'E©d¨a°¨Ð'EÐ'EÐ'Er   Ú_order_keys)r1   r€   r�   r9   rb   r   )ri   ru   rq   rs   r!   rp   rG   r   r„   r…   rv   s              r   ra   ra   J  s¼   € ð
 �(Š(‰*Œ*€CÝ�4˜˜n¨f°d¸NÈBÑOÔOÐOàÐ˜S %Ñ(Ô(€Eà—I’I‘K”Kð 8ð 8‰ˆˆSà�zŠz˜#˜uÑ%Ô%ˆØ#œy¨Aš~˜~�4˜”8�8°4ˆˆC‰ˆà#˜Ð'EÐ'E°nÐ'EÑ'EÔ'EÑE€CˆÑÝÐÐ˜ÐÐÐr   )FNN)F)rY   Únumpyr>   Ú_utilr   r   Ú
_array_apir   r   r   Úscipy._lib.array_api_extraÚ_libÚarray_api_extrard   Ú	_ESIGNERRrh   Ú
_EVALUEERRrg   Ú
_EINPUTERRÚ_ECONVERGEDr^   rM   rz   r`   r€   ra   r   r   r   ú<module>r›      s  ðð €€€Ø Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à€	Ø€	Ø€
Ø€
Ø€
Ø€Ø€ð`/ð `/ð `/ð `/ðJ @EðMAð MAð MAð MAð`ð ð ð<9ð 9ð 9ð8ð ð ð ð r   