
    TiG4                        d dl Z d dlZd dlZd dl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  ee            ee           ej                            ed          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S )    N)
_GLOBAL_CONFIGarray_namespace_asarrayxp_copyxp_assert_equalis_numpy	np_compatxp_default_dtypexp_result_typeis_torch)array_api_extra)r   )lazy_xp_functionSCIPY_ARRAY_APIzDArray 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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t          d<   t          ||          }d|j        v sJ dt          d<   d S )Nr         zarray_api_compat.numpyFr   T)nparrayr   __name__r   )selfxyxps       w/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/scipy/_lib/tests/test_array_api.pytest_array_namespacez!TestArrayAPI.test_array_namespace   s    x			""BHYYY$7$71Q""'2;6666,1()Q""'2;6666,0()))    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_likes7   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          |          st          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           dS # 1 swxY w Y   dS 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   NzMultiple namespacesr)   r   )r%   r   r   r-   r.   r/   r   r9   )r   r   r   s      r   test_array_and_array_likes_mixz+TestArrayAPI.test_array_and_array_likes_mixK   s    JJqMMq!!R''''q!$$****q!$$****tQ''2----|| 	0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 0 0 0	0 	0s$   B..B25B2#DDD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)r-   r.   r/   xpx
atleast_ndr3   s     r   test_array_api_extra_hookz&TestArrayAPI.test_array_api_extra_hook^   s     @]9C000 	* 	*N5q))))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s   AA	A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   idr/   
ValueError)r   r   _xpr   r   s        r   	test_copyzTestArrayAPI.test_copyf   s    : 	$ 	$C

999%%Ac"""A a55BqEE>>>>$! tqt||||| z*   	$ 	$s   A,,B ?B dtype)int32r9   float32float64shape )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   rI   rN   )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.)getattrbroadcast_tor%   r	   dictzipr   r   r-   r.   AssertionErrorrI   namestrrM   )r   r   rI   rM   r   r   kwarg_namesoptionss           r   test_strict_checkszTestArrayAPI.test_strict_checkst   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rT   Array-ness does not match:.*r)   *   rN   )r   rL   r%   r-   r.   rY   r9   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   Tra   r`   zResult is a NumPy 0d-array.*r)   rc   rb   )xp_assert_equal_no_0drL   r%   r-   r.   rY   r9   rd   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%   rI   r:   s     r   test_default_dtypezTestArrayAPI.test_default_dtype   s.    ##rzz"~~';;;;;;;r    N)r   
__module____qualname__r   r'   r-   markfilterwarningsr5   skip_xp_backendsr;   r=   rB   rH   parametrizer^   rf   ri   rl   rN   r    r   r   r      s       1 1 1      [ =>># # ?># [!!$}!EE: : FE:&0 0 0&* * *$ $ $ [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   rk         ?      ?r   rk   rs   zVint8 int16 int32 int64 uint8 uint16 uint32 uint64 float32 float64 complex64 complex128c           	      b    g | ],}t          j        d gt          t           |                    -S )r   rP   )r   r%   rU   ).0ts     r   
<listcomp>rw      s1    	?	?	?A"*aSA
/
/
/	?	?	?r    c                     t          | t          j                  r|                    |           S t          | t                    rt          ||           S | S )N)
isinstancer   ndarrayr%   r[   rU   )r   r   s     r   convert_typer{      sM     !RZ   zz!}}	As		 r1~~Hr    c                     t          j        |           st          | t                    r|                    |           n| } t          | d|           }|                    |d          S )NrI   )zreal floatingzcomplex floating)r   isscalarry   listr%   rU   isdtype)r   r   rI   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{   ry   r~   r%   result_type	Exceptiontyper[   r-   r.   reescaper   )	r   r   r   x_refy_ref	dtype_refexpected_errore	dtype_ress	            r   test_xp_result_type_no_forcer     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#277rk   r   r   r)   r"   T)force_floatingr   )r   ry   r[   r   r}   r-   skipr{   r~   r%   r   r/   r   appendr   r   r.   r   )
r   r   r   r   r   r   r   argsr   r   s
             r   "test_xp_result_type_force_floatingr     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>)#r   numpyr   r-   scipy._lib._array_apir   r   r   r   r   r   r	   r
   r   r   
scipy._libr   r@   scipy._lib._array_api_no_0drh   "scipy._lib.array_api_extra.testingr   ro   skipifr   scalarslistssplittypesarraysr{   r   rr   r   r   rN   r    r   <module>r      s   				                             . - - - - - P P P P P P ? ? ? ? ? ?           '899U  W WP< P< P< P< P< P< P<W WP<f 


rdWI
016  
@	?	?	?	?  D D D go5>??go5>??" " @? @?"0 go5>??go5>??$" $" @? @?$" $" $"r    