# Creating Julia function from a symbolic expression (using SymEngine)

**URL:** <https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [March 4, 2019, 7:00pm UTC](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476 "2019-03-04T19:00:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedxa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedxa/32/488_2.png) [@fedxa](https://discourse.julialang.org/u/fedxa)\
**Post date:** [March 4, 2019, 7:00pm UTC](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476/1 "2019-03-04T19:00:40Z")

</div>

Hello, what would be a good way to define a Julia function based on the result of symbolic calculations?

For the moment, using `lambdify` in `SymEngine` gives a rather slow result, as compared to “brute force” parsing of the SymEngine output:

```julia
using SymEngine
@vars x

# Complicated symbolic expression here
symf = x

fl = lambdify(symf, [x])

eval(Meta.parse("fm(x)="*SymEngine.toString(symf)))

```

This defines two functions `fl` and `fm` which evaluate the expression in `symf`. However, the variant with parsing the output is 20 times faster than `lambdify`:

```julia
@time sum(fl(y) for y in LinRange(0., 1., 10000000))

```

```julia
  0.924081 seconds (50.00 M allocations: 762.940 MiB, 3.03% gc time)

```

```julia
@time sum(fm(y) for y in LinRange(0., 1., 10000000))

```

```julia
  0.041786 seconds (7 allocations: 272 bytes)

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 4, 2019, 8:37pm UTC](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476/2 "2019-03-04T20:37:27Z")

</div>

> [@fedxa](#):
>
> @time sum(fl(y) for y in LinRange(0., 1., 10000000))

Don’t benchmark in global scope.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 4, 2019, 8:38pm UTC](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476/3 "2019-03-04T20:38:22Z")

</div>

> [@fedxa](#):
>
> @time sum(fm(y) for y in LinRange(0., 1., 10000000))

Although in this case it doesn’t matter.

---

<div class="post-metadata">

**Author:** ![fedxa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedxa/32/488_2.png) [@fedxa](https://discourse.julialang.org/u/fedxa)\
**Post date:** [March 4, 2019, 8:47pm UTC](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476/4 "2019-03-04T20:47:36Z")

</div>

The real world problem was in local scope 🙂

---

<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:** [March 4, 2019, 10:11pm UTC](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476/5 "2019-03-04T22:11:42Z")

</div>

I would guess the issue is in the call to `invokelatest` [here](https://github.com/symengine/SymEngine.jl/blob/master/src/subs.jl#L159).

Using this modified function definition seems to have better performance, on par with an anonymous function:

```julia
Lambdify(ex, vars=free_symbols(ex)) = eval(Expr(:function,
                         Expr(:call, gensym(), map(Symbol,vars)...),
                              convert(Expr, ex)))

```

But if I recall correctly, calling `invokelatest` was necessary to avoid a world age problem.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [March 5, 2019, 2:21am UTC](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476/6 "2019-03-05T02:21:33Z")

</div>

World age problem depends on which context scope you call it from.

---

<div class="post-metadata">

**Author:** ![fedxa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedxa/32/488_2.png) [@fedxa](https://discourse.julialang.org/u/fedxa)\
**Post date:** [March 5, 2019, 9:09am UTC](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476/7 "2019-03-05T09:09:13Z")

</div>

Yes, seems this is the reason.

Maybe it is a good idea to add this as “lambdify\_fast” to SymEngine.jl, or rename the curent version to “lambdify\_robust” ?
