Re: Dynamic

*To*: mathgroup at smc.vnet.net*Subject*: [mg87394] Re: Dynamic*From*: Szabolcs Horvát <szhorvat at gmail.com>*Date*: Wed, 9 Apr 2008 05:56:52 -0400 (EDT)*Organization*: University of Bergen*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... Note that Dynamic is used for *displaying* an expression (and reevaluating it before displaying it if necessary). So q = Dynamic[p+1] cannot be used as a value in calculations. Here's an example: 1 + Dynamic[a] a = 1 After setting 'a' to 1, 1+1 will be displayed and not 2. This is one way to implement something similar to the original Module[] example: DynamicModule[{p = 0, q}, {Slider[Dynamic[p]], Dynamic[q = p + 1; q]}] Each time 'p' changes, the complete expression 'q=p+1; q' will be re-evaluated. I hope this helps, Szabolcs