1

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]) ```

  • 1
    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,. – Nasser Oct 11 '21 at 10:04
  • Also, it may be useful to take advantage of memoization, so functions don't calculate the same thing more than once. – rhermans Oct 11 '21 at 10:38
  • 2
    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. – rhermans Oct 11 '21 at 10:42
  • To @OndraJanoška, if the community considered that your question has already an answer, then your question may be closed as duplicate and therefore off-topic. Please [edit] your question if you consider this is a mistake and give great emphasis to what was NOT answered in the other question linked in the comments. Please don't be discouraged by that cleaning-up policy. Your questions are and will be most welcomed. Learn about good questions here. – rhermans Oct 11 '21 at 10:48
  • 2
    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. – Alexei Boulbitch Oct 11 '21 at 11:37
  • 1
    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. – Alexei Boulbitch Oct 11 '21 at 11:42
  • 1
    Continuation 2: When you finish your session, first collapse the Section "Initialization" around its title. Like this, it will not take much place in your notebook. Then save the notebook in a standard way. The next time, after having open the notebook, go to Menu/Evaluation/EvaluateInitializationCells. Done. Mathematica now remembers your expressions. A minor comment: when defining functions in most cases it is better to use SetDelayed (:=), rather than Set (=). – Alexei Boulbitch Oct 11 '21 at 11:46
  • 3
    @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? – Szabolcs Oct 11 '21 at 12:23
  • @rhermans If you are concerned that the other question won't get the necessary attention because it's already "solved", I am happy to use some rep score to open a bounty on it. Let me know. – Szabolcs Oct 11 '21 at 12:24

2 Answers2

0

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 (=).

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
0

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

josh
  • 2,352
  • 4
  • 17