Re: Using Intermediate Variables in DynamicModules
- To: mathgroup at smc.vnet.net
- Subject: [mg80290] Re: Using Intermediate Variables in DynamicModules
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 16 Aug 2007 07:23:23 -0400 (EDT)
- Organization: Uni Leipzig
- References: <fa130g$mt7$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
Hi, try f[x_] := x^2 DynamicModule[{a = 0.5, b = 0.5, work, fw}, work = Dynamic[a + b]; fw = f[work]; Column[{ Slider[Dynamic[a], {0, 2}], Slider[Dynamic[b], {0, 2}], {work, work /. w_NumericQ :> f[w]} }] ] Regards Jens David Park wrote: > I am having a lot of trouble with a DynamicModule expression. I believe that > the following examples get to the heart of the matter. > > Here is a DynamicModule that uses an external function f. It has two > internal variables a and b, which are controlled by sliders. The third > output line gives a+b and f[a+b]. > > f[x_] := x^2 > DynamicModule[ > {a = 0.5, b = 0.5}, > Column[{Slider[Dynamic[a], {0, 2}], > Slider[Dynamic[b], {0, 2}], {Dynamic[a + b], Dynamic[f[a + b]]}}]] > > > Notice that f[a+b] is fully evaluated in the output. Now in my actual case I > have a long and complicated expression involving a and b that is used in a > number of different places. So to clarify the entire expression I would like > to define an intermediate expression that can then be used in the dynamic > output. Here is my attempt: > > f[x_] := x^2 > DynamicModule[ > {a = 0.5, b = 0.5, work}, > work = Dynamic[a + b]; > Column[{Slider[Dynamic[a], {0, 2}], > Slider[Dynamic[b], {0, 2}], {work, f[work]}}]] > > Now, although the output is formally correct, the output, and in particular > f[work], is not fully evaluated. I think that may be the cause of my > problem. Why, in the first case does the f expression become fully evaluated > but not in the second case? Is there a simple way to force kernel evaluation > when intermediate expressions are used? (I.ve tried many combinations of > using Dynamic in the output expression and none of them changed the > behavior.) > > Thanks in advanced. I've always gotten great help from this group. >