o
    0hAK                     @   sp   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	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_tc                   @   s  e Zd ZdZdejdefddZd2d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S )3ExprPrinter_torch_sympystrexprreturnc                 C      |  |jdt|S )N*	stringifyargsr   selfr	    r   O/var/www/vscode/kcb/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   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_Pow7   s   
zExprPrinter._print_Powc                 C      t dt|  )Nz#_print_ToFloat not implemented for NotImplementedErrortyper   r   r   r   _print_ToFloatE      zExprPrinter._print_ToFloatc                 C   r=   )Nz$_print_Infinity not implemented for r>   r   r   r   r   _print_InfinityH   rB   zExprPrinter._print_Infinityc                 C   r=   )Nz,_print_NegativeInfinity not implemented for r>   r   r   r   r   _print_NegativeInfinityK      z#ExprPrinter._print_NegativeInfinityc                 C   r=   )Nz$_print_FloorDiv not implemented for r>   r   r   r   r   r.   P   rB   zExprPrinter._print_FloorDivc                 C   r=   )Nz%_print_PythonMod not implemented for r>   r   r   r   r   _print_PythonModS   rB   zExprPrinter._print_PythonModc                 C   r=   )Nz&_print_IntTrueDiv not implemented for r>   r   r   r   r   _print_IntTrueDivV   rB   zExprPrinter._print_IntTrueDivc                 C   r=   Nz(_print_PowByNatural not implemented for r>   r   r   r   r   _print_PowByNaturalY   rE   zExprPrinter._print_PowByNaturalc                 C   r=   )Nz$_print_FloatPow not implemented for r>   r   r   r   r   _print_FloatPow^   rB   zExprPrinter._print_FloatPowc                 C   r=   )Nz&_print_TruncToInt not implemented for r>   r   r   r   r   _print_TruncToInta   rB   zExprPrinter._print_TruncToIntc                 C   r=   )Nz&_print_RoundToInt not implemented for r>   r   r   r   r   _print_RoundToIntd   rB   zExprPrinter._print_RoundToIntc                 C   r=   )Nz(_print_RoundDecimal not implemented for r>   r   r   r   r   _print_RoundDecimalg   rE   zExprPrinter._print_RoundDecimalc                 C   r=   )Nz(_print_TruncToFloat not implemented for r>   r   r   r   r   _print_TruncToFloatq   rE   zExprPrinter._print_TruncToFloatr-   )__name__
__module____qualname__printmethodsympyExprstrr   r   r   r   r   r    r&   r,   r/   r4   r<   rA   rC   rD   r.   rF   rG   rI   rJ   rK   rL   rM   rN   r   r   r   r   r      s0    

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%dCS )DPythonPrinterr	   r
   c                 C   *   t |jdks	J d| |jd  dS )N   ztorch.sym_float(r   r*   lenr   r3   r   r   r   r   rA   x   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>r7   r)    // r*   r#   r   r   r	   xdivmodr   rd   r   _print_ModularIndexing   s   
