a
    h?                     @   s   d Z ddlZddlZddlZddl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jjejj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G dd dZ"ej d
ejjgG dd dZ#dS )z*Test of 1D aspects of sparse array classes    N)	bsr_array	csc_array	dia_array	lil_array)supported_dtypesmatrix)ComplexWarningc                   C   s   t g ddS )N)   r      r   d)nparray r   r   \/var/www/html/assistant/venv/lib/python3.9/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>       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spr   r   
<listcomp>    r   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   	spcreatorc                 C   s>   t jtdd | g d W d    n1 s00    Y  d S )Nzarrays don't support 1D inputmatchr   r
      r	   pytestraises
ValueError)r    r   r   r   test_no_1d_support_in_init'   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d$d% Zed&d' Z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>S )?TestCommon1Dz5test common functionality shared by 1D sparse formatsc                 C   sP   t |d t dsJ t |djds4J t |d dsLJ d S )Nr	   r	   r   )r   array_equaltoarrayzerosZnnzZcount_nonzeroselfr    r   r   r   test_create_empty2   s    zTestCommon1D.test_create_emptyc                 C   s:   t jtdd |d W d    n1 s,0    Y  d S )Nzelements cannot be negativer!   )r%   r/   r   r   r   test_invalid_shapes7   s    z TestCommon1D.test_invalid_shapesc                 C   s   t || d S N)reprr0   r    r   r   r   r   	test_repr;   s    zTestCommon1D.test_reprc                 C   s   t || d S r4   )strr6   r   r   r   test_str>   s    zTestCommon1D.test_strc                 C   s0   t g dd}t | ||  s,J d S )N)r      r   r   r
   r   r   r   r   r   )r   r   r,   r-   r0   r    Ar   r   r   test_negA   s    zTestCommon1D.test_negc                 C   s   |g d}|j dksJ d S )Nr#   r
   )ndimr>   r   r   r   test_1d_supported_initE   s    z#TestCommon1D.test_1d_supported_initc                 C   s   |g d}| dd}g dg}t| |s6J ||}| d}t| |d s`J | d}|j|jkszJ |j d}t| |d sJ d S )Nr
   r      r   r   r   r   r2   r   r   r      r
      r   rF   )reshaper   r,   r-   shapeT)r0   r    xydesiredy2r   r   r   $test_reshape_1d_tofrom_row_or_columnI   s    


z1TestCommon1D.test_reshape_1d_tofrom_row_or_columnc                 C   s   |g d}| d}g dg dg dg dg}t| |sFJ | d}||u s\J | d}t| | s~J d S )	NrC   )   r	   )r
   r   rD   )r   r   r   )r   r2   r   )r   r   rE   rG   rF   )rH   r   r,   r-   )r0   r    rK   rL   rM   r   r   r   test_reshape[   s    


zTestCommon1D.test_reshapec           	   	   C   s(  t jd t g d}t jd}t g }t d}||||g}|D ]}||}t jdd t | svJ t 	| | sJ t 	|jd d|jd dsJ t 	|jdd|jddsJ t 	|jd	d|jd	dsJ W d    qJ1 s0    Y  qJ|jdt d
d d S )Ni  )	r   r
   r$   r	   r=   rE   rD   	   rE   )(   ignore)Zoveraxisr   r:   r   rW   out)
r   randomseedr   Zrandr.   Zerrstateisscalarsumallclose)	r0   r    Zdat_1Zdat_2Zdat_3Zdat_4Zarraysr   datspr   r   r   test_sumg   s    

  BzTestCommon1D.test_sumc                 C   s  t d}t g d}||}tjtdd |jdd W d    n1 sP0    Y  tjtdd |jdd W d    n1 s0    Y  tjtd	d |jd
d W d    n1 s0    Y  tjtdd |jd|d W d    n1 s0    Y  d S )Nr+   r   r
   r$   zaxis must be None, -1 or 0r!   r
   rV   Tuples are not acceptedr   r
   axis must be an integer      ?dimensions do not matchr   rX   )r   r.   r   r&   r'   r(   r]   	TypeErrorr0   r    rY   r   r_   r   r   r   test_sum_invalid_params{   s    
***z$TestCommon1D.test_sum_invalid_paramsc                 C   s>   t g d}||}t |}t |}t ||s:J d S Nra   )r   r   r]   r^   )r0   r    r   r_   dat_sum	datsp_sumr   r   r   test_numpy_sum   s
    

