o
    oh                     @   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                       s  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/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.
    _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__	_settingsr   __version__r   )selfsettingsversion	__class__ m/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/printing/tensorflow.pyr   Q   s
   

zTensorflowPrinter.__init__c                    sh    j t|d }|d u rt |S  fdd|jD }t|dkr.d ||d f S  ||S )Nc                       g | ]}  |qS r   _print.0argr   r   r   
<listcomp>]       z5TensorflowPrinter._print_Function.<locals>.<listcomp>   %s(%s)r   )	mappinggettyper   _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_transposer2   )r   r   r,   r3   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   r=      s   
z3TensorflowPrinter._print_Derivative.<locals>.unfold)	variablesanyNotImplementedErrorr.   )r   r.   r>   r   r<   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r8d| || || |dS d| || || || ||jdd   S )Nz1.0ztensorflow.selectztensorflow.wherer   )	Piecewiser$   z{}({}, {}, {}))	r   r   $sympy.functions.elementary.piecewiserB   r*   r+   r3   r,   r   )r   r.   r   tensorflow_piecewiserB   econdr   r   r   _print_Piecewise   s&   z"TensorflowPrinter._print_Piecewisec                 C   sN   |j \}}|jtjkrd| d| |S d| d| || |S )Nr2   r   z
{}({}, {})ztensorflow.math.pow)r*   expr   Halfr3   r,   r   )r   r.   baserH   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 )rL   rM   c                    r   r   r   )r   jr!   r   r   r"      r#   zBTensorflowPrinter._print_MatrixBase.<locals>.<listcomp>.<listcomp>])joinr8   r!   r   r   r"      s   , z7TensorflowPrinter._print_MatrixBase.<locals>.<listcomp>rO   r%   )free_symbolsrP   tolistr,   )r   r.   tensorflow_fdatar   r!   r   _print_MatrixBase   s   $z#TensorflowPrinter._print_MatrixBasec                    sj   ddl m   fdd|jD fdd|jD }|r/d| t|td | df S | dS )	Nr   
MatrixExprc                    s   g | ]	}t | r|qS r   )r7   r   rV   r   r   r"      s    z3TensorflowPrinter._print_MatMul.<locals>.<listcomp>c                    s   g | ]}| vr|qS r   r   r   )mat_argsr   r   r"      s    z%s*%sr   tensorflow.linalg.matmul)sympy.matrices.expressionsrW   r*   parenthesizer   fromiterr   r-   )r   r.   r*   r   )rW   rX   r   _print_MatMul   s   zTensorflowPrinter._print_MatMulc                 C   s   |  d|jg|j S )NrY   )r-   rJ   rH   )r   r.   r   r   r   _print_MatPow   s   zTensorflowPrinter._print_MatPowc                 C   s*   g }|j D ]
}|| | qd|S )N
)r*   appendr   rP   )r   r.   retsubexprr   r   r   _print_CodeBlock   s   

z"TensorflowPrinter._print_CodeBlockr   zlinalg.einsumzmath.add	transposeoneszerosr6   )N__name__
__module____qualname____doc__printmethodsympyAbssignceilingfloorlogrH   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   r1   _print_Expr_print_Application_print_MatrixExpr_print_Relational
_print_Not
_print_And	_print_Or_print_HadamardProduct_print_Trace_print_Determinantr4   r5   rA   rG   rK   rU   r]   r^   rc   _module_einsum_add
_transpose_ones_zeros__classcell__r   r   r   r   r      s    	
!"#$%&()*+7r   c                 K   s   t |}|| S r6   )r   doprint)r.   r   printerr   r   r   tensorflow_code   s   
r   )sympy.external.importtoolsr   collections.abcr   sympy.core.mulr   sympy.core.singletonr   sympy.codegen.cfunctionsr   sympy.externalr   sympy.printing.precedencer   sympy.printing.pycoder	   r
   rl   r   r   r   r   r   r   r   <module>   s     I