From 4a3d93872496decc914b58fcad83c8059d749c92 Mon Sep 17 00:00:00 2001 From: Martin Holters Date: Wed, 24 Jan 2018 09:04:53 +0100 Subject: [PATCH] Fix `argmin`/`argmax` for non-vectors Make sure `argmin` and `argmax` yield the correct return type when falling back to `indmin` and `indmax`, at least for `AbstractArray` and `Associative` arguments. --- src/Compat.jl | 13 +++++++++++-- test/runtests.jl | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Compat.jl b/src/Compat.jl index 1e1a5d1d2ae95..6c677f4f117a3 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1311,8 +1311,17 @@ if !isdefined(Base, :findall) end @static if !isdefined(Base, :argmin) - const argmin = indmin - const argmax = indmax + if VERSION >= v"0.7.0-DEV.1660" # indmin/indmax return key + const argmin = indmin + const argmax = indmax + else + argmin(x::AbstractArray) = CartesianIndex(ind2sub(x, indmin(x))) + argmin(x::AbstractVector) = indmin(x) + argmin(x::Associative) = first(Iterators.drop(keys(x), indmin(values(x))-1)) + argmax(x::AbstractArray) = CartesianIndex(ind2sub(x, indmax(x))) + argmax(x::AbstractVector) = indmax(x) + argmax(x::Associative) = first(Iterators.drop(keys(x), indmax(values(x))-1)) + end export argmin, argmax end diff --git a/test/runtests.jl b/test/runtests.jl index aaf7490609230..dd276d5d1aeb8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1170,7 +1170,11 @@ end # 0.7.0-DEV.3516 @test argmax([10,12,9,11]) == 2 +@test argmax([10 12; 9 11]) == CartesianIndex(1, 2) +@test argmax(Dict(:z=>10, :y=>12, :x=>9, :w=>11)) == :y @test argmin([10,12,9,11]) == 3 +@test argmin([10 12; 9 11]) == CartesianIndex(2, 1) +@test argmin(Dict(:z=>10, :y=>12, :x=>9, :w=>11)) == :x # 0.7.0-DEV.3415 @test findall(x -> x==1, [1, 2, 3, 2, 1]) == [1, 5]