o
    GZhA                     @   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   N/var/www/auris/lib/python3.10/site-packages/sympy/printing/tests/test_torch.py
<listcomp>&   s    rI   	   dtypec                 C   s  t | |d}dd | D }dd |D }|| }|tt| | }t|tr-| }t|drU|jrUt|t	j
rU| dkrU| }t|}t|| dk sSJ d S |js]t|trst	j| t	jd}t	j||dd	sqJ d S td
t| dt| )Nr<   c                 S   s"   g | ]}t |jd  |jd qS )r      )r3   shaperD   rG   rG   rH   rI   .   s   " z)_compare_torch_matrix.<locals>.<listcomp>c                 S   s    g | ]}t j| t jd qS rK   )r<   tensortolistfloat64rD   rG   rG   rH   rI   /   s     	is_numberr   ư>rK   ZatolzCannot compare z with )r   subsdictzipdoit
isinstancer   hasattrrS   r<   TensordimitemfloatabsZ	is_Matrixr   rP   rQ   rR   allclose	TypeErrortype)	variablesexprfZrandom_matricesZrandom_variablesrerG   rG   rH   _compare_torch_matrix+   s"   
ri   c                   C      t ddS N   randomuniformrG   rG   rG   rH   <lambda>D       rq   c                    sx   t | |d} fdd| D }dd |D }|| }t|tjr$| }|tt| | }t	|| dk s:J d S )Nr<   c                       g | ]}  qS rG   rG   rE   vrngrG   rH   rI   F       z)_compare_torch_scalar.<locals>.<listcomp>c                 S      g | ]
}t j|t jd qS rO   r<   rP   rR   rD   rG   rG   rH   rI   G       rT   )
r   rZ   r<   r\   r^   rV   rW   rX   rY   r`   rd   re   rw   rf   ZrvsZt_rvsrg   rh   rG   rv   rH   _compare_torch_scalarD   s   r}   c                   C   rj   )Nr   
   )ro   randintrG   rG   rG   rH   rq   O   rr   c                    sd   t | |d} fdd| D }dd |D }|| }t|tt| | }| |ks0J d S )Nr<   c                    rs   rG   rG   rt   rv   rG   rH   rI   Q   rx   z-_compare_torch_relational.<locals>.<listcomp>c                 S   ry   rO   rz   rD   rG   rG   rH   rI   R   r{   )r   boolrV   rW   rX   rY   r^   r|   rG   rv   rH   _compare_torch_relationalO   s   r   c                  C   s&  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s5J t
t} t| dksAJ ttf| dd d	 tt} t| d
ksWJ ttf| dd d	 tt} t| dksmJ 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d } 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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sJ ttf| dd d	 tt} t| dks6J ttf| d d d	 ttt} t| d!ksNJ tttf| d"d d	 tt} t| d#ksfJ ttf| d$d d	 tt} t| d%ks}J 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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<   )   rK   ztorch.sign(x)c                   S   rj   )Nir~   rn   rG   rG   rG   rH   rq   f   rr   z!test_torch_math.<locals>.<lambda>rv   ztorch.ceil(x)c                   S      t   S Nro   rG   rG   rG   rH   rq   j       ztorch.floor(x)c                   S   r   r   r   rG   rG   rG   rH   rq   n   r   ztorch.exp(x)c                   S   rj   Nr   rn   rG   rG   rG   rH   rq   r   rr   ztorch.sqrt(x)c                   S   r   r   r   rG   rG   rG   rH   rq   v   r      ztorch.pow(x, 4)c                   S   r   r   r   rG   rG   rG   rH   rq   z   r   ztorch.cos(x)c                   S   r   r   r   rG   rG   rG   rH   rq   ~   r   ztorch.acos(x)c                   S   rj   NgGzgGz?rn   rG   rG   rG   rH   rq      rr   ztorch.sin(x)c                   S   r   r   r   rG   rG   rG   rH   rq      r   ztorch.asin(x)c                   S   rj   r   rn   rG   rG   rG   rH   rq      rr   ztorch.tan(x)c                   S   rj   )Ng      g      ?rn   rG   rG   rG   rH   rq      rr   ztorch.atan(x)c                   S   rj   rk   rn   rG   rG   rG   rH   rq      rr   ztorch.atan2(y, x)c                   S   rj   rk   rn   rG   rG   rG   rH   rq      rr   ztorch.cosh(x)c                   S   rj   r   rn   rG   rG   rG   rH   rq      rr   ztorch.acosh(x)c                   S   rj   )Ng?rm   rn   rG   rG   rG   rH   rq      rr   ztorch.sinh(x)c                   S   rj   r   rn   rG   rG   rG   rH   rq      rr   ztorch.asinh(x)c                   S   rj   rk   rn   rG   rG   rG   rH   rq      rr   ztorch.tanh(x)c                   S   rj   r   rn   rG   rG   rG   rH   rq      rr   ztorch.atanh(x)c                   S   rj   )Ngg?rn   rG   rG   rG   rH   rq      rr   ztorch.erf(x)c                   S   rj   r   rn   rG   rG   rG   rH   rq      rr   ztorch.lgamma(x)c                   S   rj   )Ng      ?rm   rn   rG   rG   rG   rH   rq      rr   ) r<   r-   r   xr   r   rP   rR   r`   allr   r}   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s'J tttf|  ttt} t| dks;J tttf|  t	tt} t| dksOJ tttf|  t
tt} t| dkscJ tttf|  ttt} t| dkswJ 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 	|||s1J tt
 } t| dks=J ttt
