-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Numerical-stable sum (similar to math.fsum) (Trac #1855) #2448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
trac user ling wrote on 2011-06-02 This could be related to #1696. If the requested algorithm (or a better one) is implemented there, it could also be enabled by a "stable=True" parameter. I.e., users have the choice between the default (fast but may be inaccurate) and the stable one. |
I need a fast and accurate sum for large numpy arrays and did a quick test with simply using a higher precision accumulator via Questions to the numpy experts:
In any case: this requested feature for a numerically stable sum in numpy would be very useful to me. |
Your speed measure is items/time, so larger is better? That is rather As for your second question, I think the best answer is a hollow On x86-32, "long double" is an 80-bit fp number stored in 96 bits (so Nowadays we actually could expose a real float128, at least on There have several mailing list discussions about how messed up this My suggestions for using extended precision:
|
On the actual bug's request for a stable summation algorithm: The thing that makes this less than perfectly obvious is that there actually is no That said, it'd be lovely to have an exact algorithm like Shewchuk's (as used in Python's fsum, which might be steal-able code), or a bounded-given-condition-number algorithm like Kahan summation, or both, in numpy. The way to do it would be to define a special-purpose "generalized ufunc" for each, for floating point only. Details of how to do this: This wouldn't be too hard -- Kahan summation in particular is only about 10 lines of code, and hooking it up into numpy not much more. And then we could add a method= argument to np.sum and ndarray.sum that chose whether you wanted to pass on to The other thing we might want to consider is making |
Yes, my speed measure is summed elements in GHz. Thanks for your detailed comments. I guess using a |
So fsum is already available as We could include a note about this function in the docstring (the See Also section?) of np.sum. I think that would be sufficient for many users. |
Please don't use np.math.fsum. That's just an accidental alias for On Thu, Jul 18, 2013 at 7:08 PM, kyleabeauchamp notifications@github.comwrote:
|
Obviously. The point is that there's already a function to do this, and it's already imported. |
It already being imported is probably a bug. A reference to math.fsum would make sense in the np.sum docs, but it's not
|
This has been improved by #3685.
The second sum is heading towards the pathological. Note that the fsum function could be cythonized pretty easily and made part of a specialized package. Given the way x87 and SSE registers can get used on 32 bit systems it is also a bit compiler/cpu dependent, the l0 + hi trick can fail. I'm going to close this as we probably have a good enough solution for most things. |
For everyone interested: I've just released accupy to deal with ill-conditioned sums and dot products. The sums are somewhat slower that |
@nschloe - Thank you! Nice project name. |
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:
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.
The text was updated successfully, but these errors were encountered: