o
    i9)                     @   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   r/var/www/html/eduruby.in/lip-sync/lip-sync-env/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   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 ]}|  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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 ](} 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J zttjdd}W n t	y%   tj
}Y nw td|  d S )Nr0   ZPYTEST_XDIST_WORKER_COUNT1i  )r   r1   rI   rJ   rK   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_   r`   )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  q6~t  d S rc   )r   r1   rA   rM   r   r2   r   r   r	   dotr   r_   r`   )
r!   r   re   r#   r   rf   r"   Zsptypem2rg   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   ri   r   r   r_   r`   )r!   r   r   offsetsrf   vrg   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 Nr<   r?   r   F)	blocksizer   )r   r1   r2   rM   rA   rJ   r	   r   r   r   r   rf   ra   r   r   
get_matrix   s   z6TestInt32Overflow.test_bsr_1_block.<locals>.get_matrix_check_bsr_r_   r`   getattrr!   rs   rx   r   ra   r   test_bsr_1_block   s
   	z"TestInt32Overflow.test_bsr_1_blockc                    rt   )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   rl   r	   rw   ra   r   r   rx      s   z6TestInt32Overflow.test_bsr_n_block.<locals>.get_matrixry   rz   r|   r   ra   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?   rd   r   ri   r1   r2   rM   r   r   r!   rf   r   rg   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   rp   r   r1   r2   r   r   r   r   _check_bsr_diagonal   s   z%TestInt32Overflow._check_bsr_diagonalc                 C      | }|   d S r   )rq   r!   rf   r   r   r   _check_bsr_sort_indices      z)TestInt32Overflow._check_bsr_sort_indicesc                 C   r   r   )rr   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 )Nr0   r?   r<   )rv   r   )r   r	   r1   r2   rM   rv   ri   )r!   rf   r   rj   r   r   r   _check_bsr_matmat  s   &
&z#TestInt32Overflow._check_bsr_matmatN)r)   r*   r+   __doc__r   r^   rb   rh   pytestmarkslowrk   ro   paramZxslowZ_bsr_opsZparametrizer}   r~   r   r   r   r   r   r   r   r   r   r   rV   [   s:    

rV   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 r0      r=   rF   r?   r<   )r1   rI   Zint64rK   r   r2   rM   rl   rL   r   Tr   RuntimeErrorri   )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=   r>   r?   y      p@      ?l        (z, )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   ri   r8   )Za0Zb0Za_dtypeZ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=   r>   )rG   r   r<   z<f8)r>   r>   z>f8r>   )r<   r=         )r1   r2   r
   rB   rl   r   ri   )rQ   rm   r%   r"   rn   r   r   r   test_endiannessH  s   

r   )+sysrY   r_   r3   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>   s6     
$ 3
'