o
    ?Hh;                  	   @   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   `/home/air/sanwanet/gpt-api/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   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   zerosnnz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(   r6   r   r   r   test_invalid_shapesB   s   
"z TestCommon1D.test_invalid_shapesc                 C      t || d S N)reprr7   r   r   r   r   r   	test_reprF      zTestCommon1D.test_reprc                 C   r;   r<   )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   r2   r7   r   Ar   r   r   test_negL   s   zTestCommon1D.test_negc                 C   s   |g d}|j dksJ d S )Nr&   r   )ndimrH   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   r9   r   r   r      r      r   rP   )reshaper   r2   shapeT)r7   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 )	NrM   )r-   r   )r   r   rN   r   r   r   )r   r9   r   )r   r   rO   rQ   rP   )rR   r   r2   )r7   r   rU   rV   rW   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   rF   rO   rN   	   rO   )(   ignore)overaxisr   rC   r   rb   out)
r   randomseedr   randr3   errstateisscalarsumr   )	r7   r   dat_1dat_2dat_3dat_4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 )Nr1   r   r   r'   zaxis must be None, -1 or 0r$   r   ra   Tuples are not acceptedr   r   axis must be an integer      ?dimensions do not matchr   rc   )r   r3   r   r)   r*   r+   rj   	TypeErrorr7   r   rd   r   rp   r   r   r   test_sum_invalid_params   s   
"z$TestCommon1D.test_sum_invalid_paramsc                 C   s8   t g d}||}t |}t |}t|| d S Nrr   )r   r   rj   r   )r7   r   r   rp   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 )	Nrr   ra   r   rC   rb   r$   r   )r   r   r   meanri   r)   r*   r+   )r7   r   r   rp   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   rr   )r   rF   rO   )r\   rN   r]   zaxis out of ranger$   r   ra   rs   rt   ru   rv   rw   r   rc   )	r   asarrayr3   r   r)   r*   r+   r   rx   ry   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 Nrr   r   )r   r   r   rj   r   r   r   )r7   r   r   rp   r   r|   r}   r   r   r   test_sum_dtype      
zTestCommon1D.test_sum_dtypec                 C   r   r   )r   r   r   r   r   r   r   )r7   r   r   rp   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|| |jd|dd |jd|d t|| d S )Nrr   r   T)rd   keepdimsrd   )rb   rd   r   rc   )r   r   r   r   )r7   r   r   rp   dat_out	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 r{   )r   r   r   r   r   r   )r7   r   r   rp   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   rC   int16r   )r   r   r   r2   r   rH   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   r2   r   r   r   rH   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   r   )r   r   r   r   r2   r   r7   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Fr   r         ?.)r          @      @g      @)r2   r   flagsc_contiguousf_contiguousr   r3   rS   r   r   dotboolr   )r7   r   r   rp   chkrd   adense_dot_densecheckbspbool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   r2   	r7   r   r#   r   r   rp   r   r   cr   r   r   test_add>  s   zTestCommon1D.test_addc           	      C   sF   || D ]\}}}|  }d|d< |}|| }t|||   qd S )Nr   r   r   r   r   r   r   	test_raddK  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   rF   r   r'   r   r   r   )r   r   r   r2   )r7   r   r#   r   r   rp   rI   r   r   r   	test_rsubS  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   rZ   rr   r   r   r   rG   )r7   r   rI   vAspvspr   r   r   test_matmul_basicf  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   rT   r   r2   rS   
isinstancer   ndarrayr)   r*   r+   r   )r7   r   rI   r   colbad_vecsrU   
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   r2   r   )r7   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 r<   )r   r   r   r2   	transposer   )r7   r   r   rI   Br   r   r   test_transpose  s   zTestCommon1D.test_transposec                 C   sD   || D ]\}}}|| }t |||  || }t |||  qd S r<   )r   )r7   r   r#   r   r   rp   sum1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 )NrO   )coodiabsr)r   arangeformatzipr   )r7   r   r   rI   rU   rV   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-   r1   r   )rO   )r   r   r   r   r   )r   r   resizer   r2   r   r   r   r   test_resize  s   
zTestCommon1D.test_resizeN)'__name__
__module____qualname____doc__r8   r:   r?   rB   rJ   rL   rY   r[   rq   rz   r~   r   r   r   r   r   r   r)   markthread_unsafesup_complexr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r0   9   sN    	

.	r0   )%r   r)   numpyr   numpy.testingr   r   scipy.sparser   r   r   r   r   r	   r
   scipy.sparse._sputilsr   r   scipy._lib._utilr   testingsuppress_warningsr   filterr"   int64float64
complex128r!   fixturer   r#   r   parametrizer,   r/   r0   r   r   r   r   <module>   s.    $