zTestCommon1D.test_numpy_sumc                 C   s  t g d}||}t | | s.J t |jd dsDJ t |jd d|jd dsdJ t |jdd|jddsJ t |jdd|jddsJ tjtdd |jdd W d    n1 s0    Y  tjtdd |jdd W d    n1 s0    Y  d S )	Nra   rV   r   r:   rW   r!   r
   )r   r   r^   meanr\   r&   r'   r(   )r0   r    r   r_   r   r   r   	test_mean   s       *zTestCommon1D.test_meanc                 C   sj  t t d}t g dg dg dg}|jdkrptjtdd || W d    n1 sb0    Y  d S ||}tjtdd |jd	d
 W d    n1 s0    Y  tjt	dd |jdd
 W d    n1 s0    Y  tjt	dd |jdd
 W d    n1 s0    Y  tjtdd |jd|d W d    n1 s\0    Y  d S )N)r
   r	   ra   )r	   r=   rE   )rR   rD   rS   uniZzqr!   zaxis out of ranger	   rV   rb   rc   rd   re   rf   r
   rX   )
r   Zasarrayr.   r   _formatr&   r'   r(   ro   rg   rh   r   r   r   test_mean_invalid_params   s    
&**,z%TestCommon1D.test_mean_invalid_paramsc                 C   s`   t g d}||}tD ]@}|j|d}|j|d}t ||sFJ t |j|jsJ qd S Nra   r   )r   r   r   r]   r^   r,   r   )r0   r    r   r_   r   rk   rl   r   r   r   test_sum_dtype   s    zTestCommon1D.test_sum_dtypec                 C   s`   t g d}||}tD ]@}|j|d}|j|d}t ||sFJ t |j|jsJ qd S rt   )r   r   r   ro   r^   r,   r   )r0   r    r   r_   r   dat_mean
datsp_meanr   r   r   test_mean_dtype   s    zTestCommon1D.test_mean_dtypec                 C   s   t g d}||}t dg}t dg}|j|dd |j|d t ||sXJ |jd|dd |jd|d t ||sJ d S )Nra   r   T)rY   keepdimsrY   )rW   rY   rz   rX   )r   r   ro   r^   )r0   r    r   r_   Zdat_outZ	datsp_outr   r   r   test_mean_out   s    zTestCommon1D.test_mean_outc                 C   sR   t g d}||}t |}t |}t ||s:J t |j|jsNJ d S rj   )r   r   ro   r^   r,   r   )r0   r    r   r_   rw   rx   r   r   r   test_numpy_mean   s    

zTestCommon1D.test_numpy_meanc                 C   sr   t g d}t || |s&J t g d}t || |sLJ t ||dd |dsnJ d S N)r$   r	   rP   y      ?      @r   r:   int16ru   )r   r   r,   r-   r   r>   r   r   r   test_from_array   s
    zTestCommon1D.test_from_arrayc                 C   sr   g d}t || |s J g d}t || t |sFJ t ||dd t |dsnJ d S r~   )r   r,   r-   r   r   r>   r   r   r   test_from_list   s    zTestCommon1D.test_from_listc                 C   s   t g d}tj|}t || |s2J ||}t || |sRJ t g d}tj|}t || |sJ t ||dd |dsJ ||}t || |sJ t ||dd |dsJ d S )N)r
   r   r   r   r   ru   )r   r   r   sparse	coo_arrayr,   r-   r   r0   r    DSr   r   r   test_from_sparse   s    "zTestCommon1D.test_from_sparsec                 C   s  ||}|  }t||s J |jj|jjks4J |j dd}t||sPJ |jjs\J |jjshJ |j dd}t||sJ |jjsJ |jjsJ tj|j|jd}|j |d t||sJ d|d< |j |d t||sJ t	g d}t
||}t
||  }t||s*J t	g d}	t
||	}t
|  |	}t||sfJ ||td}
|t}t|
  |sJ d S )	NC)orderFru   r{         ?.)r          @      @g      @)r-   r   r,   flagsc_contiguousf_contiguousr.   rI   r   r   dotboolr   )r0   r    r   r_   ZchkrY   aZdense_dot_densecheckbZspboolZarrboolr   r   r   test_toarray  s:    
