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.

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?

Thanks!
Matt

On Jan 11, 3:29 pm, "Ondrej Certik" <[email protected]> wrote:
> Hi Matt,
>
> On Sun, Jan 11, 2009 at 11:18 AM, Matt Ball <[email protected]> wrote:
> > Hi,
> > I'm currently using Sympy in a small Python script to take a user input
>
> Thanks for your interest.
>
> > function from the command line and turn it into Python lambda, like this:
> > print "Enter f in ∇²u = f (0 for Laplace case). f is a 2D equation in terms
> > of x and y."
>
> ^^^ That's a cool unicode for \nabla. We should add it to sympy pretty
> printing when we implement div/grad and similar operators soon. I'll
> be needing it soon.
>
> > f = simplify(raw_input("f = "))
>
> Shouldn't this by sympify()? Simplify() makes the input expression
> simpler as well (maybe you want this).
>
> > symbols = f.atoms(Symbol)
> > x = Symbol("x")
> > y = Symbol("y")
> > ...
>
> > return lambdify((x,y),f.subs({'x':x,'y':y}),"math")
>
> what is the meaning of f.subs({'x':x,'y':y}) here? It seems to me the
> above line is equivalent to this:
>
> return lambdify((x,y), f, "math")
>
> or am I missing something?
>
> At least in theory, it doesn't matter which particular instances of
> the Symbol() class you use, if it does, it's a bug.
>
>
>
> > Is there a way to easily, automatically replace reasonable constants, such
> > as e and pi, with their actual value in the python lambda? Right now,
>
> Why not to use .subs({pi: 3.14, E: 2.72})?
>
> > anything that isn't a number is just a symbol, and I would rather not have
> > to have a long-ish dictionary in each lambdify.
>
> 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)
>
> You can convert constants using .evalf(), so maybe we can write some
> function, that does that for all constants automatically.
>
> > Likewise, is there an easy way to automatically enable
> > recognition/functionality of all the functions that Sympy can handle? I
>
> Just use sympify:
>
> In [1]: sympify("factorial(x)+sin(y)")
> Out[1]: Γ(1 + x) + sin(y)
>
> > tried playing around with mpmath, but that failed on even simple cases, such
> > as factorial(x), where the factorial function itself is recognized, but
> > later on when evaluating that function at a given point, I get a NameError:
> > global name 'gamma' is not defined issue (I imagine it has something to do
> > with using the gamma function to evaluate a factorial for non-natural
> > numbers). Thanks for the help again!
>
> Please post here the exact commands you tried --- I am not sure what
> went wrong from your description.
> But for the above things imho what you need is just sympify().
>
> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to