Re: What is the point of having Initializations in DynamicModule and Manipulate?
- To: mathgroup at smc.vnet.net
- Subject: [mg123047] Re: What is the point of having Initializations in DynamicModule and Manipulate?
- From: Gary <gpalmerlv at gmail.com>
- Date: Mon, 21 Nov 2011 04:29:49 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <398831.35647.1321702630679.JavaMail.root@m06> <jaal6b$13n$1@smc.vnet.net>
It is really nice to have these examples of variations. Would you
explain for a rank beginner what is happening in the following snippet
from your last example?
Dynamic@plot,
Row[{"c: ",
Slider[Dynamic[c, (c = #; calcAll[c]) &], {0, 1},
Appearance -> "Labeled"]}],
That is, what is Dynamic@plot doing? Is it evaluating plot each time
the slider is moved? What is the function of the @ symbol here in
contrast with square brackets?
And in Slider[Dynamic[c, (c = #; calcAll[c]) &], {0, 1},... what is
going on in Dynamic[c, (c = #; calcAll[c]) &] ? Is # being
instantiated with the values from the slider range? Why the ampersand?
Gary
On Nov 20, 2:35 am, "David Park" <djmp... at comcast.net> wrote:
> Mike (or is it Armand? Both nice.),
>
> I don't understand the purpose of the indicated display. Is the plot to be
> completely static and just bundled with the dynamic part? In any case, for a
> static plot:
>
> Module[{g, plot},
> g[x_] := Cos[x];
> plot :=
> Plot[g[x], {x, 1, 10},
> PlotRange -> {-1.1, 1.1},
> Frame -> True,
> ImageSize -> 200];
> DynamicModule[{c = 0.5},
> {plot, Slider[Dynamic[c]], g[Dynamic@c]}]
> ]
>
> For a case where the plot depends on c as a parameter we could use:
>
> DynamicModule[
> {c = 0.5, g, plot},
> g[x_] := Cos[x];
> plot[c_] :=
> Plot[g[c x], {x, 1, 10}, PlotRange -> {-1.1, 1.1}, Frame -> True,
> ImageSize -> 300];
> Column[{
> Dynamic@plot[c],
> Row[{"c: ", Slider[Dynamic[c], {0, 1}, Appearance -> "Labeled"]}]
> }]
> ]
>
> Here is a somewhat more interesting case where we calculate a dependent
> dynamic variable, d, and then use that in making the plot. Here calcAll is
> the routine that calculates the dependent quantities and it is called from
> the second argument of Dynamic in the Slider.
>
> DynamicModule[
> {c = 0.5, d, g, plot, calcAll},
> g[x_] := Cos[x];
>
> (* Calculate dependent dynamic variables *)
> calcAll[cc_] := (d = cc^2;
> plot = Plot[g[d x], {x, 1, 10}, PlotRange -> {-1.1, 1.1},
> Frame -> True,
> ImageSize -> 300]);
>
> (* Initialize *)
> calcAll[c];
>
> (* Display *)
> Column[{
> Dynamic@plot,
> Row[{"c: ",
> Slider[Dynamic[c, (c = #; calcAll[c]) &], {0, 1},
> Appearance -> "Labeled"]}],
> Dynamic@Row[{"d: ", d}]
> }]
> ]
>
> To me, this just seems to be more intuitive, although perhaps a bit more
> wordy. And it works.
>
> David Park
> djmp... at comcast.nethttp://home.comcast.net/~djmpark/
>
> From: Armand Tamzarian [mailto:mike.honeychu... at gmail.com]
>
> I'd be interested in feedback from other users about the way in which
> Initializations works within DynamicModule (and Manipulate?). It is
> fair to say that I expected an initialization to occur "initially."
> That is to say I expected Initialization content to be evaluated prior
> to the body of the DynamicModule. I have exchanged emails with tech
> support about this. Attached is an example sent to me by tech support.
>
> ClearAll[plot, g];
>
> (* and in a separate cell *)
> DynamicModule[{c}, {plot, Slider[Dynamic[c]], g[Dynamic@c]},
> Initialization :> (g[x_] := Cos[x]; plot = Plot[g[x], {x, 1, 10}]),
> UntrackedVariables -> {g}]
>
> It is important for this example to keep the ClearAll line in a
> separate cell so that the global variables are only cleared once. On
> my system (OS X 10.6.8, Mathematica 8.0.4) the output is a list
>
> {plot, "slider", g[0.]}
>
> where "slider" is the actual rendered slider but plot is the word
> (unset variable) plot. In other words Global`plot has no value at the
> time the body of the DynamicModule is evaluated. Ditto Global`g.
>
> If you evaluated the cell (the second cell with the DynamicModule) a
> second time a plot graphic appears in the list and g[0.] becomes
> Cos[0.]. This is not surprising because Global`plot and Global`g now
> have a value.
>
> Wolfram have indicated that this is working as designed -- which is
> presumably why this example was sent to me of how this should work. If
> that is the case it seems poorly designed because (and it is here I
> would like feedback) users would expect initializations to be
> evaluated prior to the body of the DynamicModule -- otherwise what is
> the point?
>
> I'd appreciate the thoughts of other users about this.
>
> Additionally I have been told by Wolfram that wrapping Dynamic around
> the variable(s) that are not being evaluated will trigger the
> evaluation. But why would a user want to make plot or g Dynamic? It
> seems to me that the entire purpose of having an Initialization is to
> evaluate this "one off" variable/functions.
>
> thanks
>
> Mike