# Problem compiling derivative of expression

I need to compile functions for an expression and its derivative and then pass these on to a precompiled worker loop for further processing. An If statment detects when the expression parameter expr has changed and then recompiles the functions before passing them on to the worker loop. The logic for the If statement works (simplified below), and the compilation of cFastFunc works. But compilation of derivative cFastFuncPrim throws all sorts of error messages whatever I try. Either it says D is not compilable, or it complains about Function, or something else. How to do it please?

My stub for fFast looks like this:

fFast[expr_ , a_ ?NumericQ, t_ ?NumericQ, n_?NumericQ] := (
(*Recompile the function and its derivative if it has changed*)
If[(ConditionDetectingIfExprHasChanged),
cFastFunc = FunctionCompile[Function[Typed[x, "Real64"], expr]]; (* OK *)
cFastFuncPrim = FunctionCompile[Function[Typed[x, "Real64"], Function @@ {x, D[expr, x]}]];  (* Problem! *)
]; (* End IF *)
cFastWorker[cFastFunc, cFastFuncPrim, a, t, n] (* TBD - Precompiled worker loop*)
);


In addition to what Domen wrote in their post, I strongly recommend to scope the variable in the expression to make all this less brittle. Currently, some user of your code simply needs to assign some value to the variable x and everything will break.

So instead of handing over an expression dependent on x, rather hand over a Function. Then compute the derivative by using a scoped variable x like so:

f = x |-> x^2;

compileDerivative[f_] := Block[{x},
With[{Df = D[f[x], x]},
FunctionCompile[Function[Typed[x, "Real64"], Df]]
]
];

• Thank you, works great
– Mikl
Commented Apr 2 at 20:01
• You're welcome. Commented Apr 2 at 22:27

First, remove the second Function, because you already have the Function wrapper. Second, because D is not supported by the compiler, you have to calculate the derivative in the main kernel, and insert the result into FunctionCompile. For example:

expr = x^2;
cFastFuncPrim = With[{df = D[expr, x]},
FunctionCompile[Function[Typed[x, "Real64"], df]]]

cFastFuncPrim[3]
(* 6. *)

• Thank you Domen
– Mikl
Commented Apr 2 at 20:02