a
    {d=                     @   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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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   m/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/sympy/printing/tests/test_tensorflow.py
<listcomp>&       rD   	   Fc           
      C   s$  t | |d}|s dd | D }ndd | D }t }d }| < dd |D }tjjj|d}||| }W d    n1 s0    Y  |dd t	| |D }	|	
 }	|	jrt|	ts|	 }	|	 }	|s||	k sJ n:d	d |D }d
d |	D }	tdd t	||	D s J d S )Nr6   c                 S   s   g | ]}t |j|jqS rB   r+   rowscolsr@   vrB   rB   rC   rD   .   rE   z._compare_tensorflow_matrix.<locals>.<listcomp>c                 S   s   g | ]}t |j|jd  qS )g      Y@rG   rJ   rB   rB   rC   rD   0   rE   c                 S   s   g | ]}t t|qS rB   evalr1   r?   rB   rB   rC   rD   5   rE   graphc                 S   s   i | ]\}}||qS rB   rB   r@   krK   rB   rB   rC   
<dictcomp>9   rE   z._compare_tensorflow_matrix.<locals>.<dictcomp>c                 S   s   g | ]}|D ]}|qqS rB   rB   r@   rowrA   rB   rB   rC   rD   C   rE   c                 S   s   g | ]}|D ]}|qqS rB   rB   rS   rB   rB   rC   rD   D   rE   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
	variablesexpr	use_floatfrandom_matricesrO   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 }| < dd |D }tjjj|d}||| }W d    n1 s0    Y  |dd t	| |D }	|	
 }	|	jrt|	ts|	 }	|	 }	|s||	k sJ n:d	d |D }d
d |	D }	tdd t	||	D s J d S )Nr6   c                 S   s   g | ]}t |j|jd  qS )rW   r*   rH   rI   rJ   rB   rB   rC   rD   N   rE   z6_compare_tensorflow_matrix_inverse.<locals>.<listcomp>c                 S   s   g | ]}t |j|jd  qS )gQ	@rx   rJ   rB   rB   rC   rD   P   rE   c                 S   s   g | ]}t t|qS rB   rL   r?   rB   rB   rC   rD   U   rE   rN   c                 S   s   i | ]\}}||qS rB   rB   rP   rB   rB   rC   rR   Y   rE   z6_compare_tensorflow_matrix_inverse.<locals>.<dictcomp>c                 S   s   g | ]}|D ]}|qqS rB   rB   rS   rB   rB   rC   rD   c   rE   c                 S   s   g | ]}|D ]}|qqS rB   rB   rS   rB   rB   rC   rD   d   rE   c                 s   s:   | ]2\}}t || d dttt |d    k V  qdS rU   rX   r[   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 }| < dd |D }tjjj|d}||| }W d    n1 sp0    Y  |dd t	| |D }|
 }t|| dk sJ d S )	Nr6   c                 S   s"   g | ]}t |j|j d  qS )d   )r+   rH   rI   evalfrJ   rB   rB   rC   rD   k   s   z5_compare_tensorflow_matrix_scalar.<locals>.<listcomp>c                 S   s   g | ]}t t|qS rB   rL   r?   rB   rB   rC   rD   q   rE   rN   c                 S   s   i | ]\}}||qS rB   rB   rP   rB   rB   rC   rR   u   rE   z5_compare_tensorflow_matrix_scalar.<locals>.<dictcomp>ư>)r3   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rY   )	rn   ro   rq   rr   rO   rs   rt   ru   rv   rB   rB   rC   !_compare_tensorflow_matrix_scalari   s    
