Re: DynamicModule Pure Function

*To*: mathgroup at smc.vnet.net*Subject*: [mg121795] Re: DynamicModule Pure Function*From*: "David Park" <djmpark at comcast.net>*Date*: Mon, 3 Oct 2011 04:20:16 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <15107450.116947.1317535292196.JavaMail.root@m06>

Don, In all of the following I will use the default domain of the sliders, which is 0 to 1. We could, of course, use different domains. It's important to know the domain of the Slider. Think first of the slider as an OUTPUT device that shows the value of its expression by the position of the slider within the domain. If the current value is outside the domain the slider is put to the end with a red indication of being outside the domain. A slider does not come with a built-in scale and ticks so we can only estimate the value of a slider by its position. We can, however, use the Appearance -> "Labeled" option to show the value. So here is an example of using the slider as an output device. (In these immediate examples do not move the Slider. We are only looking at how it outputs values.) x = 0.25 Slider[Dynamic[x], Appearance -> "Labeled"] Set different values (including values outside the Slider domain) of x in the first statement and the Slider will reflect the value. We could use any expression of x as the first argument in Dynamic. Its value will determine the setting of the Slider. Notice that the labeled value is the value of the Slider with respect to its domain, and not the value of x. (This Slider will not work if you try to move it.) Slider[Dynamic[1 - x], Appearance -> "Labeled"] Of course, the whole purpose of a Slider is to INPUT values so we have to make certain that the input and output values coincide. This is done with the second argument of Dynamic. Here we should think of # as the output value associated with the actual display position of the Slider. Now you can move the Slider. In the following the Slider display goes from 0 to 1 and so does the value of x. {Slider[Dynamic[x, (x = #) &], Appearance -> "Labeled"], Dynamic[x]} In the following the value of the Slider goes from 0 to 1 (It always goes over the specified domain) but the value of x goes from 1 to 0. {Slider[Dynamic[1 - x, (x = 1 - #) &], Appearance -> "Labeled"], Dynamic[x]} Generally we will want the setting of x in the second argument to be consistent with the value of the Slider given by #, otherwise the behavior will be wacky. However, we can use the second argument to constrain the location of the Slider in some meaningful way (or even calculate secondary dynamic variables that might be used in a calculation). Here we use the second argument to constrain the Slider to move in steps of 0.1. (We could also have done this in the domain specification.) DynamicModule[ {x = 0, y = 0}, {Slider[Dynamic[x, (x = Round[#, 0.1]; y = x^2) &], Appearance -> "Labeled"], Dynamic[x], Dynamic[y]}] Summary 1) Know the domain of the Slider 2) The first argument of Dynamic determines the displayed setting of the Slider, with respect to the domain, and any labeled value. 3) The second argument of Dynamic determines the value of a dynamic variable from the displayed setting, #, and can also be used to constrain the dynamic variable and calculate secondary dynamic variables. David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ From: Don [mailto:donabc at comcast.net] In the Wolfram tutorial "Introduction to Dynamic" (tutorial/IntroductionToDynamic) there is the following example approximately halfway into the tutorial: DynamicModule[{x = 0}, {Slider[Dynamic[x]], Slider[Dynamic[1 - x, (x = 1 - #) &]]}] The explanation that Wolfram gives for why this works (i.e. the second Slider is the inverse function of the first Slider) is unintelligible to me. Can anyone explain in plain, simple English (no jargon) what is going on here? For example, when x = 0.25 the second slider has to be at position 0.75. It can't update 1-x directly since 1-x cannot be set to a value. Of course, this is true, but it doesn't explain why the pure function is the solution. If x = 0.25, this implies that the # is equal to 0.75 since x = 1 - #. But, how did the system know that # = 0.75? Where did the 0.75 come from? To put it another way, I am use to the # being a placeholder in pure functions that ranges over a set of values. For example, in Select[list, # > 0 &], the # ranges over all the elements of list. What is # in the pure function of the second Dynamic ranging over and how does it come up with 0.75 as a value for #? Thank you in advance for any clear explanations you can give me.