Re: variable of integration not localized in Integrate and Sum?
- To: mathgroup at smc.vnet.net
- Subject: [mg63833] Re: [mg63827] variable of integration not localized in Integrate and Sum?
- From: "David Park" <djmp at earthlink.net>
- Date: Wed, 18 Jan 2006 02:39:02 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Don,
Generally you have to localize with Module. But even that doesn't always
work.
If
F[f_, x_] := Integrate[f[x + t], {t, -1/2, 1/2}]
then F[Sin,t] is equivalent to
Hold[Integrate[f[x + t], {t, -1/2, 1/2}]] /. {f -> Sin, x -> t}
% // ReleaseHold
Hold[Integrate[Sin[t + t], {t, -(1/2), 1/2}]]
0
But with
F[f_, x_] := Module[{t}, Integrate[f[x + t], {t, -1/2, 1/2}]]
Mathematica uses t$ as the actual symbol for the local variable and we have
F[Sin, t]
2*Sin[1/2]*Sin[t]
But it does not really isolate the local variable because if we evaluate
F[Sin, t$]
0
we are back in the same situation. It appears that Mathematica works only
because the user is unlikely to use t$ as an input symbol to the call.
I thought that I learned a long time ago that Mathematica automatically
changes the Module variable symbols so as not to conflict with input
symbols - but now I can't find where that is stated, and in fact it doesn't
do it.
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Don Hatch [mailto:hatch at plunk.org]
To: mathgroup at smc.vnet.net
Apparently I need to explicitly localize t in the following,
otherwise it messes up when I pass in t as the second argument.
In:
F[f_, x_] := Integrate[f[x + t], {t, -1/2, 1/2}]
F[Sin, x]
F[Sin, t]
Out:
2 Sin[1/2] Sin[x] <---- correct
0 <---- ?!?!
This is surprising to me-- I thought that
the fact that t is the integration variable
would cause it to be automatically localized, but apparently not.
To get the right answer, I can say:
In:
F[f_, x_] := Module[{t}, Integrate[f[x + t], {t, -1/2, 1/2}]]
F[Sin, x]
F[Sin, t]
Out:
2 Sin[1/2] Sin[x] <---- correct
2 Sin[1/2] Sin[t] <---- correct
(note, I really have to use Module, not Block-- it still messes up using
Block).
The documentation for Integrate doesn't seem to support my hope that t gets
localized, as far as I could see;
however, note that exactly the same problem
occurs for Sum, whose documentation does explicitly state:
The iteration variable i is treated as local.
So here is an example that seems to clearly contradict the documentation
of Sum:
In:
F[f_, x_] := Sum[f[x + t], {t, -1/2, 1/2, 1/10}]/11
F[Identity, x]
F[Identity, t]
Out:
x <---- correct
0 <---- ?!?!
Just as for Integrate, it works properly when I explicitly
localize using Module (but not Block):
In:
F[f_, x_] := Module[{t}, Sum[f[x + t], {t, -1/2, 1/2, 1/10}]/11]
F[Identity, x]
F[Identity, t]
Out:
x <---- correct
t <---- correct
Is this a bug in Sum and/or Integrate?
I'm using Mathematica version 5.2.0.0, on Linux/X.
Don Hatch