# Why does the integration of this symbolic matrix slow down after compilation?

There's a symbolic matrix as the following. Why does the integration of the symbolic matrix slow down after compilation? How to eliminate the error information in red? Thanks.

Note: The sample matrix has been simplified for the convenience of expression, in practice, it's very complex.

Figure:

Codes:

m1[z_] := {{1, z}, {2*z, z^2}};
Integrate[m1[z], {z, 0, 1}] // AbsoluteTiming

(*Out[21]= {0.0001278, {{1, 1/2}, {1, 1/3}}}*)

m2 = Compile[{{z, _Real}}, Evaluate@m1[z]];
Integrate[m2[z], {z, 0, 1}] // AbsoluteTiming

(*During evaluation of In[22]:= CompiledFunction::cfsa: Argument z at position 1 should be a machine-size real number.*)

(*Out[23]= {0.0011614, {{1, 1/2}, {1, 1/3}}}*)


The issue is that the compiled function is never actually used, combined with the fact that the printing of the message takes a bit of time:

(* Baseline *)
m1[z_] := {{1, z}, {2*z, z^2}};
Integrate[m1[z], {z, 0, 1}] // AbsoluteTiming
(* {0.0000917, {{1, 1/2}, {1, 1/3}}} *)

m2 = Compile[{{z, _Real}}, Evaluate@m1[z]];
Integrate[m2[z], {z, 0, 1}] // AbsoluteTiming
(* CompiledFunction::cfsa: Argument z at position 1 should be a machine-size real number. *)
(* {0.0007692, {{1, 1/2}, {1, 1/3}}} *)

(* Message suppressed *)
Quiet@Integrate[m2[z], {z, 0, 1}] // AbsoluteTiming
(* {0.0001051, {{1, 1/2}, {1, 1/3}}} *)

(* Preventing symbolic evaluation shows that the evaluation is never done numerically *)
m3 = Compile[{{z, _Real}}, Evaluate@m1[z], RuntimeOptions -> {"EvaluateSymbolically" -> False}];
Quiet@Integrate[m3[z], {z, 0, 1}] // AbsoluteTiming
(* Integrate[CompiledFunction[…][z], {z, 0, 1}] *)

(* Compare m2[z] to m3[z] *)
{m2[z], m3[z]}
(* CompiledFunction::cfsa: Argument z at position 1 should be a machine-size real number. *)
(* {{{1, z}, {2 z, z^2}}, CompiledFunction[…][z]} *)


As you can see from the last two lines, the compiled version of the function is never actually used: Integrate simply evaluates it with z, gets a symbolic result back, and integrates that, same as with m1.

• Thank you. Can you tell me: how to revise the codes in order to use the compiled function and further to speed up the process? what's the meaning of the message "CompiledFunction::cfsa: Argument z at position 1 should be a machine-size real number" and how to avoid generating the message? Jun 17, 2021 at 12:21
• The message means exactly what it says: The compiled function will only work for machine-precision numbers, not symbolic inputs. So when m2[z] is evaluated, it can't use the compiled version, since z is not a number. As for your other question: Can you give a bit more details on the task you are trying to accomplish? For this specific function, I see no reason why you would want to ever integrate the compiled function, since you can trivially get an analytic solution, which you can then evaluate more quickly than the numeric integration could ever be. Jun 17, 2021 at 15:21
• Thanks. Why didn't they give the message here and there ? Jun 18, 2021 at 2:33
• @likehust In the first question, the message is not printed because the f2[x_?NumericQ,…]:=… definition prevents the compiled function from seeing the symbolic arguments. And in the second one, it is in fact included in the code output in the question. Jun 18, 2021 at 8:25