0
$\begingroup$

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?

$\endgroup$
8
  • 3
    $\begingroup$ f evaluates to a list of numbers, and NIntegrate is expecting just a number. $\endgroup$
    – Carl Woll
    Commented Aug 18, 2020 at 16:05
  • $\begingroup$ @CarlWoll That is wrong, see my edit. $\endgroup$ Commented Aug 18, 2020 at 16:17
  • 2
    $\begingroup$ Possible duplicates: (34554), (174256). Related: (126342). $\endgroup$
    – Michael E2
    Commented Aug 18, 2020 at 16:29
  • $\begingroup$ ^ 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.} $\endgroup$
    – flinty
    Commented Aug 18, 2020 at 16:35
  • $\begingroup$ I updated the question, to address the difference between the approaches. $\endgroup$ Commented Aug 18, 2020 at 16:36

1 Answer 1

-1
$\begingroup$

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;
NIntegrate[h[{x,y}],{x,0,1},{y,0,1}]

{1.,1.}

$\endgroup$
4
  • $\begingroup$ I think you're missing the point. Usually, the NumericQ predicate is used because the function will not work if given nonnumerical input. In your simple example, this is not an issue, but probably in the OP usage, it is an issue. $\endgroup$
    – Carl Woll
    Commented Aug 18, 2020 at 17:17
  • $\begingroup$ @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. $\endgroup$ Commented Aug 18, 2020 at 17:21
  • 1
    $\begingroup$ 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}]. $\endgroup$
    – Carl Woll
    Commented Aug 18, 2020 at 17:37
  • $\begingroup$ @CarlWoll right. Exactly! Except that it uses a defined function? At any rate, we know we can define a function that returns a list, and then integrate it. $\endgroup$ Commented Aug 18, 2020 at 19:52

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.