o
    wZhAK                     @   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)
StrPrinterZ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ExprPrinterZ_torch_sympystrexprreturnc                 C      |  |jdt|S )N*	stringifyargsr   selfr    r   J/var/www/auris/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   Zrel_opr   r   r   r   r   _print_Relational   s   zExprPrinter._print_Relationalc                 C      |  |jdtd S )Nz & Z
BitwiseAndr   r   r   r   r   r   r   _print_BitwiseFn_bitwise_and   r   z(ExprPrinter._print_BitwiseFn_bitwise_andc                 C   r   )Nz | Z	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   r8   )Nz$_print_Infinity not implemented for r9   r   r   r   r   _print_InfinityH   r=   zExprPrinter._print_Infinityc                 C   r8   )Nz,_print_NegativeInfinity not implemented for r9   r   r   r   r   _print_NegativeInfinityK      z#ExprPrinter._print_NegativeInfinityc                 C   r8   )Nz$_print_FloorDiv not implemented for r9   r   r   r   r   r)   P   r=   zExprPrinter._print_FloorDivc                 C   r8   )Nz%_print_PythonMod not implemented for r9   r   r   r   r   _print_PythonModS   r=   zExprPrinter._print_PythonModc                 C   r8   )Nz&_print_IntTrueDiv not implemented for r9   r   r   r   r   _print_IntTrueDivV   r=   zExprPrinter._print_IntTrueDivc                 C   r8   Nz(_print_PowByNatural not implemented for r9   r   r   r   r   _print_PowByNaturalY   r@   zExprPrinter._print_PowByNaturalc                 C   r8   )Nz$_print_FloatPow not implemented for r9   r   r   r   r   _print_FloatPow^   r=   zExprPrinter._print_FloatPowc                 C   r8   )Nz&_print_TruncToInt not implemented for r9   r   r   r   r   _print_TruncToInta   r=   zExprPrinter._print_TruncToIntc                 C   r8   )Nz&_print_RoundToInt not implemented for r9   r   r   r   r   _print_RoundToIntd   r=   zExprPrinter._print_RoundToIntc                 C   r8   )Nz(_print_RoundDecimal not implemented for r9   r   r   r   r   _print_RoundDecimalg   r@   zExprPrinter._print_RoundDecimalc                 C   r8   )Nz(_print_TruncToFloat not implemented for r9   r   r   r   r   _print_TruncToFloatq   r@   zExprPrinter._print_TruncToFloatr(   )__name__
__module____qualname__ZprintmethodsympyExprstrr   r   r   r   r   r   r!   r'   r*   r/   r7   r<   r>   r?   r)   rA   rB   rD   rE   rF   rG   rH   rI   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   r.   r   r   r   r   r<   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>r2   r$    // r%   r   r   r   r   xdivmodr   r^   r   _print_ModularIndexing   s   
z$PythonPrinter._print_ModularIndexingc                 C      dS )Nzmath.infr   r   r   r   r   r>         zPythonPrinter._print_Infinityc                 C   rh   )Nz	-math.infr   r   r   r   r   r?      ri   z%PythonPrinter._print_NegativeInfinityc                 C   r   r   r   r   r   r   r   rA      r"   zPythonPrinter._print_PythonModc                    s&    fdd|j D \}}| d| S )Nc                 3   rW   rX   rY   r[   r^   r   r   r_      s   " z0PythonPrinter._print_FloorDiv.<locals>.<genexpr>ra   rb   r   r   rd   re   r   r^   r   r)      s   zPythonPrinter._print_FloorDivc                 C   r   )Nr#   r   r    r   r   r   r   r   rB      r"   zPythonPrinter._print_IntTrueDivc                 C   s   d|  | dS )Nz
math.sqrt(r%   r.   r   r   r   r   _helper_sqrt   r=   zPythonPrinter._helper_sqrtc                 C   r+   r,   )rl   r   r   r   r   r   _print_OpaqueUnaryFn_sqrt   r0   z'PythonPrinter._print_OpaqueUnaryFn_sqrtc                 C   r   Nz ** Powr   r   r   r   r   rE      r   zPythonPrinter._print_FloatPowc                 C   r   rn   r   r   r   r   r   rD      r   z!PythonPrinter._print_PowByNaturalc                 C   rQ   NrR   zmath.floor(r   r%   rS   r   r   r   r   _print_floor      zPythonPrinter._print_floorc                 C   rQ   rp   rS   r   r   r   r   _print_FloorToInt   rr   zPythonPrinter._print_FloorToIntc                 C   rQ   )NrR   zmath.trunc(r   r%   rS   r   r   r   r   rF         zPythonPrinter._print_TruncToIntc                 C   rQ   NrR   z
math.ceil(r   r%   rS   r   r   r   r   _print_ceiling   rr   zPythonPrinter._print_ceilingc                 C   rQ   ru   rS   r   r   r   r   _print_CeilToInt   rr   zPythonPrinter._print_CeilToIntc                 C   rQ   )NrR   zabs(r   r%   rS   r   r   r   r   
_print_Abs   rr   zPythonPrinter._print_Absc                 C   .   t |jdks	J ddt| j|j dS )N   zmax(, r%   rT   r   joinmapr.   r   r   r   r   
_print_Max      zPythonPrinter._print_Maxc                 C   ry   )Nrz   zmin(r{   r%   r|   r   r   r   r   
_print_Min   r   zPythonPrinter._print_Minc                 C   rQ   )NrR   z	math.cos(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_cos   rr   z&PythonPrinter._print_OpaqueUnaryFn_cosc                 C   rQ   )NrR   z
math.cosh(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_cosh   rr   z'PythonPrinter._print_OpaqueUnaryFn_coshc                 C   rQ   )NrR   z
math.acos(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_acos   rr   z'PythonPrinter._print_OpaqueUnaryFn_acosc                 C   rQ   )NrR   z	math.sin(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_sin   rr   z&PythonPrinter._print_OpaqueUnaryFn_sinc                 C   rQ   )NrR   z
math.sinh(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_sinh   rr   z'PythonPrinter._print_OpaqueUnaryFn_sinhc                 C   rQ   )NrR   z
math.asin(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_asin   rr   z'PythonPrinter._print_OpaqueUnaryFn_asinc                 C   rQ   )NrR   z	math.tan(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_tan   rr   z&PythonPrinter._print_OpaqueUnaryFn_tanc                 C   rQ   )NrR   z
math.tanh(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_tanh   rr   z'PythonPrinter._print_OpaqueUnaryFn_tanhc                 C   rQ   )NrR   z
math.atan(r   r%   rS   r   r   r   r   _print_OpaqueUnaryFn_atan   rr   z'PythonPrinter._print_OpaqueUnaryFn_atanc                 C   rQ   )NrR   round(r   r%   rS   r   r   r   r   rG      rr   zPythonPrinter._print_RoundToIntc                 C   sD   t |jdks	J |j\}}t|tjsJ d| | d| dS )Nrz   r   r{   r%   )rT   r   
isinstancerM   Integerr.   )r   r   numberndigitsr   r   r   rH     s   
z!PythonPrinter._print_RoundDecimalN)&rJ   rK   rL   rM   rN   rO   r<   rU   rV   rg   r>   r?   rA   r)   rB   rl   rm   rE   rD   rq   rs   rF   rv   rw   rx   r   r   r   r   r   r   r   r   r   r   r   rG   rH   r   r   r   r   rP   w   sB    rP   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win32ZLLL)sysplatformr3   r   r   r   r   _print_Integer  s   zCppPrinter._print_Integerc                    s.    fdd|j D \}}}| d| d| S )Nc                 3   rW   rX   rY   r[   r^   r   r   r_     r`   z*CppPrinter._print_Where.<locals>.<genexpr>z ? z : rb   )r   r   cpqr   r^   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 )
NrR   ,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_TYPErc   r   r   r   rg     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   rj   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 NrR   zstd::floor(r   r%   static_cast<r   rT   r   r.   r   r   r   r   rr   r   r   rq   +     zCppPrinter._print_floorc                 C   r   r   r   r   r   r   r   rs   0  r   zCppPrinter._print_FloorToIntc                 C   s<   t |jdks	J d| |jd  d}dt d| dS )NrR   std::trunc(r   r%   r   r   )rT   r   r.   r   r   r   r   r   rF   5  s   zCppPrinter._print_TruncToIntc                 C   rQ   )NrR   r   r   r%   rS   r   r   r   r   rI   :  rr   zCppPrinter._print_TruncToFloatc                 C   rQ   )NrR   static_cast<double>(r   r%   rS   r   r   r   r   r<   >  rr   zCppPrinter._print_ToFloatc                 C   s0   |j \}}| |}| |}d| d| dS )Nzc10::div_mod(r{   r%   )r   r   rj   r   r   r   rA   B  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   rB   H  s   
zCppPrinter._print_IntTrueDivc                 C   r8   rC   r9   r   r   r   r   rD   O  r@   zCppPrinter._print_PowByNaturalc                 C   r   )N	std::pow(r{   r%   r   r4   r   r   r   rE   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
   r1   z1.0/(z1.0/z1.0r   r   r   r{   )	r   r.   r   r3   r   r   absr   float)r   r   r5   r6   r   r   r   r   r7   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 )NrR   z.0/z.0r   r   r%   )r   r   r   r   r   r   r   r   _print_Rationals  s   

zCppPrinter._print_Rationalc                 C   r   NrR   z
std::ceil(r   r%   r   r   r   r   r   r   r   rv   {  r   zCppPrinter._print_ceilingc                 C   r   r   r   r   r   r   r   rw     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   rk   r\   ar^   r   r   
<listcomp>      z)CppPrinter._print_Min.<locals>.<listcomp>rz   zstd::min(static_cast<r   r   ), static_cast<rR   r   {r{   }z	std::min(r%   r   rT   r   r}   r   r   r   ilr   r^   r   r     
   &zCppPrinter._print_Minc              	      r   )Nc                    r   r   rk   r   r^   r   r   r     r   z)CppPrinter._print_Max.<locals>.<listcomp>rz   zstd::max(static_cast<r   r   r   rR   r   r   r{   r   z	std::max(r%   r   r   r   r^   r   r     r   zCppPrinter._print_Maxc                 C   rQ   )NrR   z	std::abs(r   r%   rS   r   r   r   r   rx     rr   zCppPrinter._print_Absc                 C   rQ   )NrR   z	std::cos(r   r%   rS   r   r   r   r   r     rr   z#CppPrinter._print_OpaqueUnaryFn_cosc                 C   rQ   )NrR   z
std::cosh(r   r%   rS   r   r   r   r   r     rr   z$CppPrinter._print_OpaqueUnaryFn_coshc                 C   rQ   )NrR   z
std::acos(r   r%   rS   r   r   r   r   r     rr   z$CppPrinter._print_OpaqueUnaryFn_acosc                 C   rQ   )NrR   z	std::sin(r   r%   rS   r   r   r   r   r     rr   z#CppPrinter._print_OpaqueUnaryFn_sinc                 C   rQ   )NrR   z
std::sinh(r   r%   rS   r   r   r   r   r     rr   z$CppPrinter._print_OpaqueUnaryFn_sinhc                 C   rQ   )NrR   z
std::asin(r   r%   rS   r   r   r   r   r     rr   z$CppPrinter._print_OpaqueUnaryFn_asinc                 C   rQ   )NrR   z	std::tan(r   r%   rS   r   r   r   r   r     rr   z#CppPrinter._print_OpaqueUnaryFn_tanc                 C   rQ   )NrR   z
std::tanh(r   r%   rS   r   r   r   r   r     rr   z$CppPrinter._print_OpaqueUnaryFn_tanhc                 C   rQ   )NrR   z
std::atan(r   r%   rS   r   r   r   r   r     rr   z$CppPrinter._print_OpaqueUnaryFn_atanc                 C   s   d|  |jd  dS )Nr   r   r%   r-   r   r   r   r   rm     r"   z$CppPrinter._print_OpaqueUnaryFn_sqrtc                 C   rQ   )NrR   zstd::lrint(r   r%   rS   r   r   r   r   rG     rt   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 )
Nrz   r   zOFor integer inputs, only non-negative ndigits are currently supported, but got .r1   z%static_cast<double>(std::nearbyint(1ez * z) * 1er%   )rT   r   r   
ValueErrorrZ   r   )r   r   r   r   Z
number_strr   r   r   rH     s   

zCppPrinter._print_RoundDecimalc                 C   rh   )Ntruer   r   r   r   r   _print_BooleanTrue  ri   zCppPrinter._print_BooleanTruec                 C   rh   )Nfalser   r   r   r   r   _print_BooleanFalse  ri   zCppPrinter._print_BooleanFalsec                 C   rh   )Nz'std::numeric_limits<double>::infinity()r   r   r   r   r   r>     ri   zCppPrinter._print_Infinityc                 C   s   d|  | S )N-)r>   r   r   r   r   r?     r0   z"CppPrinter._print_NegativeInfinityN)*rJ   rK   rL   rM   rN   rO   r   r   rg   r)   rq   rs   rF   rI   r<   rA   rB   rD   rE   r7   r   rv   rw   r   r   rx   r   r   r   r   r   r   r   r   r   rm   rG   rH   r   r   r>   r?   r   r   r   r   r     sJ    		r   )r   typingr   rM   Zsympy.printing.precedencer   r   Zsympy.printing.strr   r   r   rP   r   r   r   r   r   <module>   s    i 