I have a compiled function f
to be used as a piece of a much larger calculation. I would like to expand out a function that calls f
explicitly before evaluating the expanded version numerically. For this reason, I would like to manipulate the compiled function symbolically when inserting symbolic arguments.
As an example, if we define
f = Compile[{x}, 1 + Cos[x], CompilationTarget -> "C"]
I would like to be able to work with f[x]
as an object. E.g., I would like to see
Expand[(1 + f[x])^2]
produce 1 + 2 f[x] + f[x]^2
. However, when I actually run the above code, I receive the error
CompiledFunction::cfsa: Argument x at position 1 should be a machine-size real number.
and the actual output is 4 + 4 Cos[x] + Cos[x]^2
as if f[x]
were evaluated symbolically. Is there a way to compile a function such that it can be manipulated symbolically when symbolic arguments are provided, but numerically when numeric arguments are provided?
memoization
to increase the speed of subsequent uses of your functions, as it sounds like this may assist you in accomplishing your goals. $\endgroup$RuntimeOptions -> {"EvaluateSymbolically" -> False}
. See to docs forCompile
and forRuntimeOptions
for more. $\endgroup$