Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- From: Arnaud Delobelle <arnodel@xxxxxxxxxxxxxx>
- Date: Sat, 03 May 2008 18:40:20 +0100
Szabolcs Horvát <szhorvat@xxxxxxxxx> writes:
[...]
A little research shows that Mathematica uses a "compensated
summation" algorithm. Indeed, using the algorithm described at
http://en.wikipedia.org/wiki/Kahan_summation_algorithm
gives us a result around ~ 10^-17:
def compSum(arr):
s = 0.0
c = 0.0
for x in arr:
y = x-c
t = s+y
c = (t-s) - y
s = t
return s
mean = compSum(data)/len(data)
print compSum(x - mean for x in data)/len(data)
I thought that it would be very nice if the built-in sum() function
used this algorithm by default. Has this been brought up before?
Would this have any disadvantages (apart from a slight performance
impact, but Python is a high-level language anyway ...)?
Szabolcs Horvát
sum() works for any sequence of objects with an __add__ method, not
just floats! Your algorithm is specific to floats.
--
Arnaud
.
- Follow-Ups:
- Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- From: Szabolcs Horvát
- Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- References:
- Feature suggestion: sum() ought to use a compensated summation algorithm
- From: Szabolcs Horvát
- Feature suggestion: sum() ought to use a compensated summation algorithm
- Prev by Date: Re: pygame: rect moveto?
- Next by Date: Re: Getting started with pyvtk
- Previous by thread: Feature suggestion: sum() ought to use a compensated summation algorithm
- Next by thread: Re: Feature suggestion: sum() ought to use a compensated summation algorithm
- Index(es):
Relevant Pages
|