MathGroup Archive 1992

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

Search the Archive

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

Michael Trott <trott at> writes:

    Hi mathgroup,
    why is the i in

    Table[ v[i]'[t] == If[ x[i]>0,ex1,ex2] , {i,2,6} ]

    not replaced with the iterator i?

The reason is the unconventional way in which If evaluates its arguments.
If has the HoldAll attribute, which means that its arguments are not
evaluated when the statement is initially encountered, but only under
control of the If itself.  The result of evaluating an If statement of the


will be of one of two things: (1) if "condition" evaluates to True or
False, then the If statement will return "ex1" or "ex2" as appropriate.
(2) If "condition" is neither True nor False, then the If statement remains
unevaluated.  Case (2) is what's happening in your example: since, at the
time your Table statement is executed, Mathematica evidently doesn't know
whether x[1]>0, x[2]>0, etc. are True, it simply leaves the If statement as
is; this means, in particular, that the current value for i is not
substituted either.

When the If statement has a third argument, as in


then it always returns one of ex1,ex2,ex3, with ex3 representing the case
in which "condition" is not known to be either True or False.

One way to get Mathematica to do what want is this:

  In[4]:= Table[ v[j]'[t] == If[ x[j]>0,ex1,ex2] /. j->i, {i,2,6} ]

  Out[4]= {(v[2])'[t] == If[x[2] > 0, ex1, ex2], 

  >    (v[3])'[t] == If[x[3] > 0, ex1, ex2], 

  >    (v[4])'[t] == If[x[4] > 0, ex1, ex2], 

  >    (v[5])'[t] == If[x[5] > 0, ex1, ex2], 

  >    (v[6])'[t] == If[x[6] > 0, ex1, ex2]}

However, depending on the application you have in mind, it might be more
straightforward just to ensure that x[2],...,x[6] have actual values at the
time your Table statement is executed, and then use the same Table
statement you originally used.

Jack Lee
Dept. of Mathematics
University of Washington
Seattle, WA 

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