zTestCommon1D.test_toarrayc           	      C   sx   || D ]j\}}}|  }d|d< |}|| }t|| | sFJ ||dd  }t|| |d  sJ qd S )Nr   r   r
   copyr   r,   r-   	r0   r    r   r   r   r_   r   r   cr   r   r   test_add5  s    zTestCommon1D.test_addc           	      C   sL   || D ]>\}}}|  }d|d< |}|| }t|||  sJ qd S )Nr   r   r   r   r   r   r   	test_raddB  s    zTestCommon1D.test_raddc                 C   s$  || D ]\}}}|t dkr$qt || g ds<J t || g dsTJ t d|  | snJ |g ddd}t || ||  sJ t || | | sJ t | | | | sJ t ||  ||  sJ t |d d | |d d | sJ qd S )Nr   )r   r   r   r   r   )r
   r=   r   r$   r   ru   r
   )r   r   r,   r-   )r0   r    r   r   r   r_   r?   r   r   r   	test_rsubJ  s      zTestCommon1D.test_rsubc              	   C   s  t g d}||}t g dgj}t || | | sDJ |t g d jdks`J |t g d dkszJ |t g d jdksJ |t dgdgdgg jdksJ t|tg dgj t jsJ |t g dgj jdksJ t ddgt g dt dgdggg}|D ]@}t	j
td	d
 || W d    n1 s^0    Y  q*t | g d}t |t g d |sJ t |dgdgdgg |jsJ d S )N)r$   r   r   )r
   r$   r	   r      r
   r$   r	   r
   r$   r	   rP   zdimension mismatchr!   )r   r   rJ   r^   r-   rI   
isinstancer   Zndarrayr&   r'   r(   
__matmul__r   )r0   r    r?   ZAspcolZbad_vecsrK   Z
dot_resultr   r   r   test_matvec]  s"    $  ,. zTestCommon1D.test_matvecc                 C   s`   ||}t g d| t g d| s0J t g dg}t || ||  s\J d S )Nr   )r   r^   r   r-   r   )r0   r    r   Mrowr   r   r   test_rmatvecy  s    (zTestCommon1D.test_rmatvecc                 C   s`   |t g fD ]L}||}t | |s.J t |  |sFJ t |j|jsJ qd S r4   )r   r   r,   r-   Z	transposer   )r0   r    r   r?   Br   r   r   test_transpose  s
    zTestCommon1D.test_transposec                 C   sP   || D ]B\}}}|| }t ||| s.J || }t ||| sJ qd S r4   )r   r,   )r0   r    r   r   r   r_   Zsum1Zsum2r   r   r   test_add_dense_to_sparse  s
    z%TestCommon1D.test_add_dense_to_sparsec                 C   sD   t d}||}|jdvr@t||D ]\}}t ||s&J q&d S )NrE   )ZcooZdiaZbsr)r   Zarangeformatzipr,   )r0   r    r   r?   rK   rL   r   r   r   test_iterator  s
    

zTestCommon1D.test_iteratorc                 C   sf   t g d}||}|dd u s(J t | g ds@J |d t | g dsbJ d S )N)r
   r   r	   rP   r+   )r
   r   r	   )rE   )r
   r   r	   r   r   )r   r   resizer,   r-   r   r   r   r   test_resize  s    
zTestCommon1D.test_resizeN)#__name__
__module____qualname____doc__r1   r3   r7   r9   r@   rB   rO   rQ   r`   ri   rm   rp   rs   rv   ry   r|   r}   sup_complexr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r*   .   sD   	




.	r*   c                   @   s   e Zd Zdd Zdd ZdS )TestGetSet1Dc              	   C   s   t g d}||}|jd }t| |D ]}t || || s,J q,dD ]@}tjttfdd |	| W d    qN1 s0    Y  qN|d dksJ tjtdd |	d W d    n1 s0    Y  d S )	N)rP   r	   r   r   )r	   r=   index value out of boundsr!   )r   rP   )rP   )
r   r   rI   ranger,   r&   r'   
IndexErrorrg   __getitem__)r0   r    r   r?   Njijr   r   r   test_getelement  s    
*zTestGetSet1D.test_getelementc              
   C   s   t j}|d|d}t j }|tjjd |d|d< |d|d< |d|d< |d	|d
< d|d< |d|d< |d|d< dD ]>}tj	t
dd ||d W d    q1 s0    Y  qW d    n1 s0    Y  d S )NrG   ru   z?Changing the sparsity structure of a cs[cr]_matrix is expensiver   r	   r
   g      "@   rD   rn   rS   rE   )i)r
   )   i)r   )   r   r!   g     ^@)r   float64testingsuppress_warningsfilterr   r   ZSparseEfficiencyWarningr&   r'   r   __setitem__)r0   r    r   r?   supr   r   r   r   test_setelement  s"    zTestGetSet1D.test_setelementN)r   r   r   r   r   r   r   r   r   r     s   r   )$r   r&   numpyr   Zscipyr   Zscipy.sparser   r   r   r   Zscipy.sparse._sputilsr   r   Zscipy._lib._utilr   r   r   r   r   r   r   Z	dok_arrayr   Zint64r   Z
complex128r   Zfixturer   r   markZparametrizer)   r*   r   r   r   r   r   <module>   s,   





  s