U
    9%e                     @   s   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 )    )version_tuple)Iterable)Mul)S)Sqrt)import_module)
PRECEDENCE)AbstractPythonCodePrinterArrayPrinterN
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,Z1dI 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 ZFdCZGdDZHdEZIdFZJdGZKdHZL  ZMS )J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 kr(tr(tj}|| _d S )Nr   )super__init__Z	_settingsr   __version__r   )selfsettingsversion	__class__ X/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/printing/tensorflow.pyr   Q   s
    
zTensorflowPrinter.__init__c                    sl    j t|d }|d k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>]   s     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_FunctionY   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_Inverser   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_Transposev   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>   s     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   r8      s    
z3TensorflowPrinter._print_Derivative.<locals>.unfold)	variablesanyNotImplementedErrorr)   )r   r)   r9   r   r7   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)   baser@   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 )rC   rD   c                    s   g | ]}  |qS r   r   )r   jr   r   r   r      s     zBTensorflowPrinter._print_MatrixBase.<locals>.<listcomp>.<listcomp>])joinr3   r   r   r   r      s     z7TensorflowPrinter._print_MatrixBase.<locals>.<listcomp>rF   r!   )Zfree_symbolsrG   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   )r2   r   rK   r   r   r      s     
 z3TensorflowPrinter._print_MatMul.<locals>.<listcomp>c                    s   g | ]}| kr|qS r   r   r   )mat_argsr   r   r      s      z%s*%sr   tensorflow.linalg.matmul)Zsympy.matrices.expressionsrL   r%   Zparenthesizer   Zfromiterr   r(   )r   r)   r%   r   )rL   rM   r   _print_MatMul   s      zTensorflowPrinter._print_MatMulc                 C   s   |  d|jg|j S )NrN   )r(   rA   r@   )r   r)   r   r   r   _print_MatPow   s     zTensorflowPrinter._print_MatPowc                 C   s*   g }|j D ]}|| | q
d|S )N
)r%   appendr   rG   )r   r)   retZsubexprr   r   r   _print_CodeBlock   s    
z"TensorflowPrinter._print_CodeBlockr   zlinalg.einsumzmath.addZ	transposeZonesZzeros)N)N__name__
__module____qualname____doc__ZprintmethodsympyZAbssignZceilingfloorlogr@   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HadamardProduct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_Determinantr/   r0   r<   r?   rB   rJ   rO   rP   rT   _moduleZ_einsum_addZ
_transposeZ_onesZ_zeros__classcell__r   r   r   r   r      s                                           7r   c                 K   s   t |}|| S r1   )r   Zdoprint)r)   r   printerr   r   r   tensorflow_code   s    rv   )Zsympy.external.importtoolsr   collections.abcr   Zsympy.core.mulr   Zsympy.core.singletonr   Zsympy.codegen.cfunctionsr   Zsympy.externalr   Zsympy.printing.precedencer   Zsympy.printing.pycoder	   r
   rY   r   r   rv   r   r   r   r   <module>   s    I