Using Serialization to store then load a lambdified function?

The best solution could be to use Symbolics.jl instead.
The build_function is designed to make saving of generated functions easy. (And you could even generate C or Matlab source code…)

  • First step: Creating the expression and saving it as a .jl file:
using Symbolics
@variables a b c
func = a^2 + b^3-c/a

# create the function expression
f_expr = build_function(func, [a,b,c])

# Note: To use the function, one needs to apply eval:
f = eval(f_expr)
y = f([1,2,3])

# to save the function, use the expression!
write("func.jl", string(f_expr))
  • To load the function in a new session, you can simply include the Julia file
f = include("func.jl")
y = f([1,2,3])

A nice side effect is also that you don’t need any package to load the function
and you can even inspect the definition of the generated function in the file.

2 Likes