From: AES on
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 . . . ?

From: christopher arthur on

----- Original Message -----
From: "AES" <siegman(a)stanford.edu>
To: <mathgroup(a)smc.vnet.net>
Sent: Saturday, May 08, 2010 6:05 AM
Subject: Variables in Iterator limits?


> 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]
>

but kmax is undefined? probably you don't mean 'km' but 'kmax' in last
line. Then it works fine.

> 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"
>

The problem is the order of operations. The replacement rule is applied
after Show[] executes (or attempts). This means that 'kmax' does not yet
have a value. Perhaps the best way to think about it is to consider that
the replacement rule won't change a length of a list in general but can change
its contents.


Christopher Arthur


> (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 . . . ?
>
>



From: Simon on
It's an order of evaluation thing (and a little bit of a gotcha).
Here's a simpler example:

In[1]:= px:=Print[x]

In[2]:= x=5; px; x=.
5

In[3]:= px/.x->5
x

The reason your images still displays is because after the error is
outputted, the replacement rule fires: eg

Graphics[Line[{{0, 0}, {0, x}}]]
% /. x -> 1

From: Kurt TeKolste on
It would appear that in evaluating

Show[lines]/.testValues

you are first evaluating Show[lines] with no assigned value for kmax,
getting the error message and the full form of Show[lines] and then
evaluating that with kmax assigned, resulting in the graph

For comparison try Show[lines/.testValues]

ekt



On Sat, 08 May 2010 07:05 -0400, "AES" <siegman(a)stanford.edu> 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 . . . ?
>
>

From: Leonid Shifrin on
Hi,

The explanation of the behavior you observed is straightforward and requires
only a general understanding of Mathematica evaluation, nothing
specifically tied to this particular situation: in the second case, since
you apply the rule much later than Table is evaluated (and this follows
from the standard evaluation sequence), then at the time Table executes it
sees kmax as a symbolic parameter, and issues an error message.

One way to avoid this problem (this is what I would do anyway):

Clear[lines];
lines[kmax_] :=
Graphics[Table[
Line[{{k 10/kmax, -1}, {k 10/kmax, 1}}], {k, 1, kmax}]];
Show[lines[30]]

In this case, the problem is avoided because passed parameters are
"injected" in the body of the function before evaluation of the body.
Another one (which minimally modifies your code):

Clear[lines, kmax];
lines := Graphics[
Table[Line[{{k 10/kmax, -1}, {k 10/kmax, 1}}], {k, 1, kmax}]];

testValues = {kmax -> 30};
Block[{Table}, Show[lines] /. testValues]

In this case, the problem is avoided because by Block-ing Table we delay its
evaluation until the very end, and by that time kmax get substituted
according to your rule.

Hope this helps.

Regards,
Leonid


On Sat, May 8, 2010 at 4:05 AM, AES <siegman(a)stanford.edu> 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 . . . ?
>
>