14

Is the following behaviour a bug? It's the same in all versions between 9.0-10.2.0.

Let's try this simple Manipulate with the initial value of a set to 1:

Manipulate[Style[a, Large], {{a, 1}, 0, 2}, SaveDefinitions -> True]

Mathematica graphics

Now let us do the same, but set a value to the global a first:

a = 0.5;

Manipulate[Style[a, Large], {{a, 1}, 0, 2}, SaveDefinitions -> True]

Mathematica graphics

The initial value of the local a (set to 1 in the Manipulate) is hijacked by the global value of a. It now shows as 0.5 instead of 1.

This only happens with SaveDefinitions -> True.

Is this a bug or is it expected behaviour?


Analysis: As Michael points out in the comments, the SaveDefintions option causes a=0.5 to be recorded in the Initialization option. The Manipulate evaluates to this InputForm:

Manipulate[Style[a, Large], {{a, 0.5}, 0, 2}, Initialization :> {a = 0.5}]

Normally SaveDefinitions -> True is supposed to record every symbol that's used in the Manipulate, but apparently it neglects to filter out those that are used as parameters.

So is it reasonable to record the global value of a? In some cases it might be. Consider

a = 1;
f[] := a

Manipulate[f[] + a, {a, 0, 1}]

Is this Manipulate going to show a + 1 as a goes from 0..1, or is it going to show a + a?

Mathematica graphics

It shows a + 1, as one would expect based on the fact that the parameter is local to the Manipulate and shouldn't conflict with global variables. But the function f depends on the global a, so if we want to preserve f, we also need to preserve the global a. Thus on first sight it seems reasonable that SaveDefinitions should save the global a (but not that that should influence the local value).

Now let us see what actually happens if we try it:

Manipulate[f[] + a, {a, 0, 1}, SaveDefinitions -> True]

Mathematica graphics

Well, it seems SaveDefinitions changed the behaviour completely. Now it shows a+a instead of a+1 ... I would definitely call this a bug!

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Seems like a bug, in which the symbol a in the code triggers a search for a symbol Global`a with a value (perhaps) that is packaged into the Initialization option. Absolutely no reason I see that such behavior is appropriate here. – Michael E2 Jul 25 '15 at 16:22
  • Same behavior in V8.0.4. – Michael E2 Jul 25 '15 at 16:24
  • @MichaelE2 It seems we found another, related problem: Will a=1; f[] := a; Manipulate[f[] + a, {a,0,1}] show the value of a + a or 1 + a? It depends on whether we use SaveDefinitions -> True! – Szabolcs Jul 25 '15 at 16:40
  • Yikes, it redefines f (with SaveDefinitions -> True)! Try pasting several outputs into your notebook. The function f is redefined each time to represent the local a in the last pasted Manipulate -- for all the other Manipulates! – Michael E2 Jul 25 '15 at 17:01
  • @MichaelE2 Yes, but that behaviour is actually normal, despite of all the troubles it can bring. It makes it easy to create self-contained CDFs. But be very careful with these, or any notebook that has a Manipulate with SaveDefintions! Once you open the notebook, a bunch of Global symbols will get redefined. This was the source of hard to reproduce weird unexpected behaviours in many M.SE threads. – Szabolcs Jul 25 '15 at 17:04
  • One aspect is normal, but the problem with a adds to the trouble (one could always replicate it by copying the Initialization). This bug might explain a habit I developed in V6. I used SaveDefinitions a lot. I wrote all functions to be self-contained (with no parameters not explicitly an argument), which is a good habit anyway. I also deleted all Manipulate output under development before quitting for the day. – Michael E2 Jul 25 '15 at 17:38
  • 1
    We've seen this all before (32004 and my related piece on StackOverflow). Isn't this a duplicate or a corollary? – Sjoerd C. de Vries Jul 25 '15 at 22:38

0 Answers0