evalf behaves differently for E**x and exp(x)

73 views
Skip to first unread message

G B

unread,
Apr 4, 2015, 6:04:21 PM4/4/15
to sy...@googlegroups.com
Hi-- 

exp() doesn't seem to evalf its arguments when evalf is called on it.  This is probably a bug, and if so I'll file an issue.  In the mean time, is there an easy way to get sympy to convert exp(x) expressions to E**x expressions?

exp(t*sqrt(5)).n()  -> exp(t*sqrt(5))

E**(t*sqrt(5)).n() -> E**(2.23606797749979*t)

I think this is causing me some trouble when converting complex expressions to numpy via lambdify.  I'm trying to lambdify a result from dsolve that includes square roots of very large integers (not sure where those large integers are coming from).  When I try to execute the result, I get "AttributeError: 'int' object has no attribute 'sqrt'".

I'll see if I can get the lambdify to trip up with a simple set of inputs.  Right now this is the culmination of a 100 cell IPython notebook...


Aaron Meurer

unread,
Apr 4, 2015, 8:37:24 PM4/4/15
to sy...@googlegroups.com
Seems like another thing that
https://github.com/sympy/sympy/issues/4898 would fix. I think the only
way to create E**x is to use Pow(E, x, evaluate=False).

The lambdify thing might be a separate bug.

Aaron Meurer
> --
> 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.
> To post to this group, send email to sy...@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/e312e981-3e7b-4e6f-ae69-984c7e7ad9fc%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

G B

unread,
Apr 5, 2015, 12:36:23 AM4/5/15
to sy...@googlegroups.com
If I'm understanding my problem correctly, I think I actually want it to evaluate...  Is there a way to ask sympy to find all occurrences of exp() in an expression and replace them with Pow(E,...) or E**?

I've tried messing with replace, but I'm not sure I've got the syntax right...

Alternatively, can I force evalf to drill down into the exp() arguments?

Aaron Meurer

unread,
Apr 5, 2015, 2:36:57 AM4/5/15
to sy...@googlegroups.com
You have to do evaluate=False because Pow(E, x) is automatically
converted to exp() otherwise.

You can use something like expr.replace(exp, lambda x: Pow(E, x,
evaluate=False)).

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/20ecfb7f-0284-4f69-9d0e-4bd8cb21dbbe%40googlegroups.com.

G B

unread,
Apr 21, 2015, 1:39:11 AM4/21/15
to sy...@googlegroups.com
Hey Aaron--

This bug came up to bite me again, so I tried your work around.  The replace you suggested doesn't work (I get the same result back).

What does seem to work is expr.replace(exp, lambda x: Pow(E, N(x)))

That forces the evaluation of the argument to exp, and then carries on.  

Thanks--
Reply all
Reply to author
Forward
0 new messages