K iidZddlmZddlmZddlmZddlmZddl m Z ddl m Z m Z ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)dd l*m+Z+m,Z,dd l-m.Z.m/Z/dd l0m1Z1dd l*m2Z2m3Z3id ddfddfgddddddddddddddddddddddddddddd d!d"d"Z4e5e4fiid#d#d$d$d%d%d&d&d'd(d)d)d*d*d+d,d-d-d.d/d0d1d2d2d3d3d4d4d5d5d6d7Z6gd8Z7d9d:gZ8d;Z9d<Z:Gd=d>e+Z;Gd?d@e;Ze?ezer5(s 1<<'fabsc|jSr/r0r2s r4r5r5(s Q\\r6abssincostanasinacosatanatan2exploglog10sinhcoshtanhfloorceilingceilsqrtexp2expm1log2log1pCbrtcbrthypotfmaloggammalgammaerfcMaxfmaxMinfminasinhacoshatanherfgammatgamma)!autobreakcasecharconstcontinuedefaultdodoubleelseenumexternfloatforgotoifintlongregisterreturnshortsignedsizeofstaticstructentryswitchtypedefunionunsignedvoidvolatilewhileinlinerestrictc8ddlm}m}ddlm}ddlm}itjd|tjdd|dz d|dd |d d tjd tjdz d tjdz ddtjz ddtjz dd|tjz dd|tjz d|dd|ddd|dz dd|dz dS)aV Returns a dictionary with math-related macros from math.h/cmath Note that these macros are not strictly required by the C/C++-standard. For MSVC they are enabled by defining "_USE_MATH_DEFINES" (preferably via a compilation flag). Returns ======= Dictionary mapping SymPy expressions to strings (macro names) r)rMSqrt)rB)rJM_EM_LOG2EM_LN2 M_LN10M_PIM_PI_2M_PI_4M_1_PIM_2_PI M_2_SQRTPIM_SQRT2 M_SQRT1_2) sympy.codegen.cfunctionsrMr&sympy.functions.elementary.exponentialrB(sympy.functions.elementary.miscellaneousrJrExp1Pi)rMrrBrJs r4get_math_macrosr[sC4:=    QVV i  #a&)  A   B   f   Q  Q  !$$  !$$  $qtt* l  $qtt* l  Q  Q  $q' ;  $q' ;! r6c.tfd}|S)a Decorator for printer methods When a Printer's method is decorated using this decorator the expressions printed will first be looked for in the attribute ``math_macros``, and if present it will print the macro name in ``math_macros`` followed by a type suffix for the type ``real``. e.g. printing ``sympy.pi`` would print ``M_PIl`` if real is mapped to float80. c||jvr&|j||jtS||fi|Sr/) math_macros_get_math_macro_suffixr)selfexprkwargsmeths r4 _meth_wrapperz+_as_macro_if_defined.._meth_wrappersE 4## #!--d3T5P5PQU5VW Wd-f- -r6r)rrs` r4_as_macro_if_definedrs# 4[.. r6ceZdZUdZdZdZdZeeZe e jfidideddZ d e d <e eeeeeiZe d ed ed ed ed ededededededededediZde d<edhedhedhedhedhedhedhedhedhi ZiZ de d<edede!diZ"edede!diZ#e!diZ$dZ%dZ&e'Z(d e d<dWfd Z)d!Z*d"Z+d#Z,d$Z-d%Z.d&Z/e0fd'Z1e0d(Z2d)Z3d*Z4d+Z5e0fd,Z6d-Z7d.Z8d/Z9d0Z:d1Z;fd2Zd5Z?d6Z@d7ZAd8ZBd9ZCd:ZDd;ZEd<ZFeFZGd=ZHd>ZId?ZJeKdh@dAZLeKdh@dBZMdCZNdDZOdEZPdFZQeKdGh@dHZRdIZSdJZTdKZUdLZVdMZWdNZXdOZYdPZZdQZ[dRZ\dSZ]dTZ^dUZ_dVZ`e^ZaxZbS)XC89CodePrinterzr    #'||M?;LMD  t'8'8'>'>'@'/||NB'G'M'M'O"QR!%(:(:(@(@(B(0 _b(I(O(O(Q#ST t'8'8'>'>'@'/||NB'G'M'M'O"QRd&6&6&<&<&>'/||M2'F'L'L'N!PQ"&uT-D-D-J-J-L(0 5I2(N(T(T(V(X#Y%)%0J0J0P0P0R(0 5Lb(Q(W(W(Y+[&\"(,U43P3P3V3V3X(0 5OQS(T(Z(Z(\.^)_% "#DHHS =Mr0RS ]B ?@u e r6c |dzS)N)rps r4_rate_index_positionz#C89CodePrinter._rate_index_positions s r6c2|jdr|S|dzS)z@ Get code string as a statement - i.e. ending with a semicolon. ;)endswith)r codestrings r4_get_statementzC89CodePrinter._get_statements'005zK:;KKr6c$dj|S)Nz/* {} */)format)rtexts r4 _get_commentzC89CodePrinter._get_comments  &&r6c|jt}t|||j|jt h}t |}|j|j|S)N)typevalueattrs) rrr evalf decimal_digrrr_print)rnamertype_vardecls r4_declare_number_constz$C89CodePrinter._declare_number_constsY!!$'t%u{{5;L;L/MVaUbc3""4;;t#455r6c$|j|Sr/) indent_code)rliness r4 _format_codezC89CodePrinter._format_codes&&r6cJ|j\}fdt|DS)Nc3FK|]}tD]}||f ywr/)range).0ijcolss r4 z:C89CodePrinter._traverse_matrix_indices.. s%A1U4[AAAAs!)shaper)rmatrowsrs @r4_traverse_matrix_indicesz'C89CodePrinter._traverse_matrix_indices sYY dAd AAr6c $t||fi|Sr/)r _print_Mul)rrrrs r4rzC89CodePrinter._print_Mulsw!$1&11r6c d|jvr|j|St|}|jt}t |j dr9|jtdd|j|j|St |j dr.|jd|d|j|jdS|j tjd z k(r=|jd k7r.|jd |d|j|jdS|jd |d|j|jd |j|j dS)NPowg?/g?rJ()rrPpow, )r_print_Functionr(_get_func_suffixrr rA _print_Floatr parenthesizebase_nsrrOnestandard)rrPRECsuffixs r4 _print_PowzC89CodePrinter._print_Pows D(( (''- -$&&t, " %"//c ;T=N=NtyyZ^=_` ` $((C (%)XXvt{{4997MN N XXq T]]e%;%)XXvt{{4997MN N(,&$++dii:P#';;txx#8: :r6cz|j\}}|jr|jr}t|}|jDcgc]}|j||c}\}}|jr |js|j r|j r|d|Sd|d|d|d|S|j |dScc}w)Nz % ((z) + z) % fmod)known)argsr1r(r is_nonnegativeis_nonpositive_print_math_func)rrnumdenrargsnumsdens r4 _print_ModzC89CodePrinter._print_Mod"s99S >>cnnd#DBF))L3$++C6LJD$""s'9'9""s'9'9s4&))vSd4&TF; ;$$T$88MsB8ct|jt|j}}|jt}d||||fzS)Nz %d.0%s/%d.0%s)rprq_get_literal_suffixr)rrrr"rs r4_print_RationalzC89CodePrinter._print_Rational1s?466{CK1))$/!VQ!777r6cbt|jdtj}t|jdd}|j}|t |t r}|j}tj}d}|dk(s|'tt|j}|ddd}n|dk(rt|j}D]} ||fz }||| z}|}tdt||D|z} |j|jjd|j| d S) Noffsetstridesrrrrc32K|]}|d|dzyw)rrNr)rr3s r4rz0C89CodePrinter._print_Indexed..JsBq1adBs[])getattrr rZeroindices isinstancestrrrreversedrranksumziprlabel) rrr&r'r-dimsshifttemp traversalr flat_indexs r4_print_IndexedzC89CodePrinter._print_Indexed6sHaff5$))Y5,, ?j#6::DEEED#~$U499%56 !$B$-C!$)),  ! a  !GBC,ABBVK ;;tyy7;;z24 4r6c"t||Sr/)r_print_NumberSymbol)rrrs r4r<z"C89CodePrinter._print_NumberSymbolNsw*400r6cy)NHUGE_VALrrrs r4_print_InfinityzC89CodePrinter._print_InfinityRr6cy)Nz -HUGE_VALrr?s r4_print_NegativeInfinityz&C89CodePrinter._print_NegativeInfinityUr6c |jdjdk7r tdg}|jtrt |jD]\}\}}|dk(r$|j d|j|znU|t|jdz k(r|dk(r|j dn#|j d|j|z|j|}|j ||j d d j|S|jddDcgc],\}}d |j|d |j|d .}}}d|j|jdjz}dj||zdjdt|zgzScc}}w)NrTzAll 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) {rzelse {zelse if (%s) {} rz) ? ( z ) z: ( %s )z:  r) rcond ValueErrorhasr enumerateappendrlenjoinr) rrrreccode0ecpairs last_lines r4_print_PiecewisezC89CodePrinter._print_PiecewiseXs 99R=   %/0 0  88J &tyy1 " 6Aq6LLt{{1~!=>#dii.1,,dLL*LL!1DKKN!BC A U# S! "99U# #!% #20104{{1~/3{{1~?0G0%t{{499R=3E3E'FFI99W% 1CHHc#g,>N=O4PP P 0s31GcRddlm}|j|j|dS)Nr) PiecewiseF)deep)sympy.functionsrWrrewrite)rrrWs r4 _print_ITEzC89CodePrinter._print_ITEys!-{{4<< <>??r6cdj|j|jtdd|j|j |jj dzzS)Nz{}[{}]AtomT)strictr)rr parentr)rrrr?s r4_print_MatrixElementz#C89CodePrinter._print_MatrixElement}s[t00j>P 1 &&466$++*;*;A*>#>>@ @r6cjt||}||jdvrdj|S|S)Nrz(*{}))r _print_Symbol _settingsr)rrrrs r4rbzC89CodePrinter._print_Symbols7w$T* 4>>-0 0>>$' 'Kr6c|j|j}|j|j}|j}dj |||S)Nz{} {} {})rlhsrhsrel_opr)rrlhs_coderhs_codeops r4_print_Relationalz C89CodePrinter._print_RelationalsD;;txx(;;txx( [[  2x88r6c|j|j}t|jtr|jj \}}}n t d|j|j}dj|||||S)Nz*Only iterable currently supported is RangezLfor ({target} = {start}; {target} < {stop}; {target} += {step}) {{ {body} }})targetstartstopstepbody) rrmr.iterabler*rNotImplementedErrorrqr)rrrmrnrorprqs r4 _print_ForzC89CodePrinter._print_ForszT[[) dmmU + $ 2 2 E4%&RS S{{499%)*0&e4+1+1 1r6c\dj|j|jdS)Nz((({0}) > 0) - (({0}) < 0))r)rrr)rfuncs r4 _print_signzC89CodePrinter._print_signs$,33DKK ! 4MNNr6ctdjvrj|Sfd|jS)NrVct|dk(rj|dSt|dz}d|d|||ddzS)Nrrrz!((%(a)s > %(b)s) ? %(a)s : %(b)s)abrNr)rhalfinner_print_maxrs r4rz2C89CodePrinter._print_Max..inner_print_max\4yA~{{47++t9>D6$T%4[1$T$%[1: r6rrr)rrrs` @r4 _print_MaxzC89CodePrinter._print_Max8 D(( (''- - tyy))r6ctdjvrj|Sfd|jS)NrXct|dk(rj|dSt|dz}d|d|||ddzS)Nrrrz!((%(a)s < %(b)s) ? %(a)s : %(b)s)rzr})rr~inner_print_minrs r4rz2C89CodePrinter._print_Min..inner_print_minrr6r)rrrs` @r4 _print_MinzC89CodePrinter._print_Minrr6c lt|tr1|j|jd}dj |Sd}d}d}|Dcgc]}|j d}}|Dcgc]*}t tt|j|,}}|Dcgc]*}t tt|j|,}}g} d} t|D]C\} }|dvr| j|| || z} | j|| z|| || z } E| Scc}wcc}wcc}w) z0Accepts a string of code or a list of code linesTrz ){rz{ z( )rFrz r)rrG) r.r/r splitlinesrOlstriprpanymapr startswithrLrM) rcode code_linestab inc_token dec_tokenlineincreasedecreaseprettylevelns r4rzC89CodePrinter.indent_codes, dC ))$//$*?@J77:& &,  /34t E"44HLMCC y9:;MMJNO$CC;<=OO  !GAtz! d# Xa[ E MMCIt4 5 Xa[ E  ! 5MOs D'*/D,/D1cT|j|jj||Sr/)rrrrrs r4r zC89CodePrinter._get_func_suffixs&&&t'8'8'<'.s:T[[^:rF)rOr?s` r4 _print_TuplezC89CodePrinter._print_Tuples"499:T:::3>>r6c`|jj|jj|t |j j|j j|t |j|jj||jSr/) rupdaterrrrrrrrrs r4 _print_TypezC89CodePrinter._print_Typesv D--11%?@ 4++//su=>{{4--11%DEEr6c ddlm}|j}|j}|jt k(r t dt|trdjt|jvrdnd|j|jt|jvrdnd||jvrdnd|j|j }nt|tr[d jt|jvrdnd|j|j|j|j }nt!d t |z|dk7r|d |j|zz }|S)Nr)rz$C does not support untyped variablesz{vc}{t} *{pc} {r}{s}zconst rz constz restrict )vctpcrsz {vc}{t} {s})rrrzUnknown type of var: %sz = %s)sympy.codegen.cnodesrvariablerrr$rJr.r rrrrrsymbolr rs)rrrrvalresults r4_print_Declarationz!C89CodePrinter._print_Declarations*1mmii 88w CD D c7 #+22*cii78R++chh', 98r!)SYY!6+B++cjj) 3FX &"))*cii78R++chh'++cjj)*F &&?$s)&KL L $; g C 00 0F r6c|jjtt}|jj |j j|t |j|}t|j|j}d|vr d|vr|dz }|jd}|djd|d<|djdr |dxxdz cc<dj||zS)NrP.z.0r0)rrrrrrrr#r/rrsplitrstriprrO)rfltrrr num_partss r4r zC89CodePrinter._print_Floats!!%%dD1 4++//su=>))%0#))E--./ c>cn 4KCIIcN  |**3/ ! Q<  % aLC Lxx "V++r6rcy)Ntruerr?s r4_print_BooleanTruez!C89CodePrinter._print_BooleanTruesr6cy)Nfalserr?s r4_print_BooleanFalsez"C89CodePrinter._print_BooleanFalsesr6c|jdk(r?|jdk7r tddjfd|jD}n_t dt |j|jD}|jdk7r||jz }j|}djj|j|S)Nz%Expected strides when offset is givenz][c3@K|]}j|ywr/rrrrs r4rz0C89CodePrinter._print_Element..sG3dkk#.Grc3,K|] \}}||zywr/r)rrrs r4rz0C89CodePrinter._print_Element..sMTQQqSMsz{symb}[{idxs}])symbidxs) r'r&rJrOr-r2r3rrr)relemr global_idxs` r4_print_ElementzC89CodePrinter._print_Elements <<4 {{d" !HII99G$,,GHDMSt||-LMMJ{{d"dkk) ;;z*D&&T[[)'  r6c dj|jDcgc]"}|j|j|$c}Scc}w)z0 Elements of code blocks printed as statements. rG)rOrrr)rrrs r4_print_CodeBlockzC89CodePrinter._print_CodeBlock&s6yytyyQ!$--dkk!n=QRRQs'AcNdjdi|jfdS)Nz while ({condition}) {{ {body} }}c&j|Sr/r)rrs r4r5z-C89CodePrinter._print_While..,sdkk#.r6)applyr)rrr?s` r4 _print_WhilezC89CodePrinter._print_While*s2:3::1T[[.>I>01 1r6c>d|j|jzS)Nz{ %s })rrqr?s r4 _print_ScopezC89CodePrinter._print_Scope.sD11$))<<.;sI#T[[-Ir)fmtpargs)filer%rr format_stringrO print_args)rrtemplates` r4 _print_PrintzC89CodePrinter._print_Print1s 99 /H9t{{499-=H,,4$++dFXFX:Y))IIJ  r6c|jSr/)r)rstrms r4 _print_StreamzC89CodePrinter._print_Stream>s yyr6cdjfd|jD}dtfd|j|jfD|fzzS)Nrc3RK|]}jt| ywr/)rrrs r4rz:C89CodePrinter._print_FunctionPrototype..BsSC$++k#&67Ss$'z %s %s(%s)c3@K|]}j|ywr/rrs r4rz:C89CodePrinter._print_FunctionPrototype..DsM4;;s#Mr)rO parameterstuple return_typer)rrparss` r4_print_FunctionPrototypez'C89CodePrinter._print_FunctionPrototypeAsNyyS4??ST M0@0@$))/LM NRVQX X  r6cJ|j||j|Sr/)rrr?s r4_print_FunctionDefinitionz(C89CodePrinter._print_FunctionDefinitionGs(66t<**402 2r6cF|j\}d|j|zS)Nz return %srrrrrs r4 _print_ReturnzC89CodePrinter._print_ReturnKs!yyT[[---r6cRddjfd|jDzS)Nz(%s)rc3@K|]}j|ywr/rrs r4rz6C89CodePrinter._print_CommaOperator..Ps"I4;;s#3"Ir)rOrr?s` r4_print_CommaOperatorz#C89CodePrinter._print_CommaOperatorOs! "Ityy"IJJJr6cj|jtk(rdt|jzSt |jj dk(r3t|jd|j |jSt|jd|j |jdS)Nz%s:rz: z: { z })rqr%r/rrNrrr?s r4 _print_LabelzC89CodePrinter._print_LabelRsz 99 3tyy>) ) tyy~~ ! # #DII0E0Edii0PQ Q"%dii.$2G2G 2RSSr6c4d|jjzS)Nzgoto %s)r4rr?s r4 _print_gotozC89CodePrinter._print_gotoYs4::??**r6cF|j\}d|j|zS)Nz++(%s)rrs r4_print_PreIncrementz"C89CodePrinter._print_PreIncrement\!yy$++c***r6cF|j\}d|j|zS)Nz(%s)++rrs r4_print_PostIncrementz#C89CodePrinter._print_PostIncrement`rr6cF|j\}d|j|zS)Nz--(%s)rrs r4_print_PreDecrementz"C89CodePrinter._print_PreDecrementdrr6cF|j\}d|j|zS)Nz(%s)--rrs r4_print_PostDecrementz#C89CodePrinter._print_PostDecrementhrr6c d|jj|jdj|jDcgc]}|j |c}dgzdzScc}w)Nz!%(keyword)s %(name)s { %(lines)s}z; r)keywordrr)r__name__rrO declarationsr)rrrs r4 _print_structzC89CodePrinter._print_structls[3~~.. EJJ/3/@/@AtT"ARDHMJ7   AsA"cy)Nrarr_s r4_print_BreakTokenz C89CodePrinter._print_BreakTokenrsr6cy)Nrerrs r4_print_ContinueTokenz#C89CodePrinter._print_ContinueTokenurAr6r/)cr __module__ __qualname____doc__ printmethodlanguagerrreserved_wordsrr&r__annotations__rrrrrrrrrrrrrr r!r"r#rrrrrrrrr known_functions_C89rrrrrrrrrrrr r$r:r<r@rCrUr[r`rbrkrtrwrrrr r#rr _print_Listrrr r'rrrrrrrrrrrrrrrrrrrrr _print_union __classcell__)rs@r4rrsFKHH(N(,[-J-J)u" O)~ g*L h e v h y y y x   &M?$  } zl | | | |    L02K,1    K C-C-2L'6 'B22 : : 98 4011QB@@9 1O * *6LO> ?KF 6 ,{m$%{m$% S1=yk"  #   2.KT+++++ !Lr6rc eZdZUdZeeezZeee jje de dijZ eee jje dhe dhijZeZded<dj%ZdZd Zd Zed hd h eddZdZdZdZy)C99CodePrinterC99z float complexzdouble complexz complex.hrrafabs fmod remainder remquo fma fmax fmin fdim nan exp exp2 expm1 log log10 log2 log1p pow sqrt cbrt hypot sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh erf erfc tgamma lgamma ceil floor trunc round nearbyint rint frexp ldexp modf scalbn ilogb logb nextafter copysigncy)NINFINITYrr?s r4r@zC99CodePrinter._print_InfinityrAr6cy)Nz -INFINITYrr?s r4rCz&C99CodePrinter._print_NegativeInfinityrDr6cy)NNANrr?s r4 _print_NaNzC99CodePrinter._print_NaNsr6zmath.hm)rrNc b|#j|jj}t|ts(|D]\}}||j s|}n t d |g|j S#t$r5j|zjvrjtnd}YnwxYw|rj|j d}t|j dkDrd}|j ddD]8} |dz }|djj||j| z }:|d j|j|j d|z }n$d j!fd |j D}d jj||| S) NzNo matching printerrrrrrz, {ns}{name}{suffix}({next})nsrrnextrc3@K|]}j|ywr/rrs r4rz2C99CodePrinter._print_math_func..sD3dkk#.Drz{ns}{name}{suffix}({args}))rrrr)rrrr.r/rrJ TypeErrorr  _prec_funcsr rrrNrrvrO) rrnestrcbrrr paren_pilecurr_args ` r4rzC99CodePrinter._print_math_funcs =(()@)@AE%%! 8Dtyy> E 8 !!677 a* * * a48HHu4DHXHX4XT**40^`F a ;;tyy|,D499~! $ !BH#%J9@@88"%#{{84 ADKK $))B- 89 99D$))DED+22xx 3  s A33;B10B1c(|j|dSNT)r$rr?s r4rzC99CodePrinter._print_Max$$T$55r6c(|j|dSr)r*r?s r4rzC99CodePrinter._print_Minr+r6c g}g}d}|D]y}|j||j|j|j|j|j|jdzdz|jd{||fS)Nz8for (int %(var)s=%(start)s; %(var)s<%(end)s; %(var)s++){r)rrnendrF)rMrr4lowerupper)rr- open_lines close_lines loopstartrs r4_get_loop_opening_endingz'C99CodePrinter._get_loop_opening_endings  N  $A   i{{177+QWW-{{177Q;/+11 2   s #  $;&&r6)FN)rr r rrrreserved_words_c99rrrrrrrrknown_functions_C99rrrr#r@rCrr'rrrrr4rr6r4rrzsH*<<=Nu^99??A?$D egMn99??AK=[MD egL .C-LNSUW   xjSE2& 3& P66 'r6rzAbs Sqrt exp exp2 expm1 log log10 log2 log1p Cbrt hypot fma loggamma sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh erf erfc loggamma gamma ceiling floorz _print_%sc,eZdZedhdZy)C11CodePrinterz stdalign.hrcF|j\}d|j|zS)Nz alignof(%s)rrs r4_print_alignofzC11CodePrinter._print_alignofs!yyt{{3///r6N)rr r r'r:rr6r4r8r8s |n%0&0r6r8)c89c99c11N)Cr  __future__rtypingr functoolsr itertoolsr sympy.corersympy.core.numbersr r sympy.codegen.astr r r rrrrrrrrrrrrrrrrrrr r!r"r#r$r%sympy.printing.codeprinterr&r'sympy.printing.precedencer(r)sympy.sets.fancysetsr*r+r,rrr6rr5rrrrrksetattrrr8c_code_printersrr6r4rKs #2=<&: ' 03I52Q R 5 5 5   F   F  F W 5 5 W F F F W v!" F#(.3 F3 W3 F3 W 3  F 3  W 3 533 F3 63 63 W3 W3 W3 53  X!3, +"J&e![e!N\'^\'~ 89>NA NK!O^-L-LMN 0^0   r6