0

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...

VanillaSpinIce
  • 345
  • 1
  • 8

1 Answers1

2

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}

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
  • i suppose there should be some example where NumericQ hurts performance because SymbolicProcessing was able to do something useful. – george2079 Jun 23 '14 at 13:20