Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Variables in Iterator limits?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109640] Re: Variables in Iterator limits?
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sun, 9 May 2010 07:51:09 -0400 (EDT)

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


  • Prev by Date: Re: FrontEnd and bash automatization
  • Next by Date: Re: RuleDelayed for parsing XML with multiple children
  • Previous by thread: Re: Variables in Iterator limits?
  • Next by thread: Re: Variables in Iterator limits?