# Integrate function returns vector instead of scalar

When I try to integrate a function mapping a vector to a scalar Mathematica "ignores" the function and returns a vector independent of the function.

Here is a minimal example of my problem: I want to integrate the sum over all absolute values in a vector over a 2D unit circle. When I use the Total function for this, i.e. f1 Mathematica returns a scalar (2.67) that looks reasonable. When I use my f2 function which - as far as I know - should basically implement the Total function, it does not return a scalar but a vector {0., 0.}.

f1[x_] := Total[x]
f2[x_] := Apply[Plus, x]
NIntegrate[f1[Abs[x]], x \[Element] Ball[ConstantArray[0, 2]]]
NIntegrate[f2[Abs[x]], x \[Element] Ball[ConstantArray[0, 2]]]


When I directly compare what my f2 function does it looks like it behaves like the f1 function, i.e. f1[{0.2, 0.3 }] == f2[{0.2, 0.3}] returns True.

I am puzzled by the (seemingly) different behavior of Mathematica depending on whether I use the f2 function inside NIntegrate or outside; now I am looking for an explanation what happens here and how and why I have to modify the f2 function such that it behaves like f1 in my integral.

• Can you report on the result of wrapping the f1 integrand in an Evaluate? I have a feeling this is due to NIntegrate trying to transform the integrand before inserting values for x but I'm not sure. Mar 25, 2021 at 10:44
• @MariusLadegårdMeyer, it's the first thing that popped in my mind as well, but it doesn't seem to solve the issue. Also writing the function explicitely does not seem to help either. I tried plotting the two side by side and they indeed both give what you would expect. It's kinda weird. Perhaps a bug?
– alex
Mar 25, 2021 at 10:53
• @MariusLadegårdMeyer Wrapping f2 with Evaluate doesn't change the return value - it's still {0., 0.}. Mar 25, 2021 at 11:23
• Might have something to do with what f2[Abs[x]] evaluates to symbolically. Try f2[x_?VectorQ] := Apply[Plus,x]. Mar 25, 2021 at 12:58
• It did for me. Perhaps you didn't clear your first definition? Mar 25, 2021 at 16:21

The problem seems to be because f2[Abs[x]] evaluates to x symbolically:

ClearAll[f1, f2]; (* be sure to clear previous definitions *)
f1[x_] := Total[x]
f2[x_?VectorQ] := Apply[Plus, x]
NIntegrate[f1[Abs[x]], x \[Element] Ball[ConstantArray[0, 2]]]
NIntegrate[f2[Abs[x]], x \[Element] Ball[ConstantArray[0, 2]]]

(*
2.66667
2.66667
*)


Don't know why Mathematica doesn't recognize xto be a list!

Try

NIntegrate[f1[Abs[{x1, x2}]], {x1, x2} \[Element]Ball[ConstantArray[0, 2]]]
(*2.66667*)

NIntegrate[f2[Abs[{x1, x2}]], {x1, x2} \[Element]Ball[ConstantArray[0, 2]]]
(*2.66667*)

• That works! However, later on, I want to evaluate this on n dimensions which make it unfeasible to type in all x_i by hand. Any suggestion on how to solve this then? Mar 25, 2021 at 11:55
• @zimmerrol Sorry, no idea. I only know (N)DSolve is able to handle list-variables. Mar 25, 2021 at 12:32
• @zimmerrol The documentation states that Total[#]&and Apply[Plus,#]&are equivalent, but simple test Simplify[ Apply[Plus, x ] , x \[Element] Disk[] ] (*x*) shows it isn't! Mar 25, 2021 at 13:19