
    S_f1                    z   U d Z ddlmZ ddlZddlZddlZddlmZ ddl	m
Z
mZmZ g dZej                  j                  dd      Zd	ed<   ej                  j                  d
d      ZeedZd Zd Zd Z	 d!ddddZdddZdddZd Zd Zd Z	 d"dZd Zd Z	 	 d#dZ 	 	 d$dZ!	 	 d%dZ"dddZ#d Z$d  Z%y)&a6  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
    )annotationsN)array_api_compat)is_array_api_objsizenumpy)array_namespace_asarrayr   SCIPY_ARRAY_APIFz
str | boolSCIPY_DEVICEcpu)r
   r   c                T   t        t        |             D ]w  }| |   }t        |t        j                  j
                        rt        d      t        |t        j                        rt        d      t        |t        j                  t        j                  f      re|j                  }t        j                  |t        j                        rt        j                  |t        j                        rt        d|d      t        |      r	 t        j                  |      }|j                  }t        j                  |t        j                        s5t        j                  |t        j                        sd|d}t        |      || |<   z | S # t        $ r t        d      w xY w)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
    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
isinstancenpmaMaskedArray	TypeErrormatrixndarraygenericdtype
issubdtypenumberbool_r   
asanyarray)arraysiarrayr   messages        5lib/python3.12/site-packages/scipy/_lib/_array_api.pycompliance_scipyr"   %   sg    3v; q	eRUU../VWWryy)NOOebjj"**56KKEMM%3r}}UBHH7U"9% CS !T U U!%(7e, KKEMM%3r}}UBHH7UGy QG H   ((F1I12 M  7 !6 7 77s   FF'c                    d}	 |j                  |j                  |             st        |      y# t        $ r t        |      w xY w)zCheck for NaNs or Infs.z#array must not contain infs or NaNsN)allisfinite
ValueErrorr   )r   xpmsgs      r!   _check_finiter)   K   sI    
/Cvvbkk%()S/! * os	   +0 Ac                     t         d   st        S | D cg c]  }||	 } }t        |       } t        j                  |  S c c}w )a0  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
   )_GLOBAL_CONFIG	np_compatr"   r   r   )r   r   s     r!   r   r   U   sK    4 +,!'=5+<e=F=f%F++V44	 >s   ??)r'   check_finitec                  |t        |       }|j                  dv rG|du rt        j                  | ||      } nt        j                  | ||      } |j	                  |       } n	 |j	                  | ||      } |rt        | |       | S # t
        $ r1 t        |j	                  d            }|j	                  | ||      } Y Iw xY w)a  SciPy-specific replacement for `np.asarray` with `order` and `check_finite`.

    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.
    >   r   !scipy._lib.array_api_compat.numpyT)orderr   )r   copy   )r   __name__r   r   asarrayr   r)   )r   r   r0   r1   r'   r-   
coerced_xps          r!   r	   r	   z   s     
zU#	{{DD4<HHU%u=EJJuE?E 

5!	FJJuEJ=E
 eR L  	F(A7J&&uE&EE	Fs   $B 7CCr'   c                   |t        |       }|j                  |       } | j                  |k  r!|j                  | d      } t	        | ||      } | S )z9Recursively expand the dimension to have at least `ndim`.r   axisndimr'   )r   r4   r;   expand_dims
atleast_nd)xr;   r'   s      r!   r=   r=      sN    	zQ


1Avv}NN11N%qt+H    c               8    |t        |       }t        | 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.
    T)r1   r'   )r   r	   r>   r'   s     r!   r1   r1      s"    * 
zQADR((r?   c                    | j                   dv S )N)r   r/   r3   r6   s    r!   is_numpyrD          ;;HHHr?   c                    | j                   dv S )N)cupyz scipy._lib.array_api_compat.cupyrC   r6   s    r!   is_cupyrH      s    ;;FFFr?   c                    | j                   dv S )N)torchz!scipy._lib.array_api_compat.torchrC   r6   s    r!   is_torchrK      rE   r?   c                *   d}|rt        | |       |j                  |      }|r"d}| j                  |j                  k(  sJ |       |r/d}| j                  |j                  k(  sJ |       t	        | ||       |j                  || j                        }|S )NTzDdtypes do not match.
Actual: {actual.dtype}
Desired: {desired.dtype}zDShapes do not match.
Actual: {actual.shape}
Desired: {desired.shape})_assert_matching_namespacer4   r   shape_check_scalarbroadcast_to)actualdesiredr'   check_namespacecheck_dtypecheck_shape__tracebackhide___msgs           r!   _strict_checkrX      s    "673jj!GW||w}},2d2,W||w}},2d2,fgr*oogv||4GNr?   c                    d}t        | t              r| n| f} t        |      }| D ]5  }t        |      }d|j                   d|j                   }||k(  r0J |        y )NTz!Namespaces do not match.
Actual: z