,r}   c                   C   s   t ddS Nr   rV   randomrandintrB   rB   rB   rC   <lambda>{   rE   r   c           
         s   t | |d} fdd| D }t }d }| < dd |D }tjjj|d}||| }W d    n1 st0    Y  |dd t	| |D 
  }	t||	 dk sJ d S )	Nr6   c                    s   g | ]
}  qS rB   rB   rJ   rngrB   rC   rD   }   rE   z._compare_tensorflow_scalar.<locals>.<listcomp>c                 S   s   g | ]}t t|qS rB   rL   r?   rB   rB   rC   rD      rE   rN   c                 S   s   i | ]\}}||qS rB   rB   rP   rB   rB   rC   rR      rE   z._compare_tensorflow_scalar.<locals>.<dictcomp>r|   )r3   r`   ra   rb   rc   rd   re   rf   rg   rh   r{   ri   rY   
rn   ro   r   rq   ZrvsrO   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      rE   c           
         s   t | |d} fdd| D }t }d }| < dd |D }tjjj|d}||| }W d    n1 st0    Y  |dd t	| |D 
 }	||	ksJ d S )Nr6   c                    s   g | ]
}  qS rB   rB   rJ   r   rB   rC   rD      rE   z2_compare_tensorflow_relational.<locals>.<listcomp>c                 S   s   g | ]}t t|qS rB   rL   r?   rB   rB   rC   rD      rE   rN   c                 S   s   i | ]\}}||qS rB   rB   rP   rB   rB   rC   rR      rE   z2_compare_tensorflow_relational.<locals>.<dictcomp>)r3   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   r   rB   r   rC   _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sBJ 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ro   rB   rB   rC   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sHJ ttf|  tt} t| dkslJ 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d } t| dksJ ttf| dd d tt} t| dksLJ ttf| dd d tt} t| dkszJ 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s2J ttf| dd d ttt} t| dksbJ t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sJJ ttf| d(d d tt} t| d)ksxJ 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   s   t   S Nr   rB   rB   rB   rC   r      rE   z&test_tensorflow_math.<locals>.<lambda>r   ztensorflow.math.floor(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.exp(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.sqrt(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   rW   ztensorflow.math.pow(x, 4)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.cos(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.acos(x)c                   S   s   t ddS )Nr   gffffff?r   uniformrB   rB   rB   rC   r      rE   ztensorflow.math.sin(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.asin(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.tan(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.atan(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.atan2(y, x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.cosh(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.acosh(x)c                   S   s   t ddS N      r   rB   rB   rB   rC   r      rE   ztensorflow.math.sinh(x)c                   S   s   t ddS r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.asinh(x)c                   S   s   t ddS r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.tanh(x)c                   S   s   t ddS r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.atanh(x)c                   S   s   t ddS )Ng      g      ?r   rB   rB   rB   rC   r      rE   ztensorflow.math.erf(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r      rE   ztensorflow.math.lgamma(x)c                   S   s   t   S r   r   rB   rB   rB   rC   r     rE   )r`   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   rB   rB   rC   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$   rB   rB   rB   rC   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sNJ tttf|  ttt} t| dksvJ tttf|  t	tt} t| dksJ tttf|  t
tt} t| dksJ tttf|  ttt} t| dksJ 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   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 J ttf|  tt } t| dksDJ tttf|  tt } t| dksjJ tttf|  ttt} t| dksJ tttf|  tt t t } t| dksJ tttttf|  td } t| dksJ ttf|  t	t} t| d	ksJ ttf|  t
t} t| d
ks6J ttf|  tt} t| dks\J 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)rp   z1.14)Ztensorflow_versionz%tensorflow.linalg.matrix_transpose(M)z1.13ztensorflow.matrix_transpose(M))r`   r4   r8   r1   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 sJ W d    n1 s0    Y  d S )Nr   rN   r8   r   r:   r6   r   r9   rW   )r`   r4   ra   rb   rc   rd   re   r/   r2   r3   constantrf   matmulrl   )
rO   ru   r8   r:   cgrq   mambr   crB   rB   rC   test_codegen_einsumc  s    
r   c               
   C   s  t std t  } |  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sJ t||f|
d}||||}|t d||}||k sJ t||}
t
|
dks0J t||f|
d}||||}||| }||k spJ t|||}
t
|
dksJ t|||f|
d}|||||}||| | }||k sJ t||||}
t
|
dksJ t||||f|
d}||||||	}||| | |	 }||k sFJ t|ddg}
t
|
dksfJ t|f|
d}|||}|t |}||k sJ tt	||g d}
t
|
dksJ t||f|
d}||||}|t t d||g d}||k sJ tt	||d}
t
|
dks>J t||f|
d}||||}|t d||}||k sJ W d    n1 s0    Y  d S )Nr   r8   r   r:   r;   r<   r   r9   rW   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)r`   r4   ra   rb   rc   rd   re   r/   r   r   r1   r3   rf   Zeinsumrl   r   r   Z	transposer   )rO   ru   r8   r:   r;   r<   r   r   ZmcZ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s8J td| d  dksPJ |d || | }t|d	kstJ 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   rg   )r   r   r   FrB   rB   rC   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   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