2
$\begingroup$

I'm a newbie - what am I doing wrong? I want to use parts of lists (vectors) in calculations but Mathematica will not evaluate anything that involves parts of lists that update dynamically. Is there a way round this?

Example 1 (can't add u[[1]]+u[[2]]):

{Slider[Dynamic[a], {0, 10, 1}], Dynamic[a]}

u = Dynamic@{a, a + 1}
Head[u]
Head[u[[1]]]
u[[1]] + u[[2]]

Out[221]= {2,3}
Out[222]= Dynamic
Out[223]= List
During evaluation of In[221]:= Part::partw: Part 2 of {2,3} does not exist. >>
Out[224]= {({2,3})[[2]]+7,({2,3})[[2]]+8}

Example 2 (can't add v[[1]]+v[[2]]):

{Slider[Dynamic[a], {0, 10, 1}], Dynamic[a]}

v = {Dynamic[a], Dynamic[a + 1]}
Head[v]
v[[1]] + v[[2]]

Out[210]= {2,3}
Out[211]= List
Out[212]= 3+2

Example 3 (w[[1]] + w[[2]] doesn't update dynamically):

{Slider[Dynamic[a], {0, 10, 1}], Dynamic[a]}

w = {a, a + 1}
Dynamic[w] (* doesn't update *)
w[[1]] + w[[2]](* doesn't update *)

Out[95]= {4,5}
Out[96]= {4,5}
Out[97]= 9
$\endgroup$
  • $\begingroup$ Hi, welcome to Mathematica.SE, please consider taking the tour so you learn the basics of the site. Once you gain enough reputation by making good questions you will be able to vote up and down both questions and answers. When you see good ones, please vote them up by clicking the grey triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – rhermans Nov 25 '14 at 22:52
  • 1
    $\begingroup$ w := {a, a + 1}; Dynamic[ w[[1]] + w[[2]] ] $\endgroup$ – Kuba Nov 25 '14 at 22:57
  • $\begingroup$ Please take a look at tutorial: IntroductionToDynamic. $\endgroup$ – Kuba Nov 25 '14 at 23:00
  • $\begingroup$ Thank you. The delayed assignment makes all the difference, which sort of makes sense though it's stretching my understanding. I've looked at every Dynamic tutorial I can find but it still seems a bit random (though this is probably because Mathematica as a whole is still new to me). $\endgroup$ – Adam Nieman Nov 26 '14 at 1:34
5
$\begingroup$

How about this?

{Slider[Dynamic[a], {0, 10, 1}], Dynamic[a]}

Dynamic[u = {a, a + 1}]
Dynamic[u[[1]] + u[[2]]]

Edit

Regarding Kuba's comment

So is pure code an educational answer then?

No, of course it is not and most of my answers reflect that I care about this. Let me make some extensions to my answer by going through your examples and explaining in detail what goes wrong:

Example 1:

{Slider[Dynamic[a], {0, 10, 1}], Dynamic[a]}
u = Dynamic[{a, a + 1}]

When you first evaluate this, then you see the slider and the output {0,1}. What you have to understand is, that although you see a list of numbers, this is only the display form of a much more complex expression. In fact, if you evaluate u // InputForm, you see that u is not a list of numbers, but still Dynamic[{a, a + 1}].

The front end transforms this into the dynamically changing list of numbers for you, but only for displaying purpose. If you now try to use u[[1]] you basically extract the {a, a+1} part, because this is the first element of the expression Dynamic[{a,a+1}]. This is, because a has a value, instantly converted into {0,1}

u[[1]]
(* {0, 1} *)

It should be clear now, that u[[2]] doesn't exists, because the expression Dynamic[{a,a+1}] has only one argument. Therefore you get the error message. If you want to add the two values inside Dynamic, you still can do this. You only have to keep the Dynamic intact and everything will work. Let me give an example where you see what happens:

Plus @@@ dynamic[{myA, myA + 1}]
(* dynamic[1 + 2 myA] *)

When you think of your original expression, chances are good that this does the thing you tried with u[[1]]+u[[2]] and the wrapping dynamic will still be there. Let's try it:

Plus@@@u
(* after moving the slider: 7 *)

I hope this clears the situation a bit.

Example 2:

Your second example has exactly the same problem:

v = {Dynamic[a], Dynamic[a + 1]}
v[[1]] + v[[2]]

Again, keep in mind that the Dynamic doesn't go away. You just don't see it anymore, but it is still there! So if you want a non-dynamic version of your sum, you need to access the inner parts of the two Dynamic:

v[[1, 1]] + v[[2, 1]]
(* 7 here *)

If you want a dynamically changing sum, you have to keep the Dynamic wrapped around your expression. One way is the replacement

v /. {Dynamic[first_], Dynamic[last_]} :> Dynamic[first + last]

Example 3:

In your third example, unfortunately everything is too late. In the moment you evaluate

w = {a, a + 1}

the right hand side is evaluated. Since a has a value, w is assigned a list of numbers and does never know that you want it to depend on a. The workaround was already given by Kuba.

$\endgroup$
  • $\begingroup$ Without any comments it may be misleading, as soon as Dynamic[u = {a, a + 1}] scrolls out of the view... :) $\endgroup$ – Kuba Nov 25 '14 at 23:07
  • $\begingroup$ @Kuba That's one of the first lessons you have to learn, that things with Dynamic only make sense when they are displayed. You could use a single Dynamic and pack assignment and access of u[[..]] into one place, or you use delayed assignment as you showed. But currently, the question lacks of the information, what he tries to achieve. $\endgroup$ – halirutan Nov 25 '14 at 23:14
  • $\begingroup$ This way makes less intuitive sense to me, but I suppose it works the same way as the SetDelayed. The reason I need dynamic access to parts of lists is I'm trying to do some astronomy using sidereal time as a dynamic variable that determines the orientation of all sorts of planes and vectors. $\endgroup$ – Adam Nieman Nov 26 '14 at 1:36
  • $\begingroup$ @halirutan that is the comment I was looking for :) My point was that OP is clearly new and not aware of the basic concepts of Dynamic. So is pure code an educational answer then? $\endgroup$ – Kuba Nov 26 '14 at 8:49
  • $\begingroup$ @Kuba Better now? $\endgroup$ – halirutan Dec 4 '14 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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