Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: What is the point of having Initializations in DynamicModule and Manipulate?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123008] Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • From: Mike H <mike.honeychurch at gmail.com>
  • Date: Sun, 20 Nov 2011 05:37:01 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

Hi David,

The background was that I approached tech support after noticing that stuff
that was to be initialized was not being evaluated prior to the body of the
DynamicModule. My code is too lengthy to post -- runs to several pages and
the initializations are about a page of code. So the point of my question
to users is whether they expect initializations to occur *initially*, i.e.
prior to the evlaution of the body of the DynamicModule.

The code I posted was an example sent to me by tech support of something
working as designed. However that code works as my code does, i.e. the body
is evaluated *prior* to the initializations. To me if that is by design the
design is wrong.

If I place all my initialization code in the body of the DynamicModule then
everything works fine, but my point is solely about how the option In
Initialization is functioning. The example provided is working as designed
apparently -- whereas I regard it as not working.

I welcome the views of other users about this. Maybe some of you can point
out why this is a feature rather than a flaw.

Mike

(Armand when I post from Google groups)



On Sun, Nov 20, 2011 at 4:51 AM, David Park <djmpark 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
> djmpark at comcast.net
> http://home.comcast.net/~djmpark/
>
>
>
> From: Armand Tamzarian [mailto:mike.honeychurch 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
>
>


  • Prev by Date: Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • Next by Date: minimization of a matrix
  • Previous by thread: Re: What is the point of having Initializations in DynamicModule and Manipulate?
  • Next by thread: Re: What is the point of having Initializations in DynamicModule and Manipulate?