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(..., cse=True) evaluates the expression before printing #24246

Open
Davide-sd opened this issue Nov 11, 2022 · 1 comment
Open

lambdify(..., cse=True) evaluates the expression before printing #24246

Davide-sd opened this issue Nov 11, 2022 · 1 comment

Comments

@Davide-sd
Copy link
Contributor

When using lambdify with cse=True, it appears that the expression might get evaluated before the printer generates the code.

In the following example I'm using mpmath because it uses arbitrary precision floating point, hence it is able to deal with huge numbers. But the following behavior also happens when using Numpy/Scipy or math.

Why am I using cse=True? A user can pass any expression to my application, which must be evaluated as quickly as possible. My application assumes that cse=True is not going to make things worst: sometime it is useful, other times it is superfluous, as it should be in following example. Instead, here it is making things worse:

from sympy import *
var("x")

expr = (factorial(365, evaluate=False) / factorial(365 - x)) / 365**x
f1 = lambdify(x, expr, cse=False, modules="mpmath")
f2 = lambdify(x, expr, cse=True, modules="mpmath")

import inspect
print("f1 code")
print(inspect.getsource(f1))
# f1 code
# def _lambdifygenerated(x):
#     return 365**(-x)*factorial(365)/factorial(365 - x)

print("f2 code")
print(inspect.getsource(f2))
# f2 code
# def _lambdifygenerated(x):
#     return 25104128675558732292929443748812027705165520269876079766872595193901106138220937419666018009000254169376172314360982328660708071123369979853445367910653872383599704355532740937678091491429440864316046925074510134847025546014098005907965541041195496105311886173373435145517193282760847755882291690213539123479186274701519396808504940722607033001246328398800550487427999876690416973437861078185344667966871511049653888130136836199010529180056125844549488648617682915826347564148990984138067809999604687488146734837340699359838791124995957584538873616661533093253551256845056046388738129702951381151861413688922986510005440943943014699244112555755279140760492764253740250410391056421979003289600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*365**(-x)/factorial(365 - x)

print(f1(5))
print(f2(5))
0.972864426300207

---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Input In [11], in <cell line: 2>()
      1 print(f1(5))
----> 2 print(f2(5))

File <lambdifygenerated-9>:2, in _lambdifygenerated(x)
      1 def _lambdifygenerated(x):
----> 2     return 25104128675558732292929443748812027705165520269876079766872595193901106138220937419666018009000254169376172314360982328660708071123369979853445367910653872383599704355532740937678091491429440864316046925074510134847025546014098005907965541041195496105311886173373435145517193282760847755882291690213539123479186274701519396808504940722607033001246328398800550487427999876690416973437861078185344667966871511049653888130136836199010529180056125844549488648617682915826347564148990984138067809999604687488146734837340699359838791124995957584538873616661533093253551256845056046388738129702951381151861413688922986510005440943943014699244112555755279140760492764253740250410391056421979003289600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*365**(-x)/factorial(365 - x)

OverflowError: int too large to convert to float
@asmeurer
Copy link
Member

It's really cse() that is doing this. lambdify() is just calling cse().

(by the way, factorial(x)/factorial(x - k) can be represented more concisely and efficiently as ff(x, k)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants