o
    ?Hh:                     @   sf   d Z ddlZddlmZ ddlZddlmZ ddlm	Z
mZmZmZmZ ddlmZ G dd dZdS )	z'unit tests for sparse utility functions    N)assert_equal)raises)_sputils	csr_array	bsr_array	dia_array	coo_array)matrixc                   @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zejdeegdd Zdd Zdd Zdd Zejdg ddgdd gd!d"gg d#d$gg d%gd%gd%gd&gd&gd'd$gd'gd$d'gd'gd(gd(gd(d)gd)gg d*d+gd,gd,gd-d.gd/gd0d1gd1gd2d1gd1gd0d2gd1gd(d1gd1gd(d0gd0gd(d2gd2gd%d3gd3gd3d1gd1gd3d2gd1gd4d1gd1gd%d1gd1gd(d3gd0gd4d2gd2gd4d0gd0gd%d0gd0gd%d2gd2gd4d5gd5gd6d7gd7gd'gd8 d'gd'gd9 d'gd$gd8 d$gd:d; Zejd<d5d=gd>d$gd6d>gg d?d@dAgdBd@gdCdDgg dEd$gd8 d5gd8  g	dFdG ZdHdI ZdJdK ZdLdM ZdNS )OTestSparseUtilsc                 C   sT   t tdtj t tddtj t tdtttj t tddtj d S )Nintcint32float32boolid)	r   sputilsupcastnpr   float64complexfloat
complex128self r   _/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/sparse/tests/test_sputils.pytest_upcast   s   zTestSparseUtils.test_upcastc                 C   s   t jdgdd}ttjd tdt ttjd |dt j ttdd td W d    n1 s3w   Y  ttd	d tjd t j	d W d    d S 1 sRw   Y  d S )
N   int8dtype)default)az.scipy.sparse does not support dtype object. .*matchOz/scipy.sparse does not support dtype float16. .*)
r   arrayr   r   getdtyper   r   assert_raises
ValueErrorfloat16)r   Ar   r   r   test_getdtype   s   "zTestSparseUtils.test_getdtypec                 C   s   t tdd t tdd t tdd t tdd t ttdd t tdd t ttdgd t tdggd t td	d t td
d d S )N      @T      @      ?      @   16Fr   r      )r   r   isscalarliker   r&   r   r   r   r   test_isscalarlike%   s   z!TestSparseUtils.test_isscalarlikec                 C   s   t tdd t ttdd t ttdgd ttdd td W d    n1 s5w   Y  t tdd t td	d t td
d t tdd d S )Nr.   Tr1   Fz4Inexact indices into sparse matrices are not allowedr#   r-   r/   r0   r3   r4   )r   r   	isintliker   r&   r(   r)   r   r   r   r   test_isintlike2   s   zTestSparseUtils.test_isintlikec                 C   s*  t tdd t tdd t tdd t tdd t tdgdfd t tjddd	d t tjd
dd	d t tjddd	d t tjd
dd	d t tjdddd t tjdgdfddd t tjddddd t tjdddd t tjdddd t tjdddd d S )Nr4   T)   r5   )      ?r5   Fr5   r5   r5   r5   )r5   )nonneg)r5   r=   )allow_nd)r5   r5   )r>   r?   r5   )r5   r5   )r   r   isshaper   r   r   r   test_isshapeA   s"   zTestSparseUtils.test_isshapec                 C   s   t tdd t tdd t tdgd t tg dd t ttg dd t ttdgdgdggd t tdd d S )Nr3   Tr   r5   r1   r   r5   r1   F)r   r   
issequencer   r&   r   r   r   r   test_issequenceU   s   "zTestSparseUtils.test_issequencec                 C   s   t tdd t tdgdggd t ttdd  d t tddgd t ttdd t tdgggd t tdd d S )N)r   Tr   r5   r1   F)r   r   ismatrixr   aranger   r   r   r   test_ismatrix_   s   zTestSparseUtils.test_ismatrixc                 C   s2   t ttdgd t ttdgd d S )Nr   T)r   r   isdenser   r&   r	   r   r   r   r   test_isdensei   s   zTestSparseUtils.test_isdensec                 C   sB   t ttjd t ttjd t ttjd dD ]}t| qd S )Nr   r   r;   r1   )r@   r=   r   r   N)r(   	TypeErrorr   validateaxisr)   )r   axisr   r   r   test_validateaxism   s   z!TestSparseUtils.test_validateaxis	containerc                 C   sr  t t t jj}|d|f}| }|jt j|_|jt j|_t	d|d f}| }|jt j|_|jt j|_| }t j
|d gt jd|_t j
ddgt jd|_t 
dg|_|jj|jjft jt jfksrJ |jj|jjft jt jfksJ |jj|jjft jt jfksJ |jj|jjft jt jfksJ |jj|jjft jt jfksJ ||||fD ]I}t|t j\}	}
|	j|
jft jt jfksJ t|t j\}	}
|	j|
jft jt jfksJ t||jj\}	}
|	|ju sJ |
|ju sJ qtt t|t j W d    n	1 sw   Y  t|t j\}	}
|	|ju s/J |
|ju s7J d S )Nr   r   r   皙@)r   int64iinfor   maxcopyindicesastypeindptrr   r&   datar    r   safely_cast_index_arraysr(   r)   )r   rQ   imaxA32B32A64B64C64r+   rW   rY   r   r   r   !test_safely_cast_index_compressedv   s@        
z1TestSparseUtils.test_safely_cast_index_compressedc           	      C   s  t t t jj}td|f}| }tdd |jD |_td|d f}| }tdd |jD |_| }t 	|d gt 	dgf|_t 	dg|_
|jd jt jks[J |jd jt jksfJ |jd jt jksqJ |jd jt jks|J |jd jt jksJ ||||fD ]9}t|t j}|d jt jksJ t|t j}|d jt jksJ t||jd j}|d |jd u sJ qtt t|t j W d    n1 sw   Y  t|t j}|d |jd u sJ d S )Nr   c                 s       | ]	}| tjV  qd S N)rX   r   rS   .0cor   r   r   	<genexpr>       z=TestSparseUtils.test_safely_cast_index_coo.<locals>.<genexpr>c                 s   rc   rd   )rX   r   r   re   r   r   r   rh      ri   r   rR   )r   rS   rT   r   rU   r   rV   tuplecoordsr&   rZ   r    r   r[   r(   r)   )	r   r\   r]   r^   r_   r`   ra   r+   rk   r   r   r   test_safely_cast_index_coo   s6   
z*TestSparseUtils.test_safely_cast_index_cooc           	      C   s  t t t jj}td|f}| }|jt j|_td|d f}| }|jt j|_| }t 	|d g|_t 	dg|_
|jjt jksMJ |jjt jksVJ |jjt jks_J |jjt jkshJ |jjt jksqJ ||||fD ]/}t|t j}|jt jksJ t|t j}|jt jksJ t||jj}||ju sJ qwtt t|t j W d    n1 sw   Y  t|t j}||ju sJ d S )Nr   r5   rR   )r   rS   rT   r   rU   r   rV   offsetsrX   r&   rZ   r    r   r[   r(   r)   )	r   r\   r]   r^   r_   r`   ra   r+   rm   r   r   r   test_safely_cast_index_dia   s6   
z*TestSparseUtils.test_safely_cast_index_diac                 C   sb  t t t jj}|d }t jddd}t jddd}tt tj	||fddt d ||d< tt tj	||fddt d ||d< tt tj	||fddt d	 t jd
dd}t jd
dd}tt t	||ft d	 t jddd}t jddd}tt tj	||f|ddt d	 ||d< tt tj	||f|dt d	 d S )Nr   Z   uint32r   T)check_contentsr   r=   rS   Y      )maxvalrq   )rt   )
r   rS   rT   r   rU   onesr   r    r   get_index_dtype)r   r\   too_biga1a2r   r   r   test_get_index_dtype   sJ   
	z$TestSparseUtils.test_get_index_dtypezinput_shapes,target_shape)   r:   r      r   r   r           r   r   )r|   r:   r   r}   r   r   )r{   r~   )r|   r:   r   r}   r   r   )r~   r   )r   r   r<   rA   r      r4   )r   r   )r1   r}   ))r|   r   r:   r|   r   r   )r:   r   r   )r:   r|   r   r   )r   r1   r1   r   )r1   r1   )r   r   )r   r   rL   )r   r3   r1   r5   r1   r5       d   c                 C   s   t tj| | d S rd   )r   r   broadcast_shapes)r   input_shapestarget_shaper   r   r   test_broadcast_shapes_successes7  s   &z/TestSparseUtils.test_broadcast_shapes_successesr   r}   )r5   r1   )r   r   r   )r5   r:   )r1   r:   )r5   r}   )r   r1   r}   )r5   r1   r1   )r4   r   r   )
   r:   c                 C   s<   t tdd tj|  W d    d S 1 sw   Y  d S )Nzcannot be broadcastr#   )r(   r)   r   r   )r   r   r   r   r   test_broadcast_shapes_failures`  s   "z.TestSparseUtils.test_broadcast_shapes_failuresc                 C   s   t dgd}t|d d S )N)r   r=   )i  i )r   i23)r   check_shaper   )r   	new_shaper   r   r   test_check_shape_overflowo  s   z)TestSparseUtils.test_check_shape_overflowc                 C   s   g dg}t |}tt|t jsJ tt|t js J t|}d|d d d d f< t|| tj|dd}d|d d d d f< t|g dg d S )NrD   {   F)rV   r   r   r   )r   r&   
isinstancer   r	   r   r   r"   bcr   r   r   test_matrixs  s   



zTestSparseUtils.test_matrixc                 C   sr   g dg}t |}tt|t jsJ tt|t js J t|}d|d d d d f< t|g dg d S )NrD   r   r   )r   r&   r   r   asmatrixr	   r   r   r   r   r   test_asmatrix  s   


zTestSparseUtils.test_asmatrixN)__name__
__module____qualname__r   r,   r7   r9   rC   rF   rI   rK   rP   pytestmarkparametrizer   r   rb   rl   rn   rz   r   r   r   r   r   r   r   r   r   r
      s    

	
0**
<


	





















 !"#
%
r
   )__doc__numpyr   numpy.testingr   r   r   r(   scipy.sparser   r   r   r   r   r   scipy.sparse._sputilsr	   r
   r   r   r   r   <module>   s    