o
    Rh|)                     @   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__ X/home/air/sanwanet/backup_V2/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rt|
dkr|
\}}t|tst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     