How to build functions from symbolic expressions with symbolics.jl

I want to build functions from symbolic expressions with symbolics.jl. I tried the following according to the documentation instructions.

using Symbolics
@variables x
p=(1+x)^2
f_expr=build_function(p,x)
f=eval(f_expr)
f(1) # get 4

It can run in REPL, but not in the function block. How do I implement it in a function block?

2 Likes

I believe you see something like

The applicable method may be too new: running in world age 32545, while current world is 32546.

You could try something like

using Symbolics
function test()
    @variables x
    p=(1+x)^2
    f_expr=build_function(p,x)
    f=eval(f_expr)
    Base.@invokelatest f(1) # get 4
end
test()

Just do f=build_function(p,x, expression = Val{false}) like the docs say and you won’t have world age issues.

4 Likes

It does work. Thanks a lot

1 Like