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

• To: mathgroup at yoda.physics.unc.edu
• Subject: Re: Table[ ...If[...i...] ,{i,...}]
• From: John Lee <lee at math.washington.edu>
• Date: Sat, 18 Jul 92 11:34:33 -0700

```Michael Trott <trott at physik.th-ilmenau.de> 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?
thanks

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
form

If[condition,ex1,ex1]

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>0, x>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

If[condition,ex1,ex2,ex3],

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:= Table[ v[j]'[t] == If[ x[j]>0,ex1,ex2] /. j->i, {i,2,6} ]

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

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

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

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

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

However, depending on the application you have in mind, it might be more
straightforward just to ensure that x,...,x 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,...}]