Re: Re: Product

• To: mathgroup at smc.vnet.net
• Subject: [mg87681] Re: [mg87592] Re: Product
• From: "Szabolcs HorvÃt" <szhorvat at gmail.com>
• Date: Tue, 15 Apr 2008 05:55:03 -0400 (EDT)
• References: <200804110542.BAA04653@smc.vnet.net>

```>
>  Well, maybe I conceded too much too quickly ;-)
>  When I wrote that "it would be quite hard or impossible ", I had in mind
> dealing with general tensors. Unfortunately I did not devote to this many
> any detailed thought but instinctively felt that, in the case of "general
> tensors", it would not be as easy to do the same things with Apply and Plus
> as with Total. When I first read your reply I again reacted too quickly and
> conceded the entire point.  However, after cosnidering this issue a little
> more carefully, I now think the truth is more subtle. It was certainly very
> rash of me to write ""it would be quite hard or impossible", as it is
> clearly possible and in fact not at all hard. But now, I think, in the case
> of tensors there can be significant inconveniences in using the Apply, Plus
> combination instead of Total. Let me illustrate this. Consider this tensor:
>
>  A = Array[a, {2, 3, 4}];
>
>  Now, with Total we can add all elements in A with:
>
>  Total[A, Infinity]
>
>   a[1, 1, 1] + a[1, 1, 2] + a[1, 1, 3] + a[1, 1, 4] + a[1, 2, 1] + a[1, 2,
> 2] +
>   a[1, 2, 3] + a[1, 2, 4] +
>    a[1, 3, 1] + a[1, 3, 2] + a[1, 3, 3] + a[1, 3, 4] + a[2, 1, 1] +
>   a[2, 1, 2] + a[2, 1, 3] + a[2, 1, 4] +
>    a[2, 2, 1] + a[2, 2, 2] + a[2, 2, 3] + a[2, 2, 4] + a[2, 3, 1] +
>   a[2, 3, 2] + a[2, 3, 3] + a[2, 3, 4]
>
>  or we can do the same with Total[A,-1].
>
>  Note, however, that this will give a completely different answer:
>
>  Apply[Plus, A, {0, Infinity}]
>  144
>
>  Of course what happened is that all the indexes got added. If we want to
> add all the tensor entries you
>  need  to correctly specify the highest level:
>
>  Apply[Plus, A, {0, 2}]
>  a[1, 1, 1] + a[1, 1, 2] + a[1, 1, 3] + a[1, 1, 4] + a[1, 2, 1] + a[1, 2, 2]
> +
>   a[1, 2, 3] + a[1, 2, 4] +
>    a[1, 3, 1] + a[1, 3, 2] + a[1, 3, 3] + a[1, 3, 4] + a[2, 1, 1] +
>   a[2, 1, 2] + a[2, 1, 3] + a[2, 1, 4] +
>    a[2, 2, 1] + a[2, 2, 2] + a[2, 2, 3] + a[2, 2, 4] + a[2, 3, 1] +
>   a[2, 3, 2] + a[2, 3, 3] + a[2, 3, 4]
>
>  Note also that now your rule:
>
>
>
> >
> > > Total[expression, {n}]  is equivalent to  Apply[Plus, expression, {n-1}]
> > >
> >
>
>  does not work for negative indexes.  Total[A,{-1}] is Apply[Plus,A,{-3}]
> and not Apply[Plus,A,-2}].
>  The reason is again the same: Apply counts negative levels beginning with
> the atoms, which are in this case just the indexes.
>
>  OK, so now comes my main point.  Suppose that now we define
>
>  a[x_, y_, z_] := Times[x, y, z]
>
>  With this definition the array A turns into an array of integers. Now, note
> that
>
>  Total[A, Infinity]
>  180
>  Total[A, -1]
>  180
>
>  have the same meaning as before, they are both the sum of all the entries
> of A. But the meaning of
>
>  Apply[Plus, A, {0, Infinity}]
>  180
>
>  has now changed. Recall that earlier the answer was 140, which was the sum
> of the indexes, and now it has become 180, which is the sum of entries. The
> same applies to some of the other cases of using the Apply, Plus
> combination. This "change of meaning" can be a serious inconvenience in
> certain situations.
>
>  So while you can certainly do the same things with Apply and Plus and level
> specifications as with Total and level specifications, they seem to be
> intended for different purposes. Apply and Plus are more suitable for
> handling general expressions while Total is much more convenient for dealing
> with tensors. While you can always achieve the same purpose with Apply and
> Plus as with Total, doing so is not completely automatic but has to be based
> on individual cases.
>
>  So I conclude that the reason for the existence of Total is not simply that
> of syntax but more importantly that of purpose: the Apply, Plus combination
> is more suitable for handling general expressions but Total is more suitable
> for dealing with tensors. I think it is a fairly significant difference.

Dear Andrzej,

Thanks for pointing this out!  Now I see that Apply[Plus, ...] and
Total[] have a more important essential difference than the Method
option.

To sum up the main difference:  Apply treats expressions with
different heads in the same way.  But Total only touches those
subexpressions whose head matches the head of the complete expression.
For example,

Total[ f[...], Inifnity ]

will only sum the terms of expressions with head f, and

Total[ {...}, Infinity ]

will only sum lists (but it treats everything else as atoms).

Szabolcs

```

• References:
• Product
• From: "Steven" <steven_504@telenet.be>
• Prev by Date: Re: Labelling contours on contour plots
• Next by Date: Re: A Problem with Simplify
• Previous by thread: Re: Re: Product
• Next by thread: RE: Product