7
$\begingroup$

For constructing a sum of terms which are symbolic, I noticed that in addition to

Sum[f[i],{i,1,10}]

(* f[1] + f[2] + f[3] + f[4] + f[5] + f[6] + f[7] + f[8] + f[9] + f[10] *)

I can also do

Array[f,10,1,Plus]

(* f[1] + f[2] + f[3] + f[4] + f[5] + f[6] + f[7] + f[8] + f[9] + f[10] *)

or even Plus@@Table[f[i],{i,1,10}] or Total[Table[f[i],{i,1,10}]].

While it is not a huge surprise I'm able to achieve the same result using different functions in Mathematica, I'm wondering if there are specific use cases which prefers one form over another for the construction of a sum of symbolic terms. Right now all I can think of for Array is that there the local iterator i is not needed. But performance/memory-wise, is there something else I should be aware of?

$\endgroup$
  • 3
    $\begingroup$ Michael Trott in Programming (pp 707 - 710) argues that as Table has the attribute HoldAll it computes its argument for every call, whereas Array "to the extent possible" computes its argument only at the beginning. This may lead to differences in behaviour as well as speed. Compare (example given by MT) a = 0; Table[a = a + 1; ToExpression[StringJoin["a" <> ToString[a]]][i, j], {i, 3}, {j, 3}] to a = 0; Array[a = a + 1; ToExpression[StringJoin["a" <> ToString[a]]], {3, 3}]. Old SO question $\endgroup$ – user1066 Jul 1 '18 at 7:30
6
$\begingroup$

This is something that you could have tested yourself, quite easily. I sugest the use of RepeatedTiming for more reliable measurements.

BarChart[
 {
   First@RepeatedTiming[
     Sum[f[i], {i, 1, 100}]
     ],
   First@RepeatedTiming[
     Array[f, 100, 1, Plus]
     ],
   First@RepeatedTiming[
     Total@Table[f[i], {i, 1, 100}]
     ],
   First@RepeatedTiming[
     Plus @@ Table[f[i], {i, 1, 100}]
     ],
   First@RepeatedTiming[
     Plus @@ (f /@ Range[100])
     ],
   First@RepeatedTiming[
     Total[f /@ Range[100]]
     ]
   } 10^6
 , ChartLabels -> {
   "Sum",
   "Array[_,Plus]",
   "Total@Table",
   "Plus@@Table",
   "Plus@@Range",
   "Total-Range"
   }
 , PlotTheme -> "Scientific"
 , AspectRatio -> 1/2
 , FrameLabel -> {"Method", "Time \[Mu]s"}
 , ImageSize -> 600
 ]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Ok, that's interesting data. It seems to support the use of Array in the simplest case of f. Having this result can be useful to others. I'm also looking answers from seasoned/experienced programmers as well. $\endgroup$ – QuantumDot Jun 30 '18 at 20:35
  • $\begingroup$ @QuantumDot it will also depend on the auto-compilation and whether your arrays and things are packed, although I still expect Array to do best there. Also of interest might be a Fold[#+f[#2]&, ...] type thing, although if all is as I expect that will be slowest. $\endgroup$ – b3m2a1 Jul 1 '18 at 5:45
  • $\begingroup$ f /@ Range[100] is more efficient than Table[f[i], {i, 1, 100}] with either Total or Plus $\endgroup$ – Bob Hanlon Jul 1 '18 at 14:27
  • $\begingroup$ @BobHanlon, OK, I included that in the comparison. $\endgroup$ – rhermans Jul 1 '18 at 14:39
6
$\begingroup$

Sum isn't really for constructing expressions: it's primarily for finding an analytic formula that represents a sum. It's mostly useful when bounds are unknown or infinite:

Sum[1/2^i, {i, 1, n}]
(* 2^-n (-1 + 2^n) *)

Mere expression construction can't do this.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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