Numerical integration with purely numerical integrand - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-11-14T18:37:40Z https://mathematica.stackexchange.com/feeds/question/51310 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/51310 0 Numerical integration with purely numerical integrand VanillaSpinIce https://mathematica.stackexchange.com/users/16087 2014-06-23T01:21:09Z 2014-06-23T03:30:58Z <p>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 :</p> <pre><code>test[a_]:=If[NumericQ[a],a*a,Abort[] (*Meaning the parameter a is not numerical*)] </code></pre> <p>So if the parameter of <code>test</code> is numerical, this function should only return <code>a*a</code>. Now if I try to integrate this as follows:</p> <pre><code>NIntegrate[test[a],{a,-0.5,0.5},Method -&gt; {Automatic, "SymbolicProcessing" -&gt; 0}] </code></pre> <p>this does not work (it will be aborted). <strong>Is there any way around this ? I.e. some option I haven't considered for NIntegrate ?</strong> </p> <p>Note that an easy way to fix this would be to use:</p> <pre><code>test[a_?NumericQ]:=If[NumericQ[a],a*a,Abort[]] </code></pre> <p>But <strong>I want to avoid using</strong> <code>?NumericQ</code>since this slows down your numerical integration by a lot...</p> https://mathematica.stackexchange.com/questions/51310/-/51315#51315 2 Answer by Bob Hanlon for Numerical integration with purely numerical integrand Bob Hanlon https://mathematica.stackexchange.com/users/9362 2014-06-23T03:30:58Z 2014-06-23T03:30:58Z <p>Including "SymbolicProcessing" -> False in the Method for NIntegrate gives equivalent timings with or without the NumericQ pattern test in the function's definition. </p> <pre><code>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 -&gt; {Automatic, "SymbolicProcessing" -&gt; False}], {n}] // Timing </code></pre> <blockquote> <p>{1.642638, Null}</p> </blockquote> <pre><code>Do[NIntegrate[test2[a], {a, -0.5, 0.5}, Method -&gt; {Automatic, "SymbolicProcessing" -&gt; False}], {n}] // Timing </code></pre> <blockquote> <p>{1.674284, Null}</p> </blockquote> <p>Without "SymbolicProcessing" -> False, the NumericQ pattern test in the function's definition slightly improves the timing of NIntegrate in this case.</p> <pre><code>Do[NIntegrate[test[a], {a, -0.5, 0.5}], {n}] // Timing </code></pre> <blockquote> <p>{5.708290, Null}</p> </blockquote> <pre><code>Do[NIntegrate[test2[a], {a, -0.5, 0.5}], {1000}] // Timing </code></pre> <blockquote> <p>{4.512917, Null}</p> </blockquote>