o
    §ƒðj_Q  ã                   @  sà  d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	m
Z
mZmZmZmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ G dd„ deƒZG dd„ deƒZ eefee fdœZ!i dd“dd“dd“ddd„ ddfdd„ ddfdd„ ddfdd„ d dfd!d„ d"d#fd$d„ d%d&fd'd„ d(d&fg“d)d*d„ d)dfg“d+d,“d-d.“d/d0“d1d1“d2d2“d3d3“d4d4“d5d5“d6d6“d7d7“d8d8“d9d9“d:d;d<d=dd>œ¥Z"g d?¢Z#G d@dA„ dAeƒZ$G dBdC„ dCeƒZ%dS )Da  
Rust code printer

The `RustCodePrinter` converts SymPy expressions into Rust expressions.

A complete code generator, which uses `rust_code` extensively, can be found
in `sympy.utilities.codegen`. The `codegen` module can be used to generate
complete source code files.

é    )Úannotations)ÚreduceN)ÚAny)Úfloat32Úfloat64Úint32ÚrealÚintegerÚbool_)ÚSÚRationalÚFloatÚLambda)ÚExpr©Úequal_valued)ÚceilingÚfloor)ÚCodePrinter)Ú
PRECEDENCEc                   @  ó   e Zd ZdZdd„ ZdS )Úfloat_floorzi
    Same as `sympy.floor`, but mimics the Rust behavior of returning a float rather than an integer
    c                 C  ó   dS ©NF© ©Úselfr   r   ú•/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/printing/rust.pyÚ_eval_is_integerE   ó   zfloat_floor._eval_is_integerN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   A   ó    r   c                   @  r   )Úfloat_ceilingzk
    Same as `sympy.ceiling`, but mimics the Rust behavior of returning a float rather than an integer
    c                 C  r   r   r   r   r   r   r   r   L   r   zfloat_ceiling._eval_is_integerNr    r   r   r   r   r&   H   r%   r&   )r   r   r   ÚceilÚAbsÚabsÚPowc                 C  ó
   t |dƒS )Néÿÿÿÿr   ©ÚbaseÚexpr   r   r   Ú<lambda>f   ó   
 r0   Úrecipé   c                 C  r+   )Ng      à?r   r-   r   r   r   r0   g   r1   Úsqrtc                 C  r+   )Ng      à¿r   r-   r   r   r   r0   h   r1   zsqrt().recipc                 C  s   |t ddƒkS )Né   é   )r   r-   r   r   r   r0   i   s    Úcbrtc                 C  s
   t | dƒS )Nr3   r   r-   r   r   r   r0   j   r1   Úexp2r6   c                 C  ó   |j S ©N©Ú
is_integerr-   r   r   r   r0   k   s    Úpowir5   c                 C  s   |j  S r:   r;   r-   r   r   r   r0   l   s    Úpowfr/   c                 C  r   )NTr   )r/   r   r   r   r0   m   s    ÚlogÚlnÚMaxÚmaxÚMinÚminÚsinÚcosÚtanÚasinÚacosÚatanÚatan2ÚsinhÚcoshÚtanhÚasinhÚacoshÚatanh)rN   rO   rP   rQ   r4   )4ÚabstractÚasÚasyncÚawaitÚbecomeÚboxÚbreakÚconstÚcontinueÚcrateÚdoÚdynÚelseÚenumÚexternÚfalseÚfinalÚfnÚforÚgenÚifÚimplÚinÚletÚloopÚmacroÚmatchÚmodÚmoveÚmutÚoverrideÚprivÚpubÚrefÚreturnÚSelfr   ÚstaticÚstructÚsuperÚtraitÚtrueÚtryÚtypeÚtypeofÚunsafeÚunsizedÚuseÚvirtualÚwhereÚwhileÚyieldc                      sD   e Zd ZdZd‡ fdd„Zedd„ ƒZedd	„ ƒZddd„Z‡  Z	S )ÚTypeCastz9
    The type casting operator of the Rust language.
    rt   ÚNonec                   sD   t ƒ  ¡  |jo|tu| _|j| _| jr t| dtd d ƒ d S d S )NÚ
precedenceÚFuncé
   )rx   Ú__init__r<   r	   ÚexplicitÚ_assumptionsÚsetattrr   )r   ÚexprÚtype_©Ú	__class__r   r   rŠ   ý   s   
ÿzTypeCast.__init__c                 C  ó
   | j d S )Nr   ©Úargsr   r   r   r   rŽ     ó   
zTypeCast.exprc                 C  r’   )Nr5   r“   r   r   r   r   r�     r•   zTypeCast.type_Nc                 C  s   | j d j|d�S )Nr   )Úorder)r”   Úsort_key)r   r–   r   r   r   r—     s   zTypeCast.sort_key)rt   r†   r:   )
r!   r"   r#   r$   rŠ   ÚpropertyrŽ   r�   r—   Ú__classcell__r   r   r�   r   r…   ø   s    

r…   c                      sâ  e Zd ZU dZdZdZeeee	iZ
edede	dediZeejfi di d	eƒ d
œ¤ŽZded< i fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Z‡ fd!d"„Zdc‡ fd$d%„	Zd&d'„ Zd(d)„ Z dd‡ fd+d,„	Z!dd‡ fd-d.„	Z"d/d0„ Z#d1d2„ Z$d3d4„ Z%d5d6„ Z&d7d8„ Z'ddd9d:„Z(ddd;d<„Z)ddd=d>„Z*ddd?d@„Z+dddAdB„Z,dddCdD„Z-dddEdF„Z.dddGdH„Z/dIdJ„ Z0dKdL„ Z1dMdN„ Z2dOdP„ Z3dQdR„ Z4‡ fdSdT„Z5dUdV„ Z6dWdX„ Z7dYdZ„ Z8d[d\„ Z9d]d^„ Z:d_d`„ Z;dadb„ Z<‡  Z=S )eÚRustCodePrinterz>A printer to convert SymPy expressions to strings of Rust codeÚ
_rust_codeÚRustÚi32Úf32Úf64Úboolé   T)Ú	precisionÚuser_functionsÚcontractÚdereferencezdict[str, Any]Ú_default_settingsc                 C  sT   t  | |¡ ttƒ| _| di ¡}| j |¡ t| dg ¡ƒ| _ttƒ| _t	| _	d S )Nr£   r¥   )
r   rŠ   ÚdictÚknown_functionsÚgetÚupdateÚsetÚ_dereferenceÚreserved_wordsÚfunction_overrides)r   ÚsettingsÚ	userfuncsr   r   r   rŠ   (  s   


zRustCodePrinter.__init__c                 C  s   |d S )Né   r   )r   Úpr   r   r   Ú_rate_index_position1  ó   z$RustCodePrinter._rate_index_positionc                 C  ó   d| S )Nz%s;r   )r   Ú
codestringr   r   r   Ú_get_statement4  r´   zRustCodePrinter._get_statementc                 C  rµ   )Nz// %sr   )r   Útextr   r   r   Ú_get_comment7  r´   zRustCodePrinter._get_commentc                 C  s   | j | jt  }d|||f S )Nzconst %s: %s = %s;)Útype_mappingsÚtype_aliasesr   )r   ÚnameÚvaluer�   r   r   r   Ú_declare_number_const:  s   z%RustCodePrinter._declare_number_constc                 C  ó
   |   |¡S r:   )Úindent_code)r   Úlinesr   r   r   Ú_format_code>  s   
zRustCodePrinter._format_codec                   s    |j \}‰ ‡ fdd„t|ƒD ƒS )Nc                 3  s&   � | ]}t ˆ ƒD ]}||fV  qqd S r:   )Úrange)Ú.0ÚiÚj©Úcolsr   r   Ú	<genexpr>C  ó   €$ z;RustCodePrinter._traverse_matrix_indices.<locals>.<genexpr>)ÚshaperÃ   )r   ÚmatÚrowsr   rÇ   r   Ú_traverse_matrix_indicesA  s   
z(RustCodePrinter._traverse_matrix_indicesc              
   C  sX   g }g }d}|D ]}|  ||  |¡|  |j¡|  |jd ¡dœ ¡ |  d¡ q||fS )Nz#for %(var)s in %(start)s..%(end)s {r5   )ÚvarÚstartÚendÚ})ÚappendÚ_printÚlowerÚupper)r   ÚindicesÚ
open_linesÚclose_linesÚ	loopstartrÅ   r   r   r   Ú_get_loop_opening_endingE  s   

ýz(RustCodePrinter._get_loop_opening_endingc                 C  s>   t |jƒdkrd|  |¡ d S |jr| j|dd�S |  |¡S )Nr5   ú(ú)T)Ú_type)Úlenr”   rÔ   Ú	is_number©r   rŽ   r   r   r   Ú_print_caller_varR  s
   
z!RustCodePrinter._print_caller_varc                 C  sD  |j j| jv r‰| j|j j }d}d}t|tƒr|}n|D ]\}}}||jŽ r( nq|dur‡|dkrSd|  |jd ¡|t|jƒdkrL|  |jdd… d¡nddœ }|S |dkrfd	|  |jd ¡|d
œ }|S |dkryd	|  |jd ¡|d
œ }|S d||  |jd¡dœ }|S dS t	|dƒr�t|j
tƒr�|  |j
|jŽ ¡S |  |¡S )a<  
        basic function for printing `Function`

        Function Style :

        1. args[0].func(args[1:]), method with arguments
        2. args[0].func(), method without arguments
        3. args[1].func(), method without arguments (e.g. (e, x) => x.exp())
        4. func(args), function with arguments
        Nr5   z%(var)s.%(method)s(%(args)s)r   ú, Ú )rÏ   Úmethodr”   r3   z%(var)s.%(method)s())rÏ   rå   r6   z%(func)s(%(args)s))Úfuncr”   Ú_imp_)ræ   r!   r¨   Ú
isinstanceÚstrr”   râ   rß   Ú	stringifyÚhasattrrç   r   rÔ   Ú_print_not_supported)r   rŽ   Ú	cond_funcræ   ÚstyleÚcondÚretr   r   r   Ú_print_Function]  sN   

ÿ&ýñþöþ	üþê
zRustCodePrinter._print_Functionc                   s@   t dd„ |jD ƒƒ}|rttj‡ fdd„|jD ƒƒ}tƒ  |¡S )Nc                 s  ó   � | ]
}|j o
|j V  qd S r:   ©Úis_realr<   ©rÄ   Úargr   r   r   rÉ   ‘  ó   € z-RustCodePrinter._print_Mul.<locals>.<genexpr>c                 3  s&   � | ]}|d krˆ   |¡n|V  qdS )r,   N©Ú_cast_to_floatrõ   r   r   r   rÉ   “  rÊ   )Úanyr”   r   ÚoperatorÚmulrx   Ú
_print_Mul)r   rŽ   Úcontains_floatsr�   r   r   rý   �  s   zRustCodePrinter._print_MulNc                   sB   t dd„ |jD ƒƒ}|rttj‡ fdd„|jD ƒƒ}tƒ  ||¡S )Nc                 s  rò   r:   ró   rõ   r   r   r   rÉ   ˜  r÷   z-RustCodePrinter._print_Add.<locals>.<genexpr>c                 3  ó   � | ]}ˆ   |¡V  qd S r:   rø   rõ   r   r   r   rÉ   š  ó   € )rú   r”   r   rû   Úaddrx   Ú
_print_Add)r   rŽ   r–   rþ   r�   r   r   r  —  s   zRustCodePrinter._print_Addc                 C  s:   |j jr|jjst|ƒt|j ƒ|jƒ}|  |¡S |  |¡S r:   )r.   r<   r/   r|   r   rÔ   rñ   rá   r   r   r   Ú
_print_Powž  s   

zRustCodePrinter._print_Powc                 C  s4   |j s	|  |j¡S |  |j¡d| j| j|j    S )Nz as %s)r‹   rÔ   rŽ   rº   r»   r�   rá   r   r   r   Ú_print_TypeCast¤  s   "zRustCodePrinter._print_TypeCastFc                   ó,   t ƒ  |¡}|r|d| j| jt    S |S ©Nz_%s)rx   Ú_print_Floatrº   r»   r   ©r   rŽ   rÞ   rð   r�   r   r   r  ª  ó   zRustCodePrinter._print_Floatc                   r  r  )rx   Ú_print_Integerrº   r»   r	   r  r�   r   r   r
  ±  r	  zRustCodePrinter._print_Integerc                 C  s4   t |jƒt |jƒ}}| j| jt  }d|||f S )Nz
%d_%s/%d.0)Úintr²   Úqrº   r»   r   )r   rŽ   r²   r  Úfloat_suffixr   r   r   Ú_print_Rational¸  s   zRustCodePrinter._print_Rationalc                 C  sn   |j jr|jjr|jjr|j js|  |j ¡}|  |j¡}n|j }|j}|  |¡}|  |¡}|j}d |||¡S )Nz{} {} {})Úlhsr<   Úrhsrù   rÔ   Úrel_opÚformat)r   rŽ   r  r  Úlhs_codeÚrhs_codeÚopr   r   r   Ú_print_Relational½  s    

