a
    kº”h|)  ã                   @   s”   d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lZe
d
ƒZG dd„ deeƒZddd„Zd	S )é    )ÚAbstractPythonCodePrinterÚArrayPrinter)Ú
MatrixExpr)ÚMul)Ú
PRECEDENCE)Úimport_module)ÚSqrt)ÚS)ÚIntegerNÚtorchc                R       s  e Zd ZdZejdejdejdejdej	dej
dedejd	ejd
ejdejdejdejdejdejdejdejdejdejdejdejdejdejdejdejdejdejdej dej!dej"dej#d ej$d!ej%d"ej&d#ej'd$ej(d%ej)d&ej*d'ej+d(ej,d)ej-d*i)Z.e/e0j1d+d,d-d.Z1d_‡ fd/d0„	Z2‡ fd1d2„Z3e3Z4e3Z5e3Z6e3Z7e3Z8e3Z9e3Z:e3Z;e3Z<e3Z=d3d4„ Z>d5d6„ Z?d7d8„ Z@d9d:„ ZAd;d<„ ZBd=d>„ ZCd?d@„ ZDdAdB„ ZEdCdD„ ZFdEdF„ ZGdGdH„ ZHdIdJ„ ZIdKdL„ ZJdMdN„ ZKdOdP„ ZLdQdR„ ZMdSdT„ ZNdUdV„ ZOdWdX„ ZPdYZQdZZRd[ZSd\ZTd]ZUd^ZV‡  ZWS )`ÚTorchPrinterZ
_torchcodez	torch.absz
torch.signz
torch.ceilztorch.floorz	torch.logz	torch.expú
torch.sqrtz	torch.cosz
torch.acosz	torch.sinz
torch.asinz	torch.tanz
torch.atanztorch.atan2z
torch.coshztorch.acoshz
torch.sinhztorch.asinhz
torch.tanhztorch.atanhú	torch.powz
torch.realz
torch.imagztorch.anglez	torch.erfztorch.lgammaztorch.eqztorch.neztorch.gtztorch.ltztorch.leztorch.geztorch.logical_andztorch.logical_orztorch.logical_notz	torch.maxz	torch.minz	torch.addz	torch.mulztorch.tracez	torch.detNFútorch.float64)Útorch_versionÚrequires_gradÚdtypec                    sJ   t ƒ  |¡ | jd }| jd | _| jd | _|d u r@tr@tj}|| _d S )Nr   r   r   )ÚsuperÚ__init__Z	_settingsr   r   r   Ú__version__r   )ÚselfÚsettingsÚversion©Ú	__class__© úD/var/www/auris/lib/python3.9/site-packages/sympy/printing/pytorch.pyr   S   s    
zTorchPrinter.__init__c                    sl   ˆ j  t|ƒd ¡}|d u r&tƒ  |¡S ‡ fdd„|jD ƒ}t|ƒdkr\dˆ  |¡|d f S ˆ  ||¡S d S )Nc                    s   g | ]}ˆ   |¡‘qS r   ©Ú_print©Ú.0Úarg©r   r   r   Ú
<listcomp>b   ó    z0TorchPrinter._print_Function.<locals>.<listcomp>é   z%s(%s)r   )	ÚmappingÚgetÚtyper   Z_print_BasicÚargsÚlenÚ_module_formatÚ_expand_fold_binary_op)r   ÚexprÚopÚchildrenr   r"   r   Ú_print_Function]   s    þzTorchPrinter._print_Functionc                 C   s   d  |  d¡|  |jd ¡¡S )Nú{}({})ztorch.linalg.invr   )Úformatr+   r   r)   ©r   r-   r   r   r   Ú_print_Inversew   s    ÿzTorchPrinter._print_Inversec              	   C   sr   |j jr4|j jd |j jd kr4d d|  |j ¡¡S d |  |j ¡d dd„ tt|j jƒƒD ƒ¡d d d	… ¡S d S )
Nr   r%   z
{}({}).t()ztorch.transposez{}.permute({})ú, c                 S   s   g | ]}t |ƒ‘qS r   ©Ústr©r    Úir   r   r   r#   „   r$   z1TorchPrinter._print_Transpose.<locals>.<listcomp>éÿÿÿÿ)r!   Z	is_MatrixÚshaper2   r   ÚjoinÚranger*   r3   r   r   r   Ú_print_Transpose{   s     
(þzTorchPrinter._print_Transposec                 C   s(   d|   |j¡d dd„ |jjD ƒ¡f S )Nz%s.permute(%s)r5   c                 s   s   | ]}t |ƒV  qd S ©Nr6   r8   r   r   r   Ú	<genexpr>Š   r$   z2TorchPrinter._print_PermuteDims.<locals>.<genexpr>)r   r-   r<   ZpermutationZ
array_formr3   r   r   r   Ú_print_PermuteDims‡   s    
þzTorchPrinter._print_PermuteDimsc                 C   sl  |j }|j}t|ƒdksDt|ƒdkrÎt|d tƒsÎ| |d ¡dkrÎ|  |¡}i }|D ]@}t|tƒr‚|\}}| |d¡| ||< qV| |d¡d ||< qV| ¡ D ](\}}t	|ƒD ]}	d 
||  |¡¡}q°q |S t|ƒdkrb|d }
t|
tƒrJt|
ƒdkrJ|
\}}t|tƒstdƒ‚|  |¡}t	|ƒD ]}	d 
||  |¡¡}q,|S d 
|  |¡|  |
¡¡S |  |¡S )Nr%   r   z1torch.autograd.grad({}, {}, create_graph=True)[0]é   z!Only integer orders are supportedztorch.autograd.grad({}, {})[0])Ú	variablesr-   r*   Ú
isinstanceÚtupleÚcountr   r'   Úitemsr=   r2   r
   ÚNotImplementedError)r   r-   rC   Zexpr_argÚresultZ
var_groupsÚvarZbase_varÚorderÚ_Úvariabler   r   r   Ú_print_Derivative   s>    
ÿÿÿ


zTorchPrinter._print_Derivativec                 C   s‚   ddl m} |jd j\}}t|jƒdkrLd |  d¡|  |¡|  |¡d¡S d |  d¡|  |¡|  |¡|  ||jdd … Ž ¡¡S )Nr   )Ú	Piecewiser%   z{}({}, {}, {})ztorch.where)ÚsympyrO   r)   r*   r2   r+   r   )r   r-   rO   ÚeZcondr   r   r   Ú_print_Piecewise´   s    üüzTorchPrinter._print_Piecewisec                 C   sN   |j \}}|jtjkr.d |  d¡|  |¡¡S d |  d¡|  |¡|  |¡¡S )Nr1   r   ú
{}({}, {})r   )r)   Úexpr	   ZHalfr2   r+   r   )r   r-   ÚbaserT   r   r   r   Ú
_print_PowÄ   s    
ÿþzTorchPrinter._print_Powc                    s^   dd„ |j D ƒ‰ ‡ fdd„|j D ƒ}|rNd|  t |¡td ¡|  dˆ ¡f S |  dˆ ¡S d S )Nc                 S   s   g | ]}t |tƒr|‘qS r   )rD   r   r   r   r   r   r#   Ñ   r$   z.TorchPrinter._print_MatMul.<locals>.<listcomp>c                    s   g | ]}|ˆ vr|‘qS r   r   r   ©Zmat_argsr   r   r#   Ò   r$   z%s*%sr   ztorch.matmul)r)   Zparenthesizer   Zfromiterr   r,   ©r   r-   r)   r   rW   r   Ú_print_MatMulÏ   s    
þzTorchPrinter._print_MatMulc                 C   s   |   d|jg|j ¡S )Nztorch.mm)r,   rU   rT   r3   r   r   r   Ú_print_MatPowÜ   s    zTorchPrinter._print_MatPowc                    sh   dd  ‡ fdd„| ¡ D ƒ¡ d }t|ƒg}| dˆ j› ¡ ˆ jrP| d¡ d ˆ  d	¡d  |¡¡S )
Nú[r5   c                    s,   g | ]$}d d  ‡ fdd„|D ƒ¡ d ‘qS )r[   r5   c                    s   g | ]}ˆ   |¡‘qS r   r   )r    Újr"   r   r   r#   à   r$   z=TorchPrinter._print_MatrixBase.<locals>.<listcomp>.<listcomp>ú])r<   r8   r"   r   r   r#   à   r$   z2TorchPrinter._print_MatrixBase.<locals>.<listcomp>r]   zdtype=zrequires_grad=Truer1   ztorch.tensor)r<   Útolistr7   Úappendr   r   r2   r+   )r   r-   ÚdataÚparamsr   r"   r   Ú_print_MatrixBaseß   s    $

