7
$\begingroup$

A minimal example for the Bonus in Can a Dynamic be attached to the single elements of a list?

DynamicModule[{foo = Hold[$1, $2], $1 = 0, $2 = 0},
  {
    Dynamic[Print["redrawing first"]; foo[[1]]],
    Dynamic[Print["redrawing second"]; foo[[2]]],
    Button["first", ++$1],
    Button["second", ++$2]
  }
]

Observe the Messages window as these buttons are pressed. The redraw messages are printed independently after the initial evaluation, i.e. you can get a message list like:

redrawing first

redrawing second

redrawing first

redrawing first

redrawing first

redrawing first

redrawing second

(corresponding to 4, 1, in the expression)

By what mechanism does Mathematica track the value of $1 or $2 changing and update each Dynamic individually despite neither Symbol appearing explicitly in the bodies?

$\endgroup$
2
  • $\begingroup$ I imagine this behavior has come up before. If anyone can suggest a duplicate please either comment here or if you have sufficient reputation vote to close as a duplicate if appropriate. $\endgroup$
    – Mr.Wizard
    Commented Feb 23, 2017 at 11:13
  • $\begingroup$ The following might be an explanation. The symbols that are tagged for updating are all symbols that are used for the evaluation of the contents. At the first display of the DM, the kernel has to evaluate foo[[1]]. That requires $1 and therefore $1 is tagged with the dynamic object number for foo[[1]]. That is confirmed by the result of Internal`GetTrackedIDs[]. Therefore, each time when we press the button for $1, also the output of foo[[1]] is updated. $\endgroup$ Commented Feb 23, 2017 at 18:44

1 Answer 1

5
$\begingroup$

From:

tutorial / AdvancedDynamicFunctionality / Automatic Updates of Dynamic Objects

[...] If a variable value, or some other state of the system, changes, the dynamic output should be updated immediately. [...] It is critical that dependencies be tracked so that dynamic outputs are evaluated only when necessary.

And the most important part:

[...] the system keeps track of which variables or other trackable entities are actually encountered during the process of evaluating a given expression. Data is then associated with those variable(s) identifying which dynamic expressions need to be notified if the given variable receives a new value. [...]

And how this association/building dependency tree is performed can be adjusted by TrackedSymbols option. By default it is All so it will be scanning and gathering symbols needed when foo[[1]] is evaluated.

For TrackedSymbols -> Full it will stop on a visible level, so only changes to foo itself will trigger changes.

$\endgroup$
8
  • $\begingroup$ I am sure I have read and forgotten that. Hopefully gIS is as satisfied by this answer as I am. $\endgroup$
    – Mr.Wizard
    Commented Feb 23, 2017 at 10:25
  • $\begingroup$ At the moment do you know what other trackable entities means? I thought only Symbols could be tracked. I really need to read this tutorial again. $\endgroup$
    – Mr.Wizard
    Commented Feb 23, 2017 at 10:29
  • 1
    $\begingroup$ @Mr.Wizard don't know much but it seems that CurrentValue[$FrontEnd, {TaggingRules, "key"}] can be automatically sensitive for specific "key" not to CurrentValue or TaggingRules in general. It probably refers to things like Mouseover and other event related entities too., $\endgroup$
    – Kuba
    Commented Feb 23, 2017 at 10:33
  • $\begingroup$ @Mr.Wizard and Kuba: what I don't fully understand if how this tracking process happens. I get now that it happens during evaluation, which explains how the Dynamic can find out at all about $1 and $2. But given that $1 in this example starts with a value, the evaluation sequence of foo[[1]] should end up directly into the numerical value 0. So does this mean that the evaluation sequence of an expression wrapped in Dynamic is a special one, in the sense that at every single evaluation step the result is checked for trackable symbols? $\endgroup$
    – glS
    Commented Feb 23, 2017 at 11:35
  • 1
    $\begingroup$ Don't have much time now, will try to add some examples later but if you run: foo[[1]] = $1 and $1 = $2 and $2 = 3 then Kernel says, 'foo changed, update DynamicBoxObject[id]', but kernel will also rewrite dependency tree and this dynamic will be only related to foo and $2 alone, as you replaced $1 inside with its value. $\endgroup$
    – Kuba
    Commented Feb 23, 2017 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.