Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1992
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1992

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

Search the Archive

Re: Table[ ...If[...i...] ,{i,...}]

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: Re: Table[ ...If[...i...] ,{i,...}]
  • From: David Withoff <withoff>
  • Date: Mon, 20 Jul 1992 09:43:12 -0500

> Finally, as a workaround the following seems OK, though Lord knows why.
> 
>     Table[ If[ x[t], t, -t ]/.t->i, {i,2,6} ]
> 
> David Sibley
> sibley at math.psu.edu

I make no claim to Lord-liness, but do I think I know why.

One way to get hints about what is going on in such situations is to
use the Trace function.  Puzzling over the output for a few minutes
is definitely a worthwhile exercise:

In[1]:= Trace[Table[ If[ x[t], t, -t ]/.t->i, {i,2,6} ]]

Out[1]= {Table[If[x[t], t, -t] /. t -> i, {i, 2, 6}], {i = 2, 2}, 
 
>    {{{i, 2}, t -> 2, t -> 2}, If[x[t], t, -t] /. t -> 2, 
 
>     If[x[2], 2, -1 2]}, {i = 3, 3}, 
 
>    {{{i, 3}, t -> 3, t -> 3}, If[x[t], t, -t] /. t -> 3, 
 
>     If[x[3], 3, -1 3]}, {i = 4, 4}, 
 
>    {{{i, 4}, t -> 4, t -> 4}, If[x[t], t, -t] /. t -> 4, 
 
>     If[x[4], 4, -1 4]}, {i = 5, 5}, 
 
>    {{{i, 5}, t -> 5, t -> 5}, If[x[t], t, -t] /. t -> 5, 
 
>     If[x[5], 5, -1 5]}, {i = 6, 6}, 
 
>    {{{i, 6}, t -> 6, t -> 6}, If[x[t], t, -t] /. t -> 6, 
 
>     If[x[6], 6, -1 6]}, {If[x[2], 2, -1 2], If[x[3], 3, -1 3], 
 
>     If[x[4], 4, -1 4], If[x[5], 5, -1 5], If[x[6], 6, -1 6]}}

Both Table and If have the attribute HoldAll.  After the assignment
i = 2, the expression If[ x[t], t, -t ] /. t -> i becomes
If[x[t], t, -t] /. t -> 2, which leads to what you want, and so
forth for i=3, i=4, ... .

It might be useful to note that if Table did not have attribute HoldAll,
you'd be back to what you started with, since If[ x[t], t, -t ]/.t->i would
immediately evaluate to If[x[i], i, -i].

Dave Withoff
withoff at wri.com





  • Prev by Date: Re: Re-indexing Mathematica
  • Next by Date: simultaneous equations
  • Previous by thread: Re: Table[ ...If[...i...] ,{i,...}]
  • Next by thread: Re-indexing Mathematica