18
$\begingroup$

Times and Plus have the same grammar, and so do Product and Sum. So is there a function doing multiplication that has the same grammar as Total does then?

If yes, what is it? If no, why?


Now I learn from @AntonAntonov that there is less need to design a ListProduct function. So this indicates that multiplication and addition cannot be treated on the same footing.


About "duplicate": I think not. Because my question is not aimed at finding how, but at why.

About "off-topic": I think it constructive to make some of the design "philosophy" behind clearer to facilitate deeper understanding.

$\endgroup$
5
  • 1
    $\begingroup$ Related prior MSE thread $\endgroup$ Commented Apr 21, 2018 at 15:58
  • 1
    $\begingroup$ Possible duplicate of Most Efficient Way to Calculate the Product of All Items in a List? $\endgroup$
    – MarcoB
    Commented Apr 21, 2018 at 18:13
  • 1
    $\begingroup$ How about prod = Apply[Times]? prod[{1,2,3,4}] --> 24. $\endgroup$
    – Szabolcs
    Commented Apr 22, 2018 at 17:36
  • $\begingroup$ Also Fold[Times], which can be faster than Apply[Times] on packed arrays. And Tr[#, Times] &. although Tr is slightly slower than Apply. $\endgroup$
    – Michael E2
    Commented Apr 22, 2018 at 18:20
  • $\begingroup$ Um, "If yes, what is it?" seems "aimed at finding how." And, it seems to me, that many if not most users who ask "why?" are really interested in "how?", judging by which answer they accept. Of course, (1) you're not most users, just one, and (2) I'm not voting to close anyway. $\endgroup$
    – Michael E2
    Commented Apr 23, 2018 at 3:20

2 Answers 2

33
$\begingroup$

In addition to the previous answer...

I designed and implemented Total 17 years ago. Its first version was named ListSum. The primary reason for ListSum's implementation was to encapsulate the functionalities dealing with error accumulation while summing a list of numbers. (A well known phenomena in, say, numerical solvers for ODE's.)

Of course, I also considered ListProduct, having analogous functionality. But there was not really a strong reason or use case for it.

(During the final design phase Stephen Wolfram renamed ListSum to Total.)


@tomd

My memory of things is that before Total, Tr@lst was considered faster than Plus@@lst if lst was a packed array, an observation Ted Ersek attributes to Rob Knapp

Rob Knapp was supervising my work on the Total project.

$\endgroup$
3
  • 2
    $\begingroup$ Thanks for the historical context! $\endgroup$ Commented Apr 22, 2018 at 0:28
  • 1
    $\begingroup$ @HenrikSchumacher Thanks, I thought it might be of interest... $\endgroup$ Commented Apr 22, 2018 at 14:14
  • 1
    $\begingroup$ @AntonAntonov I think it interesting too. $\endgroup$ Commented Apr 22, 2018 at 14:28
16
$\begingroup$

Total is different from Plus@@ because it uses tricks to minimize round-off error. It is thus a useful addition to the toolkit. Times@@ doesn't suffer from the problem, so no similar function is needed.

$\endgroup$
7
  • 1
    $\begingroup$ Could you please give a concrete example demonstrating that, concerning to round-off errors, Total and Plus@@ are different? $\endgroup$ Commented Apr 21, 2018 at 16:07
  • 1
    $\begingroup$ I prefer to state it as Apply[Plus] rather than the syntax Plus@@, because Apply[Plus] is actually a function now that we're in the brave new curried world! $\endgroup$ Commented Apr 21, 2018 at 16:09
  • 3
    $\begingroup$ @ΑλέξανδροςΖεγγ See the documentation of Total, section Examples->Options->Method. $\endgroup$ Commented Apr 21, 2018 at 16:10
  • 2
    $\begingroup$ My memory of things is that before Total, Tr@lst was considered faster than Plus@@lst if lst was a packed array, an observation Ted Ersek attributes to Rob Knapp $\endgroup$
    – user1066
    Commented Apr 21, 2018 at 17:39
  • 2
    $\begingroup$ @ΑλέξανδροςΖεγγ Related: (103771), (145019) $\endgroup$
    – Michael E2
    Commented Apr 21, 2018 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.