o
    ?Hhc.                     @   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	 ddl
mZ ee	gZejejejgZdd Zejdd	 Zejd
d ZejdeG dd dZdS )z Test of 1D arithmetic operations    N)assert_equalassert_allclose)	coo_array	csr_array)isscalarlikec                 C   s    t | tjs
t| r| S |  S )N)
isinstancenpndarrayr   toarray)a r   d/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/sparse/tests/test_arithmetic1d.pyr
      s   r
   c                   C   s   t g ddS )N)   r      r   d)r   arrayr   r   r   r   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                   @   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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%S )&TestArithmetic1Dc                 C   s   d}t dt dt dt dt dfD ]N}|||d}|| }d| }t||  t js4J t||  t js@J |||fD ] }|| | |  ksUJ |j|ks\J t|j|kseJ qEqd S )	N)   int32float32float64	complex64
complex128r      )r   r   r   tocsrr	   tocoor
   )selfr!   shapemytyper   bcmr   r   r   test_empty_arithmetic%   s$   z&TestArithmetic1D.test_empty_arithmeticc                 C   s.   t g dd}tt|t||  d S )N)r      r   r   r   r   r   r   r   r   )r   r   r   absr
   )r-   r!   Ar   r   r   test_abs8   s   zTestArithmetic1D.test_absc                 C   s<   t g dd}||}tt j|ddt|dd  d S )N)ggQ?g     @1@gQr   r   )decimals)ndigits)r   r   r   aroundroundr
   r-   r!   r9   Aspr   r   r   
