a
    khA                     @   s  d dl Z d dlZd dlmZmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZ d d	lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 d d
l4m5Z5 d dl6m7Z7 d dl8m9Z9m:Z:m;Z;m<Z< d dl=m>Z> d dl=m?Z?m@Z@mAZA d dlmBZBmCZC d dlmDZDmEZEmFZF e7dZGedddZHedddZIedddZJedddZKed\ZLZMZNZOeGdurdd ePd ddD ZQeGjReQeGjSdZTe	eQZUdd ZVdd fd d!ZWd"d fd#d$ZXd%d& ZYd'd( ZZd)d* Z[d+d, Z\d-d. Z]d/d0 Z^d1d2 Z_d3d4 Z`d5d6 Zad7d8 Zbd9d: Zcd;d< Zdd=d> Zed?d@ ZfdS )A    N)symbols
Derivative)
torch_code)eyeMatrixSymbolMatrix)	NDimArray)ArrayTensorProductArrayAddPermuteDimsArrayDiagonal_CodegenArrayAbstract)lambdify)EqNeGeGtLeLt)Absceilingexpfloorsignsinasincosacostanatanatan2coshacoshsinhasinhtanhatanhreimargerfloggammasqrt)skip)import_module)DeterminantHadamardProductInverseTrace)
randMatrix)Identity
ZeroMatrix	OneMatrix)	conjugateI)	Heavisidegamma	polygammatorchM   NPQzx y z tc                 C   s   g | ]}t t||d  qS )r>   )listrange.0i rG   M/var/www/auris/lib/python3.9/site-packages/sympy/printing/tests/test_torch.py
<listcomp>&       rI   	   dtypec                 C   s  t | |d}dd | D }dd |D }|| }|tt| | }t|trZ| }t|dr|jrt|t	j
r| dkr| }t|}t|| dk sJ d S |jst|trt	j| t	jd}t	j||dd	sJ ntd
t| dt| d S )Nr<   c                 S   s"   g | ]}t |jd  |jd qS )r      )r3   shaperD   rG   rG   rH   rI   .   rJ   z)_compare_torch_matrix.<locals>.<listcomp>c                 S   s    g | ]}t j| t jd qS rL   )r<   tensortolistfloat64rD   rG   rG   rH   rI   /   rJ   	is_numberr   ư>rL   ZatolzCannot compare z with )r   subsdictzipdoit
isinstancer   hasattrrT   r<   TensorZdimitemfloatabsZ	is_Matrixr   rQ   rR   rS   allclose	TypeErrortype)	variablesexprfZrandom_matricesZrandom_variablesrerG   rG   rH   _compare_torch_matrix+   s"    
ri   c                   C   s   t ddS N   randomuniformrG   rG   rG   rH   <lambda>D   rJ   rp   c                    sx   t | |d} fdd| D }dd |D }|| }t|tjrH| }|tt| | }t	|| dk stJ d S )Nr<   c                    s   g | ]
}  qS rG   rG   rE   vrngrG   rH   rI   F   rJ   z)_compare_torch_scalar.<locals>.<listcomp>c                 S   s   g | ]}t j|t jd qS rP   r<   rQ   rS   rD   rG   rG   rH   rI   G   rJ   rU   )
r   r[   r<   r]   r^   rW   rX   rY   rZ   r`   rd   re   rt   rf   ZrvsZt_rvsrg   rh   rG   rs   rH   _compare_torch_scalarD   s    rw   c                   C   s   t ddS )Nr   
   )rn   randintrG   rG   rG   rH   rp   O   rJ   c                    sd   t | |d} fdd| D }dd |D }|| }t|tt| | }| |ks`J d S )Nr<   c                    s   g | ]
}  qS rG   rG   rq   rs   rG   rH   rI   Q   rJ   z-_compare_torch_relational.<locals>.<listcomp>c                 S   s   g | ]}t j|t jd qS rP   ru   rD   rG   rG   rH   rI   R   rJ   )r   boolrW   rX   rY   rZ   r^   rv   rG   rs   rH   _compare_torch_relationalO   s    r{   c                  C   s2  t std tt} t| dks$J tt| d}t jg dgt jd}||}t |}t 	||ksjJ t
t} t| dksJ ttf| dd d	 tt} t| d
ksJ ttf| dd d	 tt} t| dksJ ttf| dd d	 tt} t| dksJ ttf| dd d	 tt} t| dks6J ttf| dd d	 td } t| dksdJ ttf| dd d	 tt} t| dksJ ttf| dd d	 tt} t| dksJ ttf| dd d	 tt} t| dksJ ttf| dd d	 tt} t| dksJ ttf| dd d	 tt} t| dksJJ ttf| dd d	 tt} t| dksxJ ttf| d d d	 ttt} t| d!ksJ tttf| d"d d	 tt} t| d#ksJ ttf| d$d d	 tt} t| d%ksJ ttf| d&d d	 tt} t| d'ks4J ttf| d(d d	 tt} t| d)ksbJ ttf| d*d d	 tt} t| d+ksJ ttf| d,d d	 tt} t| d-ksJ ttf| d.d d	 tt} t| d/ksJ ttf| d0d d	 tt} t| d1ksJ ttf| d2d d	 d S )3NPyTorch not installedztorch.abs(x)r<   )   rL   ztorch.sign(x)c                   S   s   t ddS )Nirx   rm   rG   rG   rG   rH   rp   f   rJ   z!test_torch_math.<locals>.<lambda>rs   ztorch.ceil(x)c                   S   s   t   S Nrn   rG   rG   rG   rH   rp   j   rJ   ztorch.floor(x)c                   S   s   t   S r   r   rG   rG   rG   rH   rp   n   rJ   ztorch.exp(x)c                   S   s   t ddS Nr~   rm   rG   rG   rG   rH   rp   r   rJ   ztorch.sqrt(x)c                   S   s   t   S r   r   rG   rG   rG   rH   rp   v   rJ      ztorch.pow(x, 4)c                   S   s   t   S r   r   rG   rG   rG   rH   rp   z   rJ   ztorch.cos(x)c                   S   s   t   S r   r   rG   rG   rG   rH   rp   ~   rJ   ztorch.acos(x)c                   S   s   t ddS NgGzgGz?rm   rG   rG   rG   rH   rp      rJ   ztorch.sin(x)c                   S   s   t   S r   r   rG   rG   rG   rH   rp      rJ   ztorch.asin(x)c                   S   s   t ddS r   rm   rG   rG   rG   rH   rp      rJ   ztorch.tan(x)c                   S   s   t ddS )Ng      g      ?rm   rG   rG   rG   rH   rp      rJ   ztorch.atan(x)c                   S   s   t ddS rj   rm   rG   rG   rG   rH   rp      rJ   ztorch.atan2(y, x)c                   S   s   t ddS rj   rm   rG   rG   rG   rH   rp      rJ   ztorch.cosh(x)c                   S   s   t ddS r   rm   rG   rG   rG   rH   rp      rJ   ztorch.acosh(x)c                   S   s   t ddS )Ng?rl   rm   rG   rG   rG   rH   rp      rJ   ztorch.sinh(x)c                   S   s   t ddS r   rm   rG   rG   rG   rH   rp      rJ   ztorch.asinh(x)c                   S   s   t ddS rj   rm   rG   rG   rG   rH   rp      rJ   ztorch.tanh(x)c                   S   s   t ddS r   rm   rG   rG   rG   rH   rp      rJ   ztorch.atanh(x)c                   S   s   t ddS )Ngg?rm   rG   rG   rG   rH   rp      rJ   ztorch.erf(x)c                   S   s   t ddS r   rm   rG   rG   rG   rH   rp      rJ   ztorch.lgamma(x)c                   S   s   t ddS )Ng      ?rl   rm   rG   rG   rG   rH   rp      rJ   ) r<   r-   r   xr   r   rQ   rS   r`   allr   rw   r   r   r   r,   r   r   r   r   r   r   r    yr!   r"   r#   r$   r%   r&   r*   r+   )re   rf   maZy_abscrG   rG   rH   test_torch_mathX   s    