Desired: )r   tupler   r3   )rQ   rR   rV   desired_spacearr	arr_spacerW   s          r!   rM   rM      s{    !&%0VviF#G,M 0#C(	$--. /)2235 M)/4/)0r?   c                   d}|j                   dk7  st        |      sy |d   }dt        |        dt        |       }|j                  |       r|j                  |      s*|j                  |       s|j                  |      rJ |       y y )NT zTypes do not match:
 Actual: z
 Desired: )rN   rD   typeisscalar)rQ   rR   r'   rV   rW   s        r!   rO   rO      s     }}(2, bkG+DL>d7m_UDKKBKK$8KK'G0DMHLM F0D %9r?   c           	     D   d}|t        |       }t        | |||||      }t        |      r|j                  j	                  | ||      S t        |      r+|dk(  rd n|}|j                  j                  | |dddd|      S t        j                  j	                  | ||      S )NTrS   rT   rU   )err_msg r   Frtolatol	equal_nanrT   r(   )r   rX   rH   testingassert_array_equalrK   assert_closer   )rQ   rR   rS   rT   rU   rd   r'   rV   s           r!   xp_assert_equalrm     s    	zV$FGR(3NGr{zz,,VWg,NN	" "R-$Wzz&&vwQQRV38g ' G 	G::(('(JJr?   c	           	     L   d}	|t        |       }t        | |||||      }t        |      r |j                  j	                  | ||||      S t        |      r+|dk(  rd n|}|j                  j                  | |||dd|      S t        j                  j	                  | ||||      S )NTrc   )rg   rh   rd   re   Frf   )r   rX   rH   rj   assert_allcloserK   rl   r   )
rQ   rR   rg   rh   rS   rT   rU   rd   r'   rV   s
             r!   xp_assert_closerp     s    	zV$FGR(3NGr{zz))&'/3W * F 	F	"!R-$Wzz&&vwT155g ' W 	W::%%fgD+/ & B Br?   c                   d}|t        |       }t        | |||||      }t        |      r|j                  j	                  | |||      S t        |      rR| j                  j                  dk7  r| j                         } |j                  j                  dk7  r|j                         }t        j                  j	                  | |||      S )NTrc   )rd   verboser   )
r   rX   rH   rj   assert_array_lessrK   devicer`   r   r   )	rQ   rR   rS   rT   rU   rd   rr   r'   rV   s	            r!   xp_assert_lessru   0  s    	zV$FGR(3NGr{zz++FG4;W , N 	N	"==&ZZ\F>>%'kkmG::''07 ( J Jr?   c               N   |t        |       }t        | |      }|j                  ||j                        }t	        |d|      }|j                  ||      }|j                  |d      }|j                  d   dz
  }|dk  rt        j                  dt        d	       d
}||d d d f   z  }|j                  }|j                  |j                  d      r|j                  |      }||z  }||z  }t        d t!        |j                        D              }|j#                  ||      S )Nr6      r:   )r      r8   r   z!Degrees of freedom <= 0 for slice)
stacklevelg        complex floatingc              3  2   K   | ]  \  }}|d k(  s|  yw)rx   Nr_   ).0r9   lengths      r!   	<genexpr>zcov.<locals>.<genexpr>[  s     L,$!Ls   )r   r1   result_typefloat64r=   r4   meanrN   warningswarnRuntimeWarningTisdtyper   conjrZ   	enumeratesqueeze)	r>   r'   Xr   avgfactX_Tcaxess	            r!   covr   C  s   	zQQ2ANN1bjj)E11$A


1E
"A
''!!'
C771:>Dqy9$	4QWA
##C	zz#))/0ggcl	CAIAL)AGG*<LLD::ad:##r?   c                    d| dS )Nz
Providing z$ is only supported for numpy arrays.r_   )params    r!   xp_unsupported_param_msgr   _  s    y DEEr?   c                :    |j                  | j                  d      S )Nrz   )r   r   rA   s     r!   
is_complexr   c  s    ::agg122r?   )NNN)TTT)TTTre   N)gHz>r   TTTre   N)TTTre   TN)&__doc__
__future__r   osr   r   r   
scipy._libr   scipy._lib.array_api_compatr   r   r,   __all__environgetr
   __annotations__r   r+   r"   r)   r   r	   r=   r1   rD   rH   rK   rX   rM   rO   rm   rp   ru   r   r   r   r_   r?   r!   <module>r      s   # 	   '  2 !jjnn->F Fzz~~ne4 ' #L"5L )-#48u#L #   )6IGI
 GK*	0M2 HL59K$ JNGKB$ GKBFJ&  $8F3r?   