I work a lot on Mathematica and sometimes I need to optimize my computations by using compiled functions. Now I'm making numerical integrations and I'm trying to compile the functions involved to make everything faster. I'm having basically the fallowing issue which explain by it self:

gCC = Compile[{{x, _Real}}, x^2];

fCC = Compile[{{R, _Real}}, NIntegrate[gCC[x + y]*Boole[x^2 + y^2 < R^2], {x, -R, R}, {y, -R, R}] ];

Then when I use the function fCC I get the fallowing error:


CompiledFunction::cfsa: Argument x+y at position 1 should be a machine-size real number. >>


How can I solve this? Regards.


marked as duplicate by Jason B., MarcoB, m_goldberg, user9660, RunnyKine Mar 30 '16 at 15:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ NIntegrate[] is not compilable. $\endgroup$ – J. M. is away Mar 30 '16 at 10:29
  • $\begingroup$ Have a look at documentation regarding the (optional) third argument of Compile. It might be useful in this situation. Also the Compile options for inlining might apply. $\endgroup$ – Daniel Lichtblau Mar 30 '16 at 11:28
  • 1
    $\begingroup$ Just to take one level off the problem, if you try NIntegrate[ gCC[x + y]*Boole[x^2 + y^2 < 2.1^2], {x, -2.1, 2.1}, {y, -2.1, 2.1}] it gives the same error, so the issue is not with the definition of fCC $\endgroup$ – Jason B. Mar 30 '16 at 11:47
  • $\begingroup$ Try this: gCC = Compile[{{x, _Real}}, x^2]; gCC2[x_?NumericQ] := gCC[x]; fCC = Compile[{{R, _Real}}, NIntegrate[ gCC2[x + y]*Boole[x^2 + y^2 < R^2], {x, -R, R}, {y, -R, R}]]; fCC[2.1] $\endgroup$ – Jason B. Mar 30 '16 at 11:49
  • $\begingroup$ Thanks JasonB, your simple solution worked and now my computations are considerably faster ! Sorry for my delayed feedback. $\endgroup$ – gambarimas87 Apr 19 '16 at 12:26

Browse other questions tagged or ask your own question.