o
    g<                     @   sb  d dl 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-m.Z.m/Z/m0Z0m1Z1 d d	l2m3Z3m4Z4m5Z5m6Z6m7Z7 d d
l8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? d dl>m@Z@ ed ZAZBeBrd dlCZCdeCjDd< e6dddZEe6dddZFe6dddZGe6dddZHed\ZIZJZKZLeAdurdd eMd ddD ZNeAOeNZPe.eNZQd;ddZRd;ddZSd d! ZTd"d# fd$d%ZUd&d# fd'd(ZVd)d* ZWe@d+d, ZXd-d. ZYd/d0 ZZe@d1d2 Z[d3d4 Z\d5d6 Z]d7d8 Z^d9d: Z_dS )<    N)
Derivative)symbols)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)Matrix
MatrixBaseeye
randMatrix)DeterminantHadamardProductInverseMatrixSymbolTrace)tensorflow_code)convert_matrix_to_array)lambdify)skip)XFAIL
tensorflow2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 rC   [/var/www/visachat/venv/lib/python3.10/site-packages/sympy/printing/tests/test_tensorflow.py
<listcomp>&       rE   	   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 )Nr6   c                 S   s   g | ]	}t |j|jqS rC   r+   rowscolsrA   vrC   rC   rD   rE   .       z._compare_tensorflow_matrix.<locals>.<listcomp>c                 S   s   g | ]}t |j|jd  qS )g      Y@rI   rL   rC   rC   rD   rE   0   rF   c                 S      g | ]}t t|qS rC   evalr1   r@   rC   rC   rD   rE   5       graphc                 S      g | ]	}|D ]}|qqS rC   rC   rA   rowrB   rC   rC   rD   rE   C   rN   c                 S   rU   rC   rC   rV   rC   rC   rD   rE   D   rN   c                 s   <    | ]\}}t || d dttt |d    k V  qdS 
      Nabsintr'   rA   abrC   rC   rD   	<genexpr>E       ,
z-_compare_tensorflow_matrix.<locals>.<genexpr>r3   tfGraph
as_defaultcompatv1Sessionrunsubsdictzipdoit	is_Matrix
isinstancer)   as_explicittolistall
	variablesexpr	use_floatfrandom_matricesrT   rrandom_variablessessionerC   rC   rD   _compare_tensorflow_matrix+   0   

r   c           
      C   rH   )Nr6   c                 S      g | ]}t |j|jd  qS )r[   r*   rJ   rK   rL   rC   rC   rD   rE   N   rF   z6_compare_tensorflow_matrix_inverse.<locals>.<listcomp>c                 S   r   )gQ	@r   rL   rC   rC   rD   rE   P   rF   c                 S   rO   rC   rP   r@   rC   rC   rD   rE   U   rR   rS   c                 S   rU   rC   rC   rV   rC   rC   rD   rE   c   rN   c                 S   rU   rC   rC   rV   rC   rC   rD   rE   d   rN   c                 s   rX   rY   r\   r_   rC   rC   rD   rb   e   rc   z5_compare_tensorflow_matrix_inverse.<locals>.<genexpr>rd   ru   rC   rC   rD   "_compare_tensorflow_matrix_inverseK   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 )Nr6   c                 S   s"   g | ]}t |j|j d  qS )d   )r+   rJ   rK   evalfrL   rC   rC   rD   rE   k   s    z5_compare_tensorflow_matrix_scalar.<locals>.<listcomp>c                 S   rO   rC   rP   r@   rC   rC   rD   rE   q   rR   rS   ư>)r3   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   r]   )	rv   rw   ry   rz   rT   r{   r|   r}   r~   rC   rC   rD   !_compare_tensorflow_matrix_scalari   s   
