-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
implement a better summation algorithm #199
Comments
By pairwise summation, I assume you mean recursive pairwise, as in this sort of thing:
|
Kahan summation looks promising since it looks like it can be done with just a couple extra arithmetic ops on values already in registers. |
Maybe a keyword option for this: |
sorry about that -- this is the part that matters # bettersum.jl
#
# bettersum(Vector{Float64}) is more accurate and faster than kahansum()
#
# Jeffrey Sarnoff on 2012-Jul-05
# Kahan's compensated summation
# W. Kahan.
# Further remarks on reducing truncation erros.
# Comm. ACM, 8:40, 1965
function kahansum(x)
n = length(x)
if (n==0) return(0) end
s = x[1]
c = 0
for i in 2:n
y = x[i] - c
t = s + y
c = (t - s) -y
s = t
end
s
end
# Kahan and Babuska summation, Neumaier variant
# A. Neumaier.
# Rundungsfehleranalyse einiger Verfahren zur Summation endlicher Summen.
# Math. Mechanik, 54:39–51, 1974.
function bettersum(x)
n = length(x)
if (n == 0) return(0) end
s = x[1]
c = 0
for i in 2:n
t = s + x[i]
if ( abs(s) >= abs(x[i]) )
c += ( (s-t) + x[i] )
else
c += ( (x[i]-t) + s )
end
s = t
end
s + c
end
# test vector is Tim Peters'
# truesum( vec ) == 2_000.0
vec = [1,1e100,1,-1e100]*1000
sum(vec) == 0.0
kahansum(vec) == 0.0
kbnsum(vec) == 2_000.0
[pao: syntax highlights] |
Is kbnsum always better? Maybe we should use this by default for float arrays. |
kbnsum (above implemented as bettersum, written as kbnsum --truer name-- in the test) I recommend using kbnsum for Julia until there is compelling reason to use a different |
written carefully it is no more than 20% slower closes JuliaLang#199
Now that we have optional arguments, perhaps |
If so, On Thu, Apr 18, 2013 at 5:14 AM, Viral B. Shah notifications@git.luolix.topwrote:
|
e.g.
|
PS. For interested parties on this thread, note that we now use pairwise summation (#4039), which is often surprisingly close to Kahan summation for large arrays, but without the performance penalty. |
Don't export String since it is already exported by Base
sum
should use a better algorithm, or at least we should provide an alternative function that does a better job. Candidates include Kahan summation (http://en.wikipedia.org/wiki/Kahan_summation_algorithm) and pairwise summation.The text was updated successfully, but these errors were encountered: