1
$\begingroup$

Sometimes I keep two or more notebooks side by side. I noticed that changing $Assumptions in one notebook affects the others, although I have changed Notebook's default context to "Unique to this Notebook".

Is there any walk around of this?

$\endgroup$
4
  • 2
    $\begingroup$ Just start a separate kernel for each notebook? $\endgroup$ May 24, 2018 at 13:39
  • $\begingroup$ How to do that? Quit kernel and start a new one? $\endgroup$ May 24, 2018 at 13:40
  • 1
    $\begingroup$ See C.E.'s commente here on hwo to do it. $\endgroup$ May 24, 2018 at 13:40
  • $\begingroup$ You can set CellProlog/Epilog to set/reset assumptions $\endgroup$
    – Kuba
    May 24, 2018 at 13:50

1 Answer 1

4
$\begingroup$

There are less painful way than duplicating the kernel to have $Assumptions evaluate to different values.

One way to do it, keeping the context local to each notebook, is to use a supporting variable

$Assumptions := ToExpression["myAssumptions"]

You can then set the variable in different notebook and it will pick the context automatically

myAssumptions = a < 0;

If you want to keep the same context among all your notebooks, you can still use the same trick with a small modification

getContext[] := 
 StringJoin["Notebook", ToString[Hash[InputNotebook[]]], "`"]

setAssumption[expr_] := (# = expr) &@
  ToExpression[getContext[] <> "myAssumptions", InputForm, Unevaluated]

$Assumptions := ToExpression[getContext[] <> "myAssumptions"]]

setAssumption[a < 0];

In the current notebook you will have

$Assumptions
(* a < 0 *)

while in another notebook you will see something like

$Assumptions    
(* Notebook944295401273993393`myAssumptions *)

you can then set your different assumptions by doing the same and again $Assumptions will pick up the correct one.

setAssumption[a > 0];
$Assumptions
(* a > 0 *)

You can even get fancier and define a value for when your custom assumptions are not set

$Assumptions := 
 If[ValueQ[#], #, True] &@
  ToExpression[getContext[] <> "myAssumptions", InputForm, Unevaluated]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.