-
-
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
argmax
behaves differently from other higher-order functions
#48502
Comments
IMO, the correct 2.0 solution is getting rid of all the other ones like |
AFAICT it's just a specialization bottleneck. We could fix that today, but the cost is longer compile times. |
But that wouldn't change the fact that |
In my opinion, the methods like |
Also #41339 with some summarized discussion of the previous discussions |
Given that the point made by @oscardssmith and @jakobnissen is natural but misses the main point, maybe it's worth thinking about this way: how would you express the 1-arg method in terms of the 2-arg method? The answer is argmax(itr) = argmax(idx -> itr[idx], CartesianIndices(itr)) Note that the |
I suggested something similar, though slightly less general, in one of those posts. |
That's totally a footgun! If starting from scratch: an intuitive, unambiguous, and consistent interface could be |
This has been discussed many times. I would summarize as
|
Agreed that |
I'm really surprised to see people complain about But even as an experienced user I find those functions really convenient and natural. |
@MasonProtter If Julia had concise reduction syntax, it could be easier to use even than |
argmax
has two meanings:argmax(indexable)
returns the indexi
for whichindexable[i]
is maximumargmax(f, itr)
returns thex∈itr
which achieves the maximum value off(x)
Both of these are useful and well-defined operations. But they should be considered with respect to Julia's overall consistency: it's worth asking how
argmax(f, itr)
compares to other such functions that accept a "mapping" functionf
as an input. For example,sum(f, itr)
computessum(f(x) for x in itr)
,maximum(f, itr)
computesmaximum(f(x) for x in itr)
, and so on. One simple consequence is that these functions have a very natural property:sum(identity, itr) == sum(itr)
.So, is
argmax(identity, a) == argmax(a)
?Nope. How about "is
argmax(f, itr)
equivalent toargmax(f(x) for x in itr)
"?Again nope: if it did, it would return the index
i
such thata[i] == maximum(f, a)
.Personally, I think this is a footgun. If we ever decide to go to Julia 2.0, I think we should reconsider whether these two methods belong to the same function.
One potential solution is that we could have
argmax(itr)
become synonymous withargmax(identity, itr)
and embrace the newer meaning ofargmax
. Then, noting thatfindmax(f, a)
does act in a manner consistent with other mapping functions, we could just eliminate the original meaning ofargmax
, asargmax
seems to sometimes be used as a substitute forfindmax(A)[2]
. But I'm less fond of having to remember whether the index is the first or second output; hence we could switch to having it return aNamedTuple
(we might be able to do that even now).The text was updated successfully, but these errors were encountered: