# Remembering function definitions across sessions to avoid recalculation [duplicate]

I'm working with some large expressions and fourier transforms, which usually take a long time to compute. When I save my notebook with outputs to all my inputs, I'd like it to remember those definitions so when I open Mathematica the next time, I won't have to recalculate everything. Is there a way other than the workaround of rewriting the inputs to directly equal the outputs as suggested below?

In[21]:= fring[u_, v_] = FourierTransform[ring[x, y], {x, y}, {u, v}]

Out[21]= BesselJ[0, Sqrt[u^2 + v^2]]/(4 \[Pi])

In[11]:= fring[u_, v_] = BesselJ[0, Sqrt[u^2 + v^2]]/(4 \[Pi])

Out[11]= BesselJ[0, Sqrt[u^2 + v^2]]/(4 \[Pi])
$$$$

• may be use Save or DumpSave ? These are the two command I know about for such a thing. I do not know if they will work for what you are trying to do. You can try and see,. Oct 11 at 10:04
• To the people voting to close, it would be nice to have a complete answer that combines memorization and DumpSave and a clever file name with dates, so yesterday's latest status can be recovered easily. I see an opportunity in this question. I wouldn't close it too fast. Oct 11 at 10:42
• There is one simple way to do what you need. But it requires some attention. In the end of your notebook create a special Section. Let us name it "Initialization." Under this Section make an input cell and set it as the InitializationCell. That is, make a usual input cell, select its right bracket, go to Menu/Cell/CellProperties/InitializationCell. The cell will become light gray. Done. In the course of your current session, as soon as you get an expression that you may want to use in the future as a result of some lengthy calculation, do the following. Oct 11 at 11:37
• Continuation: Give this expression a new name and write its definition into the Initialization Cell. Using your example, it may be fringRes[u_, v_] := BesselJ[0, Sqrt[u^2 + v^2]]/(4 \[Pi]); . It is important that this way you memorize the results, rather than the original expressions. Like this you do not need to recalculate your expressions. Do the same with all other expressions that you will need. Oct 11 at 11:42
• @rhermans It seems to me that the two questions are essentially the same. I agree with you that it would be nice to have a pre-packaged solution, but I think it would be better to have all solutions in a single place. IMO it would be better to have this closed as a duplicate and if you have a solution, post it in the other thread. Do you agree? Oct 11 at 12:23

There is one simple way to do what you need that I use regularly. But it requires some attention.

1. At the end of your notebook create a special Section. Let us name it "Initialization."

2. Under this Section make an input cell and set it as the InitializationCell. That is, make a usual input cell, select its right bracket, go to Menu/Cell/CellProperties/InitializationCell. The cell will become light gray. Done.

3. Suppose that in the course of your current session you came to an expression as a result of some lengthy calculation. Suppose, further, that you want to use in future. Then, do the following.

3a. Give this expression a new name and write its definition into the Initialization Cell. Using your example, it may be

fringRes[u_, v_] := BesselJ[0, Sqrt[u^2 + v^2]]/(4 \[Pi]);


It is important that this way you memorize the result of the calculation, rather than the original expression. Like this, you do not need to recalculate your expressions. Do the same with all other expressions that you will need.

3b. When you finish your session, first collapse the Section "Initialization" around its title. Like this, it will not take much place in your notebook.

3c. Then save the notebook in a standard way.

3d. The next time, after having open the notebook, go to Menu/Evaluation/EvaluateInitializationCells. Done. Mathematica now remembers your expressions.

1. This also works for lengthy arrays/lists obtained from some calculations. Here it will be a good practice to memorize each such array under its individual Sub...section. Otherwise, it will be difficult to navigate through this material. The second piece of advice is that if you have a really huge array (that can require a considerable memory) it may be better to keep it in a usual, rather than the initialization cell. Like this, you can only initialize it when you use it and then clear it as soon as you do not need it anymore. In cases of such lengthy arrays, it may also be a good idea to use a separate notebook to keep such data. Like this, your working notebook will not occupy too much memory. This will be good during performing calculations that are not using these huge arrays if any.

A minor comment: when defining functions in most cases it is better to use SetDelayed (:=), rather than Set (=).

I use Export and Import to save/retrieve data from disk:

(* define some data an a set (not delayed) function *)
myData = Table[n, {n, 1, 100}]
fun1[x_] = Prime[x]

(* save data and definition of function to disk*)
theFileName = "D:\\mathematica\\file1.m";
Print["     Saving data to  ", theFileName];
Export[theFileName, {myData, Definition[fun1]}];

(* retrieve the data and function *)
{dataFromDisk, theFun[x_]} = Import[theFileName]
theFun[25]
`

Now you can do as Alexei stated above and create an Initialization Group which is automatically executed when you start the notebook and include the Import code to retrieve the data. Not sure though if this is the best way to implement this method. Maybe others can comment