2009/1/12 Matt <[email protected]>:
>
> Hi Ondrej,
>
> I was using simplify everywhere, and changed it to sympify, though
> that didn't affect the functionality of my code at all. I'll stick
> with sympify for now since it seems to do exactly the same thing as
> simplify (for my purposes).
>
> If I do the following commands, as you suggested it does seem to work
> as expected:
>
> > f=sympify("factorial(x)+sin(y)")
> > x = Symbol('x')
> > y = Symbol('y')
> > f=lambdify((x,y),f,"math")
>
> I'm not sure what documentation implied that I should use subs(), but
> I know I didn't come up with that myself.
>
>> By the "long-ish" dictionary, do you mean this dict: {pi: 3.14, E:
>> 2.72}? (of course when you add all the constants in there, it will
>> get longer)
>
> Exactly what I'm trying to avoid, since I use lambdify in quite a few
> places in my code, so it's neither elegant nor good programming
> practice to have to put such a dictionary into each lambdify. For
> instance, if I wanted to add another constant, I would have to add it
> to every lambdify. Also, the precision of each constant is then
> limited to whatever I use; this is fine if I use constants to 32
> decimal places but, as I said, ugly and poor style.
So write a function that does the conversion for you and then just
call that function. You can also send us a patch, so that sympy can do
that as well.
>
> Here's the full code that generates that gamma-related error:
>
>>>> from sympy import *
>>>> f=sympify('factorial(x)')
>>>> x=Symbol('x')
>>>> f=lambdify(x,f,"math")
>>>> f(10)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<string>", line 1, in <lambda>
> NameError: global name 'gamma' is not defined
>
> However I can use the gamma function itself just fine, e.g.
>>>> gamma(10)
> 362880.
>
> is not an issue. Any ideas here?
Yes, try this:
In [1]: f=sympify('factorial(x)')
In [2]: f=lambdify(x,f)
In [3]: f
Out[3]: <function <lambda> at 0xa41e3ac>
In [4]: f(10)
Out[4]: 3628800.0
Basically by using "math" you tell lambdify to only use the math module.
Ondrej
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---