r   c                   C      t ddS Nr   rZ   randomrandintrC   rC   rC   rD   <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 )Nr6   c                       g | ]}  qS rC   rC   rL   rngrC   rD   rE   }       z._compare_tensorflow_scalar.<locals>.<listcomp>c                 S   rO   rC   rP   r@   rC   rC   rD   rE      rR   rS   r   )r3   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   r   ro   r]   
rv   rw   r   ry   rvsrT   r{   tf_rvsr}   r~   rC   r   rD   _compare_tensorflow_scalarz   s   
r   c                   C   r   r   r   rC   rC   rC   rD   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 )Nr6   c                    r   rC   rC   rL   r   rC   rD   rE      r   z2_compare_tensorflow_relational.<locals>.<listcomp>c                 S   rO   rC   rP   r@   rC   rC   rD   rE      rR   rS   )r3   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   r   rC   r   rD   _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]]))	r1   r*   r(   xr   yr   ztrw   rC   rC   rD   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   rC   rC   rC   rD   r          z&test_tensorflow_math.<locals>.<lambda>r   ztensorflow.math.floor(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.exp(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.sqrt(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   r[   ztensorflow.math.pow(x, 4)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.cos(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.acos(x)c                   S   r   )Nr   gffffff?r   uniformrC   rC   rC   rD   r      r   ztensorflow.math.sin(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.asin(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.tan(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.atan(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.atan2(y, x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.cosh(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.acosh(x)c                   S   r   N      r   rC   rC   rC   rD   r      r   ztensorflow.math.sinh(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.asinh(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.tanh(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.atanh(x)c                   S   r   )Ng      g      ?r   rC   rC   rC   rD   r      r   ztensorflow.math.erf(x)c                   S   r   r   r   rC   rC   rC   rD   r      r   ztensorflow.math.lgamma(x)c                   S   r   r   r   rC   rC   rC   rD   r     r   )re   r4   r   r   r1   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r%   r&   r   rC   rC   rD   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))r1   r"   r   r#   r$   rC   rC   rC   rD   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))re   r4   r   r   r   r1   r   r	   r
   r   r   r   r   rC   rC   rD   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   r9   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)tensorflow_versionz%tensorflow.linalg.matrix_transpose(M)z1.13ztensorflow.matrix_transpose(M))re   r4   r9   r1   r   r;   r-   r<   r=   r0   r,   r   r.   r   Tr   rC   rC   rD   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   rS   r9   r   r;   r6   r   r:   r[   )re   r4   rf   rg   rh   ri   rj   r/   r2   r3   constantrk   matmulrt   )
rT   r}   r9   r;   cgry   mambr   crC   rC   rD   test_codegen_einsumc  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   r9   r   r;   r<   r=   r   r:   r[   r   r   r      z'tensorflow.linalg.einsum("ab,cd", M, N)r6   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)re   r4   rf   rg   rh   ri   rj   r/   r   r   r1   r3   rk   einsumrt   r   r   	transposer   )rT   r}   r9   r;   r<   r=   r   r   mcmdr   ry   r   r   rC   rC   rD   test_codegen_extrax  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])r/   r1   rl   )r   r   r   FrC   rC   rD   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   r1   r   rC   rC   rD   test_tensorflow_Derivative  s   r   )F)`r   sympy.core.functionr   sympy.core.symbolr   0sympy.tensor.array.expressions.array_expressionsr   r   r   r   sympy.core.relationalr   r	   r
   r   r   r   sympy.externalr   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'   sympy.matricesr(   r)   r*   r+   sympy.matrices.expressionsr,   r-   r.   r/   r0   sympy.printing.tensorflowr1   3sympy.tensor.array.expressions.from_matrix_to_arrayr2   sympy.utilities.lambdifyr3   sympy.testing.pytestr4   r5   re   r6   osenvironr9   r;   r<   r=   r   r   r   r   r?   llor   m3x3	m3x3sympyr   r   r   r   r   r   r   r   r   r   r   r   r   r   rC   rC   rC   rD   <module>   sZ     l



 


`
5J