MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Initialization problem in a DynamicModule

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106700] Re: [mg106664] Re: Initialization problem in a DynamicModule
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Thu, 21 Jan 2010 04:54:40 -0500 (EST)
  • References: <hj40pp$sgd$1@smc.vnet.net> <201001201150.GAA09317@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

Yes, I see the difference now.

> So I expect, when this expression gets evaluated, that simply
> "something" has OwnValues containing Print["you should never see
> this!"], but the Print command doesn't get evaluated until I
> explicitly evaluate the "something" Symbol.

But, apparently, your expectation is wrong.

It looks as if "something" is not only defined by the Initialization  
construct, but it's also evaluated at some point. Why, I don't know.

Perhaps it's part of saving the value for a possible later opening of the  
same notebook.

Bobby

On Wed, 20 Jan 2010 13:10:26 -0600, Norbert Pozar <bertapozar at gmail.com>  
wrote:

> Hi Bobby,
>
> but that's not quite the same thing. Yes, I expect Initialization
> expression to be evaluated before displaying Dynamic. But if you look
> carefully at my example, the expression is
>
> SetDelayed[something, Print["you should never see this!"]]
>
> So I expect, when this expression gets evaluated, that simply
> "something" has OwnValues containing Print["you should never see
> this!"], but the Print command doesn't get evaluated until I
> explicitly evaluate the "something" Symbol. This is not what happens
> with DynamicModule, because DynamicModule evaluates the symbols
> internally. That can be quite surprising, if the symbols have side
> effects.
>
> Best,
> Norbert
>
> On Wed, Jan 20, 2010 at 10:34 AM, DrMajorBob <btreat1 at austin.rr.com>  
> wrote:
>>> As you can see, DynamicModule evaluates its variables, even though I
>>> wouldn't expect it to do it. Is there anything about it in the
>>> documentation?
>>
>> Look at, for instance, the first two examples on the Help page for
>> Initialization:
>>
>> Specify an expression to be evaluated before displaying Dynamic:
>> Dynamic[c[1],Initialization:>(c[x_]:={x})]
>> {1}
>>
>> DynamicModule:
>> DynamicModule[{c},Dynamic[c[1]],Initialization:>(c[x_]:={x})]
>> {1}
>>
>> Bobby
>>
>> On Wed, 20 Jan 2010 05:50:36 -0600, Norbert P. <bertapozar at gmail.com>  
>> wrote:
>>
>>> Hi Istv=E1n,
>>>
>>> this is one of the weird behaviors of DynamicModule. The dynamic
>>> functionality is awesome, but I find it very hard to create a more
>>> involved interface due to the unpredictability of the components. More
>>> involved means anything more than the simple demos from
>>> demonstrations.wolfram.com. Even though the documentation is quite
>>> extensive, many details are missing.
>>>
>>> I was staring at your code for quite a while =) It turns out that it
>>> still contains a lot of clutter. You could've stripped most of it and
>>> you'd get:
>>>
>>> In[1]:=
>>> DynamicModule[{something},
>>>  1,
>>>  Initialization:>(something:=(Print["you should never see this!"]);)
>>> ]
>>> Out[1]= 1
>>> During evaluation of In[1]:= you should never see this!
>>>
>>> As you can see, DynamicModule evaluates its variables, even though I
>>> wouldn't expect it to do it. Is there anything about it in the
>>> documentation?
>>>
>>> The solution for you is to define functions not as OwnValues as above,
>>> but as DownValues, as in:
>>>
>>> In[2]:= DynamicModule[{something},
>>>  1,
>>>  Initialization:>(something[]:=(Print["you should never see
>>> this!"]);)
>>> ]
>>> Out[2]= 1
>>>
>>> In this case, something doesn't get evaluated and it works the way you
>>> expect. So try
>>>
>>> Panel@DynamicModule[{x,y,assign,initialize},Grid[{{"","A","B"},{"w/
>>> assign:",RadioButton[Dynamic[x,(x=#;assign[])&],True],RadioButton
>>> [Dynamic[x,(x=#;assign[])&],False]},{"w/o assign:",RadioButton
>>> [Dynamic@x,True],RadioButton[Dynamic@x,False]},
>>> {"value:",Dynamic@x}},Dividers->{False,
>>> {False,True,True,True,False}},Alignment->{Left,
>>> {Center}}],Initialization:>(assign[]:=(y=x);(*further variables to
>>> update*)initialize[]:=(x=True;assign[]);(*initialization function*)
>>> initialize[];)]
>>>
>>> For some reason, DynamicModule evaluates its variables whenever I
>>> assign to x as in the following code by clicking the first radio
>>> button (it evaluates it twice!!), but not when I press the second:
>>>
>>> DynamicModule[{x=False,something,i=0},
>>> {RadioButton[Dynamic[x,(x=#)&],True],RadioButton[Dynamic
>>> [x],False],Dynamic[i]},Initialization:>(something:=(Print["init",i+
>>> +]))]
>>>
>>> It would be great to hear from someone who knows more about the
>>> internal working of DynamicModule. My all-time favourite bug is:
>>>
>>> In[3]:= DynamicModule[{x=Sequence[]},1]
>>> During evaluation of In[3]:= Transpose::nmtx: The first two levels of
>>> the one-dimensional list {{Hold[x]},{}} cannot be transposed. >>
>>> Out[3]= Manipulate`Dump`eDynamicModule[Transpose
>>> [Manipulate`Dump`heldsetting[{{Hold[x]},{}}]],1,DynamicModuleValues:>
>>> {}]
>>>
>>> Compare that to ordinary module:
>>>
>>> In[4]:= Module[{x=Sequence[]},1]
>>> Out[4]= 1
>>>
>>> I discovered it a couple days after buying Mathematica 6, still
>>> excited about the new dynamic functionality. That was pretty
>>> disappointing;)
>>>
>>> Best,
>>> Norbert
>>>
>>> On Jan 19, 2:16 am, Istv=E1n <replicator... at gmail.com> wrote:
>>>>
>>>> Dear Group,
>>>>
>>>> I have some problem with a complex interface inside a DynamicModule.
>>>> This is a toy version of the program, which can fully reproduce the
>>>> malfunction:
>>>>
>>>> Panel@DynamicModule[
>>>>  {x, y, assign, initialize},
>>>>
>>>>  Grid[{
>>>>    {"", "A", "B"},
>>>>    {"w/ assign:", RadioButton[Dynamic[x, (x = #; assign) &], True]=
>>>
>>> ,
>>>>
>>>>     RadioButton[Dynamic[x, (x = #; assign) &], False]},
>>>>    {"w/o assign:", RadioButton[Dynamic@x, True],
>>>>     RadioButton[Dynamic@x, False]},
>>>>    {"value:", Dynamic@x}
>>>>    }, Dividers -> {False, {False, True, True, True, False}},
>>>>   Alignment -> {Left, {Center}}],
>>>>
>>>>  Initialization :> (
>>>>    assign := (y = x);(* further variables to update *)
>>>>    initialize := (x = True; assign); (* initialization function =
>>>
>>> *)
>>>>
>>>>    initialize;
>>>>    )
>>>>  ]
>>>>
>>>> Now for some reason, the radiobuttons do not function as intended (at
>>>> least as I want).
>>>> The following clicking orders do not work:
>>>>
>>>> [w/ + B] then [w/ + A]
>>>> [w/o + B] then [w/ + A]
>>>>
>>>> these work correctly:
>>>>
>>>> [w/ + B] then [w/o + A]
>>>> [w/o + B] then [w/o + A]
>>>>
>>>> I guess, that the problem is with the "initialize" (or the "assign")
>>>> function. Any idea?
>>>> Thanks in advance
>>>>
>>>> Istv=E1n
>>>
>>>
>>
>>
>> --
>> DrMajorBob at yahoo.com
>>
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: Re: Initialization problem in a DynamicModule
  • Next by Date: Re: Re: Plotting date-time series in 3D how to handle date-time to plot
  • Previous by thread: Re: Re: Initialization problem in a DynamicModule
  • Next by thread: First function debug help