-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Changing argmax
and argmin
behavior to match the mathematical definition
#27639
Comments
That definition and implementation don't depend on I agree this is useful but I don't think |
The answer would have form of some collection of |
We should probably resurrect
separately. |
What is the |
The mathematical definition defines the solution as a set, but for computational reasons it might be best to provide a @nalimilan, thanks for linking those issues (I had hunting for those in my task list for today). I would like to know your thoughts on the issue. I concur |
Well, if we wanted to be fully consistent Something I would really have liked to change is renaming |
On the decision of returning the indices or subset of domain, The only implementation of this functionality using My number one concern is to avoid having a misnamed concept similarly to the |
The mathematical meaning of The only question in my mind is whether it would be better to leave Bottom line: if you're interested in indices, you can always use the indices as your collection and make indexing part of the function you're maximizing. You can kind of go in the other direction since you can always use an index to extract an element back out of an indexed collection, but if we're choosing between |
Good points. The edge cases seem to suggest the best design would be a version of |
Is that |
I am perfectly happy with having that option default to a single value (i.e., |
Doing |
Using the implementation at the start of this of this post it wouldn't be costly at all. However, I now think having the keyword argument would be best since the implementation could be adapted to only update upon finding a new frontier as opposed to expanding the solution set when new images are at the frontier (i.e., the efficiency gains would come from knowing the desired output at the start). With a bit of guidance I could prep a PR for these changes after deciding on the specifics. |
I think we have different notions of what "costly" means. Your implementation allocates two |
Here's a sketch implementation of the two-arg function argmax(f, itr)
r = iterate(itr)
r === nothing && error("empty collection")
m, state = r
f_m = f(m)
while true
r = iterate(itr, state)
r === nothing && break
x, state = r
f_x = f(x)
isless(f_m, f_x) || continue
m, f_m = x, f_x
end
return m
end The iterable |
The reason for the two collections are different: (1) keeps track of whether the image is known (it skips the computation of |
|
We could just ask the users to pass |
g(a, b) = argmax(x -> -abs(x-3), min(a,b):max(a,b)) then the native code for a call like cmpq %rdi, %rsi
movq %rdi, %r8
cmovleq %rsi, %r8
cmovlq %rdi, %rsi
cmpq %rsi, %r8
jne L23
movq %r8, %rax
retq
L23:
leaq -3(%r8), %rax
movl $3, %ecx
subq %r8, %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
negq %rcx
movq %r8, %rax
L48:
movl $2, %edi
subq %r8, %rdi
leaq -2(%r8), %r9
leaq 1(%r8), %rdx
testq %r9, %r9
cmovnsq %r9, %rdi
negq %rdi
cmpq %rdi, %rcx
cmovlq %rdx, %rax
cmovlq %rdi, %rcx
movq %rdx, %r8
cmpq %rsi, %rdx
jne L48
retq It's all just simple integer instructions, comparisons and jumps. If there was any allocation whatsoever in the definition of |
I think we can close this after discussion at #27612 (comment) which leaves us in the situation that the only actionable change is adding the two-argument |
Technically there's no ambiguity, but am I the only one to find it weird that I'm starting to regret having renamed |
It's a little unexpected but having |
I, too, find it weird. Also, there is no |
I concur with @nalimilan here. I find I think fundamentally things would be easier if A separate issue seems to be whether it should return a single value, a list or a set. On that I concur with @StefanKarpinski: a single element seems the most common and useful case. |
Fixes JuliaLang#27613. Related: JuliaLang#27639, JuliaLang#27612, JuliaLang#34674. Thanks to @tkf, @StefanKarpinski and @drewrobson for their assistance with this PR.
Fixes JuliaLang#27613. Related: JuliaLang#27639, JuliaLang#27612, JuliaLang#34674. Thanks to @tkf, @StefanKarpinski and @drewrobson for their assistance with this PR.
Fixes JuliaLang#27613. Related: JuliaLang#27639, JuliaLang#27612, JuliaLang#34674. Thanks to @tkf, @StefanKarpinski and @drewrobson for their assistance with this PR.
Fixes JuliaLang#27613. Related: JuliaLang#27639, JuliaLang#27612, JuliaLang#34674. Thanks to @tkf, @StefanKarpinski and @drewrobson for their assistance with this PR.
Fixes JuliaLang#27613. Related: JuliaLang#27639, JuliaLang#27612, JuliaLang#34674. Thanks to @tkf, @StefanKarpinski and @drewrobson for their assistance with this PR.
Defines a descending total order, `isgreater` (not exported), where unordered values like NaNs and missing are last. This makes defining min, argmin, etc, simpler and more consistent. Also adds 2-arg versions of findmax/min, argmax/min. Defines and exports the `isunordered` predicate for testing whether a value is unordered like NaN and missing. Fixes #27613. Related: #27639, #27612, #34674. Thanks to @tkf, @StefanKarpinski and @drewrobson for their assistance with this PR. Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Takafumi Arakaki <[email protected]>
Defines a descending total order, `isgreater` (not exported), where unordered values like NaNs and missing are last. This makes defining min, argmin, etc, simpler and more consistent. Also adds 2-arg versions of findmax/min, argmax/min. Defines and exports the `isunordered` predicate for testing whether a value is unordered like NaN and missing. Fixes JuliaLang#27613. Related: JuliaLang#27639, JuliaLang#27612, JuliaLang#34674. Thanks to @tkf, @StefanKarpinski and @drewrobson for their assistance with this PR. Co-authored-by: Jameson Nash <[email protected]> Co-authored-by: Takafumi Arakaki <[email protected]>
The definition of
argmax
from Wikipedia,Given a collection of elements (domain), which map to an ordered set (range), what is the subset containing those elements in the domain whose image is the frontier.
Currently,
argmax
is actuallyindmax
which is defined for indexed collections and returns the index of the first encountered element whose image is equal to the frontier. The generalization ofkeymax
(discussed, but not implemented) extends the supported collections from indexed topairs
-implemented collections. This is the result of renamingindmax
toargmax
. A current proposal is forargmax
to be defined forGenerator
(#27613),with that change, we could fix the implementation with something like
and call it with the following syntax
The text was updated successfully, but these errors were encountered: