o
    iP                     @   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dejdefddZd6d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d0d1Zdejdefd2d3Zdejdefd4d5ZdS )7ExprPrinter_torch_sympystrexprreturnc                 C      |  |jdt|S )N*	stringifyargsr   selfr
    r   X/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/torch/utils/_sympy/printers.py
_print_Mul      zExprPrinter._print_Mulc                 C      d|  |jd  dS )Nznot (r   )_printr   r   r   r   r   
_print_Not      zExprPrinter._print_NotN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(   r   zExprPrinter._print_Modc                 C   s$   |  |jdtd d }d| dS )N / r+   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   r   r   r   r   r   _print_Identity2      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_Float5   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_PowE   s   
zExprPrinter._print_Powc                 C      t dt|  )Nz#_print_ToFloat not implemented for NotImplementedErrortyper   r   r   r   _print_ToFloatS      zExprPrinter._print_ToFloatc                 C   rH   )Nz$_print_Infinity not implemented for rI   r   r   r   r   _print_InfinityV   rM   zExprPrinter._print_Infinityc                 C   rH   )Nz,_print_NegativeInfinity not implemented for rI   r   r   r   r   _print_NegativeInfinityY      z#ExprPrinter._print_NegativeInfinityc                 C   rH   )Nz$_print_FloorDiv not implemented for rI   r   r   r   r   r3   ^   rM   zExprPrinter._print_FloorDivc                 C   rH   )Nz%_print_PythonMod not implemented for rI   r   r   r   r   _print_PythonModa   rM   zExprPrinter._print_PythonModc                 C   rH   )Nz&_print_IntTrueDiv not implemented for rI   r   r   r   r   _print_IntTrueDivd   rM   zExprPrinter._print_IntTrueDivc                 C   rH   )N(_print_PowByNatural not implemented for rI   r   r   r   r   _print_PowByNaturalg   rP   zExprPrinter._print_PowByNaturalc                 C   rH   )Nz$_print_FloatPow not implemented for rI   r   r   r   r   _print_FloatPowl   rM   zExprPrinter._print_FloatPowc                 C   rH   )Nz&_print_TruncToInt not implemented for rI   r   r   r   r   _print_TruncToInto   rM   zExprPrinter._print_TruncToIntc                 C   rH   )Nz&_print_RoundToInt not implemented for rI   r   r   r   r   _print_RoundToIntr   rM   zExprPrinter._print_RoundToIntc                 C   rH   )Nz(_print_RoundDecimal not implemented for rI   r   r   r   r   _print_RoundDecimalu   rP   zExprPrinter._print_RoundDecimalc                 C   rH   )Nz(_print_TruncToFloat not implemented for rI   r   r   r   r   _print_TruncToFloat   rP   zExprPrinter._print_TruncToFloatr2   ) __name__
__module____qualname__printmethodr>   Exprr=   r   r   r   r   r!   r%   r'   r-   r1   r4   r7   r@   rG   rL   rN   rO   r3   rQ   rR   rT   rU   rV   rW   rX   rY   r   r   r   r   r      s4    
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   r   r   r   r   r   rL      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>rB   r/    // r   r*   r   r   r
   xdivmodr   rm   r   _print_ModularIndexing   s   
z$PythonPrinter._print_ModularIndexingc                 C      dS )Nzmath.infr   r   r   r   r   rN         zPythonPrinter._print_Infinityc                 C   rw   )Nz	-math.infr   r   r   r   r   rO      rx   z%PythonPrinter._print_NegativeInfinityc                 C   r(   r)   r$   r   r   r   r   rQ      r   zPythonPrinter._print_PythonModc                    s&    fdd|j D \}}| d| S )Nc                 3   rf   rg   rh   rj   rm   r   r   rn      s   " z0PythonPrinter._print_FloorDiv.<locals>.<genexpr>rp   rq   r   r
   rs   rt   r   rm   r   r3      s   zPythonPrinter._print_FloorDivc                 C   r(   )Nr.   r+   r,   r$   r   r   r   r   rR      r   zPythonPrinter._print_IntTrueDivc                 C   s   d|  | dS )Nz
