# SymEngine - turn to function

**URL:** https://discourse.julialang.org/t/symengine-turn-to-function/28907
**Category:** New to Julia
**Created:** [September 18, 2019, 6:25pm UTC](https://discourse.julialang.org/t/symengine-turn-to-function/28907 "2019-09-18T18:25:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 18, 2019, 6:25pm UTC](https://discourse.julialang.org/t/symengine-turn-to-function/28907/1 "2019-09-18T18:25:10Z")

</div>

Hey guys!

I have derived a symbolic array as:

```julia
Bmat
3×8 Array{Basic,2}:
 (1/4)*y/(a*b) + (-1/4)*a^(-1) 0 (-1/4)*y/(a*b) + (1/4)*a^(-1) … 0 (-1/4)*y/(a*b) + (-1/4)*a^(-1) 0
                             0 (1/4)*x/(a*b) + (-1/4)*b^(-1) 0 (1/4)*x/(a*b) + (1/4)*b^(-1) 0 (-1/4)*x/(a*b) + (1/4)*b^(-1)
 (1/4)*x/(a*b) + (-1/4)*b^(-1) (1/4)*y/(a*b) + (-1/4)*a^(-1) (-1/4)*x/(a*b) + (-1/4)*b^(-1) (1/4)*y/(a*b) + (1/4)*a^(-1) (-1/4)*x/(a*b) + (1/4)*b^(-1) (-1/4)*y/(a*b) + (-1/4)*a^(-1)

```

How would I turn this into a function of x,y,a,b using the SymEngine.jl package?

Kind regards

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 18, 2019, 6:46pm UTC](https://discourse.julialang.org/t/symengine-turn-to-function/28907/2 "2019-09-18T18:46:39Z")

</div>

I’ve found this temporary solution:

> [@Creating Julia function from a symbolic expression (using SymEngine)](https://discourse.julialang.org/t/creating-julia-function-from-a-symbolic-expression-using-symengine/21476):
>
> 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: 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 par…

```julia
Bf = lambdify(Bmat, [x,y,a,b])

```

But according to the post it might be slow. It works atleast.

Kind regards, if someone else have a better solution I would love to hear

---

<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: [September 18, 2019, 7:24pm UTC](https://discourse.julialang.org/t/symengine-turn-to-function/28907/3 "2019-09-18T19:24:01Z")

</div>

As a followup to the mentioned discussion, you may use the following:

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

```

and then

```julia
Bf = lambdify_fast(Bmat,[x,y,a,b])

```

This one is a bit faster. It is told that it is prone to the “world time problem”, However I am not sure how really to achieve the problem – so feel free to use the faster version

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 18, 2019, 7:26pm UTC](https://discourse.julialang.org/t/symengine-turn-to-function/28907/4 "2019-09-18T19:26:21Z")

</div>

Thanks! I didn’t understand the problem either, but I got the right result (in this case) 🙂 Thanks for taking your time to write it out.

Kind regards
