14
$\begingroup$

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!

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$
    – Michael E2
    Commented Jul 25, 2015 at 16:22
  • $\begingroup$ Same behavior in V8.0.4. $\endgroup$
    – Michael E2
    Commented Jul 25, 2015 at 16:24
  • $\begingroup$ @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! $\endgroup$
    – Szabolcs
    Commented Jul 25, 2015 at 16:40
  • $\begingroup$ 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! $\endgroup$
    – Michael E2
    Commented Jul 25, 2015 at 17:01
  • 1
    $\begingroup$ We've seen this all before (32004 and my related piece on StackOverflow). Isn't this a duplicate or a corollary? $\endgroup$ Commented Jul 25, 2015 at 22:38

0

Browse other questions tagged or ask your own question.