# Using Serialization to store then load a lambdified function?

**URL:** <https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884>\
**Category:** General Usage\
**Tags:** question, sympy, serialization\
**Created:** [August 17, 2022, 6:20pm UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884 "2022-08-17T18:20:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pluie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pluie/32/35949_2.png) [@pluie](https://discourse.julialang.org/u/pluie)\
**Post date:** [August 17, 2022, 6:20pm UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/1 "2022-08-17T18:20:31Z")

</div>

I have a lengthy symbolic expression that I find and lambdify in one function once. I hope to use this lambdified function repeatedly in a different function. It takes about a minute for lambdify to execute, so I do not want to have to solve for it repeatedly.

I am trying to lambdify and serialize the lambdified expression in one function (no issue there) and then deserialize it in another function. The function errors at the deserialize step: `UndefVarError: ###274 not defined`.

I have made a simple test setup for illustrative purposes.

Lambdify and serialize:

```julia
using Serialization
using SymPy

@syms a b c 
func = a^2 + b^3-c/a 
funky = lambdify(func,(a,b,c))
sol1 = funky(1,2,3)

fname = string("testSerial",".txt")
serialize(fname,funky)

```

Then, in a new instance of Julia

```julia
using Serialization
using SymPy
fname = "testSerial.txt"  
tfunc = deserialize(fname)

sol2 = tfunc(1,2,3)

```

which errors at the deserialize step.

Does anyone have any insights into this issue? Or a proposed better way to accomplish my goal?

Thank you.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 18, 2022, 1:05am UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/2 "2022-08-18T01:05:44Z")

</div>

The best solution could be to use [Symbolics.jl](https://github.com/JuliaSymbolics/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:

```julia
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

```julia
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.

---

<div class="post-metadata">

**Author:** ![pluie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pluie/32/35949_2.png) [@pluie](https://discourse.julialang.org/u/pluie)\
**Post date:** [August 18, 2022, 2:34pm UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/3 "2022-08-18T14:34:48Z")

</div>

@SteffenPL Thank you! That is very helpful. However, do you happen to know if there is a way to convert SymPy variables/expressions to expressions accepted by Symbolics?

I have another function that builds up a SymPy expression using some of SymPy’s trig functions that I don’t think have an equivalent in Symbolics.jl. This expression is being ported into the (now) Symbolics function I have alluded to in this question. The `build_function` keeps SymPy variables as SymPy variables rather than converting them to Symbolics `Num` type.

I have done a search for both these functionalities and have not found anything.

Edit: Also, Symbolics seems to lack SymPy’s ability to convert an expression to a polynomial and see the terms (p.terms()) of that polynomial.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 18, 2022, 2:51pm UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/4 "2022-08-18T14:51:07Z")

</div>

I’m not sure if there is a direct way.

One thing you could just try is to use:

```julia
using SymPy, Symbolics

@syms a b c 
func = a^2 + b^3-c/a 
funky = lambdify(func,(a,b,c))

@variables a b c 
func_symbolics = funky(a,b,c)

# ... and then proceed as posed with Symbolics...

```

(But it depends a bit on your particular example.)

I wouldn’t be surprised if there are ways do it with Symbolics and some related packages,  
for example it should definitely be possible to make a polynomial.  
Anyway, that’s in the end your decision. But feel free to post examples and we can see if it works.

* * *

By the way, feel free to uncheck my post as solution if you are looking for another one 😉

---

<div class="post-metadata">

**Author:** ![pluie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pluie/32/35949_2.png) [@pluie](https://discourse.julialang.org/u/pluie)\
**Post date:** [August 18, 2022, 2:59pm UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/5 "2022-08-18T14:59:12Z")

</div>

You can definitely make a polynomial. But SymPy let’s you append the handle for the polynomial with `.terms()` which yields a tuple containing, for each term, the coefficient and powers of each variable in that term.

I will try out that suggestion though!

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 18, 2022, 3:07pm UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/6 "2022-08-18T15:07:22Z")

</div>

Yes, I’m also a big fan of both Symbolics and SymPy 😉

Just in case, there is this function:

```julia
using Symbolics
@variables x y
polynomial_coeffs(x+y+2x*y, [x,y]) 
# = (Dict{Any, Any}(y => 1, x => 1, x*y => 2), 0)

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [August 18, 2022, 5:46pm UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/7 "2022-08-18T17:46:15Z")

</div>

Maybe convert to an Expr and save that. It can be evaled into a function after desrialization.

---

<div class="post-metadata">

**Author:** ![shce](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@shce](https://discourse.julialang.org/u/shce)\
**Post date:** [January 30, 2023, 11:08am UTC](https://discourse.julialang.org/t/using-serialization-to-store-then-load-a-lambdified-function/85884/8 "2023-01-30T11:08:26Z")

</div>

I know the topic has been marked as solved but I have found myself with a similar problem and I would like to comment my workaround.

The solution proposed herein does not work in my case since I want to include the symbolic computation inside a function (I know it is technically possible to make a include local with some macros, etc; but I think my solution is neater).

Actually, the computational cost is in the symbolic computation, so it is possible to serialize the symbolic function, and then later, deserialize and lambdify. In this way, you don’t need Symbolics package, just SymPy and Serialization.

Following, you can find the code to do so. You may adapt it as you wish, of course.

```julia
using SymPy
using Serialization
using Random

struct myVars
    x
end

function myVars(dim::Int)
    x = [SymPy.Sym(string("x",d)) for d in 1:dim]
    return myVars(x)
end

function doComputationsToExport()
    A = myVars(3)
    x = A.x
    func = x[1]^2 + x[2]^3-x[3]/x[1]
    funky = lambdify(func,[x...])
    myExport(func)
    return funky
end

function myExport(func)
    Serialization.serialize("testSerializeSymPy", func)
end

function doImports()
    A = myVars(3)
    x = A.x
    g = Serialization.deserialize("testSerializeSymPy")
    funky = lambdify(g,[x...])
    return funky
end

function main()
    Random.seed!(1234)
    rrr = [rand(), rand(), rand()]
    try
        funky = doImports()
        y = funky(rrr...)
        println("g(rrr) = ", y)
    catch
        funky = doComputationsToExport()
        y = funky(rrr...)
        println("f(rrr) = ", y)
    end
end

main()

```
