# How to evaluate Dynamic expression? [duplicate]

I am trying to work my way through Dynamic but there is something I dont get and I would appreciate very much some clarification. Consider the following code

Manipulate[
var1 = Dynamic@par1;
im1 = Graphics[Circle[], Axes -> True];
locd = Dynamic[locator];
pointOK = Dynamic[locator + {-1, 1}];
pointWRONG = locd + {-1, 1};
Grid[{
{"[1.1]", par1 + 2}
, {"[1.2]", var1 + 2}
, {"[1.3]", Evaluate[var1 + 2]}
, {"[1.4]", ReleaseHold[var1 + 2]}
, {"[2.1]", LocatorPane[locd, im1]}
, {"[2.2]", locd}
, {"[2.3]", pointOK}
, {"[2.4]", pointWRONG}
, {"[2.5]", Graphics[{
PointSize -> Large
, Blue, Point@pointOK
, Red, Point@pointWRONG
}, PlotRange -> 2, Axes -> True]}
}
, Alignment -> Left
]
, {par1, 0, 1}
, {{locator, {0, 0}}, None}
]


For different reasons I would like to have a manipulable parameter par1 and perform some alebraic operations on variables defined on it, like in [1.1] in the picture above. But, if, just out of fun, I declare a symbol var1 = Dynamic@par1 and try to perform some algebra on it, like in [1.2], it does not get evaluated. I read that Dynamic has HoldFirst and I thought that Evaluate would help me, as I tried in [1.3], but without luck. The same for ReleaseHOld, [1.4].

The weird thing for me is now that Point (and surely other functions) is able to evaluate this kind of expressions. Consider the Locator in [2.1] based on the locator control and the symbol locd = Dynamic[locator] now. If I want to some algebra on the momentary values of locator, I have to do it like in the symbol pointOK, [2.3], and NOT as in symbol pointWRONG, [2.4], since the Dynamic object is "added" to each element of the list {-1,1}. Nevertheless, in the last graphic, Point is able to process pointWRONG, otherwise you would not see the 2 red points.

So, what property has Point or what do I need to evaluate expressions containing Dynamic objects (such that, e.g., I get the same in [1.1] and [1.2])? Thank you!

EDIT: Compared to the question Programming with Dynamic, I think this question deals more with the question on how to evaluate an expression containing a Dynamic object (if Dynamic could not be evaluated numerically, then Point would not be able to display anything, but you can see the points in my plot). In Programming with Dynamic I had the feeling that it was important to understand that the head of Dynamic[x] is Dynamic. But some evaluation has to be possible even if objects like Integer and Dynamic, again, otherwise Graphics@Point@pointWRONG would not work.

## marked as duplicate by Kuba♦, MarcoB, Szabolcs, Feyre, m_goldbergJan 19 '17 at 1:10

• Dynamic doesn't evaluate. And when displayed Dynamic[x]+1 you will just see 1+1 (if x=1). And Graphics@Point@Dynamic@x doesn't evaluate Dynamic either, it is the front end that knows the Point @ Dynamic @ x should be rendered as a point with coordinates given by x. – Kuba Jan 18 '17 at 14:54
• @Kuba : Ou... so it is a special property of Point (and surely other built in functions) to identify Dynamic and evaluate it internally? (see my edit) – Mauricio Fernández Jan 18 '17 at 14:57
• No, InputForm@ToBoxes@Graphics@Point@Dynamic@x take a look. When written to notebook there is no Point, just Boxes. The FrontEnd cares about rendering them. E.g. that FrameBox is a rectangle with a frame. So it cares about Dynamic it know it has to know its current value for display purposes, and if in GraphicsBox it is interpreted accordingly, but in no case Dynamic evaluates. The FrontEnd just takes the content of Dynamic and evaluates it but as a "separate" evaluation which is a part of rendering process, not the evaluation of the original expression. – Kuba Jan 18 '17 at 15:03
• @Kuba hmmmm .... ok, I think I get the point, but I have to digest it. Thanks! Should I delete the question? – Mauricio Fernández Jan 18 '17 at 15:09
• I don't know, maybe it is not a duplicate at the end. And in each case it should stay as a separate question or marked duplicate, for future readers. – Kuba Jan 18 '17 at 15:09

If for some reason you need to have a dynamic object that does some calculations, and what you want is to get to the value of the actual expression defined as a function of a dynamically changing parameter (par1 in your case), than you can simply do that by extracting that expression from a variable defined with the Dynamic head.

var1 = Dynamic[par1 + 1];
Slider[Dynamic@par1, {0, 10, 1}]
Dynamic[var1 + 3]
Dynamic[var1[[1]] + 3]


The first object is not evaluated (it still has a Dynamic head, so it can't be added to the value of 3, but notice that 1 was added to par1 inside Dynamic when it was pushed to the FE, so it is displayed as 2), but the second one is evaluated once we remove the Dynamic wrapper.

In addition, as Kuba suggested, an alternative to Partis Setting. Both extract the first argument, which is important if the Dynamichas multiple arguments, since the expression you are interested in is in the first one.

• @Kuba Agree, Evaluate is not particularly interesting. I will update. ReleaseHold would still be needed if you have a function with Hold, isn't it? Anyways, the point I was trying to communicate is that you can replace the Dynamic with anything needed to properly evaluate the expression inside Dynamic. – Stitch Jan 18 '17 at 18:14
• Sorry for being picky. I just want to avoid confusion for new users, I remember how I was once overwhelmed by all this stuff. – Kuba Jan 18 '17 at 18:47
• @Kuba Not at all! Thanks for your comments! I agree it might be confusing, you have an excellent example. – Stitch Jan 18 '17 at 18:49
• @Kuba : guys, thank you very much for the discussion and explanations, it helps to understand a lot. A lot I have to learn as a young padawan :D – Mauricio Fernández Jan 18 '17 at 23:18
• @MauricioLobos ,Kuba Thank you guys! I am learning myself from our jedis :) – Stitch Jan 19 '17 at 1:33