symengine / SymEngine.jl

Julia wrappers of SymEngine
MIT License
192 stars 43 forks source link

Returning anonymous function when using lambdify instead of using gensym() #272

Closed Zaphragor closed 5 months ago

Zaphragor commented 6 months ago

As I described here https://stackoverflow.com/a/78110549 I had some issues with saving and loading functions created with lambdify, which I solved by using my own version of lambdify returning an anonymous function:

function generate_func(ex, vars = [])
    body = convert(Expr, ex)
    if isempty(vars)
        syms = Symbol.(free_symbols(ex))
    else
        syms = Symbol.(vars)
    end
    fn = eval(:($(Expr(:tuple,syms...)) -> $body));
    return(fn)
end

Now I was wondering if there is any reason not to return anonymous functions in the first place. After briefly skipping threw it looks like one perhaps might just have to change one Line in the definition of _lambdify in subs.jl?

from

fn = eval(Expr(:function,
                  Expr(:call, gensym(), map(Symbol,vars)...),
                       ex))

to

fn = eval(:($(Expr(:tuple,syms...)) -> $body));

This would from my understanding allow the saving and loading of the results from lambdify using Serialization .