Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to make a simple cdf file that makes some hard computations inside Manipulate. Is there a simple way, lets say using a "stop!" Button, to terminate the time consuming calculations--without closing the web browser or Mathematica? To clarify my self: The following code works well because 2014 is not large enough, so Mathematica can easily compute the sum of Logs.

Manipulate[tick; tot = 1. Total[Log@Range[2014]]; color = Green;, tot, Button["Run", color = Red; tick = Not[tick], Background -> Dynamic@color], {{color, Green}, None}, {{tot, 0}, None}, {{tick, False}, None}, TrackedSymbols :> {tick}]

but when I introduce range at the place of 2014 as in :

Manipulate[tick;
 tot = 1. Total[Log@Range[range]];
 color = Green;
 tot, Button["Run", color = Red; tick = Not[tick], 
  Background -> Dynamic@color], {{color, Green}, None}, {{tot, 0}, 
  None}, {{tick, False}, None}, {{range, 1}, 1, 300000000, 1}, 
 TrackedSymbols :> {tick}]

the things are coming very bad for huge values of range! So my question is : Is it possible to terminate a running calculation from inside Manipulate?

share|improve this question
    
whyare you multiplying by one here 1. Total[Log@Range[range]] what does 1 do ? –  Nasser Feb 10 at 12:43
    
@Nasser 1 nothing but 1. works like N. –  Kuba Feb 10 at 12:46
    
@Kuba I see. I wonder if this supposed to be faster/better than using N@ ? –  Nasser Feb 10 at 12:47
    
@kornaros maybe it will be ok to use MemoryConstrained or something similar. Take a look at See Also part of the documentation for this. –  Kuba Feb 10 at 13:05

1 Answer 1

A good old fashioned way of doing it. Set a flag using another button, and check for the flag while doing the computation. (btw, I tried calling Abort[] and Interrupt[] when the second button is hit, but for some reason that did not stop the computation running in Manipulate. That is why I used this method below, which always works)

Manipulate[tick;

 Module[{r, i = 1},
  r = Range[range];
  tot = Total@N@Flatten@Rest@Reap[While[ok && i <= Length[r], Sow[Log[r[[i++]]]]]];
  color = Green;
  tot
  ],

 Row[{Button["Run", color = Red; ok = True; tick = Not[tick], 
   Background -> Dynamic@color, ImageSize -> {80, 40}],
   Button["Stop", ok = False; tick = Not[tick], ImageSize -> {80, 40}]
   }],
 {{range, 1, "range"}, 1, 3000000, 1, Appearance -> "Labeled", ImageSize -> Small},

 {{color, Green}, None},
 {{tot, 0}, None},
 {{tick, False}, None},
 {{ok, True}, None},
 TrackedSymbols :> {tick}

 ]

enter image description here

And if you want to see where the computation is at any time, you can use a counter (this is for illustration only. In real application, you might want to go easy on this as it will take time from the actual computation

Manipulate[tick;

 Module[{r},
  r = Range[range];
  tot = Total@N@Flatten@Rest@Reap[While[ok && i < Length[r], Sow[Log[r[[++i]]]]]];
  color = Green;
  tot
  ],

 Grid[{
   {Dynamic@Row[{"busy at item ", i}], SpanFromLeft},
   {Button["Run", color = Red; ok = True; i = 0; tick = Not[tick], 
    Background -> Dynamic@color, ImageSize -> {80, 40}],
    Button["Stop", ok = False; tick = Not[tick], ImageSize -> {80, 40}]
    }}, Spacings -> {1, 1}],

 {{range, 1, "range"}, 1, 30000000, 1, Appearance -> "Labeled", ImageSize -> Small},

 {{color, Green}, None},
 {{tot, 0}, None},
 {{tick, False}, None},
 {{ok, True}, None},
 {{i, 0}, None},
 TrackedSymbols :> {tick}     
 ]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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