Skip to content

sum_kbn(fn, A) (issue 3, in part) #6

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

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ These functions were formerly part of Julia's Base library.
```julia
julia> using KahanSummation

julia> vec = [1.0, 1.0e16, -1.0e16, -0.5];
julia> avec = [1.0, 1.0e16, -1.0e16, -0.5];

julia> sum(vec), sum_kbn(vec)
julia> sum(avec), sum_kbn(avec)
(-0.5, 0.5)

julia> vec = [1.0, 1.0e16, 1.0, -1.0e16];
julia> avec = [1.0, 1.0e16, 1.0, -1.0e16];

julia> cumsum_kbn(vec) .- cumsum(vec)
julia> cumsum_kbn(avec) .- cumsum(avec)
4-element Array{Float64,1}:
0.0
0.0
Expand Down
7 changes: 5 additions & 2 deletions src/KahanSummation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ function cumsum_kbn(v::AbstractVector{T}) where T<:AbstractFloat
end

"""
sum_kbn(A)
sum_kbn([f,] A)

Return the sum of all elements of `A`, using the Kahan-Babuska-Neumaier compensated
summation algorithm for additional accuracy.
summation algorithm for additional accuracy. When a function `f` is supplied, the
result of calling f on each element of `A` is summed.
"""
function sum_kbn(A)
T = @default_eltype(typeof(A))
Expand All @@ -113,4 +114,6 @@ function sum_kbn(A)
s - c
end

sum_kbn(f, A) = f.(A)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite correct; f is applied elementwise to A but the result isn't summed. Perhaps you meant sum_kbn(f.(A))?

Even that will make two passes over the array, so we're likely able to do better. I'm not sure this is entirely correct either, but something like this would be a start:

diff --git a/src/KahanSummation.jl b/src/KahanSummation.jl
index fde6700..26a7535 100644
--- a/src/KahanSummation.jl
+++ b/src/KahanSummation.jl
@@ -91,22 +91,23 @@ end
 Return the sum of all elements of `A`, using the Kahan-Babuska-Neumaier compensated
 summation algorithm for additional accuracy.
 """
-function sum_kbn(A)
+function sum_kbn(f, A)
     T = @default_eltype(typeof(A))
-    c = promote_sys_size_add(zero(T)::T)
+    c = f(promote_sys_size_add(zero(T)::T))
     i = start(A)
     if done(A, i)
         return c
     end
     Ai, i = next(A, i)
-    s = Ai - c
+    s = f(Ai) - c
     while !(done(A, i))
         Ai, i = next(A, i)
-        t = s + Ai
-        if abs(s) >= abs(Ai)
-            c -= ((s-t) + Ai)
+        fAi = f(Ai)
+        t = s + fAi
+        if abs(s) >= abs(fAi)
+            c -= ((s-t) + fAi)
         else
-            c -= ((Ai-t) + s)
+            c -= ((fAi-t) + s)
         end
         s = t
     end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is view applicable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, not exactly, I don't think, since the issue is iterating over the input multiple times, whereas view would be useful to avoid making copies.


end # module