Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2003

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

Search the Archive

Re: Why is this replacement not performed the first time?

  • To: mathgroup at
  • Subject: [mg44140] Re: [mg44128] Why is this replacement not performed the first time?
  • From: Andrzej Kozlowski <akoz at>
  • Date: Fri, 24 Oct 2003 04:24:09 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

On Thursday, October 23, 2003, at 08:16 PM, Steven T. Hatton wrote:

> When I evaluate the following expression, I get the error shown
> subsequently:
> Table[locations[[i1]][[j1]][[k1]]/.
>     Flatten[Table[{i1 -> i + i2, j1 -> j + j2, k1 -> k + k2}, {i2, -1, 
> 1,
>           2}, {j2, -1, 1, 2}, {k2, -1, 1, 2}], 2]
>   , {i, 2, 4}, {j, 2, 4}, {k, 2, 4}]
> \!\(\*
>   RowBox[{\(Part::"pspec"\), \(\(:\)\(\ \)\), "\<\"Part specification \
> \\!\\(i1\\) is neither an integer nor a list of integers. \\!\\(
> \*ButtonBox[\
> \\\"More\[Ellipsis]\\\", ButtonStyle->\\\"RefGuideLinkText\\\", \
> ButtonFrame->None, ButtonData:>\\\"General::pspec\\\"]\\)\"\>"}]\)
> After the error, I get the table I expect from the statement.  Why am I
> getting this error?  Is it because Mathematica is doing some kind of 
> syntax
> checking before executing?
> -- 
> "Philosophy is written in this grand book, The Universe. ... But the 
> book
> cannot be understood unless one first learns to comprehend the 
> language...
> in which it is written. It is written in the language of mathematics, 
> ...;
> without which wanders about in a dark labyrinth."   The Lion of Gaul

No, it's just a matter of evaluation order. Let me illustrate this on a 
simpler. But first of all let me point out that you need not use the 
unwieldy form

> locations[[i1]][[j1]][[k1]]

since locations[[i1,j1,k1]]
will do the same and has some additional advantages.

To illustrate your problem consider first the simple example:

f = {1, 2, 3};

Table[f[[i]] /. i -> j, {j, 1, 2}]

 From In[3]:=
    Part specification i is neither an integer nor a list
       of integers. More?
 From In[3]:=
{1, 2}

What happened is simply this. The first argument of Table is 
ReplaceAll[f[[i]],i->j]. When this is evaluated within Table, since 
ReplaceAll does not hold its first argument it tried to evaluate it, 
which of course it can't do since i has no value at this point. So you 
get the error message. But since the result of the unsuccessful 
evaluation of f[[i]] is just f[[i]], that is what ReplaceAll receives 
and it can then proceed with the replacement. So in the end you end up 
with the right answer. A simple way to deal with this problem is to 
wrap Unevaluated around f[[i]]:

Table[Unevaluated[f[[i]]] /. i -> j, {j, 1, 2}]

 From In[5]:=
{1, 2}

But let's now complicate things further, to make them more like your 
case. Instead of a single replacement rule let7s have a list of 
replacement rules. Now wrapping Unevaluated no longer works:

Table[Unevaluated[f[[i]]] /. {{i -> j}, {i -> j + 1}}, {j, 1, 2}]

 From In[7]:=
    Part specification i is neither an integer nor a list
       of integers. More?

 From In[7]:=
    Further output of Part::pspec
      will be suppressed during this calculation. More?

 From In[7]:=
{{1, 2}, {2, 3}}

The reason is again the evaluation order. Rather than try to describe 
it let me present one solution that should be self-explanatory:

Table[(Unevaluated[f[[i]]] /. #1 & ) /@ {{i -> j}, {i -> j + 1}}, {j, 
1, 2}]

{{1, 2}, {2, 3}}

So if you want to avoid your error message you will have to do 
something like this. But actually, since the message is quite harmless 
in this case you may simply just turn it off with using the function 

Andrzej Kozlowski
Yokohama, Japan

  • Prev by Date: Baffling Failure when plotting multiple curves.
  • Next by Date: RE: LogLogListPlot, LogLinearListPlot- Ticks and TickMarks
  • Previous by thread: Re: Why is this replacement not performed the first time?
  • Next by thread: NDSolve does not work in shooting method