-
-
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
Unintuitive findmin and findmax #39203
Comments
For the record, |
This was discussed quite a bit. What's implemented is the mathematical definition of argmin/argmax. It's slightly more general than returning an index, since not all collections have indices, e.g. sets or iterators. |
I think it's very unintuitive that in one case it returns the index of max/min, in another case, it returns the value of evaluation. And currently, we don't have a way to get the index of max/min within an array giving a For consistency and mathematical definition, I would suggest deprecate |
The discussion happened at #27613. I agree it's confusing. For consistency with |
As I understand, there are multiple subissues here, and not all of them are about consistency by itself.
2.1. As for comparison with julia> findfirst([false, false, true])
3
julia> findfirst(identity, [false, false, true])
3 # same as above
julia> argmax([false, false, true])
3
julia> argmax(identity, [false, false, true])
true # ... 2.2. Now let's compare finding maximum and sorting. They are both similar as both require that ordering is specified, and maximum is just a common and optimized special case for partial sort. I don't have a perfect solution for these issues. But it seems reasonable to do some of the following (non-breaking!):
|
+1 for just A |
Unpopular opinion: maybe the single-argument version of |
Edit: oh, I realize in my mind |
The problem here is that reducers use |
I'll just add my voice here that it would probably be worthwhile to do something to prevent the surprise that these forms break the established pattern for when there are array first, and function first/array second forms of the same function. Perhaps another solution is for the function domain forms to return different values. For the array forms: What if the function domain forms did this? That way the output is a different type and mistakes would be more obvious. And easy enough to discard the other outputs. That is, the outputs are the equivalent of:
Still a window for confusion if people are extracting values from findmin without inspecting its result, but perhaps a lot clearer what's happening? For those domains for which there are no valid indexes, perhaps |
Or, seen another way: By that logic, maybe the function/array forms should always return the index into the array. OR, it could return a value for each step ( |
triage thinks 1 arg |
Who's "assigned" to work on this for the milestone? Do we need to rebase #7327 for this? |
This issue is marked "closed", but is fixed only partially (by @JeffBezanson's commit): for
Why introduce |
Wow, even a cursory search on JuliaHub and GitHub revealed many packages that effectively define Most widely-used package: https://github.com/JuliaData/SentinelArrays.jl/blob/v1.3.3/src/chainedvector.jl#L771 |
argmin(collection)
will return the index of the smallest element in the collection, however, with the newly added:it returns the value of the minimal element (with evaluation) instead:
And this is not even useful since it's just taking the second element from the returned value of
findmin(f, collection)
:julia/base/reduce.jl
Line 956 in 3d922e4
We should have argmin/argmax to return index instead.
The text was updated successfully, but these errors were encountered: