o
    §ƒðj|)  ã                   @   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                       s\  e Zd ZdZi 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“i 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 )`ÚTorchPrinterÚ
_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__Ú	_settingsr   r   r   Ú__version__r   )ÚselfÚsettingsÚversion©Ú	__class__© ú˜/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/printing/pytorch.pyr   S   s   

zTorchPrinter.__init__c                    sh   ˆ j  t|ƒd ¡}|d u rtƒ  |¡S ‡ fdd„|jD ƒ}t|ƒdkr.dˆ  |¡|d f S ˆ  ||¡S )Nc                    ó   g | ]}ˆ   |¡‘qS r   ©Ú_print©Ú.0Úarg©r   r   r   Ú
<listcomp>b   ó    z0TorchPrinter._print_Function.<locals>.<listcomp>é   z%s(%s)r   )	ÚmappingÚgetÚtyper   Ú_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   r1   r   r   r   Ú_print_Inversew   s   ÿzTorchPrinter._print_Inversec              	   C   sn   |j jr|j jd |j jd krd d|  |j ¡¡S d |  |j ¡d dd„ tt|j jƒƒD ƒ¡d d d	… ¡S )
Nr   r(   z
{}({}).t()ztorch.transposez{}.permute({})ú, c                 S   s   g | ]}t |ƒ‘qS r   ©Ústr©r#   Úir   r   r   r&   „   s    z1TorchPrinter._print_Transpose.<locals>.<listcomp>éÿÿÿÿ)r$   Ú	is_MatrixÚshaper6   r!   ÚjoinÚranger.   r7   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)r9   c                 s   s   � | ]}t |ƒV  qd S ©Nr:   r<   r   r   r   Ú	<genexpr>Š   s   € z2TorchPrinter._print_PermuteDims.<locals>.<genexpr>)r!   r1   rA   ÚpermutationÚ
array_formr7   r   r   r   Ú_print_PermuteDims‡   s   
þzTorchPrinter._print_PermuteDimsc                 C   sb  |j }|j}t|ƒdks"t|ƒdkrgt|d tƒsg| |d ¡dkrg|  |¡}i }|D ] }t|tƒrA|\}}| |d¡| ||< q+| |d¡d ||< q+| ¡ D ]\}}t	|ƒD ]}	d 
||  |¡¡}qXqP|S t|ƒdkr¬|d }
t|
tƒr t|
ƒdkr |
\}}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])Ú	variablesr1   r.   Ú
isinstanceÚtupleÚcountr!   r*   ÚitemsrB   r6   r
   ÚNotImplementedError)r   r1   rJ   Úexpr_argÚresultÚ
var_groupsÚvarÚbase_varÚorderÚ_Úvariabler   r   r   Ú_print_Derivative�   s6   ,

ÿ

zTorchPrinter._print_Derivativec                 C   s‚   ddl m} |jd j\}}t|jƒdkr&d |  d¡|  |¡|  |¡d¡S d |  d¡|  |¡|  |¡|  ||jdd … Ž ¡¡S )Nr   )Ú	Piecewiser(   z{}({}, {}, {})ztorch.where)ÚsympyrY   r-   r.   r6   r/   r!   )r   r1   rY   ÚeÚcondr   r   r   Ú_print_Piecewise´   s   üüzTorchPrinter._print_Piecewisec                 C   sN   |j \}}|jtjkrd |  d¡|  |¡¡S d |  d¡|  |¡|  |¡¡S )Nr5   r   ú
{}({}, {})r   )r-   Úexpr	   ÚHalfr6   r/   r!   )r   r1   Úbaser_   r   r   r   Ú
_print_PowÄ   s   
ÿþzTorchPrinter._print_Powc                    sZ   dd„ |j D ƒ‰ ‡ fdd„|j D ƒ}|r'd|  t |¡td ¡|  dˆ ¡f S |  dˆ ¡S )Nc                 S   s   g | ]	}t |tƒr|‘qS r   )rK   r   r"   r   r   r   r&   Ñ   s    z.TorchPrinter._print_MatMul.<locals>.<listcomp>c                    s   g | ]}|ˆ vr|‘qS r   r   r"   ©Úmat_argsr   r   r&   Ò   s    z%s*%sr   ztorch.matmul)r-   Úparenthesizer   Úfromiterr   r0   ©r   r1   r-   r   rc   r   Ú_print_MatMulÏ   s   
þzTorchPrinter._print_MatMulc                 C   s   |   d|jg|j ¡S )Nztorch.mm)r0   ra   r_   r7   r   r   r   Ú_print_MatPowÜ   s   zTorchPrinter._print_MatPowc                    sh   dd  ‡ fdd„| ¡ D ƒ¡ d }t|ƒg}| dˆ j› �¡ ˆ jr(| d¡ d ˆ  d	¡d  |¡¡S )
Nú[r9   c                    s,   g | ]}d d  ‡ fdd„|D ƒ¡ d ‘qS )rj   r9   c                    r   r   r    )r#   Újr%   r   r   r&   à   r'   z=TorchPrinter._print_MatrixBase.<locals>.<listcomp>.<listcomp>ú])rA   r<   r%   r   r   r&   à   s   , z2TorchPrinter._print_MatrixBase.<locals>.<listcomp>rl   zdtype=zrequires_grad=Truer5   ztorch.tensor)rA   Útolistr;   Úappendr   r   r6   r/   )r   r1   ÚdataÚparamsr   r%   r   Ú_print_MatrixBaseß   s   $

þzTorchPrinter._print_MatrixBasec                 C   ó   d|   |jd ¡› d�S )Nztorch.isnan(r   ú)©r!   r-   r7   r   r   r   Ú_print_isnanë   ó   zTorchPrinter._print_isnanc                 C   rr   )Nztorch.isinf(r   rs   rt   r7   r   r   r   Ú_print_isinfî   rv   zTorchPrinter._print_isinfc                 C   s^   t dd„ |jD ƒƒrd |  d¡|  |jd ¡¡S d |  d¡|  |jd ¡|  |jd ¡¡S )Nc                 s   s   � | ]}|j V  qd S rD   )Ú
is_Integer)r#   Údimr   r   r   rE   ò   s   € z/TorchPrinter._print_Identity.<locals>.<genexpr>r5   z	torch.eyer   r^   r(   )Úallr@   r6   r/   r!   r7   r   r   r   Ú_print_Identityñ   s   þýzTorchPrinter._print_Identityc                 C   ó   d  |  d¡|  |j¡¡S )Nr5   ztorch.zeros©r6   r/   r!   r@   r7   r   r   r   Ú_print_ZeroMatrixÿ   ó   
þzTorchPrinter._print_ZeroMatrixc                 C   r|   )Nr5   z
torch.onesr}   r7   r   r   r   Ú_print_OneMatrix  r   zTorchPrinter._print_OneMatrixc                 C   ó"   |   d¡› d|  |jd ¡› d�S )Nz
torch.conjú(r   rs   ©r/   r!   r-   r7   r   r   r   Ú_print_conjugate  ó   "zTorchPrinter._print_conjugatec                 C   s   dS )NÚ1jr   r7   r   r   r   Ú_print_ImaginaryUnit  s   z!TorchPrinter._print_ImaginaryUnitc                 C   sZ   |   |jd ¡dg}t|jƒdkr|   |jd ¡|d< |  d¡› d|d › d|d › d�S )Nr   z0.5r(   ztorch.heavisider‚   r9   rs   )r!   r-   r.   r/   rg   r   r   r   Ú_print_Heaviside  s   $zTorchPrinter._print_Heavisidec                 C   r�   )Nztorch.special.gammar‚   r   rs   rƒ   r7   r   r   r   Ú_print_gamma  r…   zTorchPrinter._print_gammac                 C   s:   |j d tjkr|  d¡› d|  |j d ¡› d�S tdƒ‚)Nr   ztorch.special.digammar‚   r(   rs   z3PyTorch only supports digamma (0th order polygamma))r-   r	   ÚZeror/   r!   rO   r7   r   r   r   Ú_print_polygamma  s   "zTorchPrinter._print_polygammar   ÚeinsumÚaddÚtÚonesÚzerosrD   )XÚ__name__Ú
__module__Ú__qualname__ÚprintmethodrZ   ÚAbsÚsignÚceilingÚfloorÚlogr_   r   ÚcosÚacosÚsinÚasinÚtanÚatanÚatan2ÚcoshÚacoshÚsinhÚasinhÚtanhÚatanhÚPowÚreÚimr$   ÚerfÚloggammaÚEqÚNeÚStrictGreaterThanÚStrictLessThanÚLessThanÚGreaterThanÚAndÚOrÚNotÚMaxÚMinÚMatAddÚHadamardProductÚTraceÚDeterminantr)   Údictr   Ú_default_settingsr   r4   Ú_print_ExprÚ_print_ApplicationÚ_print_MatrixExprÚ_print_RelationalÚ
_print_NotÚ
_print_AndÚ	_print_OrÚ_print_HadamardProductÚ_print_TraceÚ_print_Determinantr8   rC   rH   rX   r]   rb   rh   ri   rq   ru   rw   r{   r~   r€   r„   r‡   rˆ   r‰   r‹   Ú_moduleÚ_einsumÚ_addÚ
_transposeÚ_onesÚ_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   Údoprint)r1   r   r   r   Úprinterr   r   r   Ú
torch_code'  s   rÐ   )Fr   )Úsympy.printing.pycoder   r   Úsympy.matrices.expressionsr   Úsympy.core.mulr   Úsympy.printing.precedencer   Úsympy.externalr   Úsympy.codegen.cfunctionsr   rZ   r	   r
   r   r   rÐ   r   r   r   r   Ú<module>   s     