in Base.argmax
argmax(r::AbstractRange)
Ranges can have multiple maximal elements. In that case `argmax` will return a maximal index, but **not necessarily** the first one.
also
argmax(itr)
Return the index or key of the maximal element in a collection. If there are multiple maximal elements, then the first one will be returned.
My question:
-
what is the difference between collection
and AbstractRange
-
for vectors, such as a = [1, 7, 7, 6]
, would argmax(a)
always return the first one?
1 Like
These are the docstrings for two separate methods of the argmax
function.
-
The second method is the most general one, called if there are no more specific methods defined. AbstractRange
is an abstract type and the first method is the one that would be called for all subtypes of AbstractRange
. “collection” is just the word chosen by whomever wrote the docstring of the general method, while AbstractRange
is actually a Julia type.
-
You can check whether arrays are subtypes of AbstractRange
:
julia> a = [1,2,3]
julia> typeof(a) <: AbstractRange
false
They are not! So it will not be the first method that is called. If there is no array specific method, then the second method would be called. You can see all methods defined by the currently imported libraries with methods(argmax)
. There is actually an array-specific method, namely argmax(::AbstractArray; dim)
so we should look at its documentation. You can directly see that method with @which argmax([1,2,3])
. I do not have a great way to see the documentation for that specific method, but you can write edit(@which argmax([1,2,3]))
which would actually open an editor with the source code of the function and you will be able to read the docstring directly that way.
For argmax(::AbstractArray; dim)
the documentation happens to be at the bottom of the list here: Collections and Data Structures · The Julia Language but I do not consider it particularly clear (people would be grateful if you submit a pull request to clarify the documentation).
Anyway, the answer to your actual question seems to be “for arrays it returns the first instance of the maximum”. Generally speaking, you should not really look at the documentation of argmax(::AbstractRange)
if you are interested in arrays, because you know arrays are not subtypes of AbstractRange.
1 Like