z!RustCodePrinter._print_Relationalc                 C  s`   |j }tj}tj}tt|jƒƒD ]}||j| | 7 }||| 9 }qd|  |j	j
¡|  |¡f S )Nú%s[%s])rË   r   ÚZeroÚOneÚreversedrÃ   Úrankr×   rÔ   r.   Úlabel)r   rŽ   ÚdimsÚelemÚoffsetrÅ   r   r   r   Ú_print_IndexedÉ  s   zRustCodePrinter._print_Indexedc                 C  s   |j jS r:   )r  r¼   rá   r   r   r   Ú
_print_IdxÓ  r´   zRustCodePrinter._print_Idxc                 C  r9   r:   )r¼   rá   r   r   r   Ú_print_DummyÖ  s   zRustCodePrinter._print_Dummyc                 C  r   )NÚEr   ©r   rŽ   rÞ   r   r   r   Ú_print_Exp1Ù  r   zRustCodePrinter._print_Exp1c                 C  r   )NÚPIr   r$  r   r   r   Ú	_print_PiÜ  r   zRustCodePrinter._print_Pic                 C  r   )NÚINFINITYr   r$  r   r   r   Ú_print_Infinityß  r   zRustCodePrinter._print_Infinityc                 C  r   )NÚNEG_INFINITYr   r$  r   r   r   Ú_print_NegativeInfinityâ  r   z'RustCodePrinter._print_NegativeInfinityc                 C  r   )Nrz   r   r$  r   r   r   Ú_print_BooleanTrueå  r   z"RustCodePrinter._print_BooleanTruec                 C  r   )Nra   r   r$  r   r   r   Ú_print_BooleanFalseè  r   z#RustCodePrinter._print_BooleanFalsec                 C  s   t |ƒ ¡ S r:   )ré   rÕ   r$  r   r   r   Ú_print_boolë  s   zRustCodePrinter._print_boolc                 C  r   )NÚNANr   r$  r   r   r   Ú
_print_NaNî  r   zRustCodePrinter._print_NaNc                 C  sÔ   |j d jdkrtdƒ‚g }t|j ƒD ]G\}\}}|dkr(| d|  |¡ ¡ n#|t|j ƒd kr>|dkr>|d  d7  < n|d  d|  |¡ 7  < |  |¡}| |¡ | d	¡ q| jd
 red |¡S d |¡S )Nr,   Tz¼All Piecewise expressions must contain an (expr, True) statement to be used as a default condition. Without one, the generated expression may not evaluate to anything under some condition.r   z	if (%s) {r5   z else {z else if (%s) {rÒ   Úinlineú Ú
)	r”   rï   Ú
ValueErrorÚ	enumeraterÓ   rÔ   rß   Ú	_settingsÚjoin)r   rŽ   rÁ   rÅ   ÚeÚcÚcode0r   r   r   Ú_print_Piecewiseñ  s   




z RustCodePrinter._print_Piecewisec                 C  s    ddl m} |  |j|dd�¡S )Nr   )Ú	PiecewiseF)Údeep)Úsympy.functionsr<  rÔ   Úrewrite)r   rŽ   r<  r   r   r   Ú
_print_ITE  s   zRustCodePrinter._print_ITEc                   s.   |j dkrdd ‡ fdd„|D ƒ¡ S tdƒ‚)Nr5   z[%s]rã   c                 3  rÿ   r:   )rÔ   )rÄ   Úar   r   r   rÉ     r   z4RustCodePrinter._print_MatrixBase.<locals>.<genexpr>zLFull Matrix Support in Rust need Crates (https://crates.io/keywords/matrix).)rÈ   r7  r4  )r   ÚAr   r   r   Ú_print_MatrixBase  s   
z!RustCodePrinter._print_MatrixBasec                 C  r¿   r:   )rì   )r   rÌ   r   r   r   Ú_print_SparseRepMatrix  r•   z&RustCodePrinter._print_SparseRepMatrixc                 C  s"   d|j |j|j|j jd   f S )Nr  r5   )ÚparentrÆ   rÅ   rË   rá   r   r   r   Ú_print_MatrixElement  s   ÿz$RustCodePrinter._print_MatrixElementc                   s"   t ƒ  |¡}|| jv rd| S |S )Nz(*%s))rx   Ú_print_Symbolr¬   )r   rŽ   r¼   r�   r   r   rG    s   
zRustCodePrinter._print_Symbolc                 C  sh   ddl m} |j}|j}| jd r!| |¡s| |¡r!|  ||¡S |  |¡}|  |¡}|  d||f ¡S )Nr   )ÚIndexedBaser¤   z%s = %s)	Úsympy.tensor.indexedrH  r  r  r6  ÚhasÚ_doprint_loopsrÔ   r·   )r   rŽ   rH  r  r  r  r  r   r   r   Ú_print_Assignment'  s   ÿ

z!RustCodePrinter._print_Assignmentc                 C  s   |   |jd ¡}d||f S )Nr   z/(if (%s == 0.0) { 0.0 } else { (%s).signum() }))rÔ   r”   )r   rŽ   rö   r   r   r   Ú_print_sign5  s   zRustCodePrinter._print_signc                 C  s"   |j st|tƒS |jrt|ƒS |S r:   )rà   r…   r   r<   r   rá   r   r   r   rù   9  s
   
zRustCodePrinter._cast_to_floatc                 C  s$   || j v p|tv pt| d |¡dƒS )zŠ Check if function ``name`` is either a known function or has its own
            printing method. Used to check if rewriting is possible.z	_print_{}F)r¨   r®   Úgetattrr  )r   r¼   r   r   r   Ú
_can_print@  s   $zRustCodePrinter._can_printc                 C  sB   t ƒ }t|tƒr|jr| |j¡ |jD ]
}| |  |¡¡}q|S r:   )	r«   rè   r   Úis_Functionr  ræ   r”   ÚunionÚ_collect_functions)r   rŽ   Ú	functionsrö   r   r   r   rR  I  s   

z"RustCodePrinter._collect_functionsc                   s^   t |tƒs|S ˆ  |¡}‡ fdd„ˆ j ¡ D ƒ}|D ]}| |jd¡\}}|r,| |¡}q|S )Nc                   s>   i | ]\}\}}ˆ   |¡rt‡ fd d„|D ƒƒr|||f“qS )c                 3  rÿ   r:   )rO  )rÄ   Úfr   r   r   rÉ   [  r   zFRustCodePrinter._rewrite_known_functions.<locals>.<dictcomp>.<genexpr>)rO  Úall)rÄ   r¼   Útarget_fÚrequired_fsr   r   r   Ú
<dictcomp>W  s    
ýüÿz<RustCodePrinter._rewrite_known_functions.<locals>.<dictcomp>)NN)rè   r   rR  Ú_rewriteable_functionsÚitemsr©   r!   r?  )r   rŽ   Úexpression_functionsÚrewriteable_functionsræ   rV  Ú_r   r   r   Ú_rewrite_known_functionsR  s   


þ
€z(RustCodePrinter._rewrite_known_functionsc           
        sÂ   t |tƒr|  | d¡¡}d |¡S d}d‰d‰ dd„ |D ƒ}‡fdd„|D ƒ}‡ fd	d„|D ƒ}g }d
}t|ƒD ]%\}}	|	dv rG| |	¡ q9||| 8 }| d|| |	f ¡ ||| 7 }q9|S )z0Accepts a string of code or a list of code linesTrä   z    )Ú{rÜ   z{
z(
)rÒ   rÝ   c                 S  s   g | ]}|  d ¡‘qS )z 	)Úlstrip©rÄ   Úliner   r   r   Ú
<listcomp>n  s    z/RustCodePrinter.indent_code.<locals>.<listcomp>c                   ó    g | ]}t tt|jˆ ƒƒƒ‘qS r   )r  rú   ÚmapÚendswithra  )Ú	inc_tokenr   r   rc  p  s     c                   rd  r   )r  rú   re  Ú
startswithra  )Ú	dec_tokenr   r   rc  q  s    ÿr   )rä   r3  z%s%s)rè   ré   rÀ   Ú
splitlinesr7  r5  rÓ   )
r   ÚcodeÚ
code_linesÚtabÚincreaseÚdecreaseÚprettyÚlevelÚnrb  r   )ri  rg  r   rÀ   c  s*   


ÿ
zRustCodePrinter.indent_coder:   )F)>r!   r"   r#   r$   ÚprintmethodÚlanguager	   r   r   r   r»   r   r
   rº   r§   r   r¦   r«   Ú__annotations__rŠ   r³   r·   r¹   r¾   rÂ   rÎ   rÛ   râ   rñ   rý   r  r  r  r  r
  r  r  r   r!  r"  r%  r'  r)  r+  r,  r-  r.  r0  r;  r@  rC  rD  rF  rG  rL  rM  rù   rO  rR  r^  rÀ   r™   r   r   r�   r   rš     sx   
 þü
ü	3








			rš   )&r$   Ú
__future__r   Ú	functoolsr   rû   Útypingr   Úsympy.codegen.astr   r   r   r   r	   r
   Ú
sympy.corer   r   r   r   Úsympy.core.exprr   Úsympy.core.numbersr   Ú#sympy.functions.elementary.integersr   r   Úsympy.printing.codeprinterr   Úsympy.printing.precedencer   r   r&   r®   r¨   r­   r…   rš   r   r   r   r   Ú<module>   sˆ    ! 	þúùõúðéèâá!ß"Þ#Ý$Ü%Û&Ú'Ù+Õ,Ô-Ïl6