o
    Eb'                     @   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ejjejddd Zejje j !dooe"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   t ttj d S N)assert_raisesMemoryErrorr   Ztest_throw_error r   r   E/usr/lib/python3/dist-packages/scipy/sparse/tests/test_sparsetools.py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   Zcsr_plus_csrindptrindicesdataappend)selfbjabresn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>*   s    z test_threads.<locals>.<listcomp>   )r   npones	threadingZThreadr   startjoinr   alltoarray)ZnthreadsZthreadsthreadr   r   )r)   r!   r"   r#   r$   r   test_threads   s   


r6   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   r-         dtyper   )r   r.   arrayastyper   r   Zgetcolr4   )r;   Zadr!   r   r   r   !test_regression_std_vector_dtypes4   s
   &r>   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 )Nr7   	       .Ag      ?r:   r9   )r7   r7   F)shaper   )
r.   iinfoint32maxr   zerosint8r   r4   r   )Znnzrowcolr   sdr   r   r   test_nnz_overflow?   s   rL   Z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 )Nr-   ZPYTEST_XDIST_WORKER_COUNT1i  )r#   r.   rC   rD   rE   intosenvironget
ValueErrorinfr   )r   Zparallel_countr   r   r   setup_method`   s   
zTestInt32Overflow.setup_methodc                 C   s   t   d S r   )gccollectr   r   r   r   teardown_methodl   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   r7   r-   r:   r   r   )rA   rA   )	r#   r.   r<   rG   r   Ztodenser   rX   rY   )r   r#   ir   r   mrr   r   r   test_coo_todenseo   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 r\   )r#   r.   r<   rG   r   r/   r   r   r	   dotr   rX   rY   )
r   r#   r^   r   r   r_   r   Zsptypem2r`   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:   )rB   r7   r   )r#   r.   r/   rG   aranger
   rB   rb   r   rX   rY   )r   r#   r   offsetsr_   vr`   r   r   r   test_dia_matvec   s   


z!TestInt32Overflow.test_dia_matvecZmatmat)ZmarksZmatvecsZ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 Nr7   r:   r   F)	blocksizer   )r#   r.   r/   rG   r<   rD   r	   r#   r   r   r   r_   rZ   r   r   
get_matrix   s   z6TestInt32Overflow.test_bsr_1_block.<locals>.get_matrix_check_bsr_rX   rY   getattrr   rl   rq   r   rZ   r   test_bsr_1_block   s
   	z"TestInt32Overflow.test_bsr_1_blockc                    rm   )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 rn   )r#   r.   r/   rG   r<   rD   re   r	   rp   rZ   r   r   rq      s   z6TestInt32Overflow.test_bsr_n_block.<locals>.get_matrixrr   rs   ru   r   rZ   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 )Nr-   r:   r]   r#   rb   r.   r/   rG   r   r   r_   r#   r`   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   rx   ry   r   r   r   _check_bsr_matvec   s   z#TestInt32Overflow._check_bsr_matvecc                 C   s(   | }| j }| }t|t| d S r   )r#   ri   r   r.   r/   ry   r   r   r   _check_bsr_diagonal   s   z%TestInt32Overflow._check_bsr_diagonalc                 C      | }|   d S r   )rj   r   r_   r   r   r   _check_bsr_sort_indices      z)TestInt32Overflow._check_bsr_sort_indicesc                 C   r}   r   )rk   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 )Nr-   r:   r7   )ro   r   )r#   r	   r.   r/   rG   ro   rb   )r   r_   r#   rc   r   r   r   _check_bsr_matmat   s   &
&z#TestInt32Overflow._check_bsr_matmatN)r&   r'   r(   __doc__r#   rW   r[   ra   pytestmarkslowrd   rh   ZparamZxslowZ_bsr_opsZparametrizerv   rw   rz   r{   r|   r   r   r   r   r   r   r   rO   T   s:    

rO   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 r-      r8   r@   r:   r7   )r.   rC   Zint64rE   r   r/   rG   re   rF   r   Tr   RuntimeErrorrb   )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r6|  	|}n| j
 	|}t|tjrM| 	|}n|j
 	|}|tjkr_|tjkswt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              ?r8   r9   r:   y      p@      ?l        z(%r, %r))r-   r-   )err_msg)r   r.   Zpicomplexr<   r   Z
issubdtypeZcomplexfloatingr   r=   realZbool_rF   r   rU   r   Z
csr_matvecr   r   r   Zfloat64Zresult_typer   rb   r4   )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)r8   r9   )rA   r   r7   z<f8)r9   r9   z>f8r9   )r7   r8         )r.   r/   r
   r=   re   r   rb   )rK   rf   r!   r   rg   r   r   r   test_endianness>  s   

r   )*sysrR   rX   r0   Znumpyr.   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   r6   r>   r   r   Zxfail_on_32bitrL   Zskipifplatform
startswithr;   ZintpitemsizerO   skipr   r   r   r   r   r   r   <module>   s4     
$ 3
$