From ff29bcc211b71b9741e4cc7d7a8c850d8b315727 Mon Sep 17 00:00:00 2001 From: Kevin Squire Date: Thu, 19 Jun 2014 15:10:16 -0700 Subject: [PATCH] Added findminmax, indminmax; min/max bikeshedding * For parity with findmin/indmin, findmax/indmax * indmin/indmax -> argmin, argmax --- base/array.jl | 25 +++++++++++++++++++++++-- base/deprecated.jl | 2 ++ base/exports.jl | 6 ++++-- doc/stdlib/base.rst | 28 ++++++++++++++++++++++++++-- test/arrayops.jl | 8 ++++++-- test/euler.jl | 2 +- test/perf/perfcomp.jl | 4 ++-- 7 files changed, 64 insertions(+), 11 deletions(-) diff --git a/base/array.jl b/base/array.jl index fcaa3009a2c9e..fa9fb4175029f 100644 --- a/base/array.jl +++ b/base/array.jl @@ -1153,8 +1153,29 @@ function findmin(a) return (m, mi) end -indmax(a) = findmax(a)[2] -indmin(a) = findmin(a)[2] +function findminmax(a) + if isempty(a) + error("array must be non-empty") + end + vmin = vmax = a[1] + imin = imax = 1 + for i = 2:length(a) + ai = a[i] + if ai < vmin || vmin!=vmin + vmin = ai + imin = i + end + if ai > vmax || vmax!=vmax + vmax = ai + imax = i + end + end + return ((vmin,imin), (vmax,imax)) +end + +argmax(a) = findmax(a)[2] +argmin(a) = findmin(a)[2] +argminmax(a) = (x = findminmax(a); (x[1][2], x[2][2])) # similar to Matlab's ismember # returns a vector containing the highest index in b for each value in a that is a member of b diff --git a/base/deprecated.jl b/base/deprecated.jl index 94b1e52681814..5046d1c73db11 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -159,3 +159,5 @@ scale!{T<:Base.LinAlg.BlasReal}(X::Array{T}, s::Complex) = error("scale!: Cannot @deprecate which(f::Callable, args...) @which f(args...) @deprecate rmdir rm +@deprecate indmin argmin +@deprecate indmax argmax diff --git a/base/exports.jl b/base/exports.jl index 21fcddd0eaf3f..f85cd4ce7ce64 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -486,6 +486,9 @@ export zeta, # arrays + argmax, + argmin, + argminmax, bitbroadcast, broadcast!, broadcast!_function, @@ -516,6 +519,7 @@ export findin, findmax, findmin, + findminmax, findn, findnext, findnz, @@ -528,8 +532,6 @@ export hvcat, ind2sub, indexin, - indmax, - indmin, invperm, ipermute!, ipermutedims, diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index dcbca0cc2ff97..ca872d913d6c9 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -661,32 +661,56 @@ Iterable Collections Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple. -.. function:: indmax(itr) -> Integer +.. function:: argmax(itr) -> Integer Returns the index of the maximum element in a collection. -.. function:: indmin(itr) -> Integer + See also: :func:`maximum`, :func:`findmax` + +.. function:: argmin(itr) -> Integer Returns the index of the minimum element in a collection. + See also: :func:`minimum`, :func:`findmin` + +.. function:: argminmax(itr) -> (Integer, Integer) + + Returns the index of the minimum and maximum elements in a collection. + + See also: :func:`minimum`, :func:`findmin` + .. function:: findmax(itr) -> (x, index) Returns the maximum element and its index. + See also: :func:`maximum`, :func:`argmax` + .. function:: findmax(A, dims) -> (maxval, index) For an array input, returns the value and index of the maximum over the given dimensions. + See also: :func:`maximum`, :func:`argmax` + .. function:: findmin(itr) -> (x, index) Returns the minimum element and its index. + See also: :func:`minimum`, :func:`argmin` + .. function:: findmin(A, dims) -> (minval, index) For an array input, returns the value and index of the minimum over the given dimensions. + See also: :func:`minimum`, :func:`argmin` + +.. function:: findminmax(itr) -> ((x, index),(y,index)) + + Returns the minimum and maximum elements and their indices. + + See also: :func:`extrema`, :func:`argminmax` + .. function:: maxabs(itr) Compute the maximum absolute value of a collection of values. diff --git a/test/arrayops.jl b/test/arrayops.jl index b6b564fc4f152..fc306de57d9eb 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -298,14 +298,18 @@ end @test isequal(a,findn(z)) #argmin argmax -@test indmax([10,12,9,11]) == 2 -@test indmin([10,12,9,11]) == 3 +@test argmax([10,12,9,11]) == 2 +@test argmin([10,12,9,11]) == 3 +@test argminmax([10,12,9,11]) == (3,2) @test findmin([NaN,3.2,1.8]) == (1.8,3) @test findmax([NaN,3.2,1.8]) == (3.2,2) +@test findminmax([NaN,3.2,1.8]) == ((1.8,3),(3.2,2)) @test findmin([NaN,3.2,1.8,NaN]) == (1.8,3) @test findmax([NaN,3.2,1.8,NaN]) == (3.2,2) +@test findminmax([NaN,3.2,1.8,NaN]) == ((1.8,3),(3.2,2)) @test findmin([3.2,1.8,NaN,2.0]) == (1.8,2) @test findmax([3.2,1.8,NaN,2.0]) == (3.2,1) +@test findminmax([3.2,1.8,NaN,2.0]) == ((1.8,2),(3.2,1)) ## permutedims ## diff --git a/test/euler.jl b/test/euler.jl index 04fd4f3b29c9b..b5184787e5db9 100644 --- a/test/euler.jl +++ b/test/euler.jl @@ -233,7 +233,7 @@ function euler14(m) d -= 1 end end - indmax(c) + argmax(c) end @test euler14(999999) == 837799 diff --git a/test/perf/perfcomp.jl b/test/perf/perfcomp.jl index 8bd06b1b51607..c430015ab4b8d 100644 --- a/test/perf/perfcomp.jl +++ b/test/perf/perfcomp.jl @@ -32,8 +32,8 @@ function main() end println() - minname = names[indmin(change)] - maxname = names[indmax(change)] + minname = names[argmin(change)] + maxname = names[argmax(change)] minstd = baseline[minname][4]/baseline[minname][3] maxstd = baseline[maxname][4]/baseline[maxname][3]