o
    `^h/)                     @   s(  d dl Z d dlZd dlZd dlZd dlZd dlmZmZm	Z	 d dl
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Zd dlmZ dd Zd	d
 Zdd Zdd Zejjejddd Zejj e j!"dose#ej$j%dk ddG dd dZ&ejj'dddd Z(dd Z)dd Z*dS )    N)assert_equalassert_assert_allclose)_sparsetools
coo_matrix
csr_matrix
csc_matrix
bsr_matrix
dia_matrix)supported_dtypes)check_free_memory)raisesc                 C   s   | d d d S )z6
    Wrap an integer to the interval [-128, 127].
           )nr   r   a/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/scipy/sparse/tests/test_sparsetools.pyint_to_int8   s   r   c                   C   s   t ttj d S N)assert_raisesMemoryErrorr   test_throw_errorr   r   r   r   test_exception   s   r   c                     s   d} ddt tgg G fdddtj  fddt| D }|D ]}|  q,|D ]}|  q5D ]}tt	|
 dk q>d S )	N
   d      c                       s   e Zd Z fddZdS )ztest_threads.<locals>.Workerc                    sT      }tD ]}t j j j j j j|j|j|j q| d S r   )copyranger   csr_plus_csrindptrindicesdataappend)selfbjabresr   niterr   r   run(   s   z test_threads.<locals>.Worker.runN)__name__
__module____qualname__r*   r   r&   r   r   Worker'   s    r.   c                    s   g | ]}  qS r   r   ).0_)r.   r   r   
<listcomp>1   s    z test_threads.<locals>.<listcomp>   )r   npones	threadingThreadr   startjoinr   alltoarray)nthreadsthreadsthreadr$   r   )r.   r'   r(   r   r)   r   test_threads   s   


r>   c                  C   sZ   t D ](} tddgddgg| }t|| d}t|d |d d d df  qd S )N   r2         dtyper   )r   r3   arrayastyper   r   getcolr:   )rC   adr'   r   r   r   !test_regression_std_vector_dtypes;   s
   &rH   z!Can't create large array for testc                  C   s   t t jjd } td|  d d  t j| t jd}t j| t jd}t j| t jd}d|d< t|||ffdd	d
}| }t	|dgg d S )Nr?   	       .Ag      ?rB   rA   )r?   r?   F)shaper   )
r3   iinfoint32maxr   zerosint8r   r:   r   )nnzrowcolr!   sdr   r   r   test_nnz_overflowF   s   rW   linux   ztest requires 64-bit Linux)reasonc                	   @   s   e Zd ZdZdZdd Zdd Zdd Zej	j
d	d
 Zej	j
dd Zejdej	jdejdej	jddddejdej	jdgZej	j
ej	dedd Zej	j
ej	dedd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%S )&TestInt32Overflowa  
    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                 C   s\   | j d ttjjksJ zttjdd}W n t	y%   tj
}Y nw td|  d S )Nr2   PYTEST_XDIST_WORKER_COUNT1i  )r   r3   rM   rN   rO   intosenvironget
ValueErrorinfr   )r#   parallel_countr   r   r   setup_methodi   s   
zTestInt32Overflow.setup_methodc                 C   s   t   d S r   )gccollectr#   r   r   r   teardown_methodu   s   z!TestInt32Overflow.teardown_methodc                 C   s   | j }td|d g}td|d g}tjddgtjd}t|||ff}| }t|d d t|d d ~t  d S Nr   r?   r2   rB   r   r   )rK   rK   )	r   r3   rD   rQ   r   todenser   rf   rg   )r#   r   ir%   r!   mrr   r   r   test_coo_todensex   s   z"TestInt32Overflow.test_coo_todensec           
      C   s   | j }td|d g}td|d g}tjddgtjd}t|||ff}tj||ftjd}tttfD ]}||}|	|}	t
|	d d t
|	d d ~	t  q6~t  d S rj   )r   r3   rD   rQ   r   r4   r   r   r	   dotr   rf   rg   )
r#   r   rm   r%   r!   rn   r$   sptypem2ro   r   r   r   test_matvecs   s   

zTestInt32Overflow.test_matvecsc                 C   s   | j }tj||ftjd}t|}t||f||fd}tj|jd tjd}||}t|d t	| ~~~~~t
  d S )NrB   )rL   r?   r   )r   r3   r4   rQ   aranger
   rL   rq   r   r   rf   rg   )r#   r   r!   offsetsrn   vro   r   r   r   test_dia_matvec   s   


z!TestInt32Overflow.test_dia_matvecmatmat)marksmatvecsmatvecdiagonalsort_indices	transposeopc                    @    fdd}t   zt d| | W t   d S t   w )Nc                     sd    j } tjd| | ftjd}tjddgtjd}tjdgtjd}t|||f| | fdd}~~~|S Nr?   rB   r   F)	blocksizer   )r   r3   r4   rQ   rD   rN   r	   r   r!   r   r    rn   rh   r   r   
get_matrix   s   z6TestInt32Overflow.test_bsr_1_block.<locals>.get_matrix_check_bsr_rf   rg   getattrr#   r   r   r   rh   r   test_bsr_1_block   s
   	z"TestInt32Overflow.test_bsr_1_blockc                    r   )Nc                     sb    j } tj| | dftjd}tjd| gtjd}tj| tjd}t|||f| dfdd}~~~|S r   )r   r3   r4   rQ   rD   rN   ru   r	   r   rh   r   r   r      s   z6TestInt32Overflow.test_bsr_n_block.<locals>.get_matrixr   r   r   r   rh   r   test_bsr_n_block   s
   	z"TestInt32Overflow.test_bsr_n_blockc                 C   s<   | }| j }|tj|dftjd}t|d t| d S )Nr2   rB   rk   r   rq   r3   r4   rQ   r   r   r#   rn   r   ro   r   r   r   _check_bsr_matvecs   s   z$TestInt32Overflow._check_bsr_matvecsc                 C   s:   | }| j }|tj|ftjd}t|d t| d S )NrB   r   r   r   r   r   r   _check_bsr_matvec   s   z#TestInt32Overflow._check_bsr_matvecc                 C   s(   | }| j }| }t|t| d S r   )r   r}   r   r3   r4   r   r   r   r   _check_bsr_diagonal   s   z%TestInt32Overflow._check_bsr_diagonalc                 C      | }|   d S r   )r~   r#   rn   r   r   r   _check_bsr_sort_indices      z)TestInt32Overflow._check_bsr_sort_indicesc                 C   r   r   )r   r   r   r   r   _check_bsr_transpose   r   z&TestInt32Overflow._check_bsr_transposec                 C   sr   | }| j }ttj|dftjd|jd dfd}|| ~ttjd|ftjdd|jd fd}|| d S )Nr2   rB   r?   )r   r   )r   r	   r3   r4   rQ   r   rq   )r#   rn   r   rs   r   r   r   _check_bsr_matmat  s   &
&z#TestInt32Overflow._check_bsr_matmatN)r+   r,   r-   __doc__r   re   ri   rp   pytestmarkslowrt   rx   paramxslow_bsr_opsparametrizer   r   r   r   r   r   r   r   r   r   r   r   r[   [   s:    

r[   z/64-bit indices in sparse matrices not availablec                  C   s   d} | d t t jjksJ t| d d d  t j| ft jd}t j| d t jd}t j| t jd}t	|||f}|j
}tt|j| d S )Nl   4s	j r2      r@   rJ   rB   r?   )r3   rM   int64rO   r   r4   rQ   ru   rP   r   Tr   RuntimeErrorrq   )r   r!   r   r    r'   r$   r   r   r   test_csr_matmat_int64_overflow  s   r   c                  C   s  t tjtjd gddggtd} tjddgtd}tD ]}tD ]}d|d|d	}t|tjr9|  	|}n| j
 	|}t|tjrP| 	|}ntjd
d |j
 	|}W d    n1 siw   Y  |tjkrx|tjkstjdtjd}tttjdd|j|j|j||	 t|tjrt|tjrt|tjst|tjrtjdtjd}tttjdd|j|j|j||	 tjdt||d}tdd|j|j|j|| t|t| ||d qqd S )Ny              ?r@   rA   rB   y      p@      ?l        (z, )ignore)invalid)r2   r2   )err_msg)r   r3   picomplexrD   r   
issubdtypecomplexfloatingr   rE   realerrstatebool_rP   r   rb   r   
csr_matvecr   r    r!   float64result_typer   rq   r:   )a0b0a_dtypeb_dtypemsgr'   r$   cr   r   r   test_upcast#  sD    r   c                  C   sp   t d} g d}t| d|fd}t| d|fd}t d}t||g d t||g d d S )N)r@   rA   )rK   r   r?   z<f8)rA   rA   z>f8rA   )r?   r@         )r3   r4   r
   rE   ru   r   rq   )rV   rv   r'   r$   rw   r   r   r   test_endiannessJ  s   

r   )+sysr_   rf   r5   numpyr3   numpy.testingr   r   r   scipy.sparser   r   r   r   r	   r
   scipy.sparse._sputilsr   scipy._lib._testutilsr   r   r   r   r   r   r>   rH   r   r   xfail_on_32bitrW   skipifplatform
startswithrC   intpitemsizer[   skipr   r   r   r   r   r   r   <module>   s8     
 3
'