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

26 views (last 30 days)

Show older comments

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?

##### 0 Comments

### Accepted Answer

### 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 https://www.mathworks.com/matlabcentral/answers/550-compensated-summation-in-sum?s_tid=answers_rc1-1_p1_MLT#answer_822 Of course, as noted there, while not likely to have changed, there's no guarantee TMW hasn't changed any heuristic rules.

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]

% Saturates

sum(singles)

% In 'double', doesn't saturate

sum(singles, 'double')

int8s = [intmax('int8'), intmax('int8'), intmin('int8')]

% Default summation in 'double' - doesn't saturate

sum(int8s)

% Saturates

sum(int8s, 'native')

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 =

4.9999e+06

Elapsed time is 0.005212 seconds.

>> maxNumCompThreads=4;

>> tic; s4=sum(a), toc

s4 =

4.9999e+06

Elapsed time is 0.005153 seconds.

>> s1-s4

ans =

0

>> ss=sum(sort(a));

>> ss-s1

ans =

3.7253e-09

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.

##### 7 Comments

Bruno Luong
on 9 Nov 2021

### See Also

### Community Treasure Hunt

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

Start Hunting!