math.sqrt(r   r   r   r   r   r   _helper_sqrt   rM   zPythonPrinter._helper_sqrtc                 C   r5   r6   )r{   r   r   r   r   r   _print_OpaqueUnaryFn_sqrt   r8   z'PythonPrinter._print_OpaqueUnaryFn_sqrtc                 C   r"   Nz ** Powr$   r   r   r   r   rU      r   zPythonPrinter._print_FloatPowc                 C   r"   r}   r$   r   r   r   r   rT      r   z!PythonPrinter._print_PowByNaturalc                 C   r`   Nra   zmath.floor(r   r   rb   r   r   r   r   _print_floor      zPythonPrinter._print_floorc                 C   r`   r   rb   r   r   r   r   _print_FloorToInt   r   zPythonPrinter._print_FloorToIntc                 C   r`   )Nra   zmath.trunc(r   r   rb   r   r   r   r   rV         zPythonPrinter._print_TruncToIntc                 C   r`   Nra   z
math.ceil(r   r   rb   r   r   r   r   _print_ceiling   r   zPythonPrinter._print_ceilingc                 C   r`   r   rb   r   r   r   r   _print_CeilToInt   r   zPythonPrinter._print_CeilToIntc                 C   r`   )Nra   zabs(r   r   rb   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   rc   r   joinmapr   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`   )Nra   z	math.cos(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_cos   r   z&PythonPrinter._print_OpaqueUnaryFn_cosc                 C   r`   )Nra   z
math.cosh(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_cosh   r   z'PythonPrinter._print_OpaqueUnaryFn_coshc                 C   r`   )Nra   z
math.acos(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_acos   r   z'PythonPrinter._print_OpaqueUnaryFn_acosc                 C   r`   )Nra   z	math.sin(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_sin   r   z&PythonPrinter._print_OpaqueUnaryFn_sinc                 C   r`   )Nra   z
math.sinh(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_sinh   r   z'PythonPrinter._print_OpaqueUnaryFn_sinhc                 C   r`   )Nra   z
math.asin(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_asin   r   z'PythonPrinter._print_OpaqueUnaryFn_asinc                 C   r`   )Nra   z	math.tan(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_tan  r   z&PythonPrinter._print_OpaqueUnaryFn_tanc                 C   r`   )Nra   z
math.tanh(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_tanh  r   z'PythonPrinter._print_OpaqueUnaryFn_tanhc                 C   r`   )Nra   z
math.atan(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_atan
  r   z'PythonPrinter._print_OpaqueUnaryFn_atanc                 C   r`   )Nra   z
math.log2(r   r   rb   r   r   r   r   _print_OpaqueUnaryFn_log2  r   z'PythonPrinter._print_OpaqueUnaryFn_log2c                 C   r`   )Nra   round(r   r   rb   r   r   r   r   rW     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   )rc   r   
isinstancer>   Integerr   )r   r
   numberndigitsr   r   r   rX     s   
z!PythonPrinter._print_RoundDecimalN)'rZ   r[   r\   r>   r^   r=   rL   rd   re   rv   rN   rO   rQ   r3   rR   r{   r|   rU   rT   r   r   rV   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rW   rX   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platformrC   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   rf   rg   rh   rj   rm   r   r   rn   +  ro   z*CppPrinter._print_Where.<locals>.<genexpr>z ? z : rq   )r   r
   cpqr   rm   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 )
Nra   ,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   rr   r   r   r   rv   0  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   ry   r   r   r   r3   <  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 Nra   zstd::floor(r   r   static_cast<r   rc   r   r   r   r   r   r
   rr   r   r   r   D     zCppPrinter._print_floorc                 C   r   r   r   r   r   r   r   r   I  r   zCppPrinter._print_FloorToIntc                 C   s<   t |jdks	J d| |jd  d}dt d| dS )Nra   std::trunc(r   r   r   r   )rc   r   r   r   r   r   r   r   rV   N  s   zCppPrinter._print_TruncToIntc                 C   r`   )Nra   r   r   r   rb   r   r   r   r   rY   S  r   zCppPrinter._print_TruncToFloatc                 C   r`   )Nra   static_cast<double>(r   r   rb   r   r   r   r   rL   W  r   zCppPrinter._print_ToFloatc                 C   s0   |j \}}| |}| |}d| d| dS )Nzc10::div_mod(r   r   )r   r   ry   r   r   r   rQ   [  s   


zCppPrinter._print_PythonModc                 C   (   |j \}}d| | d| | dS )Nr   z) / static_cast<double>(r   r   r   )r   r
   lhsrhsr   r   r   rR   a  s   
