3
$\begingroup$

I have 2 sections is my notebook:

___________SECTION 1______________
Contains variables x y z
___________SECTION 2______________
Should copy over the values of x and y up to now, but make all changes to x
and y local to section 2

The idea is that if I set x = 2 in Section 1, then when the notebook is executed we will have x == 2 in Section 2. However, should I execute x = 3 in Section 2 and then go back and execute x in Section 1, it will still give me back x = 2 in section 1 because the changes to x are local to Section 2.

Meanwhile, if I do the same for z, which I did not make "local" to section 2, then the value for z will be altered in section 1 by what I do in section 2.

Thanks a lot for your help!

--- UPDATE ---

Okay, to make my problem clearer. I have a very long notebook in which I do many calculations with symbolic variables (matrices included). At some point, I arrive at the symbolic expression that I want and from that point onwards I want to begin substituting real values for the symbolic variables, e.g. what may have before been the symbolic variable AA I now substitute with {{a,b,c},{d,e,f},{g,h,i}}. My problem is that once I do this, execution of anything from the start of my document - which assumes symbolic variables - fails and throws lots of errors because now those symbolic variables are substituted with their definitions by Mathematica.

Since my notebook has 1000+ lines of code, I do not want to change anything in terms of how I write my expressions to avoid this "propagation from end to beginning of notebook" (too late for that, although for my future better coding your suggestions are welcome). Therefore, I am looking for a non-intrusive way to separate the section with symbolic variables and the section where these variables become substituted by their definitions. At the same time, I want the latter section to be able to use the previous (symbolic) section's results.

I hope this is a useful clarification. Thank you!

$\endgroup$
  • 1
    $\begingroup$ You will want to use scoping constructs such as Module, Block, etc... Here is a detailed discussion on MMA.SE of their use cases as well. To be honest though, your setup seems to be designed on purpose to engender confusion should the need to troubleshoot it arise :-) $\endgroup$ – MarcoB Jul 31 '15 at 16:22
  • $\begingroup$ @MarcoB Thanks for the pointer, but could you give an example? I would learn best with an answer to my problem, which I could then readily apply to other cases. $\endgroup$ – space_voyager Jul 31 '15 at 16:24
  • $\begingroup$ When you make an assignment to a variable, that's an operation performed by the Kernel. But the Kernel doesn't know anything about the sectioning of the notebook from which the command was sent. So when jumping between sections of a notebook, you're doing things that are invisible to the Kernel. You will need to follow the suggestions of @MarcoB or use Contexts. $\endgroup$ – Jens Jul 31 '15 at 16:35
  • $\begingroup$ By the way, I think it's a valid question because it the notebook paradigm leads to exactly this kind of confusion very frequently. $\endgroup$ – Jens Jul 31 '15 at 16:42
  • 2
    $\begingroup$ All I can say is: start section 1 with this: Clear[Evaluate[Context[]<>"*"]]. This will allow the symbolic stuff to always work. Trying to fix an existing notebook to do everything you asked is probably not realistic. $\endgroup$ – Jens Jul 31 '15 at 17:50
4
$\begingroup$

As @MarcoB states, you should probably use scoping constructs...

If you're opposed to that idea, you can set the Notebooks default context to be Unique to Each Cell Group. I wouldn't recommend that, but it works. You can set that under Evaluation > Notebook's Default Context > Unique to Each Cell Group:

enter image description here

Note, to escape this, you need to specify global variables like z in the correct context.

$\endgroup$
  • $\begingroup$ That's the closest I think one can get (+1) (I was writing my earlier comment when you posted this)... $\endgroup$ – Jens Jul 31 '15 at 16:38
  • 2
    $\begingroup$ @Jens I hesitated to answer because this is a dangerous way to go about constructing notebooks. I can just envision all the problems it could cause. But it is a MMA feature, and it seems to do pretty much what the OP wanted. $\endgroup$ – kale Jul 31 '15 at 16:45
4
$\begingroup$

Note: shown below is an answer to the first version of the OP's question


Here is a simple example using scoping constructs, namely Block in this case. The idea is that you can indicate symbols to make local to Block, which implements dynamic scoping: take a look at the "Background and Context" section of its docs for a more complete explanation. Symbols not localized by Block will retain their global value and "accessibility", so they can be modified from within Block and the modification will be reflected outside of Block as well.

Clear[x, y, z]
x = 2; y = 4; z = 6;

Print["This is the Section 1 value of x: ", x]
Print["This is the Section 1 value of z: ", z]

Block[
  {x = x},
  Print["\nJust entered section 2; here is the current value of x: ", x];
  x = 3;
  Print["Now we changed the value of x local to Section 2; x == ", x];
  z = 8;
  Print["We also changed the value of z: z == ", z];
]

Print["\nNow we are back to Section 1:"]
Print["\tThe value of x is restored: x == ", x]
Print["\tThe value of z has changed: z == ", z]

And here is the output:

Mathematica graphics

$\endgroup$
  • $\begingroup$ Block looks like a good solution, but since I have 200+ lines of code in Section 2 this means I would have to have a 200 line cell? That's no good for debugging especially. Can I start Block[ in one cell and close the brackets ] in another? $\endgroup$ – space_voyager Jul 31 '15 at 17:37
  • $\begingroup$ @space_voyager No, you can't. $\endgroup$ – MarcoB Jul 31 '15 at 17:43
  • $\begingroup$ @space_voyager Just wanted to mention DynamicModuleParent, although I think it's not worth the trouble. $\endgroup$ – obsolesced Aug 20 '16 at 18:35

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.