MathGroup Archive 2010

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

Search the Archive

Re: Initialization problem in a DynamicModule

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106664] Re: Initialization problem in a DynamicModule
  • From: "Norbert P." <bertapozar at gmail.com>
  • Date: Wed, 20 Jan 2010 06:50:36 -0500 (EST)
  • References: <hj40pp$sgd$1@smc.vnet.net>

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



  • Prev by Date: Re: First function debug help
  • Next by Date: Re: Re: More /.{I->-1} craziness
  • Previous by thread: Re: Re: Initialization problem in a DynamicModule
  • Next by thread: Re: Re: Initialization problem in a DynamicModule