a
    ŪĄhKP  ć                   @   sx   d dl Z d dlmZ d dlZd dlmZmZ d dlmZ dZ	dZ
dZG dd	 d	eZG d
d deZG dd deZdS )é    N)ŚOptional)Ś
PRECEDENCEŚ
precedence)Ś
StrPrinterZint64_tl   ’’’’ ģū’’’         c                   @   s¶  e Zd ZdZejedddZd3ejee edddZ	ejedd	d
Z
ejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejeddd Zejedd!d"Zejedd#d$Zejedd%d&Zejedd'd(Zejedd)d*Zejedd+d,Zejedd-d.Zejedd/d0Zejedd1d2ZdS )4ŚExprPrinterZ_torch_sympystr©ŚexprŚreturnc                 C   s   |   |jdt|”S )NŚ*©Ś	stringifyŚargsr   ©Śselfr	   © r   śW/var/www/html/assistant/venv/lib/python3.9/site-packages/torch/utils/_sympy/printers.pyŚ
_print_Mul   s    zExprPrinter._print_MulN)r	   Śorderr
   c                 C   s   |   |jdt|”S )Nz + r   )r   r	   r   r   r   r   Ś
_print_Add   s    zExprPrinter._print_Addc                 C   s   |   |jd|j dt|”S )Nś )r   r   Zrel_opr   r   r   r   r   Ś_print_Relational   s    zExprPrinter._print_Relationalc                 C   s   |   |jdtd ”S )Nz & Z
BitwiseAnd©r   r   r   r   r   r   r   Ś_print_BitwiseFn_bitwise_and   s    z(ExprPrinter._print_BitwiseFn_bitwise_andc                 C   s   |   |jdtd ”S )Nz | Z	BitwiseOrr   r   r   r   r   Ś_print_BitwiseFn_bitwise_or    s    z'ExprPrinter._print_BitwiseFn_bitwise_orc                 C   s   |   |jdtd d ”S ©Nś % ŚAtomē      ą?r   r   r   r   r   Ś
_print_Mod%   s    zExprPrinter._print_Modc                 C   s$   |   |jdtd d ”}d| dS )Nś / r   r   ś(ś)r   )r   r	   Śsr   r   r   Ś_print_FloatTrueDiv(   s    zExprPrinter._print_FloatTrueDivc                 C   s
   |   |”S )N)Ś_print_FloorDivr   r   r   r   Ś_print_CleanDiv,   s    zExprPrinter._print_CleanDivc                 C   s   |   |jd ”S ©Nr   ©Ś_printr   r   r   r   r   Ś_print_Identity/   s    zExprPrinter._print_Identityc                 C   s(   |j dkrttj|ddS t|S d S )Né5   é   )Zdps)Z_precŚstrŚsympyŚFloatr   r   r   r   Ś_print_Float2   s    
zExprPrinter._print_Floatc                 C   sV   |j \}}|t|ksJ |t|}|dks2J |dkrR|  |g| dtd ”S dS )Nr   r   ŚMulŚ1)r   Śintr   r   ©r   r	   ŚbaseŚexpr   r   r   Ś
_print_PowB   s    
zExprPrinter._print_Powc                 C   s   t dt|  d S )Nz#_print_ToFloat not implemented for ©ŚNotImplementedErrorŚtyper   r   r   r   Ś_print_ToFloatP   s    zExprPrinter._print_ToFloatc                 C   s   t dt|  d S )Nz$_print_Infinity not implemented for r8   r   r   r   r   Ś_print_InfinityS   s    zExprPrinter._print_Infinityc                 C   s   t dt|  d S )Nz,_print_NegativeInfinity not implemented for r8   r   r   r   r   Ś_print_NegativeInfinityV   s    ’z#ExprPrinter._print_NegativeInfinityc                 C   s   t dt|  d S )Nz$_print_FloorDiv not implemented for r8   r   r   r   r   r%   [   s    zExprPrinter._print_FloorDivc                 C   s   t dt|  d S )Nz%_print_PythonMod not implemented for r8   r   r   r   r   Ś_print_PythonMod^   s    zExprPrinter._print_PythonModc                 C   s   t dt|  d S )Nz&_print_IntTrueDiv not implemented for r8   r   r   r   r   Ś_print_IntTrueDiva   s    zExprPrinter._print_IntTrueDivc                 C   s   t dt|  d S )Nś(_print_PowByNatural not implemented for r8   r   r   r   r   Ś_print_PowByNaturald   s    ’zExprPrinter._print_PowByNaturalc                 C   s   t dt|  d S )Nz$_print_FloatPow not implemented for r8   r   r   r   r   Ś_print_FloatPowi   s    zExprPrinter._print_FloatPowc                 C   s   t dt|  d S )Nz&_print_TruncToInt not implemented for r8   r   r   r   r   Ś_print_TruncToIntl   s    zExprPrinter._print_TruncToIntc                 C   s   t dt|  d S )Nz&_print_RoundToInt not implemented for r8   r   r   r   r   Ś_print_RoundToInto   s    zExprPrinter._print_RoundToIntc                 C   s   t dt|  d S )Nz(_print_RoundDecimal not implemented for r8   r   r   r   r   Ś_print_RoundDecimalr   s    ’zExprPrinter._print_RoundDecimalc                 C   s   t dt|  d S )Nz(_print_TruncToFloat not implemented for r8   r   r   r   r   Ś_print_TruncToFloat|   s    ’zExprPrinter._print_TruncToFloat)N)Ś__name__Ś
__module__Ś__qualname__Zprintmethodr.   ŚExprr-   r   r   r   r   r   r   r   r$   r&   r*   r0   r7   r;   r<   r=   r%   r>   r?   rA   rB   rC   rD   rE   rF   r   r   r   r   r      s0   
r   c                   @   s^  e Zd ZejedddZejedddZejedddZejeddd	Z	ejedd
dZ
ejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedd d!Zejedd"d#Zejedd$d%Zejedd&d'Zejedd(d)Zejedd*d+Zejedd,d-Zejedd.d/Zejedd0d1Zejedd2d3Zejedd4d5Zejedd6d7Z ejedd8d9Z!ejedd:d;Z"ejedd<d=Z#ejedd>d?Z$ejedd@dAZ%ejeddBdCZ&dDS )EŚPythonPrinterr   c                 C   s*   t |jdksJ d|  |jd ” dS )Né   ztorch.sym_float(r   r"   ©Ślenr   r)   r   r   r   r   r;      s    zPythonPrinter._print_ToFloatc                 C   s   |   |jdt|”S )Nz and r   r   r   r   r   Ś
_print_And   s    zPythonPrinter._print_Andc                 C   s   |   |jdt|”S )Nz or r   r   r   r   r   Ś	_print_Or   s    zPythonPrinter._print_Orc                    sF    fdd|j D \}}}|dkr4d| d| d}d| d| dS )Nc                 3   s"   | ]}   |td  d ”V  qdS ©r   r   N©Śparenthesizer   ©Ś.0Śarg©r   r   r   Ś	<genexpr>   s   z7PythonPrinter._print_ModularIndexing.<locals>.<genexpr>r2   r!   ś // r"   r   ©r   ©r   r	   ŚxŚdivŚmodr   rW   r   Ś_print_ModularIndexing   s    
’z$PythonPrinter._print_ModularIndexingc                 C   s   dS )Nzmath.infr   r   r   r   r   r<   ¢   s    zPythonPrinter._print_Infinityc                 C   s   dS )Nz	-math.infr   r   r   r   r   r=   „   s    z%PythonPrinter._print_NegativeInfinityc                 C   s   |   |jdtd d ”S r   r   r   r   r   r   r>   ©   s    zPythonPrinter._print_PythonModc                    s&    fdd|j D \}}| d| S )Nc                 3   s"   | ]}   |td  d ”V  qdS rQ   rR   rT   rW   r   r   rX   ®   ó    z0PythonPrinter._print_FloorDiv.<locals>.<genexpr>rY   rZ   ©r   r	   r\   r]   r   rW   r   r%   ­   s    zPythonPrinter._print_FloorDivc                 C   s   |   |jdtd d ”S )Nr    r   r   r   r   r   r   r   r?   ³   s    zPythonPrinter._print_IntTrueDivc                 C   s   d|   |” dS )Nz
math.sqrt(r"   ©r)   r   r   r   r   Ś_helper_sqrt¶   s    zPythonPrinter._helper_sqrtc                 C   s   |   |jd ”S r'   )rc   r   r   r   r   r   Ś_print_OpaqueUnaryFn_sqrt¹   s    z'PythonPrinter._print_OpaqueUnaryFn_sqrtc                 C   s   |   |jdtd ”S ©Nz ** ŚPowr   r   r   r   r   rB   ¼   s    zPythonPrinter._print_FloatPowc                 C   s   |   |jdtd ”S re   r   r   r   r   r   rA   Ą   s    z!PythonPrinter._print_PowByNaturalc                 C   s*   t |jdksJ d|  |jd ” dS ©NrL   zmath.floor(r   r"   rM   r   r   r   r   Ś_print_floorĆ   s    zPythonPrinter._print_floorc                 C   s*   t |jdksJ d|  |jd ” dS rg   rM   r   r   r   r   Ś_print_FloorToIntĒ   s    zPythonPrinter._print_FloorToIntc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   zmath.trunc(r   r"   rM   r   r   r   r   rC   Ė   s    zPythonPrinter._print_TruncToIntc                 C   s*   t |jdksJ d|  |jd ” dS ©NrL   z
math.ceil(r   r"   rM   r   r   r   r   Ś_print_ceilingŠ   s    zPythonPrinter._print_ceilingc                 C   s*   t |jdksJ d|  |jd ” dS rj   rM   r   r   r   r   Ś_print_CeilToIntŌ   s    zPythonPrinter._print_CeilToIntc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   zabs(r   r"   rM   r   r   r   r   Ś
_print_AbsŲ   s    zPythonPrinter._print_Absc                 C   s.   t |jdksJ dd t| j|j” dS )Né   zmax(ś, r"   ©rN   r   ŚjoinŚmapr)   r   r   r   r   Ś
_print_Maxß   s    zPythonPrinter._print_Maxc                 C   s.   t |jdksJ dd t| j|j” dS )Nrn   zmin(ro   r"   rp   r   r   r   r   Ś
_print_Minć   s    zPythonPrinter._print_Minc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z	math.cos(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_cosē   s    z&PythonPrinter._print_OpaqueUnaryFn_cosc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
math.cosh(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_coshė   s    z'PythonPrinter._print_OpaqueUnaryFn_coshc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
math.acos(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_acosļ   s    z'PythonPrinter._print_OpaqueUnaryFn_acosc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z	math.sin(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_sinó   s    z&PythonPrinter._print_OpaqueUnaryFn_sinc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
math.sinh(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_sinh÷   s    z'PythonPrinter._print_OpaqueUnaryFn_sinhc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
math.asin(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_asinū   s    z'PythonPrinter._print_OpaqueUnaryFn_asinc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z	math.tan(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_tan’   s    z&PythonPrinter._print_OpaqueUnaryFn_tanc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
math.tanh(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_tanh  s    z'PythonPrinter._print_OpaqueUnaryFn_tanhc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
math.atan(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_atan  s    z'PythonPrinter._print_OpaqueUnaryFn_atanc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
math.log2(r   r"   rM   r   r   r   r   Ś_print_OpaqueUnaryFn_log2  s    z'PythonPrinter._print_OpaqueUnaryFn_log2c                 C   s*   t |jdksJ d|  |jd ” dS )NrL   śround(r   r"   rM   r   r   r   r   rD     s    zPythonPrinter._print_RoundToIntc                 C   sD   t |jdksJ |j\}}t|tjs,J d|  |” d| dS )Nrn   r   ro   r"   )rN   r   Ś
isinstancer.   ŚIntegerr)   )r   r	   ŚnumberŚndigitsr   r   r   rE     s    
z!PythonPrinter._print_RoundDecimalN)'rG   rH   rI   r.   rJ   r-   r;   rO   rP   r_   r<   r=   r>   r%   r?   rc   rd   rB   rA   rh   ri   rC   rk   rl   rm   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   rD   rE   r   r   r   r   rK      sB   rK   c                   @   s¦  e Zd ZejedddZejedddZejedddZejeddd	Z	ejedd
dZ
ejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedddZejedd d!Zejedd"d#Zejedd$d%Zejedd&d'Zejedd(d)Zejedd*d+Zejedd,d-Zejedd.d/Zejedd0d1Zejedd2d3Zejedd4d5Zejedd6d7Z ejedd8d9Z!ejedd:d;Z"ejedd<d=Z#ejedd>d?Z$ejedd@dAZ%ejeddBdCZ&ejeddDdEZ'ejeddFdGZ(ejeddHdIZ)ejeddJdKZ*dLS )MŚ
CppPrinterr   c                 C   sj   t jdv rdnd}t|}|tks*|tk r>t| dt n |tkr^|dksRJ d| dS | | S )N)ŚdarwinŚwin32ZLLŚLz too big to convert to r   z(-1z << 63))ŚsysŚplatformr3   ŚINDEX_TYPE_MAXŚINDEX_TYPE_MINŚOverflowErrorŚ
INDEX_TYPE)r   r	   ŚsuffixŚir   r   r   Ś_print_Integer  s    zCppPrinter._print_Integerc                    s.    fdd|j D \}}}| d| d| S )Nc                 3   s"   | ]}   |td  d ”V  qdS rQ   rR   rT   rW   r   r   rX   (  s   z*CppPrinter._print_Where.<locals>.<genexpr>z ? z : rZ   )r   r	   ŚcŚpŚqr   rW   r   Ś_print_Where'  s    
’zCppPrinter._print_Wherec              	   C   s|   |j \}}}|  |”}|dkrT|  |”}|jrBd| d| d}nd| d| d}|  |”}dt d| d	t d| d	S )
NrL   ś,c10::div_floor_integer(static_cast<int64_t>(ś), static_cast<int64_t>(ś))ś,c10::div_floor_floating(static_cast<double>(ś), static_cast<double>(z(static_cast<ś>(z) % static_cast<)r   ŚdoprintŚ
is_integerr   r[   r   r   r   r_   -  s    


z!CppPrinter._print_ModularIndexingc                 C   sH   |j \}}|  |”}|  |”}|jr6d| d| dS d| d| dS )Nr   r   r   r   r   )r   r   r   ra   r   r   r   r%   9  s    


zCppPrinter._print_FloorDivc                 C   sF   t |jdksJ d|  |jd ” d}|jrBdt d| dS |S ©NrL   zstd::floor(r   r"   śstatic_cast<r   ©rN   r   r)   r   r   ©r   r	   Śrr   r   r   rh   A  s    zCppPrinter._print_floorc                 C   sF   t |jdksJ d|  |jd ” d}|jrBdt d| dS |S r   r   r    r   r   r   ri   F  s    zCppPrinter._print_FloorToIntc                 C   s<   t |jdksJ d|  |jd ” d}dt d| dS )NrL   śstd::trunc(r   r"   r   r   )rN   r   r)   r   r    r   r   r   rC   K  s    zCppPrinter._print_TruncToIntc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   r¢   r   r"   rM   r   r   r   r   rF   P  s    zCppPrinter._print_TruncToFloatc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   śstatic_cast<double>(r   r"   rM   r   r   r   r   r;   T  s    zCppPrinter._print_ToFloatc                 C   s0   |j \}}|  |”}|  |”}d| d| dS )Nzc10::div_mod(ro   r"   )r   r   ra   r   r   r   r>   X  s    


zCppPrinter._print_PythonModc                 C   s(   |j \}}d|  |” d|  |” dS )Nr£   z) / static_cast<double>(r"   ©r   r)   )r   r	   ŚlhsŚrhsr   r   r   r?   ^  s    
zCppPrinter._print_IntTrueDivc                 C   s:   |j \}}|dkr$d|  |” dS tdt|  d S )Nrn   z(1 << (r   r@   )r   r)   r9   r:   r4   r   r   r   rA   e  s    
’zCppPrinter._print_PowByNaturalc                 C   s(   |j \}}d|  |” d|  |” dS )Nś	std::pow(ro   r"   r¤   r4   r   r   r   rB   n  s    
zCppPrinter._print_FloatPowc                 C   sō   |j \}}|dks|dkrD|  |”}|dkr8d| dS d| dS |jrŚt|}|dkrt|  |g| dtd ”}nJ|d	k r¢d
|  |gt| dtd ” d }n|d	krŗd|  |” }nd}|jrÖdt d| dS |S d| dt| dS d S )Nr   g      ąæś
std::sqrt(r"   z1.0/std::sqrt(r   r   r1   é’’’’z1.0/(z1.0/z1.0r   r   r§   ro   )	r   r)   r   r3   r   r   Śabsr   Śfloat)r   r	   r5   r6   r”   r   r   r   r7   r  s(    

 ’ž’zCppPrinter._print_Powc                 C   sD   |j dkr|j }n|j d|j  d}|jr@dt d| dS |S )NrL   z.0/z.0r   r   r"   )r   r   r   r   r    r   r   r   Ś_print_Rational  s    

