1
$\begingroup$

I'm not able to NIntegrate a function that has a numeric list as an argument. My original problem involves a compiled function, but a MWE is the following:

g[r : {__?NumericQ}, x_?NumericQ] := r;
NIntegrate[g[{2}, x], {x, 0, 10}]

This gives me the following error:

error integrand is not numerical

which I fail to understand, since

g[{2}, 0.0795732]

evaluates perfectly fine to {2}.

Other functions like Plot are fine, i.e. the following works as expected:

Plot[g[{2}, x], {x, 0, 10}] 

Also, if we don't force the arguments to be numeric, i.e.:

gS[r_, x_] := r;
NIntegrate[gS[{2}, x], {x, 0, 10}]

everything works fine. But I do need to force them to be numeric because eventually I want to NIntegrate a compiled function. The example posted here is the simplest MWE I've been able to provide.

I would really appreciate any thoughts, since I'm quite lost here. Thank you so much, Luis

$\endgroup$
3
  • $\begingroup$ {2} is not a number $\endgroup$
    – Carl Woll
    Commented Dec 20, 2018 at 19:34
  • $\begingroup$ @CarlWoll. I don't think that's the problem, because NIntegrate[{2}, {x, 0, 1}] evaluates just fine. The issue is that in NIntegrate[gS[{2}, x], {x, 0, 10}], the integrand won't evaluate to a number because x is not a number. On the face of it, I wouldn't know how to short-circuit the evaluation here, because Evaluate@gS[{2}, x] of course yields gS[{2}, x], but it needs to be evaluated before the NIntegrate so that {2} can integrate to {2.}. Luis, I suspect the thing that you are trying to do should be done in a different way. Perhaps the greater context would be helpful. $\endgroup$
    – march
    Commented Dec 20, 2018 at 19:46
  • $\begingroup$ @march, thank you so much for pointing that out. What I am trying to do is to NIntegrate each of the components of a compiled function that returns a list. Thanks to CarlWoll and you, I understand the problem much better now. $\endgroup$
    – Luis
    Commented Dec 21, 2018 at 10:52

1 Answer 1

3
$\begingroup$

NIntegrate requires the integrand to be numeric, and the list {2} is not numeric:

NumericQ[{2}]

False

You need your function g to return something that is numeric, not a list.

(aside: @march points out that:)

NIntegrate[{2}, {x, 0, 1}]

{2.}

works fine, but that is because NIntegrate recognizes that the first argument is a list and so it threads the first argument. One can see this happening using TracePrint:

Trace @ NIntegrate[{2}, {x, 0, 1}]

{NIntegrate[{2},{x,0,1}],{NIntegrate[2,{x,0,1}]},{NIntegrate[2,{x,0,1}],{{x}=.,{x=.},{x=.,Null},{Null}},{x=.,Null},2.},{2.}}

Notice how NIntegrate[{2}, {x, 0, 1}] evaluates to {NIntegrate[2, {x, 0, 1}]}.

Your other approach worked because NIntegrate evaluated its first argument to a list, and then the default threading that happens when the first argument is a list took over.

So, you need to make sure the output of g is a number. For instance:

g[r:{__?NumericQ}, x_?NumericQ] := First @ r
NIntegrate[g[{2}, x], {x, 0, 10}]

20.

$\endgroup$
1
  • $\begingroup$ Thank you so much Carl and March, now I understand everything. Perfectly clear explanation, thanks a lot Guys!!! $\endgroup$
    – Luis
    Commented Dec 21, 2018 at 10:36

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.