U
    /d(                     @   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   G/tmp/pip-unpacked-wheel-9gxwnfpp/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   s     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   r0         dtyper   )r   r1   arrayastyper   r   Zgetcolr8   )r@   adr%   r   r   r   !test_regression_std_vector_dtypes;   s    rD   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   )
r1   iinfoint32maxr   zerosint8r   r8   r   )Znnzrowcolr   sdr   r   r   test_nnz_overflowF   s    rR   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tzttj	dd}W n t
k
rL   tj}Y nX td|  d S )Nr0   ZPYTEST_XDIST_WORKER_COUNT1i  )r   r1   rI   rJ   rK   AssertionError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<   r0   r?   r   r   )rG   rG   )	r   r1   rA   rM   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   r1   rA   rM   r   r2   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?   )rH   r<   r   )r   r1   r2   rM   aranger
   rH   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                    s:    fdd}t   zt d| | W 5 t   X 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   r1   r2   rM   rA   rJ   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                    s:    fdd}t   zt d| | W 5 t   X 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   r1   r2   rM   rA   rJ   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 )Nr0   r?   re   r   rj   r1   r2   rM   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   r1   r2   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 )Nr0   r?   r<   )rv   r   )r   r	   r1   r2   rM   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   rV   [   s8   

rV   z/64-bit indices in sparse matrices not availablec                  C   s   d} | d t t jjkst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 r0      r=   rF   r?   r<   )r1   rI   Zint64rK   rX   r   r2   rM   rm   rL   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||f }t|tjrp|  	|}n| j
 	|}t|tjr| 	|}n(tjdd	 |j
 	|}W 5 Q R X |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r,t|tjrLt|tjs|t|tjr|t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(%r, %r)ignore)invalid)r0   r0   )err_msg)r   r1   picomplexrA   r   Z
issubdtypeZcomplexfloatingr   rB   realZerrstateZbool_rL   r   r]   r   Z
csr_matvecr   r   r   Zfloat64Zresult_typer   rj   r8   )Za0Zb0Za_dtypeZb_dtypemsgr%   r"   cr   r   r   test_upcast!  sV     

            r   c                  C   sz   t d} dddg}t| d|fd}t| d|fd}t d}t||dd	d
dg t||dd	d
dg d S )N)r=   r>   rG   r   r<   z<f8)r>   r>   z>f8r>   r=         )r1   r2   r
   rB   rm   r   rj   )rQ   rn   r%   r"   ro   r   r   r   test_endiannessH  s    


r   )+sysrZ   r`   r3   Znumpyr1   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;   rD   r   r   Zxfail_on_32bitrR   Zskipifplatform
startswithr@   ZintpitemsizerV   skipr   r   r   r   r   r   r   <module>   s4    
$ 3
'