Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I don't understand the following:

  f[a_, b_] := a + b
  ls = {1, 2, 3};
  MapThread[f, {ls, {10, 20, 30}}]

This yields ( as expected ) {11,22,33}

If I change the code to

  f[a_, b_] := a + b
  ls = {1, 2, 3};
  MapThread[f, {Dynamic[ls], {10, 20, 30}}]

mma returns the following message:

  MapThread::mptd: Object {1,2,3} at position {2, 1} in MapThread[f,{{1,2,3},{10,20,30}}] has only 0 of required 1 dimensions. >>

Question: Why does this happen? How can it be fixed?

share|improve this question
    
The first gives me {f[1, 10], f[2, 20], f[3, 30]}; I suppose you cut and pasted the wrong thing! – acl May 20 '12 at 9:54
    
Already changed it, sorry ! – nilo de roock May 20 '12 at 9:58
up vote 10 down vote accepted

Even though Dynamic[ls] is presented as ls, its head is Dynamic:

x = Dynamic[ls]
Head[x]
(*
{1, 2, 3}
Dynamic
*)

or

Basically, Dynamic is a wrapper, and is there even if you cannot see it. Its like you were writing

MapThread[f, {symb, {10, 20, 30}}]

with symb undefined, or

MapThread[f, {Integrate[symb[var], var], {10, 20, 30}}] 

You can fix it by doing

Dynamic[MapThread[f, {ls, {10, 20, 30}}]]

instead.

So:

Wherever you had written Dynamic[ls], you still have Dynamic[ls]. It may be displayed by the frontend as {1,2,3}, but it is not; it's Dynamic[ls]. So, MapThread goes to look at the first part of its second argument and sees Dynamic instead of a List, so it stops and emits a message; this message includes Dynamic[ls] in it. When the message reaches the "surface" to be displayed, Dynamic[ls] gets automatically interpreted by the frontend as usual: it's displayed as {1,2,3}, because that's what ls evaluates to. But it is still Dynamic[ls], not {1,2,3}, which is simply how it is displayed.

Try this: Do[Sin[i], Dynamic@{i, 1, 2}] and think about what happens...

share|improve this answer
    
Sorry, I don't get this. What should I do to fix this? – nilo de roock May 20 '12 at 10:04
    
I still don't get, but it WORKED. Dynamic still has its mysteries to me. Thank you. :-) – nilo de roock May 20 '12 at 10:22

Dynamic doesn't work the way you think it does. See this answer for a full explanation.

In short, Dynamic doesn't do anything until it is actually displayed on screen.
Therefore, you are essentially doing this (note the String):

MapThread[f, {"Dynamic[ls]", {10, 20, 30}}]

As acl already showed, you can wrap the entire expression in Dynamic so that it does not attempt to evaluate the failing condition shown above.

share|improve this answer
    
Can we move here the linked topic? – Kuba Feb 25 at 5:59
    
@Kuba You mean migrate the question form Stack Overflow? No, that's not done anymore. There was a one-time offer to migrate important questions when they introduced the new policy, but now nothing over 30 (or 60?) days old can be migrated. – Mr.Wizard Feb 25 at 6:13
    
Pity, thanks for info. Your answer and linked topic are quite broader than the scope of this question. And often I'm linking this topic because of your answer really. A generic one would be nice. – Kuba Feb 25 at 6:20
    
@Kuba Ask John Fultz to do a self-Q&A on this site to share that information, along with any updates he might want to make since 2009. If he is too busy we can just do a CW post and quote that one verbatim. – Mr.Wizard Feb 25 at 6:23
    
Feel free to do that, I'm not so motiviated at the moment, maybe I will be soon. I will try to not forget that and do this when another broader but closely related question appears. – Kuba Feb 25 at 6:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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