o
    8Va)                     @   s   d dl mZ d dlmZ d dlmZ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Ze
d	ZG d
d deZdd ZdS )    )version_tuple)Iterable)MulS)Sqrt)import_module)
PRECEDENCE)AbstractPythonCodePrinterN
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,Z1dQ 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 ZHdGdH ZIdIdJ ZJdKdL ZKdMdN ZLdOdP ZM  ZNS )R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.minimum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__ ;/usr/lib/python3/dist-packages/sympy/printing/tensorflow.pyr   P   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   Z_print_Basicargslen_module_format_expand_fold_binary_op)r   expropchildrenr   r   r   _print_FunctionX   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_Inverseq   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)   r0   r   r   )r   r+   r   r,   r   r   r   _print_Transposeu   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   r9   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   sympyr?   r'   r(   r0   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Halfr0   r)   r   )r   r+   baserC   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 )rF   rG   c                    r   r   r   r   jr   r   r   r       r!   zBTensorflowPrinter._print_MatrixBase.<locals>.<listcomp>.<listcomp>])joinr5   r   r   r   r       s   , z7TensorflowPrinter._print_MatrixBase.<locals>.<listcomp>rJ   r#   )Zfree_symbolsrK   tolistr)   )r   r+   Z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   )r4   r   rO   r   r   r           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)Zsympy.matrices.expressionsrP   r'   Zparenthesizer   Zfromiterr   r*   )r   r+   r'   r   )rP   rR   r   _print_MatMul   s   zTensorflowPrinter._print_MatMulc                 C   s   |  d|jg|j S )NrS   )r*   rD   rC   r   r+   r   r   r   _print_MatPow   s   zTensorflowPrinter._print_MatPowc                 C   s   d|  |j|  |jf S )Nz%s = %s)r   ZlhsZrhsrU   r   r   r   _print_Assignment   s   

z#TensorflowPrinter._print_Assignmentc                 C   s*   g }|j D ]
}|| | qd|S )N
)r'   appendr   rK   )r   r+   retZsubexprr   r   r   _print_CodeBlock   s   

z"TensorflowPrinter._print_CodeBlockc                 c   s>    t ddD ]}t|V  qt ddD ]}t|V  qtd)Na   {   A   [   zout of letters)rangechr
ValueError)r   r6   r   r   r    _get_letter_generator_for_einsum   s   z2TensorflowPrinter._get_letter_generator_for_einsumc                    sL      d fdd|jD }dd|dfdd|jD f S )N,c                    s(   g | ]}d   fddt|D qS ) c                    s   g | ]}t  qS r   )nextrH   lettersr   r   r       s    zJTensorflowPrinter._print_ArrayTensorProduct.<locals>.<listcomp>.<listcomp>)rK   r`   r5   rg   r   r   r       s   ( z?TensorflowPrinter._print_ArrayTensorProduct.<locals>.<listcomp>%s("%s", %s)tensorflow.linalg.einsumrG   c                    r   r   r   r   r   r   r   r       r!   )rc   rK   subranksr)   r'   )r   r+   contraction_stringr   )rh   r   r   _print_ArrayTensorProduct   s   z+TensorflowPrinter._print_ArrayTensorProductc           	         sx   ddl m} |j}|j} |j|\}}}|s |S t||r9 fdd|jD }d 	d|d
|f S t )Nr   ArrayTensorProductc                    s   g | ]	}d   | qS )z%sr   r   r   r   r   r       rQ   z=TensorflowPrinter._print_ArrayContraction.<locals>.<listcomp>ri   rj   rG   )0sympy.tensor.array.expressions.array_expressionsro   r+   contraction_indices_get_einsum_stringrk   r   r4   r'   r)   rK   r=   )	r   r+   ro   rD   rq   rl   letters_freeletters_dumelemsr   r   r   _print_ArrayContraction   s   

z)TensorflowPrinter._print_ArrayContractionc           	   	      s   ddl m} t|j}t|dkrtt|d dkrtt|j|r,|jj}|jj	}n|j}|jg} 
||\}}} fdd|D }d dd	|d
|d
|d|f S )Nr   rn   r"      c                    r   r   r   r5   r   r   r   r      r!   z:TensorflowPrinter._print_ArrayDiagonal.<locals>.<listcomp>ri   rj   z{}->{}{}re   rG   )rp   ro   listdiagonal_indicesr(   r=   r4   r+   rk   r'   rr   r)   r0   rK   )	r   r+   ro   ry   rk   ru   Zdiagonal_stringrs   rt   r   r   r   _print_ArrayDiagonal   s$   

z&TensorflowPrinter._print_ArrayDiagonalc                 C   s&   d|  d| |j| |jjf S )Nz
%s(%s, %s)ztensorflow.transpose)r)   r   r+   ZpermutationZ
array_formrU   r   r   r   _print_PermuteDims  s
   
z$TensorflowPrinter._print_PermuteDimsc                 C   s   |  d|jS )Nr   )r*   r'   rU   r   r   r   _print_ArrayAdd  s   z!TensorflowPrinter._print_ArrayAddr3   )O__name__
__module____qualname____doc__Zprintmethodr@   ZAbssignZceilingZfloorlogrC   r   ZcosZacosZsinZasinZtanZatanZatan2ZcoshZacoshZsinhZasinhZtanhZatanhreZimr   ZerfZloggammaZEqZNeZStrictGreaterThanZStrictLessThanZLessThanZGreaterThanZAndZOrZ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_Determinantr1   r2   r>   rB   rE   rN   rT   rV   rW   r[   rc   rm   rv   rz   r{   r|   __classcell__r   r   r   r   r      s    	
!"#$%&()*+7	r   c                 K   s   t |}|| S r3   )r   Zdoprint)r+   r   Zprinterr   r   r   tensorflow_code  s   
r   )Zsympy.external.importtoolsr   Zcollections.abcr   r@   r   r   Zsympy.codegen.cfunctionsr   Zsympy.externalr   Zsympy.printing.precedencer   Zsympy.printing.pycoder	   r
   r   r   r   r   r   r   <module>   s      