zCppPrinter._print_IntTrueDivc                 C   s6   |j \}}|dkrd| | dS tdt|  )Nr   z(1 << (r   rS   )r   r   rJ   rK   rD   r   r   r   rT   h  s   
zCppPrinter._print_PowByNaturalc                 C   r   )N	std::pow(r   r   r   rD   r   r   r   rU   q  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   rA   z1.0/(z1.0/z1.0r   r   r   r   )	r   r   r   rC   r   r   absr   float)r   r
   rE   rF   r   r   r   r   rG   u  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 )Nra   z.0/z.0r   r   r   )r   r   r   r   r   r   r   r   _print_Rational  s   

zCppPrinter._print_Rationalc                 C   r   Nra   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   rz   rk   arm   r   r   
<listcomp>      z)CppPrinter._print_Min.<locals>.<listcomp>r   zstd::min(static_cast<r   r   ), static_cast<ra   r   {r   }z	std::min<r   r   rc   r   r   r   r
   r   ilr   rm   r   r     
   &zCppPrinter._print_Minc              	      r   )Nc                    r   r   rz   r   rm   r   r   r     r   z)CppPrinter._print_Max.<locals>.<listcomp>r   zstd::max(static_cast<r   r   r   ra   r   r   r   r   z	std::max<r   r   r   r   rm   r   r     r   zCppPrinter._print_Maxc                 C   r`   )Nra   z	std::abs(r   r   rb   r   r   r   r   r     r   zCppPrinter._print_Absc                 C   r`   )Nra   z	std::cos(r   r   rb   r   r   r   r   r     r   z#CppPrinter._print_OpaqueUnaryFn_cosc                 C   r`   )Nra   z
std::cosh(r   r   rb   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_coshc                 C   r`   )Nra   z
std::acos(r   r   rb   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_acosc                 C   r`   )Nra   z	std::sin(r   r   rb   r   r   r   r   r     r   z#CppPrinter._print_OpaqueUnaryFn_sinc                 C   r`   )Nra   z
std::sinh(r   r   rb   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_sinhc                 C   r`   )Nra   z
std::asin(r   r   rb   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_asinc                 C   r`   )Nra   z	std::tan(r   r   rb   r   r   r   r   r     r   z#CppPrinter._print_OpaqueUnaryFn_tanc                 C   r`   )Nra   z
std::tanh(r   r   rb   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_tanhc                 C   r`   )Nra   z
std::atan(r   r   rb   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_atanc                 C   r   )Nr   r   r   r   r   r   r   r   r|     r   z$CppPrinter._print_OpaqueUnaryFn_sqrtc                 C   r   )Nz
std::log2(r   r   r   r   r   r   r   r     r   z$CppPrinter._print_OpaqueUnaryFn_log2c                 C   r`   )Nra   zstd::lrint(r   r   rb   r   r   r   r   rW     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 .rA   z%static_cast<double>(std::nearbyint(1ez * z) * 1er   )rc   r   r   
ValueErrorri   r   )r   r
   r   r   
number_strr   r   r   rX     s   

zCppPrinter._print_RoundDecimalc                 C   rw   )Ntruer   r   r   r   r   _print_BooleanTrue  rx   zCppPrinter._print_BooleanTruec                 C   rw   )Nfalser   r   r   r   r   _print_BooleanFalse  rx   zCppPrinter._print_BooleanFalsec                 C   rw   )Nz'std::numeric_limits<double>::infinity()r   r   r   r   r   rN     rx   zCppPrinter._print_Infinityc                 C   s   d|  | S )N-)rN   r   r   r   r   rO     r8   z"CppPrinter._print_NegativeInfinityN)+rZ   r[   r\   r>   r^   r=   r   r   rv   r3   r   r   rV   rY   rL   rQ   rR   rT   rU   rG   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r|   r   rW   rX   r   r   rN   rO   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    u 