How do Clear[] & ClearAll[] operate? Clear[] failing to clear out variables used by two different Manipulate[]s
I have two different Mathematica notebooks with similar, but different functions. Both work fine when they're the only notebook open. One of them consistently fails when the other notebook is open, despite my (liberal) use of Clear[] to clear out the relevant variables.
Call this one, say, GlobalManipulate
ClearAll["Global`*"]
Clear["Global`*"]
Definition[linear]
linear[x_] := a x;
quad[x_] := a x^2;
functionList := {linear, quad};
Manipulate[
Plot[function[dummy], {dummy, -10, 10}],
{function, functionList}, {a, -10, 10},
LocalizeVariables -> False, TrackedSymbols -> All
]
Call this one, say, LocalManipulate
Clear["Global`*"];
Manipulate[
{
linear := a x; quad := a x^2;
linear, quad, function,
Plot[ReleaseHold@function, {x, -10, 10}]
},
{function, {HoldForm@linear, HoldForm@quad}},
{a, -10, 10}, TrackedSymbols -> All
]
When run by itself, GlobalManipulate works as expected and I see the plot that updates as a
is changed. The definition of linear produces Null
.
When LocalManipulate is open, running, GlobalManipulate no longer works. even when it's rerun It's plot appear开发者_运维百科s for a second and then vanishes.
I've reproduced this using my local copy of Mathematica 8 and a remote copy of Mathematica 7.
The problem must involve the functions linear[x_]
and quad[x_]
since
GlobalManipulatePrime:
ClearAll["Global`*"]
Clear["Global`*"]
Definition[linear]
linear1[x_] := a x;
quad1[x_] := a x^2;
functionList := {linear1, quad1};
Manipulate[
Plot[function[dummy], {dummy, -10, 10}],
{function, functionList}, {a, -10, 10},
LocalizeVariables -> False, TrackedSymbols -> All
]
works fine.
Edited to add bold text to stress that I'm rerunning Global, and that I'm trying to figure out why functions persist despite my ClearAll[].
Inside your LocalManipulate you have the definitions
linear := a x; quad := a x^2
.
Since these are not declared in the controller part of the Manipulate
command, they are not localized and thus overwrite the global variables and break your GlobalManipulate.
The simplest way to localize variables inside a Manipulate
is to use add them in with the controller type None
. Below I've modified your LocalManipulate code, so that the last line makes linear
and quad
local to that particular Manipulate
(DynamicModule
)
Manipulate[{linear := a x; quad := a x^2; linear, quad, function,
Plot[ReleaseHold@function, {x, -10, 10}]},
{function, {HoldForm@linear, HoldForm@quad}}, {a, -10, 10},
{linear, None}, {quad, None}, TrackedSymbols -> All]
Apart from wrapping the functions in Module
or DynamicModule
and localizing the variables you could also place the notebooks in individual contexts using the Evaluation ► Notebook's Default Context
menu.
You could also set the context using the function Begin
. This should all be done before your functions are defined.
Update
I didn't read your question too well I think. As to the cause of ClearAll not working:
- Because of the definition of linear in LocalManipulate using the
localized variable a, the symbol linear gets defined as something like
linear:=FE`a$$51 x
- In GlobalManipulate you use both the name linear (in the function selection) and the function linear
- Because of defining the symbol
linear
inFE`a$$51 x
the linefunctionList := {linear, quad};
in GlobalManipulate now actually meansfunctionList := {FE`a$$51 x,FE`a$$53 x^2}
which, out of the otherManipulate
context, doesn't mean anything, so your plot disappears - Why then doesn't
ClearAll
remove this definition? This has to do with the properties of the dynamic variables buried in the Manipulate box visible in the LocalManipulate notebook. They keep on getting redefined there. Remove the output of the Manipulate and re-execute GlobalManipulate and you'll see it now works. Something similar is discussed here.
精彩评论