Skip to content

Lambdifying a parametric integral leads to integrand symbol not being defined #461

Closed
jverzani/SymPyCore.jl
#80
@notchxu

Description

@notchxu
using QuadGK

@syms w, t

function integral_with_quad(expr, lims)
    v, a, b = lims
    return quadgk(lambdify(expr, (v,)), a, b)
end

ex = sympy.Integral(t^2, (t,0,w))
fn = lambdify(ex, fns=Dict("Integral" => integral_with_quad))
fn(5)

Attempting to evaluate fn(5) leads to error UndefVarError: t not defined. However, this should not be the case, since the only free symbol in ex is w. The same error occurs if I don't try to replace Integral with the quadgk numerical.

Attempting to fix this by replacing the final two lines with

fn = lambdify(ex, (w,t), fns=Dict("Integral" => integral_with_quad))
fn(5,t)

leads to error MethodError: no method matching iterate(::Symbol) with corresponding stack trace

Stacktrace:
 [1] expr_to_function(body::Expr, vars::Symbol)
   @ SymPy lambdify.jl:242
 [2] lambdify(ex::Sym, vars::Symbol; fns::Dict{Any, Any}, values::Dict{Any, Any}, use_julia_code::Bool, invoke_latest::Bool)
   @ SymPy lambdify.jl:217
 [3] lambdify(ex::Sym, vars::Symbol)
   @ SymPy lambdify.jl:216
 [4] integral_as_quad(f::Sym, lims::Tuple{Symbol, Int64, Symbol})
   @ Main .\REPL[18]:3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions