o
    ,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)
StrPrinterint64_tl             c                   @   s  e Zd ZdZdejdefddZd4dejdee defdd	Z	dejdefd
dZ
dejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefd d!Zdejdefd"d#Zdejdefd$d%Zdejdefd&d'Zdejdefd(d)Zdejdefd*d+Zdejdefd,d-Zdejdefd.d/Zdejdefd0d1Zdejdefd2d3ZdS )5ExprPrinter_torch_sympystrexprreturnc                 C      |  |jdt|S )N*	stringifyargsr   selfr
    r   V/var/www/html/scripts/venv/lib/python3.10/site-packages/torch/utils/_sympy/printers.py
_print_Mul      zExprPrinter._print_MulNorderc                 C   r   )Nz + r   )r   r
   r   r   r   r   
_print_Add   r   zExprPrinter._print_Addc                 C   s   |  |jd|j dt|S )N )r   r   rel_opr   r   r   r   r   _print_Relational   s   zExprPrinter._print_Relationalc                 C      |  |jdtd S )Nz & 
BitwiseAndr   r   r   r   r   r   r   _print_BitwiseFn_bitwise_and   r   z(ExprPrinter._print_BitwiseFn_bitwise_andc                 C   r   )Nz | 	BitwiseOrr   r   r   r   r   _print_BitwiseFn_bitwise_or    r   z'ExprPrinter._print_BitwiseFn_bitwise_orc                 C      |  |jdtd d S N % Atom      ?r   r   r   r   r   
_print_Mod%      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      |  |jd S Nr   _printr   r   r   r   r   _print_Identity/      zExprPrinter._print_Identityc                 C   s$   |j dkrttj|ddS t|S )N5      )dps)_precstrsympyFloatr   r   r   r   _print_Float2   s   
zExprPrinter._print_Floatc                 C   sV   |j \}}|t|ksJ |t|}|dksJ |dkr)| |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      t dt|  )Nz#_print_ToFloat not implemented for NotImplementedErrortyper   r   r   r   _print_ToFloatP      zExprPrinter._print_ToFloatc                 C   rF   )Nz$_print_Infinity not implemented for rG   r   r   r   r   _print_InfinityS   rK   zExprPrinter._print_Infinityc                 C   rF   )Nz,_print_NegativeInfinity not implemented for rG   r   r   r   r   _print_NegativeInfinityV      z#ExprPrinter._print_NegativeInfinityc                 C   rF   )Nz$_print_FloorDiv not implemented for rG   r   r   r   r   r/   [   rK   zExprPrinter._print_FloorDivc                 C   rF   )Nz%_print_PythonMod not implemented for rG   r   r   r   r   _print_PythonMod^   rK   zExprPrinter._print_PythonModc                 C   rF   )Nz&_print_IntTrueDiv not implemented for rG   r   r   r   r   _print_IntTrueDiva   rK   zExprPrinter._print_IntTrueDivc                 C   rF   )N(_print_PowByNatural not implemented for rG   r   r   r   r   _print_PowByNaturald   rN   zExprPrinter._print_PowByNaturalc                 C   rF   )Nz$_print_FloatPow not implemented for rG   r   r   r   r   _print_FloatPowi   rK   zExprPrinter._print_FloatPowc                 C   rF   )Nz&_print_TruncToInt not implemented for rG   r   r   r   r   _print_TruncToIntl   rK   zExprPrinter._print_TruncToIntc                 C   rF   )Nz&_print_RoundToInt not implemented for rG   r   r   r   r   _print_RoundToInto   rK   zExprPrinter._print_RoundToIntc                 C   rF   )Nz(_print_RoundDecimal not implemented for rG   r   r   r   r   _print_RoundDecimalr   rN   zExprPrinter._print_RoundDecimalc                 C   rF   )Nz(_print_TruncToFloat not implemented for rG   r   r   r   r   _print_TruncToFloat|   rN   zExprPrinter._print_TruncToFloatr.   )__name__
__module____qualname__printmethodr<   Exprr;   r   r   r   r   r   r!   r'   r-   r0   r5   r>   rE   rJ   rL   rM   r/   rO   rP   rR   rS   rT   rU   rV   rW   r   r   r   r   r      s2    
r   c                   @   s  e Zd ZdejdefddZdejdefddZdejdefddZdejdefd	d
Z	dejdefddZ
dejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefdd Zdejdefd!d"Zdejdefd#d$Zdejdefd%d&Zdejdefd'd(Zdejdefd)d*Zdejdefd+d,Zdejdefd-d.Zdejdefd/d0Zdejdefd1d2Zdejdefd3d4Zdejdefd5d6Zdejdefd7d8Z dejdefd9d:Z!dejdefd;d<Z"dejdefd=d>Z#dejdefd?d@Z$dejdefdAdBZ%dejdefdCdDZ&dES )FPythonPrinterr
   r   c                 C   *   t |jdks	J d| |jd  dS )N   ztorch.sym_float(r   r+   lenr   r4   r   r   r   r   rJ      s   zPythonPrinter._print_ToFloatc                 C   r   )Nz and r   r   r   r   r   
_print_And   r   zPythonPrinter._print_Andc                 C   r   )Nz or r   r   r   r   r   	_print_Or   r   zPythonPrinter._print_Orc                    sF    fdd|j D \}}}|dkrd| d| d}d| d| dS )Nc                 3   $    | ]}  |td  d V  qdS r%   r&   Nparenthesizer   .0argr   r   r   	<genexpr>       
z7PythonPrinter._print_ModularIndexing.<locals>.<genexpr>r@   r*    // r+   r$   r   r   r
   xdivmodr   rk   r   _print_ModularIndexing   s   
z$PythonPrinter._print_ModularIndexingc                 C      dS )Nzmath.infr   r   r   r   r   rL         zPythonPrinter._print_Infinityc                 C   ru   )Nz	-math.infr   r   r   r   r   rM      rv   z%PythonPrinter._print_NegativeInfinityc                 C   r"   r#   r   r   r   r   r   rO      r(   zPythonPrinter._print_PythonModc                    s&    fdd|j D \}}| d| S )Nc                 3   rd   re   rf   rh   rk   r   r   rl      s   " z0PythonPrinter._print_FloorDiv.<locals>.<genexpr>rn   ro   r   r
   rq   rr   r   rk   r   r/      s   zPythonPrinter._print_FloorDivc                 C   r"   )Nr)   r%   r&   r   r   r   r   r   rP      r(   zPythonPrinter._print_IntTrueDivc                 C   s   d|  | dS )Nz
math.sqrt(r+   r4   r   r   r   r   _helper_sqrt   rK   zPythonPrinter._helper_sqrtc                 C   r1   r2   )ry   r   r   r   r   r   _print_OpaqueUnaryFn_sqrt   r6   z'PythonPrinter._print_OpaqueUnaryFn_sqrtc                 C   r   Nz ** Powr   r   r   r   r   rS      r   zPythonPrinter._print_FloatPowc                 C   r   r{   r   r   r   r   r   rR      r   z!PythonPrinter._print_PowByNaturalc                 C   r^   Nr_   zmath.floor(r   r+   r`   r   r   r   r   _print_floor      zPythonPrinter._print_floorc                 C   r^   r}   r`   r   r   r   r   _print_FloorToInt   r   zPythonPrinter._print_FloorToIntc                 C   r^   )Nr_   zmath.trunc(r   r+   r`   r   r   r   r   rT         zPythonPrinter._print_TruncToIntc                 C   r^   Nr_   z
math.ceil(r   r+   r`   r   r   r   r   _print_ceiling   r   zPythonPrinter._print_ceilingc                 C   r^   r   r`   r   r   r   r   _print_CeilToInt   r   zPythonPrinter._print_CeilToIntc                 C   r^   )Nr_   zabs(r   r+   r`   r   r   r   r   
_print_Abs   r   zPythonPrinter._print_Absc                 C   .   t |jdks	J ddt| j|j dS )N   zmax(, r+   ra   r   joinmapr4   r   r   r   r   
_print_Max      zPythonPrinter._print_Maxc                 C   r   )Nr   zmin(r   r+   r   r   r   r   r   
_print_Min   r   zPythonPrinter._print_Minc                 C   r^   )Nr_   z	math.cos(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_cos   r   z&PythonPrinter._print_OpaqueUnaryFn_cosc                 C   r^   )Nr_   z
math.cosh(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_cosh   r   z'PythonPrinter._print_OpaqueUnaryFn_coshc                 C   r^   )Nr_   z
math.acos(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_acos   r   z'PythonPrinter._print_OpaqueUnaryFn_acosc                 C   r^   )Nr_   z	math.sin(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_sin   r   z&PythonPrinter._print_OpaqueUnaryFn_sinc                 C   r^   )Nr_   z
math.sinh(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_sinh   r   z'PythonPrinter._print_OpaqueUnaryFn_sinhc                 C   r^   )Nr_   z
math.asin(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_asin   r   z'PythonPrinter._print_OpaqueUnaryFn_asinc                 C   r^   )Nr_   z	math.tan(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_tan   r   z&PythonPrinter._print_OpaqueUnaryFn_tanc                 C   r^   )Nr_   z
math.tanh(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_tanh  r   z'PythonPrinter._print_OpaqueUnaryFn_tanhc                 C   r^   )Nr_   z
math.atan(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_atan  r   z'PythonPrinter._print_OpaqueUnaryFn_atanc                 C   r^   )Nr_   z
math.log2(r   r+   r`   r   r   r   r   _print_OpaqueUnaryFn_log2  r   z'PythonPrinter._print_OpaqueUnaryFn_log2c                 C   r^   )Nr_   round(r   r+   r`   r   r   r   r   rU     r   zPythonPrinter._print_RoundToIntc                 C   sD   t |jdks	J |j\}}t|tjsJ d| | d| dS )Nr   r   r   r+   )ra   r   
isinstancer<   Integerr4   )r   r
   numberndigitsr   r   r   rV     s   
z!PythonPrinter._print_RoundDecimalN)'rX   rY   rZ   r<   r\   r;   rJ   rb   rc   rt   rL   rM   rO   r/   rP   ry   rz   rS   rR   r~   r   rT   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   rV   r   r   r   r   r]      sD    r]   c                   @   s  e Zd ZdejdefddZdejdefddZdejdefddZdejdefd	d
Z	dejdefddZ
dejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefddZdejdefdd Zdejdefd!d"Zdejdefd#d$Zdejdefd%d&Zdejdefd'd(Zdejdefd)d*Zdejdefd+d,Zdejdefd-d.Zdejdefd/d0Zdejdefd1d2Zdejdefd3d4Zdejdefd5d6Zdejdefd7d8Z dejdefd9d:Z!dejdefd;d<Z"dejdefd=d>Z#dejdefd?d@Z$dejdefdAdBZ%dejdefdCdDZ&dejdefdEdFZ'dejdefdGdHZ(dejdefdIdJZ)dejdefdKdLZ*dMS )N
CppPrinterr
   r   c                 C   sh   t jdv rdnd}t|}|tks|tk rt| dt |tkr.|dks(J d| dS | | S )N)darwinwin32LLLz too big to convert to r   z(-1z << 63))sysplatformrA   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   rd   re   rf   rh   rk   r   r   rl   (  rm   z*CppPrinter._print_Where.<locals>.<genexpr>z ? z : ro   )r   r
   cpqr   rk   r   _print_Where'  s   
zCppPrinter._print_Wherec              	   C   s|   |j \}}}| |}|dkr*| |}|jr!d| d| d}n	d| d| d}| |}dt d| d	t d| d	S )
Nr_   ,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   rp   r   r   r   rt   -  s   


z!CppPrinter._print_ModularIndexingc                 C   sH   |j \}}| |}| |}|jrd| d| dS d| d| dS )Nr   r   r   r   r   )r   r   r   rw   r   r   r   r/   9  s   


zCppPrinter._print_FloorDivc                 C   F   t |jdks	J d| |jd  d}|jr!dt d| dS |S Nr_   zstd::floor(r   r+   static_cast<r   ra   r   r4   r   r   r   r
   rr   r   r   r~   A     zCppPrinter._print_floorc                 C   r   r   r   r   r   r   r   r   F  r   zCppPrinter._print_FloorToIntc                 C   s<   t |jdks	J d| |jd  d}dt d| dS )Nr_   std::trunc(r   r+   r   r   )ra   r   r4   r   r   r   r   r   rT   K  s   zCppPrinter._print_TruncToIntc                 C   r^   )Nr_   r   r   r+   r`   r   r   r   r   rW   P  r   zCppPrinter._print_TruncToFloatc                 C   r^   )Nr_   static_cast<double>(r   r+   r`   r   r   r   r   rJ   T  r   zCppPrinter._print_ToFloatc                 C   s0   |j \}}| |}| |}d| d| dS )Nzc10::div_mod(r   r+   )r   r   rw   r   r   r   rO   X  s   


zCppPrinter._print_PythonModc                 C   (   |j \}}d| | d| | dS )Nr   z) / static_cast<double>(r+   r   r4   )r   r
   lhsrhsr   r   r   rP   ^  s   
