Hi Aaron, Thanks for the advice. Do you perhaps mean _print_Mul (since I need to replace 0.5 * I with 0.5j)? I checked lambdify, however I will most likely need a multi-statement function, and hence lambdify seems insufficient for me. codegen seems appealing, but there is no python code generation there.
Best, Anton Akhmerov On Monday, June 9, 2014 5:20:54 PM UTC+2, Aaron Meurer wrote: > > I guess you'll need to subclass whatever printer you are using > (StrPrinter or LambdaPrinter probably) and override _print_Add to get > this behavior. See http://docs.sympy.org/latest/modules/printing.html. > > By the way, what you are doing might be simplified by using > lambdify(). And I think the LambdaPrinter should perform the > translation you suggest automatically, if you want to submit a patch. > > Aaron Meurer > > On Mon, Jun 9, 2014 at 10:09 AM, Anton Akhmerov > <anton.a...@gmail.com <javascript:>> wrote: > > Hi everyone, > > > > I'm using sympy to automatically apply a certain transformation to > symbolic > > input and to generate a reasonably efficient numerical function as an > output > > (I'm creating a finite difference Hamiltonian from a continuum one). My > > current plan is to define a bunch of strings containing function > definitions > > that I'd feed to exec(). The problem that I have currently is with > numerical > > prefactors and imaginary unit. Sympy functions that generate string form > of > > expressions like to keep it as 'I', thus turning e.g. "(0.5 + .5j)" into > > "(0.5 + 0.5 * I)". I could replace every I with 1j in the output, or > modify > > the value assigned to I, but this is not really robust, and not > efficient, > > since at least there's an extra multiplication. So here's my question: > how > > to generate a string form of an expression where all the numerical > > coefficients would be evaluated into float/complex numbers? > > > > I have found a somewhat suboptimal solution similar to the one described > > over here: > http://www.mail-archive.com/sympy@googlegroups.com/msg05139.html > > . The reason why it is suboptimal is because terms like 0.5 * I will > > transform into 0.5 * 1.j instead of 0.5j (and thus an extra unnecessary > > multiplication is performed). > > > > Thanks, > > Anton Akhmerov > > > > -- > > You received this message because you are subscribed to the Google > Groups > > "sympy" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to sympy+un...@googlegroups.com <javascript:>. > > To post to this group, send email to sy...@googlegroups.com > <javascript:>. > > Visit this group at http://groups.google.com/group/sympy. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/sympy/8586a91c-e70a-43e3-ba70-6a918a6589e7%40googlegroups.com. > > > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/1d67e7a5-59ec-47c0-9b8b-7c03c7a342f5%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.