
    (ph/)                     L   S SK r S SKrS SKrS SKrS SKrS SKJrJrJ	r	  S SK
JrJrJrJrJrJr  S SKJr  S SKJr  S SKrS SKJr  S rS rS	 rS
 r\R8                  R:                  \R8                  R=                  S5      S 5       5       r\R8                  RA                  \ RB                  RE                  S5      =(       a)    \RF                  " \RH                  5      RJ                  S:  (       + SS9 " S S5      5       r&\R8                  RO                  SS9S 5       r(S r)S r*g)    N)assert_equalassert_assert_allclose)_sparsetools
coo_matrix
csr_matrix
csc_matrix
bsr_matrix
dia_matrix)supported_dtypes)check_free_memory)raisesc                     U S-   S-  S-
  $ )z.
Wrap an integer to the interval [-128, 127].
       )ns    V/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/tests/test_sparsetools.pyint_to_int8r      s     Gs?S      c                  @    [        [        [        R                  5        g N)assert_raisesMemoryErrorr   test_throw_errorr   r   r   test_exceptionr      s    +|<<=r   c                    ^^^^	 Sn Sm	Sm[        [        R                  " TT/5      5      m/ m " UUUU	4S jS[        R                  5      n[        U 5       Vs/ s H	  o!" 5       PM     nnU H  nUR                  5         M     U H  nUR                  5         M     T H3  n[        [        R                  " UR                  5       S:H  5      5        M5     g s  snf )N
   d      c                   (   > \ rS rSrU UUU4S jrSrg)test_threads.<locals>.Worker'   c                 `  > TR                  5       n[        T5       H}  n[        R                  " TTTR                  TR
                  TR                  TR                  TR
                  TR                  UR                  UR
                  UR                  5        M     TR                  U5        g r   )copyranger   csr_plus_csrindptrindicesdataappend)selfbjabresr   niters      r   run test_threads.<locals>.Worker.run(   su    A5\))!Q*+((AIIqvv*+((AIIqvv*+((AIIqvvG "
 KKNr   r   N)__name__
__module____qualname____firstlineno__r2   __static_attributes__)r/   r0   r   r1   s   r   Workerr"   '   s    	 	r   r9      )r   npones	threadingThreadr&   startjoinr   alltoarray)
nthreadsr9   _threadsthreadr-   r/   r0   r   r1   s
         @@@@r   test_threadsrG      s     HE
A277Aq6?#AD !!  "'x1AvxG1   qyy{a'()  2s   Cc                      [          Hi  n [        R                  " SS/SS//5      R                  U 5      n[	        XS9n[        UR                  S5      R                  5       US S 2S S24   5        Mk     g )N   r:         dtyper   )r   r;   arrayastyper   r   getcolrB   )rM   adr/   s      r   !test_regression_std_vector_dtypesrR   ;   sg     "XX1v1v&'..u5r' 	QXXa[((*Bq"1"uI6 "r   z!Can't create large array for testc                     [         R                  " [         R                  5      R                  S-   n [	        SU -  S-  S-   5        [         R
                  " U [         R                  S9n[         R
                  " U [         R                  S9n[         R
                  " U [         R                  S9nSUS'   [        X1U44SS	S
9nUR                  5       n[        US//5        g )NrI   	       .Ag      ?rL   rK   )rI   rI   F)shaper%   )
r;   iinfoint32maxr   zerosint8r   rB   r   )nnzrowcolr*   sds         r   test_nnz_overflowrb   F   s    
 ((288

 
 1
$CyC'#-34 ((3bhh
'C
((3bhh
'C88Crww'DDHD*%V%@A			AAur   linux   ztest requires 64-bit Linux)reasonc            
          \ rS rSrSrSrS rS rS r\	R                  R                  S 5       r\	R                  R                  S 5       r\	R                  " S	\	R                  R                  S
9\	R                  " S\	R                  R                  S
9SSS\	R                  " S\	R                  R                  S
9/r\	R                  R                  \	R                  R#                  S\5      S 5       5       r\	R                  R                  \	R                  R#                  S\5      S 5       5       rS rS rS rS rS rS rSrg)TestInt32Overflow[   z
Some of the sparsetools routines use dense 2D matrices whose
total size is not bounded by the nnz of the sparse matrix. These
routines used to suffer from int32 wraparounds; here, we try to
check that the wraparounds don't occur any more.
iP  c                 6   U R                   S-  [        R                  " [        R                  5      R                  :  d   e [        [        R                  R                  SS5      5      n[        SU-  5        g ! [         a    [        R                  n N+f = f)Nr:   PYTEST_XDIST_WORKER_COUNT1i  )r   r;   rX   rY   rZ   intosenvironget
ValueErrorinfr   )r,   parallel_counts     r   setup_methodTestInt32Overflow.setup_methodi   ss    vvqy288BHH-11111	$ 0KS!QRN 	$/0  	$VVN	$s   )A; ;BBc                 .    [         R                  " 5         g r   )gccollect)r,   s    r   teardown_method!TestInt32Overflow.teardown_methodu   s    


r   c                 v   U R                   n[        R                  " SUS-
  /5      n[        R                  " SUS-
  /5      n[        R                  " SS/[        R                  S9n[	        XBU445      nUR                  5       n[        US   S5        [        US   S5        A[        R                  " 5         g Nr   rI   r:   rL   r   r   )rV   rV   )	r   r;   rN   r\   r   todenser   rv   rw   )r,   r   ir.   r*   mrs          r   test_coo_todense"TestInt32Overflow.test_coo_todensex   s    
 FFHHa1XHHa1XxxAbgg.!f~&IIKQsVQQuXq!


r   c                 ,   U R                   n[        R                  " SUS-
  /5      n[        R                  " SUS-
  /5      n[        R                  " SS/[        R                  S9n[	        XBU445      n[        R
                  " X4[        R                  S9n[        [        [        4 HP  nU" U5      nUR                  U5      n	[        U	S   S5        [        U	S   S5        A	[        R                  " 5         MR     A[        R                  " 5         g r{   )r   r;   rN   r\   r   r<   r   r	   r
   dotr   rv   rw   )
r,   r   r~   r.   r*   r   r-   sptypem2r   s
             r   test_matvecsTestInt32Overflow.test_matvecs   s     FFHHa1XHHa1XxxAbgg.!f~&GGQF"''*!:z:FBq	A3#51%JJL ; 


r   c                    U R                   n[        R                  " X4[        R                  S9n[        R                  " U5      n[        X#4X4S9n[        R                  " UR                  S   [        R                  S9nUR                  U5      n[        US   [        U5      5        AAAAA[        R                  " 5         g )NrL   )rW   rI   r   )r   r;   r<   r\   aranger   rW   r   r   r   rv   rw   )r,   r   r*   offsetsr   vr   s          r   test_dia_matvec!TestInt32Overflow.test_dia_matvec   s     FFwwvRWW-))A,qf5GGAGGAJbgg.EE!HQqT;q>*'1a


r   matmat)marksmatvecsmatvecdiagonalsort_indices	transposeopc                    ^  U 4S jn[         R                  " 5          [        T SU-   5      " U5        [         R                  " 5         g ! [         R                  " 5         f = f)Nc                    > TR                   n [        R                  " SX 4[        R                  S9n[        R                  " SS/[        R
                  S9n[        R                  " S/[        R
                  S9n[        XU4X 4SS9nAAAU$ NrI   rL   r   F)	blocksizer%   )r   r;   r<   r\   rN   rY   r
   r   r*   r(   r)   r   r,   s        r   
get_matrix6TestInt32Overflow.test_bsr_1_block.<locals>.get_matrix   ss    A77Aq9BGG4DXXq!fBHH5Fhhs"((3GD62qf5QAfgHr   _check_bsr_rv   rw   getattrr,   r   r   s   `  r   test_bsr_1_block"TestInt32Overflow.test_bsr_1_block   s?    	 	

	D-",-j9JJLBJJL   A	 	A c                    ^  U 4S jn[         R                  " 5          [        T SU-   5      " U5        [         R                  " 5         g ! [         R                  " 5         f = f)Nc                    > TR                   n [        R                  " X S4[        R                  S9n[        R                  " SU /[        R
                  S9n[        R                  " U [        R
                  S9n[        XU4U S4SS9nAAAU$ r   )r   r;   r<   r\   rN   rY   r   r
   r   s        r   r   6TestInt32Overflow.test_bsr_n_block.<locals>.get_matrix   ss    A77A!9BGG4DXXq!fBHH5Fii2GD62q!f5QAfgHr   r   r   r   s   `  r   test_bsr_n_block"TestInt32Overflow.test_bsr_n_block   s?    	 	

	D-",-j9JJLBJJLr   c                     U" 5       nU R                   nUR                  [        R                  " US4[        R                  S95      n[        US   [        U5      5        g )Nr:   rL   r|   r   r   r;   r<   r\   r   r   r,   r   r   r   s       r   _check_bsr_matvecs$TestInt32Overflow._check_bsr_matvecs   sG    CFF EE"''1a&01QtWk!n-r   c                     U" 5       nU R                   nUR                  [        R                  " U4[        R                  S95      n[        US   [        U5      5        g )NrL   r   r   r   s       r   _check_bsr_matvec#TestInt32Overflow._check_bsr_matvec   sE    CFF EE"''1$bgg./QqT;q>*r   c                     U" 5       nU R                   nUR                  5       n[        U[        R                  " U5      5        g r   )r   r   r   r;   r<   r   s       r   _check_bsr_diagonal%TestInt32Overflow._check_bsr_diagonal   s1    CFF JJLQ
#r   c                 2    U" 5       nUR                  5         g r   )r   r,   r   s     r   _check_bsr_sort_indices)TestInt32Overflow._check_bsr_sort_indices   s    C	r   c                 2    U" 5       nUR                  5         g r   )r   r   s     r   _check_bsr_transpose&TestInt32Overflow._check_bsr_transpose   s    C	r   c                 `   U" 5       nU R                   n[        [        R                  " US4[        R                  S9UR
                  S   S4S9nUR                  U5        A[        [        R                  " SU4[        R                  S9SUR
                  S   4S9nUR                  U5        g )Nr:   rL   rI   )r   r   )r   r
   r;   r<   r\   r   r   )r,   r   r   r   s       r   _check_bsr_matmat#TestInt32Overflow._check_bsr_matmat  s    CFF Abgg61;;q>STBUV	b	 Abgg61akkRSnBUV
q	r   r   N)r4   r5   r6   r7   __doc__r   rs   rx   r   pytestmarkslowr   r   paramxslow_bsr_opsparametrizer   r   r   r   r   r   r   r   r8   r   r   r   rg   rg   [   s@   
 	A
1$ [[ * [[
 
 XV[[->->?Yfkk.?.?@[0A0ABDH [[[[T8, - ( [[[[T8, - *.+$

r   rg   z/64-bit indices in sparse matrices not availablec                     Sn U S-  [         R                  " [         R                  5      R                  :  d   e[	        U S-  S-  S-  5        [         R
                  " U 4[         R                  S9n[         R                  " U S-   [         R                  S9n[         R                  " U [         R                  S9n[        XU45      nUR                  n[        [        UR                  U5        g )Nl   4s	j r:      rJ   rU   rL   rI   )r;   rX   int64rZ   r   r<   r\   r   r[   r   Tr   RuntimeErrorr   )r   r*   r(   r)   r/   r-   s         r   test_csr_matmat_int64_overflowr     s    Aa4"((288$((((( a7ma'#-. 77A4rww'DYYqs"((+Fhhq)GD6*+A	A,q)r   c                  v   [        [        R                  [        R                  S-  /SS//[        S9n [        R                  " SS/[        S9n[
         GHN  n[
         GH?  nSU< SU< S	3n[        R                  " U[        R                  5      (       a   U R                  5       R                  U5      nO)U R                  R                  5       R                  U5      n[        R                  " U[        R                  5      (       a   UR                  5       R                  U5      nOF[        R                  " S
S9   UR                  R                  5       R                  U5      nS S S 5        U[        R                  :X  a  U[        R                  :X  df  [        R                  " S[        R                  S9n[        [        [         R"                  SSUR$                  UR&                  UR(                  WU5	        [        R                  " U[        R                  5      (       a*  [        R                  " U[        R                  5      (       aT  [        R                  " U[        R                  5      (       d  [        R                  " U[        R                  5      (       af  [        R                  " S[        R*                  S9n[        [        [         R"                  SSUR$                  UR&                  UR(                  WU5	        [        R                  " S[        R,                  " X#5      S9n[         R"                  " SSUR$                  UR&                  UR(                  WU5        [/        U[        R0                  " UR3                  5       U5      US9  GMB     GMQ     g ! , (       d  f       GNG= f)Ny              ?rJ   rK   rL   y      p@      ?l        (z, )ignore)invalid)r:   r:   )err_msg)r   r;   picomplexrN   r   
issubdtypecomplexfloatingr%   rO   realerrstatebool_r[   r   rp   r   
csr_matvecr(   r)   r*   float64result_typer   r   rB   )a0b0a_dtypeb_dtypemsgr/   r-   cs           r   test_upcastr   #  sm   	beeRUU2X&A/w	?B	65/	1B##''Gg[7+Q/C}}Wb&8&899GGI$$W-GGLLN))'2}}Wb&8&899GGI$$W-[[2 --g6A 3
 rxx'Grxx,?HHT2j,*A*AAHHaiiAG w(:(:;;]]7B,>,>??]]7B,>,>??w(:(:;;HHT4j,*A*AAHHaiiAG R^^G%EFA##Aq!((AIIqvvq!LArvvaiik15sC? ( $ 32s   ?*N))
N8c                  L   [         R                  " S5      n / SQn[        U R                  S5      U4S5      n[        U R                  S5      U4S5      n[         R                  " S5      n[        UR                  U5      / SQ5        [        UR                  U5      / SQ5        g )N)rJ   rK   )rV   r   rI   z<f8)rK   rK   z>f8rK   )rI   rJ         )r;   r<   r   rO   r   r   r   )ra   r   r/   r-   r   s        r   test_endiannessr   J  sw    
AGAHHUOW-v6AAHHUOW-v6A
		!AAEE!Hl+AEE!Hl+r   )+sysrm   rv   r=   numpyr;   numpy.testingr   r   r   scipy.sparser   r   r   r	   r
   r   scipy.sparse._sputilsr   scipy._lib._testutilsr   r   r   r   r   r   rG   rR   r   r   xfail_on_32bitrb   skipifplatform
startswithrM   intpitemsizerg   skipr   r   r   r   r   r   <module>r      s   
 	 	   @ @2 2 2 3  *!>*>7 ?@ A & 		 	 	)	Mbhhrww.?.H.HA.MN'  o o	od JK* L*"$DN	,r   