How exactly does MATLAB calculate a sum of array elements by its sum() function? Does it use any compensated summation algorithm such as Kahan?

13 views (last 30 days)
I'm currently working on a software project translating some MATLAB code to Java. During this process, I recognized, that the output of MATLAB's sum() function applied to some 'single' or 'double' array is not identical to a plain addition of consecutive array elements in a loop. I did not find any further information regarding the concrete implementation of sum() in any recent MATLAB version (e.g. for me R2019a), so I tried to replicate its functionality by implementing some common summation algorithms for floating-point addition such as the Kahan summation algorithm or some of its variants. However, the results did not match with the output of MATLAB's sum().
Does anyone know any details about the implementation of MATLAB's sum() function? Is any kind of compensated summation included? Are 'single' arrays cast to 'double' before the summation? Is the summation distributed across multiple threads per default? Are arrays sorted to reduce errors due to the addition of tiny and big floating-point numbers?

Accepted Answer

Steven Lord
Steven Lord on 9 Nov 2021
You might find this post on Loren Shure's blog interesting and informative.

More Answers (3)

dpb on 8 Nov 2021
TMW does not document publicly algorithms beyond anything provided in the documentation Description section or, occasionally an Algorithms section may add some additional insight.
I've not poked around to investigate, the following thread has some Info Of course, as noted there, while not likely to have changed, there's no guarantee TMW hasn't changed any heuristic rules.
  1 Comment
Jonas K.
Jonas K. on 9 Nov 2021
Thank you, unfortunately I'm already aware of the thread you noted, but it would be great to have any information about a current implementation. Especially, since TMW seems to have changed the multi-threading capabilities since then, as Bruno Luong pointed out, which is also in agreement with what I've tested so far.

Sign in to comment.

Edric Ellis
Edric Ellis on 9 Nov 2021
The outtype parameter to sum controls the numeric type used for summation, described in the doc. The default is that sum on single values is performed in single, but other types are operated on in double. (The examples below do rely on the order of operations, which is not guaranteed)
singles = realmax('single') .* [1, 1, -1, -1]
singles = 1×4
1.0e+38 * 3.4028 3.4028 -3.4028 -3.4028
% Saturates
ans = single Inf
% In 'double', doesn't saturate
sum(singles, 'double')
ans = 0
int8s = [intmax('int8'), intmax('int8'), intmin('int8')]
int8s = 1×3
127 127 -128
% Default summation in 'double' - doesn't saturate
ans = 126
% Saturates
sum(int8s, 'native')
ans = int8 -1
  1 Comment
Jonas K.
Jonas K. on 9 Nov 2021
Thank you, the information that 'single' arrays are summed up per default using their native type is definitely helpful, although any details about the concrete way / algorithm how the summation is performed, would be even more helpful.

Sign in to comment.

Bruno Luong
Bruno Luong on 9 Nov 2021
Edited: Bruno Luong on 9 Nov 2021
According to my test it seems MATLAB does not sum by chunk when operating on vector, to ensure the result is consistent, i.e. not depending on number of threads.
>> a=rand(1,1e7);
>> maxNumCompThreads=1;
>> tic; s1=sum(a), toc
s1 =
Elapsed time is 0.005212 seconds.
>> maxNumCompThreads=4;
>> tic; s4=sum(a), toc
s4 =
Elapsed time is 0.005153 seconds.
>> s1-s4
ans =
>> ss=sum(sort(a));
>> ss-s1
ans =
IMO opinion the sum just carried out linearly from left to right with some internal internat result with fiw number of bits > 64.
IIRC, in some version (2015?) MATLAB implements a multi-thread on vector and that raises some questions and that has been discussed on the old newsgroup, then they switched back to single thread.
The multi-thread is used for sum on 2D or ND-array, where each thread is in charge a set of vectors.
All that is hypothetic as TMW does not document the algorirthm.
Bruno Luong
Bruno Luong on 9 Nov 2021
I don't think Loren's blog include details about sum. And beside this post is aimed to alert the behavior change in R2021b, however you are using R2019a.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by