2 edited title
| link

Save part of Memoizedmemoized function

1
source | link

Save part of Memoized function

I have a function whose performing a series of expansions of a complicated function, which can take a lot of time. To avoid bottlenecks, I use memoization so I need to evaluate it only once.

For completness let's say it is simply

f[a_, trunc_] := f[a, trunc] = Series[Sin[a*x], {x, 0, trunc}] // Normal;

In my script I will need several expansions for different a at a given trunc (defined at the beginning of the script and not modified until the end). As evaluating f can take more than a minute and I need order 50 values of a, I usually place a

Save["savedExpansion.m", f]; 

at the end of the script so that next time I can directly load it with Get and same myself some time.

Now, I want to create a script that basically doing only the initialisation of f for both the values of a and trunc I will ever need so that I have everything that takes time to compute once and for all. I wrote the following snippet of code

listA = Table[a, {a, 1, 10}];
listTrunc = Table[trunc, {trunc, 1, 10}];

Do[
    ParallelDo[Print["Going over ",a];f[a, trunc];, {a, listA}];
    Save["savedExpansionTrunc" <> ToString[trunc] <> ".m", f];
, {trunc, listTrunc}];

The thing is that each created file contains the values for f of all the previous values trunc, which I want to avoid to save memory. I there a way of saving only the values of f[a,trunc] for a given trunc? Moreover, is it possible to clear the values from memory with Clear without clearing the definition of the function at the same time?

By the way, is there a best practice for these kinds of things, e.g. using Save rather than Dump, or using parallelisation?