Message Boards Message Boards

0
|
4250 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Tables and Sums and real Results

Posted 9 years ago

The world is full of surprises. The iterators in Table and Sum are of the form { i, imin, imax } and obviously it is assumed, that strictly imax > imin. Entries not fullfilling this conditions seem to be suppressed. That is most important in tables and sums with e.g. two indices, one being a function of the other.

Look at

In[55]:= Table[{i, j}, {i, 2, 9}, {j, 0, 5 - i}]

Out[55]= {{{2, 0}, {2, 1}, {2, 2}, {2, 3}}, {{3, 0}, {3, 1}, {3, 
   2}}, {{4, 0}, {4, 1}}, {{5, 0}}, {}, {}, {}, {}}

with four "empty" results.

What I expected was

In[56]:= tt = 
 Flatten[Table[{i, j}, {i, 2, 9}, {j, Min[0, 5 - i], Max[0, 5 - i]}], 
  1]

Out[56]= {{2, 0}, {2, 1}, {2, 2}, {2, 3}, {3, 0}, {3, 1}, {3, 2}, {4, 
  0}, {4, 1}, {5, 0}, {6, -1}, {6, 0}, {7, -2}, {7, -1}, {7, 
  0}, {8, -3}, {8, -2}, {8, -1}, {8, 
  0}, {9, -4}, {9, -3}, {9, -2}, {9, -1}, {9, 0}}

Accordingly double-sums have to be considered with caution. Change Table to Sum:

 In[64]:= Sum[{i, j}, {i, 2, 9}, {j, 0, 5 - i}]

    Out[64]= {30, 10}

while the correct result should read

In[58]:= Total[tt]

Out[58]= {140, -10}
POSTED BY: Hans Dolhaine
3 Replies

Sum and Table do exactly the same thing

In[1]:= Table[{i, j}, {i, 2, 9}, {j, 0, 5 - i}]
Out[1]= {{{2, 0}, {2, 1}, {2, 2}, {2, 3}}, {{3, 0}, {3, 1}, {3, 2}}, {{4, 0}, {4, 1}}, {{5, 0}}, {}, {}, {}, {}}

if $j_{max} < j_{min}$ the element is skipped. Now for Sum in symbolic form

In[7]:= Sum[{f[i], g[j]}, {i, 2, 9}, {j, 0, 5 - i}]
Out[7]= {4 f[2] + 3 f[3] + 2 f[4] + f[5], 4 g[0] + 3 g[1] + 2 g[2] + g[3]}

In[8]:= (* {f,g} *)
{4 2 + 3 3 + 2 4 + 5, 4 0 + 3 1 + 2 2 + 3}
Out[8]= {30, 10}

The iterator step is by default 1 and with that step one cannot reach -1 starting from 0. If in

Table[{i, j}, {i, 2, 9}, {j, 0, 5 - i}]

j should step down too one takes that into account by typing

In[11]:= Table[{i, j}, {i, 2, 9}, {j, 0, 5 - i, Sign[5 - i]}]
Out[11]= {{{2, 0}, {2, 1}, {2, 2}, {2, 3}}, 
          {{3, 0}, {3, 1}, {3, 2}}, 
          {{4, 0}, {4, 1}},
          {{5, 0}}, 
          {{6, 0}, {6, -1}}, 
          {{7, 0}, {7, -1}, {7, -2}}, 
          {{8, 0}, {8, -1}, {8, -2}, {8, -3}}, 
          {{9, 0}, {9, -1}, {9, -2}, {9, -3}, {9, -4}}}

which fits to the expectations

In[15]:= Plus @@@ Transpose[Flatten[Table[{i, j}, {i, 2, 9}, {j, 0, 5 - i, Sign[5 - i]}], 1]]
Out[15]= {140, -10}

And, yes

In[16]:= Sum[{i, j}, {i, 2, 9}, {j, 0, 5 - i, Sign[5 - i]}]
Out[16]= {140, -10}

And yes

If you program ... without care

you do not program, but merely type. It is the same thing as the infamous wrong proof in mathematics.

POSTED BY: Udo Krause

To avoid negative indices, you might try

In[4]:= Table[{i, j}, {i, 2, 9}, {j, 0, (5 - i) Boole[i <= 5]}]

Out[4]= {{{2, 0}, {2, 1}, {2, 2}, {2, 3}}, {{3, 0}, {3, 1}, {3, 
   2}}, {{4, 0}, {4, 1}}, {{5, 0}}, {{6, 0}}, {{7, 0}}, {{8, 0}}, {{9,
    0}}}
POSTED BY: S M Blinder

yep - thanks. But I wanted to have these negative indices. The point is, if you program the sum without care you have good chances to get wrong results.

POSTED BY: Hans Dolhaine
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract