Is numpy.sum executed in such a way as to avoid numerical errors?

It is well known that adding numbers can lead to numerical errors (for example, if the first number is really large, while there are many other small numbers).

This can be solved by combining the numbers in a non-direct way. See for example: https://en.wikipedia.org/wiki/Kahan_summation_algorithm

Is numpy.sum used this way to avoid numerical errors?

+6
source share
1 answer

A search on numpy kahan revealed a closed error / problem

https://github.com/numpy/numpy/issues/2448 Numerically stable amount (similar to math.fsum)

I did not read it in detail. Pay attention to the link math.fsum

 fsum(iterable) Return an accurate floating point sum of values in the iterable. Assumes IEEE-754 floating point arithmetic. (from the Python math docs) Return an accurate floating point sum of values in the iterable. Avoids loss of precision by tracking multiple intermediate partial sums 

And the SO question, with some discussion, but not the real answer:

Is there numpy numerical stability documentation?

Simple comparison:

 In [320]: x=np.ones(100000)/100000 In [321]: sum(x)-1 Out[321]: -1.9162449405030202e-12 In [322]: np.sum(x)-1 Out[322]: 1.3322676295501878e-15 In [323]: math.fsum(x)-1 Out[323]: 0.0 

the corresponding time is 72 ms, 304 ΞΌs, 23.8 ms

np.sum clearly faster; but fsum better than sum , probably due to its specialized implementation of C.

+3
source

All Articles