a
    kh                     @   s   d dl Zd dl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 d d	lmZmZ d dlZed
ZG dd deeZdd ZdS )    N)version_tuple)Iterable)Mul)S)Sqrt)import_module)
PRECEDENCE)AbstractPythonCodePrinterArrayPrinter
tensorflowc                P       s  e Zd ZdZdZ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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/e0j1d+d,Z1dM fd-d.	Z2 fd/d0Z3e3Z4e3Z5e3Z6e3Z7e3Z8e3Z9e3Z:e3Z;e3Z<e3Z=d1d2 Z>d3d4 Z?d5d6 Z@d7d8 ZAd9d: ZBd;d< ZCd=d> ZDd?d@ ZEdAdB ZFdCdD ZGdEdF ZHdGZIdHZJdIZKdJZLdKZMdLZN  ZOS )NTensorflowPrinterz
    Tensorflow printer which handles vectorized piecewise functions,
    logical operators, max/min, and relational operators.
    Z_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                    s2   t  | | jd }|d u r(tr(tj}|| _d S )Nr   )super__init__Z	_settingsr   __version__r   )selfsettingsversion	__class__ G/var/www/auris/lib/python3.9/site-packages/sympy/printing/tensorflow.pyr   S   s
    
zTensorflowPrinter.__init__c                    sl    j t|d }|d u r&t |S  fdd|jD }t|dkr\d ||d f S  ||S d S )Nc                    s   g | ]}  |qS r   _print.0argr   r   r   
<listcomp>_       z5TensorflowPrinter._print_Function.<locals>.<listcomp>   %s(%s)r   )	mappinggettyper   Z_print_Basicargslen_module_format_expand_fold_binary_op)r   expropchildrenr   r   r   _print_Function[   s    z!TensorflowPrinter._print_Functionc                 C   s   |  d}d|| |jS )Nztensorflow.linalg.inv{}({}))r(   formatr   r   )r   r*   r+   r   r   r   _print_Inverset   s    
z TensorflowPrinter._print_Inversec                 C   sD   | j }|r&t|tdk r&| d}n
| d}d|| |jS )Nz1.14ztensorflow.matrix_transposez"tensorflow.linalg.matrix_transposer.   )r   r   r(   r/   r   r   )r   r*   r   r+   r   r   r   _print_Transposex   s
    
z"TensorflowPrinter._print_Transposec                    s:   |j }tdd |D r td fdd|j|S )Nc                 s   s   | ]}t |tV  qd S N)
isinstancer   r   ir   r   r   	<genexpr>   r    z6TensorflowPrinter._print_Derivative.<locals>.<genexpr>z1derivation by multiple variables is not supportedc                    s:   |s  | S d d| |d d   |d f S )Nz%s(%s, %s)[0]ztensorflow.gradients)r   r(   )r*   r&   r   unfoldr   r   r9      s    
z3TensorflowPrinter._print_Derivative.<locals>.unfold)	variablesanyNotImplementedErrorr*   )r   r*   r:   r   r8   r   _print_Derivative   s
    z#TensorflowPrinter._print_Derivativec                 C   s   | j }|r t|tdk r d}nd}ddlm} |jd j\}}t|jdkrpd| || || |dS d| || || || ||jdd   S )Nz1.0ztensorflow.selectztensorflow.wherer   )	Piecewiser!   z{}({}, {}, {}))	r   r   Z$sympy.functions.elementary.piecewiser>   r&   r'   r/   r(   r   )r   r*   r   Ztensorflow_piecewiser>   eZcondr   r   r   _print_Piecewise   s&    z"TensorflowPrinter._print_Piecewisec                 C   sN   |j \}}|jtjkr.d| d| |S d| d| || |S )Nr.   r   z
{}({}, {})ztensorflow.math.pow)r&   expr   ZHalfr/   r(   r   )r   r*   baserA   r   r   r   
_print_Pow   s    
zTensorflowPrinter._print_Powc                    sD   |j r
dnd}dd fdd| D  d }d ||f S )	Nztensorflow.Variableztensorflow.constant[, c                    s,   g | ]$}d d  fdd|D  d qS )rD   rE   c                    s   g | ]}  |qS r   r   )r   jr   r   r   r      r    zBTensorflowPrinter._print_MatrixBase.<locals>.<listcomp>.<listcomp>])joinr4   r   r   r   r      r    z7TensorflowPrinter._print_MatrixBase.<locals>.<listcomp>rG   r"   )Zfree_symbolsrH   tolistr(   )r   r*   Ztensorflow_fdatar   r   r   _print_MatrixBase   s    $z#TensorflowPrinter._print_MatrixBasec                    sn   ddl m   fdd|jD fdd|jD }|r^d| t|td | df S | dS d S )	Nr   
MatrixExprc                    s   g | ]}t | r|qS r   )r3   r   rL   r   r   r      r    z3TensorflowPrinter._print_MatMul.<locals>.<listcomp>c                    s   g | ]}| vr|qS r   r   r   )mat_argsr   r   r      r    z%s*%sr   tensorflow.linalg.matmul)Zsympy.matrices.expressionsrM   r&   Zparenthesizer   Zfromiterr   r)   )r   r*   r&   r   )rM   rN   r   _print_MatMul   s    zTensorflowPrinter._print_MatMulc                 C   s   |  d|jg|j S )NrO   )r)   rB   rA   )r   r*   r   r   r   _print_MatPow   s    zTensorflowPrinter._print_MatPowc                 C   s*   g }|j D ]}|| | q
d|S )N
)r&   appendr   rH   )r   r*   retZsubexprr   r   r   _print_CodeBlock   s    
z"TensorflowPrinter._print_CodeBlockc                 C   s   d| j |j  dS )Nztensorflow.math.is_nan()r   r&   r   rA   r   r   r   _print_isnan   s    zTensorflowPrinter._print_isnanc                 C   s   d| j |j  dS )Nztensorflow.math.is_inf(rV   rW   rX   r   r   r   _print_isinf   s    zTensorflowPrinter._print_isinfr   zlinalg.einsumzmath.addZ	transposeZoneszeros)N)P__name__
__module____qualname____doc__ZprintmethodsympyZAbssignZceilingfloorlogrA   r   cosacossinasintanatanatan2coshacoshsinhasinhtanhatanhreZimr   erfZloggammaEqZNeZStrictGreaterThanZStrictLessThanZLessThanZGreaterThanAndOrNotZMaxZMinZMatAddZHadamardProductZTraceZDeterminantr#   dictr	   Z_default_settingsr   r-   Z_print_ExprZ_print_ApplicationZ_print_MatrixExprZ_print_RelationalZ
_print_NotZ
_print_AndZ	_print_OrZ_print_HadamardProductZ_print_TraceZ_print_Determinantr0   r1   r=   r@   rC   rK   rP   rQ   rU   rY   rZ   _moduleZ_einsumZ_addZ
_transposeZ_onesZ_zeros__classcell__r   r   r   r   r      s   7r   c                 K   s   t |}|| S r2   )r   Zdoprint)r*   r   printerr   r   r   tensorflow_code   s    r{   )Zsympy.codegenr`   Zsympy.codegen.cfunctionsZsympy.external.importtoolsr   collections.abcr   Zsympy.core.mulr   Zsympy.core.singletonr   r   Zsympy.externalr   Zsympy.printing.precedencer   Zsympy.printing.pycoder	   r
   r   r   r{   r   r   r   r   <module>   s    O