
    Pi@                     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	m
Z
mZmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlmZ ej        j        Z ee	            ee
           ej                            e d	           G d
 d                      Z g dZ!dgdgdggZ"d#                                Z$d e$D             Z%d Z&d Z'ej        (                    de!e"z   e$z   e%z             ej        (                    de!e"z   e$z   e%z             d                         Z)ej        (                    de!e"z   e$z   e%z             ej        (                    de!e"z   e$z   e%z             d                         Z*dgZ+d Z,ej        (                    d e,                      d             Z-dS )    N)import_module)SCIPY_ARRAY_APIarray_namespace_asarrayxp_copyxp_assert_equalis_numpy	np_compatxp_default_dtypexp_result_typeis_torchxp_capabilities_table_xp_copy_to_numpy)is_named_function_like_object)array_api_extra)r   )lazy_xp_functionzDArray API test; set environment variable SCIPY_ARRAY_API=1 to run it)reasonc            
          e Zd Zd Zd Zej                            d          d             Zej        	                    dd          d             Z
d	 Zd
 Zd Zd Zd Zej                            dg d          ej                            dg dg dddgddggg          d                         Zej                            dg d          ej                            dddg          d                         Zej        	                    dd          d             Zej        	                    dd          d             Zd ZdS ) TestArrayAPIc                     t          j        g d          t          j        g d          }}t          ||          }d|j        v sJ d S )Nr         zarray_api_compat.numpy)nparrayr   __name__)selfxyxps       s/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/scipy/_lib/tests/test_array_api.pytest_array_namespacez!TestArrayAPI.test_array_namespace   sO    x			""BHYYY$7$71Q""'2;666666    c                     t          g d|          t          t          j        d          |          }}|                    g d          }t	          ||           t	          ||           d S )Nr   r       )r   r   arangeasarrayr   )r   r    r   r   refs        r!   test_asarrayzTestArrayAPI.test_asarray#   sl    			b)))8BIaLLR+H+H+H1jj##33r#   zignore: the matrix subclassc                    d}t          j        t          |          5  t          t          j                            d          t	          j        d                     d d d            n# 1 swxY w Y   d}t          j        t          |          5  t          t	          j        d          t	          j        d                     d d d            n# 1 swxY w Y   d}t          j        t          |          5  t          t                      g           d d d            n# 1 swxY w Y   t          j        t          |          5  t          d           d d d            d S # 1 swxY w Y   d S )Nz0of type `numpy.ma.MaskedArray` are not supportedmatchr   z(of type `numpy.matrix` are not supported/only boolean and numerical dtypes are supportedabc)	pytestraises	TypeErrorr   r   mar   matrixobjectr   msgs     r!   test_raiseszTestArrayAPI.test_raises)   s3   @]9C000 	9 	9BEKKNNBHQKK888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 9]9C000 	7 	7BHQKK1666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 @]9C000 	( 	(VXXJ'''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(]9C000 	# 	#E"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#sH   A A**A.1A.5CCC;D$$D(+D(
E''E+.E+TzArray-likes)np_onlyr   c                 Z   t          g d          |u sJ t          d          |u sJ t          ddd          |u sJ t          d          |u sJ t          t          j        d                    |u sJ t          g dd          |u sJ t                      |u sJ t          d          |u sJ t          dd          |u sJ t          dd          |u sJ t          t          j        ddg          ddg          |u sJ t          t          j        d          ddg          |u sJ dS )zuTest that if all parameters of array_namespace are Array-likes,
        the output is array_api_compat.numpy
        r   r   r   r&   N   )r   r   int64r(   r   r    s     r!   test_array_likeszTestArrayAPI.test_array_likes9   sr   
 yyy))R////y))R////q!Q''2----q!!R''''rx{{++r1111yyy!,,2222  B&&&&t$$****q$''2----tQ''2---- rz1a&11Aq6::b@@@@rx{{QF33r999999r#   c                    |                     d          }t          |          |u sJ t          |d          |u sJ t          d|          |u sJ t          d|          |u sJ t          |          rt          |ddg          |u sJ dS t          j        t
          d          5  t          |ddg           ddd           n# 1 swxY w Y   t          j        t
          d          5  t          |t          j        d                     ddd           n# 1 swxY w Y   t          j        t
          d          5  t          |t          j        d                     ddd           n# 1 swxY w Y   t          j        t
          d          5  t          |t          j	        d                     ddd           dS # 1 swxY w Y   dS )a  Test that if there is at least one Array API object among
        the parameters of array_namespace, and all other parameters
        are scalars, the output is its namespace.

        If there are non-scalar Array-Likes, raise as in array-api-compat.
        r   Nr   zMultiple namespacesr,   )
r(   r   r	   r0   r1   r2   r   r<   float64
complex128)r   r    r   s      r!   test_array_and_array_likes_mixz+TestArrayAPI.test_array_and_array_likes_mixM   s    JJqMMq!!R''''q!$$****q!$$****tQ''2----B<< 	5"1q!f--333333y0EFFF + +Aq6***+ + + + + + + + + + + + + + +y0EFFF 0 028A;;///0 0 0 0 0 0 0 0 0 0 0 0 0 0 0y0EFFF 2 22:a==1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 y0EFFF 5 52=#3#34445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5sH   'CC
C
,#DD"D#E00E47E4#GG
G
c                     d}t          j        t          |          5  t          j        dd           ddd           dS # 1 swxY w Y   dS )znTest that the `array_namespace` function used by
        array-api-extra has been overridden by scipy
        r.   r,   r/   r   )ndimN)r0   r1   r2   xpx
atleast_ndr6   s     r!   test_array_api_extra_hookz&TestArrayAPI.test_array_api_extra_hookh   s     @]9C000 	* 	*N5q))))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s   AA	Ac                    t          j        d          }t          j        d          } |                    |                    |j        d                    |                    d                    }t          |          |u sJ dS )zTest array_namespace special case for JAX zero-gradient arrays, which are
        numpy arrays but must be treated as JAX arrays.
        See matching code and tests in array_api_compat.
        jaxz	jax.numpyT)	allow_intr;   N)r0   importorskipvmapgradfloat32r'   r   )r   rI   r    jax_zeros       r!   test_jax_zero_gradient_arrayz)TestArrayAPI.test_jax_zero_gradient_arrayp   sz    
 !%(( --A388CHHRZ4H@@AA"))A,,OOx((B......r#   c                 d   t          j        dt          j        g                     }t          j        t
          d          5  t          |           ddd           n# 1 swxY w Y   t          j        t
          d          5  t          ||g           ddd           dS # 1 swxY w Y   dS )z{A void dtype that is not a jax.float0 must not be caught in the
        special case for JAX zero-gradient arrays.
        r   dtypez!only boolean and numerical dtypesr,   N)r   emptyrS   r0   r1   r2   r   )r   voids     r!   )test_void_but_not_jax_zero_gradient_arrayz6TestArrayAPI.test_void_but_not_jax_zero_gradient_array{   s%    x"...]9,OPPP 	" 	"D!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"]9,OPPP 	* 	*T4L)))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s$   A  A$'A$B%%B),B)c                 
   |d fD ]}}|                     g d          }t          ||          }t          |          t          |          k    sJ 	 d|d<   |d         |d         k    sJ g# t          t          f$ r Y zw xY wd S )Nr   r   r&   r%   
   r   )r(   r   idr2   
ValueError)r   r    _xpr   r   s        r!   	test_copyzTestArrayAPI.test_copy   s    : 	$ 	$C

999%%Ac"""A a55BqEE>>>>$! tqt||||| z*   	$ 	$s   A,,B ?B rS   )rN   r@   	complex64rA   int32r<   datar   rX   r   r&   c                    t          ||          }t          t          |          }|                    ||          }t          |          }t	          |t          j                  sJ |j        |k    sJ |j        |j        k    sJ t          j        	                    |t          j        ||                     t          |          r"t          |          t          |          k    sJ d S d S )NrR   )getattrr   r(   r   
isinstancendarrayrS   shapetestingassert_equalr	   rZ   )r   r    r`   rS   xp_dtypenp_dtyper   r   s           r!   test_copy_to_numpyzTestArrayAPI.test_copy_to_numpy   s     2u%%2u%%JJt8J,,a  !RZ(((((w(""""w!'!!!!

2:d(#C#C#CDDDB<< 	"a55BqEE>>>>	" 	"!>r#   )r_   r<   rN   r@   re    )r&   c                    t          ||          }|                    |                    d|          |          }|r|n|d         }t          j        d          d         }g d}t	          t          |g d                    }t          |          rt          ||fi | nt          j	        t          d          5  t          ||fi | d d d            n# 1 swxY w Y   t          j	        t          d          5  t          ||fi | d d d            n# 1 swxY w Y   t	          t          |g d	                    }|j        j        t          |j                  v rt          ||fi | nAt          j	        t          d
          5  t          ||fi | d d d            n# 1 swxY w Y   t	          t          |g d                    }|j        |j        k    rt          ||fi | nTt          j	        t          d          5  t          ||                    |          fi | d d d            n# 1 swxY w Y   t	          t          |g d                    }t          |          r |j        |j        k    rt          ||fi | d S t          |          rDt          j	        t          d          5  t          ||fi | d d d            d S # 1 swxY w Y   d S d S )Nr   rR   rk   )check_namespacecheck_dtypecheck_shapecheck_0d)TFFFz)Namespace of desired array does not matchr,   z3Namespace of actual and desired arrays do not match)FTFFzdtypes do not match.)FFTFzShapes do not match.)FFFTzArray-ness does not match.)rb   broadcast_tor(   r
   dictzipr	   r   r0   r1   AssertionErrorrS   namestrre   )r   r    rS   re   r   r   kwarg_namesoptionss           r!   test_strict_checkszTestArrayAPI.test_strict_checks   sy    E""OOBJJqJ66>>!AAAbEa  $SSSs;(C(C(CDDEEB<< 	1Aq,,G,,,,A   1 1  100000	1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 K   1 1  100000	1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 s;(C(C(CDDEE7<3qw<<''Aq,,G,,,,~5KLLL 1 11000001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 s;(C(C(CDDEE7agAq,,G,,,,~5KLLL = =2::a==<<G<<<= = = = = = = = = = = = = = = s;(C(C(CDDEEB<< 	1AGqw..Aq,,G,,,,,b\\ 	1~5QRRR 1 11000001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1	1 	1sZ   >CC C?DD!DF))F-0F-"H;;H?H?>KK!KzScalars only exist in NumPyc                    t          |                    d          |                    d                     t          |                    d          |                    d                     t          |                    d          |                    d          d           t          |                    d          |                    d          d           d}t          j        t
          |          5  t          |                    d          |                    d                     d d d            n# 1 swxY w Y   t          j        t
          |          5  t          |                    d          |                    d                     d d d            n# 1 swxY w Y   t          j        t
          |          5  t          |                    d          |                    d                     d d d            n# 1 swxY w Y   t          j        t
          |          5  t          |                    d          |                    d                     d d d            n# 1 swxY w Y   t          |                    d          |                    d          d           t          |                    d          |                    d          d           t          |                    d          dd           t          d|                    d          d           t          |                    d          dd           t          d|                    d          d           t          |                    d          |                    d          d                    d S )	Nr           Frp   Array-ness does not match:.*r,   *   rk   )r   r@   r(   r0   r1   rt   r<   r   r    messages      r!   test_check_scalarzTestArrayAPI.test_check_scalar   s    	

1rzz!}}555

2

2777

1rzz!}}uEEEE

2

2GGGG 1]>999 	; 	;BJJrNNBJJqMM:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;]>999 	; 	;BJJqMM2::b>>:::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;]>999 	: 	:BJJrNNBHHRLL999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:]>999 	: 	:BHHRLL"**R..999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	

2

1FFFF

1rzz"~~FFFF

2U;;;;BJJrNNU;;;;

2U;;;;BJJrNNU;;;; 	

1rzz"~~b'9:::::sH   :7D==EE#7F&&F*-F*7HHH57I88I<?I<c                    t          |                    d          |                    d                     t          |                    d          |                    d          d           t          |                    d          |                    d          d           d}t          j        t
          |          5  t          |                    d          |                    d                     d d d            n# 1 swxY w Y   t          j        t
          |          5  t          |                    d          |                    d                     d d d            n# 1 swxY w Y   t          j        t
          |          5  t          |                    d          |                    d                     d d d            n# 1 swxY w Y   t          |                    d          |                    d                     t          |                    d          |                    d                     d}t          j        t
          |          5  t          |                    d          |                    d          d           d d d            n# 1 swxY w Y   t          j        t
          |          5  t          |                    d          |                    d          d           d d d            n# 1 swxY w Y   t          j        t
          |          5  t          |                    d          |                    d          d           d d d            n# 1 swxY w Y   t          j        t
          |          5  t          |                    d          |                    d          d           d d d            n# 1 swxY w Y   t          d|                    d                     t          d|                    d                     d S )	Nr   Tr|   r{   zResult is a NumPy 0d-array.*r,   r~   r}   )xp_assert_equal_no_0dr@   r(   r0   r1   rt   r<   r   s      r!   test_check_scalar_no_0dz$TestArrayAPI.test_check_scalar_no_0d   s{    	bjjmmRZZ]];;;bjjmmRZZ]]TJJJJbjjnnbjjnntLLLL 1]>999 	B 	B!"**R.."**R..AAA	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B]>999 	A 	A!"**R.."**Q--@@@	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A]>999 	@ 	@!"**R.."((2,,???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	bjjmmRZZ^^<<<bhhrllBJJrNN;;; 1]>999 	P 	P!"**R.."**Q--$OOOO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P]>999 	P 	P!"**Q--B$OOOO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P]>999 	N 	N!"**R.."((1++MMMM	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N]>999 	N 	N!"((1++rzz"~~MMMM	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	b"**R..111b"**R..11111s~   7DDD-7E00E47E47GG G-9J22J69J69LL!$L!9NNN.9O33O7:O7c                 `    t          |          |                    d          j        k    sJ d S )N      ?)r   r(   rS   r=   s     r!   test_default_dtypezTestArrayAPI.test_default_dtype  s.    ##rzz"~~';;;;;;;r#   N)r   
__module____qualname__r"   r*   r0   markfilterwarningsr8   skip_xp_backendsr>   rB   rG   rP   rV   r]   parametrizerj   ry   r   r   r   rk   r#   r!   r   r      s&       7 7 7
      [ =>># # ?># [!!$}!EE: : FE:&5 5 56* * *	/ 	/ 	/* * *$ $ $ [KKK  [Q			QFQF#34 " " 	 " [W&N&N&NOO[Wr4j11*1 *1 21 PO*1X [!!$7T!UU; ; VU;< [!!$7T!UU 2  2 VU 2D< < < < <r#   r   )r   r         ?      ?r   r   r   zVint8 int16 int32 int64 uint8 uint16 uint32 uint64 float32 float64 complex64 complex128c           	      b    g | ],}t          j        d gt          t           |                    -S )r   rR   )r   r(   rb   ).0ts     r!   
<listcomp>r   !  s1    	?	?	?A"*aSA
/
/
/	?	?	?r#   c                     t          | t          j                  r|                    |           S t          | t                    rt          ||           S | S N)rc   r   rd   r(   rv   rb   )r   r    s     r!   convert_typer   $  sM     !RZ   zz!}}	As		 r1~~Hr#   c                     t          j        |           st          | t                    r|                    |           n| } t          | d|           }|                    |d          S )NrS   )zreal floatingzcomplex floating)r   isscalarrc   listr(   rb   isdtype)r   r    rS   s      r!   
is_inexactr   /  sW    QE:a+>+>E

1AAAw""E::eBCCCr#   r   r   c                    t          | |          } t          ||          }t          | t                    r|                    |           n| }t          |t                    r|                    |          n|}	 |                    ||          }d }n5# t
          $ r(}t          |          t          |          f}Y d }~nd }~ww xY w|`t          j	        |d         t          j        |d                             5  t          | ||           d d d            n# 1 swxY w Y   d S t          | ||          }||k    sJ d S )Nr   r   r,   r%   )r   rc   r   r(   result_type	Exceptiontyperv   r0   r1   reescaper   )	r   r   r    x_refy_ref	dtype_refexpected_errore	dtype_ress	            r!   test_xp_result_type_no_forcer   6  s    	QAQA'4007BJJqMMMaE'4007BJJqMMMaE+NN5%00	 + + +q''3q66*+ !]>!,BInQ>O4P4PQQQ 	( 	(1aB''''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(q!+++I	!!!!!!s*   :B 
