Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I found out that,it is INCREDIBLY(like 100 times) faster to use Dot instead of Sum,to perform long sums. But I have not been able to use this alternative to perform the following sum:



I want to get this output:


I tried:


but did not work.

The easy way to do it is:


But I want to avoid this slow way. Any faster alternative?

share|improve this question
Have you looked at using Total? – chuy Sep 5 '13 at 17:40
NO did not. I will try that... – Mencia Sep 5 '13 at 17:41
@chuy Actually it's just Total[a] in this case. What your input does is more simply done as Total[a, {2}]. – Szabolcs Sep 5 '13 at 17:44
up vote 19 down vote accepted

Sum is meant for working with symbolic sums (check the examples in the docs to see what I mean). What you are looking for here is Total:


(* ==> {b+d,c+e} *)

Before we had this shorthand, we used Plus with Apply:

Plus @@ a

(* ==> {b+d,c+e} *)

The most important difference between using Total and Plus is that Total is optimized for performance. Plus will always do compensated summation while this needs to be explicitly turned on in Total using Method -> CompensatedSummation.

Both can be used at various levels or ranges of levels. Compare Total[a, {2}] with Apply[Plus, a, {1}].

share|improve this answer
Good ol´ times, eh? I find Plus@@a also especially educational... – Yves Klett Sep 5 '13 at 17:47
Thanks @Szabolcs!! – Mencia Sep 5 '13 at 18:00

Or, in keeping with the request to use Dot, you can restate it as a vector/matrix product

{1, 1}.a

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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