o
    ic.                     @   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   ^/home/kim/smarthome/.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   )Z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)   Zint32Zfloat32float64Z	complex64
complex128r      )r   r   r   Ztocsrr	   Ztocoor
   )selfr    shapeZ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   )Zdecimals)ndigits)r   r   r   aroundroundr
   r'   r    r2   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)	r0   r-   r   r   r&   r      r   r&   zinput is not scalarmatch)r   r   r   powerr
   pytestraisesNotImplementedErrorr7   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    Dr2   r   r   r   	test_realJ      zTestArithmetic1D.test_realc                 C   rD   rE   )r   r   r   imagr
   rG   r   r   r   	test_imagO   rJ   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 rM   rN   rO   r   r   r   test_rmul_scalarY   rR   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boolr<   r   )r   r0   r   r&   r   r%   )r   r   r   r
   Zzeros)r'   r    r   r   r   rP   r2   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)rP   r   r   r   s       z.TestArithmetic1D.test_add0.<locals>.<listcomp>r   r   c                    rV   r   r   rW   )r   r   r   r   t   rY   )r   r
   sumranger   )r'   r    r   r   ZsumSZsumDr   )r   rP   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)r<   r   	   )r      r-   y      ?       y              @y              y      @       @y      @      y             ?)r   r   r   multiplyr
   )
r'   r    r2   Br8   BspCrH   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 )Nr<   )r   r-   r   )r^   r]   rh   r   r&   r   )      r   )r0   r   r&   )rj   rj   rj   r   r&   r   )r   r<   r   )r   Ninconsistent shapesr=   z&broadcast together|inconsistent shapes)r   r   onesTr
   
ValueErrorr@   rA   ra   r   	TypeError)!r'   r    r2   rb   rd   rH   EFGHJKLr8   re   ZGsprc   rf   ZEspZFspZHspZHsppZJspZJsppZKspZmatricesZ
spmatricesZsp1dmatricesijZ
dense_multZsp_multZ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   r<   r   r%   r   r   r_   r`   rk   )r   r   r&   ignore)divide)r|   invalid)	r   r   nanZtestingZassert_array_equalinfr   Zerrstater   )r'   r    r   rP   expectedactualdenomr2   rb   r8   rc   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 powersr=   r-   z
zero powerr   )r   r&   r   g@)	r   r   r@   rA   ro   rB   r   r
   r   )r'   r    r2   rb   exponentZret_spZret_npr   r   r   test_pow   s   

zTestArithmetic1D.test_powc                 C   s4   ||}d}| |}|| }t| |  d S )N
   )dotr   r
   )r'   r    r   r2   Z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   rk   )r   r   r   r   r   zScalar operands are not allowedr=   )r   r   rn   r   r
   r   r	   r(   r@   rA   ro   )r'   r    ZMsprb   colVZVspZMsp_Vspr   r   r   test_matmul  s.   "

"zTestArithmetic1D.test_matmulc                 C   sd   || D ]+\}}}|t dkrq|| | | }t|||  || | | }t|||  qd S )NrT   )r   r   r   )r'   r    r   r   r   rP   Zsum1Z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"   rl   r=   zdimension mismatchrj   )r   r   Zreshaperm   r@   rA   ro   __add__r   r   ra   r
   )r'   r    Zmatr   r   fZaspZdspr   r   r    test_size_zero_matrix_arithmetic2  s(   


z1TestArithmetic1D.test_size_zero_matrix_arithmeticN)__name__
__module____qualname__r,   r3   r9   rC   rI   rL   rQ   rS   rU   r\   rg   rz   r   r   r   r   r   r   r   r   r   r   r!   #   s&    		<""r!   )__doc__r@   numpyr   Znumpy.testingr   r   Zscipy.sparser   r   Zscipy.sparse._sputilsr   r   Zint64r#   r$   r   r
   Zfixturer   r   markZparametrizer!   r   r   r   r   <module>   s    

