o
    5Bi?                     @   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/Trade-python/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spr   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   	spcreatorc                 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
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sJ t |d ds&J d S )Nr	   r	   r   )r   array_equaltoarrayzerosnnz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    d S 1 sw   Y  d S )Nzelements cannot be negativer    )r$   r0   r   r   r   test_invalid_shapes7   s   
"z TestCommon1D.test_invalid_shapesc                 C      t || d S N)reprr1   r   r   r   r   r   	test_repr;      zTestCommon1D.test_reprc                 C   r5   r6   )strr8   r   r   r   test_str>   r:   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,   r1   r   Ar   r   r   test_negA   s    zTestCommon1D.test_negc                 C   s   |g d}|j dksJ d S )Nr"   r
   )ndimrA   r   r   r   test_1d_supported_initE   s   z#TestCommon1D.test_1d_supported_initc                 C   s   |g d}| dd}g dg}t| |sJ ||}| d}t| |d s0J | d}|j|jks=J |j d}t| |d sOJ d S )Nr
   r      r   r   r   r   r3   r   r   r      r
      r   rI   )reshaper   r+   r,   shapeT)r1   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| |s#J | d}||u s.J | d}t| | s?J d S )	NrF   )   r	   )r
   r   rG   )r   r   r   )r   r3   r   )r   r   rH   rJ   rI   )rK   r   r+   r,   )r1   r   rN   rO   rP   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 ]a}||}t jddM t | s;J t 	| | sGJ t 	|jd d|jd dsWJ t 	|jdd|jddsgJ t 	|jd	d|jd	dswJ W d    n1 sw   Y  q%|jdt d
d d S )Ni  )	r   r
   r#   r	   r@   rH   rG   	   rH   )(   ignore)overaxisr   r=   r   r[   out)
r   randomseedr   randr-   errstateisscalarsumallclose)	r1   r   dat_1dat_2dat_3dat_4arraysr   datspr   r   r   test_sumg   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 )Nr*   r   r
   r#   zaxis must be None, -1 or 0r    r
   rZ   Tuples are not acceptedr   r
   axis must be an integer      ?dimensions do not matchr   r\   )r   r-   r   r%   r&   r'   rc   	TypeErrorr1   r   r]   r   rj   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 Nrl   )r   r   rc   rd   )r1   r   r   rj   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s"J t |jd d|jd ds2J t |jdd|jddsBJ t |jdd|jddsRJ tjtdd |jdd W d    n1 sjw   Y  tjtdd |jdd W d    d S 1 sw   Y  d S )	Nrl   rZ   r   r=   r[   r    r
   )r   r   rd   meanrb   r%   r&   r'   )r1   r   r   rj   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r9tjtdd || W d    d S 1 s2w   Y  d S ||}tjtdd |jd	d
 W d    n1 sUw   Y  tjt	dd |jdd
 W d    n1 srw   Y  tjt	dd |jdd
 W d    n1 sw   Y  tjtdd |jd|d W d    d S 1 sw   Y  d S )N)r
   r	   rl   )r	   r@   rH   )rU   rG   rV   unizqr    zaxis out of ranger	   rZ   rm   rn   ro   rp   rq   r
   r\   )
r   asarrayr-   r   _formatr%   r&   r'   rz   rr   rs   r   r   r   test_mean_invalid_params   s,   


"z%TestCommon1D.test_mean_invalid_paramsc                 C   `   t g d}||}tD ] }|j|d}|j|d}t ||s#J t |j|js-J qd S Nrl   r   )r   r   r   rc   rd   r+   r   )r1   r   r   rj   r   rv   rw   r   r   r   test_sum_dtype      zTestCommon1D.test_sum_dtypec                 C   r   r   )r   r   r   rz   rd   r+   r   )r1   r   r   rj   r   dat_mean
datsp_meanr   r   r   test_mean_dtype   r   zTestCommon1D.test_mean_dtypec                 C   s   t g d}||}t dg}t dg}|j|dd |j|d t ||s,J |jd|dd |jd|d t ||sCJ d S )Nrl   r   T)r]   keepdimsr]   )r[   r]   r   r\   )r   r   rz   rd   )r1   r   r   rj   dat_out	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s'J d S ru   )r   r   rz   rd   r+   r   )r1   r   r   rj   r   r   r   r   r   test_numpy_mean   s   

