o
    GZhB                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ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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. d d	l/m0Z0m1Z1 d d
l2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z;m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZD d dlCmEZE ed ZFZGeGrd dlHZHdeHjId< e;dddZJe;dddZKe;dddZLe;dddZMed\ZNZOZPZQeFdurdd eRd ddD ZSeFTeSZUe3eSZVd?ddZWd?d d!ZXd"d# ZYd$d% fd&d'ZZd(d% fd)d*Z[d+d, Z\eEd-d. Z]d/d0 Z^d1d2 Z_eEd3d4 Z`d5d6 Zad7d8 Zbd9d: Zcd;d< Zdd=d> ZedS )@    N)
Derivative)symbols)	Piecewise)ArrayTensorProductArrayAddPermuteDimsArrayDiagonal)EqNeGeGtLeLt)import_module)Absceilingexpfloorsignsinasinsqrtcosacostanatanatan2coshacoshsinhasinhtanhatanhreimargerfloggammalog)isnanisinf)Matrix
MatrixBaseeye
randMatrix)DeterminantHadamardProductInverseMatrixSymbolTrace)tensorflow_code)convert_matrix_to_array)lambdify)skip)XFAIL
tensorflow2ZTF_CPP_MIN_LOG_LEVELM   NPQzx y z tc                 C   s   g | ]}t t||d  qS )r<   )listrange.0i rE   S/var/www/auris/lib/python3.10/site-packages/sympy/printing/tests/test_tensorflow.py
<listcomp>(       rG   	   Fc           
      C     t | |d}|sdd | D }ndd | D }t }d }|  dd |D }tjjj|d}||| }W d    n1 sBw   Y  |t	t
| |}	|	 }	|	jret|	tsa|	 }	|	 }	|sq||	k soJ d S dd |D }dd |	D }	td	d
 t
||	D sJ d S )Nr9   c                 S   s   g | ]	}t |j|jqS rE   r.   rowscolsrC   vrE   rE   rF   rG   0       z._compare_tensorflow_matrix.<locals>.<listcomp>c                 S   s   g | ]}t |j|jd  qS )g      Y@rK   rN   rE   rE   rF   rG   2   rH   c                 S      g | ]}t t|qS rE   evalr4   rB   rE   rE   rF   rG   7       graphc                 S      g | ]	}|D ]}|qqS rE   rE   rC   rowrD   rE   rE   rF   rG   E   rP   c                 S   rW   rE   rE   rX   rE   rE   rF   rG   F   rP   c                 s   <    | ]\}}t || d dttt |d    k V  qdS 
      Nabsintr(   rC   abrE   rE   rF   	<genexpr>G       ,
z-_compare_tensorflow_matrix.<locals>.<genexpr>r6   tfGraph
as_defaultcompatv1SessionrunsubsdictzipdoitZ	is_Matrix
isinstancer,   Zas_explicittolistall
	variablesexpr	use_floatfrandom_matricesrV   rrandom_variablessessionerE   rE   rF   _compare_tensorflow_matrix-   0   

r   c           
      C   rJ   )Nr9   c                 S      g | ]}t |j|jd  qS )r]   r-   rL   rM   rN   rE   rE   rF   rG   P   rH   z6_compare_tensorflow_matrix_inverse.<locals>.<listcomp>c                 S   r   )gQ	@r   rN   rE   rE   rF   rG   R   rH   c                 S   rQ   rE   rR   rB   rE   rE   rF   rG   W   rT   rU   c                 S   rW   rE   rE   rX   rE   rE   rF   rG   e   rP   c                 S   rW   rE   rE   rX   rE   rE   rF   rG   f   rP   c                 s   rZ   r[   r^   ra   rE   rE   rF   rd   g   re   z5_compare_tensorflow_matrix_inverse.<locals>.<genexpr>rf   ru   rE   rE   rF   "_compare_tensorflow_matrix_inverseM   r   r   c           	      C   s   t | |d}dd | D }t }d }|  dd |D }tjjj|d}||| }W d    n1 s8w   Y  |t	t
| |}| }t|| dk sUJ d S )Nr9   c                 S   s"   g | ]}t |j|j d  qS )d   )r.   rL   rM   evalfrN   rE   rE   rF   rG   m   s    z5_compare_tensorflow_matrix_scalar.<locals>.<listcomp>c                 S   rQ   rE   rR   rB   rE   rE   rF   rG   s   rT   rU   ư>)r6   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   r_   )	rv   rw   ry   rz   rV   r{   r|   r}   r~   rE   rE   rF   !_compare_tensorflow_matrix_scalark   s   
r   c                   C      t ddS Nr   r\   randomrandintrE   rE   rE   rF   <lambda>}       r   c           
         s   t | |d} fdd| D }t }d }|  dd |D }tjjj|d}||| }W d    n1 s:w   Y  |t	t
| |  }	t||	 dk sWJ d S )Nr9   c                       g | ]}  qS rE   rE   rN   rngrE   rF   rG          z._compare_tensorflow_scalar.<locals>.<listcomp>c                 S   rQ   rE   rR   rB   rE   rE   rF   rG      rT   rU   r   )r6   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   r   rq   r_   
rv   rw   r   ry   ZrvsrV   r{   Ztf_rvsr}   r~   rE   r   rF   _compare_tensorflow_scalar|   s   
r   c                   C   r   r   r   rE   rE   rE   rF   r      r   c           
         s   t | |d} fdd| D }t }d }|  dd |D }tjjj|d}||| }W d    n1 s:w   Y  |t	t
| | }	||	ksQJ d S )Nr9   c                    r   rE   rE   rN   r   rE   rF   rG      r   z2_compare_tensorflow_relational.<locals>.<listcomp>c                 S   rQ   rE   rR   rB   rE   rE   rF   rG      rT   rU   )r6   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   r   rE   r   rF   _compare_tensorflow_relational   s   
r   c                  C   sF   t tddks
J ttttgttt gg} t | dks!J d S )Nr<   z6tensorflow.constant([[1, 0, 0], [0, 1, 0], [0, 0, 1]])zPtensorflow.Variable([[x, tensorflow.math.sin(y)], [tensorflow.math.exp(z), -t]]))	r4   r-   r+   xr   yr   ztrw   rE   rE   rF   test_tensorflow_printing   s   
r   c                  C   s  t std tt} t| dksJ ttf|  tt} t| dks$J ttf|  tt} t| dks6J ttf| dd d tt} t| dksLJ ttf| d	d d t	t} t| d
ksbJ ttf| dd d t
t} t| dksxJ 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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} t| dks+J tttf| dd d tt} t| dksCJ ttf| d d d tt} t| d!ksZJ ttf| d"d d tt} t| d#ksqJ 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| d.d d d S )/NTensorFlow not installedztensorflow.math.abs(x)ztensorflow.math.sign(x)ztensorflow.math.ceil(x)c                   S      t   S Nr   rE   rE   rE   rF   r          z&test_tensorflow_math.<locals>.<lambda>r   ztensorflow.math.floor(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.exp(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.sqrt(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   r]   ztensorflow.math.pow(x, 4)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.cos(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.acos(x)c                   S   r   )Nr   gffffff?r   uniformrE   rE   rE   rF   r      r   ztensorflow.math.sin(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.asin(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.tan(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.atan(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.atan2(y, x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.cosh(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.acosh(x)c                   S   r   N      r   rE   rE   rE   rF   r      r   ztensorflow.math.sinh(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.asinh(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.tanh(x)c                   S   r   r   r   rE   rE   rE   rF   r      r   ztensorflow.math.atanh(x)c                   S   r   )Ng      g      ?r   rE   rE   rE   rF   r      r   ztensorflow.math.erf(x)c                   S   r   r   r   rE   rE   rE   rF   r     r   ztensorflow.math.lgamma(x)c                   S   r   r   r   rE   rE   rE   rF   r     r   )rg   r7   r   r   r4   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r&   r'   r   rE   rE   rF   test_tensorflow_math   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tensorflow.math.real(x)ztensorflow.math.imag(x)ztensorflow.math.angle(x))r4   r#   r   r$   r%   rE   rE   rE   rF   test_tensorflow_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tensorflow.math.equal(x, y)ztensorflow.math.not_equal(x, y)z#tensorflow.math.greater_equal(x, y)ztensorflow.math.greater(x, y)z tensorflow.math.less_equal(x, y)ztensorflow.math.less(x, y))rg   r7   r	   r   r   r4   r   r
   r   r   r   r   r   rE   rE   rF   test_tensorflow_relational  s(   





r   c                  C   s  t std t} t| dksJ ttf|  tt } t| dks"J tttf|  tt } t| dks5J tttf|  ttt} t| dksIJ tttf|  tt t t } t| dks`J tttttf|  td } t| dksuJ ttf|  t	t} t| d	ksJ ttf|  t
t} t| d
ksJ ttf|  tt} t| dksJ ttf| dd tj} t| dddksJ t| dddksJ ttf|  d S )Nr   r;   tensorflow.math.add(M, N)ztensorflow.linalg.matmul(M, N)ztensorflow.math.multiply(M, N)zXtensorflow.linalg.matmul(tensorflow.linalg.matmul(tensorflow.linalg.matmul(M, N), P), Q)r<   z;tensorflow.linalg.matmul(tensorflow.linalg.matmul(M, M), M)ztensorflow.linalg.trace(M)ztensorflow.linalg.det(M)ztensorflow.linalg.inv(M)T)rx   z1.14)Ztensorflow_versionz%tensorflow.linalg.matrix_transpose(M)z1.13ztensorflow.matrix_transpose(M))rg   r7   r;   r4   r   r=   r0   r>   r?   r3   r/   r   r1   r   Tr   rE   rE   rF   test_tensorflow_matrices/  sR   


r   c            
      C   s   t std t  } |  Z t jjj| d}tddd}tddd}t|| }t	||f|d}t 
ddgdd	gg}t 
dd
gddgg}||||}|t ||}	||	k s`J W d    d S 1 skw   Y  d S )Nr   rU   r;   r   r=   r9   r   r<   r]   )rg   r7   rh   ri   rj   rk   rl   r2   r5   r6   constantrm   matmulrt   )
rV   r}   r;   r=   cgry   mambr   crE   rE   rF   test_codegen_einsume  s   
"r   c               
   C   s  t std t  } |   t jj }tddd}tddd}tddd}tddd}t ddgdd	gg}t dd
gddgg}t ddgddgg}t ddgd	dgg}	t	||}
t
|
dksgJ t||f|
d}||||}|t d||}||k sJ t||}
t
|
dksJ t||f|
d}||||}||| }||k sJ t|||}
t
|
dksJ t|||f|
d}|||||}||| | }||k sJ t||||}
t
|
dksJ t||||f|
d}||||||	}||| | |	 }||k sJ t|ddg}
t
|
dks-J t|f|
d}|||}|t |}||k sLJ tt	||g d}
t
|
dks_J t||f|
d}||||}|t t d||g d}||k sJ tt	||d}
t
|
dksJ t||f|
d}||||}|t d||}||k sJ W d    d S 1 sw   Y  d S )Nr   r;   r   r=   r>   r?   r   r<   r]   r   r   r      z'tensorflow.linalg.einsum("ab,cd", M, N)r9   zij,klr   z1tensorflow.math.add(tensorflow.math.add(M, N), P)zItensorflow.math.add(tensorflow.math.add(tensorflow.math.add(M, N), P), Q)ztensorflow.transpose(M, [1, 0]))r   r   r<   r   zKtensorflow.transpose(tensorflow.linalg.einsum("ab,cd", M, N), [1, 2, 3, 0])zab,cd)r   r   z,tensorflow.linalg.einsum("ab,bc->acb", M, N)z
ab,bc->acb)rg   r7   rh   ri   rj   rk   rl   r2   r   r   r4   r6   rm   Zeinsumrt   r   r   Z	transposer   )rV   r}   r;   r=   r>   r?   r   r   Zmcmdr   ry   r   r   rE   rE   rF   test_codegen_extraz  s   


 
$r   c                  C   sx   t ddd} t ddd}t ddd}t| d dksJ td| d  dks(J |d || | }t|d	ks:J d S )
NAr   r<   BC)r   r   zA[0, 0]z	3*A[0, 0]z&(tensorflow.math.add((-1)*B, A))[0, 0])r2   r4   rn   )r   r   r   FrE   rE   rF   test_MatrixElement_printing  s   r   c                  C   s"   t ttt} t| dksJ d S )Nz2tensorflow.gradients(tensorflow.math.sin(x), x)[0])r   r   r   r4   r   rE   rE   rF   test_tensorflow_Derivative  s   r   c                  C   sX  t std td} tdt| fd}t|}d}t||ks)J d| d| tddftd	d
ftdd
fdfD ]\}}t| |ddt |gd}||k	 
 sWJ q;td} tdt| fd}t|}d}t||ks{J d| d| td	dftddftdd
fdfD ]\}}t| |ddt |gd}||k	 
 sJ qd S )Nr   r   g        )      ?Tz5tensorflow.where(tensorflow.math.is_nan(x), 0.0, 1.0)zIncorrect printed result z, expected naninfr   z-inf)r   r   r9   )modules)r   z5tensorflow.where(tensorflow.math.is_inf(x), 0.0, 1.0))rg   r7   r   r   r)   r4   floatr6   r   numpyrt   r*   )r   Z
expressionZprinted_codeZexpected_printed_code_inputZ	_expected_outputrE   rE   rF   test_tensorflow_isnan_isinf  s&    , ,r   )F)fr   Zsympy.core.functionr   Zsympy.core.symbolr   Zsympyr   Z0sympy.tensor.array.expressions.array_expressionsr   r   r   r   Zsympy.core.relationalr	   r
   r   r   r   r   Zsympy.externalr   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'   r(   Zsympy.codegen.cfunctionsr)   r*   Zsympy.matricesr+   r,   r-   r.   Zsympy.matrices.expressionsr/   r0   r1   r2   r3   Zsympy.printing.tensorflowr4   Z3sympy.tensor.array.expressions.from_matrix_to_arrayr5   Zsympy.utilities.lambdifyr6   Zsympy.testing.pytestr7   r8   rg   r9   osenvironr;   r=   r>   r?   r   r   r   r   rA   Zllor   Zm3x3Z	m3x3sympyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   rE   rE   rF   <module>   s`     l



 


`
5J