Using Dynamic to track progress within features

I am interested in tracking the progress of a calculation using Dynamic. This can be done interactively as follows:

In[3]:= Dynamic[iter] In[4]:= Table[{iter, iter^2}, {iter, 1, 500000}]; 

However, if the table is inside a function such as

 f[m_] := Module[{iter}, Table[{iter, iter^2}, {iter, 1, m}]]; 

how can i track the iter value when i execute the function through

 f[500000]; 

?

+6
source share
3 answers

Not sure if this is good advice, but:

 f[m_] := Module[{iter}, getIter[] := iter; Table[{iter, iter^2}, {iter, 1, m}]]; 

And then:

 Dynamic[getIter[]] f[500000]; 

EDIT

This will be better, but somewhat more obscure:

 ClearAll[f]; SetAttributes[f, HoldRest]; f[m_, monitorSymbol_: monitor] := Block[{monitorSymbol}, Module[{iter}, monitorSymbol := iter; Table[{iter, iter^2}, {iter, 1, m}]] ]; 

Here you assign a specific character to monitor the state of your localized variable. Using Block , you guarantee that your symbol will not receive any global value at the end (more precisely, its global value will not be changed at the end - you can also use a symbol that has some kind of global value if you so wish). The default character is monitor , but you can change it. Here's how you use it:

 Dynamic[monitor] f[500000]; 

This is a slightly better offer than the first, simpler one, because with the help of Block you guarantee that after the function is completed no state changes will occur.

+4
source

If you want to use the ProgressIndicator , you can do something like this:

 (*version 1.0*) Manipulate[ msg = "busy..."; result = process[ Unevaluated@progressCounter , max]; msg = "done!"; result, Dynamic@Grid [{ {ProgressIndicator[progressCounter, {0, 100}, ImageSize -> {105, 23}, ImageMargins -> 0, BaselinePosition -> Bottom], Spacer[5], progressCounter, " %"}, {msg} } ], {{max, 100, "maximum"}, 10, 10000, 10, Appearance -> "Labeled", ContinuousAction -> False}, {{progressCounter, 0}, None}, {{result, 0}, None}, SynchronousUpdating -> False, TrackedSymbols :> {max}, Initialization :> ( process[c_, max_] := Module[{i, tbl}, c = 0.; tbl = Table[0, {max}]; Do[ tbl[[i]] = {i, i^2}; Pause[0.00001]; c = i/max*100., {i, 1, max} ]; c = 0.; tbl[[-1]] ] ) ] 

enter image description here

+2
source

To track the expression, you can try using Monitor :

 Monitor[ t = Table[{i, i^2}, {i, 500000}]; Last[t] , i ] 

Alternatively, you can use a ProgressIndicator with a range on i :

 Monitor[ t = Table[{i, i^2}, {i, 500000}]; Last[t] , ProgressIndicator[i, {1, 500000}] ] 
+2
source

Source: https://habr.com/ru/post/905421/


All Articles