# Making a “cached” version of Manipulate[]?

I have a function that is a bit too slow for Manipulate[]. Right now I plan to cache outputs outside the manipulation and just have the manipulator select among cached outputs, but I am wondering if some better version of that trick exists.

Basically, I am creating a CDF presentation in which I intend to show the effect of different values for a correlation parameter $\rho$ in Monte Carlo simulations to the audience using Manipulate[] to show the resulting sample histograms. The simulation process is too slow to make for a good user experience as a live function, so one way or another I will have to cache some things.

It seems that if I am clever about writing a Module as an argument to Manipulate I should be able to make it cache, say, 199 different output sets corresponding to the various % correlations between -0.99 and +0.99.

-
How many parameters do you adjust with Manipulate? If it's only one, the minimal effort solution might be just precomputing a number of frames to show and using ListAnimate. – Szabolcs Mar 12 '12 at 18:35
Also, with Manipulate it's possible not to update the output before releasing the slider, or updating the output only with "low quality" (actually custom output can be used) while dragging the slider. This lets you give instant feedback when dragging the slider and a precise result will appear a second or two after you release the slider. If the simulation is even slower than 1-2 seconds then you really need to cache something. How the caching is best implemented depends on the details of the problem. The most general solution (e.g. ListAnimate) is not necessarily the best. – Szabolcs Mar 12 '12 at 18:39
Risking one more comment, one typical way of caching in Mathematica is memoization, in case you didn't know about it. There lots of variations on the technique. E.g. the number of values remembered can be limited. However, it seems to me that precomputing values is more appropriate for your case than caching them. – Szabolcs Mar 12 '12 at 18:48
I am planning on only one parameter. I did indeed use the ControlActive to get low-res behavior (as recommended in the "Advanced Tutorial") but as you anticipate it was too slow. ListAnimate is an interesting idea but I rather like having variable values on the scrubber. – Brian B Mar 12 '12 at 20:30

The following code demonstrates a bit of caching. The initialization code is run only once, because the variable cached which is saved in the Manipulate remembers the cached state. So when you copy the Manipulate into an empty notebook and open this in a fresh session, the cached plots stored in the saved variable plot (stored because of the SaveDefinitions option) won't be calculated again.

Remove the cached = True line and insert a Print["Init"] before the Plot command to see that plot is recalculated when the notebook containing the Manipulate is opened and viewed in a fresh session (without explicitly executing any code).

DynamicModule[{cached, plot},
Manipulate[
plot[[kx, ky]],
{kx, 1, 10, 1},
{ky, 1, 10, 1},
Initialization :> {
If[\[Not] TrueQ[cached],
plot =
Table[Plot3D[
Sin[kx x] Cos[ky y], {x, 0, \[Pi]}, {y, 0, \[Pi]}], {kx, 10}, {ky, 10}];
cached = True
]},
SaveDefinitions -> True,
SynchronousInitialization -> False
]
]


-
Exactly the sort of idea I was looking for, thank you. I'll be interested to see if the saving works in CDF, which I plan to test today. – Brian B Mar 13 '12 at 15:24
As an aside, this works in CDF player (after clicking past a warning) but not in embedded web pages. I presume this is due to the security restrictions on general functions like DynamicModule. – Brian B Mar 14 '12 at 13:37
@BrianB Good to hear that. Perhaps you could try to work without DynamicModule it isn't really essential here. I used it to restrict the effects of SaveDefinition to localized names, but if you are careful doing without shouldn't cause problems. – Sjoerd C. de Vries Mar 14 '12 at 14:39
Just wondering.. Whats done here is a kinda brute force (elegant) approach (which works). Is there an inbuilt feature to do this? (I am expecting that such "inbuilt" feature could be advantageous over this approach) – my account_ram Aug 12 '13 at 14:11
@myaccount_ram If I had known of such a method I would have provided it, of course. – Sjoerd C. de Vries Aug 12 '13 at 15:30