z$PythonPrinter._print_ModularIndexingc                 C      dS )Nzmath.infr   r   r   r   r   rC         zPythonPrinter._print_Infinityc                 C   rn   )Nz	-math.infr   r   r   r   r   rD      ro   z%PythonPrinter._print_NegativeInfinityc                 C   r!   r"   r   r   r   r   r   rF      r'   zPythonPrinter._print_PythonModc                    s&    fdd|j D \}}| d| S )Nc                 3   r]   r^   r_   ra   rd   r   r   re      s   " z0PythonPrinter._print_FloorDiv.<locals>.<genexpr>rg   rh   r   r	   rj   rk   r   rd   r   r.      s   zPythonPrinter._print_FloorDivc                 C   r!   )Nr(   r$   r%   r   r   r   r   r   rG      r'   zPythonPrinter._print_IntTrueDivc                 C   s   d|  | dS )Nz
math.sqrt(r*   r3   r   r   r   r   _helper_sqrt   rB   zPythonPrinter._helper_sqrtc                 C   r0   r1   )rr   r   r   r   r   r   _print_OpaqueUnaryFn_sqrt   r5   z'PythonPrinter._print_OpaqueUnaryFn_sqrtc                 C   r   Nz ** Powr   r   r   r   r   rJ      r   zPythonPrinter._print_FloatPowc                 C   r   rt   r   r   r   r   r   rI      r   z!PythonPrinter._print_PowByNaturalc                 C   rW   NrX   zmath.floor(r   r*   rY   r   r   r   r   _print_floor      zPythonPrinter._print_floorc                 C   rW   rv   rY   r   r   r   r   _print_FloorToInt   rx   zPythonPrinter._print_FloorToIntc                 C   rW   )NrX   zmath.trunc(r   r*   rY   r   r   r   r   rK         zPythonPrinter._print_TruncToIntc                 C   rW   NrX   z
math.ceil(r   r*   rY   r   r   r   r   _print_ceiling   rx   zPythonPrinter._print_ceilingc                 C   rW   r{   rY   r   r   r   r   _print_CeilToInt   rx   zPythonPrinter._print_CeilToIntc                 C   rW   )NrX   zabs(r   r*   rY   r   r   r   r   
_print_Abs   rx   zPythonPrinter._print_Absc                 C   .   t |jdks	J ddt| j|j dS )N   zmax(, r*   rZ   r   joinmapr3   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   rW   )NrX   z	math.cos(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_cos   rx   z&PythonPrinter._print_OpaqueUnaryFn_cosc                 C   rW   )NrX   z
math.cosh(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_cosh   rx   z'PythonPrinter._print_OpaqueUnaryFn_coshc                 C   rW   )NrX   z
math.acos(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_acos   rx   z'PythonPrinter._print_OpaqueUnaryFn_acosc                 C   rW   )NrX   z	math.sin(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_sin   rx   z&PythonPrinter._print_OpaqueUnaryFn_sinc                 C   rW   )NrX   z
math.sinh(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_sinh   rx   z'PythonPrinter._print_OpaqueUnaryFn_sinhc                 C   rW   )NrX   z
math.asin(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_asin   rx   z'PythonPrinter._print_OpaqueUnaryFn_asinc                 C   rW   )NrX   z	math.tan(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_tan   rx   z&PythonPrinter._print_OpaqueUnaryFn_tanc                 C   rW   )NrX   z
math.tanh(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_tanh   rx   z'PythonPrinter._print_OpaqueUnaryFn_tanhc                 C   rW   )NrX   z
math.atan(r   r*   rY   r   r   r   r   _print_OpaqueUnaryFn_atan   rx   z'PythonPrinter._print_OpaqueUnaryFn_atanc                 C   rW   )NrX   round(r   r*   rY   r   r   r   r   rL      rx   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*   )rZ   r   
isinstancerS   Integerr3   )r   r	   numberndigitsr   r   r   rM     s   
z!PythonPrinter._print_RoundDecimalN)&rO   rP   rQ   rS   rT   rU   rA   r[   r\   rm   rC   rD   rF   r.   rG   rr   rs   rJ   rI   rw   ry   rK   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   rL   rM   r   r   r   r   rV   w   sB    rV   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)dKS )L
CppPrinterr	   r
   c                 C   s&   t jdv rt| dS t| dS )N)darwinwin32LLL)sysplatformr8   r   r   r   r   _print_Integer  s   zCppPrinter._print_Integerc                    s.    fdd|j D \}}}| d| d| S )Nc                 3   r]   r^   r_   ra   rd   r   r   re     rf   z*CppPrinter._print_Where.<locals>.<genexpr>z ? z : rh   )r   r	   cpqr   rd   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 )
NrX   ,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_integer
INDEX_TYPEri   r   r   r   rm     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   rp   r   r   r   r.   #  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 NrX   zstd::floor(r   r*   static_cast<r   rZ   r   r3   r   r   r   r	   rr   r   r   rw   +     zCppPrinter._print_floorc                 C   r   r   r   r   r   r   r   ry   0  r   zCppPrinter._print_FloorToIntc                 C   s<   t |jdks	J d| |jd  d}dt d| dS )NrX   std::trunc(r   r*   r   r   )rZ   r   r3   r   r   r   r   r   rK   5  s   zCppPrinter._print_TruncToIntc                 C   rW   )NrX   r   r   r*   rY   r   r   r   r   rN   :  rx   zCppPrinter._print_TruncToFloatc                 C   rW   )NrX   static_cast<double>(r   r*   rY   r   r   r   r   rA   >  rx   zCppPrinter._print_ToFloatc                 C   s0   |j \}}| |}| |}d| d| dS )Nzc10::div_mod(r   r*   )r   r   rp   r   r   r   rF   B  s   


zCppPrinter._print_PythonModc                 C   (   |j \}}d| | d| | dS )Nr   z) / static_cast<double>(r*   r   r3   )r   r	   lhsrhsr   r   r   rG   H  s   
zCppPrinter._print_IntTrueDivc                 C   r=   rH   r>   r   r   r   r   rI   O  rE   zCppPrinter._print_PowByNaturalc                 C   r   )N	std::pow(r   r*   r   r9   r   r   r   rJ   T  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   r6   z1.0/(z1.0/z1.0r   r   r   r   )	r   r3   r   r8   r   r   absr   float)r   r	   r:   r;   r   r   r   r   r<   X  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 )NrX   z.0/z.0r   r   r*   )r   r   r   r   r   r   r   r   _print_Rationals  s   

zCppPrinter._print_Rationalc                 C   r   NrX   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              	      d    fdd|j D }t|dkr#dt d|d  dt d|d  d		S d
d| d }d| dS )Nc                       g | ]}  |qS r   rq   rb   ard   r   r   
<listcomp>      z)CppPrinter._print_Min.<locals>.<listcomp>r   zstd::min(static_cast<r   r   ), static_cast<rX   r   {r   }z	std::min(r*   r   rZ   r   r   r   r	   r   ilr   rd   r   r     
   &zCppPrinter._print_Minc              	      r   )Nc                    r   r   rq   r   rd   r   r   r     r   z)CppPrinter._print_Max.<locals>.<listcomp>r   zstd::max(static_cast<r   r   r   rX   r   r   r   r   z	std::max(r*   r   r   r   rd   r   r     r   zCppPrinter._print_Maxc                 C   rW   )NrX   z	std::abs(r   r*   rY   r   r   r   r   r~     rx   zCppPrinter._print_Absc                 C   rW   )NrX   z	std::cos(r   r*   rY   r   r   r   r   r     rx   z#CppPrinter._print_OpaqueUnaryFn_cosc                 C   rW   )NrX   z
std::cosh(r   r*   rY   r   r   r   r   r     rx   z$CppPrinter._print_OpaqueUnaryFn_coshc                 C   rW   )NrX   z
std::acos(r   r*   rY   r   r   r   r   r     rx   z$CppPrinter._print_OpaqueUnaryFn_acosc                 C   rW   )NrX   z	std::sin(r   r*   rY   r   r   r   r   r     rx   z#CppPrinter._print_OpaqueUnaryFn_sinc                 C   rW   )NrX   z
std::sinh(r   r*   rY   r   r   r   r   r     rx   z$CppPrinter._print_OpaqueUnaryFn_sinhc                 C   rW   )NrX   z
std::asin(r   r*   rY   r   r   r   r   r     rx   z$CppPrinter._print_OpaqueUnaryFn_asinc                 C   rW   )NrX   z	std::tan(r   r*   rY   r   r   r   r   r     rx   z#CppPrinter._print_OpaqueUnaryFn_tanc                 C   rW   )NrX   z
std::tanh(r   r*   rY   r   r   r   r   r     rx   z$CppPrinter._print_OpaqueUnaryFn_tanhc                 C   rW   )NrX   z
std::atan(r   r*   rY   r   r   r   r   r     rx   z$CppPrinter._print_OpaqueUnaryFn_atanc                 C   s   d|  |jd  dS )Nr   r   r*   r2   r   r   r   r   rs     r'   z$CppPrinter._print_OpaqueUnaryFn_sqrtc                 C   rW   )NrX   zstd::lrint(r   r*   rY   r   r   r   r   rL     rz   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 .r6   z%static_cast<double>(std::nearbyint(1ez * z) * 1er*   )rZ   r   r   
ValueErrorr`   r   )r   r	   r   r   
number_strr   r   r   rM     s   

zCppPrinter._print_RoundDecimalc                 C   rn   )Ntruer   r   r   r   r   _print_BooleanTrue  ro   zCppPrinter._print_BooleanTruec                 C   rn   )Nfalser   r   r   r   r   _print_BooleanFalse  ro   zCppPrinter._print_BooleanFalsec                 C   rn   )Nz'std::numeric_limits<double>::infinity()r   r   r   r   r   rC     ro   zCppPrinter._print_Infinityc                 C   s   d|  | S )N-)rC   r   r   r   r   rD     r5   z"CppPrinter._print_NegativeInfinityN)*rO   rP   rQ   rS   rT   rU   r   r   rm   r.   rw   ry   rK   rN   rA   rF   rG   rI   rJ   r<   r   r|   r}   r   r   r~   r   r   r   r   r   r   r   r   r   rs   rL   rM   r   r   rC   rD   r   r   r   r   r     sJ    		r   )r   typingr   rS   sympy.printing.precedencer   r   sympy.printing.strr   r   r   rV   r   r   r   r   r   <module>   s    i 