# Why does Mathematica constantly reevaluate an expression?

Manipulate[
f[n_] := 1/n^2;
pt = Accumulate[Table[Table[f[i], {i, max}][[n]],
{n, PermutationReplace[Table[i, {i, max}], RandomPermutation[max]]}]];

DiscretePlot[pt[[n]], {n, 1, max}],

{{max, 5}, 2, 15, 1, Appearance -> "Labeled"},

Deployed -> True]


The problem is that Mathematica constantly reevaluates the randomization. Even if the mouse is completely outside the Manipulate, the plot constantly flashes and changes.

But now here goes the really weird part. If the whole Manipulate gets a bit less complex... this behaviour ceases.

Manipulate[
pt = Accumulate[PermutationReplace[Table[i, {i, 1, max}],
RandomPermutation[max]]];

DiscretePlot[pt[[n]], {n, 1, max}],

{{max, 5}, 2, 15, 1, Appearance -> "Labeled"},
Deployed -> True]


Everything looks nicely here. Yes, it does seem Mathematica re-randomizes this two or three Times too often, so it takes a second for the plot to calm down and stop changing; but after that time, everything looks well.

How to fix this? And why does this happen?

PS. Sorry for spamming the site... I really need to have a better knowledge of Mathematica, I know. But, for my defence, this really seems weird and unandersandable.

• Move the function definition outside the manipulate. – bill s Jan 14 '15 at 4:40
• @bills Can't believe it, works! Finally stopped this bizzare blinking. Thanks! But, could you elaborate this a little bit more? I'd be very grateful if you made me understand was was actually going on :) – gaazkam Jan 14 '15 at 4:49
• Best to use the Initialization option to initialize the function rather than have it in the body as others have pointed out. – Mike Honeychurch Jan 14 '15 at 5:24

Manipulate updates each time its expression changes. The expression is a function of the control variables. Or if the expression contains a symbol or expression that appears only inside the Manipulate expression (i.e. not global before), even though it is not control variable, and this symbol/expression gets updated during evaluation of the manipulate expression.

In your example below, we see these symbols f[],n,i,max,pt

The control variable is max, so when max changes, pt changes. The symbols i,n are local to Table by definition, hence they do not affect the update.

So what is left is f[]. Notice that when pt changes, inside the Table you are calling f[i], then Table will make repeated evaluation for each Table index See

http://reference.wolfram.com/language/tutorial/EvaluationInIterationFunctions.html

To solve this, you make make a module inside Manipulate, and define any symbols used in the expression, which are not control variable, in the module, like this:

Manipulate[
Module[{f, n, i},
f[n_] := 1/n^2;
pt = Accumulate[
Table[
Table[Evaluate@f[i], {i, max}][[n]], {n, PermutationReplace[Table[i,
{i, max}], RandomPermutation[max]]}]
];
DiscretePlot[pt[[n]], {n, 1, max}]
],
{{max, 5}, 2, 15, 1, Appearance -> "Labeled"},
Deployed -> True]


Another way to solve this, which is what I prefer to do, is to use TrackedSymbols.

 Manipulate[
f[n_] := 1/n^2;
pt = Accumulate[
Table[
Table[f[i], {i, max}][[n]], {n, PermutationReplace[Table[i, {i, max}],
RandomPermutation[max]]}]
];
DiscretePlot[pt[[n]], {n, 1, max}],
{{max, 5}, 2, 15, 1, Appearance -> "Labeled"},
TrackedSymbols :> {max},
Deployed -> True]


Here is a simple example that shows the problem you were having

Manipulate[
g[n_] := n;
Table[g[i], {i, 1, max}],
{{max, 10, "max"}, 1, 10, 1}
]


This will now re-evaluate repeatedly.

• Many thanks, it works! One last question. Why: TrackedSymbols:>{max}, and not: TrackedSymbols->{max}? Why do we have to have here a RuleDelayed rather then a normal Rule? – gaazkam Jan 14 '15 at 13:31
• From reference.wolfram.com/language/ref/TrackedSymbols.html Using Rule for TrackedSymbols fails in some cases because the symbols are evaluated That is why delayed in this case is always used. Generally, RuleDelayed should always be used for TrackedSymbols: This is very important. Always use :> with trackedsymbols. I once spent whole day trying to figure what I was doing wrong, then at the end I noticed I used -> instead of :> – Nasser Jan 14 '15 at 13:40