o
    i<                  	   @   s  d Z ddlZddlZddlmZmZ ddlmZm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZ ej Zee eeegZejejejgZejdd Zejd	d
 Zej !dee	e
egdd Z"ej !deeee	e
egdd Z#ej !deG dd dZ$dS )z*Test of 1D aspects of sparse array classes    N)assert_equalassert_allclose)	bsr_array	csc_array	dia_array	lil_array	coo_array	csr_array	dok_array)supported_dtypesmatrix)ComplexWarningc                   C   s   t g ddS )N)   r      r   d)nparray r   r   Z/home/kim/smarthome/.venv/lib/python3.10/site-packages/scipy/sparse/tests/test_common1d.pydat1d   s   r   c                    s$    fddt D fddtD S )Nc                    s   i | ]}|  |qS r   )astype).0dtyper   r   r   
<dictcomp>   s    z%datsp_math_dtypes.<locals>.<dictcomp>c                    s$   i | ]   fd d  D qS )c                    s   g | ]\}}|| |fqS r   r   )r   r   dat	spcreatorr   r   
<listcomp>!   s    z0datsp_math_dtypes.<locals>.<dictcomp>.<listcomp>)items)r   )
dat_dtypesr   r   r       s    )math_dtypes
spcreatorsr   r   )r   r    r   datsp_math_dtypes   s   
r#   r   c                 C   s@   t jtdd | g d W d    d S 1 sw   Y  d S )Nzarrays don't support 1D inputmatchr   r      r   pytestraises
ValueErrorr   r   r   r   test_no_1d_support_in_init(   s   "r,   c                 C   sB   t jtdd | td W d    d S 1 sw   Y  d S )Nzarrays don't.*support 3Dr$   )r   r'      )r)   r*   r+   r   Zonesr   r   r   r   test_no_nd_support_in_init0   s   "r.   c                   @   s,  e Zd 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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jed$d% Zejjed&d' Zejjed(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z d4d5 Z!d6d7 Z"d8d9 Z#d:d; Z$d<d= Z%d>d? Z&d@S )ATestCommon1Dz5test common functionality shared by 1D sparse formatsc                 C   s>   t |d td t |djd t |d d d S )Nr   r   r   )r   toarrayr   zerosZnnzZcount_nonzeroselfr   r   r   r   test_create_empty=   s   zTestCommon1D.test_create_emptyc                 C   s<   t jtdd |d W d    d S 1 sw   Y  d S )Nzelements cannot be negativer$   )r(   r3   r   r   r   test_invalid_shapesB   s   
"z TestCommon1D.test_invalid_shapesc                 C      t || d S N)reprr4   r   r   r   r   r   	test_reprF      zTestCommon1D.test_reprc                 C   r8   r9   )strr;   r   r   r   test_strI   r=   zTestCommon1D.test_strc                 C   s*   t g dd}t| ||   d S )N)r      r   r   r   r   r   r   r   r   r   r   r   r1   r4   r   Ar   r   r   test_negL   s   zTestCommon1D.test_negc                 C   s   |g d}|j dksJ d S )Nr&   r   )ndimrE   r   r   r   test_1d_supported_initP   s   z#TestCommon1D.test_1d_supported_initc                 C   s   |g d}| dd}g dg}t| | ||}| d}t| |d  | d}|j|jks7J |j d}t| |d  d S )Nr   r      r   r   r   r   r6   r   r   r      r      r   rM   )reshaper   r1   shapeT)r4   r   xydesiredy2r   r   r   $test_reshape_1d_tofrom_row_or_columnT   s   


z1TestCommon1D.test_reshape_1d_tofrom_row_or_columnc                 C   sv   |g d}| d}g dg dg dg dg}t| | | d}||u s+J | d}t| |  d S )	NrJ   )r-   r   )r   r   rK   r   r   r   )r   r6   r   )r   r   rL   rN   rM   )rO   r   r1   )r4   r   rR   rS   rT   r   r   r   test_reshapef   s   


zTestCommon1D.test_reshapec           	   	   C   s  t jd t g d}t jd}t g }t d}||||g}|D ]U}||}t jddA t | s;J t	| |  t	|jd d|jd d t	|jdd|jdd t	|jd	d|jd	d W d    n1 suw   Y  q%|jdt d
d d S )Ni  )	r   r   r'   r   rC   rL   rK   	   rL   )(   ignore)Zoveraxisr   r@   r   r^   out)
r   randomseedr   Zrandr2   Zerrstateisscalarsumr   )	r4   r   Zdat_1Zdat_2Zdat_3Zdat_4Zarraysr   datspr   r   r   test_sumr   s"   

zTestCommon1D.test_sumc                 C   s  t d}t g d}||}tjtdd |jdd W d    n1 s(w   Y  tjtdd |jdd W d    n1 sEw   Y  tjtd	d |jd
d W d    n1 sbw   Y  tjtdd |jd|d W d    d S 1 sw   Y  d S )Nr0   r   r   r'   zaxis must be None, -1 or 0r$   r   r]   Tuples are not acceptedr   r   axis must be an integer      ?"output parameter.*wrong.*dimensionr   r_   )r   r2   r   r)   r*   r+   rd   	TypeErrorr4   r   r`   r   re   r   r   r   test_sum_invalid_params   s   
"z$TestCommon1D.test_sum_invalid_paramsc                 C   s8   t g d}||}t |}t |}t|| d S Nrg   )r   r   rd   r   )r4   r   r   re   dat_sum	datsp_sumr   r   r   test_numpy_sum   s
   

zTestCommon1D.test_numpy_sumc                 C   s  t g d}||}t| |  t |jd dsJ t|jd d|jd d t|jdd|jdd t|jdd|jdd tjtdd |jdd W d    n1 s^w   Y  tjtdd |jdd W d    d S 1 s|w   Y  d S )	Nrg   r]   r   r@   r^   r$   r   )r   r   r   meanrc   r)   r*   r+   )r4   r   r   re   r   r   r   	test_mean   s   "zTestCommon1D.test_meanc                 C   s$  t t d}t g dg dg dg}||}tjtdd |jdd W d    n1 s2w   Y  tjtd	d |jd
d W d    n1 sOw   Y  tjtdd |jdd W d    n1 slw   Y  tjtdd |jd|d W d    d S 1 sw   Y  d S )N)r   r   rg   )r   rC   rL   )rY   rK   rZ   zaxis out of ranger$   r   r]   rh   ri   rj   rk   rl   r   r_   )	r   Zasarrayr2   r   r)   r*   r+   ru   rm   rn   r   r   r   test_mean_invalid_params   s   "z%TestCommon1D.test_mean_invalid_paramsc                 C   T   t g d}||}tD ]}|j|d}|j|d}t|| t|j|j qd S Nrg   r   )r   r   r   rd   r   r   r   )r4   r   r   re   r   rq   rr   r   r   r   test_sum_dtype      
zTestCommon1D.test_sum_dtypec                 C   rx   ry   )r   r   r   ru   r   r   r   )r4   r   r   re   r   dat_mean
datsp_meanr   r   r   test_mean_dtype   r|   zTestCommon1D.test_mean_dtypec                 C   s   t g d}||}t d}t d}|j|d |j|d t|| |jd|d |jd|d t|| tjtdd |jt dgd W d    n1 sUw   Y  tjtdd |jt dggd W d    d S 1 sxw   Y  d S )Nrg   r   r`   r_   zoutput parameter.*dimensionr$   )r   r   ru   r   r)   r*   r+   )r4   r   r   re   Zdat_outZ	datsp_outr   r   r   test_mean_out   s    



"zTestCommon1D.test_mean_outc                 C   sF   t g d}||}t |}t |}t|| t|j|j d S rp   )r   r   ru   r   r   r   )r4   r   r   re   r}   r~   r   r   r   test_numpy_mean   s   


zTestCommon1D.test_numpy_meanc                 C   s`   t g d}t|| | t g d}t|| | t||dd |d d S N)r'   r   r-   y      ?      @r   r@   int16rz   )r   r   r   r1   r   rE   r   r   r   test_from_array   s
    zTestCommon1D.test_from_arrayc                 C   s`   g d}t || | g d}t || t| t ||dd t|d d S r   )r   r1   r   r   r   rE   r   r   r   test_from_list   s   zTestCommon1D.test_from_listc                 C   s   t g d}t|}t|| | ||}t|| | t g d}t|}t|| | t||dd |d ||}t|| | t||dd |d d S )N)r   r   r   r   r   rz   )r   r   r   r   r1   r   r4   r   DSr   r   r   test_from_sparse  s    zTestCommon1D.test_from_sparsec                 C   s`  ||}|  }t|| |jj|jjksJ |j dd}t|| |jjs(J |jjs.J |j dd}t|| |jjs?J |jjsEJ tj|j|jd}|j |d t|| d|d< |j |d t|| t	g d}t
||}t
||  }t|| t	g d}	t
||	}t
|  |	}t|| ||td}
|t}t|
  | d S )	NC)orderFrz   r         ?.)r          @      @g      @)r1   r   flagsc_contiguousf_contiguousr   r2   rP   r   r   dotboolr   )r4   r   r   re   Zchkr`   aZdense_dot_densecheckbZspboolZarrboolr   r   r   test_toarray  s:   







zTestCommon1D.test_toarrayc           	      C   sl   || D ]/\}}}|  }d|d< |}|| }t|| |  ||dd  }t|| |d   qd S )Nr   r   r   copyr   r1   	r4   r   r#   r   r   re   r   r   cr   r   r   test_addC  s   zTestCommon1D.test_addc           	      C   sF   || D ]\}}}|  }d|d< |}|| }t|||   qd S )Nr   r   r   r   r   r   r   	test_raddP  s   zTestCommon1D.test_raddc                 C   s   || D ]r\}}}|t dkrqt|| g d t|| g d td|  |  |g ddd}t|| ||   t|| | |  t| | | |  t||  ||   t|d d | |d d |  qd S )Nr   )r   r   r   r   r   )r   rC   r   r'   r   rz   r   )r   r   r   r1   )r4   r   r#   r   r   re   rF   r   r   r   	test_rsubX  s   $zTestCommon1D.test_rsubc                 C   s"  t g dg dg dg}t g d}||}||}t||  ||  t|| ||  t|| ||  t||  ||  t|| ||  t|| ||  t|| ||  t|| ||  t|| ||  t||  ||  t|| ||  t|| ||  d S )Nr'   r   r   rW   rg   r   r   r   rD   )r4   r   rF   vAspZvspr   r   r   test_matmul_basick  s    zTestCommon1D.test_matmul_basicc              	   C   s  t g d}||}t g dgj}t|| | |  |t g d jdks-J |t g d dks:J |t g d jdksHJ |t dgdgdgg jdksZJ t|tg dgj t jsjJ t ddgt g d	t dgdggg}|D ]}t	j
td
d ||  W d    n1 sw   Y  qt | g d}t|t g d | t|dgdgdgg |j d S )Nr   )r   r'   r   r      r   r'   r   )r   r   r'   r   r-   zdimension mismatchr$   )r   r   rQ   r   r1   rP   
isinstancer   Zndarrayr)   r*   r+   r   )r4   r   rF   r   colZbad_vecsrR   Z
dot_resultr   r   r   test_matvec  s$   $ ,
 zTestCommon1D.test_matvecc                 C   sT   ||}t g d| tg d|  tg dg}t || ||   d S )Nr   )r   r   r   r1   r   )r4   r   r   Mrowr   r   r   test_rmatvec  s   "zTestCommon1D.test_rmatvecc                 C   sN   |t g fD ]}||}t| | t|  | t|j|j qd S r9   )r   r   r   r1   Z	transposer   )r4   r   r   rF   Br   r   r   test_transpose  s   zTestCommon1D.test_transposec                 C   sD   || D ]\}}}|| }t |||  || }t |||  qd S r9   )r   )r4   r   r#   r   r   re   Zsum1Zsum2r   r   r   test_add_dense_to_sparse  s   z%TestCommon1D.test_add_dense_to_sparsec                 C   sB   t d}||}|jdvrt||D ]\}}t|| qd S d S )NrL   )ZcooZdiaZbsr)r   Zarangeformatzipr   )r4   r   r   rF   rR   rS   r   r   r   test_iterator  s   

zTestCommon1D.test_iteratorc                 C   sZ   t g d}||}|dd u sJ t| g d |d t| g d d S )N)r   r   r   r-   r0   r   )rL   )r   r   r   r   r   )r   r   resizer   r1   r   r   r   r   test_resize  s   
zTestCommon1D.test_resizeN)'__name__
__module____qualname____doc__r5   r7   r<   r?   rG   rI   rV   rX   rf   ro   rs   rv   rw   r{   r   r   r   r)   markZthread_unsafesup_complexr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r/   9   sN    	

.	r/   )%r   r)   numpyr   Znumpy.testingr   r   Zscipy.sparser   r   r   r   r   r	   r
   Zscipy.sparse._sputilsr   r   Zscipy._lib._utilr   ZtestingZsuppress_warningsr   filterr"   Zint64Zfloat64Z
complex128r!   Zfixturer   r#   r   Zparametrizer,   r.   r/   r   r   r   r   <module>   s.    $







