I wrote a module which numerically solves an ODE using NDSolve. The result from this is e.g. either InterpolatingFunction[{{0.,1000.}},<>][t] or InterpolatingFunction[{{0.,1000.}},<>][t$1062], depending whether the independent varialbe t is global or local in the module.

So far, I didn't care that it is global and everything worked fine. As this is imho is a source of upcoming trouble, I would like to switch to the local variable-version. As therefore I can't use a replacement rule like InterpolatingFunction[{{0.,1000.}},<>][t] /. t->3 to extract single values or to plot the function any more, I tried to wrap it into a function as the following minimal example demonstrates.

compute[] := Module[{res, f},
   points = Table[{x, x}, {x, 0, 10}];
   res = Interpolation[points];
   f[x_] := res[x];

Using this code works fine.

In[109]:= ex = compute[];
Out[110]= 3

I now want to run this computation with different parameters so I put it into a ParallelTable. The resulting list then does not contain the correct functions anymore but only their symbols.

In[111]:= a = ParallelTable[compute[], {i, 0, 100}];

Out[112]= f$1870[3]

Note that this works perfectly when ParallelTable is replace by a normal Table.

Can you explain to me why this happens? Has this something to do with the definitions of the functions "living" on the sub-kernels and not being passed back to the main kernel?


Another aspect of the problem, a solution to which would help me too, is the following. If I multiply the InterpolatingFunction by a number and try to then extract values from it, it doesn't work as I would expect. Using the above example and assuming tha res is returned (indicated by the use of res in the following).

In[150]:= fcn=5*res;

Out[151]= (5 InterpolatingFunction[{{0,10}},<>])[3]

Is there maybe a workaround for this?

  • $\begingroup$ Yes, it does. The symbol f$1870 has no definition associated with it in the master kernel. You could avoid this problem if compute[] would return res rather than f. $\endgroup$
    – Sasha
    Nov 15, 2012 at 21:24
  • $\begingroup$ But then I'm stuck with the problem of using the result. If t is global, I can use the replacement rule. If not, I don't see another way to use it. Do you know if there is a way to copy the definition to the master kernel? $\endgroup$
    – mincos
    Nov 15, 2012 at 22:42
  • $\begingroup$ Are you aware that NDSolve[] can be made to output a pure function, which would seem to be more convenient for your applications? Witness, for instance, f = y /. First @ NDSolve[{y''[x] == -y[x], y[0] == 1, y'[0] == 0}, y, {x, 0, 3}] $\endgroup$ Nov 16, 2012 at 9:30
  • $\begingroup$ @J.M. That's what I tried to describe in the edit of the question. Using your solution has (at least at my machine) the problem that I can't modify the function by multiplying something, e.g. a = 3*f; a[3] won't return a number because it doesn't evaluate the InterpolatingFunction then. $\endgroup$
    – mincos
    Nov 16, 2012 at 12:08
  • 1
    $\begingroup$ That works like a charm. Thank you! PS: If you put your comment in an answer, I could mark it as solved. $\endgroup$
    – mincos
    Nov 16, 2012 at 13:33


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.