
    \h_Q                       S r SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	J
r
JrJrJrJr  SSKJrJrJrJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr   " S S\5      r " S S\5      r \\4\\ 4S.r!0 SS_SS_SS_SS SS4S SS4S SS4S SS4S  S!S"4S# S$S%4S& S'S%4/_S(S) S(S4/_S*S+_S,S-_S.S/_S0S0_S1S1_S2S2_S3S3_S4S4_S5S5_S6S6_S7S7_S8S8_S9S:S;S<SS=.Er"/ S>Qr# " S? S@\5      r$ " SA SB\5      r%g)Ca  
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                      \ rS rSrSrS rSrg)float_floorA   za
Same as `sympy.floor`, but mimics the Rust behavior of returning a float rather than an integer
c                    gNF selfs    K/var/www/auris/envauris/lib/python3.13/site-packages/sympy/printing/rust.py_eval_is_integerfloat_floor._eval_is_integerE           r   N__name__
__module____qualname____firstlineno____doc__r    __static_attributes__r   r#   r   r   r   A       r#   r   c                      \ rS rSrSrS rSrg)float_ceilingH   zc
Same as `sympy.ceiling`, but mimics the Rust behavior of returning a float rather than an integer
c                    gr   r   r   s    r   r    float_ceiling._eval_is_integerL   r"   r#   r   Nr$   r   r#   r   r-   r-   H   r+   r#   r-   )r   r   r   ceilAbsabsPowc                    [        US5      $ )Nr   baseexps     r   <lambda>r:   f   s    |C4r#   recip   c                    [        US5      $ )Ng      ?r   r7   s     r   r:   r:   g   s    |C5r#   sqrtc                    [        US5      $ )Ng      r   r7   s     r   r:   r:   h   s    |C6r#   zsqrt().recipc                     U[        SS5      :H  $ )N      )r   r7   s     r   r:   r:   i   s    shq!n4r#   cbrtc                    [        U S5      $ )Nr<   r   r7   s     r   r:   r:   j   s    |D!4r#   exp2rB   c                    UR                   $ N
is_integerr7   s     r   r:   r:   k   s    s~~r#   powirA   c                $    UR                   (       + $ rG   rH   r7   s     r   r:   r:   l   s    3>>1r#   powfr9   c                    g)NTr   )r9   s    r   r:   r:   m   s    r#   loglnMaxmaxMinminsincostanasinacosatanatan2sinhcoshtanhasinhacoshatanh)r]   r^   r_   r`   r>   )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                  Z   ^  \ rS rSrSrSU 4S jjr\S 5       r\S 5       rS	S jr	Sr
U =r$ )
TypeCast   z1
The type casting operator of the Rust language.
c                   > [         TU ]  5         UR                  =(       a    U[        LU l        UR
                  U l        U R                  (       a  [        U S[        S   S-   5        g g )N
precedenceFunc
   )r   __init__rI   r
   explicit_assumptionssetattrr   )r   exprtype_	__class__s      r   r   TypeCast.__init__   sS    @E,@ --==D,
6(:R(?@ r#   c                     U R                   S   $ )Nr   argsr   s    r   r   TypeCast.expr      yy|r#   c                     U R                   S   $ )NrA   r   r   s    r   r   TypeCast.type_  r   r#   c                :    U R                   S   R                  US9$ )Nr   )order)r   sort_key)r   r   s     r   r   TypeCast.sort_key  s    yy|$$5$11r#   )r   r   )r   NonerG   )r%   r&   r'   r(   r)   r   propertyr   r   r   r*   __classcell__r   s   @r   r   r      s@    A    2 2r#   r   c            	        ^  \ rS rSr% SrSrSr\\\	\
0r\S\S\
S\S0r\" \R"                  40 S	0 S
\" 5       S.D6rS\S'   0 4S jrS rS rS rS rS rS rS rS rS rU 4S jrS9U 4S jjrS r S r!S:U 4S jjr"S:U 4S jjr#S r$S r%S  r&S! r'S" r(S:S# jr)S:S$ jr*S:S% jr+S:S& jr,S:S' jr-S:S( jr.S:S) jr/S:S* jr0S+ r1S, r2S- r3S. r4S/ r5U 4S0 jr6S1 r7S2 r8S3 r9S4 r:S5 r;S6 r<S7 r=S8r>U =r?$ );RustCodePrinteri  z>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                0   [         R                  " X5        [        [        5      U l        UR	                  S0 5      nU R                  R                  U5        [        UR	                  S/ 5      5      U l        [        [        5      U l        [        U l	        g )Nr   r   )
r   r   dictknown_functionsgetupdateset_dereferencereserved_wordsfunction_overrides)r   settings	userfuncss      r   r   RustCodePrinter.__init__(  so    T,#O4LL!126	##I.]B ?@!.1"4r#   c                    US-  $ )N   r   )r   ps     r   _rate_index_position$RustCodePrinter._rate_index_position1  s    s
r#   c                    SU-  $ )Nz%s;r   )r   
codestrings     r   _get_statementRustCodePrinter._get_statement4  s    z!!r#   c                    SU-  $ )Nz// %sr   )r   texts     r   _get_commentRustCodePrinter._get_comment7  s    ~r#   c                `    U R                   U R                  [              nSU< SU< SU< S3$ )Nzconst z:  = ;)type_mappingstype_aliasesr	   )r   namevaluer   s       r   _declare_number_const%RustCodePrinter._declare_number_const:  s*    ""4#4#4T#:;'+UE::r#   c                $    U R                  U5      $ rG   )indent_code)r   liness     r   _format_codeRustCodePrinter._format_code>  s    &&r#   c                L   ^ UR                   u  nmU4S j[        U5       5       $ )Nc              3  P   >#    U  H  n[        T5        H  o!U4v   M
     M     g 7frG   )range).0ijcolss      r   	<genexpr>;RustCodePrinter._traverse_matrix_indices.<locals>.<genexpr>C  s     A1U4[A[s   #&)shaper   )r   matrowsr   s      @r   _traverse_matrix_indices(RustCodePrinter._traverse_matrix_indicesA  s    YY
dAdAAr#   c           
         / n/ nSnU Hp  nUR                  UU R                  U5      U R                  UR                  5      U R                  UR                  S-   5      S.-  5        UR                  S5        Mr     X#4$ )Nz#for %(var)s in %(start)s..%(end)s {rA   )varstartend})append_printlowerupper)r   indices
open_linesclose_lines	loopstartr   s         r   _get_loop_opening_ending(RustCodePrinter._get_loop_opening_endingE  s    
9	Ai{{1~QWW-{{177Q;/+1 1 2 s#  &&r#   c                    [        UR                  5      S:  a  SU R                  U5      -   S-   $ UR                  (       a  U R                  USS9$ U R                  U5      $ )NrA   ()T)_type)lenr   r   	is_numberr   r   s     r   _print_caller_var!RustCodePrinter._print_caller_varR  sV    tyy>A T**S00^^;;t4;00;;t$$r#   c                   UR                   R                  U R                  ;   GaF  U R                  UR                   R                     nSnSn[        U[        5      (       a  UnO"U H  u  pSnU" UR
                  6 (       d  M    O   Ub  US:X  a_  SU R                  UR
                  S   5      U[        UR
                  5      S:  a  U R                  UR
                  SS S5      OSS.-  nU$ US:X  a&  S	U R                  UR
                  S   5      US
.-  nU$ US:X  a&  S	U R                  UR
                  S   5      US
.-  nU$ SUU R                  UR
                  S5      S.-  nU$ g[        US5      (       aG  [        UR                  [        5      (       a(  U R                  UR                  " UR
                  6 5      $ U R                  U5      $ )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
NrA   z%(var)s.%(method)s(%(args)s)r   ,  )r   methodr   r<   z%(var)s.%(method)s())r   r  rB   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rets          r   _print_FunctionRustCodePrinter._print_Function]  s    99!5!55,,TYY-?-?@IDE)S)) )2%DTYY'' *3 A:8#55diilC"&GJ499~XYGYtyy}d C_a< C( 
 aZ0#55diilC"&4 C 
 aZ0#55diilC"&4 C 
	 / $ $tyy$ ?2 C 
-  . T7##
4::v(F(F;;tzz499566,,T22r#   c                   >^  [        S UR                   5       5      nU(       a.  [        [        R                  U 4S jUR                   5       5      n[
        TT ]  U5      $ )Nc              3  h   #    U  H(  oR                   =(       a    UR                  (       + v   M*     g 7frG   is_realrI   r   args     r   r   -RustCodePrinter._print_Mul.<locals>.<genexpr>  !     VISkk@#...@@I   02c              3  T   >#    U  H  oS :w  a  TR                  U5      OUv   M     g7f)r6   N_cast_to_floatr   r#  r   s     r   r   r$    s)     'lbk[^2I(;(;C(@SV(Vbks   %()anyr   r   operatormulr   
_print_Mul)r   r   contains_floatsr   s   `  r   r.  RustCodePrinter._print_Mul  sG    VDIIVV(,,'lbfbkbk'lmDw!$''r#   c                   >^  [        S UR                   5       5      nU(       a.  [        [        R                  U 4S jUR                   5       5      n[
        TT ]  X5      $ )Nc              3  h   #    U  H(  oR                   =(       a    UR                  (       + v   M*     g 7frG   r   r"  s     r   r   -RustCodePrinter._print_Add.<locals>.<genexpr>  r%  r&  c              3  F   >#    U  H  nTR                  U5      v   M     g 7frG   r(  r*  s     r   r   r3    s     (WYc)<)<S)A)AY   !)r+  r   r   r,  addr   
_print_Add)r   r   r   r/  r   s   `   r   r7  RustCodePrinter._print_Add  sD    VDIIVV(,,(WTYY(WXDw!$..r#   c                   UR                   R                  (       a[  UR                  R                  (       d@  [        U5      " [	        UR                   5      UR                  5      nU R                  U5      $ U R                  U5      $ rG   )r8   rI   r9   r   r   r   r  r
  s     r   
_print_PowRustCodePrinter._print_Pow  sX    99(;(;:eDII.9D;;t$$##D))r#   c                    UR                   (       d  U R                  UR                  5      $ U R                  UR                  5      SU R                  U R                  UR
                        -  -   $ )Nz as %s)r   r   r   r   r   r   r
  s     r   _print_TypeCastRustCodePrinter._print_TypeCast  sV    }};;tyy));;tyy)Ht7I7I$J[J[\`\f\fJg7h,hhhr#   c                   > [         TU ]  U5      nU(       a&  USU R                  U R                  [              -  -   $ U$ Nz_%s)r   _print_Floatr   r   r	   r   r   r  r  r   s       r   rA  RustCodePrinter._print_Float  s@    g"4(!3!3D4E4Ed4K!LLLLJr#   c                   > [         TU ]  U5      nU(       a&  USU R                  U R                  [              -  -   $ U$ r@  )r   _print_Integerr   r   r
   rB  s       r   rE  RustCodePrinter._print_Integer  s@    g$T*!3!3D4E4Eg4N!OOOOJr#   c                    [        UR                  5      [        UR                  5      p2U R                  U R                  [
              nSX$U4-  $ )Nz
%d_%s/%d.0)intr   qr   r   r	   )r   r   r   rI  float_suffixs        r   _print_RationalRustCodePrinter._print_Rational  sD    466{CK1))$*;*;D*ABq222r#   c                   UR                   R                  (       a  UR                  R                  (       a6  UR                  R                  (       aR  UR                   R                  (       d7  U R                  UR                   5      nU R                  UR                  5      nOUR                   nUR                  nU R	                  U5      nU R	                  U5      nUR
                  nSR                  XFU5      $ )Nz{} {} {})lhsrI   rhsr)  r   rel_opformat)r   r   rN  rO  lhs_coderhs_codeops          r   _print_Relational!RustCodePrinter._print_Relational  s    HH(;(;ATAT]a]e]e]p]p%%dhh/C%%dhh/C((C((C;;s#;;s#[[  x88r#   c                R   UR                   n[        R                  n[        R                  n[	        [        UR                  5      5       H  nX1R                  U   U-  -  nXBU   -  nM      U R                  UR                  R                  5      < SU R                  U5      < S3$ )N[])r   r   ZeroOnereversedr   rankr   r   r8   label)r   r   dimselemoffsetr   s         r   _print_IndexedRustCodePrinter._print_Indexed  s}    zzvv%		*+ALLOF**D1gF ,  ;;tyy7T9JKKr#   c                .    UR                   R                  $ rG   )r^  r   r
  s     r   
_print_IdxRustCodePrinter._print_Idx  s    zzr#   c                    UR                   $ rG   )r   r
  s     r   _print_DummyRustCodePrinter._print_Dummy  s    yyr#   c                    g)NEr   r   r   r  s      r   _print_Exp1RustCodePrinter._print_Exp1  s    r#   c                    g)NPIr   rl  s      r   	_print_PiRustCodePrinter._print_Pi  s    r#   c                    g)NINFINITYr   rl  s      r   _print_InfinityRustCodePrinter._print_Infinity  s    r#   c                    g)NNEG_INFINITYr   rl  s      r   _print_NegativeInfinity'RustCodePrinter._print_NegativeInfinity  s    r#   c                    g)Nr   r   rl  s      r   _print_BooleanTrue"RustCodePrinter._print_BooleanTrue  s    r#   c                    g)Nrp   r   rl  s      r   _print_BooleanFalse#RustCodePrinter._print_BooleanFalse  s    r#   c                4    [        U5      R                  5       $ rG   )r  r   rl  s      r   _print_boolRustCodePrinter._print_bool  s    4y  r#   c                    g)NNANr   rl  s      r   
_print_NaNRustCodePrinter._print_NaN  r"   r#   c                \   UR                   S   R                  S:w  a  [        S5      e/ n[        UR                   5       H  u  nu  pEUS:X  a$  UR	                  SU R                  U5      -  5        OOU[        UR                   5      S-
  :X  a  US:X  a  US==   S-  ss'   OUS==   SU R                  U5      -  -  ss'   U R                  U5      nUR	                  U5        UR	                  S	5        M     U R                  S
   (       a  SR                  U5      $ SR                  U5      $ )Nr6   TzAll 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) {rA   z else {z else if (%s) {r   inline 
)	r   r  
ValueError	enumerater   r   r  	_settingsjoin)r   r   r   r   eccode0s          r   _print_Piecewise RustCodePrinter._print_Piecewise  s   99R=%  / 0 0
 "499-IAvAv[4;;q>9:c$))nq((Q$Yb	Y&	b	.Q??	KKNELLLL . >>(#88E?"99U##r#   c                L    SSK Jn  U R                  UR                  USS95      $ )Nr   )	PiecewiseF)deep)sympy.functionsr  r   rewrite)r   r   r  s      r   
_print_ITERustCodePrinter._print_ITE  s!    -{{4<<	<>??r#   c                v   ^  UR                   S:X  a  SSR                  U 4S jU 5       5      -  $ [        S5      e)NrA   z[%s]r  c              3  F   >#    U  H  nTR                  U5      v   M     g 7frG   )r   )r   ar   s     r   r   4RustCodePrinter._print_MatrixBase.<locals>.<genexpr>  s     %@adkk!nnar5  zLFull Matrix Support in Rust need Crates (https://crates.io/keywords/matrix).)r   r  r  )r   As   ` r   _print_MatrixBase!RustCodePrinter._print_MatrixBase  s4    66Q;DII%@a%@@@@kllr#   c                $    U R                  U5      $ rG   )r  )r   r   s     r   _print_SparseRepMatrix&RustCodePrinter._print_SparseRepMatrix  s    ((--r#   c                    UR                   < SUR                  UR                  UR                   R                  S   -  -   < S3$ )NrX  rA   rY  )parentr   r   r   r
  s     r   _print_MatrixElement$RustCodePrinter._print_MatrixElement  s9    ;;66DFF4;;+<+<Q+?$??A 	Ar#   c                N   > [         TU ]  U5      nXR                  ;   a  SU-  $ U$ )Nz(*%s))r   _print_Symbolr   )r   r   r   r   s      r   r  RustCodePrinter._print_Symbol  s-    w$T*$$$T>!Kr#   c                T   SSK Jn  UR                  nUR                  nU R                  S   (       a=  UR                  U5      (       d  UR                  U5      (       a  U R                  XC5      $ U R                  U5      nU R                  U5      nU R                  U< SU< 35      $ )Nr   )IndexedBaser   r   )	sympy.tensor.indexedr  rN  rO  r  has_doprint_loopsr   r   )r   r   r  rN  rO  rR  rS  s          r   _print_Assignment!RustCodePrinter._print_Assignment'  s    4hhhh>>*%377;+?+?$$ &&s00{{3'H{{3'H&&Hh'GHHr#   c                T    U R                  UR                  S   5      nSU< SU< S3$ )Nr   z(if (z == 0.0) { 0.0 } else { (z).signum() }))r   r   )r   r   r#  s      r   _print_signRustCodePrinter._print_sign5  s#    kk$))A,'DGMMr#   c                    UR                   (       d  [        U[        5      $ UR                  (       a  [	        U5      $ U$ rG   )r	  r   r	   rI   r   r
  s     r   r)  RustCodePrinter._cast_to_float9  s,    ~~D$''__;r#   c                    XR                   ;   =(       d,    U[        ;   =(       d    [        U SR                  U5      S5      $ )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   getattrrQ  )r   r   s     r   
_can_printRustCodePrinter._can_print@  s;     +++{t7I/I{WUY[f[m[mnr[suzM{{r#   c                   [        5       n[        U[        5      (       a_  UR                  (       a  UR	                  UR
                  5        UR                   H#  nUR                  U R                  U5      5      nM%     U$ rG   )	r   r  r   is_Functionr6  r  r   union_collect_functions)r   r   	functionsr#  s       r   r  "RustCodePrinter._collect_functionsI  s\    E	dD!!dii(yy%OOD,C,CC,HI	 !r#   c           	       ^  [        U[        5      (       d  U$ T R                  U5      nT R                  R	                  5        VVVs0 s H@  u  nu  pET R                  U5      (       d  M   [        U 4S jU 5       5      (       d  M<  X4U4_MB     nnnnU H;  nUR                  UR                  S5      u  pHU(       d  M*  UR                  U5      nM=     U$ s  snnnf )Nc              3  F   >#    U  H  nTR                  U5      v   M     g 7frG   )r  )r   fr   s     r   r   ;RustCodePrinter._rewrite_known_functions.<locals>.<genexpr>[  s     <1DOOA&&r5  )NN)
r  r   r  _rewriteable_functionsitemsr  allr   r%   r  )	r   r   expression_functionsr   target_frequired_fsrewriteable_functionsr  _s	   `        r   _rewrite_known_functions(RustCodePrinter._rewrite_known_functionsR  s    $%%K#66t< 261L1L1R1R1T!
1T--xx( * <<< *D[))1T 	 !
 )D/33DMM<PKHx||H- ) !
s   C+CCc                   [        U[        5      (       a1  U R                  UR                  S5      5      nSR	                  U5      $ SnSnSnU Vs/ s H  ofR                  S5      PM     nnU Vs/ s H+  n[        [        [        UR                  U5      5      5      PM-     nnU Vs/ s H+  n[        [        [        UR                  U5      5      5      PM-     nn/ n	Sn
[        U5       HE  u  pUS;   a  U	R                  U5        M  XU   -  n
U	R                  X:-  < U< 35        XU   -  n
MG     U	$ s  snf s  snf s  snf )	z0Accepts a string of code or a list of code linesTr  z    ){r  z{
z(
)r   r  z 	r   )r  r  )r  r  r   
splitlinesr  lstriprH  r+  mapendswith
startswithr  r   )r   code
code_linestab	inc_token	dec_tokenlineincreasedecreaseprettylevelns               r   r   RustCodePrinter.indent_codec  s3    dC  ))$//$*?@J77:&&,		046U#6IMOSS	:;<O!%(!% S)<=>!% 	 (  GAz!d#a[ EMMCIt45a[ E '  7O(s   D712D<)2E)r   r   r   r   rG   )F)@r%   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.  r7  r:  r=  rA  rE  rK  rU  rb  re  rh  rm  rq  ru  ry  r|  r  r  r  r  r  r  r  r  r  r  r  r)  r  r  r  r   r*   r   r   s   @r   r   r     s[   HKH 	gL 	uv	M )-[-J-J )u	O )~  !# 5";'B'	%13f(/*i3

9L!$6@m.AIN|" r#   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   <module>r     s  	B #     2 1   + > 2 0"% G  [!' 2 72 V2 
52  
4gqA5vqA6J4fa@4fa@-vq9161=?!2. 
ua()/20 
412< 
5=2> 
5?2B 
5C2D 
5E2F 
5G2H FI2J FK2L FM2N WO2V FW2X FY2Z c2X3l2t 20mk mr#   