Group Abstract Group Abstract

Message Boards Message Boards

Why operation on point doesn't work inside DynamicModule

Posted 3 years ago

I have the following code that shows how the coordinates of the point p1 changes when the user moves it around.

I am trying to see how to point is represented as a complex number z1. But I can't figure out why it doesn't work - the value of z1 stays at 1+2i. I want z1 value to be updated according to the value of p1.

Any idea?

DynamicModule[
 {p1 = {1, 2}},
 z1 = Complex @@ p1;
 {Graphics[
   Locator[Dynamic[p1]]
   ]
  ,
  Dynamic@p1
  ,
  Dynamic[z1]
  }
 ]

See image

enter image description here

POSTED BY: Ehud Behar
2 Replies
Posted 3 years ago

Let's start with some info about Dynamic. Dynamic is just a wrapper. It tells the front end to refresh the display of some expression whenever the variables that that expression depend on change. So, Dynamic[z1] will only ever get its display refreshed if z1 gets changed (z1 is the only variable in the expression). But in your example, z1 never gets changed. That leads to...

Using Set (e.g. z1=Complex@@p1) is defining the OwnValues for a symbol. It's not defining some procedure to be executed in the future. Once the Set is executed, it's finished and is never consulted again (well, unless you re-execute that expression). So, after z1 is defined with Set, any later usage of z1 is replaced based on z1's OwnValues.

The simplest way to get what I assume you want would probably be to replace Dynamic[z1] with Dynamic[z1 = Complex @@ p1]. There are other ways that might be better for your context, but that's what came to my mind first. That's assuming that you actually need the variable z1. If not, then you could simplify it even more to just Dynamic[Complex @@ p1]

POSTED BY: Eric Rimbey
Posted 3 years ago

Thank you once again for such a reply full of important details. The information you provide here is invaluable.

Dynamic[z1 = Complex @@ p1] and Dynamic[Complex @@ p1] both work. Excellent.

Yes, I am about to do some manipulations on the complex point z1.

I will read more about down values here.

POSTED BY: Ehud Behar
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard