§
    m¬¸j­;  ã                   óv   — d dl Z d dlZddlmZmZ ddlmZ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éÿÿÿÿéþÿÿÿéýÿÿÿéüÿÿÿéûÿÿÿFc                 ó¸  ‡ ‡‡‡‡‡‡— t          |¦  «        }‰€	t          |Ž n‰Š ‰j        g |¢‰¢R Ž } ‰j        d„ |D ¦   «         Ž Š‰                     ‰d¦  «        r‰                     d¦  «        j        n‰Š|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`.
    Nc                 ó   — g | ]	}|j         ‘Œ
S © ©Údtype)Ú.0Úxas     úx/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/scipy/_lib/_elementwise_iterative_method.pyú
<listcomp>z_initialize.<locals>.<listcomp>V   s   € Ð2Ð2Ð2¨˜2œ8Ð2Ð2Ð2ó    Úintegralg      ð?c                 ó>   •— g | ]}‰                      |‰¬ ¦  «        ‘ŒS ©r   ©Úasarray)r   ÚxÚxatÚxps     €€r   r   z_initialize.<locals>.<listcomp>Y   s)   ø€ Ð	/Ð	/Ð	/ qˆ"�*Š*�Q˜cˆ*Ñ
"Ô
"Ð	/Ð	/Ð	/r   c                 óH   •— g | ]}‰                       ‰|g‰¢R Ž ¦  «        ‘ŒS r   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 r1   r   )r   r.   r3   r   s     €€r   r   z_initialize.<locals>.<listcomp>v   r4   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 r7   r9   )r   r.   r   s     €r   r   z_initialize.<locals>.<listcomp>{   r;   r   c                 óf   •— g | ]-}‰                      ‰                     |d ¬¦  «        d¦  «        ‘Œ.S )T)r2   r8   )r:   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   Úbroadcast_arraysÚresult_typeÚisdtyper   r   r"   ÚnpÚbroadcast_shapesÚlowerÚallÚ
ValueError)r!   Úxsr    Ú
complex_okÚpreserve_shaper   ÚnxÚxasÚfsÚmessageÚshapes_equalr'   r"   r   r3   s   ` `  `     @@@@r   Ú_initializerN      sÀ  øøøøøøø€ õb 
ˆR‰Œ€BØ!# �˜"Ð	Ð	°€Bð ˆ"Ô
Ð
)˜rÐ
) DÐ
)Ð
)Ð
)€CØ
ˆ"Œ.Ð2Ð2¨cÐ2Ñ2Ô2Ð
3€CØ"$§*¢*¨S°*Ñ"=Ô"=Ð
Fˆ"�*Š*�R‰.Œ.Ô
Ð
À3€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‰�°½|rt,          nt.          | j        dd…<   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   r8   r   r   )ÚNotImplementedErrorÚmathÚprodÚarangerQ   ÚboolÚfullr   Ú_EINPROGRESSÚint32r   r    Ú_check_terminationÚ_prepare_resultr   rW   r   Úndimr"   Úappendr:   rX   Ú
_ECALLBACKÚ	_ECONVERRrV   )ÚworkÚcallbackr"   Úmaxiterr!   r    r   Úpre_func_evalÚpost_func_evalÚcheck_terminationÚpost_termination_checkÚcustomize_resultÚres_work_pairsr   rH   Úcb_terminateÚactiveÚres_dictÚresÚtempr   r,   Ú
n_new_dimsÚ	new_shapeÚx_shaper.   rS   s         `      `            @r   Ú_looprx   €   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 $0Ð>•Z�ZµY€D„K����NÝ˜4  n°f¸eØ+¨^¸RñAô Að Ar   c           	      ón  ‡
—  || ¦  «        }|                      |¦  «        r“t          | ||||||¦  «         |r||         }| Š
|‰
         }|sg|                      ¦   «         D ]:\  }}	|dk    rŒ	 |	‰
         | |<   Œ# t          t          t
          f$ r |	| |<   Y Œ7w xY wˆ