zCppPrinter._print_IntTrueDivc                 C   s6   |j \}}|dkrd| | dS tdt|  )Nr   z(1 << (r   rQ   )r   r4   rH   rI   rB   r   r   r   rR   e  s   
zCppPrinter._print_PowByNaturalc                 C   r   )N	std::pow(r   r+   r   rB   r   r   r   rS   n  s   
zCppPrinter._print_FloatPowc                 C   s   |j \}}|dks|dkr"| |}|dkrd| dS d| dS |jrmt|}|dkr:| |g| dtd }n%|d	k rQd
| |gt| dtd  d }n|d	kr]d| | }nd}|jrkdt d| dS |S d| dt| dS )Nr&   g      
std::sqrt(r+   z1.0/std::sqrt(r   r   r?   z1.0/(z1.0/z1.0r   r   r   r   )	r   r4   r   rA   r   r   absr   float)r   r
   rC   rD   r   r   r   r   rE   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 )Nr_   z.0/z.0r   r   r+   )r   r   r   r   r   r   r   r   _print_Rational  s   

zCppPrinter._print_Rationalc                 C   r   Nr_   z
std::ceil(r   r+   r   r   r   r   r   r   r   r     r   zCppPrinter._print_ceilingc                 C   r   r   r   r   r   r   r   r     r   zCppPrinter._print_CeilToIntc              	      j    fdd|j D }t|dkr#dt d|d  dt d|d  d		S d
d| d }dt d| dS )Nc                       g | ]}  |qS r   rx   ri   ark   r   r   
<listcomp>      z)CppPrinter._print_Min.<locals>.<listcomp>r   zstd::min(static_cast<r   r   ), static_cast<r_   r   {r   }z	std::min<r+   r   ra   r   r   r   r
   r   ilr   rk   r   r     
   &zCppPrinter._print_Minc              	      r   )Nc                    r   r   rx   r   rk   r   r   r     r   z)CppPrinter._print_Max.<locals>.<listcomp>r   zstd::max(static_cast<r   r   r   r_   r   r   r   r   z	std::max<r+   r   r   r   rk   r   r     r   zCppPrinter._print_Maxc                 C   r^   )Nr_   z	std::abs(r   r+   r`   r   r   r   r   r     r   zCppPrinter._print_Absc                 C   r^   )Nr_   z	std::cos(r   r+   r`   r   r   r   r   r     r   z#CppPrinter._print_OpaqueUnaryFn_cosc                 C   r^   )Nr_   z
std::cosh(r   r+   r`   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_coshc                 C   r^   )Nr_   z
std::acos(r   r+   r`   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_acosc                 C   r^   )Nr_   z	std::sin(r   r+   r`   r   r   r   r   r     r   z#CppPrinter._print_OpaqueUnaryFn_sinc                 C   r^   )Nr_   z
std::sinh(r   r+   r`   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_sinhc                 C   r^   )Nr_   z
std::asin(r   r+   r`   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_asinc                 C   r^   )Nr_   z	std::tan(r   r+   r`   r   r   r   r   r     r   z#CppPrinter._print_OpaqueUnaryFn_tanc                 C   r^   )Nr_   z
std::tanh(r   r+   r`   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_tanhc                 C   r^   )Nr_   z
std::atan(r   r+   r`   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_atanc                 C      d|  |jd  dS )Nr   r   r+   r3   r   r   r   r   rz     r(   z$CppPrinter._print_OpaqueUnaryFn_sqrtc                 C   r   )Nz
std::log2(r   r+   r3   r   r   r   r   r     r(   z$CppPrinter._print_OpaqueUnaryFn_log2c                 C   r^   )Nr_   zstd::lrint(r   r+   r`   r   r   r   r   rU     r   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 )
Nr   r   zOFor integer inputs, only non-negative ndigits are currently supported, but got .r?   z%static_cast<double>(std::nearbyint(1ez * z) * 1er+   )ra   r   r   
ValueErrorrg   r   )r   r
   r   r   
number_strr   r   r   rV     s   

zCppPrinter._print_RoundDecimalc                 C   ru   )Ntruer   r   r   r   r   _print_BooleanTrue  rv   zCppPrinter._print_BooleanTruec                 C   ru   )Nfalser   r   r   r   r   _print_BooleanFalse  rv   zCppPrinter._print_BooleanFalsec                 C   ru   )Nz'std::numeric_limits<double>::infinity()r   r   r   r   r   rL     rv   zCppPrinter._print_Infinityc                 C   s   d|  | S )N-)rL   r   r   r   r   rM     r6   z"CppPrinter._print_NegativeInfinityN)+rX   rY   rZ   r<   r\   r;   r   r   rt   r/   r~   r   rT   rW   rJ   rO   rP   rR   rS   rE   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rz   r   rU   rV   r   r   rL   rM   r   r   r   r   r     sL    			r   )r   typingr   r<   sympy.printing.precedencer   r   sympy.printing.strr   r   r   r   r   r]   r   r   r   r   r   <module>   s    r 