sympy icon indicating copy to clipboard operation
sympy copied to clipboard

dummify argument in lambdify should be smarter

Open asmeurer opened this issue 7 years ago • 13 comments

The default option in lambdify is dummy=True, which is handles names that aren't valid Python identifiers, like functions, or symbols with weird symbols in them.

However, this option breaks introspection for the lambdified function.

>>> x, y = symbols('x, y')
>>> f = lambdify((x, y), x + y**2)
>>> help(f)

Shows

Help on function <lambda> in module numpy:

<lambda> lambda _Dummy_123, _Dummy_124
    Created with lambdify. Signature:

    func(x, y)

    Expression:

    x + y**2

Whereas with

>>> f = lambdify((x, y), x + y**2, dummify=False)
>>> help(f)

you get

Help on function <lambda> in module numpy:

<lambda> lambda x, y
    Created with lambdify. Signature:

    func(x, y)

    Expression:

    x + y**2

I think the default should be to dummify only if the names are not valid Python identifiers.

asmeurer avatar Mar 30 '17 22:03 asmeurer

It also seems to break numba.jit (although that has other issues too, see https://github.com/numba/numba/issues/2328).

asmeurer avatar Mar 30 '17 22:03 asmeurer

@asmeurer , I would like to work on this issue

mohit3011 avatar Mar 30 '17 22:03 mohit3011

@asmeurer , could you please specify what does smarter means here ?

mohit3011 avatar Mar 31 '17 17:03 mohit3011

As I said above, it should only apply when it needs to, i.e., when the names aren't valid Python identifiers.

asmeurer avatar Mar 31 '17 17:03 asmeurer

Could you please refer any starting point in utilities/lambdify.py ?

mohit3011 avatar Mar 31 '17 17:03 mohit3011

@asmeurer shouldn't it supposed to throw errors with invalid python identifiers, If you want to set dummify True only for non-python identifiers then we should add a check to see if the identifier is valid or invalid.

SatyaPrakashDwibedi avatar Mar 31 '17 19:03 SatyaPrakashDwibedi

This pull request is related https://github.com/sympy/sympy/pull/11457.

asmeurer avatar Mar 31 '17 19:03 asmeurer

This was fixed by https://github.com/sympy/sympy/pull/14713

asmeurer avatar May 23 '18 23:05 asmeurer

needs a test added for this issue

smichr avatar Sep 01 '18 12:09 smichr

I am working on documenting this at https://github.com/sympy/sympy/pull/13485. It's odd that the default is False instead of None for lambdify, even though the default behavior seems to be to only dummify when necessary. For lambdastr, the default is None.

asmeurer avatar Jan 22 '19 23:01 asmeurer

Probably, this and this suggest that the work on this issue is complete. Though I am adding a Could Close label. @asmeurer

czgdp1807 avatar Sep 25 '19 17:09 czgdp1807

Closing this in reference to the above comment. Please let us know if there is a need to re-open this. Thanks for your contributions.

czgdp1807 avatar Jan 17 '20 18:01 czgdp1807

The default for dummify right now is False, but it should be None, which would automatically dummify names that require it, but nothing else (True would still dummify everything).

asmeurer avatar Jul 19 '22 16:07 asmeurer