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
@variables x y g_compiled = build_function(g(x, y), x, y)
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?)