Re: Re: Product

```On 15 Apr 2008, at 06:43, Andrzej Kozlowski wrote:
>
> On 15 Apr 2008, at 00:42, Szabolcs Horv=E1t wrote:
>> On Mon, Apr 14, 2008 at 3:13 PM, Andrzej Kozlowski
>> <akoz at mimuw.edu.pl> wrote:
>>>
>>>> But of course in principle there is nothing that would make it
>>>> impossible to introduce a special case for functions like Times and
>>>> Plus, and make them work with packed arrays.  So this alone does =

>>>> not
>>>> make it necessary to introduce a new syntax/new function, like
>>>> Total[].
>>>>
>>>
>>> Well, I never claimed that it did.
>>
>> I didn't mean that you did :-)
>>
>>> It seems to me that WRI wanted to add
>>> new functionality to Mathematica and modifying Plus would break
>>> too many
>>> things. For example, Total, in version 6 accepts level
>>> specifications, so:
>>>
>>> Total[{{a, b}, {c, d}}, {2}]
>>> {a + b, c + d}
>>>
>>> while
>>>
>>> Apply[Plus, {{a, b}, {c, d}}, {2}]
>>> {{a, b}, {c, d}}
>>>
>>> The key thing, in my opinion, is that Total[list] is just a
>>> spacial case of
>>> Total (=Total[list,1]) and the general case of Total would be
>>> quite hard or
>>> impossible to define using Apply and Plus.
>>>
>>
>> Actually one just needs to shift the level specification by 1, so
>>
>> Total[{{a, b}, {c, d}}, {2}] is equivalent to Apply[Plus, {{a, b}, =

>> {c, d}}, {1}]
>>
>> Generally,
>>
>> Total[expression, {n}]  is equivalent to  Apply[Plus, expression,
>> {n-1}]
>>
>> and
>>
>> Total[expression, n]  is equivalent to  Apply[Plus, expression, {0, =

>> n-1}]
>>
>> Szabolcs Horv=E1t
>
>
> Yes, you are right, of course. (I gues I should stop posting replies =

> whose conents consist of the first thing that comes to my mind, but =

> I usually do not have the time for much more).
> Level specifications are certianly not the reason for having Total
> (though I find them a bit more convenient to use - which is what
> made me make that silly claim). I guess the reason was just the
> feeling that the syntax of Apply and Plus was just a bit too
> convoluted.
>
> Andrzej
>

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

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.

Andrzej Kozlowski

```

• References:
• Product
• From: "Steven" <steven_504@telenet.be>
• RE: Product
• From: "Jose Luis Gomez" <jose.luis.gomez@itesm.mx>
• Re: Product
• From: Szabolcs Horvát <szhorvat@gmail.com>
• Prev by Date: Re: A kernel, multiple notebooks, and Global?
• Next by Date: Re: EdgeRenderingFunction to produce edge labels in GraphPlot
• Previous by thread: Re: Re: Product
• Next by thread: Re: Re: Product