CC  C>DD!$D!c                 H   t          |          rft          | t                    sQt          |t                    s<t          j        |           r(t          j        |          rt          j        d           t          | |          } t          ||          }t          | t                    r|	                    |           n| }t          |t                    r|	                    |          n|}d }	 |
                    ||d          }n# t          $ rq g }t          ||          r|                    |           t          ||          r|                    |            |j
        g ||	                    d          R  }Y n4t          $ r(}t          |          t          |          f}Y d }~nd }~ww xY w|Nt          j        |d         |d                   5  t#          | ||           d d d            n# 1 swxY w Y   d S t#          | |d|          }	|	|k    sJ d S )	Nz8See 3/27/2024 comment at  data-apis/array-api-compat#277r   r   r   r,   r%   T)force_floatingr    )r   rc   rv   r   r   r0   skipr   r   r(   r   r2   r   appendr   r   r1   r   )
r   r   r    r   r   r   r   argsr   r   s
             r!   "test_xp_result_type_force_floatingr   P  sx    	 PZ3// P:a3E3E PAP#%;q>>PNOOOQAQA'4007BJJqMMMaE'4007BJJqMMMaEN
+NN5%55		 ; ; ;eR   	KKeR   	KK"BN:D:"**S//:::			 + + +q''3q66*+ !]>!,N14EFFF 	( 	(1aB''''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(q!DR@@@I	!!!!!!s1   1D	 	A8F4	F4F//F4G::G>G>zscipy.statsc            
          g } t           D ]i}t          |          }|j        D ]P}t          ||          }t	          |          s"|                     t          j        || d|                      Qj| S )N.)rZ   )tested_modulesr   __all__rb   r   r   r0   param)	functionsmodule_namemoduleru   objs        r!   collect_public_functionsr     s    I% L L{++N 	L 	LD&$''C055 V\#[2I2I42I2IJJJKKKK		L
 r#   funcc                     | t           v sJ d S r   )r   )r   s    r!   test_xp_capabilities_coverager     s    (((((((r#   ).r   numpyr   r0   	importlibr   scipy._lib._array_apir   r   r   r   r   r	   r
   r   r   r   r   r   !scipy._lib._array_api_docs_tablesr   
scipy._libr   rE   scipy._lib._array_api_no_0dr   "scipy._lib.array_api_extra.testingr   r   array_api_backends
pytestmarkskipifr   scalarslistssplittypesarraysr   r   r   r   r   r   r   r   rk   r#   r!   <module>r      s3   				      # # # # # #                           
 L K K K K K - - - - - - P P P P P P ? ? ? ? ? ? [+
           'U  W W}< }< }< }< }< }< }<W W}<@ 


rdWI
016  
@	?	?	?	?  D D D go5>??go5>??" " @? @?"0 go5>??go5>??$" $" @? @?$"Z  	 	 	 !9!9!;!;<<) ) =<) ) )r#   