
    Ed                         d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ d d	lZ ed
          Z G d dee          Zd Zd	S )    )version_tuple)Iterable)Mul)S)Sqrt)import_module)
PRECEDENCE)AbstractPythonCodePrinterArrayPrinterN
tensorflowc                   B    e Zd ZdZdZi ej        dej        dej        dej	        dej
        dej        ded	ej        d
ej        dej        dej        dej        dej        dej        dej        dej        dej        di ej        dej        dej        dej        dej        dej        dej        dej        dej        dej         dej!        dej"        dej#        d ej$        d!ej%        d"ej&        d#ej'        d$ej(        d%ej)        d&ej*        d'ej+        d(ej,        d)ej-        d*iZ. e/e0j1        d+,          Z1d> fd-	Z2 fd.Z3e3Z4e3Z5e3Z6e3Z7e3Z8e3Z9e3Z:e3Z;e3Z<e3Z=d/ Z>d0 Z?d1 Z@d2 ZAd3 ZBd4 ZCd5 ZDd6 ZEd7 ZFd8ZGd9ZHd:ZId;ZJd<ZKd=ZL xZMS )?TensorflowPrinterz
    Tensorflow printer which handles vectorized piecewise functions,
    logical operators, max/min, and relational operators.
    _tensorflowcodeztensorflow.math.absztensorflow.math.signztensorflow.math.ceilztensorflow.math.floorztensorflow.math.logztensorflow.math.exptensorflow.math.sqrtztensorflow.math.cosztensorflow.math.acosztensorflow.math.sinztensorflow.math.asinztensorflow.math.tanztensorflow.math.atanztensorflow.math.atan2ztensorflow.math.coshztensorflow.math.acoshztensorflow.math.sinhztensorflow.math.asinhztensorflow.math.tanhztensorflow.math.atanhztensorflow.math.realztensorflow.math.imagztensorflow.math.angleztensorflow.math.erfztensorflow.math.lgammaztensorflow.math.equalztensorflow.math.not_equalztensorflow.math.greaterztensorflow.math.lessztensorflow.math.less_equalztensorflow.math.greater_equalztensorflow.math.logical_andztensorflow.math.logical_orztensorflow.math.logical_notztensorflow.math.maximumztensorflow.math.minimumztensorflow.math.addztensorflow.math.multiplyztensorflow.linalg.traceztensorflow.linalg.detN)tensorflow_versionc                     t                                          |           | j        d         }|t          rt          j        }|| _        d S )Nr   )super__init__	_settingsr   __version__r   )selfsettingsversion	__class__s      9lib/python3.11/site-packages/sympy/printing/tensorflow.pyr   zTensorflowPrinter.__init__Q   sM    """.!56 	-z 	- ,G")    c                 V     j                             t          |          d           }|!t                                          |          S  fd|j        D             }t          |          dk    r!                     |          d|d         dS                      ||          S )Nc                 :    g | ]}                     |          S  _print).0argr   s     r   
<listcomp>z5TensorflowPrinter._print_Function.<locals>.<listcomp>]   s%    :::DKK$$:::r      (r   ))	mappinggettyper   _print_Basicargslen_module_format_expand_fold_binary_op)r   expropchildrenr   s   `   r   _print_Functionz!TensorflowPrinter._print_FunctionY   s    \d4jj$// 	.77''---::::	:::x==A 	=##B'''' 
 ..r8<<<r   c                     |                      d          }d                    ||                     |j                            S )Nztensorflow.linalg.inv{}({}))r.   formatr!   r#   )r   r0   r1   s      r   _print_Inversez TensorflowPrinter._print_Inverser   s8      !899r4;;tx#8#8999r   c                    | j         }|r6t          |          t          d          k     r|                     d          }n|                     d          }d                    ||                     |j                            S )Nz1.14ztensorflow.matrix_transposez"tensorflow.linalg.matrix_transposer5   )r   r   r.   r6   r!   r#   )r   r0   r   r1   s       r   _print_Transposez"TensorflowPrinter._print_Transposev   s|    ) 	K}W--f0E0EE 	K$$%BCCBB$$%IJJBr4;;tx#8#8999r   c                      |j         }t          d |D                       rt          d           fd |j        |          S )Nc              3   @   K   | ]}t          |t                    V  d S N)
isinstancer   )r"   is     r   	<genexpr>z6TensorflowPrinter._print_Derivative.<locals>.<genexpr>   s,      ::1z!X&&::::::r   z1derivation by multiple variables is not supportedc                     |s                     |           S                     d          d | |d d                   d                     |d                   dS )Nztensorflow.gradientsr&   , z)[0])r!   r.   )r0   r,   r   unfolds     r   rC   z3TensorflowPrinter._print_Derivative.<locals>.unfold   su     ){{4(((''(>????F4crc++++KKR)))) r   )	variablesanyNotImplementedErrorr0   )r   r0   rD   rC   s   `  @r   _print_Derivativez#TensorflowPrinter._print_Derivative~   sn    N	::	::::: 	[%&YZZZ	 	 	 	 	 	 vdi+++r   c                 N   | j         }|r#t          |          t          d          k     rd}nd}ddlm} |j        d         j        \  }}t          |j                  dk    rQd                    |                     |          |                     |          |                     |          d          S d                    |                     |          |                     |          |                     |          |                      ||j        dd                               S )Nz1.0ztensorflow.selectztensorflow.wherer   )	Piecewiser%   z{}({}, {}, {}))	r   r   $sympy.functions.elementary.piecewiserI   r,   r-   r6   r.   r!   )r   r0   r   tensorflow_piecewiserI   econds          r   _print_Piecewisez"TensorflowPrinter._print_Piecewise   s'   ) 	6}W--e0D0DD 	6#6  #5 BBBBBB)A,#4ty>>Q 	#**##$899D!!A	    && 455KKKKNNKK		49QRR=122	4 4 	4r   c                 X   |j         \  }}|j        t          j        k    r<d                    |                     d          |                     |                    S d                    |                     d          |                     |          |                     |                    S )Nr5   r   z
{}({}, {})ztensorflow.math.pow)r,   expr   Halfr6   r.   r!   )r   r0   baserP   s       r   
_print_PowzTensorflowPrinter._print_Pow   s     I	c8qv 	P??##$:;;T[[=N=NP P P"" 566KKt{{3//1 1 	1r   c                      |j         rdnd}dd                     fd|                                D                       z   dz   }                     |          d|dS )	Nztensorflow.Variableztensorflow.constant[rB   c                 ^    g | ])}d d                     fd|D                       z   dz   *S )rU   rB   c                 :    g | ]}                     |          S r   r    )r"   jr   s     r   r$   zBTensorflowPrinter._print_MatrixBase.<locals>.<listcomp>.<listcomp>   s#    ,G,G,GT[[^^,G,G,Gr   ])join)r"   r>   r   s     r   r$   z7TensorflowPrinter._print_MatrixBase.<locals>.<listcomp>   sE    dddQRc$)),G,G,G,GQ,G,G,G"H"HHLdddr   rY   r&   r'   )free_symbolsrZ   tolistr.   )r   r0   tensorflow_fdatas   `   r   _print_MatrixBasez#TensorflowPrinter._print_MatrixBase   s}    040A\,,G\499ddddVZVaVaVcVcdddeeefii----DD
 	
r   c                 &   ddl m fd|j        D             fd|j        D             }|rL|                     t	          j        |          t          d                   d|                     d          S |                     d          S )Nr   )
MatrixExprc                 4    g | ]}t          |          |S r   )r=   )r"   r#   ra   s     r   r$   z3TensorflowPrinter._print_MatMul.<locals>.<listcomp>   s(    LLLC
3
0K0KLCLLLr   c                     g | ]}|v|	S r   r   )r"   r#   mat_argss     r   r$   z3TensorflowPrinter._print_MatMul.<locals>.<listcomp>   s#    @@@Cx,?@@@@r   r   *tensorflow.linalg.matmul)sympy.matrices.expressionsra   r,   parenthesizer   fromiterr	   r/   )r   r0   r,   ra   rd   s      @@r   _print_MatMulzTensorflowPrinter._print_MatMul   s    999999LLLL49LLL@@@@ty@@@ 	6!!#,t"4"4j6GHHHH++.: : :  ..*H6 6 6r   c                 J    |                      d|j        g|j        z            S )Nrf   )r/   rR   rP   )r   r0   s     r   _print_MatPowzTensorflowPrinter._print_MatPow   s*    **&DH(<> > 	>r   c                     g }|j         D ]*}|                    |                     |                     +d                    |          S )N
)r,   appendr!   rZ   )r   r0   retsubexprs       r   _print_CodeBlockz"TensorflowPrinter._print_CodeBlock   sI    y 	- 	-GJJt{{7++,,,,yy~~r   r   zlinalg.einsumzmath.add	transposeoneszerosr<   )N__name__
__module____qualname____doc__printmethodsympyAbssignceilingfloorlogrP   r   cosacossinasintanatanatan2coshacoshsinhasinhtanhatanhreimr#   erfloggammaEqNeStrictGreaterThanStrictLessThanLessThanGreaterThanAndOrNotMaxMinMatAddHadamardProductTraceDeterminantr(   dictr
   _default_settingsr   r3   _print_Expr_print_Application_print_MatrixExpr_print_Relational
_print_Not
_print_And	_print_Or_print_HadamardProduct_print_Trace_print_Determinantr7   r9   rG   rN   rS   r_   rj   rl   rr   _module_einsum_add
_transpose_ones_zeros__classcell__)r   s   @r   r   r      s         $K5	(5
*5
 	-5 	,5 		(5 		(5 	$5 		(5 	
*5 		(5 	
*5 		(5 	
*5  	,!5$ 	
*%5& 	,'5( 	
*)5 5* 	,+5, 	
*-5. 	,/52 	(354 	(556 		*75< 		(=5> 	0?5B 	)C5D 	-E5F 	!:G5H 	4I5J 	4K5L 	:M5P 		0Q5R 	.S5T 		0U5 5V 		,	, 	+9. 	3i5 5Gn !3  
* * * * * *= = = = = "K('' J JI,"L(: : :: : :, , ,4 4 4,	1 	1 	1
 
 
6 6 6> > >   GGDJEFFFFFr   r   c                 J    t          |          }|                    |           S r<   )r   doprint)r0   r   printers      r   tensorflow_coder      s!    ))G??4   r   )sympy.external.importtoolsr   collections.abcr   sympy.core.mulr   sympy.core.singletonr   sympy.codegen.cfunctionsr   sympy.externalr   sympy.printing.precedencer	   sympy.printing.pycoder
   r   r{   r   r   r   r   r   r   <module>r      s   4 4 4 4 4 4 $ $ $ $ $ $       " " " " " " ) ) ) ) ) ) ( ( ( ( ( ( 0 0 0 0 0 0 I I I I I I I I ]<((
E E E E E&? E E EP! ! ! ! !r   