test_round<   s   $zTestArithmetic1D.test_roundc                 C   sv   t g dd}||}tt |d|d  tjtdd ||| W d    d S 1 s4w   Y  d S )N)	r7   r4   r   r   r*   r      r   r*   zinput is not scalarmatch)r   r   r   powerr
   pytestraisesNotImplementedErrorr?   r   r   r   test_elementwise_powerA   s   "z'TestArithmetic1D.test_elementwise_powerc                 C   ,   t ddg}||}t|j |j d S Ny      ?      @y       @      )r   r   r   realr
   r-   r!   Dr9   r   r   r   	test_realJ      zTestArithmetic1D.test_realc                 C   rL   rM   )r   r   r   imagr
   rO   r   r   r   	test_imagO   rR   zTestArithmetic1D.test_imagc                 C   sD   || D ]\}}}t |d |d   t |d |d   qd S Nr*   gL1@r   r
   r-   r!   r    r   r   datspr   r   r   test_mul_scalarT      z TestArithmetic1D.test_mul_scalarc                 C   sD   || D ]\}}}t d| d|   t d| d|   qd S rU   rV   rW   r   r   r   test_rmul_scalarY   rZ   z!TestArithmetic1D.test_rmul_scalarc                 C   s   || D ]S\}}}|t dkrqt||  t d t|d  | |g ddd}t||  ||   t||  | |  t| |d  ||d   qd S )NboolrD   r   )r   r7   r   r*   r   r)   )r   r   r   r
   zeros)r-   r!   r    r   r   rX   r9   r   r   r   test_sub^   s    zTestArithmetic1D.test_subc                    sp   || D ]1\} t d    tfddtddD }t fddtddD }t| | qd S )Nr   c                       g | ]}|  qS r   r   r   k)rX   r   r   r   s       z.TestArithmetic1D.test_add0.<locals>.<listcomp>r   r   c                    r_   r   r   r`   )r   r   r   r   t   rb   )r   r
   sumranger   )r-   r!   r    r   sumSsumDr   )r   rX   r   	test_add0n   s   zTestArithmetic1D.test_add0c           
      C   s   t g d}t g d}||}||}t|| ||  t|| ||  t g d}t g d}||}||}	t||	 ||  t|| ||  t||	 ||  t|| ||  d S )N)rD   r   	   )r      r4   y      ?       y              @y              y      @       @y      @      y             ?)r   r   r   multiplyr
   )
r-   r!   r9   Br@   BspCrP   CspDspr   r   r   test_elementwise_multiplyw   s   z*TestArithmetic1D.test_elementwise_multiplyc           !      C   s  t dg}t dgg}t g d}t g dg}t dgdgdgg}t g dg d	g d
g}g d}t d}	|	j}
t dgg}t ddgddggg}||}||}||}||}||}||}||}||	}||	d }||
}||
d d dd f }||}||||||||	|
||g}||||||||||||g}|||g}|D ]C}|D ]>}z
| |  }W n% ty   tjtdd || W d    n1 sw   Y  Y qw ||}t	| | qq|D ]N}|D ]I}z| | }W n2 t
y   Y q ty5   d} tjt| d || W d    n	1 s.w   Y  Y qw ||}t	t|| qqd S )NrD   )r   r4   r   )ri   rh   rs   r   r*   r   )      r   )r7   r   r*   )ru   ru   ru   r   r*   r   )r   rD   r   )r   Ninconsistent shapesrE   z&broadcast together|inconsistent shapes)r   r   onesTr
   
ValueErrorrH   rI   rl   r   	TypeError)!r-   r!   r9   rm   ro   rP   EFGHJKLr@   rp   Gsprn   rq   EspFspHspHsppJspJsppKspmatrices
spmatricessp1dmatricesij
dense_multsp_multmatchmer   r   r   #test_elementwise_multiply_broadcast   sp   



z4TestArithmetic1D.test_elementwise_multiply_broadcastc                 C   s  ||}t dt jdt jg}|| }t j|| |g ddd}dt jt jdg}t j|| | t g d}t g d}||}	||}
t|	|
 ||  t g d	}t g d
}||}	||}
t jdd t|	|
 ||  W d    n1 sw   Y  t ddg}t ddg}||}	||}
t jddd t|	|
 ||  W d    d S 1 sw   Y  d S )Nr   )r   r   r   rD   r   r)   r   r   rj   rk   rv   )r   r   r*   ignore)divide)r   invalid)	r   r   nantestingassert_array_equalinfr   errstater   )r-   r!   r   rX   expectedactualdenomr9   rm   r@   rn   r   r   r   test_elementwise_divide   s4   "z(TestArithmetic1D.test_elementwise_dividec                 C   s   t g d}||}tjtdd |d  W d    n1 s!w   Y  tjtdd |d  W d    n1 s<w   Y  dD ]}|| }|| }t| | t|j|j qCd S )N)r   r   r*   r   znegative integer powersrE   r4   z
zero powerr   )r   r*   r   g@)	r   r   rH   rI   rz   rJ   r   r
   r   )r-   r!   r9   rm   exponentret_spret_npr   r   r   test_pow   s   

zTestArithmetic1D.test_powc                 C   s4   ||}d}| |}|| }t| |  d S )N
   )dotr   r
   )r-   r!   r   r9   scalarr   r   r   r   r   test_dot_scalar   s
   
z TestArithmetic1D.test_dot_scalarc                 C   s  |g d}|t ddgddgddggd}t g dgj}t|| | |  t||  ||   t| | ||   t||  ||   t g d}t|| | |  ||}|| }t|t jstJ |jdks{J tt d	| tt d	| |  tt d	||   tt d	| |   tj	t
d
d |d  W d    n1 sw   Y  tj	t
d
d d|  W d    d S 1 sw   Y  d S )N)r*   r   g      @r   r   r*   r   rv   )r   r   r   r   r   zScalar operands are not allowedrE   )r   r   ry   r   r
   r   r	   r.   rH   rI   rz   )r-   r!   Msprm   colVVspMsp_Vspr   r   r   test_matmul  s.   "

"zTestArithmetic1D.test_matmulc                 C   sd   || D ]+\}}}|t dkrq|| | | }t|||  || | | }t|||  qd S )Nr\   )r   r   r   )r-   r!   r    r   r   rX   sum1sum2r   r   r   test_sub_dense$  s   zTestArithmetic1D.test_sub_densec                 C   sn  t g }|d}|d}t ddg}||}||}tjtdd || W d    n1 s5w   Y  t|	|t 	|| tjtdd |	| W d    n1 s]w   Y  t|
| t 
|| t|
| t 
|| t|
d t 
|d tjtdd |
| W d    n1 sw   Y  t|| || d S )Nr   )r   r   r#   rw   rE   zdimension mismatchru   )r   r   reshaperx   rH   rI   rz   __add__r   r   rl   r
   )r-   r!   matr   r   faspdspr   r   r    test_size_zero_matrix_arithmetic2  s(   


z1TestArithmetic1D.test_size_zero_matrix_arithmeticN)__name__
__module____qualname__r3   r:   rA   rK   rQ   rT   rY   r[   r^   rg   rr   r   r   r   r   r   r   r   r   r   r   r   r"   #   s&    		<""r"   )__doc__rH   numpyr   numpy.testingr   r   scipy.sparser   r   scipy.sparse._sputilsr   r   int64r&   r(   r   r
   fixturer   r    markparametrizer"   r   r   r   r   <module>   s    

