Message Boards Message Boards

5 Replies
7 Total Likes
View groups...
Share this post:

Using the Monitor function

Hi everyone,

I'm going to run some long simulations, so I want to use the Monitor function to keep track of my iterators and see the progress of my simulation.
In the documentation centre I find the following example:
Monitor[i = 0; While[i < 100, Pause[0.1]; i++]; i, i]
When I evaluate it, the value of i is printed, and I see it count up to 100. 
I then copy this function into my notebook, but when I evaluate it again (without any modifications) I just see the letter "i", and only at the end of the evaluation it changes to "100".
I have browsed through the property editor to see if it could be due to some setting, but I cannot figure out what it is. Could somebody please help me?

Thank you very much!


POSTED BY: Andreas Hafver
5 Replies

I'm assuming you disagree with what I wrote, since you mention Context.  I think you misunderstood.

Copying the code works fine, as I said; there I think we agree.  However, copying the cell (by selecting the cell bracket) does not.   The behavior when the whole cell is copied is obvious. This shot was taken while it's running. Note that i has not been updated and the contexts are different.  (The whole point of adding the use of Context was to show this difference.)

FWIW, here is full expression of the cell I think the OP copied (obtained from the menu command Cell > Show Expression):
  RowBox[{"Monitor", "[",
     RowBox[{"i", "=", "0"}], ";",
     RowBox[{"While", "[",
       RowBox[{"i", "<", "100"}], ",",
       RowBox[{"Pause", "[", "0.1", "]"}], ";",
       RowBox[{"i", "++"}]}]}], "]"}], ";", "i"}], ",", "i"}], "]"}]], "Input",
GeneratedCellStyles->{"PrintTemporary"->{"PrintTemporary", CellContext -> CellGroup}},

Copying this cell copies the option setting for CellContext that causes the i being monitored to be different than the i in the While loop. 
POSTED BY: Michael Rogers
Andreas, maybe this can help. It's another form of monitor evaluation that works with using ParallelMap too.
Here is the link to SE.
POSTED BY: Rodrigo Murta
I do not agree
The code 
Monitor[i = 0; While[i < 100, Pause[0.1]; i++]; i, i]
is evaluated as intended in a freshly opened notebook using a fresh kernel. It has nothing to do with the specific settings of the documentation. Documentations have their own settings so they woul not effect your existing evaluations.  Using Context is up to you if you want to, again this is not a must, as I nothed the Monitor should and do run correctly

It's a bit hard for me to explain, but it has to do with Contexts.  The documentation pages have their own context.  You can evaluate expressions in them, and you won't affect your own definitions in your other open notebooks.  You must have copied the whole input cell from the documentation.  Just copy the input code text and execute it.  The input cell from the documentation page has the following option set:
GeneratedCellStyles->{"PrintTemporary"->{"PrintTemporary", CellContext -> CellGroup}},
The PrintTemporary cell that is created by Monitor has a different default context, and the symbol i in the PrintTemporary cell is not the same as the symbol i being monitored.  Therefore the PrintTemporary cell i is never changed during evaluation and all you see is i.  At the end of the evaluation, the PrintTemporary cell is destroyed and an Output cell is created.  There is nothing special about the output cell and it shows the result of the computation as expected.

If you edit the text of the cell you pasted from the documentation page to read as below, you will see the different contexts while the computation is running.
Monitor[i = 0;Print[Context[i]]; While[i < 100, Pause[0.1]; i++]; i^2, {Context[i],i}]
POSTED BY: Michael Rogers
Thank you, now I learnt something new!
I also got my program to work.

POSTED BY: Andreas Hafver
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract