4
$\begingroup$

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.

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$ Commented Mar 25, 2021 at 10:44
  • $\begingroup$ @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? $\endgroup$
    – alex
    Commented Mar 25, 2021 at 10:53
  • $\begingroup$ @MariusLadegårdMeyer Wrapping f2 with Evaluate doesn't change the return value - it's still {0., 0.}. $\endgroup$
    – zimmerrol
    Commented Mar 25, 2021 at 11:23
  • 1
    $\begingroup$ Might have something to do with what f2[Abs[x]] evaluates to symbolically. Try f2[x_?VectorQ] := Apply[Plus,x]. $\endgroup$
    – Michael E2
    Commented Mar 25, 2021 at 12:58
  • 1
    $\begingroup$ It did for me. Perhaps you didn't clear your first definition? $\endgroup$
    – Michael E2
    Commented Mar 25, 2021 at 16:21

2 Answers 2

3
$\begingroup$

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
*)
$\endgroup$
2
$\begingroup$

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*)
$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – zimmerrol
    Commented Mar 25, 2021 at 11:55
  • $\begingroup$ @zimmerrol Sorry, no idea. I only know (N)DSolve is able to handle list-variables. $\endgroup$ Commented Mar 25, 2021 at 12:32
  • 1
    $\begingroup$ @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! $\endgroup$ Commented Mar 25, 2021 at 13:19

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.