5
$\begingroup$

Suppose I have a function that requires a list is as input. I.e.

f[list_]:=DoSomethingWithList

Now, if I pass a variable as Dynamic like this

pts={{0.1,0.1} , {0.5,0.1}, {0.1, 0.5}};
f[Dynamic[pts]]

everything works as expected as long as I don't use parts of the list. If I use a part of a list then things no longer work as expected ( by me ).

Another example

g[point_] := Slider2D[point]
p = {.1, .1}
g[Dynamic[p]]
Dynamic[p]

that works as expected, and this one

g[point_] := Slider[point[[1]]]
p = {.1, .1}
g[Dynamic[p]]
Dynamic[p]

that does not.

Question: What behaviour of Dynamic explains the above?

$\endgroup$

2 Answers 2

8
$\begingroup$

You should try to just follow the flow of your replacements, then you clearly see what's going wrong. In you first definition everything goes as you expect:

g[point_] := Slider2D[point]
g[Dynamic[p]] =  (* Becomes => Slider2D[Dynamic[p]] *) 

But in the second case you are stripping away the Dynamic tag:

g[point_] := Slider[point[[1]]]
g[Dynamic[p]] (*Becomes =>Slider[Dynamic[p][[1]]]  Which becomes => Slider[p]*)

Note: This is why your output is Slider[{0.1, 0.1}].

If you want slider Dynamically editing point[[1]] you need the Dynamic wrapper to be around it, so you would do:

g[point_] := Slider[point /. Dynamic[var_] :> Dynamic[var[[1]]]]
p = {.1, .1}
g[Dynamic[p]]
Dynamic[p]

Though I would personally define this as:

g[Dynamic[point_]] := Slider[Dynamic[point[[1]]]]

Simply to make it clear that I'm defining the behavior for dynamically changeable variables.

$\endgroup$
1
  • $\begingroup$ I go for how you do it personally. Dynamic is what makes Mma powerful AND difficult, if you ask me. $\endgroup$ Aug 7, 2012 at 13:35
3
$\begingroup$

Perhaps this helps to see what is going on:

f[l_] := {Head[l], Head[l[[1]]]};
pts = {{0.1, 0.1}, {0.5, 0.1}, {0.1, 0.5}};
f[pts]
f[Dynamic[pts]]

gives

(*
{List, List}
{Dynamic, List}
*)

Why? Because even though Dynamic[{1,2,3}] gets printed as {1,2,3}, this is just output formatting; in reality, it remains Dynamic[{1,2,3}]. This is why when you look at the Head of the dynamic object passed to f you see Dynamic, but if you look at its first part you see List.

In summary then, putting Dynamic around an expression changes its head, even though it is still displayed as if the Dynamic wasn't there.

Another way to see this is to evaluate Dynamic[x] then select the output cell and shift-cmd-E to show what it is:

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ What I see is that Head works on part of list with Dynamic in front of it. Then why, for example, doesn't Slider work on that part? $\endgroup$ Aug 7, 2012 at 12:58
  • $\begingroup$ what do you mean? What do you expect Slider[{.1,.1}] to do? $\endgroup$
    – acl
    Aug 7, 2012 at 13:43
  • $\begingroup$ Thanks, jVincent already explained it in his answer. I am clearer on Dynamic now. $\endgroup$ Aug 7, 2012 at 14:25

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.