zCppPrinter._print_Rationalc                 C   sF   t |jdksJ d|  |jd ” d}|jrBdt d| dS |S ©NrL   z
std::ceil(r   r"   r   r   r   r    r   r   r   rk     s    zCppPrinter._print_ceilingc                 C   sF   t |jdksJ d|  |jd ” d}|jrBdt d| dS |S r­   r   r    r   r   r   rl     s    zCppPrinter._print_CeilToIntc              	      sn    fdd|j D }t|dkrFdt d|d  dt d|d  d		S d
d |” d }dt d| dS d S )Nc                    s   g | ]}   |”qS r   rb   ©rU   ŚarW   r   r   Ś
<listcomp>   r`   z)CppPrinter._print_Min.<locals>.<listcomp>rn   zstd::min(static_cast<r   r   ś), static_cast<rL   r   Ś{ro   Ś}z	std::min<r"   ©r   rN   r   rq   ©r   r	   r   Śilr   rW   r   rt     s
    &zCppPrinter._print_Minc              	      sn    fdd|j D }t|dkrFdt d|d  dt d|d  d		S d
d |” d }dt d| dS d S )Nc                    s   g | ]}   |”qS r   rb   r®   rW   r   r   r°   ©  r`   z)CppPrinter._print_Max.<locals>.<listcomp>rn   zstd::max(static_cast<r   r   r±   rL   r   r²   ro   r³   z	std::max<r"   r“   rµ   r   rW   r   rs   Ø  s
    &zCppPrinter._print_Maxc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z	std::abs(r   r"   rM   r   r   r   r   rm   ±  s    zCppPrinter._print_Absc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z	std::cos(r   r"   rM   r   r   r   r   ru   µ  s    z#CppPrinter._print_OpaqueUnaryFn_cosc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
std::cosh(r   r"   rM   r   r   r   r   rv   ¹  s    z$CppPrinter._print_OpaqueUnaryFn_coshc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
std::acos(r   r"   rM   r   r   r   r   rw   ½  s    z$CppPrinter._print_OpaqueUnaryFn_acosc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z	std::sin(r   r"   rM   r   r   r   r   rx   Į  s    z#CppPrinter._print_OpaqueUnaryFn_sinc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
std::sinh(r   r"   rM   r   r   r   r   ry   Å  s    z$CppPrinter._print_OpaqueUnaryFn_sinhc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
std::asin(r   r"   rM   r   r   r   r   rz   É  s    z$CppPrinter._print_OpaqueUnaryFn_asinc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z	std::tan(r   r"   rM   r   r   r   r   r{   Ķ  s    z#CppPrinter._print_OpaqueUnaryFn_tanc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
std::tanh(r   r"   rM   r   r   r   r   r|   Ń  s    z$CppPrinter._print_OpaqueUnaryFn_tanhc                 C   s*   t |jdksJ d|  |jd ” dS )NrL   z
std::atan(r   r"   rM   r   r   r   r   r}   Õ  s    z$CppPrinter._print_OpaqueUnaryFn_atanc                 C   s   d|   |jd ” dS )NrØ   r   r"   r(   r   r   r   r   rd   Ł  s    z$CppPrinter._print_OpaqueUnaryFn_sqrtc                 C   s   d|   |jd ” dS )Nz
std::log2(r   r"   r(   r   r   r   r   r~   Ü  s    z$CppPrinter._print_OpaqueUnaryFn_log2c                 C   s*   t |jdksJ d|  |jd ” dS )NrL   zstd::lrint(r   r"   rM   r   r   r   r   rD   ß  s    zCppPrinter._print_RoundToIntc                 C   sh   t |jdksJ |j\}}|jr>|dk s.J td| d|  |td ”}d| d| d|  d	S )
Nrn   r   zOFor integer inputs, only non-negative ndigits are currently supported, but got Ś.r1   z%static_cast<double>(std::nearbyint(1ez * z) * 1er"   )rN   r   r   Ś
ValueErrorrS   r   )r   r	   r   r   Z
number_strr   r   r   rE   ä  s    

’zCppPrinter._print_RoundDecimalc                 C   s   dS )NŚtruer   r   r   r   r   Ś_print_BooleanTrueš  s    zCppPrinter._print_BooleanTruec                 C   s   dS )NŚfalser   r   r   r   r   Ś_print_BooleanFalseó  s    zCppPrinter._print_BooleanFalsec                 C   s   dS )Nz'std::numeric_limits<double>::infinity()r   r   r   r   r   r<   ö  s    zCppPrinter._print_Infinityc                 C   s   d|   |” S )Nś-)r<   r   r   r   r   r=   ł  s    z"CppPrinter._print_NegativeInfinityN)+rG   rH   rI   r.   rJ   r-   r   r   r_   r%   rh   ri   rC   rF   r;   r>   r?   rA   rB   r7   r¬   rk   rl   rt   rs   rm   ru   rv   rw   rx   ry   rz   r{   r|   r}   rd   r~   rD   rE   rŗ   r¼   r<   r=   r   r   r   r   r     sJ   			r   )r   Śtypingr   r.   Zsympy.printing.precedencer   r   Zsympy.printing.strr   r   r   r   r   rK   r   r   r   r   r   Ś<module>   s   r 