K i_Q dZddlmZddlmZddlZddlmZddlm Z m Z m Z m Z m Z mZddlmZmZmZmZddlmZdd lmZdd lmZmZdd lmZdd lmZGd deZGddeZ eefee fdZ!iddddddddddfdddfdddfdddfd d!d"fd#d$d%fd&d'd%fgd(d)d(dfgd*d+d,d-d.d/d0d0d1d1d2d2d3d3d4d4d5d5d6d6d7d7d8d8d9d:d;dZ#Gd?d@eZ$GdAdBeZ%y)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)float32float64int32realintegerbool_)SRationalFloatLambda)Expr equal_valued)ceilingfloor) CodePrinter) PRECEDENCEceZdZdZdZy) float_floorzi Same as `sympy.floor`, but mimics the Rust behavior of returning a float rather than an integer cyNFselfs Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/printing/rust.py_eval_is_integerzfloat_floor._eval_is_integerEN__name__ __module__ __qualname____doc__rrr!rrrA r!rceZdZdZdZy) float_ceilingzk Same as `sympy.ceiling`, but mimics the Rust behavior of returning a float rather than an integer cyrrrs rrzfloat_ceiling._eval_is_integerLr r!Nr"rr!rr)r)Hr'r!r))rrrceilAbsabsPowct|dS)Nrbaseexps rr4fs |C4r!recipct|dS)Ng?rr1s rr4r4gs |C5r!sqrtct|dS)Ngrr1s rr4r4hs |C6r!z sqrt().recipc |tddk(S)N)r r1s rr4r4isshq!n4r!cbrtct|dS)Nr6rr1s rr4r4js |D!4r!exp2r<c|jSN is_integerr1s rr4r4ks s~~r!powir;c|j SrArBr1s rr4r4ls 3>>1r!powfr3cy)NTr)r3s rr4r4msr!loglnMaxmaxMinminsincostanasinacosatanatan2sinhcoshtanhasinhacoshatanh)rWrXrYrZr8)4abstractasasyncawaitbecomeboxbreakconstcontinuecratedodynelseenumexternfalsefinalfnforgenifimplinletloopmacromatchmodmovemutoverrideprivpubrefreturnSelfrstaticstructsupertraittruetrytypetypeofunsafeunsizedusevirtualwherewhileyieldcLeZdZdZdfd ZedZedZddZxZ S)TypeCastz9 The type casting operator of the Rust language. ct||jxr|tu|_|j |_|jrt |dtddzyy)N precedenceFunc )r__init__rCr explicit _assumptionssetattrr)rexprtype_ __class__s rrzTypeCast.__init__sS @E,@  -- == D, 6(:R(? @ r!c |jdS)Nrargsrs rrz TypeCast.expryy|r!c |jdS)Nr;rrs rrzTypeCast.type_rr!c@|jdj|S)Nr)order)rsort_key)rrs rrzTypeCast.sort_key syy|$$5$11r!)r}NonerA) r#r$r%r&rpropertyrrr __classcell__rs@rrrs;A2r!rc eZdZUdZdZdZeeee iZ ede de de diZ eej fidid ed Zd ed <ifd ZdZdZdZdZdZdZdZdZdZfdZd7fd ZdZdZ d8fd Z!d8fd Z"dZ#dZ$dZ%d Z&d!Z'd8d"Z(d8d#Z)d8d$Z*d8d%Z+d8d&Z,d8d'Z-d8d(Z.d8d)Z/d*Z0d+Z1d,Z2d-Z3d.Z4fd/Z5d0Z6d1Z7d2Z8d3Z9d4Z:d5Z;d6Z<xZ=S)9RustCodePrinterz>A printer to convert SymPy expressions to strings of Rust code _rust_codeRusti32f32f64boolT) precisionuser_functionscontract dereferencezdict[str, Any]_default_settingsc0tj||tt|_|j di}|jj |t |j dg|_t t|_t|_ y)Nrr) rrdictknown_functionsgetupdateset _dereferencereserved_wordsfunction_overrides)rsettings userfuncss rrzRustCodePrinter.__init__(sqT8,#O4LL!126  ##I. ]B ?@!.1"4r!c |dzS)Nr)rps r_rate_index_positionz$RustCodePrinter._rate_index_position1s s r!c d|zS)Nz%s;r)r codestrings r_get_statementzRustCodePrinter._get_statement4s z!!r!c d|zS)Nz// %sr)rtexts r _get_commentzRustCodePrinter._get_comment7s ~r!cZ|j|jt}d|d|d|dS)Nzconst z:  = ;) type_mappings type_aliasesr )rnamevaluers r_declare_number_constz%RustCodePrinter._declare_number_const:s*""4#4#4T#:;'+UE::r!c$|j|SrA) indent_code)rliness r _format_codezRustCodePrinter._format_code>s&&r!cJ|j\}fdt|DS)Nc3FK|]}tD]}||f ywrA)range).0ijcolss r z;RustCodePrinter._traverse_matrix_indices..Cs%A1U4[AAAAs!)shaper)rmatrowsrs @r_traverse_matrix_indicesz(RustCodePrinter._traverse_matrix_indicesAsYY dAd AAr!c g}g}d}|D]o}|j||j||j|j|j|jdzdz|jdq||fS)Nz#for %(var)s in %(start)s..%(end)s {r;)varstartend})append_printlowerupper)rindices open_lines close_lines loopstartrs r_get_loop_opening_endingz(RustCodePrinter._get_loop_opening_endingEs  9  $A   i{{1~QWW-{{177Q;/+11 2   s #  $;&&r!ct|jdkDrd|j|zdzS|jr|j|dS|j|S)Nr;()T)_type)lenrr is_numberrrs r_print_caller_varz!RustCodePrinter._print_caller_varRsV tyy>A T**S0 0 ^^;;t4;0 0;;t$ $r!cr|jj|jvr6|j|jj}d}d}t|tr|}n|D]\}}}||j sn||dk(r^d|j |j d|t|j dkDr|j|j dddnddz}|S|dk(r&d |j |j d|d z}|S|d k(r&d |j |j d|d z}|Sd ||j|j dd z}|Syt|drBt|jtr(|j|j|j S|j|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 Nr;z%(var)s.%(method)s(%(args)s)r, )rmethodrr6z%(var)s.%(method)s())rrr<z%(func)s(%(args)s))funcr_imp_)rr#r isinstancestrrrr stringifyhasattrrrr_print_not_supported)rr cond_funcrstylecondrets r_print_FunctionzRustCodePrinter._print_Function]s 99  !5!5 5,,TYY-?-?@IDE)S) )2%D$TYY'A:8#55diilC"&GJ499~XYGYtyy}d C_a<C( aZ0#55diilC"&4C aZ0#55diilC"&4C / $ $tyy$ ?2C - .T7 # 4::v(F;;ztzz49956 6,,T2 2r!ctd|jD}|r-ttjfd|jD}t |S)Nc3RK|]}|jxr |j !ywrAis_realrCrargs rrz-RustCodePrinter._print_Mul..#VSckk@#...@@V%'c3NK|]}|dk7rj|n|yw)r0N_cast_to_floatrr rs rrz-RustCodePrinter._print_Mul..s)'l[^C2I(;(;C(@SV(V'ls"%)anyrroperatormulr _print_Mul)rrcontains_floatsrs` rrzRustCodePrinter._print_MulsGVDIIVV (,,'lbfbkbk'lmDw!$''r!ctd|jD}|r-ttjfd|jD}t ||S)Nc3RK|]}|jxr |j !ywrAr r s rrz-RustCodePrinter._print_Add..rrc3@K|]}j|ywrArrs rrz-RustCodePrinter._print_Add..s(Wc)<)rrs r _print_IdxzRustCodePrinter._print_Idxszzr!c|jSrA)rrs r _print_DummyzRustCodePrinter._print_Dummys yyr!cy)NErrrrs r _print_Exp1zRustCodePrinter._print_Exp1sr!cy)NPIrrIs r _print_PizRustCodePrinter._print_Pisr!cy)NINFINITYrrIs r_print_InfinityzRustCodePrinter._print_Infinitysr!cy)N NEG_INFINITYrrIs r_print_NegativeInfinityz'RustCodePrinter._print_NegativeInfinitysr!cy)NrrrIs r_print_BooleanTruez"RustCodePrinter._print_BooleanTruesr!cy)NrjrrIs r_print_BooleanFalsez#RustCodePrinter._print_BooleanFalsesr!c4t|jSrA)rrrIs r _print_boolzRustCodePrinter._print_bools4y  r!cy)NNANrrIs r _print_NaNzRustCodePrinter._print_NaNr r!cH|jdjdk7r tdg}t|jD]\}\}}|dk(r$|j d|j |znM|t |jdz k(r|dk(r|dxxdz cc<n|dxxd|j |zz cc<|j |}|j ||j d |jd rd j|Sd j|S) Nr0TzAll 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.rz if (%s) {r;z else {z else if (%s) {rinline  ) rr ValueError enumeraterrr _settingsjoin)rrrreccode0s r_print_Piecewisez RustCodePrinter._print_Piecewises 99R=   %/0 0 "499- IAv1Av [4;;q>9:c$))nq((Q$Yb Y& b .Q?? KKNE LL  LL   >>( #88E? "99U# #r!cRddlm}|j|j|dS)Nr) PiecewiseF)deep)sympy.functionsrjrrewrite)rrrjs r _print_ITEzRustCodePrinter._print_ITE s!-{{4<< <>??r!cr|jdk(rddjfd|DzStd)Nr;z[%s]rc3@K|]}j|ywrA)r)rars rrz4RustCodePrinter._print_MatrixBase..s%@dkk!n%@rzLFull Matrix Support in Rust need Crates (https://crates.io/keywords/matrix).)rrdra)rAs` r_print_MatrixBasez!RustCodePrinter._print_MatrixBases4 66Q;DII%@a%@@@ @kl lr!c$|j|SrA)r)rrs r_print_SparseRepMatrixz&RustCodePrinter._print_SparseRepMatrixs((--r!c|jd|j|j|jjdzzdS)Nr8r;r9)parentrrrrs r_print_MatrixElementz$RustCodePrinter._print_MatrixElements9;;66DFF4;;+<+ !Kr!c4ddlm}|j}|j}|jdr4|j |s|j |r|j ||S|j|}|j|}|j|d|S)Nr) IndexedBaserr) sympy.tensor.indexedr|r/r0rchas_doprint_loopsrr)rrr|r/r0r3r4s r_print_Assignmentz!RustCodePrinter._print_Assignment's4hhhh >>* %377;+? $&&sC0 0{{3'H{{3'H&&Hh'GH Hr!cP|j|jd}d|d|dS)Nrz(if (z == 0.0) { 0.0 } else { (z ).signum() }))rr)rrr s r _print_signzRustCodePrinter._print_sign5s#kk$))A,'DGMMr!cl|jst|tS|jr t |S|SrA)rrr rCrrs rrzRustCodePrinter._cast_to_float9s,~~D$' ' __;  r!cn||jvxs&|tvxst|dj|dS)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)rrgetattrr2)rrs r _can_printzRustCodePrinter._can_print@s=t+++{t7I/I{WUY[f[m[mnr[suzM{{r!ct}t|trX|jr|j |j |j D]"}|j|j|}$|SrA) rrr is_Functionrrrunion_collect_functions)rr functionsr s rrz"RustCodePrinter._collect_functionsIs`E dD ! dii(yy J%OOD,C,CC,HI  Jr!c t|ts|Sj|}jj Dcic]2\}\}}j |rt fd|Dr|||f4}}}}|D]5}|j|jd\}}|s%|j|}7|Scc}}}w)Nc3@K|]}j|ywrA)r)rfrs rrz;RustCodePrinter._rewrite_known_functions..[s<1DOOA&((  !GAtz! d# Xa[ E MMCIt4 5 Xa[ E  ! 7O(s D'*/D,/D1rA)F)>r#r$r%r& printmethodlanguager rr rrrr rrrrr__annotations__rrrrrrrrrrrrr r"r%r(r-r6rBrDrFrJrMrPrSrUrWrYr\rhrnrsrurxrzrrrrrrrrrs@rrrsVHKH  gL u v M)-[-J-J)u O)~!#5";'B ' %13f(/* i 3 9L!$6@m .A IN|"r!r)&r& __future__r functoolsrrtypingrsympy.codegen.astrrrr r r sympy.corer r rrsympy.core.exprrsympy.core.numbersr#sympy.functions.elementary.integersrrsympy.printing.codeprinterrsympy.printing.precedencerrr)rrrrrrr!rrs B#21 +>20"%G[ !' 2 7 2V2 52  4gq A 5vq A 6 J 4fa @ 4fa @ -vq 9 161 =  ?!2. ua ( )/20 412< 5=2> 5?2B 5C2D 5E2F 5G2H FI2J FK2L FM2N WO2V FW2X FY2Z     c2X3l2t20mkmr!