0

I've been working heavily with Block[] for months in my research so I've read the documentation for the seemingly similar commands With[], Block[], and Module[].

Personally, I've had by far the greatest success with Block, despite With having the connotation of what I think I am trying to accomplish. Sometimes With will solve a problem where Block works as well, but often not. I haven't tried Module as much, but the documentation seems so similar.

I am sure there are very critical subtleties which make these three commands have their respective places in the world and I fear choosing the wrong one may cause problems.

My incentive for using these types of commands typically is, I work with expressions of several variables (large matrices with 7 variable dependencies), and often I wish to (temporarily) examine a lower-dimensional subspace of my expression; i.e. I may wish to "substitute" numerical values for 5 or 6 of the variables to produce a simpler expression which depends only on 2 or 1 variables. Of course, it is important for me to retain the true expression (with 7 variables) so I don't simply want to set values for the variables (I could do this and then use Clear, but this gets messy very quickly). Also, I have found that using rule substitutions like

(expr)/.x->1.45   

often cause mathematica to treat x as 1.45 elsewhere. I need local variable assignments, so it seems Block, With, or Module is the way to go. But how should I chose?

(* ==========================  EDIT  ========================== *)  

When Module, for example, is wrapped inside another module, the text becomes red. Does this indicate a problem or is it normal/functioning properly?

Module[{x = 4}, (x^2 + y^2) Module[{x = 1}, (x + y)]]  
Block[{x = 4}, (x^2 + y^2) Block[{x = 1}, (x + y)]]
With[{x = 4}, (x^2 + y^2) With[{x = 1}, (x + y)]]  

all of the above seem to return the desired output of

(y+1)(y^2+16)  

but mathematica gives slightly different variable colorings. Maybe the red isn't bad?

Steve
  • 1,153
  • 7
  • 17
  • There is a short tutorial on the difference between Block and Module, which I guess I understand a little bit, but I still don't know how With works differently or totally understand this article. – Steve Feb 27 '14 at 21:26
  • Steve, please look at the linked duplicate above. Leonid's answer goes into a lot of detail on the differences. – rm -rf Feb 27 '14 at 21:28
  • Awesome, yeah, there's a lot there. Should I delete this question? – Steve Feb 27 '14 at 21:31
  • Generally we leave duplicates around because it helps with Google keywords for searches, but if you'd rather have it deleted, feel free to do so :) – rm -rf Feb 27 '14 at 21:33
  • Ok thanks. Actually I do have a slightly new question that I'll edit into the original post. – Steve Feb 27 '14 at 21:42
  • Steve, your updated question would be a duplicate of this: Frontend coloring of Table[x, {x, 0, x}] The simple answer is: while it might give the right result, it's bad practice and makes your code hard to debug. – rm -rf Feb 27 '14 at 21:58
  • I should avoid nesting Block within Block, etc. as above, since while it worked in that simple example, I've already found in my research problem that it gives bad results. What is the proper alternative to this? – Steve Feb 27 '14 at 22:08
  • Block[{x = 4}, (x^2 + y^2) ReplaceAll[(x + y), x -> 1]] also works, but, again, with so many options, beyond trial and error, I don't know why certain methods sometimes work and sometimes not. – Steve Feb 27 '14 at 22:12
  • I didn't say you shouldn't nest a Block within a Block... sometimes it is necessary. Just don't reuse the same variables in the inner and outer blocks. Very often, it's a sign of bad programming, which is why the highlighter is trying to warn you. – rm -rf Feb 27 '14 at 22:12
  • Oh, yes, sorry, you're right, nesting two blocks with different variable specifications should be fine, but right now I am dealing with a situation where I do need to set a single variable to different values within different parts of an expression, similar to this x=1 and x=4 in different parts of the expression example I gave in the edit. – Steve Feb 27 '14 at 22:14
  • Well, I can't answer that generally, but if there's some way to separate the "part"s like with Numerator/Denominator or by collecting the terms, etc., you can set them separately. This seems like an oddly constructed example but if you indeed have such a situation, then I would look at the previous steps that lead to such an expression and see if I can retain the parts separately and combine them at the end. – rm -rf Feb 27 '14 at 22:18
  • Yeah, I apologize for this messy question. I can't really bring it down to a MWE from the full research problem at the moment, so I think I'll stop bothering you/everyone with this for now. Things seem to be working for me now, but definitely I still have much to learn in this area. – Steve Feb 27 '14 at 22:31

0 Answers0