o
    i:                     @   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int32Zfloat32boolid)	r   sputilsZupcastnpr   Zfloat64complexfloatZ
complex128self r   Y/home/kim/smarthome/.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   Zgetdtyper   r   assert_raises
ValueErrorZ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      @      ?      @   Z16Fr   r      )r   r   Z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(   Tr+   Fz4Inexact indices into sparse matrices are not allowedr   r'   r)   r*   r,   r-   )r   r   Z	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 )Nr-   T)   r.   )      ?r.   Fr.   r.   r.   r.   )r.   )nonneg)r.   r4   )allow_nd)r.   r.   )r5   r6   r.   )r.   r.   )r   r   Z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 )Nr,   Tr   r.   r+   r   r.   r+   F)r   r   Z
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   r.   r+   F)r   r   Zismatrixr   Z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   Z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   r2   r+   )r7   r4   r   r   N)r#   	TypeErrorr   Zvalidateaxisr$   )r   Z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   rA   imaxA32B32A64B64C64r%   rG   rI   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)rH   r   rC   .0cor   r   r   	<genexpr>       z=TestSparseUtils.test_safely_cast_index_coo.<locals>.<genexpr>c                 s   rS   rT   )rH   r   r   rU   r   r   r   rX      rY   r   rB   )r   rC   rD   r   rE   r   rF   tuplecoordsr"   rJ   r   r   rK   r#   r$   )	r   rL   rM   rN   rO   rP   rQ   r%   r[   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   r.   rB   )r   rC   rD   r   rE   r   rF   offsetsrH   r"   rJ   r   r   rK   r#   r$   )	r   rL   rM   rN   rO   rP   rQ   r%   r]   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   Zuint32r   T)check_contentsr   r4   rC   Y      )maxvalr`   )rc   )
r   rC   rD   r   rE   Zonesr   r   r   Zget_index_dtype)r   rL   Ztoo_bigZa1Za2r   r   r   test_get_index_dtype   sJ   
	z$TestSparseUtils.test_get_index_dtypezinput_shapes,target_shape)   r1   r      r   r   r           ri   r   )rf   r1   r   rg   ri   ri   )re   rh   )rf   r1   r   rg   r   ri   )rh   rj   )ri   ri   r3   r8   r      r-   )r   r   )r+   rg   ))rf   rl   r1   rf   r   rk   )r1   r   rl   )r1   rf   rl   rm   )r   r+   r+   r   )r+   r+   )r   r   )r   r   r>   )r   r,   r+   r.   r+   r.       d   c                 C   s   t tj| | d S rT   )r   r   broadcast_shapes)r   input_shapesZtarget_shaper   r   r   test_broadcast_shapes_successes7  s   &z/TestSparseUtils.test_broadcast_shapes_successesrt   rg   )r.   r+   )ro   ro   rv   )r.   r1   )r+   r1   )r.   rg   )r   r+   rg   )r.   r+   r+   )r-   rn   rp   )
   r1   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   rs   )r   rt   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)rw   r4   )i  i )rw   i23)r   Zcheck_shaper   )r   Z	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 )Nr:   {   F)rF   rz   rz   rz   )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 )Nr:   rz   r{   )r   r"   r|   r   Zasmatrixr	   r   r}   r   r   r   test_asmatrix  s   


zTestSparseUtils.test_asmatrixN)__name__
__module____qualname__r   r&   r/   r0   r9   r;   r<   r=   r@   pytestmarkZparametrizer   r   rR   r\   r^   rd   ru   rx   ry   r   r   r   r   r   r   r
      s    

	
0**
<


	





















 !"#
%
r
   )__doc__numpyr   Znumpy.testingr   r   r   r#   Zscipy.sparser   r   r   r   r   r   Zscipy.sparse._sputilsr	   r
   r   r   r   r   <module>   s    