a
    Ÿ¬<bF*  ã                   @   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 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                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,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__© úi/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/sympy/printing/tensorflow.pyr   Q   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_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>€   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>rH   r"   )Zfree_symbolsrI   Ú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   rM   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.expressionsrN   r&   Zparenthesizer   Zfromiterr   r)   )r   r*   r&   r   )rN   rO   r   Ú_print_MatMulµ   s    ÿþÿzTensorflowPrinter._print_MatMulc                 C   s   |   d|jg|j ¡S )NrP   )r)   rB   rA   ©r   r*   r   r   r   Ú_print_MatPowÃ   s    ÿzTensorflowPrinter._print_MatPowc                 C   s   d|   |j¡|   |j¡f S )Nz%s = %s)r   ÚlhsÚrhsrR   r   r   r   Ú_print_AssignmentÇ   s    

þz#TensorflowPrinter._print_Assignmentc                 C   s*   g }|j D ]}| |  |¡¡ q
d |¡S )NÚ
)r&   Úappendr   rI   )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ƒ‚d S )Néa   é{   éA   é[   zout of letters)ÚrangeÚchrÚ
ValueError)r   r5   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   )ÚnextrF   ©Úlettersr   r   r   Þ   r    zJTensorflowPrinter._print_ArrayTensorProduct.<locals>.<listcomp>.<listcomp>)rI   r_   r4   rf   r   r   r   Þ   r    z?TensorflowPrinter._print_ArrayTensorProduct.<locals>.<listcomp>ú%s("%s", %s)útensorflow.linalg.einsumrE   c                    s   g | ]}ˆ   |¡‘qS r   r   r   r   r   r   r   â   r    )rb   rI   Úsubranksr(   r&   )r   r*   Úcontraction_stringr   )rg   r   r   Ú_print_ArrayTensorProductÜ   s    ýz+TensorflowPrinter._print_ArrayTensorProductc           	         s|   ddl m} |j}|j}ˆ  |j|¡\}}}|s:ˆ  |¡S t||ƒrr‡ fdd„|jD ƒ}dˆ  	d¡|d 
|¡f S tƒ ‚d S )Nr   ©ÚArrayTensorProductc                    s   g | ]}d ˆ   |¡ ‘qS )z%sr   r   r   r   r   r   î   r    z=TensorflowPrinter._print_ArrayContraction.<locals>.<listcomp>rh   ri   rE   )Ú0sympy.tensor.array.expressions.array_expressionsrn   r*   Úcontraction_indicesÚ_get_einsum_stringrj   r   r3   r&   r(   rI   r<   )	r   r*   rn   rB   rp   rk   Ú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|ƒrX|jj}|jj	}n|j}|jg}ˆ  
||¡\}}}‡ fdd„|D ƒ}dˆ  d¡d	 |d
 |¡d
 |¡¡d |¡f S )Nr   rm   r!   é   c                    s   g | ]}ˆ   |¡‘qS r   r   r4   r   r   r   r     r    z:TensorflowPrinter._print_ArrayDiagonal.<locals>.<listcomp>rh   ri   z{}->{}{}rd   rE   )ro   rn   ÚlistÚdiagonal_indicesr'   r<   r3   r*   rj   r&   rq   r(   r/   rI   )	r   r*   rn   rx   rj   rt   Zdiagonal_stringrr   rs   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_formrR   r   r   r   Ú_print_PermuteDims  s
    
ýz$TensorflowPrinter._print_PermuteDimsc                 C   s   |   d|j¡S )Nr   )r)   r&   rR   r   r   r   Ú_print_ArrayAdd  s    z!TensorflowPrinter._print_ArrayAdd)N)OÚ__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   rL   rQ   rS   rV   rZ   rb   rl   ru   ry   rz   r{   Ú__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.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€   r
   r   rš   r   r   r   r   Ú<module>   s     