zTestCommon1D.test_numpy_meanc                 C   sr   t g d}t || |sJ t g d}t || |s&J t ||dd |ds7J d S N)r#   r	   rS   y      ?      @r   r=   int16r   )r   r   r+   r,   r   rA   r   r   r   test_from_array   s
   &zTestCommon1D.test_from_arrayc                 C   sr   g d}t || |sJ g d}t || t |s#J t ||dd t |ds7J d S r   )r   r+   r,   r   r   rA   r   r   r   test_from_list   s   zTestCommon1D.test_from_listc                 C   s   t g d}tj|}t || |sJ ||}t || |s)J t g d}tj|}t || |sBJ t ||dd |dsSJ ||}t || |scJ t ||dd |dstJ d S )N)r
   r   r   r   r   r   )r   r   r   sparse	coo_arrayr+   r,   r   r1   r   DSr   r   r   test_from_sparse   s   "&zTestCommon1D.test_from_sparsec                 C   s  ||}|  }t||sJ |jj|jjksJ |j dd}t||s(J |jjs.J |jjs4J |j dd}t||sBJ |jjsHJ |jjsNJ tj|j|jd}|j |d t||seJ d|d< |j |d t||swJ t	g d}t
||}t
||  }t||sJ t	g d}	t
||	}t
|  |	}t||sJ ||td}
|t}t|
  |sJ d S )	NC)orderFr   r         ?.)r          @      @g      @)r,   r   r+   flagsc_contiguousf_contiguousr-   rL   r   r   dotboolr   )r1   r   r   rj   chkr]   adense_dot_densecheckbspboolarrboolr   r   r   test_toarray  s:   
zTestCommon1D.test_toarrayc           	      C   sx   || D ]5\}}}|  }d|d< |}|| }t|| | s#J ||dd  }t|| |d  s9J qd S )Nr   r   r
   copyr   r+   r,   	r1   r   r   r   r   rj   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s)J t d|  | s6J |g ddd}t || ||  sLJ t || | | sZJ t | | | | sjJ t ||  ||  szJ t |d d | |d d | sJ qd S )Nr   )r   r   r   r   r   )r
   r@   r   r#   r   r   r
   )r   r   r+   r,   )r1   r   r   r   r   rj   rB   r   r   r   	test_rsubJ  s     *zTestCommon1D.test_rsubc              	   C   s  t g d}||}t g dgj}t || | | s"J |t g d jdks0J |t g d dks=J |t g d jdksKJ |t dgdgdgg jdks]J t|tg dgj t jsmJ |t g dgj jdks}J 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 |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	   rS   zdimension mismatchr    )r   r   rM   rd   r,   rL   
isinstancer   ndarrayr%   r&   r'   
__matmul__r   )r1   r   rB   Aspcolbad_vecsrN   
dot_resultr   r   r   test_matvec]  s&   $  ,&zTestCommon1D.test_matvecc                 C   s`   ||}t g d| t g d| sJ t g dg}t || ||  s.J d S )Nr   )r   rd   r   r,   r   )r1   r   r   Mrowr   r   r   test_rmatvecy  s   ( zTestCommon1D.test_rmatvecc                 C   s`   |t g fD ]&}||}t | |sJ t |  |s#J t |j|js-J qd S r6   )r   r   r+   r,   	transposer   )r1   r   r   rB   Br   r   r   test_transpose  s   zTestCommon1D.test_transposec                 C   sP   || D ]!\}}}|| }t ||| sJ || }t ||| s%J qd S r6   )r   r+   )r1   r   r   r   r   rj   sum1sum2r   r   r   test_add_dense_to_sparse  s   z%TestCommon1D.test_add_dense_to_sparsec                 C   sH   t d}||}|jdvr t||D ]\}}t ||sJ qd S d S )NrH   )coodiabsr)r   arangeformatzipr+   )r1   r   r   rB   rN   rO   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s1J d S )N)r
   r   r	   rS   r*   )r
   r   r	   )rH   )r
   r   r	   r   r   )r   r   resizer+   r,   r   r   r   r   test_resize  s   
zTestCommon1D.test_resizeN)#__name__
__module____qualname____doc__r2   r4   r9   r<   rC   rE   rR   rT   rk   rt   rx   r{   r   r   r   r   r   sup_complexr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   .   sF    	




.	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    n1 sBw   Y  q'|d dksPJ tjtdd |	d W d    d S 1 shw   Y  d S )	N)rS   r	   r   r   )r	   r@   index value out of boundsr    )r   rS   )rS   )
r   r   rL   ranger+   r%   r&   
IndexErrorrr   __getitem__)r1   r   r   rB   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    n1 s[w   Y  qAW d    d S 1 slw   Y  d S )NrJ   r   z?Changing the sparsity structure of a cs[cr]_matrix is expensiver   r	   r
   g      "@   rG   ry   rV   rH   )i)r
   )   i)r   )   r   r    g     ^@)r   float64testingsuppress_warningsfilterr   r   SparseEfficiencyWarningr%   r&   r   __setitem__)r1   r   r   rB   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   scipyr   scipy.sparser   r   r   r   scipy.sparse._sputilsr   r   scipy._lib._utilr   r   r   r   r   r   r   	dok_arrayr   int64r   
complex128r   fixturer   r   markparametrizer(   r)   r   r   r   r   r   <module>   s.    





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