þzTorchPrinter._print_MatrixBasec                 C   s   d|   |jd ¡› dS )Nztorch.isnan(r   ú)©r   r)   r3   r   r   r   Ú_print_isnanë   s    zTorchPrinter._print_isnanc                 C   s   d|   |jd ¡› dS )Nztorch.isinf(r   rc   rd   r3   r   r   r   Ú_print_isinfî   s    zTorchPrinter._print_isinfc                 C   sb   t dd„ |jD ƒƒr2d |  d¡|  |jd ¡¡S d |  d¡|  |jd ¡|  |jd ¡¡S d S )Nc                 s   s   | ]}|j V  qd S r?   )Z
is_Integer)r    Zdimr   r   r   r@   ò   r$   z/TorchPrinter._print_Identity.<locals>.<genexpr>r1   z	torch.eyer   rS   r%   )Úallr;   r2   r+   r   r3   r   r   r   Ú_print_Identityñ   s    þýzTorchPrinter._print_Identityc                 C   s   d  |  d¡|  |j¡¡S )Nr1   ztorch.zeros©r2   r+   r   r;   r3   r   r   r   Ú_print_ZeroMatrixÿ   s    
þzTorchPrinter._print_ZeroMatrixc                 C   s   d  |  d¡|  |j¡¡S )Nr1   z
torch.onesri   r3   r   r   r   Ú_print_OneMatrix  s    
þzTorchPrinter._print_OneMatrixc                 C   s"   |   d¡› d|  |jd ¡› dS )Nz
torch.conjú(r   rc   ©r+   r   r)   r3   r   r   r   Ú_print_conjugate  s    zTorchPrinter._print_conjugatec                 C   s   dS )NZ1jr   r3   r   r   r   Ú_print_ImaginaryUnit  s    z!TorchPrinter._print_ImaginaryUnitc                 C   sZ   |   |jd ¡dg}t|jƒdkr6|   |jd ¡|d< |  d¡› d|d › d|d › dS )Nr   z0.5r%   ztorch.heavisiderl   r5   rc   )r   r)   r*   r+   rX   r   r   r   Ú_print_Heaviside  s    zTorchPrinter._print_Heavisidec                 C   s"   |   d¡› d|  |jd ¡› dS )Nztorch.special.gammarl   r   rc   rm   r3   r   r   r   Ú_print_gamma  s    zTorchPrinter._print_gammac                 C   s>   |j d tjkr2|  d¡› d|  |j d ¡› dS tdƒ‚d S )Nr   ztorch.special.digammarl   r%   rc   z3PyTorch only supports digamma (0th order polygamma))r)   r	   ZZeror+   r   rH   r3   r   r   r   Ú_print_polygamma  s    "zTorchPrinter._print_polygammar   ZeinsumÚaddÚtZonesÚzeros)N)XÚ__name__Ú
__module__Ú__qualname__ZprintmethodrP   ZAbsÚsignZceilingÚfloorÚlogrT   r   ÚcosÚacosÚsinÚasinÚtanÚatanÚatan2ÚcoshÚacoshÚsinhÚasinhÚtanhÚatanhÚPowÚreZimr!   ÚerfZloggammaÚEqZNeZStrictGreaterThanZStrictLessThanZLessThanZGreaterThanÚAndÚOrÚNotZMaxZMinZMatAddZHadamardProductZTraceZDeterminantr&   Údictr   Z_default_settingsr   r0   Z_print_ExprZ_print_ApplicationZ_print_MatrixExprZ_print_RelationalZ
_print_NotZ
_print_AndZ	_print_OrZ_print_HadamardProductZ_print_TraceZ_print_Determinantr4   r>   rA   rN   rR   rV   rY   rZ   rb   re   rf   rh   rj   rk   rn   ro   rp   rq   rr   Ú_moduleZ_einsumZ_addZ
_transposeZ_onesZ_zerosÚ__classcell__r   r   r   r   r      s¬   Ë8ü
'r   Fr   c                 K   s"   t ||dœd}|j| fi |¤ŽS )N)r   r   )r   )r   Zdoprint)r-   r   r   r   Úprinterr   r   r   Ú
torch_code'  s    r”   )Fr   )Zsympy.printing.pycoder   r   Zsympy.matrices.expressionsr   Zsympy.core.mulr   Zsympy.printing.precedencer   Zsympy.externalr   Zsympy.codegen.cfunctionsr   rP   r	   r
   r   r   r”   r   r   r   r   Ú<module>   s     