This answer addresses Ajasja's question in rcollyer's answer:
Do you perhaps know why adding "RuntimeOptions" -> {"EvaluateSymbolically" -> False} to FFc does not cure the problem?
This is due to the fact that the methods inside NIntegrate[] (attempt to) perform a preliminary symbolic analysis, which can be helpful for integrands composed of built-in mathematical functions, but not terribly useful for compiled functions. Thus, one has to turn it off in this case, in addition to enabling the compilation options mentioned by Ajasja.
Here is a quick demonstration:
FFc = Compile[{{x, _Real}, {EF, _Real}},
If[x > EF, 0., If[x == EF, 0.5, 1.]],
"RuntimeOptions" -> {"EvaluateSymbolically" -> False}];
With the default setting, we get this (Mathematica 10.4):
NIntegrate[FFc[x, 3.2], {x, -6.5, 6.5}]
CompiledFunction::cfsa: Argument -x at position 1 should be a machine-size real number.
CompiledFunction::cfsa: Argument -x at position 1 should be a machine-size real number.
NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the
following: singularity, value of the integration is 0, highly oscillatory integrand, or
WorkingPrecision too small.
NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9
recursive bisections in x near {x} = {3.22441}. NIntegrate obtained 9.699759342263453`
and 0.0005405922634428764` for the integral and error estimates.
before 9.69976 is returned. In contrast, if we disable symbolic processing like so:
NIntegrate[FFc[x, 3.2], {x, -6.5, 6.5}, Method -> {Automatic, "SymbolicProcessing" -> 0}]
NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the
following: singularity, value of the integration is 0, highly oscillatory integrand, or
WorkingPrecision too small.
NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9
recursive bisections in x near {x} = {3.22441}. NIntegrate obtained 9.699759342263453`
and 0.0005405922634428764` for the integral and error estimates.
we no longer get the CompiledFunction::cfsa message.
"RuntimeOptions" -> {"EvaluateSymbolically" -> False}toFFcdoes not cure the problem? – Ajasja Jan 18 '13 at 09:40FFc[-x,0.]gives error whileFFc[x,0.]doesn't. I wonder whyNIntegratepasses-xas argument to the function. (This comment is assuming{"EvaluateSymbolically" -> False}) – ssch Jan 18 '13 at 12:20{x,-a,a}it doesn't work, while it is unsymmetric like{x,-a-0.00001,a}it does – ssch Jan 18 '13 at 12:26Compileis not my strong suit. – rcollyer Jan 18 '13 at 13:14Compileis supposed to give. I am referring to the_?NumericQpattern matching. Isn't there a more direct solution? Perhaps in Math 10? – a06e Oct 21 '14 at 15:42NumericQ? That is the only way to tell definitively. But, I suspect the speed hit will be marginal. – rcollyer Oct 21 '14 at 15:47NIntegrate, I don't expect it to be very large. That said, you can turn off symbolic processing inNIntegrateby settingMethod -> {Automatic, "SymbolicProcessing" -> 0}. – rcollyer Apr 29 '16 at 18:48