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 3 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
27 changes: 25 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,26 @@ function sum_kbn(A)
s - c
end

function sum_kbn(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.

Can we merge the two functions? i.e. define sum_kbn(A) = sum_kbn(identity, A)?

Copy link
Member Author

Choose a reason for hiding this comment

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

how is that done? how should I modify latest PR

Copy link
Member Author

Choose a reason for hiding this comment

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

oh -- I see~

T = @default_eltype(typeof(A))
c = promote_sys_size_add(zero(T)::T)
Copy link
Member

Choose a reason for hiding this comment

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

You'll need to apply the function here as well

Copy link
Member

Choose a reason for hiding this comment

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

This is the wrong type. The output of f might not match the eltype of A.

Copy link
Member Author

Choose a reason for hiding this comment

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

how does one obtain the f mapped eltype from eltype(A)?

i = start(A)
if done(A, i)
return c
end
Ai, i = next(A, i)
s = f(Ai) - c
while !(done(A, i))
Ai, i = next(A, i)
fAi = f(Ai); t = s + fAi
if abs(s) >= abs(fAi)
c -= ((s-t) + fAi)
else
c -= ((fAi-t) + s)
end
s = t
end
s - c
end

end # module