Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Numerical-stable sum (similar to math.fsum) (Trac #1855) #2448

@numpy-gitbot

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/1855 on 2011-06-02 by trac user ling, assigned to unknown.

For some applications, having a numerical-stable sum is critical. What I mean can be well illustrated in the examples below:

math.fsum([1, 1e-10] * 1000), numpy.sum([1, 1e-10] * 1000)
(1000.0000001, 1000.0000001000171)
math.fsum([1, 1e100, 1, -1e100] * 10000), numpy.sum([1, 1e100, 1, -1e100] * 10000)
(20000.0, 0.0)

Here math.fsum is a numerical stable sum introduced in Python 2.6, which uses the Shewchuk algorithm (seems to be described in http://code.activestate.com/recipes/393090/).

Other complaints about the plain vanilla numpy.sum could be found in a recent thread: http://article.gmane.org/gmane.comp.python.numeric.general/42756.

I hope numpy.sum can also implement something similar (e.g., Shewchuk algorithm or Kahan summation algorithm or partial sum). We could add an optional argument "stable=True/False" to numpy.sum and let the user decides whether they want a more accuracy (but potentially slower) version.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions