Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5374 > unrolled thread

Re: DynamicModule Pure Function

Started byDon <donabc@comcast.net>
First post2011-10-05 07:59 +0000
Last post2011-10-06 08:28 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: DynamicModule Pure Function Don <donabc@comcast.net> - 2011-10-05 07:59 +0000
    Re: DynamicModule Pure Function A Retey <awnl@gmx-topmail.de> - 2011-10-06 08:28 +0000

#5374 — Re: DynamicModule Pure Function

FromDon <donabc@comcast.net>
Date2011-10-05 07:59 +0000
SubjectRe: DynamicModule Pure Function
Message-ID<j6h2pr$6ol$1@smc.vnet.net>
Thank you all for instructive insights into this problem of why the 
pure function works the way it does.

To see if I understood the points made in the emails, I changed
the original problem very slightly and tried to find
a similar solution to the new problem using a modified pure function
from the original problem.

The new problem is exactly the same as the original problem except
that the range for Slider #2 goes from 0 to 2 instead of 0 to 1.
That is the only modification to the original problem.  
The problem is to find an inverse function that will allow Slider #1 to 
control Slider #2 throughout its range and vice versa.

To define this modified problem completely:

(1) The starting position of Slider #1 with range 0 to 1 is 0.

(2) The starting position of Slider #2 with range 0 to 2 is 2.

(3) When Slider #1 in moved to the right, over its range from  0 to 1, Slider #2
should go to the left from 2 to 0.

(4) When Slider #2 is moved to the left, over its range from 2 to 0, Slider #1 
should go to the right from 0 to 1.

I was not able to solve this problem.

The closest I was able to do is to break up the problem
into component parts, hoping to synthesize a solution
from the parts.

The code below works when Slider #1 is controlling both Sliders. That is to say,
it satisfies the requirements 1, 2 and 3 above:   
the starting positions are 0 for Slider #1 and 2 for Slider #2 and when
Slider #1 is moved to the right over its range of 0 to 1, Slider #2 goes to the left
from   2 to 0.

ap = Appearance->"Labeled";

DynamicModule[{x = 0}, {Slider[Dynamic[x], ap], 
  Slider[Dynamic[2 - 2 x, (x = 2 - 2 #) &], {0, 2}, ap]}]



But, I was not able to find a function that would satisfy requirements 1, 2 and 4 above
where Slider #2 is controlling the action.

In addition, to solve the problem completely, there would have to be a synthesis
of both component solutions.

Is there a way to do this?

Thank you in advance.

[toc] | [next] | [standalone]


#5428

FromA Retey <awnl@gmx-topmail.de>
Date2011-10-06 08:28 +0000
Message-ID<j6jor4$m9p$1@smc.vnet.net>
In reply to#5374
Hi,

> To see if I understood the points made in the emails, I changed
> the original problem very slightly and tried to find
> a similar solution to the new problem using a modified pure function
> from the original problem.
>
> The new problem is exactly the same as the original problem except
> that the range for Slider #2 goes from 0 to 2 instead of 0 to 1.
> That is the only modification to the original problem.
> The problem is to find an inverse function that will allow Slider #1 to
> control Slider #2 throughout its range and vice versa.
>
> To define this modified problem completely:
>
> (1) The starting position of Slider #1 with range 0 to 1 is 0.
>
> (2) The starting position of Slider #2 with range 0 to 2 is 2.
>
> (3) When Slider #1 in moved to the right, over its range from  0 to 1, Slider #2
> should go to the left from 2 to 0.
>
> (4) When Slider #2 is moved to the left, over its range from 2 to 0, Slider #1
> should go to the right from 0 to 1.
>
> I was not able to solve this problem.
>
> The closest I was able to do is to break up the problem
> into component parts, hoping to synthesize a solution
> from the parts.
>
> The code below works when Slider #1 is controlling both Sliders. That is to say,
> it satisfies the requirements 1, 2 and 3 above:
> the starting positions are 0 for Slider #1 and 2 for Slider #2 and when
> Slider #1 is moved to the right over its range of 0 to 1, Slider #2 goes to the left
> from   2 to 0.
>
> ap = Appearance->"Labeled";
>
> DynamicModule[{x = 0}, {Slider[Dynamic[x], ap],
>    Slider[Dynamic[2 - 2 x, (x = 2 - 2 #)&], {0, 2}, ap]}]
>
>
>
> But, I was not able to find a function that would satisfy requirements 1, 2 and 4 above
> where Slider #2 is controlling the action.
>
> In addition, to solve the problem completely, there would have to be a synthesis
> of both component solutions.
>
> Is there a way to do this?

This would do what I think you want:

DynamicModule[{x = 0}, {Slider[Dynamic[x]],
   Slider[Dynamic[2 - 2 x, (x = 1 - #/2) &], {0, 2}]}]

Although you only got the arithmetics wrong, I'm quite sure that that 
was not what caused your problem. I think things are much simpler as you 
think they are: you probably want to first get a grip of pure functions 
and if you are familiar with them look at the documentation of Dynamic 
to learn what the optional second argument does.

You should also understand that you could achieve what you want without 
using pure functions at all (note that the second argument of Dynamic is 
actually called with two arguments):

setx[val_, expr_] := (x = 1 - val/2);

{
  Slider[Dynamic[x]],
  Slider[Dynamic[2 - 2 x, setx], {0, 2}]
  }

but if you want to localize x with DynamicModule this gets a little more 
complicated, e.g.:

DynamicModule[{x = 0}, {
   Slider[Dynamic[x]],
   Slider[Dynamic[2 - 2 x, setx], {0, 2}]
   }, Initialization :> (setx[val_, expr_] := (x = 1 - val/2);)]

The reason why you will almost always see pure functions used as second 
argument to Dynamic might be that it avoids the two complications shown 
above...


hth,

albert

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web