Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambdify performs unwanted integer division when used with with sympy.sqrt and modules='numexpr' #9871

Open
mvanvleet opened this issue Aug 28, 2015 · 13 comments
Labels
Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. utilities.lambdify

Comments

@mvanvleet
Copy link

The lambdify function does not correctly handle integer vs. floating point division in conjunction with sympy's native sqrt function and the numexpr module. This bug occurs in Python 2.7; Python 3 has not been tested. The following code illustrates the problem:

from __future__ import division
import numpy as np
import sympy as sp
from sympy.utilities import lambdify

x,y,z = sp.symbols("x y z")

# First, define r using sympy's sqrt function
r = sp.sqrt(x**2 + y**2 + z**2)
print sp.diff(1/r,x)

f_numpy = lambdify( (x,y,z), sp.diff(1/r,x), modules='numpy')
f_numexpr = lambdify( (x,y,z), sp.diff(1/r,x), modules='numexpr')

xn = np.arange(1,10)
yn = np.arange(1,10)
zn = np.arange(1,10)
print 'Numpy result:'
print f_numpy(xn,yn,zn)
print 'Numexpr result:'
print f_numexpr(xn,yn,zn)

print

# Next, define r using the 1/2 power function
r = (x**2 + y**2 + z**2)**0.5
print sp.diff(1/r,x)

f_numpy = lambdify( (x,y,z), sp.diff(1/r,x), modules='numpy')
f_numexpr = lambdify( (x,y,z), sp.diff(1/r,x), modules='numexpr')
print 'Numpy result:'
print f_numpy(xn,yn,zn)
print 'Numexpr result:'
print f_numexpr(xn,yn,zn)

which outputs:

-x/(x**2 + y**2 + z**2)**(3/2)
Numpy result:
[-0.19245009 -0.04811252 -0.02138334 -0.01202813 -0.007698   -0.00534584
 -0.00392755 -0.00300703 -0.00237593]
Numexpr result:
[0 0 0 0 0 0 0 0 0]

-1.0*x*(x**2 + y**2 + z**2)**(-1.5)
Numpy result:
[-0.19245009 -0.04811252 -0.02138334 -0.01202813 -0.007698   -0.00534584
 -0.00392755 -0.00300703 -0.00237593]
Numexpr result:
[-0.19245009 -0.04811252 -0.02138334 -0.01202813 -0.007698   -0.00534584
 -0.00392755 -0.00300703 -0.00237593]

Floating point division should occur in all 4 test cases, however integer division is being performed when sp.sqrt and the numpexpr module are used.

@moorepants moorepants added utilities.lambdify Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. labels Aug 28, 2015
@rresol
Copy link
Contributor

rresol commented Sep 10, 2015

I would like to fix it.

@rresol
Copy link
Contributor

rresol commented Sep 11, 2015

Do we need to be specific while printing ?I mean is it necessary to return a value of type 'int' if the input is of type 'int'.

@moorepants
Copy link
Member

Do we need to be specific while printing ?

Not sure what this means.

I mean is it necessary to return a value of type 'int' if the input is of type 'int'.

The behavior should match the lambdify backend. If it is using numpy, for example, then if should do the same thing numpy functions do to ints.

@harshsavasil
Copy link

@moorepants i tried running the code through python debugger but i couldn't access the source code being executed.. i want to fix this bug. can u suggest something ?

@moorepants
Copy link
Member

You can use the lambdarepr printer to see what source code is generated.

@Curious72
Copy link
Contributor

@moorepants can you tell exactly how to use lambdarepr !! i somehow want to access the code being accessed by the line print f_numexpr(xn,yn,zn) , the debugger cannot access the code for this line !! how should we go about solving this !!

@harshsavasil
Copy link

@moorepants The problem is with necompile.py in numexpr repo where the default value of truediv is set to False, Because of which integer division is taking place..

@moorepants
Copy link
Member

Maybe this needs to be set to true or false depending on the Python version?

@Shekharrajak
Copy link
Member

It seems numpy and numexpr handling error part .Am I right ?

@harshsavasil
Copy link

@moorepants did u check it with other versions of python ?

@moorepants
Copy link
Member

Nope.

@harshsavasil
Copy link

@moorepants I think a issue must be raised in numexpr to fix this ?and this should be closed !! because ultimately the bug is numexpr repo.

@moorepants
Copy link
Member

If you think that is the case, then open an issue there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Easy to Fix This is a good issue for new contributors. Feel free to work on this if no one else has already. utilities.lambdify
Projects
None yet
Development

No branches or pull requests

6 participants