r   c                   C   s@   t ttdksJ t ttdks(J t ttdks<J d S )Nztorch.real(x)ztorch.imag(x)ztorch.angle(x))r   r'   r   r(   r)   rG   rG   rG   rH   test_torch_complexes   s    r   c                  C   s   t std ttt} t| dks&J tttf|  ttt} t| dksNJ tttf|  ttt} t| dksvJ tttf|  t	tt} t| dksJ tttf|  t
tt} t| dksJ tttf|  ttt} t| dksJ tttf|  d S )Nr|   ztorch.eq(x, y)ztorch.ne(x, y)ztorch.ge(x, y)ztorch.gt(x, y)ztorch.le(x, y)ztorch.lt(x, y))r<   r-   r   r   r   r   r{   r   r   r   r   r   re   rG   rG   rH   test_torch_relational   s(    





r   c                  C   s  t d u rtd t} t| dks$J ttf| d}td}t j| t jd}t 	|||sbJ tt
 } t| dkszJ ttt
f|  td } t| dksJ ttf|  tt
 t t } t| dksJ ttt
ttf|  tt} t| d	ksJ ttf|  tt} t| d
ksJ ttf|  ttt
} t| dksDJ ttt
f|  tt} t| dkslJ td}t j| t jd}ttf| d}||}t j|}t 	||sJ d S )Nr|   r=   r<   r>   rL   ztorch.matmul(M, N)ztorch.mm(torch.mm(M, M), M)z4torch.matmul(torch.matmul(torch.matmul(M, N), P), Q)ztorch.trace(M)ztorch.det(M)ztorch.mul(M, N)ztorch.linalg.inv(M))r<   r-   r=   r   r   r   rQ   rR   rS   ra   r?   ri   r@   rA   r2   r/   r0   r1   Zlinalginv)re   rf   Zeye_matZ
eye_tensorresultexpectedrG   rG   rH   test_torch_matrix   sD    
r   c                  C   s&  t std tddd} tddd}tddd}tddd}t jddgd	d
ggt jd}t jddgdd	ggt jd}t jddgddggt jd}t jddgd
dggt jd}t| |}t|dksJ t| |f|d}	|	||}
t d||}t 	|
|sJ t
| |}t|dksJ t| |f|d}	|	||}
|| }t 	|
|sNJ t
| ||}t|dkslJ t| ||f|d}	|	|||}
|| | }t 	|
|sJ t
| |||}t|dksJ t| |||f|d}	|	||||}
|| | | }t 	|
|sJ t| ddg}t|dks,J t| f|d}	|	|}
|j}t 	|
|sZJ tt| |g d}t|dksJ t| |f|d}	|	||}
t d||dddd}t 	|
|sJ tt| |d}t|dksJ t| |f|d}	|	||}
t d||}t 	|
|s"J d S ) Nr|   r=   r~   r?   r@   rA         ?       @      @      @rL   g                     g      @ztorch.einsum("ab,cd", M, N)r<   zij,klztorch.add(M, N)ztorch.add(torch.add(M, N), P)z+torch.add(torch.add(torch.add(M, N), P), Q)rN   r   zM.permute(1, 0))rN   r~   r>   r   z/torch.einsum("ab,cd", M, N).permute(1, 2, 3, 0)zab,cdr>   )rN   r~   z torch.einsum("ab,bc->acb", M, N)z
ab,bc->acb)r<   r-   r   rQ   rS   r	   r   r   Zeinsumra   r
   r   TZpermuter   )r=   r?   r@   rA   r   mbZmcZmdZcgrf   r   r   rG   rG   rH   test_torch_array_operations  sh    





r   c                  C   s"   t ttt} t| dksJ dS )zTest derivative handling.'torch.autograd.grad(torch.sin(x), x)[0]N)r   r   r   r   r   rG   rG   rH   test_torch_derivativeK  s    r   c                  C   s   t std ttttgttt gg} dt	| v s:J dt	| ddv sNJ t	| dd}d|v sfJ d|v srJ t	| ddd	}d|v sJ d|v sJ d S )
Nr|   zdtype=torch.float64zdtype=torch.float32ztorch.float32rL   Trequires_gradzrequires_grad=True)r   rM   )
r<   r-   r   r   r   r   r   ztr   )re   r   rG   rG   rH   test_torch_printing_dtypeQ  s    r   c                  C   s   t std tttt } tttg| d}t jddd}t jddd}|||}|js\J |	  t
|j ttd  dk sJ t
|j ttd   dk sJ d S )Nr|   r<   r   Tr   r   rU   )r<   r-   r   r   r   r   r   rQ   r   Zbackwardr`   Zgradr^   r_   Zevalf)re   rf   x_valy_valr   rG   rG   rH   test_requires_gradg  s    

&r   c                  C   s  t std td\} }}tt| | }t|dks8J tt| | df}t|dksZJ tt| | | |}t|}d}|dd}|dd}||ksJ tt| | | }t|}d}||ksJ tt| | | | |df|}t|}d	}|dd}|dd}||ksJ d S )
Nr|   zx y zr   r~   zhtorch.autograd.grad(torch.autograd.grad(torch.sin(x), x, create_graph=True)[0], x, create_graph=True)[0]zjtorch.autograd.grad(torch.autograd.grad(torch.sin(x*y), x, create_graph=True)[0], y, create_graph=True)[0]  ztorch.autograd.grad(torch.autograd.grad(torch.autograd.grad(torch.autograd.grad(torch.sin(x*y*z), x, create_graph=True)[0], y, create_graph=True)[0], y, create_graph=True)[0], z, create_graph=True)[0])r<   r-   r   r   r   r   replace)r   r   r   re   r   r   Znormalized_resultZnormalized_expectedrG   rG   rH   %test_torch_multi_variable_derivatives|  s6    r   c                  C   sL  t std td} td}t| d | }t| |d}t jddd}||}t |t d	sbJ tt| | df}t| |d}t jd
dd}||}t j|t d
ddsJ t jtj	d dd}||}t j|t dddsJ t| |d  | |}t| |f|d}t jddd}t jddd}|||}t |t dsHJ d S )Nr|   r   r   r~   r<   r   Tr   r   r   gh㈵>rV   r   r   g      @)
r<   r-   r   r   r   rQ   iscloser   mathpi)r   r   re   rf   r   r   r   rG   rG   rH   test_torch_derivative_lambdify  s.    
r   c                  C   s   t std td} t| dks$J td}t|} t| dksDJ tdd} t| dks^J td\}}t||} t| d	ksJ tdd} t| d
ksJ t||} t| dksJ d S )Nr|   r>   ztorch.eye(3)nztorch.eye(n, n)r~   ztorch.zeros((2, 3))zm nztorch.zeros((m, n))ztorch.ones((2, 3))ztorch.ones((m, n)))r<   r-   r4   r   r   r5   r6   )re   r   mrG   rG   rH   test_torch_special_matrices  s     



r   c                  C   s   t std td} tg | d}| }t d}t ||s@J tdd} tg | d}| }t d}t ||svJ tdd} tg | d}| }t 	d}t ||sJ d S )Nr|   r>   r<   r~   )r~   r>   )
r<   r-   r4   r   r   ra   r5   zerosr6   Zones)re   rf   r   r   rG   rG   rH   $test_torch_special_matrices_lambdify  s"    




r   c                  C   s   t std tt} t| dks$J tttttt  } t| dksLJ t} t| dks`J dt t } t| dks|J t	tt } t| dksJ d S )Nr|   ztorch.conj(x)z6torch.sin(torch.conj(x)) - 1j*torch.cos(torch.conj(y))Z1jr~   zx + 2*1jztorch.exp(1j*x))
r<   r-   r7   r   r   r   r8   r   r   r   r   rG   rG   rH   test_torch_complex_operations  s    r   c                  C   s   t std tt} t| dks$J ttd} t| dks>J tt} t| dksVJ tdt} t| dkspJ ttt} t| dksJ d S )Nr|   ztorch.heaviside(x, 1/2)r   ztorch.heaviside(x, 0)ztorch.special.gamma(x)ztorch.special.digamma(x)z!torch.special.gamma(torch.sin(x)))r<   r-   r9   r   r   r:   r;   r   r   rG   rG   rH   test_torch_special_functions  s    

r   )grn   r   Zsympyr   r   Zsympy.printing.pytorchr   r   r   r   Zsympy.tensor.arrayr   Z0sympy.tensor.array.expressions.array_expressionsr	   r
   r   r   r   Zsympy.utilities.lambdifyr   Zsympy.core.relationalr   r   r   r   r   r   Zsympy.functionsr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   Zsympy.testing.pytestr-   Zsympy.externalr.   Zsympy.matrices.expressionsr/   r0   r1   r2   Zsympy.matricesr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r?   r@   rA   r   r   r   r   rC   ZllorQ   rS   Zm3x3Z	m3x3sympyri   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   rG   rG   rH   <module>   sV    h
	a/@! 