0
$\begingroup$

I'm trying to perform a numerical integral with an integrand that should not be manipulated with any symbolic preprocessing whatsoever. Consider the following simple test :

test[a_]:=If[NumericQ[a],a*a,Abort[] (*Meaning the parameter a is not numerical*)]

So if the parameter of test is numerical, this function should only return a*a. Now if I try to integrate this as follows:

NIntegrate[test[a],{a,-0.5,0.5},Method -> {Automatic, "SymbolicProcessing" -> 0}]

this does not work (it will be aborted). Is there any way around this ? I.e. some option I haven't considered for NIntegrate ?

Note that an easy way to fix this would be to use:

test[a_?NumericQ]:=If[NumericQ[a],a*a,Abort[]]

But I want to avoid using ?NumericQsince this slows down your numerical integration by a lot...

$\endgroup$
1
  • 1
    $\begingroup$ This question shows that ?NumericQ speeds up integration a lot. (In your case it may slow it down, but it does not always do so.) $\endgroup$
    – Michael E2
    Commented Jun 23, 2014 at 2:47

1 Answer 1

2
$\begingroup$

Including "SymbolicProcessing" -> False in the Method for NIntegrate gives equivalent timings with or without the NumericQ pattern test in the function's definition.

n = 1000; (* iterations in Do loops *)

test[a_] := a^2;

test2[a_?NumericQ] := a^2;

Do[NIntegrate[test[a], {a, -0.5, 0.5},
   Method -> {Automatic,
     "SymbolicProcessing" -> False}], {n}] //
 Timing

{1.642638, Null}

Do[NIntegrate[test2[a], {a, -0.5, 0.5},
   Method -> {Automatic,
     "SymbolicProcessing" -> False}], {n}] //
 Timing

{1.674284, Null}

Without "SymbolicProcessing" -> False, the NumericQ pattern test in the function's definition slightly improves the timing of NIntegrate in this case.

Do[NIntegrate[test[a], {a, -0.5, 0.5}], {n}] //
 Timing

{5.708290, Null}

Do[NIntegrate[test2[a], {a, -0.5, 0.5}], {1000}] //
 Timing

{4.512917, Null}

$\endgroup$
1
  • $\begingroup$ i suppose there should be some example where NumericQ hurts performance because SymbolicProcessing was able to do something useful. $\endgroup$
    – george2079
    Commented Jun 23, 2014 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.