Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Gabriel Genellina
En Mon, 05 May 2008 05:08:15 -0300, Raymond Hettinger <[EMAIL PROTECTED]> escribió: On May 3, 9:50 am, Szabolcs Horvát <[EMAIL PROTECTED]> wrote: I did the following calculation:  Generated a list of a million random numbers between 0 and 1, constructed a new list by subtracting the mean valu

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Gabriel Genellina
En Mon, 05 May 2008 11:08:02 -0300, Szabolcs <[EMAIL PROTECTED]> escribió: On May 5, 12:24 pm, Duncan Booth <[EMAIL PROTECTED]> wrote: Szabolcs <[EMAIL PROTECTED]> wrote: > On May 5, 9:37 am, Szabolcs Horvát <[EMAIL PROTECTED]> wrote: >> Gabriel Genellina wrote: >> > Python doesn't require __a

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Robert Kern
Gabriel Genellina wrote: Python doesn't require __add__ to be associative, so this should not be used as a general sum replacement. But if you know that you're adding floating point numbers you can use whatever algorithm best fits you. Or use numpy arrays; I think they implement Kahan summatio

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Rhamphoryncus
On May 3, 4:31 pm, Thomas Dybdahl Ahle <[EMAIL PROTECTED]> wrote: > On Sat, 2008-05-03 at 21:37 +, Ivan Illarionov wrote: > > On Sat, 03 May 2008 20:44:19 +0200, Szabolcs Horvát wrote: > > > > Arnaud Delobelle wrote: > > > >> sum() works for any sequence of objects with an __add__ method, not >

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Szabolcs
On May 5, 12:24 pm, Duncan Booth <[EMAIL PROTECTED]> wrote: > Szabolcs <[EMAIL PROTECTED]> wrote: > > On May 5, 9:37 am, Szabolcs Horvát <[EMAIL PROTECTED]> wrote: > >> Gabriel Genellina wrote: > > >> > Python doesn't require __add__ to be associative, so this should > >> > not be > > used as a gen

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Duncan Booth
Szabolcs <[EMAIL PROTECTED]> wrote: > On May 5, 9:37 am, Szabolcs Horvát <[EMAIL PROTECTED]> wrote: >> Gabriel Genellina wrote: >> >> > Python doesn't require __add__ to be associative, so this should >> > not be > used as a general sum replacement. >> >> It does not _require_ this, but using an

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Duncan Booth
"Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Sun, 04 May 2008 12:58:25 -0300, Duncan Booth > <[EMAIL PROTECTED]> escribió: > >> Szabolcs Horvát <[EMAIL PROTECTED]> wrote: >> >>> I thought that it would be very nice if the built-in sum() function >>> used this algorithm by default. Has th

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Szabolcs
On May 5, 9:37 am, Szabolcs Horvát <[EMAIL PROTECTED]> wrote: > Gabriel Genellina wrote: > > > Python doesn't require __add__ to be associative, so this should not be > > used as a general sum replacement. > > It does not _require_ this, but using an __add__ that is not commutative > and associati

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Raymond Hettinger
> > > However I find the seccond argument hack ugly. Does the sum way have any > > > performance advantages over the reduce way? > > > Yes, sum() is faster: ... > Not really; you measure the import and the attribute access time in > the second case. sum() is 2x faster for adding numbers only: Try

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Raymond Hettinger
On May 3, 9:50 am, Szabolcs Horvát <[EMAIL PROTECTED]> wrote: > I did the following calculation:  Generated a list of a million random > numbers between 0 and 1, constructed a new list by subtracting the mean > value from each number, and then calculated the mean again. > > The result should be 0,

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Szabolcs Horvát
Gabriel Genellina wrote: Python doesn't require __add__ to be associative, so this should not be used as a general sum replacement. It does not _require_ this, but using an __add__ that is not commutative and associative, or has side effects, would qualify as a serious misuse, anyway. So I

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Szabolcs Horvát
Duncan Booth wrote: Szabolcs Horvát <[EMAIL PROTECTED]> wrote: 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

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-04 Thread Gabriel Genellina
En Sun, 04 May 2008 12:58:25 -0300, Duncan Booth <[EMAIL PROTECTED]> escribió: > Szabolcs Horvát <[EMAIL PROTECTED]> wrote: > >> 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 disadvan

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-04 Thread Duncan Booth
Szabolcs Horvát <[EMAIL PROTECTED]> wrote: > 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 languag

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-04 Thread hdante
On May 3, 7:05 pm, sturlamolden <[EMAIL PROTECTED]> wrote: > On May 3, 10:13 pm, hdante <[EMAIL PROTECTED]> wrote: > > >  I believe that moving this to third party could be better. What about > > numpy ? Doesn't it already have something similar ? > > Yes, Kahan summation makes sence for numpy arra

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Ivan Illarionov
On Sat, 03 May 2008 17:43:57 -0700, George Sakkis wrote: > On May 3, 7:12 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: >> On Sun, 04 May 2008 00:31:01 +0200, Thomas Dybdahl Ahle wrote: >> > On Sat, 2008-05-03 at 21:37 +, Ivan Illarionov wrote: >> >> On Sat, 03 May 2008 20:44:19 +0200, Szabol

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Ivan Illarionov
On Sun, 04 May 2008 00:31:01 +0200, Thomas Dybdahl Ahle wrote: > On Sat, 2008-05-03 at 21:37 +, Ivan Illarionov wrote: >> On Sat, 03 May 2008 20:44:19 +0200, Szabolcs Horvát wrote: >> >> > Arnaud Delobelle wrote: >> >> >> >> sum() works for any sequence of objects with an __add__ method, not

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread George Sakkis
On May 3, 7:12 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > On Sun, 04 May 2008 00:31:01 +0200, Thomas Dybdahl Ahle wrote: > > On Sat, 2008-05-03 at 21:37 +, Ivan Illarionov wrote: > >> On Sat, 03 May 2008 20:44:19 +0200, Szabolcs Horvát wrote: > > >> > Arnaud Delobelle wrote: > > >> >> sum

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Thomas Dybdahl Ahle
On Sat, 2008-05-03 at 21:37 +, Ivan Illarionov wrote: > On Sat, 03 May 2008 20:44:19 +0200, Szabolcs Horvát wrote: > > > Arnaud Delobelle wrote: > >> > >> sum() works for any sequence of objects with an __add__ method, not > >> just floats! Your algorithm is specific to floats. > > > > Thi

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread sturlamolden
On May 3, 10:13 pm, hdante <[EMAIL PROTECTED]> wrote: > I believe that moving this to third party could be better. What about > numpy ? Doesn't it already have something similar ? Yes, Kahan summation makes sence for numpy arrays. But the problem with this algorithm is optimizing compilers. The

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Ivan Illarionov
On Sat, 03 May 2008 20:44:19 +0200, Szabolcs Horvát wrote: > Arnaud Delobelle wrote: >> >> sum() works for any sequence of objects with an __add__ method, not >> just floats! Your algorithm is specific to floats. > > This occurred to me also, but then I tried > > sum(['abc', 'efg'], '') Inter

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Erik Max Francis
Torsten Bronger wrote: No, the above expression should yield ''+'abc'+'efg', look for the signature of sum in the docs. You're absolutely right, I misread it. Sorry about that. -- Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && A

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Torsten Bronger
Hallöchen! Erik Max Francis writes: > Szabolcs Horvát wrote: > >> Arnaud Delobelle wrote: >>> >>> sum() works for any sequence of objects with an __add__ method, not >>> just floats! Your algorithm is specific to floats. >> >> This occurred to me also, but then I tried >> >> sum(['abc', 'efg'],

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Erik Max Francis
Szabolcs Horvát wrote: Arnaud Delobelle wrote: sum() works for any sequence of objects with an __add__ method, not just floats! Your algorithm is specific to floats. This occurred to me also, but then I tried sum(['abc', 'efg'], '') and it did not work. Or is this just a special exceptio

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Arnaud Delobelle
Szabolcs Horvát <[EMAIL PROTECTED]> writes: > Arnaud Delobelle wrote: >> >> sum() works for any sequence of objects with an __add__ method, not >> just floats! Your algorithm is specific to floats. > > This occurred to me also, but then I tried > > sum(['abc', 'efg'], '') > > and it did not work.

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread hdante
On May 3, 3:44 pm, Szabolcs Horvát <[EMAIL PROTECTED]> wrote: > Arnaud Delobelle wrote: > > > sum() works for any sequence of objects with an __add__ method, not > > just floats!  Your algorithm is specific to floats. > > This occurred to me also, but then I tried > > sum(['abc', 'efg'], '') > > an

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Szabolcs Horvát
Arnaud Delobelle wrote: sum() works for any sequence of objects with an __add__ method, not just floats! Your algorithm is specific to floats. This occurred to me also, but then I tried sum(['abc', 'efg'], '') and it did not work. Or is this just a special exception to prevent the misuse

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Nick Craig-Wood
Szabolcs Horvát <[EMAIL PROTECTED]> wrote: > I did the following calculation: Generated a list of a million random > numbers between 0 and 1, constructed a new list by subtracting the mean > value from each number, and then calculated the mean again. > > The result should be 0, but of cours

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Ivan Illarionov
On Sat, 03 May 2008 18:50:34 +0200, Szabolcs Horvát wrote: > I did the following calculation: Generated a list of a million random > numbers between 0 and 1, constructed a new list by subtracting the mean > value from each number, and then calculated the mean again. > > The result should be 0, b

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Arnaud Delobelle
Szabolcs Horvát <[EMAIL PROTECTED]> 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): >

Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-03 Thread Szabolcs Horvát
I did the following calculation: Generated a list of a million random numbers between 0 and 1, constructed a new list by subtracting the mean value from each number, and then calculated the mean again. The result should be 0, but of course it will differ from 0 slightly because of rounding