4
$\begingroup$

Why can't I add to the dynamic variable z below?

z = 1;
Dynamic[z] + 1

returns

1+1

and not 2 as expected?

Questions:

  1. Using the Head, I could see that the expression Dynamic[z] is not Integer as I expected, but is Dynamic. I was hoping that Mathematica would automatically take care of this, changing the Head as required. But apparently it does not. Why?

  2. How to get the expected behavior? (I could achieve it using Dynamic[z+1]. But I'd like to see how to "convert" Dynamic[z] to Integer.)

$\endgroup$

1 Answer 1

7
$\begingroup$

Dynamic[something] is a spot on the screen, which gets updated when its content is updated.

There is really no dynamic variable in the traditional sense. Better to think of Dynamic[something] as just a Dynamic spot or location on the screen at same place where Dynamic is located.

Mathematica will update this spot on the screen when it detects something has changed. i.e. if any variable or the expression inside the Dynamic has changed due to changes elsewhere in the notebook. That is all.

To get what you want do

z = 1;
Dynamic[z + 1]

(*2*)

Now scroll down a little, and type z=9, then you'll notice that earlier spot on the screen now will change to 10 and not to 11.

If you cover that spot by another window, then Mathematica will no longer update that spot. It will only update that spot if there is nothing in front of the notebook.

I could achieve it using Dynamic[z+1]

Yes, that is the right approach.

You do not get 2 when you do Dynamic[z] + 1 because there is no algebra with Dynamic. You are trying to add 1 to a spot on the screen. So Mathematica just returns 1+1. Dynamic is like a wrapper here. You can't add to it.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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