0
$\begingroup$

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?

$\endgroup$
12
  • $\begingroup$ 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. $\endgroup$ Commented Feb 27, 2014 at 21:26
  • $\begingroup$ Steve, please look at the linked duplicate above. Leonid's answer goes into a lot of detail on the differences. $\endgroup$ Commented Feb 27, 2014 at 21:28
  • $\begingroup$ Awesome, yeah, there's a lot there. Should I delete this question? $\endgroup$ Commented Feb 27, 2014 at 21:31
  • $\begingroup$ 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 :) $\endgroup$ Commented Feb 27, 2014 at 21:33
  • $\begingroup$ Ok thanks. Actually I do have a slightly new question that I'll edit into the original post. $\endgroup$ Commented Feb 27, 2014 at 21:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.