6

My calculations are something like this:

f[x_]:= (*runs fast*)
g[x_]:=(*runs fast*)
h[x_]=depends on f[x],g[x] (*runs slow ~5mins*)

Now I need only the results of h[x] and every time I restart mma, I essentially redo the same calculations to get h[x], when it could have been stored. Is there some way I can save the symbolic results, and just import it anytime I need it?

  • Have you tried my solution? Does it work in your application? Loading with `Get` should *not* require the time it took to generate `h[x_]` in the first place. – Mr.Wizard May 09 '11 at 15:45
  • 4
    Related: ["The best way to construct a function with memory."](http://stackoverflow.com/questions/5287817/the-best-way-to-construct-a-function-with-memory/5291299#5291299) – Alexey Popkov May 09 '11 at 17:17
  • @Alexy: Thanks a lot! That question was helpful. –  May 12 '11 at 15:07

1 Answers1

5

Try Save

Use like:

Save["h-defs.m", h]

And load with Get:

<< "h-defs.m"
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • 2
    I suggest using `DumpSave["h-defs.mx", h]` instead, it is much much faster for large data. But it is platform-dependent. I.e. the 64 bit version won't load something saved by the 32-bit version. – Szabolcs May 09 '11 at 15:22
  • `Get`'s documentation says that it _"reads in a file, evaluating each expression in it, and returning the last one"_. I don't want it to evaluate each time I load it. –  May 09 '11 at 15:29
  • You were not clear about *what* takes 5 minutes to evaluate (certainly not the definition of the function since it was with :=). Do the computation, store the result into a variable, then save this variable with `(Dump)Save`. Alternatively use memoization with `h` and save `h` after doing the computation. – Szabolcs May 09 '11 at 15:33
  • You can also use use `DumpSave`, but I think using `Save` is better for a beginner as you can read the .m file to see what it contains. – Mr.Wizard May 09 '11 at 15:35
  • @d'o-o'b as Szabolcs describes, I am assuming you are creating a series of DownValues for `h`, via memoization or otherwise. When you use `Get` (`<<`) it evaluates the expression that sets these DownValues, which is what you want. – Mr.Wizard May 09 '11 at 15:38
  • @Mr. Wizard: Sorry about the `:=`. I just copied from the lines above. I do use `=`, which is why the evaluation takes time. –  May 09 '11 at 15:46
  • @d'o-o'b I understand that, and my solution should work. Please try it, and report any problems. – Mr.Wizard May 09 '11 at 15:48
  • @Mr. Wizard: Your solution does work and loads immediately; however, variables with subscripts do not get saved this way. Do you know of a workaround for this? I've also symbolized `Subscriptbox["_","_"]`, so I don't really know why it won't save. –  May 09 '11 at 16:22
  • @d'o-o'b there are several ways to use subscripts, if I recall correctly. Would you mind posting a small example of your use? – Mr.Wizard May 09 '11 at 16:29
  • @Mr. Wizard: I was wrong. It actually does save the variables with subscripts. I was confused because it didn't show anything when I used `?variable`, but `Definition@variable` worked. Accepting your answer. –  May 09 '11 at 16:44
  • @d'o-o'b thanks for the accept. If you wonder what is being saved, you can view the .m file in a text editor or Mathematica. That is why is intentionally recommended `Save` over `DumpSave`. (Also, the fact that `DumpSave` is machine dependent and therefore if you think you have saved your code safely, and delete the original, you will be sorry later.) – Mr.Wizard May 09 '11 at 16:52