Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 appears 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[].

like image 484
BenB Avatar asked Jan 18 '23 19:01

BenB


2 Answers

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]
like image 118
Simon Avatar answered Mar 30 '23 00:03

Simon


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:

  1. 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
  2. In GlobalManipulate you use both the name linear (in the function selection) and the function linear
  3. Because of defining the symbol linear in FE`a$$51 x the line functionList := {linear, quad}; in GlobalManipulate now actually means functionList := {FE`a$$51 x,FE`a$$53 x^2} which, out of the other Manipulate context, doesn't mean anything, so your plot disappears
  4. 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.
like image 32
Sjoerd C. de Vries Avatar answered Mar 30 '23 00:03

Sjoerd C. de Vries