o
    m9:j<q                     @   sl   d dl 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)
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d:dejd	edB 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ejdefd6d7Zdejdefd8d9Z dS );ExprPrinter_torch_sympystrexprreturnc                 C      |  |jdt|S )N*	stringifyargsr   selfr	    r   b/home/nk/hobo-godmode/plappi-mvp/.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   r!   )Nz ^ 
BitwiseXorr#   r   r   r   r   _print_BitwiseFn_bitwise_xor&   r   z(ExprPrinter._print_BitwiseFn_bitwise_xorc                 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   r	   sr   r   r   _print_FloatTrueDiv.   s   zExprPrinter._print_FloatTrueDivc                 C   s
   |  |S N)_print_FloorDivr   r   r   r   _print_CleanDiv2   s   
zExprPrinter._print_CleanDivc                 C      |  |jd S Nr   r   r   r   r   r   _print_Identity5   s   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_Float9   s   
zExprPrinter._print_Floatc                 C   s`   |j \}}|t|krt|t|}|dk rtd| |dkr.| |g| dtd S dS )Nr   z#exponent must be non-negative, got r   Mul1)r   intAssertionErrorr   r   r   r	   baseexpr   r   r   
_print_PowI   s   
zExprPrinter._print_Powc                 C      t dt|  )Nz#_print_ToFloat not implemented for NotImplementedErrortyper   r   r   r   _print_ToFloatY      zExprPrinter._print_ToFloatc                 C   rJ   )Nz$_print_Infinity not implemented for rK   r   r   r   r   _print_Infinity\   rO   zExprPrinter._print_Infinityc                 C   rJ   )Nz,_print_NegativeInfinity not implemented for rK   r   r   r   r   _print_NegativeInfinity_      z#ExprPrinter._print_NegativeInfinityc                 C   rJ   )Nz_print_NaN not implemented for rK   r   r   r   r   
_print_NaNd   rO   zExprPrinter._print_NaNc                 C   rJ   )Nz$_print_FloorDiv not implemented for rK   r   r   r   r   r5   g   rO   zExprPrinter._print_FloorDivc                 C   rJ   )Nz%_print_PythonMod not implemented for rK   r   r   r   r   _print_PythonModj   rO   zExprPrinter._print_PythonModc                 C   rJ   )Nz&_print_IntTrueDiv not implemented for rK   r   r   r   r   _print_IntTrueDivm   rO   zExprPrinter._print_IntTrueDivc                 C   rJ   )N(_print_PowByNatural not implemented for rK   r   r   r   r   _print_PowByNaturalp   rR   zExprPrinter._print_PowByNaturalc                 C   rJ   )Nz$_print_FloatPow not implemented for rK   r   r   r   r   _print_FloatPowu   rO   zExprPrinter._print_FloatPowc                 C   rJ   )Nz&_print_TruncToInt not implemented for rK   r   r   r   r   _print_TruncToIntx   rO   zExprPrinter._print_TruncToIntc                 C   rJ   )Nz&_print_RoundToInt not implemented for rK   r   r   r   r   _print_RoundToInt{   rO   zExprPrinter._print_RoundToIntc                 C   rJ   )Nz(_print_RoundDecimal not implemented for rK   r   r   r   r   _print_RoundDecimal~   rR   zExprPrinter._print_RoundDecimalc                 C   rJ   )Nz(_print_TruncToFloat not implemented for rK   r   r   r   r   _print_TruncToFloat   rR   zExprPrinter._print_TruncToFloatr4   )!__name__
__module____qualname__printmethodr?   Exprr>   r   r   r   r    r$   r&   r(   r.   r3   r6   r9   rA   rI   rN   rP   rQ   rS   r5   rT   rU   rW   rX   rY   rZ   r[   r\   r   r   r   r   r      s8    
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(dIS )JPythonPrinterr	   r
   c                 C   .   t |jdkrtdd| |jd  dS )N   $ToFloat expects exactly one argumentztorch.sym_float(r   r   lenr   rE   r   r   r   r   r   rN      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>rC   r1    // r   r+   r   r   r	   xdivmodr   rq   r   _print_ModularIndexing   s   
z$PythonPrinter._print_ModularIndexingc                 C      dS )Nzmath.infr   r   r   r   r   rP         zPythonPrinter._print_Infinityc                 C   r{   )Nz	-math.infr   r   r   r   r   rQ      r|   z%PythonPrinter._print_NegativeInfinityc                 C   r{   )Nzmath.nanr   r   r   r   r   rS      r|   zPythonPrinter._print_NaNc                 C   r)   r*   r#   r   r   r   r   rT      r/   zPythonPrinter._print_PythonModc                    s&    fdd|j D \}}| d| S )Nc                 3   rj   rk   rl   rn   rq   r   r   rr      s   " z0PythonPrinter._print_FloorDiv.<locals>.<genexpr>rt   ru   r   r	   rw   rx   r   rq   r   r5      s   zPythonPrinter._print_FloorDivc                 C   r)   )Nr0   r,   r-   r#   r   r   r   r   rU      r/   zPythonPrinter._print_IntTrueDivc                 C   s   d|  | dS )Nztorch._sym_sqrt(r   r   r   r   r   r   _helper_sqrt   s   zPythonPrinter._helper_sqrtc                 C   r7   r8   )r   r   r   r   r   r   _print_OpaqueUnaryFn_sqrt      z'PythonPrinter._print_OpaqueUnaryFn_sqrtc                 C   r!   Nz ** Powr#   r   r   r   r   rX      r   zPythonPrinter._print_FloatPowc                 C   r!   r   r#   r   r   r   r   rW      r   z!PythonPrinter._print_PowByNaturalc                 C   rc   )Nrd   "floor expects exactly one argumentmath.floor(r   r   rf   r   r   r   r   _print_floor      zPythonPrinter._print_floorc                 C   rc   )Nrd   'FloorToInt expects exactly one argumentr   r   r   rf   r   r   r   r   _print_FloorToInt   r   zPythonPrinter._print_FloorToIntc                 C   rc   )Nrd   'TruncToInt expects exactly one argumentzmath.trunc(r   r   rf   r   r   r   r   rY         zPythonPrinter._print_TruncToIntc                 C   rc   )Nrd   $ceiling expects exactly one argument
math.ceil(r   r   rf   r   r   r   r   _print_ceiling   r   zPythonPrinter._print_ceilingc                 C   rc   )Nrd   &CeilToInt expects exactly one argumentr   r   r   rf   r   r   r   r   _print_CeilToInt   r   zPythonPrinter._print_CeilToIntc                 C   rc   )Nrd    Abs expects exactly one argumentzabs(r   r   rf   r   r   r   r   
_print_Abs   r   zPythonPrinter._print_Absc                 C   2   t |jdk rtdddt| j|j dS )N   z"Max expects at least two argumentszmax(, r   rg   r   rE   joinmapr   r   r   r   r   
_print_Max     zPythonPrinter._print_Maxc                 C   r   )Nr   z"Min expects at least two argumentszmin(r   r   r   r   r   r   r   
_print_Min  r   zPythonPrinter._print_Minc                 C   rc   )Nrd    cos expects exactly one argumentz	math.cos(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_cos  r   z&PythonPrinter._print_OpaqueUnaryFn_cosc                 C   rc   )Nrd   !cosh expects exactly one argumentz
math.cosh(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_cosh  r   z'PythonPrinter._print_OpaqueUnaryFn_coshc                 C   rc   )Nrd   !acos expects exactly one argumentz
math.acos(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_acos  r   z'PythonPrinter._print_OpaqueUnaryFn_acosc                 C   rc   Nrd   z sin expects exactly one argumentz	math.sin(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_sin$  r   z&PythonPrinter._print_OpaqueUnaryFn_sinc                 C   rc   )Nrd   !sinh expects exactly one argumentz
math.sinh(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_sinh*  r   z'PythonPrinter._print_OpaqueUnaryFn_sinhc                 C   rc   )Nrd   !asin expects exactly one argumentz
math.asin(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_asin0  r   z'PythonPrinter._print_OpaqueUnaryFn_asinc                 C   rc   )Nrd    tan expects exactly one argumentz	math.tan(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_tan6  r   z&PythonPrinter._print_OpaqueUnaryFn_tanc                 C   rc   )Nrd   !tanh expects exactly one argumentz
math.tanh(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_tanh<  r   z'PythonPrinter._print_OpaqueUnaryFn_tanhc                 C   rc   )Nrd   !atan expects exactly one argumentz
math.atan(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_atanB  r   z'PythonPrinter._print_OpaqueUnaryFn_atanc                 C   rc   )Nrd   z!log2 expects exactly one argumentz
math.log2(r   r   rf   r   r   r   r   _print_OpaqueUnaryFn_log2H  r   z'PythonPrinter._print_OpaqueUnaryFn_log2c                 C   rc   )Nrd   'RoundToInt expects exactly one argumentround(r   r   rf   r   r   r   r   rZ   N  r   zPythonPrinter._print_RoundToIntc                 C   sL   t |jdkrtd|j\}}t|tjstdd| | d| dS )Nr   *RoundDecimal expects exactly two argumentsz,ndigits must be an instance of sympy.Integerr   r   r   )rg   r   rE   
isinstancer?   Integer	TypeErrorr   )r   r	   numberndigitsr   r   r   r[   T  s   
z!PythonPrinter._print_RoundDecimalc                 C   sl   d }t |jD ](\}}| |}|dkr|}q| |}|d u r#|}qd| d| d| d}q|r4|S dS )NTr1   z if z else r   0)reversedr   r   r   r	   resultexpr_icond_iexpr_strcond_strr   r   r   _print_Piecewise]  s   

zPythonPrinter._print_PiecewiseN))r]   r^   r_   r?   ra   r>   rN   rh   ri   rz   rP   rQ   rS   rT   r5   rU   r   r   rX   rW   r   r   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ   r[   r   r   r   r   r   rb      sH    		rb   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*dejdefdMdNZ+dejdefdOdPZ,dejdefdQdRZ-dSS )T
CppPrinterr	   r
   c                 C   sl   t jdv rdnd}t|}|tks|tk rt| dt |tkr0|dkr*tdd| dS | | S )	N)darwinwin32LLLz too big to convert to r   z#unexpected minimum index type valuez(-1z << 63))sysplatformrD   INDEX_TYPE_MAXINDEX_TYPE_MINOverflowError
INDEX_TYPErE   )r   r	   suffixir   r   r   _print_Integers  s   zCppPrinter._print_Integerc                    s.    fdd|j D \}}}| d| d| S )Nc                 3   rj   rk   rl   rn   rq   r   r   rr     rs   z*CppPrinter._print_Where.<locals>.<genexpr> ?  : ru   )r   r	   cpqr   rq   r   _print_Where  s   
zCppPrinter._print_Wherec                 C   r   )Nz || r   r   r   r   r   ri     r   zCppPrinter._print_Orc                 C   s   d }t |jD ]0\}}| |td d }|dkr|}q| |td d }|d u r-|}q| d| d| }q|r@d| dS dS )	Nr,   r-   Tr   r   r1   r   r   )r   r   rm   r   r   r   r   r   r     s   zCppPrinter._print_Piecewisec              	   C   s|   |j \}}}| |}|dkr*| |}|jr!d| d| d}n	d| d| d}| |}dt d| d	t d| d	S )
Nrd   ,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   rv   r   r   r   rz     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   r}   r   r   r   r5     s   


zCppPrinter._print_FloorDivc                 C   J   t |jdkrtdd| |jd  d}|jr#dt d| dS |S )Nrd   r   std::floor(r   r   static_cast<r   rg   r   rE   r   r   r   r   r	   rr   r   r   r        zCppPrinter._print_floorc                 C   r   )Nrd   r   r   r   r   r   r   r   r   r   r   r   r     r   zCppPrinter._print_FloorToIntc                 C   s@   t |jdkrtdd| |jd  d}dt d| dS )Nrd   r   std::trunc(r   r   r   r   )rg   r   rE   r   r   r   r   r   r   rY     s   zCppPrinter._print_TruncToIntc                 C   rc   )Nrd   z)TruncToFloat expects exactly one argumentr   r   r   rf   r   r   r   r   r\     r   zCppPrinter._print_TruncToFloatc                 C   rc   )Nrd   re   static_cast<double>(r   r   rf   r   r   r   r   rN     r   zCppPrinter._print_ToFloatc                 C   s0   |j \}}| |}| |}d| d| dS )Nzc10::div_mod(r   r   )r   r   r}   r   r   r   rT     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   rU     s   
zCppPrinter._print_IntTrueDivc                 C   s6   |j \}}|dkrd| | dS tdt|  )Nr   z(1 << (r   rV   )r   r   rL   rM   rF   r   r   r   rW     s   
zCppPrinter._print_PowByNaturalc                 C   r   )N	std::pow(r   r   r   rF   r   r   r   rX     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   rB   z1.0/(z1.0/z1.0r   r   r   r   )	r   r   r   rD   r   r   absr   float)r   r	   rG   rH   r   r   r   r   rI     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 )Nrd   z.0/z.0r   r   r   )r   r   r   r   r   r   r   r   _print_Rational
  s   

zCppPrinter._print_Rationalc                 C   r   )Nrd   r   
std::ceil(r   r   r   r   r   r   r   r   r   r     r   zCppPrinter._print_ceilingc                 C   r   )Nrd   r   r   r   r   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   r~   ro   arq   r   r   
<listcomp>"      z)CppPrinter._print_Min.<locals>.<listcomp>r   zstd::min(static_cast<r   r   ), static_cast<rd   r   {r   }z	std::min<r   r   rg   r   r   r   r	   r   ilr   rq   r   r      
   &zCppPrinter._print_Minc              	      r   )Nc                    r   r   r~   r   rq   r   r   r   ,  r   z)CppPrinter._print_Max.<locals>.<listcomp>r   zstd::max(static_cast<r   r   r   rd   r   r   r   r   z	std::max<r   r   r   r   rq   r   r   *  r   zCppPrinter._print_Maxc                 C   rc   )Nrd   r   z	std::abs(r   r   rf   r   r   r   r   r   4  r   zCppPrinter._print_Absc                 C   rc   )Nrd   r   z	std::cos(r   r   rf   r   r   r   r   r   :  r   z#CppPrinter._print_OpaqueUnaryFn_cosc                 C   rc   )Nrd   r   z
std::cosh(r   r   rf   r   r   r   r   r   @  r   z$CppPrinter._print_OpaqueUnaryFn_coshc                 C   rc   )Nrd   r   z
std::acos(r   r   rf   r   r   r   r   r   F  r   z$CppPrinter._print_OpaqueUnaryFn_acosc                 C   rc   r   rf   r   r   r   r   r   L  r   z#CppPrinter._print_OpaqueUnaryFn_sinc                 C   rc   )Nrd   r   z
std::sinh(r   r   rf   r   r   r   r   r   R  r   z$CppPrinter._print_OpaqueUnaryFn_sinhc                 C   rc   )Nrd   r   z
std::asin(r   r   rf   r   r   r   r   r   X  r   z$CppPrinter._print_OpaqueUnaryFn_asinc                 C   rc   )Nrd   r   z	std::tan(r   r   rf   r   r   r   r   r   ^  r   z#CppPrinter._print_OpaqueUnaryFn_tanc                 C   rc   )Nrd   r   z
std::tanh(r   r   rf   r   r   r   r   r   d  r   z$CppPrinter._print_OpaqueUnaryFn_tanhc                 C   rc   )Nrd   r   z
std::atan(r   r   rf   r   r   r   r   r   j  r   z$CppPrinter._print_OpaqueUnaryFn_atanc                 C   r   )Nr   r   r   r   r   r   r   r   r   p  r   z$CppPrinter._print_OpaqueUnaryFn_sqrtc                 C   r   )Nz
std::log2(r   r   r   r   r   r   r   r   t  r   z$CppPrinter._print_OpaqueUnaryFn_log2c                 C   rc   )Nrd   r   zstd::lrint(r   r   rf   r   r   r   r   rZ   x  r   zCppPrinter._print_RoundToIntc                 C   sp   t |jdkrtd|j\}}|jr#|dkrtdtd| d| |td }d| d	| d
|  dS )Nr   r   r   z+ndigits must be negative for integer inputszOFor integer inputs, only non-negative ndigits are currently supported, but got .rB   z%static_cast<double>(std::nearbyint(1ez * z) * 1er   )rg   r   rE   r   
ValueErrorrm   r   )r   r	   r   r   
number_strr   r   r   r[     s   

zCppPrinter._print_RoundDecimalc                 C   r{   )Ntruer   r   r   r   r   _print_BooleanTrue  r|   zCppPrinter._print_BooleanTruec                 C   r{   )Nfalser   r   r   r   r   _print_BooleanFalse  r|   zCppPrinter._print_BooleanFalsec                 C   r{   )Nz'std::numeric_limits<double>::infinity()r   r   r   r   r   rP     r|   zCppPrinter._print_Infinityc                 C   s   d|  | S )N-)rP   r   r   r   r   rQ     r   z"CppPrinter._print_NegativeInfinityc                 C   r{   )Nz(std::numeric_limits<double>::quiet_NaN()r   r   r   r   r   rS     r|   zCppPrinter._print_NaNN).r]   r^   r_   r?   ra   r>   r   r   ri   r   rz   r5   r   r   rY   r\   rN   rT   rU   rW   rX   rI   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ   r[   r  r  rP   rQ   rS   r   r   r   r   r   r  sR    


r   )r   r?   sympy.printing.precedencer   r   sympy.printing.strr   r   r   r   r   rb   r   r   r   r   r   <module>   s     e