fd„| j        D ¦   «         | _        |S )Nr    c                 ó    •— g | ]
}|‰         ‘ŒS r   r   )r   r,   Úproceeds     €r   r   z&_check_termination.<locals>.<listcomp>-  s   ø€ Ð;Ð;Ð;¨#˜˜WœÐ;Ð;Ð;r   )ÚanyÚ_update_activeÚitemsÚ
IndexErrorÚ	TypeErrorÚKeyErrorr    )rg   rs   ro   rq   rl   rH   r   ÚstopÚkeyÚvalr{   s             @r   ra   ra     sû   ø€ ð
 Ð˜TÑ"Ô"€Dà	‡v‚vˆd�|„|ð <õ 	�t˜S .°&¸$ÀÐPRÑSÔSÐSàð 	 Ø˜”<ˆDà�%ˆØ˜”ˆàð 	<à ŸJšJ™LœLð $ð $‘��Sð ˜&’=�=Øð$Ø # G¤�D˜‘I�IøÝ"¥I­xÐ8ð $ð $ð $Ø #�D˜‘I�I�Ið$øøøà;Ð;Ð;Ð;°´Ð;Ñ;Ô;ˆDŒIà€Ms   Á/A;Á;BÂBc                 óŠ  ‡ — ˆ fd„|D ¦   «         }‰ j         dk    |d<   |�Õ|ru|                     |¦  «        }d||<   ||z  }|                     ¦   «         D ]?\  }	}
	 |
|         ||	         |<   Œ# t          t          t
          f$ r |
||	         |<   Y Œ<w xY wd S ||         }|                     ¦   «         D ]?\  }	}
	 |
|         ||	         |<   Œ# t          t          t
          f$ r |
||	         |<   Y Œ<w xY wd S |                     ¦   «         D ]9\  }	}
|r'	 |
|         }
n# t          t          t
          f$ r Y nw xY w|
||	         |<   Œ:d S )Nc                 ó(   •— i | ]\  }}|‰|         “ŒS r   r   )r   Úkey1Úkey2rg   s      €r   rT   z"_update_active.<locals>.<dictcomp>7  s#   ø€ ÐEÐEÐE©
¨¨d�4˜˜dœÐEÐEÐEr   r   rU   r   )rV   Ú
zeros_liker~   r   r€   r�   )rg   rs   ro   rq   ÚmaskrH   r   Úupdate_dictÚactive_maskrƒ   r„   s   `          r   r}   r}   2  sÜ  ø€ ð
 FÐEÐEÐE°nÐEÑEÔE€KØ!œ[¨AÒ-€K�	ÑàÐØð 	0ØŸ-š-¨Ñ-Ô-ˆKØ"#ˆK˜ÑØ%¨Ñ,ˆKØ'×-Ò-Ñ/Ô/ð 0ð 0‘��Sð0Ø,/°Ô,<�C˜”H˜[Ñ)Ð)øÝ"¥I­xÐ8ð 0ð 0ð 0Ø,/�C˜”H˜[Ñ)Ð)Ð)ð0øøøð0ð 0ð ! œ,ˆKØ'×-Ò-Ñ/Ô/ð 0ð 0‘��Sð0Ø,/°¬I�C˜”H˜[Ñ)Ð)øÝ"¥I­xÐ8ð 0ð 0ð 0Ø,/�C˜”H˜[Ñ)Ð)Ð)ð0øøøð0ð 0ð $×)Ò)Ñ+Ô+ð 	#ð 	#‰HˆC�Øð ðØ˜fœ+�C�CøÝ"¥I­xÐ8ð ð ð Ø�Dðøøøà"ˆC�ŒH�VÑÐð	#ð 	#s6   ÁA-Á-"BÂBÂ9CÃ"C0Ã/C0ÄDÄD4Ä3D4c           	      ó:  — |                      ¦   «         }t          | |||d ||¦  «          |||¦  «        }|                     ¦   «         D ]3\  }}	|                     |	|¦  «        }
|
j        dk    r|
d         n|
||<   Œ4dgd„ |D ¦   «         z   |d<   t          di |¤ŽS )Nr   r   rU   c                 ó   — g | ]\  }}|‘ŒS r   r   )r   r&   rR   s      r   r   z#_prepare_result.<locals>.<listcomp>d  s   € Ð'EÐ'EÐ'E©d¨a°¨Ð'EÐ'EÐ'Er   Ú_order_keys)r2   r}   r~   r:   rc   r   )rg   rs   ro   rq   r"   rn   rH   r   rƒ   r„   rt   s              r   rb   rb   U  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)rZ   ÚnumpyrA   Ú_utilr   r   Ú
_array_apir   r   Ú	_ESIGNERRrf   Ú
_EVALUEERRre   Ú
_EINPUTERRÚ_ECONVERGEDr_   rN   rx   ra   r}   rb   r   r   r   ú<module>r—      så   ðð €€€Ø Ð Ð Ð Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0à€	Ø€	Ø€
Ø€
Ø€
Ø€Ø€ða/ð a/ð a/ð a/ðL @EðMAð MAð MAð MAð`ð ð ðD #ð  #ð  #ðFð ð ð ð r   