o
    8Va<                     @   sZ  d dl Z d dlmZ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.m/Z/m0Z0 d dl1m2Z2m3Z3m4Z4m5Z5m6Z6 d d	l7m8Z8 d d
l9m:Z: d dl;m<Z< d dl=m>Z> d dl=m?Z? ed Z@ZAeArd dlBZBdeBjCd< e5dddZDe5dddZEe5dddZFe5dddZGed\ZHZIZJZKe@durdd eLd ddD ZMe@NeMZOe-eMZPd:ddZQd:ddZRdd  ZSd!d" fd#d$ZTd%d" fd&d'ZUd(d) ZVe?d*d+ ZWd,d- ZXd.d/ ZYe?d0d1 ZZd2d3 Z[d4d5 Z\d6d7 Z]d8d9 Z^dS );    N)symbols
Derivative)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2ZTF_CPP_MIN_LOG_LEVELM   NPQzx y z tc                 C   s$   g | ]}d d t ||d D qS )c                 S   s   g | ]}|qS  r=   ).0jr=   r=   F/usr/lib/python3/dist-packages/sympy/printing/tests/test_tensorflow.py
<listcomp>%   s    z<listcomp>.<listcomp>r9   )ranger>   ir=   r=   r@   rA   %   s   $ rA   	   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  |dd t	| |D }	|	
 }	|	jrht|	tsd|	 }	|	 }	|st||	k srJ 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 r=   r+   rowscolsr>   vr=   r=   r@   rA   -       z._compare_tensorflow_matrix.<locals>.<listcomp>c                 S   s   g | ]}t |j|jd  qS )g      Y@rG   rJ   r=   r=   r@   rA   /       c                 S      g | ]}t t|qS r=   evalr1   rC   r=   r=   r@   rA   4       graphc                 S      i | ]\}}||qS r=   r=   r>   krK   r=   r=   r@   
<dictcomp>8       z._compare_tensorflow_matrix.<locals>.<dictcomp>c                 S      g | ]	}|D ]}|qqS r=   r=   r>   rowrD   r=   r=   r@   rA   B   rL   c                 S   rY   r=   r=   rZ   r=   r=   r@   rA   C   rL   c                 s   <    | ]\}}t || d dttt |d    k V  qdS 
      Nabsintr'   r>   abr=   r=   r@   	<genexpr>D       ,
z-_compare_tensorflow_matrix.<locals>.<genexpr>r3   tfGraph
as_defaultcompatv1SessionrunsubszipdoitZ	is_Matrix
isinstancer)   Zas_explicittolistall
	variablesexpr	use_floatfrandom_matricesrS   rrandom_variablessessioner=   r=   r@   _compare_tensorflow_matrix*   0   

r   c           
      C   rF   )Nr6   c                 S      g | ]}t |j|jd  qS )r_   r*   rH   rI   rJ   r=   r=   r@   rA   M   rM   z6_compare_tensorflow_matrix_inverse.<locals>.<listcomp>c                 S   r   )gQ	@r   rJ   r=   r=   r@   rA   O   rM   c                 S   rN   r=   rO   rC   r=   r=   r@   rA   T   rQ   rR   c                 S   rT   r=   r=   rU   r=   r=   r@   rW   X   rX   z6_compare_tensorflow_matrix_inverse.<locals>.<dictcomp>c                 S   rY   r=   r=   rZ   r=   r=   r@   rA   b   rL   c                 S   rY   r=   r=   rZ   r=   r=   r@   rA   c   rL   c                 s   r\   r]   r`   rc   r=   r=   r@   rf   d   rg   z5_compare_tensorflow_matrix_inverse.<locals>.<genexpr>rh   rv   r=   r=   r@   "_compare_tensorflow_matrix_inverseJ   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  |dd t	| |D }|
 }t|| dk sXJ d S )	Nr6   c                 S   s"   g | ]}t |j|j d  qS )d   )r+   rH   rI   evalfrJ   r=   r=   r@   rA   j   s    z5_compare_tensorflow_matrix_scalar.<locals>.<listcomp>c                 S   rN   r=   rO   rC   r=   r=   r@   rA   p   rQ   rR   c                 S   rT   r=   r=   rU   r=   r=   r@   rW   t   rX   z5_compare_tensorflow_matrix_scalar.<locals>.<dictcomp>ư>)r3   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   ra   )	rw   rx   rz   r{   rS   r|   r}   r~   r   r=   r=   r@   !_compare_tensorflow_matrix_scalarh   s   
r   c                   C      t ddS Nr   r^   randomZrandintr=   r=   r=   r@   <lambda>z       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  |dd t	| |D 
  }	t||	 dk sZJ d S )	Nr6   c                       g | ]}  qS r=   r=   rJ   rngr=   r@   rA   |       z._compare_tensorflow_scalar.<locals>.<listcomp>c                 S   rN   r=   rO   rC   r=   r=   r@   rA      rQ   rR   c                 S   rT   r=   r=   rU   r=   r=   r@   rW      rX   z._compare_tensorflow_scalar.<locals>.<dictcomp>r   )r3   ri   rj   rk   rl   rm   rn   ro   rp   rq   r   rr   ra   
rw   rx   r   rz   ZrvsrS   r|   Ztf_rvsr~   r   r=   r   r@   _compare_tensorflow_scalary   s   
"r   c                   C   r   r   r   r=   r=   r=   r@   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  |dd t	| |D 
 }	||	ksTJ d S )Nr6   c                    r   r=   r=   rJ   r   r=   r@   rA      r   z2_compare_tensorflow_relational.<locals>.<listcomp>c                 S   rN   r=   rO   rC   r=   r=   r@   rA      rQ   rR   c                 S   rT   r=   r=   rU   r=   r=   r@   rW      rX   z2_compare_tensorflow_relational.<locals>.<dictcomp>)r3   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   r   r=   r   r@   _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 )Nr9   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rx   r=   r=   r@   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   r=   r=   r=   r@   r          z&test_tensorflow_math.<locals>.<lambda>r   ztensorflow.math.floor(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.exp(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.sqrt(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   r_   ztensorflow.math.pow(x, 4)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.cos(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.acos(x)c                   S   r   )Nr   gffffff?r   Zuniformr=   r=   r=   r@   r      r   ztensorflow.math.sin(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.asin(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.tan(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.atan(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.atan2(y, x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.cosh(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.acosh(x)c                   S   r   N      r   r=   r=   r=   r@   r      r   ztensorflow.math.sinh(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.asinh(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.tanh(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.atanh(x)c                   S   r   )Ng      g      ?r   r=   r=   r=   r@   r      r   ztensorflow.math.erf(x)c                   S   r   r   r   r=   r=   r=   r@   r      r   ztensorflow.math.lgamma(x)c                   S   r   r   r   r=   r=   r=   r@   r     r   )ri   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   r=   r=   r@   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$   r=   r=   r=   r@   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))ri   r4   r   r   r   r1   r   r	   r
   r   r   r   r   r=   r=   r@   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   r8   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)r9   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)ry   z1.14)Ztensorflow_versionz%tensorflow.linalg.matrix_transpose(M)z1.13ztensorflow.matrix_transpose(M))ri   r4   r8   r1   r   r:   r-   r;   r<   r0   r,   r   r.   r   Tr   r=   r=   r@   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   rR   r8   r   r:   r6   r   r9   r_   )ri   r4   rj   rk   rl   rm   rn   r/   r2   r3   constantro   matmulru   )
rS   r~   r8   r:   cgrz   mambr   cr=   r=   r@   test_codegen_einsumb  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   r8   r   r:   r;   r<   r   r9   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   r9   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)ri   r4   rj   rk   rl   rm   rn   r/   r   r   r1   r3   ro   Zeinsumru   r   r   Z	transposer   )rS   r~   r8   r:   r;   r<   r   r   ZmcZmdr   rz   r   r   r=   r=   r@   test_codegen_extraw  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   r9   BC)r   r   zA[0, 0]z	3*A[0, 0]z&(tensorflow.math.add((-1)*B, A))[0, 0])r/   r1   rp   )r   r   r   Fr=   r=   r@   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   r=   r=   r@   test_tensorflow_Derivative  s   r   )F)_r   Zsympyr   r   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.matricesr(   r)   r*   r+   Zsympy.matrices.expressionsr,   r-   r.   r/   r0   Zsympy.printing.tensorflowr1   Z3sympy.tensor.array.expressions.conv_matrix_to_arrayr2   Zsympy.utilities.lambdifyr3   Zsympy.testing.pytestr4   r5   ri   r6   osenvironr8   r:   r;   r<   r   r   r   r   rB   Zllor   Zm3x3Z	m3x3sympyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   r=   r=   r@   <module>   sX     l



 


`
5J