-1
$\begingroup$

In my notebook, I have to call another file, let's say 'program.m' that defines functions, let's say F[] of whom result c I need in my notebook. What I generally do, is to open a new front end and write

<<program.m
F[] (*=c*)

and after copy and paste the result c in my notebook.

Is there a way to do this inside my notebook, that is without opening a new notebook (and kernel) and evaluating there the above lines?

I'm thinking to use something like ParallelEvaluate but I've never used parallel computations. Now, program.m defines variables and functions sometimes using the same symbols that I used in my notebook for other purposes. So I want that the computations made in my notebook and the ones made by program.m are completely independent each other, that is my notebook and program.m don't have to share functions and variables (for this reason, now I do it in two different kernels). So, for example, in my notebook there is a=0 and program.m has to compute b=D[a,a] (*=1*) and I want b in my notebook. I've tried

a=0;
a (*0*)
ParallelEvaluate[D[a,a],1] (*error*)
ParallelEvaluate[D[a,a],2] (*error*)

but doesn't work. Any suggestions?

$\endgroup$
  • $\begingroup$ Opening a new notebook does not get you a new front end. $\endgroup$ – m_goldberg Feb 7 '17 at 8:53
  • $\begingroup$ It seems like you are asking how to "get away with" bad programming practice, since you want global variables and functions, but don't want them to clash. Maybe put the stuff in program.m in a Context? $\endgroup$ – Marius Ladegård Meyer Feb 7 '17 at 8:56
  • $\begingroup$ @m_goldberg well, you don't get a new front end if you click on File>New>Notebook (or CTRL+N) but you can get it opening a new session of Mathematica that is opening a new welcome screen or by clicking on a .nb file $\endgroup$ – Giancarlo Feb 7 '17 at 9:23
  • $\begingroup$ @MariusLadegårdMeyer well, I'm not an expert programmer, so yes, that is basically my question; every suggestion is very very welcomed! I was thinking about Context too (see the tag) but again I'm not confident with this, and I don't know how can I put everything in the program.m in a Context $\endgroup$ – Giancarlo Feb 7 '17 at 9:28
  • $\begingroup$ This should be the first thing to read. $\endgroup$ – Marius Ladegård Meyer Feb 7 '17 at 9:31
2
$\begingroup$

A couple of solutions that I've found: first of all I had to set another kernel, following this procedure support.wolfram.com/kb/12425

I called my additional kernel "Kernel2", so that I have 2 kernels: "Local"and "Kernel2".

After that, let's say that I start my notebook with "Local" (it's possible to see the used kernel in the menu 'Evaluation > Notebook's Kernel'); in this kernel I can do everything, for example set variables or functions:

a=1
F[x_]:=x

and using them

a+1 (*2*)
F[3] (*3*)

I can stop to use "Local" and switch to "Kernel2" with the following command

SetOptions[EvaluationNotebook[], Evaluator -> "Kernel2"]

From this point onwards, since I have a new kernel, all the new calculations will not interact with the previous one, for example

a+1 (*a+1 not 2*)
F[3] (*F[3] not 3*)

this solve my problem, since now I can call and use another file

<<program.m
F[] 

without interacting at all with what I've done before. When I want to use the first kernel again, I just write

SetOptions[EvaluationNotebook[], Evaluator -> "Local"]

and I can work, set variables, functions etc., just as I've never used the program.m.

If in the kernel "Local" I want result that I calculated in "Kernel2", when I use "Kernel2" I use the Mathematica function Export to safe data in a file and, when I'm working with "Local"I can read information with Import.

Another way to do the same is with the code

a=1
F[x_]:=x

a+1 (*2*)
F[3] (*3*)

new = CreateDocument[{ExpressionCell[Defer[
      SetOptions[EvaluationNotebook[], Evaluator -> "Kernel2"];

      <<program.m
      F[] 
      {"F[3]", F[3]}

      ], "Input",
      Evaluator -> "Kernel2"]}];


SelectionMove[new, All, Notebook]
SelectionEvaluate[new]

a+1 (*2*)
F[3] (*3*)

NotebookImport[new, "Output"] (*{"F[3]", F[3]}*)

where the F and F[3] calculated with "Local" can be different from F and F[3] calculated with "Kernel2"

$\endgroup$

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.