开发者

Evaluation of Expressions inside Manipulate Statements

I have problems getting Manipulate to work with code assigned to variables that should be evaluated inside the Manipulate statement. Here is how it goes ...

test1={a,b,c};
Manipulate[test1,{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]

Evaluation of Expressions inside Manipulate Statements

So {a, b, c} are not updated. Ok, whatever, let's enforce the evaluation of test1

Manipulate[Evaluate[test1],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]

Evaluation of Expressions inside Manipulate Statements

Now it works. But if I want to plot the list of manipulated elements, like this

Manipulate[ListPlot[Evaluate[test1]],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]
Manipulate[Evaluate[ListPlot[test1]],{a,0,10,.1},{b,0,10,.1},{c,0,10,.1}]

I end up with

Evaluation of Expressions inside Manipulate Statements

in both c开发者_运维知识库hases.

I am aware of 'Evaluate Expressions inside Dynamic or Manipulate' in Mathematica's documentation, but I am pretty sure that it does not provide a solution to my problem.


So the problem is that test1 is defined in terms of global variable Global`a, but the a defined in the manipulate is created by a DynamicModule and is thus local. This is what acl showed with his Hold[a] example.

Maybe the easiest way to fix this is to use With to insert test1 into the manipulate:

Clear[a, b, c]
test1 = {a, b, c};
With[{test1 = test1}, 
     Manipulate[test1, {a, 0, 10, .1}, {b, 0, 10, .1}, {c, 0, 10, .1}]]

This way the Manipulate never actually sees test1, all it sees is {a,b,c} which it then goes on to correctly localize. Although, this will run into problems if a,b,c have been given a value before the Manipulate is run - thus the Clear[a,b,c] command.

I think that the best practice is to make all local variables completely explicit in the manipulate. So you should do something like

Clear[a, b, c, test1]
test1[a_, b_, c_] := {a, b, c};
Manipulate[test1[a, b, c], {a, 0, 10, .1}, {b, 0, 10, .1}, {c, 0, 10, .1}]

This avoids problems with the global vs local variables that you were having. It also makes it easier for you when you have to come back and read your own code again.


Edit to answer the question in the comments "I really would like to understand why Evaluate does not work with the somewhat nested ListPlot?". IANLS (I am not Leonid Shifrin) and so I don't have a perfect Mathematica (non)standard evaluation sequence running in my brain, but I'll try to explain what's going on.

Ok, so unlike Plot, ListPlot does not need to localize any variables, so it does not have the Attribute HoldAll.

Let's define something similar to your example:

ClearAll[a, test]
test = {a, a + 1};

The final example you gave is like

Manipulate[Evaluate[ListPlot[test]], {a, 0, 1}]

By looking at the Trace, you see that this first evaluates the first argument which is ListPlot[test] ~> ListPlot[{a,a+1}] and since a is not yet localized, it produces an empty list plot. To see this, simply run

ListPlot[{a, a + 1}]//InputForm

to get the empty graphics object

Graphics[{}, {AspectRatio -> GoldenRatio^(-1), Axes -> True, AxesOrigin -> {0, 0}, PlotRange -> {{0., 0.}, {0., 0.}}, PlotRangeClipping -> True, PlotRangePadding -> {Scaled[0.02], Scaled[0.02]}}]

As the symbolic values a have been thrown out, they can not get localized by the Manipulate and so not much else happens.

This could be fixed by still evaluating the first argument, but not calling ListPlot until after Manipulate has localized the variables. For example, both of the following work

Manipulate[Evaluate[listPlot[test]], {a, 0, 1}] /. listPlot -> ListPlot
Manipulate[Evaluate[Hold[ListPlot][test]], {a, 0, 1}] // ReleaseHold

The fact that ListPlot throws away non-numeric values without even the slightest complaint, is probably a feature, but can lead to some annoyingly hard to track bugs (like the one this question pertains to). Maybe a more consistent (but less useful?) behaviour would be to return an unevaluated ListPlot if the plot values are non-numeric... Or to at least issue a warning that some non-numeric points have been discarded.

The penultimate example you gave is (more?) interesting, it looks like

Manipulate[ListPlot[Evaluate[test]], {a, 0, 1}]

Now since Manipulate has the attribute HoldAll, the first thing it does is wrap the arguments in Hold, so if you look at the Trace, you'll see Hold[ListPlot[Evaluate[test]]] being carried around. The Evaluate is not seen, since as described in the Possible Issues section, "Evaluate works only on the first level, directly inside a held function". This means that test is not evaluated until after the variables have been localized and so they are taken to be the global a and not the local (DynamicModule) a.

It's worth thinking about how the following variations work

ClearAll[a, test, f, g]
SetAttributes[g, HoldAll];
test = {a, a + 1};

Grid[{
  {Manipulate[test, {a, 0, 1}], Manipulate[Evaluate[test], {a, 0, 1}]},
  {Manipulate[f[test], {a, 0, 1}], 
   Manipulate[f[Evaluate[test]], {a, 0, 1}]},
  {Manipulate[g[test], {a, 0, 1}], 
   Manipulate[g[Evaluate[test]], {a, 0, 1}]}
  }]

Evaluation of Expressions inside Manipulate Statements


Here is why it doesn't work:

Manipulate[
 {
  Hold[a]
  },
 {a, 0, 10, .1},
 {b, 0, 10, .1},
 {c, 0, 10, .1}
 ]

Evaluation of Expressions inside Manipulate Statements

One may fix this in various ways. One is to simply define test1 with the localized variables, like so:

ClearAll[test1, a, b, c];
Manipulate[
 test1 = {a, b, c};
 {
  test1
  },
 {a, 0, 10, .1},
 {b, 0, 10, .1},
 {c, 0, 10, .1}
 ]

and then eg

ClearAll[test1, a, b, c];
Manipulate[
 test1 = {a, b, c};
 ListPlot@test1, 
 {a, 0, 10, .1},
 {b, 0, 10, .1},
 {c, 0, 10, .1}
 ]

works.

If you prefer to define test1 globally, this

ClearAll[test1, a, b, c];
test1 = {a, b, c};
Manipulate[
 test1,
 {a, 0, 10, .1},
 {b, 0, 10, .1},
 {c, 0, 10, .1},
 LocalizeVariables -> False,
 TrackedSymbols -> test1
 ]

works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