f|  td } t| dksPJ ttf|  tt
 t t } t| dksfJ ttt
ttf|  tt} t| d	ks{J ttf|  tt} t| d
ksJ ttf|  ttt
} t| dksJ ttt
f|  tt} t| dksJ td}t j| t jd}ttf| d}||}t j|}t 	||sJ d S )Nr   r=   r<   r>   rK   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   rP   rQ   rR   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scJ t| |f|d}	|	||}
t d||}t 	|
|sJ t
| |}t|dksJ t| |f|d}	|	||}
|| }t 	|
|sJ t
| ||}t|dksJ 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 	|
|s(J tt| |g d}t|dks;J t| |f|d}	|	||}
t d||dddd}t 	|
|s^J tt| |d}t|dksoJ t| |f|d}	|	||}
t d||}t 	|
|sJ d S ) Nr   r=   r   r?   r@   rA         ?       @      @      @rK   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)rM   r   zM.permute(1, 0))rM   r   r>   r   z/torch.einsum("ab,cd", M, N).permute(1, 2, 3, 0)zab,cdr>   )rM   r   z torch.einsum("ab,bc->acb", M, N)z
ab,bc->acb)r<   r-   r   rP   rR   r	   r   r   Zeinsumra   r
   r   TZpermuter   )r=   r?   r@   rA   r   mbZmc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 s'J t	| dd}d|v s3J d|v s9J t	| ddd	}d|v sFJ d|v sLJ d S )
Nr   zdtype=torch.float64zdtype=torch.float32ztorch.float32rK   Trequires_gradzrequires_grad=True)r   rL   )
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 sEJ t
|j ttd   dk sYJ d S )Nr   r<   r   Tr   r   rT   )r<   r-   r   r   r   r   r   rP   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sJ tt| | df}t|dks-J tt| | | |}t|}d}|dd}|dd}||ksOJ tt| | | }t|}d}||kscJ tt| | | | |df|}t|}d	}|dd}|dd}||ksJ 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   sJ  t std td} td}t| d | }t| |d}t jddd}||}t |t d	s1J tt| | df}t| |d}t jd
dd}||}t j|t d
ddsXJ t jtj	d dd}||}t j|t dddssJ t| |d  | |}t| |f|d}t jddd}t jddd}|||}t |t dsJ d S )Nr   r   r   r   r<   r   Tr   r   r   gh㈵>rU   r   r   g      @)
r<   r-   r   r   r   rP   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s"J tdd} t| dks/J td\}}t||} t| d	ksBJ tdd} t| d
ksOJ t||} t| dks\J 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 ||s;J tdd} tg | d}| }t 	d}t ||sVJ d S )Nr   r>   r<   r   )r   r>   )
r<   r-   r4   r   r   ra   r5   Z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s&J t} t| dks0J dt t } t| dks>J t	tt } t| dksLJ 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s+J tdt} t| dks8J ttt} t| dksFJ 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   )gro   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llorP   rR   Zm3x3Z	m3x3sympyri   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   rG   rG   rH   <module>   sX     h	a/@! 