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.akhme...@gmail.com> 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+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/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/CAKgW%3D6KQ_YEUsOZpwgVbqsxbtc1iSQU2gqwY8-SsifQ%2BZSpkCA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to