Message Boards Message Boards

0
|
256 Views
|
8 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Sum and Product a feature request

Wouldn't it be nice to have the following definitions for Sum and Product in order to get the sum resp. the product of a list/vector?

Unprotect[Product];
Product[a_] := Times @@ a
Protect[Product];
Unprotect[Sum];
Sum[a_] := Plus @@ a
Protect[Sum];

\[Sum]{a, b, c, d} == Sum[{a, b, c, d}] == Sum[e, {e, {a, b, c, d}}] ==
  Plus @@ {a, b, c, d} == a + b + c + d
\[Product]{a, b, c, d} == Product[e, {e, {a, b, c, d}}] == 
 Product[{a, b, c, d}] == Times @@ {a, b, c, d} == a*b*c*d


True

True
POSTED BY: Robert Nowak
8 Replies

Tr[list] as well as Total[list] (which Gianluca Gorni mentioned) sum a list, and quickly if list is a packed array. Plus @@ list unpacks the list first. If list is not packed, then the three have similar performance. If the list can be packed, then Total[Developer`ToPackedArray@list] is faster than Total[list]. And list . ConstantArray[1, Length@list] is often fastest, except for the amount of typing. FromDigits[list, 1] is an arcane way to sum a list, but it's faster than Plus @@ list.

I do not know of a similar function for products. WRI has introduced variants for a thousand operations, but not for this fairly common one. Tr[list, Times] forms the product, but not as efficiently as it sums a list. On large packed arrays, Fold[Times, list] is about twice as fast as Times @@ list or Tr[list, Times].

That said — and ignore the arcana — I often see users using Sum[] to add up some numbers. Sum[] is, to my mind, principally a symbolic summation tool. It will do symbolic analysis first to determine a summation method. If it chooses something other than Method -> "Procedural", it will probably do more symbolic analysis, which is usually slow compared to machine float/int operations. Here is an example:

Sum[2. i, {i, 10^6}] // RepeatedTiming
Sum[2. i, {i, 10^6}, Method -> "Procedural"] // RepeatedTiming
Total[2. Range[10^6]] // RepeatedTiming
(*
{0.176149, 1.*10^12}
{0.0119958, 1.*10^12}
{0.000709462, 1.*10^12}
*)

While it is nice to have an efficient Total[] (or Tr[]), it would be nicer if Sum[] did all summation tasks, including this. Ditto for Product[]. Your one-argument suggestions are worth considering.

POSTED BY: Michael Rogers

What about Sum[{{1,2},{3,4}}] ?

POSTED BY: Gianluca Gorni

Interestingly

Sum[{{a, b}, {c, d}}]

gives

{a + c, b + d}

This seems to be an undocumented behavior. According to mathematica doc/help Sum[ ] requires two parameters not one as in the example. I can't find a one parameter version in the doc.

edit: sorry that was a mishap I did not recognizance that my modified Sum was active. All correct with the mathematica doc ;).

POSTED BY: Robert Nowak
POSTED BY: Gianluca Gorni

....oooouch

you are right I evaluated with my proposed feature. With the feature activated this would be the correct desired behavior of summing up the 1st level elements.

Sum[{{a, b}, {c, d}}]

{a + c, b + d}

POSTED BY: Robert Nowak

The function Total does that too. You would like to incorporate Total into Sum?

POSTED BY: Gianluca Gorni

Total does more than simple summation. With Total you can control at which nesting level the summation happens.

POSTED BY: Robert Nowak
Σ{a,b,c,d}

a + b + c + d

Π{a,b,c,s} 

a * b * c * d

POSTED BY: Robert Nowak
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