U
    	-e=                     @   sf  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 ZAZBeBrdd dlCZCdeCjDd< e6dddZEe6dddZFe6dddZGe6dddZHed\ZIZJZKZLeAdk	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2ZTF_CPP_MIN_LOG_LEVELM   NPQzx y z tc                 C   s   g | ]}t t||d  qS )r9   )listrange.0i rB   e/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/sympy/printing/tests/test_tensorflow.py
<listcomp>&   s     rD   	   Fc           
   	   C   s  t | |d}|s dd | D }ndd | D }t }d }| 2 dd |D }tjjj|d}||| }W 5 Q R X |dd t	| |D }	|	
 }	|	jrt|	ts|	 }	|	 }	|s||	k stn:d	d |D }d
d |	D }	tdd t	||	D std S )Nr6   c                 S   s   g | ]}t |j|jqS rB   r+   rowscolsr@   vrB   rB   rC   rD   .   s     z._compare_tensorflow_matrix.<locals>.<listcomp>c                 S   s   g | ]}t |j|jd  qS )g      Y@rF   rI   rB   rB   rC   rD   0   s     c                 S   s   g | ]}t t|qS rB   evalr1   r?   rB   rB   rC   rD   5   s     graphc                 S   s   i | ]\}}||qS rB   rB   r@   krJ   rB   rB   rC   
<dictcomp>9   s      z._compare_tensorflow_matrix.<locals>.<dictcomp>c                 S   s   g | ]}|D ]}|qqS rB   rB   r@   rowrA   rB   rB   rC   rD   C   s       c                 S   s   g | ]}|D ]}|qqS rB   rB   rR   rB   rB   rC   rD   D   s       c                 s   s:   | ]2\}}t || d dttt |d    k V  qdS 
      Nabsintr'   r@   abrB   rB   rC   	<genexpr>E   s    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AssertionError
	variablesexpr	use_floatfrandom_matricesrN   rrandom_variablessessionerB   rB   rC   _compare_tensorflow_matrix+   s.    

rw   c           
   	   C   s  t | |d}|s dd | D }ndd | D }t }d }| 2 dd |D }tjjj|d}||| }W 5 Q R X |dd t	| |D }	|	
 }	|	jrt|	ts|	 }	|	 }	|s||	k stn:d	d |D }d
d |	D }	tdd t	||	D std S )Nr6   c                 S   s   g | ]}t |j|jd  qS )rV   r*   rG   rH   rI   rB   rB   rC   rD   N   s     z6_compare_tensorflow_matrix_inverse.<locals>.<listcomp>c                 S   s   g | ]}t |j|jd  qS )gQ	@rx   rI   rB   rB   rC   rD   P   s     c                 S   s   g | ]}t t|qS rB   rK   r?   rB   rB   rC   rD   U   s     rM   c                 S   s   i | ]\}}||qS rB   rB   rO   rB   rB   rC   rQ   Y   s      z6_compare_tensorflow_matrix_inverse.<locals>.<dictcomp>c                 S   s   g | ]}|D ]}|qqS rB   rB   rR   rB   rB   rC   rD   c   s       c                 S   s   g | ]}|D ]}|qqS rB   rB   rR   rB   rB   rC   rD   d   s       c                 s   s:   | ]2\}}t || d dttt |d    k V  qdS rT   rW   rZ   rB   rB   rC   r]   e   s    z5_compare_tensorflow_matrix_inverse.<locals>.<genexpr>r^   rm   rB   rB   rC   "_compare_tensorflow_matrix_inverseK   s.    

ry   c           	   	   C   s   t | |d}dd | D }t }d }| 2 dd |D }tjjj|d}||| }W 5 Q R X |dd t	| |D }|
 }t|| dk std S )	Nr6   c                 S   s"   g | ]}t |j|j d  qS )d   )r+   rG   rH   evalfrI   rB   rB   rC   rD   k   s    z5_compare_tensorflow_matrix_scalar.<locals>.<listcomp>c                 S   s   g | ]}t t|qS rB   rK   r?   rB   rB   rC   rD   q   s     rM   c                 S   s   i | ]\}}||qS rB   rB   rO   rB   rB   rC   rQ   u   s      z5_compare_tensorflow_matrix_scalar.<locals>.<dictcomp>ư>)r3   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   rX   rl   )	rn   ro   rq   rr   rN   rs   rt   ru   rv   rB   rB   rC   !_compare_tensorflow_matrix_scalari   s    
r}   c                   C   s   t ddS Nr   rU   randomrandintrB   rB   rB   rC   <lambda>{       r   c           
   	      s   t | |d} fdd| D }t }d }| 2 dd |D }tjjj|d}||| }W 5 Q R X |dd t	| |D 
  }	t||	 dk std S )	Nr6   c                    s   g | ]
}  qS rB   rB   rI   rngrB   rC   rD   }   s     z._compare_tensorflow_scalar.<locals>.<listcomp>c                 S   s   g | ]}t t|qS rB   rK   r?   rB   rB   rC   rD      s     rM   c                 S   s   i | ]\}}||qS rB   rB   rO   rB   rB   rC   rQ      s      z._compare_tensorflow_scalar.<locals>.<dictcomp>r|   )r3   r_   r`   ra   rb   rc   rd   re   rf   rg   r{   rh   rX   rl   
rn   ro   r   rq   ZrvsrN   rs   Ztf_rvsru   rv   rB   r   rC   _compare_tensorflow_scalarz   s    
"r   c                   C   s   t ddS r~   r   rB   rB   rB   rC   r      r   c           
   	      s   t | |d} fdd| D }t }d }| 2 dd |D }tjjj|d}||| }W 5 Q R X |dd t	| |D 
 }	||	kstd S )Nr6   c                    s   g | ]
}  qS rB   rB   rI   r   rB   rC   rD      s     z2_compare_tensorflow_relational.<locals>.<listcomp>c                 S   s   g | ]}t t|qS rB   rK   r?   rB   rB   rC   rD      s     rM   c                 S   s   i | ]\}}||qS rB   rB   rO   rB   rB   rC   rQ      s      z2_compare_tensorflow_relational.<locals>.<dictcomp>)r3   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   rl   r   rB   r   rC   _compare_tensorflow_relational   s    
r   c                  C   sF   t tddkstttttgttt	 gg} t | dksBt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*   rl   r(   xr   yr   ztro   rB   rB   rC   test_tensorflow_printing   s    
r   c                  C   s  t std tt} t| dks$tttf|  tt} t| dksHtttf|  tt} t| dksltttf| dd d t	t} t| dkstttf| d	d d t
t} t| d
kstttf| dd d tt} t| dkstttf| dd d td } t| dkstttf| dd d tt} t| dksLtttf| dd d tt} t| dksztttf| dd d tt} t| dkstttf| dd d tt} t| dkstttf| dd d tt} t| dkstttf| dd d tt} t| dks2tttf| dd d ttt} t| dksbttttf| dd d tt} t| dkstttf| d d d tt} t| d!kstttf| d"d d tt} t| d#kstttf| d$d d tt} t| d%kstttf| d&d d tt} t| d'ksJtttf| d(d d tt} t| d)ksxtttf| d*d d tt} t| d+kstttf| d,d d tt} t| d-kst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   s   t   S Nr   rB   rB   rB   rC   r      r   z&test_tensorflow_math.<locals>.<lambda>r   ztensorflow.math.floor(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.exp(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.sqrt(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   rV   ztensorflow.math.pow(x, 4)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.cos(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.acos(x)c                   S   s   t ddS )Nr   gffffff?r   uniformrB   rB   rB   rC   r      r   ztensorflow.math.sin(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.asin(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.tan(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.atan(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.atan2(y, x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.cosh(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.acosh(x)c                   S   s   t ddS N      r   rB   rB   rB   rC   r      r   ztensorflow.math.sinh(x)c                   S   s   t ddS r   r   rB   rB   rB   rC   r      r   ztensorflow.math.asinh(x)c                   S   s   t ddS r   r   rB   rB   rB   rC   r      r   ztensorflow.math.tanh(x)c                   S   s   t ddS r   r   rB   rB   rB   rC   r      r   ztensorflow.math.atanh(x)c                   S   s   t ddS )Ng      g      ?r   rB   rB   rB   rC   r      r   ztensorflow.math.erf(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      r   ztensorflow.math.lgamma(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r     r   )r_   r4   r   r   r1   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r%   r&   r   rB   rB   rC   test_tensorflow_math   s    
      r   c                   C   s@   t ttdkstt ttdks(tt ttdks<td S )Nztensorflow.math.real(x)ztensorflow.math.imag(x)ztensorflow.math.angle(x))r1   r"   r   rl   r#   r$   rB   rB   rB   rC   test_tensorflow_complexes  s    r   c                  C   s   t std ttt} t| dks&ttttf|  ttt} t| dksNttttf|  t	tt} t| dksvttttf|  t
tt} t| dksttttf|  ttt} t| dksttttf|  ttt} t| dkst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))r_   r4   r   r   r   r1   rl   r   r	   r
   r   r   r   r   rB   rB   rC   test_tensorflow_relational  s(    





r   c                  C   s  t std t} t| dks tttf|  tt } t| dksDttttf|  tt } t| dksjttttf|  ttt} t| dksttttf|  tt t t	 } t| dkstttttt	f|  td } t| dkstttf|  t
t} t| d	kstttf|  tt} t| d
ks6tttf|  tt} t| dks\tttf| dd tj} t| dddkstt| dddkst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)rp   z1.14)Ztensorflow_versionz%tensorflow.linalg.matrix_transpose(M)z1.13ztensorflow.matrix_transpose(M))r_   r4   r8   r1   rl   rw   r:   r-   r;   r<   r0   r,   r}   r.   ry   Tr   rB   rB   rC   test_tensorflow_matrices-  sR    




r   c            
   	   C   s   t std t  } |   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 stW 5 Q R X d S )Nr   rM   r8   r   r:   r6   r   r9   rV   )r_   r4   r`   ra   rb   rc   rd   r/   r2   r3   constantre   matmulrk   rl   )
