7

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?

rhermans
  • 36,518
  • 4
  • 57
  • 149
Yrefef
  • 71
  • 1
  • 2
    Look for DumpSave in the documentation. – Ulrich Neumann May 18 '22 at 10:27
  • 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. – rhermans May 18 '22 at 10:57
  • 3
    You can use PersistentSymbol["symbol", "Notebook"] to create a variable that gets saved into a notebook to make it persist between sessions. – Sjoerd Smit May 18 '22 at 11:07
  • @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? – rhermans May 18 '22 at 12:19
  • @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. – Sjoerd Smit May 18 '22 at 14:28
  • @SjoerdSmit evaluating PersistentSymbol["symbol", "Notebook"] in Wolfram Cloud give the error PersistenceLocation::feopt "PersistenceLocation :Subtype NotebookPersistence of persistence location FrontEnd is not supported". Also with any of the other "Supported location types". {"KernelSession", "FrontEndSession", "Notebook", "ServerSession", "ServerSession", "CookieManaged", "Local", "LocalShared", "Cloud", "Installation"} – rhermans May 18 '22 at 14:31
  • @rhermans In the Cloud you can use PersistentSymbol["symbol","Cloud"] instead. – Sjoerd Smit May 18 '22 at 14:36

1 Answers1

8

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

rhermans
  • 36,518
  • 4
  • 57
  • 149