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

Conversation

JeffreySarnoff
Copy link
Member

No description provided.

@@ -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.

sum_kbn(f, A) = f.(A)
function sum_kbn(f,A)
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

@ararslan
Copy link
Member

It would also be great if you could add some tests for this functionality. Thanks for taking this on!

@@ -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~

@JeffreySarnoff
Copy link
Member Author

@stevengj @ararslan I think this does it for now -- let me know if something should be changed.

function sum_kbn(f::Function, A)
f === identity && return sum_kbn(A)
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.

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)?

@@ -115,6 +115,7 @@ function sum_kbn(A)
end

function sum_kbn(f::Function, A)
f === identity && return sum_kbn(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 is backwards. sum_kbn(A) should be implemented in terms of sum_kbn(identity, A), not the other way around. This way there will be only one implementation of the sum.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean that I should follow this pattern?

function sum_kbn(f, A)
     ...
end
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.

later comment says "yes" -- I will do that

@@ -113,4 +114,27 @@ function sum_kbn(A)
s - c
end

function sum_kbn(f::Function, 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 needn't be a function; for example, one could also pass a type.

Copy link
Member Author

Choose a reason for hiding this comment

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

I know -- I do not know how to annotate Function or TypeConstructor. When I had it as sum_kbn(f, A) things got murky with sum_kbn(identity, A) and there was no clean dispatch resolution. What is the correct way to approach this while following the guide that sum_kbn(A) should be defined in terms of sum_kbn(identity, A)?

Copy link
Member

Choose a reason for hiding this comment

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

I think the issue there is that the definition with identity was backwards. It should be sum_kbn(A) = sub_kbn(identity, A), with sum_kbn(f, A) as the only long-form definition of the function. That should remove any ambiguities.

Copy link
Member Author

Choose a reason for hiding this comment

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

does the application of identity disappear always?

Copy link
Member Author

Choose a reason for hiding this comment

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

e.g. map(identity, [....])

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the function call should get optimized out

@simonbyrne
Copy link
Member

I've managed to do this largely in #7, though don't have tests yet.

@JeffreySarnoff
Copy link
Member Author

@simonbyrne 👍

@JeffreySarnoff
Copy link
Member Author

what are
Base.r_promote_type(::typeof(plus_kbn), ::Type{T}) where {T<:AbstractFloat} = TwicePrecision{T}
Base.mr_empty(f, ::typeof(plus_kbn), T) = TwicePrecision{T}
and what is their equivalent for v0.7?

@JeffreySarnoff
Copy link
Member Author

is this correct:

    T = Base.return_types(f, (@default_eltype(typeof(A)),))[1]

@stevengj
Copy link
Member

See how the reduce code does it in base. If the collection is non empty, you shouldn’t use inference at all

@JeffreySarnoff
Copy link
Member Author

I do not know what more I can do to make coveralls happy. I put in what it seems to want -- then again I am not conversant with coveralls.

@JeffreySarnoff
Copy link
Member Author

this is ok to close

@JeffreySarnoff
Copy link
Member Author

hmm -- this is applesauce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants