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

Errors when using Lambdify on sympy.Sum #295

Closed
MostlyHarmless420 opened this issue Sep 6, 2019 · 3 comments
Closed

Errors when using Lambdify on sympy.Sum #295

MostlyHarmless420 opened this issue Sep 6, 2019 · 3 comments

Comments

@MostlyHarmless420
Copy link

MostlyHarmless420 commented Sep 6, 2019

I have encountered a couple of errors when using lambdify and Sum:
When summing over a symbol, it brings up an error:

using Sympy

x = symbols("x")

lambdify(sympy.Sum(x, (x,0,10)))() #>UndefVarError: x not defined


Also when using euler_maclaurin summing over certain functions it produces an error:

x, y = symbols("x y")
lambdify(sympy.Sum(x*y, (x,0,10)).euler_maclaurin(n=3)[1])(3.) # Works fine
lambdify(sympy.Sum(sympy.exp(x), (x,0,10)).euler_maclaurin(n=3)[1])() #Works fine
lambdify(sympy.Sum(sympy.exp(x*y), (x,0,10)).euler_maclaurin(n=3)[1])(3.) #MethodError: no method matching &(::Bool, ::Sym)

@jverzani
Copy link
Collaborator

jverzani commented Sep 6, 2019

Okay, thanks for reporting. I made some adjustments in #296 that address these last cases.

The first did lead to a useful change, but is still problematic: well, Sum(x, (x,0,10)) doesn't have any free variables; but that isn't it, reallySum(x, (x,0,10)) does not have a Julia counterpart (but Sum(x, (x,0,10)).doit() does), so the converted expression refers to sympy objects

convert(Expr, sympy.Sum(x, (x,0,10)))

That Sum is a problem, but so is x. You can bypass these manually by creating the function in the calling module, along the lines of:

Sum = sympy.Sum
x = symbols("x")
ex = Sum(x, (x,0,10))
ux = convert(Expr, ex)
fn = eval(Expr(:function, Expr(:call, gensym()), ux))
fn()  # a SymPy object though

@MostlyHarmless420
Copy link
Author

MostlyHarmless420 commented Sep 7, 2019

Thanks for looking into it.
Just tested the latest #master and, while not receiving an error any more, Lambdify isn't properly substituting for certain functions and is returning a Sym type.

x, y = symbols("x y")
lambdify(sympy.Sum(sympy.exp(x*y), (x,0,10)).euler_maclaurin(n=3)[1])(3.)  #Returns: 7699986595795.14 + exp(10*y)/y - 1/y

@jverzani
Copy link
Collaborator

jverzani commented Sep 9, 2019

Hmm, there are two errors, one I can work around, one I'm stumped with:

x, y = symbols("x y")
ex = sympy.Sum(sympy.exp(x*y), (x,0,10)).euler_maclaurin(n=3)[1]

ux = convert(Expr, ex)

## ux = :(1 / 2 + (1 / 2) * exp(10y) + -1//12y + -1//30240 * y ^ 5 + 1//720 * y ^ 3 + -1//720 * y ^ 3 * exp(10y) + 1//12 * y * exp(10y) + 1//30240 * y ^ 5 * exp(10y) + ifelse(&(y > -Inf, y < Inf, y !== 0), exp(10*y)/y - 1/y, ifelse(true, 10, nothing)))

## The  `&(...)` is an issue we can work around
ALL(xs...) = all(xs)
ux = SymPy.walk_expression(ex, fns=Dict("And" => :ALL))

## then
fn = eval(Expr(:function, Expr(:call, gensym(), :y), ux))

fn(3.0) # has a `y`

## But, directly this works (avoiding) eval...

fn = y -> (1 / 2 + (1 / 2) * exp(10y) + -1//12y + -1//30240 * y ^ 5 + 1//720 * y ^ 3 + -1//720 * y ^ 3 * exp(10y) + 1//12 * y * exp(10y) + 1//30240 * y ^ 5 * exp(10y) + ifelse(ALL(y > -Inf, y < Inf, y !== 0), exp(10*y)/y - 1/y, ifelse(true, 10, nothing)))
fn(3.0)

jverzani added a commit to jverzani/SymPy.jl that referenced this issue Jun 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants