-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Updates to findmin/findmax: function application over multidims #45061
Updates to findmin/findmax: function application over multidims #45061
Conversation
This adds support for findmin (and findmax) syntax: findmin(f, A; dims). It is a natural extension of the extant findmin versions, and required only small changes to the core algorithm (findminmax!). However, it was necessary to redirect the 2-arg version in reduce.jl in order to provide a single call site for _findmin(f, A, :).
The 2 fails from SparseArrays/sparsevector. can be fixed by adding However, the errors in SparseArrays made me aware of the possible need to cover I would appreciate input on this -- inspection of the code in SparseArrays reveals that it would not be too difficult to handle. In fact, this PR's code works on the SparseArrays (but it's far less efficient, and the returned type is a SparseMatrix, which is part of the problem).
Thus, I believe that there's a need to handle this a bit more elegantly than just adding a dispatch such as |
Well, other than a test failure due to timeout on x86_x64-apple-darwin, the third revision looks like it does not have an outstanding problems. In hindsight, this was probably better posted as a comment on this issue, thus, I reproduce it here for posterity, with edits for relevance. Current Base interface + dispatches for findmin/findmax:
In the above scheme, the call to Option 1 (not used) of new Base interface + dispatches for
Under those changes, the call to Instead, it seems preferable to keep the original interface + dispatch intact, while incorporating the new ideas. Option 2 (current proposal) of new Base interface + dispatches for
This approach allows our hypothetical user to get the behavior they expect by defining just Thoughts:
Having worked through this with a clear head, I don't see much reason to consider anything other than Option 2. The PR has been amended to reflect this. Edit: add some backticks. |
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 looks useful and seems to be a good implementation. It would need tests and probably NEWS.
base/reducedim.jl
Outdated
@@ -200,6 +200,9 @@ function reducedim_init(f::ExtremaMap, op::typeof(_extrema_rf), A::AbstractArray | |||
return reducedim_initarray(A, region, v0, Tuple{Tmin,Tmax}) | |||
end | |||
|
|||
# Why is this defined for `max` but not also `min`? | |||
# It leads to improper behavior, e.g. maximum(abs2, Matrix{Int}(undef, 0, 1), dims=1) | |||
# Moreover, reducedim_init (above) handles the cases acceptably. |
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 am not sure. If you think you can delete this, go for it.
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.
IIUC, this branch is equivalent with the general fallback except for it allows empty reduction.
We have
mapreduce_empty(f::typeof(abs), ::typeof(max), T) = abs(zero(T))
mapreduce_empty(f::typeof(abs2), ::typeof(max), T) = abs2(zero(T))
in Base. So I believe this is needed to keep consistence.
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.
The odd thing is the return value.
julia> maximum(abs2, Matrix{Int}(undef, 0, 1), dims=1)
1×1 Matrix{Int64}:
0
What led me to this discovery was the potential need to match the behavior with findmax
; currently I do not, as there is no sensible index which could be returned (while also returning a non-empty array).
Whether the lines can be removed, I am not sure. I suspect that a separate PR would be necessary to handle any problems the removal might cause (I'd rather not hold this PR up). Consequently, I am removing the comment form this PR.
Let me know whether the additions/changes in /test/reducedim.jl look OK. If needed, more can be added. |
I have not abandoned this, but insofar as I can tell, the PR is OK. The failing test does not seem to be related to the proposed changes. Incidentally, feedback on the added tests would be helpful. |
This adds support for findmin (and findmax) syntax: findmin(f, A;
dims). It is a natural extension of the extant findmin versions,
and required only small changes to the core algorithm (findminmax!).
However, it was necessary to redirect the 2-arg version in
reduce.jl in order to provide a single call site for
_findmin(f, A, :).