On 11 November 2011 02:26, Aaron Meurer <[email protected]> wrote:

> On Thu, Nov 10, 2011 at 8:58 AM, [email protected]
> <[email protected]> wrote:
> > Hi,
> >
> > I hope Aaron could comment as this concerns the plotting problem he was
> > working on (pull request 696).
> >
> > So for the last day or so I was trying to understand lambdify. According
> to
> > the docstring it's used for numeric calculations - that's the whole idea
> > behind translating sympy functions to mpmath, numpy, etc.
> >
> > Also according to the docstring lambdify is used for _fast_ calculations.
> > That's why it's not simply a shortcut for .subs(vars).evalf() which is so
> > much simpler but calls subs on every evaluation. (The guy that has
> created
> > the plotting module few years ago has some benchmarks on his blog)
> >
> > And it seems that the original idea behind lambdify was to be used in the
> > plotting framework (see the author of the first commit in git log),
> giving
> > numerical values for the coordinates to be plotted. If I'm incorrect here
> > please correct me.
> >
> > But all this falls apart when I look at the tests:
> >
> > - the most hard for me to understand are the following:
> >  256     f = lambdify(x, x * y)
> >  257     assert f(z) == z * y
> > and
> >  250     f = Lambda(x, exp(-x**2))
> >  251     l = lambdify(x, Integral(f(x), (x, -oo, oo)), modules="sympy")
> >  252     assert l(x) == Integral(exp(-x**2), (x, -oo, oo))
> >
> > Why would anyone ever want to do this instead of using a simple python
> > lambda? There is no translation to numpy or other modules. The only thing
> > that can happen is to mix numpy functions with sympy expressions, but why
> > would someone want to do this?
> >
> > An enormous amount of the tests for lambdify show behaviour that
> _should_ be
> > implemented with python lambda. And that makes lambdify that much harder
> to
> > implement. Why should lambdify care about lists, tuples, dictionaries and
> > whatnot when even in the docstring it's said that it's used for numeric
> > computation?
> >
> >
> > Or probably I'm simply missing the point of lambdify. Could some one
> explain
> > it to me?
>
> If you look at the implementation of lambdify, and LambdaPrinter, it
> is just a wrapper around Python lambda that does some intelligent
> translation when going to other modules (like numpy).  Actually, it's
> particularly nice for using numpy or some other module because
> lambdify([x], sin(x), "numpy")(1) will use numpy's sin() function to
> evaluate sin(1), not mpmath.
>
I've looked at this and I agree, the problem is when not used to make
translations.

>
> But in general, I think the reason to use it instead of a straight
> Python lambda is because it will be cleaner, and also because for
> those cases where those translations matter, you don't have to worry
> about them.
>

About the translations I completely agree. The problem is that most of the
code assumes that lambdify is for numeric computations (most of the code
that I have seen). The same thing is stated in the docstring. But then the
tests permit stuff like this:
 250     f = Lambda(x, exp(-x**2))
 251     l = lambdify(x, Integral(f(x), (x, -oo, oo)), modules="sympy")
 252     assert l(x) == Integral(exp(-x**2), (x, -oo, oo))
Which seems wrong to me as it's not a numeric computation. The plotting
module has problems with this, and if this test is to be taken as correct
the fix for the plotting module is not trivial (creating a function
lambdify_numeric maybe?)

>
> And by the way, I'm not the best person to ask about lambdify(), as
> I've only used it a little; and similarly with the plotting: I only
> have used it a little (for the plotting, it's mostly because it
> usually doesn't work on Mac OS X, so I don't bother.  I definitely
> would use it more if it worked).  That pull request was fixing someone
> else's problem from IRC, for which I was immediately able to see the
> problem.
>
> Aaron Meurer
>
> >
> >
> > notes:
> > the integral tests comes from this commit:
> > commit 558c862ac98e58a1d27bdc57a777d355cabee28d
> > Author: Ondrej Certik <[email protected]>
> > Date:   Sun Mar 29 17:49:24 2009 -0700
> >     Make lambdify() work with Integral
> > but it does not address Sums, Products, any single one quantum
> expression,
> > etc. Why is integral so special (obviously those were not needed until
> know,
> > but the proposed solution does not scale at all)
> >
> > --
> > 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.
> >
>
> --
> 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.
>
>
Do you think this should be permitted to happen:
In [1]: expr = sin(x)*Integral(x*y, (y, 0, 1))

In [2]: l = lambdify([x], expr)

In [3]: l(2)
Out[3]:

                              1
                              ⌠
0.909297426825682⋅⎮ 2⋅y dy
                              ⌡
                              0

In [4]: l(x)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/stefan/Workspace/sympy/<ipython-input-4-ac45276a6cb0> in <module>()
----> 1 l(x)

/usr/lib/pymodules/python2.7/numpy/__init__.pyc in <lambda>(x)

/home/stefan/Workspace/sympy/sympy/core/expr.pyc in __float__(self)
    159             return float(result)
    160         else:
--> 161             raise ValueError("Symbolic value, can't compute")
    162
    163     def __complex__(self):

ValueError: Symbolic value, can't compute

Because of the mix of expectations (numeric evaluation vs any possible
lambda expression) lambdify is trying to do two things that don't have much
in common. And as far it's used for only one of those (numeric
computations) and the other is much better done with a python lambda
expression (because no one would do translations to numpy unless numeric
evaluations are wanted). If I'm wrong please correct me.

As a workaround for the moment I'm creating lambda_numeric for use in the
plotting module (or anywhere with a need for purely numeric calculations).

-- 
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