# NIntegrate can't integrate function which returns list, but can integrate list directly

I have the following dummy Code

f[arg : {__?NumericQ}] := 2*arg;
g := f[{x, y}]
NIntegrate[g, {x, 0, 1}, {y, 0, 1}]
NIntegrate[f[{x, y}], {x, 0, 1}, {y, 0, 1}]


Both tries yield

NIntegrate::inum: Integrand f[{x,y}] is not numerical at {x,y} = {0.5,0.5}.

Despite

f[{x, y}] /. x -> 0.5 /. y -> 0.5
{1.,1.}
f[{0.5, 0.5}]
{1.,1.}


However, if I enter the function directly, I get the expected result

NIntegrate[2*{x, y}, {x, 0, 1}, {y, 0, 1}]
{1., 1.}


Why does the first try fail and the last try evaluate?

• f evaluates to a list of numbers, and NIntegrate is expecting just a number. Aug 18, 2020 at 16:05
• @CarlWoll That is wrong, see my edit. Aug 18, 2020 at 16:17
• Possible duplicates: (34554), (174256). Related: (126342). Aug 18, 2020 at 16:29
• ^ from (126342), the answer is to use Indexed like so {NIntegrate[Indexed[f[{x, y}], 1], {x, 0, 1}, {y, 0, 1}], NIntegrate[Indexed[f[{x, y}], 2], {x, 0, 1}, {y, 0, 1}]} which gives {1., 1.} Aug 18, 2020 at 16:35
• I updated the question, to address the difference between the approaches. Aug 18, 2020 at 16:36

The issue here is that even with using Evaluate, neither g nor f[{x,y}] will evaluate without satisfying the NumericQ that has been imposed on arg. There are a number of solutions mentioned in the related links that @MichaelE2 has linked to in the comments, and other solutions offered by @flinty & @Bob Hanlon. However, if control of the function being input into NIntegrate is possible, you may find inspiration in the following simple solution:
h[arg:{__}]:=2*arg;

{1.,1.}
• @CarlWoll right, this is why I mention this in my answer. “If control of the function being input...” While I understand what you are saying, I don’t think I am “missing the point”—the usage of NumericQ here is very much well understood, and the reasons why it is not working are also well understood. I offer a simple solution which satisfies OP’s main point iterated in the title of the question. “NIntegrate can’t integrate function which returns list, but can integrate list directly”. This is shown, clearly, to not be so, as in the simple solution offered in my answer. Aug 18, 2020 at 17:21
• Your solution works because NIntegrate (while HoldAll) evaluates the integrand symbolically before computing the result. Your approach is no different than using NIntegrate[2 {x, y}, {x, 0, 1}, {y, 0, 1}]. Aug 18, 2020 at 17:37