Message Boards Message Boards

0
|
3584 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How can I gather the results of several computations of a notebook?

Posted 11 years ago

I have a mathematica notebook which generates some random numbers, performs a certain computation with them, and then displays a result (which is a variable in my notebook). I would like to be able to analyze the result over many different computations, so I need a way to programmatically tell the notebook to evaluate itself, then grab the variable, add it to a list, and repeat many times. What is the best way to accomplish this?

POSTED BY: Vladimir Korukov
4 Replies
Posted 11 years ago

The usual way to do this is within the notebook. The code below demonstrates 2 different ways to do this. Neither uses a procedural loop, which is not needed since computations stand alone, and do not depend on a preceding result.

(* a function that sums a list *)
(* Your function will of course be different *)
sumNumbers[listOfNumbers_] := Total[listOfNumbers]

(* We can build a table *)
(* Here we have a list of the results for 10 sets of numbers *)
results = Table[sumNumbers[RandomInteger[{1, 10}, 5]], {100}];

(* a histogram of the sums *)
Histogram[results, PlotLabel -> "By Table"]

(* or we can make a set of results by mapping our function onto a \
list of lists of random numbers *)
(* /@ is shorthand -- look up Map in the help system *)

results2 = sumNumbers /@ RandomInteger[{1, 10}, {100, 5}];

(* a histogram of the sums *)
Histogram[results2, PlotLabel -> "By Map"]
POSTED BY: David Keith

This makes sense, however my function is about a page long, and I would like to be able to separately edit and run parts of it. Is there a way to define a function over multiple cells?

POSTED BY: Vladimir Korukov
Posted 11 years ago

Hi Vladimir,

In the example above, the code length of the function does not matter. When used in Table or Map it can have a short name for convenience.

If you want to run "parts" of the function alone or in different combinations, then you might want separate functions for these computations. You can also build functions which perform some computation by calling these sub-functions.

If you are exploring some dataset in a really interactive way, remember that while you are working in the notebook, it is really just an interface to the kernel. It is the kernel that contains the state of the system. For example, I sometimes work with a notebook with various analysis tools that operated an a dataset. I can import a dataset, then analyze it as I like. I can go back up to the top of the notebook, import a new dataset, then go to the various cells for analysis, again. The order in which cells appear in a notebook is only significant when you evaluate the cells sequentially.

I don't know of a way to define a function across multiple cells. I often develop a function by going through a computation in multiple cells. starting the work on some named object. When I am confident it's all working as expected, I merge cells into a module definition which accepts that starting name as an argument. Then I can use that function as I like, for example by mapping it onto a list of file names to analyze the set.

I have thought at times that it would be nice to uses notebooks this way: to call several notebooks from a master notebook to perform computations. I suspect it can be done, but I've never come upon a method that seemed like it would become routine for me.

Best, David

POSTED BY: David Keith

Thanks for the input David. Now that I think about it I guess I can just make each cell into a function, and then the last function will evaluate the whole notebook, so then I can use your method to get a histogram.

I poked around the Notebook commands and found something that I think fits my scenario better. I think this might be the routine you're talking about to evaluate different notebooks from a master notebook.

(* Assuming the notebook you want is the third one *)
(* Also make sure the last line of our notebook is the variable you need *)
nb = Notebooks[][[3]]
(* values gets assigned the list of values of the last cell in the notebook *)
values = Function[{}, EvaluateNotebook[nb]] /@ Range[0,10]
POSTED BY: Vladimir Korukov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract