20
$\begingroup$

I have noticed something odd: Let

f[t_]:=Integrate[x, {x, 0, t}]
h[t_]:=Sum[x, {x, 0, t}]

Now, enter

Table[f[x], {x, 0, 5}]
Table[h[x], {x, 0, 5}]

The first one fails, the second one does not. Consulting the documentation shows that the summation variable for Sum is local, but the integration variable for Integrate is not. There's even an article that mentions this fact.

My question is now: Is there any reason for this, i.e. any case where this 'not-scoping' is useful and not annoying? (Especially since they seem to have omitted this 'feature' for Sum or even NIntegrate)

$\endgroup$
  • 1
    $\begingroup$ Also, Table, Do and Plot scope, Fit does not. I always wanted to ask this myself. $\endgroup$ – István Zachar Dec 21 '15 at 23:41
  • $\begingroup$ Try Array instead of Table, like here Array[ f[#]&, {5}] and you'll see what may be the reason. $\endgroup$ – Artes Dec 21 '15 at 23:43
  • $\begingroup$ @Artes I already know why it is happening if that is what you're trying to explain. I am asking why it was implemented like this $\endgroup$ – Lukas Lang Dec 22 '15 at 9:10
  • 8
    $\begingroup$ I actually think this is in part a vagary of different development histories. But there is at least one important difference between the functions: Sum has both symbolic and procedural aspects to it (made more transparent by Method settings added several eyars ago). The procedural form really is akin to Table and that argues strongly for scoping the variabler(s) of summation. Integrate does not have any analogous behavior, hence does not really warrant such scoping. It is akin to Solve in that respect. $\endgroup$ – Daniel Lichtblau Dec 22 '15 at 15:50
  • 2
    $\begingroup$ Interesting, Sum admits such nonsense.. Sum[ i , {i, i}] -> i (1 + i)/2 .. I expect if they started from scratch the way to resolve all this would be to insist on using formal parameters for integration variables and sum indices. $\endgroup$ – george2079 Dec 23 '15 at 15:28
3
$\begingroup$

I don't see this primarily as an issue of localization but rather of assignment. In a construct like Table the variables are assigned values in some way. This could potentially be done in several different ways, but a method like Block was chosen. So we see:

Dynamic[x]
Table[Pause[1], {x, 10}];

(* changing Dynamic value *)

(I think this was probably chosen for its compatibility with standard For loop code. Manipulate also changes the value of a variable, but uses something akin to Module.)

Sum also assigns a value to a variable and a Block-like method was chosen for it as well:

Dynamic[x]
Sum[Pause[1], {x, 0, 10}];

(* changing Dynamic value *)

But what does Integrate do? It does not assign anything; there is no variable.

If nothing is assigned, no assignment needs to be localized.

Dynamic[x]
Integrate[Print[x]; Pause[1]; x, {x, 0, 5}]

x

x

25/2

There is no need to choose whether to use Block, or Module, or something else (With?) as nothing is iteratively changed. Rather x is merely a specification, not a variable.

  • Expecting a scoping construct here would be like expecting one
    on x in Collect[a x + b y + c x, x] as that too is a specification.

There is a related construct that does use variables: NIntegrate. And guess what? It works like Table and Sum do:

Dynamic[x]
fn[_?NumericQ] := Pause[1]
NIntegrate[fn[x], {x, 0, 15}];

(* changing Dynamic value *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.