Re: Dynamic

*To*: mathgroup at smc.vnet.net*Subject*: [mg87395] Re: Dynamic*From*: Albert Retey <awnl at arcor.net>*Date*: Wed, 9 Apr 2008 05:57:03 -0400 (EDT)*References*: <ftfeji$bue$1@smc.vnet.net>

Gianluca Gorni wrote: > Hi! > > I am trying to learn how the new Dynamic features work. > > The following two Manipulate expressions give outputs that > make perfect intuitive sense to me: > > Manipulate[q = p + 1; > q, > {p, 0, 1}] > > Manipulate[q = p + 1; > FullForm@q, > {p, 0, 1}] > > (don't keep both outputs active at the same time > if you are short on battery power!) > > When I try to reproduce similar effects with > DynamicModule, I get all sorts of different behaviours. > Compare the following: > > DynamicModule[{p = 0, q}, > q = p + 1; > {Slider[Dynamic@p], q}] > > DynamicModule[{p = 0, q}, > q = p + 1; > {Slider[Dynamic@p], Dynamic@q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p] + 1; > {Slider[Dynamic@p], q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p] + 1; > {Slider[Dynamic@p], FullForm@q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p] + 1; > {Slider[Dynamic@p], FullForm@Dynamic@q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p] + 1; > {Slider[Dynamic@p], Dynamic@FullForm@q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], q[[1, 1]]}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], q[[1, 2]]}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], Dynamic@q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], Dynamic[q[[1, 1]]]}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], Dynamic[q[[1, 2]]]}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], FullForm@q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], Dynamic@FullForm@q}] > > DynamicModule[{p = 0, q}, > q = Dynamic[p + 1]; > {Slider[Dynamic@p], FullForm@Dynamic@q}] > > Right now all I need is a few kind words of encouragement > from somebody who has already mastered dynamic interactivity... > > Gianluca > > I think this will do what you want: DynamicModule[{p = 0, q}, { Slider[Dynamic@p], Dynamic[ q = p + 1; q ] } ] it might help to split things into "controller" and "viewer" parts, where in this case the controller is the slider, the viewer part is all the code that needs to be executed to show your results. Here this is (q=p+1;q) and needs to be entirely wrapped with Dynamic. To get a first working version you can always do this (enclose everything that's used to create the view with Dynamic), but for many realistic cases you will not need to recalculate everything that is shown (or "part of the viewer"). Then for performance reason you should only use Dynamic on those parts that need to be recalculated when one of the controller changes a variable. Here the explanation does not change, so no Dynamic is needed for it: DynamicModule[{p = 0, q}, Row[{ Slider[Dynamic@p], " this is the value: ",Dynamic[p+1] }] ] hth, albert