7
$\begingroup$

I am performing a very time-consuming symbolic calculation. When I reopen a saved Mathematica notebook, it takes quite a long time to evaluate.

Is there an option to tell Mathematica to remember the result of each evaluation so if the next time Mathematica is asked to calculate the same thing and parameters have not changed it won't perform calculation but take the result from memory?

$\endgroup$
7
  • 2
    $\begingroup$ Look for DumpSave in the documentation. $\endgroup$ May 18 at 10:27
  • $\begingroup$ Welcome! To make the most of Mma.SE start by taking the tour now. It will help us to help you if you write an excellent question. Edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. As you receive give back, vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$
    – rhermans
    May 18 at 10:57
  • 3
    $\begingroup$ You can use PersistentSymbol["symbol", "Notebook"] to create a variable that gets saved into a notebook to make it persist between sessions. $\endgroup$ May 18 at 11:07
  • $\begingroup$ @SjoerdSmit unfortunately, PersistentSymbol["symbol", "Notebook"] is not available in Wolfram Cloud or Wolfram Engine for me to test a complete solution based on that. Do you mind giving an example of how to use it to store the definitions of memoized functions? $\endgroup$
    – rhermans
    May 18 at 12:19
  • $\begingroup$ @rhermans PersistentSymbol is just a variable. If you want to memoize function evaluations, you should use Once with the desired persistance location specified. Also take a look at PersistenceLocation to see what options you have for storing the values. $\endgroup$ May 18 at 14:28

1 Answer 1

8
$\begingroup$

Strategy: Use memoization and then save the relevant definition in a file at the end of your session. Next session you can recover the "memoized" definition.

Memoization

Look at memoization also on other questions, particularly 2639. You can do this

f[x_]:=f[x]=ExpensiveFunctionOf[x]

You can then call f[x] and ExpensiveFunctionOf is evaluated only if the parameter x is different from previous evaluations. Otherwise, Mathematica will remember the output value from previous evaluations.

For example, notice how the first time f[10] is evaluated it takes 5 seconds (Pause[5]) but the second time is instantaneous because the definition of f now includes a specific entry for f[10] with the output of ExpensiveFunctionOf[10] as DownValues.

ClearAll[f,ExpensiveFunctionOf];
ExpensiveFunctionOf[x_]:=(Pause[5];Factorial[Round[x]])

f[x_]:=f[x]=ExpensiveFunctionOf[x]

AbsoluteTiming[f[10]]
(* {5.00058,3628800} *)

AbsoluteTiming[f[10]]
(* {0.00002,3628800} *)

?f

enter image description here

DumpSave

Then, as suggested by @UlrichNeumann, you can save those definitions using DumpSave and recover them using Get

DumpSave["f.mx", f]

Get

Get["f.mx"]

enter image description here

$\endgroup$
3
  • $\begingroup$ Why don't you make the title of the question a bit shorter and definitely colder? :) $\endgroup$
    – Syed
    May 18 at 14:38
  • $\begingroup$ @Syed You ask me or the author of the question? Also, feel free to edit the question yourself. $\endgroup$
    – rhermans
    May 18 at 14:56
  • $\begingroup$ You of course since you have answered it well. $\endgroup$
    – Syed
    May 18 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.