# Symbolics.jl: How to preevaluate a computationally expensive expression in build\_function()

**URL:** https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147
**Category:** Modelling & Simulations
**Tags:** question, package
**Created:** [December 2, 2022, 2:19pm UTC](https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147 "2022-12-02T14:19:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![davidschlegel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidschlegel/32/29711_2.png) [@davidschlegel](https://discourse.julialang.org/u/davidschlegel)
#### Post date: [December 2, 2022, 2:19pm UTC](https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147/1 "2022-12-02T14:19:40Z")

</div>

I like the build\_function() method of Symbolics.jl which basically bakes symbolic expressions into compiled code.  
However, it does so by compiling the full expression which might be numerically highly inefficient.  
Suppose I have a symbolic expression that depends on some computationally expensive function f(x). In the symbolic expression, denoted by g(x, f(x)), f(x) is called multiple times.  
Here is my question: Is there a way to preevaluate the value of f(x) once and then only reference to it by using build\_function()?  
Right now a workaround is to define

```julia
@variables x y
g_compiled = build_function(g(x, y), x, y)[1]

```

and then create a function that does the substitution:  
`g_final(x) = g_compiled(x, f(x))`  
This way, f(x) is only computed once and then accessed multiple times as desired. I however find this way of writing this a bit cumbersome. Is there a better way (e.g by registering a function to avoid the reduction to primitives?)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 2, 2022, 2:36pm UTC](https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147/2 "2022-12-02T14:36:34Z")

</div>

You can try setting `cse=true` in `build_function`. I’m not sure if that ever was completed though? @shashi @YingboMa

---

<div class="post-metadata">

### Author: ![davidschlegel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidschlegel/32/29711_2.png) [@davidschlegel](https://discourse.julialang.org/u/davidschlegel)
#### Post date: [December 2, 2022, 2:48pm UTC](https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147/3 "2022-12-02T14:48:46Z")

</div>

This is unfortunately not well documented. As far as I can see `cse=true` just unwraps the expression.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 2, 2022, 2:55pm UTC](https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147/4 "2022-12-02T14:55:40Z")

</div>

Indeed it’s underdocumented. @shashi let’s chat about this on Thursday.

---

<div class="post-metadata">

### Author: ![davidschlegel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidschlegel/32/29711_2.png) [@davidschlegel](https://discourse.julialang.org/u/davidschlegel)
#### Post date: [December 2, 2022, 4:21pm UTC](https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147/5 "2022-12-02T16:21:05Z")

</div>

I think it works with setting `cse=true`. It would be super nice to add that in the documentation. Is there a reason why this isn’t the default option? I think, this could speed things up a lot if the function is properly declared.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [December 2, 2022, 4:23pm UTC](https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147/6 "2022-12-02T16:23:30Z")

</div>

> [@davidschlegel](#):
>
> Is there a reason why this isn’t the default option? I think, this could speed things up a lot if the function is properly declared.

I don’t know, hence the discussion to be had.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [December 2, 2022, 6:23pm UTC](https://discourse.julialang.org/t/symbolics-jl-how-to-preevaluate-a-computationally-expensive-expression-in-build-function/91147/7 "2022-12-02T18:23:30Z")

</div>

Yeah, cse is mostly broken atm, I have opened a few issues with examples that could use some improvement

- [CSE does not make use of previously found subexpressions when calculating new ones · Issue #745 · JuliaSymbolics/Symbolics.jl · GitHub](https://github.com/JuliaSymbolics/Symbolics.jl/issues/745)
- [cse fails to do anything when building sparse matrices · Issue #743 · JuliaSymbolics/Symbolics.jl · GitHub](https://github.com/JuliaSymbolics/Symbolics.jl/issues/743)
- [`cse` on arrays of expressions · Issue #441 · JuliaSymbolics/Symbolics.jl · GitHub](https://github.com/JuliaSymbolics/Symbolics.jl/issues/441)