rN   ru   r8   r:   cgrq   mambr   crB   rB   rC   test_codegen_einsumc  s    
r   c               
   C   s  t std t  } |  r 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stt||f|
d}||||}|t d||}||k stt||}
t
|
dks0tt||f|
d}||||}||| }||k sptt|||}
t
|
dkstt|||f|
d}|||||}||| | }||k stt||||}
t
|
dkstt||||f|
d}||||||	}||| | |	 }||k sFtt|ddg}
t
|
dksftt|f|
d}|||}|t |}||k sttt	||ddddg}
t
|
dkstt||f|
d}||||}|t t d||ddddg}||k s$ttt	||d}
t
|
dksFtt||f|
d}||||}|t d||}||k stW 5 Q R X d S )Nr   r8   r   r:   r;   r<   r   r9   rV   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])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)r_   r4   r`   ra   rb   rc   rd   r/   r   r   r1   rl   r3   re   Zeinsumrk   r   r   Z	transposer   )rN   ru   r8   r:   r;   r<   r   r   Zmcmdr   rq   r   r   rB   rB   rC   test_codegen_extrax  s    




$
r   c                  C   sx   t ddd} t ddd}t ddd}t| d dks8ttd| d  dksPt|d || | }t|d	kstt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   rl   rf   )r   r   r   FrB   rB   rC   test_MatrixElement_printing  s    r   c                  C   s"   t ttt} t| dkstd S )Nz2tensorflow.gradients(tensorflow.math.sin(x), x)[0])r   r   r   r1   rl   r   rB   rB   rC   test_tensorflow_Derivative  s    r   )F)F)`r   Zsympy.core.functionr   Zsympy.core.symbolr   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.from_matrix_to_arrayr2   Zsympy.utilities.lambdifyr3   Zsympy.testing.pytestr4   r5   r_   r6   osenvironr8   r:   r;   r<   r   r   r   r   r>   Zllor   Zm3x3Z	m3x3sympyrw   ry   r}   r   r   r   r   r   r   r   r   r   r   r   rB   rB   rB   rC   <module>   sX    l



 



`
5J