a
    Re9)                     @   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oe#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   W/var/www/sunrise/env/lib/python3.9/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   Z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 ]}|  qX|D ]}|  qj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 ]4}t j j j j j j|j|j|j q| d S r   )copyranger   Z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       z test_threads.<locals>.<listcomp>   )r   npones	threadingThreadr   startjoinr   alltoarray)Znthreadsthreadsthreadr"   r   )r,   r%   r&   r   r'   r   test_threads   s    


r<   c                  C   sZ   t D ]P} tddgddgg| }t|| d}t|d |d d d df  qd S )N   r1         dtyper   )r   r2   arrayastyper   r   Zgetcolr9   )rA   adr%   r   r   r   !test_regression_std_vector_dtypes;   s    rE   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      ?r@   r?   )r=   r=   F)shaper   )
r2   iinfoint32maxr   zerosint8r   r9   r   )Znnzrowcolr   sdr   r   r   test_nnz_overflowF   s    rS   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	yJ   tj
}Y n0 td|  d S )Nr1   ZPYTEST_XDIST_WORKER_COUNT1i  )r   r2   rJ   rK   rL   intosenvironget
ValueErrorinfr   )r!   Zparallel_countr   r   r   setup_methodg   s    zTestInt32Overflow.setup_methodc                 C   s   t   d S r   )gccollectr!   r   r   r   teardown_methods   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=   r1   r@   r   r   )rH   rH   )	r   r2   rB   rN   r   Ztodenser   r`   ra   )r!   r   ir#   r   mrr   r   r   test_coo_todensev   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  ql~t  d S rd   )r   r2   rB   rN   r   r3   r   r   r	   dotr   r`   ra   )
r!   r   rf   r#   r   rg   r"   Zsptypem2rh   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 )Nr@   )rI   r=   r   )r   r2   r3   rN   aranger
   rI   rj   r   r   r`   ra   )r!   r   r   offsetsrg   vrh   r   r   r   test_dia_matvec   s    


z!TestInt32Overflow.test_dia_matvecZmatmat)ZmarksZmatvecsZmatvecdiagonalsort_indices	transposeopc                    sB    fdd}t   zt d| | W t   n
t   0 d S )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=   r@   r   F)	blocksizer   )r   r2   r3   rN   rB   rK   r	   r   r   r   r   rg   rb   r   r   
get_matrix   s    z6TestInt32Overflow.test_bsr_1_block.<locals>.get_matrix_check_bsr_r`   ra   getattrr!   rt   rx   r   rb   r   test_bsr_1_block   s
    	z"TestInt32Overflow.test_bsr_1_blockc                    sB    fdd}t   zt d| | W t   n
t   0 d S )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 ru   )r   r2   r3   rN   rB   rK   rm   r	   rw   rb   r   r   rx      s    z6TestInt32Overflow.test_bsr_n_block.<locals>.get_matrixry   rz   r|   r   rb   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 )Nr1   r@   re   r   rj   r2   r3   rN   r   r   r!   rg   r   rh   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 )Nr@   r   r   r   r   r   r   _check_bsr_matvec   s    z#TestInt32Overflow._check_bsr_matvecc                 C   s(   | }| j }| }t|t| d S r   )r   rq   r   r2   r3   r   r   r   r   _check_bsr_diagonal   s    z%TestInt32Overflow._check_bsr_diagonalc                 C   s   | }|   d S r   )rr   r!   rg   r   r   r   _check_bsr_sort_indices   s    z)TestInt32Overflow._check_bsr_sort_indicesc                 C   s   | }|   d S r   )rs   r   r   r   r   _check_bsr_transpose   s    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 )Nr1   r@   r=   )rv   r   )r   r	   r2   r3   rN   rv   rj   )r!   rg   r   rk   r   r   r   _check_bsr_matmat  s    &
&z#TestInt32Overflow._check_bsr_matmatN)r)   r*   r+   __doc__r   r_   rc   ri   pytestmarkslowrl   rp   paramZxslowZ_bsr_opsZparametrizer}   r~   r   r   r   r   r   r   r   r   r   r   rW   [   s8   

rW   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 r1      r>   rG   r@   r=   )r2   rJ   int64rL   r   r3   rN   rm   rM   r   Tr   RuntimeErrorrj   )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rv|  	|}n| j
 	|}t|tjr| 	|}n<tjd
d  |j
 	|}W d    n1 s0    Y  |tjkr|tjks&tjdtjd}tttjdd|j|j|j||	 t|tjrFt|tjrft|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@q6d S )Ny              ?r>   r?   r@   y      p@      ?l        (z, )ignore)invalid)r1   r1   )err_msg)r   r2   picomplexrB   r   Z
issubdtypeZcomplexfloatingr   rC   realZerrstateZbool_rM   r   r]   r   Z
csr_matvecr   r   r   float64Zresult_typer   rj   r9   )Za0Zb0Za_dtypeZb_dtypemsgr%   r"   cr   r   r   test_upcast!  s>     

.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>   r?   )rH   r   r=   z<f8)r?   r?   z>f8r?   )r=   r>         )r2   r3   r
   rC   rm   r   rj   )rR   rn   r%   r"   ro   r   r   r   test_endiannessH  s    

r   )+sysrZ   r`   r4   numpyr2   Znumpy.testingr   r   r   Zscipy.sparser   r   r   r   r	   r
   Zscipy.sparse._sputilsr   Zscipy._lib._testutilsr   r   r   r   r   r   r<   rE   r   r   Zxfail_on_32bitrS   Zskipifplatform
startswithrA   ZintpitemsizerW   skipr   r   r   r   r   r   r   <module>   s4    
$ 3
'