-
Notifications
You must be signed in to change notification settings - Fork 11
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
Conversation
src/KahanSummation.jl
Outdated
@@ -113,4 +114,6 @@ function sum_kbn(A) | |||
s - c | |||
end | |||
|
|||
sum_kbn(f, A) = f.(A) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is view
applicable?
There was a problem hiding this comment.
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.
src/KahanSummation.jl
Outdated
sum_kbn(f, A) = f.(A) | ||
function sum_kbn(f,A) | ||
T = @default_eltype(typeof(A)) | ||
c = promote_sys_size_add(zero(T)::T) |
There was a problem hiding this comment.
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
It would also be great if you could add some tests for this functionality. Thanks for taking this on! |
src/KahanSummation.jl
Outdated
@@ -113,4 +114,26 @@ function sum_kbn(A) | |||
s - c | |||
end | |||
|
|||
function sum_kbn(f,A) |
There was a problem hiding this comment.
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)
?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh -- I see~
src/KahanSummation.jl
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)?
src/KahanSummation.jl
Outdated
@@ -115,6 +115,7 @@ function sum_kbn(A) | |||
end | |||
|
|||
function sum_kbn(f::Function, A) | |||
f === identity && return sum_kbn(A) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
src/KahanSummation.jl
Outdated
@@ -113,4 +114,27 @@ function sum_kbn(A) | |||
s - c | |||
end | |||
|
|||
function sum_kbn(f::Function, A) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.g. map(identity, [....])
There was a problem hiding this comment.
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
I've managed to do this largely in #7, though don't have tests yet. |
what are |
is this correct:
|
See how the reduce code does it in base. If the collection is non empty, you shouldn’t use inference at all |
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. |
this is ok to close |
hmm -- this is applesauce |
No description provided.