Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
1  
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.) –  Michael E2 Jun 23 at 2:47

1 Answer 1

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}

share|improve this answer
    
i suppose there should be some example where NumericQ hurts performance because SymbolicProcessing was able to do something useful. –  george2079 Jun 23 at 13:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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