4

Is there any way to save and load several lists of interpolating functions efficiently and cross-platform?

I want to save the resulting interpolating functions of NDSolve which takes very long to generate so next time I can just load the same result into Mathematica. The following is an example of lists of long interpolating functions:

s1 = Table[
    NDSolve[{f''[x] == -k f[x], f[0] == 1, f'[0] == 0}, f, {x, 0, 100000}][[1]],
    {k, 1, 10}
  ];
fl1 = f /. s1; (* 1st list of functions *)
s2 = Table[
    NDSolve[{f''[x] == -k f[x], f[0] == 0, f'[0] == 1}, f, {x, 0, 100000}][[1]],
    {k, 1, 10}
  ];
fl2 = f /. s2; (* 2nd list of functions *)

The sizes of them are ByteCount[{fl1, fl2}] 1,363,723,536 Bytes (about 1.3 GB) in memory.

The fastest way I know is to DumpSave them using

DumpSave["data.mx", {fl1, fl2}];

This only takes a few seconds. The file size is also about 1.3 GB.

Loading them back also takes a few second:

Quit[]
Get["data.mx"]

However, DumpSave is not cross-platform. The saved .mx file can't be read by other computers with different architectures/system. (At least, it doesn't work between Mathematica 10.2 and 10.4.)

I have tried using Save:

Save["data2.mx", {fl1, fl2}]

but this is too slow and computationally intensive. The file size also appears to be much bigger.

So is there other ways to save & load interpolating functions with efficiency similar to DumpSave?

Louis Yang
  • 456
  • 2
  • 10

1 Answers1

4

While I was writing my question, I found a solution. I can use Export and Import.

Export["data3.mx", {fl1, fl2}, "MX"] // AbsoluteTiming
(* {13.1173, "data3.mx"} *)

which takes few second longer than DumpSave but with the same file size (1.3 GB). I can then Import them by

{fl1, fl2} = Import["data3.mx"];

(See also the last section of How do I save a variable or function definition to a file?)

The slight issue is that you need to know the order of each symbol and their names when you import them.

Also, using Get["data3.mx"] won't work in this case. Not sure why.

It will be helpful if someone know a way to save/load several lists of interpolating functions efficiently but without knowing their names while loading them.

Louis Yang
  • 456
  • 2
  • 10