From: AES on 10 May 2010 06:38 To make this query a bit more generic, suppose you're typing in at the beginning of a notebook a series of expressions that you're going to use later on expr1 := . . . expr2 := . . . expr3 := . . . . . . where each of these expressions contains some subset of a fairly lengthy set of global variables a, b, c, . . . to which no values have yet been assigned. Suppose further that as you're typing in these expressions, you may to test whether one or another of them (let's say expr2) works properly, without having any persistent side effects on that expression, or any of the previous or subsequent expression definitions, or any of the global variables, after the test is carried out, the test line deleted, and the expression definitions all reevaluated. One way to do this might reasonably seem to be to define a typical set of _numerical_ test values a1, b1, . . . and apply them as follows: testValues = {a->a1, b->b1, . . . } expr1 := . . . expr2 := . . . expr2 /. testValues expr3 := . . . . . . after which you remove the test line and re-evaluate all the definitions. But this approach apparently can encounter problems arising from the order of evaluations. So, how about testValues = {a=a1, b=b1, . . . } expr1 := . . . expr2 := . . . With[ testValues, expr2 ] expr3 := . . . . . . Is that a safe approach to this objective?
From: David Bailey on 11 May 2010 06:29 AES wrote: > If I execute the following lines, I get the graphic that I want: > > x; Remove["Global`*"]; > > lines := Graphics[ Table[ Line[{{k 10/kmax,-1}, > {k 10/kmax,1}}], {k,1,kmax}]] > > km = 30; Show[lines] > > If I execute the following lines, I get exactly the same graphic, all OK, > > x; Remove["Global`*"]; > > lines := Graphics[ Table[ Line[{{k 10/kmax,-1}, > {k 10/kmax,1}}], {k,1,kmax}]] > > testValues = {kmax->30}; Show[lines] /. testValues > > -- except that I also get a beep, and an error msg in the Messages > window: > > "Iterator {k,1,kmax} does not have appropriate bounds" > > (but no little red square in the graphics output cell). > > Same thing happens in the /. case if I do the two tests in reverse order. > > Seems like another annoying little Mathematica "gotcha" to me . . . ? > Clearly the error message arises from the attempt to execute the expression: Table[ Line[{{k 10/kmax,-1},{k 10/kmax,1}}], {k,1,kmax}] The left hand side of ReplaceAll is evaluated before the ReplaceAll is executed - which surely makes sense! Therefore, are you suggesting that expressions should never generate errors if they cant be evaluated - just left unevaluated on the off chance that a subsequent replacement will leave something that could be evaluated! Imagine how frustrating it would be to use a system like that! Obviously Mathematica is not going to change in this regard, but what on earth would you prefer it to do? I generally avoid using SetDelayed to set the value of individual variables (as opposed to functions) precisely because the order of evaluation can surprise and anyway mathematical intuition leads one to expect a variable to be no more than a repository for a value. David Bailey http://www.dbaileyconsultancy.co.uk
First
|
Prev
|
Pages: 1 2 Prev: labels in GraphicsGrid[] Next: How to Explain This Behavior of Simple Procedures? |