Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
sumlog #48
base: master
Are you sure you want to change the base?
sumlog #48
Changes from 26 commits
581b9df
5725aa9
77aa3d9
88d6fb1
9ecf589
9db732f
76533e1
5747205
0f5a927
977723d
4d488cd
afa5d94
e400483
cc1aaac
07809b7
16ee153
1af518b
0eaf8d2
1f478d0
0807f7a
2a0004d
e5809d1
eb1b524
6fe8bb1
0def97d
3ad95b2
a0a9348
fa667ec
989a111
a54a024
dc48433
39ca989
207fce2
55d125e
bef4728
9572e48
3848848
c4c3e89
e0f410e
23b5bf1
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
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.
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 does not work in general, e.g., if
T === Int
. We need something likeThere 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 you also add a docstring?
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.
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.
No? According to the docstring of
frexp
: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.
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.
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.
@devmotion can you help me understand why you prefer this?
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.
Because it's more efficient in older Julia versions (IIRC it doesn't matter in more recent versions, probably >= 1.6?).
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.
See e.g. JuliaLang/julia#35585
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.
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.
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.
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.
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.
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.
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 agree with
x
instead ofX
. I tried to match the original docs, but now I see they use lowercase. I may have been looking at an old version.I think in general it makes sense to match the Julia docs for
sum
. For arrays, that looks likesum(f, A::AbstractArray; dims)
.We've already discussed that we don't need
<:Real
for dispatch, and it disallows some types that would be useful to include.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 sounds as if the function requires
float(T) <: AbstractFloat
which is not the case.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.
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 it would be better to keep this restricted to
Real
as other methods in LogExpFunctions?In any case, the type parameter is not necessary it seems and the dispatch on
dims
is not needed (see below).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.
Real would be fine. The tests use complex numbers only as examples of things for which
float(x)
isn't an AbstractFloat. (Which in reality could be weird numbers like Dual, etc.)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.
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 it's better to not rely on some internal error function in base, it seems simple enough to throw a custom error message.
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.
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.
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, BTW, was my alternative attempt at error checking. Instead of checking on every iteration, if you ensure that
sig
ends up negative, then the finallog
will throw the right error automatically. I wasn't able to make this as fast, which I'm surprised by, maybe it can be done.I think maybe focusing on the big questions first might be better than immediately nitpicking.
The other big one is how to handle Float16. At the moment it's super-inaccurate. Maybe accumulation should happen in higher precision. Maybe that should happen for Float32 too for accuracy. But I have not run any careful accuracy tests.
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'm strongly against using any internal non-exported functions in Base in a package such as LogExpFunctions. I really think we should just use
exponent
.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 this really needed? Users could just create these iterators themselves and call
sumlog(x)
.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.
Lets you use a do block, which is tidier than writing a generator yourself.
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.
In older Julia versions
should be better
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.
Again, I don't think we should use such internals.
We could just return
or
Unfortunately,
sum(log, x)
doesn't seem to work for empty arrays - but this would even allow us to throw an error for empty arrays to be consistent withsum(log, x)
.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.
Again, no internal function:
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.
Why is this needed?
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.
Because there is no guarantee that half way through the iterator, you won't encounter one non-Float. There's a test for this exact case.
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.
Sure but in this case returning
sum(log, x)
seems sufficient?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.
It's avoiding re-starting the iterator.
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.
But why is it not restarted in the case of the first element? In general, reiterating is not guaranteed to yield the same values.
Maybe we should just error if
float(xi)
is not an AbstractFloat.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.
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.
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 already know sig is an AbstractFloat.
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, if we assume that
typeof(log(sig)) === typeof(sig)
this could be simplified toThe assumption should hold in almost all cases, but it didn't seem harmful to not rely on this fact.
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.
Some surprises:
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.
Calling
Base.Math._exponent_finite_nonzero
works around the NaN problem (although not for BigFloats).Adding
xj < 0 && Base.Math.throw_complex_domainerror(:log, xj)
doesn't seem to cost much speed.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.
Also, note that tests right now only test Float64