o
    Ö­jQW  ã                   @   s–  U d Z ddlZddlmZ ddlmZmZmZ ddlZ	ddl
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZ g d¢Zej  !dd	¡Z"e#e$B e%d< ej  !d
d¡Z&e"e&dœZ'eZ(ee%d< e(ej)B Z)ee%d< dd„ Z*de(deddfdd„Z+de(defdd„Z,			dfdd	d	dœde)deded dB de$dB dedB de$de$de(fd d!„Z-dd"œd#e(dedB de(fd$d%„Z.d&d&d&d&d'œd(d)„Z/d*d+„ Z0d&d&d&d&d,dd-œd.d/„Z1ddd&d&d&d&d,dd0œd1d2„Z2d&d&d&d&d,d&dd3œd4d5„Z3dgd7d8„Z4dhd:d;„Z5d<ede#fd=d>„Z6d#e(dede$fd?d@„Z7dede8e# e8d B fdAdB„Z9dededB fdCdD„Z:dd"œd#e(dEe;dedB de(fdFdG„Z<dd"œdHe(dIe(dedB de(fdJdK„Z=dd"œd#e(dedB de(fdLdM„Z>dd	dNddOœd#e(dPe;e?e; B dB dQe$dRe;e@B dedB de(fdSdT„ZAdd"œd#e(dedB de(fdUdV„ZBdd"œd#e(dedB de(fdWdX„ZCdYddZœd[e(d\e(dPe;dedB de(f
d]d^„ZDd	d	dd_œd`da„ZEdid[e(dedB de(fdbdc„ZFddde„ ZGdS )ja6  Utility functions to use Python Array API compatible libraries.

For the context about the Array API see:
https://data-apis.org/array-api/latest/purpose_and_scope.html

The SciPy use case of the Array API is described on the following page:
https://data-apis.org/array-api/latest/use_cases.html#use-case-scipy
é    N)Ú
ModuleType)ÚAnyÚLiteralÚ	TypeAlias)Úarray_api_compat)	Úis_array_api_objÚsizeÚnumpyÚdeviceÚis_numpy_namespaceÚis_cupy_namespaceÚis_torch_namespaceÚis_jax_namespaceÚis_array_api_strict_namespace)Ú_asarrayÚarray_namespaceÚassert_almost_equalÚassert_array_almost_equalÚget_xp_devicesÚis_array_api_strictÚ
is_complexÚis_cupyÚis_jaxÚis_numpyÚis_torchÚSCIPY_ARRAY_APIÚSCIPY_DEVICEÚscipy_namespace_forÚxp_assert_closeÚxp_assert_equalÚxp_assert_lessÚxp_copyÚxp_copysignÚ	xp_deviceÚxp_moveaxis_to_endÚxp_ravelÚxp_realÚxp_signÚxp_sizeÚxp_take_along_axisÚxp_unsupported_param_msgÚxp_vector_normr   Fr   Úcpu)r   r   ÚArrayÚ	ArrayLikec              	   C   s  t t| ƒƒD ]„}| | }ddlm} ||ƒrd}t|ƒ‚t|tjjƒr't	dƒ‚t|tj
ƒr1t	dƒ‚t|tjtjB ƒrT|j}t |tj¡sSt |tj¡sSt	d|›d�ƒ‚qt|ƒsŠzt |¡}W n t	yj   t	dƒ‚w |j}t |tj¡s†t |tj¡s†d	|›d�}t	|ƒ‚|| |< q| S )
a€  Raise exceptions on known-bad subclasses.

    The following subclasses are not supported and raise and error:
    - `numpy.ma.MaskedArray`
    - `numpy.matrix`
    - NumPy arrays which do not have a boolean or numerical dtype
    - Any array-like which is neither array API compatible nor coercible by NumPy
    - Any array-like which is coerced by NumPy to an unsupported dtype
    r   )Úissparsez�Sparse arrays/matrices are not supported by this function. Perhaps one of the `scipy.sparse.linalg` functions would work instead.z8Inputs of type `numpy.ma.MaskedArray` are not supported.z0Inputs of type `numpy.matrix` are not supported.zAn argument has dtype `z3`; only boolean and numerical dtypes are supported.zCAn argument is neither array API compatible nor coercible by NumPy.z1An argument was coerced to an unsupported dtype `)ÚrangeÚlenÚscipy.sparser/   Ú
ValueErrorÚ
isinstanceÚnpÚmaÚMaskedArrayÚ	TypeErrorÚmatrixÚndarrayÚgenericÚdtypeÚ
issubdtypeÚnumberÚbool_r   Ú
asanyarray)ÚarraysÚiÚarrayr/   Úmsgr<   Úmessage© rF   úR/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/_lib/_array_api.pyÚ_compliance_scipy9   s:   
€ÿ
ÿ€rH   rC   ÚxpÚreturnc                 C   s:   d}z|  | | ¡¡st|ƒ‚W dS  ty   t|ƒ‚w )zCheck for NaNs or Infs.z#array must not contain infs or NaNsN)ÚallÚisfiniter3   r8   )rC   rI   rD   rF   rF   rG   Ú_check_finiteh   s   ÿÿrM   rA   c                  G   s,   t d stS dd„ | D ƒ}t|ƒ}tj|Ž S )a1  Get the array API compatible namespace for the arrays xs.

    Parameters
    ----------
    *arrays : sequence of array_like
        Arrays used to infer the common namespace.

    Returns
    -------
    namespace : module
        Common namespace.

    Notes
    -----
    Thin wrapper around `array_api_compat.array_namespace`.

    1. Check for the global switch: SCIPY_ARRAY_API. This can also be accessed
       dynamically through ``_GLOBAL_CONFIG['SCIPY_ARRAY_API']``.
    2. `_compliance_scipy` raise exceptions on known-bad subclasses. See
       its definition for more details.

    When the global switch is False, it defaults to the `numpy` namespace.
    In that case, there is no compliance check. This is a convenience to
    ease the adoption. Otherwise, arrays must comply with the new rules.
    r   c                 S   ó   g | ]}|d ur|‘qS ©NrF   )Ú.0rC   rF   rF   rG   Ú
<listcomp>�   ó    z#array_namespace.<locals>.<listcomp>)Ú_GLOBAL_CONFIGÚ	np_compatrH   r   r   )rA   Ú_arraysrF   rF   rG   r   r   s
   
r   )rI   Úcheck_finiteÚsubokr<   Úorder)ÚKÚAÚCÚFÚcopyrV   rW   c                C   s´   |du rt | ƒ}t|ƒr.|du rtj| |||d�} n7|r%tj| ||d�} n,tj| ||d�} n#z
|j| ||d�} W n tyP   t | d¡ƒ}|j| ||d�} Y nw |rXt| |ƒ | S )a`  SciPy-specific replacement for `np.asarray` with `order`, `check_finite`, and
    `subok`.

    Memory layout parameter `order` is not exposed in the Array API standard.
    `order` is only enforced if the input array implementation
    is NumPy based, otherwise `order` is just silently ignored.

    `check_finite` is also not a keyword in the array API standard; included
    here for convenience rather than that having to be a separate function
    call inside SciPy functions.

    `subok` is included to allow this function to preserve the behaviour of
    `np.asanyarray` for NumPy based inputs.
    NT)rX   r<   rW   )rX   r<   )r<   r]   é   )r   r   r5   rC   r@   Úasarrayr8   rM   )rC   r<   rX   r]   rI   rV   rW   Ú
coerced_xprF   rF   rG   r   —   s"   þ
r   ©rI   Úxc                C   s   |du rt | ƒ}t| d|d�S )a3  
    Copies an array.

    Parameters
    ----------
    x : array

    xp : array_namespace

    Returns
    -------
    copy : array
        Copied array

    Notes
    -----
    This copy function does not offer all the semantics of `np.copy`, i.e. the
    `subok` and `order` keywords are not used.
    NT)r]   rI   )r   r   ©rb   rI   rF   rF   rG   r!   Æ   s   r!   T©Úcheck_namespaceÚcheck_dtypeÚcheck_shapeÚcheck_0dc          	      C   sè   d}|r	t | |ƒ t|ƒr3|r3dt| ƒ› dt|ƒ› �}| | ¡r%| |¡s3| | ¡s/| |¡r3J |ƒ‚| | ¡} | |¡}|rSd| j› d|j› �}| j|jksSJ |ƒ‚|rid| j› d|j› �}| j|jksiJ |ƒ‚| || j¡}| |fS )NTz$Array-ness does not match:
 Actual: z
 Desired: zdtypes do not match.
Actual: ú

Desired: zShapes do not match.
Actual: )Ú_assert_matching_namespacer   ÚtypeÚisscalarr_   r<   ÚshapeÚbroadcast_to)	ÚactualÚdesiredrI   re   rf   rg   rh   Ú__tracebackhide__Ú_msgrF   rF   rG   Ú_strict_checkâ   s4   
ÿÿÿÿÿ

rs   c                 C   sZ   d}t | tƒr	| n| f} t|ƒ}| D ]}t|ƒ}d|j› d|j› �}||ks*J |ƒ‚qd S )NTz!Namespaces do not match.
Actual: ri   )r4   Útupler   Ú__name__)ro   rp   rq   Údesired_spaceÚarrÚ	arr_spacerr   rF   rF   rG   rj      s   ÿþûrj   Ú )re   rf   rg   rh   Úerr_msgrI   c          	   	   C   sŒ   d}|d u r
t | ƒ}t| ||||||d�\} }t|ƒr$|jj| ||d�S t|ƒr=|dkr.d n|}|jj| |dddd|d�S tjj| ||d�S )NTrd   )rz   ry   r   F©ÚrtolÚatolÚ	equal_nanrf   rD   )r   rs   r   ÚtestingÚassert_array_equalr   Úassert_closer5   )	ro   rp   re   rf   rg   rh   rz   rI   rq   rF   rF   rG   r     s    
ýÿr   )r|   r}   re   rf   rg   rh   rz   rI   c             	   C   sÒ   d}
|	d u r
t | ƒ}	t| ||	||||d�\} }|	 | jd¡}|d u r0|r0|	 | j¡jd d }n|d u r6d}t|	ƒrE|	jj| ||||d�S t	|	ƒr^|dkrOd n|}|	jj
| |||dd	|d
�S tjj| ||||d�S )NTrd   )úreal floatingúcomplex floatingç      à?é   gH¯¼šò×z>)r|   r}   rz   ry   Fr{   )r   rs   Úisdtyper<   ÚfinfoÚepsr   r   Úassert_allcloser   r�   r5   )ro   rp   r|   r}   re   rf   rg   rh   rz   rI   rq   ÚfloatingrF   rF   rG   r   $  s2   
ýÿÿÿr   )re   rf   rg   rh   rz   ÚverboserI   c          
   	   C   sŽ   d}	|d u r
t | ƒ}t| ||||||d�\} }t|ƒr%|jj| |||d�S t|ƒr=| jjdkr3|  ¡ } |jjdkr=| ¡ }t	jj| |||d�S )NTrd   )rz   r‹   r,   )
r   rs   r   r   Úassert_array_lessr   r
   rk   r,   r5   )
ro   rp   re   rf   rg   rh   rz   r‹   rI   rq   rF   rF   rG   r    F  s(   
ý
ÿ
ÿr    é   c                 O   ó6   ddd|   }}t | |g|¢R ||dddœ|¤ŽS ©zPBackwards compatible replacement. In new code, use xp_assert_close instead.
    r   g      ø?é
   F)r}   r|   rf   rg   ©r   ©ro   rp   ÚdecimalÚargsÚkwdsr|   r}   rF   rF   rG   r   _  ó   þÿþr   é   c                 O   rŽ   r�   r‘   r’   rF   rF   rG   r   h  r–   r   Úparamc                 C   s   d| ›d�S )Nz
Providing z$ is only supported for numpy arrays.rF   )r˜   rF   rF   rG   r*   q  s   r*   c                 C   s   |  | jd¡S ©Nrƒ   )r†   r<   rc   rF   rF   rG   r   u  s   r   c           
      C   s@  g }t | ƒr1|dg7 }ddl}|j ¡ }td|ƒD ]
}|d|› �g7 }q|jj ¡ r/|dg7 }|S t| ƒrQddl	}|jj
 ¡ }td|ƒD ]
}|d|› �g7 }qD|S t| ƒr�ddl}|jdd�}td|ƒD ]
}|d|› �g7 }qd|jdd�}td|ƒD ]
}|d	|› �g7 }qz|jd
d�}	td|	ƒD ]
}|d|› �g7 }q�|S dgS )z<Returns a list of available devices for the given namespace.r,   r   Nzcuda:Úmps)Úbackendzcpu:Úgpuzgpu:Útpuztpu:)r   ÚtorchÚcudaÚdevice_countr0   Úbackendsrš   Úis_availabler   ÚcupyÚruntimeÚgetDeviceCountr   Újax)
rI   Údevicesrž   Únum_cudarB   r£   r¦   Únum_cpuÚnum_gpuÚnum_tpurF   rF   rG   r   y  s:   


r   c                 C   s<   t | ƒrddl}|jS t| ƒrddl}|jS t| ƒr| S dS )a  Return the `scipy`-like namespace of a non-NumPy backend

    That is, return the namespace corresponding with backend `xp` that contains
    `scipy` sub-namespaces like `linalg` and `special`. If no such namespace
    exists, return ``None``. Useful for dispatching.
    r   N)r   ÚcupyxÚscipyr   r¦   r   )rI   r¬   r¦   rF   rF   rG   r   �  s   r   Úsourcec               C   sB   |d u rt |ƒn|}tt| jƒƒ}| |¡}||g }| | |¡S rO   )r   Úlistr0   ÚndimÚpopÚpermute_dims)rb   r®   rI   ÚaxesÚtemprF   rF   rG   r$   µ  s
   

r$   Úx1Úx2c               C   s4   |d u r	t | |ƒn|}| | ¡}| |dk|| ¡S )Nr   )r   ÚabsÚwhere)rµ   r¶   rI   Úabs_x1rF   rF   rG   r"   Ã  s   
r"   c               C   s„   |d u rt | ƒn|}t|ƒr| | ¡S | | ¡}|jd| jd�}| | dk||¡}| | dk | |¡}| | | ¡|j| |¡}|S )Né   ©r<   r   )	r   r   ÚsignÚ
zeros_liker_   r<   r¸   ÚisnanÚnan)rb   rI   r¼   ÚonerF   rF   rG   r'   Ì  s   

r'   é   )ÚaxisÚkeepdimsÚordrI   rÂ   rÃ   rÄ   c               C   sx   |d u rt | ƒn|}tr2t|dƒr|jj| |||d�S |dkr#tdƒ‚|j| | ¡|  ||d�d S tjj	| |||d�S )NÚlinalg)rÂ   rÃ   rÄ   rÁ   z‚only the Euclidean norm (`ord=2`) is currently supported in `xp_vector_norm` for backends not implementing the `linalg` extension.)rÂ   rÃ   r„   )rÄ   rÂ   rÃ   )
r   r   ÚhasattrrÅ   Úvector_normr3   ÚsumÚconjr5   Únorm)rb   rÂ   rÃ   rÄ   rI   rF   rF   rG   r+   Ø  s   
ÿr+   c               C   s    |d u rt | ƒn|}| | d¡S )N)éÿÿÿÿ)r   Úreshaperc   rF   rF   rG   r%   ò  s   r%   c               C   s0   |d u rt | ƒn|}| | jd¡r| | ¡S | S r™   )r   r†   r<   Úrealrc   rF   rF   rG   r&   ú  s   r&   rË   )rÂ   rI   rw   Úindicesc               C   sJ   |d u rt | ƒn|}t|ƒr|j| ||d�S t|ƒrtdƒ‚| | ||¡S )N)Údimz2Array API standard does not define take_along_axis)r   r   Útake_along_dimr   ÚNotImplementedErrorÚtake_along_axis)rw   rÎ   rÂ   rI   rF   rF   rG   r)     s   r)   )Úensure_writeableÚforce_floatingrI   c              
      s¢  ˆ d u rt |Ž nˆ ‰ dd„ |D ƒ}dd„ |D ƒ}ˆ  d¡j}dd„ |D ƒ}zˆ j|Ž }|r9ˆ  |d¡r9ˆ  ||¡}W n) tyc   ‡ fdd„|D ƒ}|rWˆ jg |¢|‘R Ž }n
|r\|}nˆ j|Ž }Y nw dd	„ |D ƒ}	zt|	ƒd
krwtj|	Ž n|d j	}
W n t
y� } zd}t
|ƒ|‚d }~ww g }|D ]9}|d u r¡| |¡ q•|j	|
krºtˆ ƒr®ddini }ˆ j||
fi |¤Ž}|j|ksÁ| rÉˆ j||dd�}| |¡ q•|S )Nc                 S   s$   g | ]}|d urt |dd�n|‘qS )NT)rW   )r   ©rP   ÚargrF   rF   rG   rQ     s   $ z(xp_broadcast_promote.<locals>.<listcomp>c                 S   rN   rO   rF   rÕ   rF   rF   rG   rQ     rR   g      ð?c                 S   s   g | ]}|j ‘qS rF   r»   rÕ   rF   rF   rG   rQ     ó    Úintegralc                    s   g | ]
}ˆ   |d ¡s|‘qS )rØ   )r†   )rP   r<   ra   rF   rG   rQ   !  s    

ÿc                 S   s   h | ]}|j ’qS rF   )rm   rÕ   rF   rF   rG   Ú	<setcomp>+  r×   z'xp_broadcast_promote.<locals>.<setcomp>rº   r   z/Array shapes are incompatible for broadcasting.rW   T)r]   )r   r_   r<   Úresult_typer†   r8   r1   r5   Úbroadcast_shapesrm   r3   Úappendr   rn   Úastype)rÓ   rÔ   rI   r”   Úargs_not_noneÚdefault_floatÚdtypesr<   Úfloat_dtypesÚshapesrm   ÚerE   ÚoutrÖ   ÚkwargsrF   ra   rG   Úxp_broadcast_promote  sR   
€
€øÿ
€þ

ræ   c                 C   sX   |d u rt | ƒn|}| j}| ||j¡r| | |j¡} | S | |d¡r*| | |j¡} | S )Nr‚   )r   r<   r†   Úfloat32rÝ   Ú	complex64Ú
complex128)rw   rI   Ú	arr_dtyperF   rF   rG   Úxp_float_to_complexH  s   ýrë   c                 C   s   t | ƒr|  ¡ S | jS )z@Query the namespace-dependent default floating-point dtype.
    )r   Úget_default_dtypeÚfloat64ra   rF   rF   rG   Úxp_default_dtypeV  s   rî   )NNN)r�   )r—   rO   )HÚ__doc__ÚosÚtypesr   Útypingr   r   r   r	   r5   Únumpy.typingÚnptÚ
scipy._libr   Úscipy._lib.array_api_compatr   r   r(   rT   r
   r#   r   r   r   r   r   r   r   r   r   r   Ú__all__ÚenvironÚgetr   ÚstrÚboolÚ__annotations__r   rS   r-   r.   rH   rM   r   r   r!   rs   rj   r   r   r    r   r   r*   r   r¯   r   r   Úintr$   r"   r'   rt   Úfloatr+   r%   r&   r)   ræ   rë   rî   rF   rF   rF   rG   Ú<module>   sÖ    ,þ/
'üøÿþ
ýüúùø	
÷ /þÿþ"
ÿ

		$üÿþü
ü$ 	
üÿþýü
ü  	
ýÿþý
ý7