Skip to content

Commit 7cbf5cf

Browse files
nalimilanararslan
authored andcommitted
Rename indmin and indmax to argmin and argmax (#25654)
argmin and argmax match the mathematical names as well as those used by Numpy. On the contrary, there does not appear to be any precedent for indmin and indmax, and they do not follow any pattern in Julia either.
1 parent 45a1ec1 commit 7cbf5cf

File tree

15 files changed

+61
-54
lines changed

15 files changed

+61
-54
lines changed

NEWS.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,9 @@ This section lists changes that do not have deprecation warnings.
308308
This avoids stack overflows in the common case of definitions like
309309
`f(x, y) = f(promote(x, y)...)` ([#22801]).
310310

311-
* `findmin`, `findmax`, `indmin`, and `indmax` used to always return linear indices.
311+
* `indmin` and `indmax` have been renamed to `argmin` and `argmax`, respectively ([#25654]).
312+
313+
* `findmin`, `findmax`, `argmin`, and `argmax` used to always return linear indices.
312314
They now return `CartesianIndex`es for all but 1-d arrays, and in general return
313315
the `keys` of indexed collections (e.g. dictionaries) ([#22907]).
314316

@@ -1238,3 +1240,4 @@ Command-line option changes
12381240
[#25545]: https://github.com/JuliaLang/julia/issues/25545
12391241
[#25616]: https://github.com/JuliaLang/julia/issues/25616
12401242
[#25634]: https://github.com/JuliaLang/julia/issues/25634
1243+
[#25654]: https://github.com/JuliaLang/julia/issues/25654

base/array.jl

+10-10
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,7 @@ function findmin(a)
20772077
end
20782078

20792079
"""
2080-
indmax(itr) -> Integer
2080+
argmax(itr) -> Integer
20812081
20822082
Return the index of the maximum element in a collection. If there are multiple maximal
20832083
elements, then the first one will be returned.
@@ -2086,20 +2086,20 @@ The collection must not be empty.
20862086
20872087
# Examples
20882088
```jldoctest
2089-
julia> indmax([8,0.1,-9,pi])
2089+
julia> argmax([8,0.1,-9,pi])
20902090
1
20912091
2092-
julia> indmax([1,7,7,6])
2092+
julia> argmax([1,7,7,6])
20932093
2
20942094
2095-
julia> indmax([1,7,7,NaN])
2095+
julia> argmax([1,7,7,NaN])
20962096
4
20972097
```
20982098
"""
2099-
indmax(a) = findmax(a)[2]
2099+
argmax(a) = findmax(a)[2]
21002100

21012101
"""
2102-
indmin(itr) -> Integer
2102+
argmin(itr) -> Integer
21032103
21042104
Return the index of the minimum element in a collection. If there are multiple minimal
21052105
elements, then the first one will be returned.
@@ -2108,17 +2108,17 @@ The collection must not be empty.
21082108
21092109
# Examples
21102110
```jldoctest
2111-
julia> indmin([8,0.1,-9,pi])
2111+
julia> argmin([8,0.1,-9,pi])
21122112
3
21132113
2114-
julia> indmin([7,1,1,6])
2114+
julia> argmin([7,1,1,6])
21152115
2
21162116
2117-
julia> indmin([7,1,1,NaN])
2117+
julia> argmin([7,1,1,NaN])
21182118
4
21192119
```
21202120
"""
2121-
indmin(a) = findmin(a)[2]
2121+
argmin(a) = findmin(a)[2]
21222122

21232123
# similar to Matlab's ismember
21242124
"""

base/deprecated.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,8 @@ import .Iterators.enumerate
691691
return p
692692
end
693693

694-
# ease transition for return type change of e.g. indmax due to PR #22907 when used in the
695-
# common pattern `ind2sub(size(a), indmax(a))`
694+
# ease transition for return type change of e.g. argmax due to PR #22907 when used in the
695+
# common pattern `ind2sub(size(a), argmax(a))`
696696
@deprecate(ind2sub(dims::NTuple{N,Integer}, idx::CartesianIndex{N}) where N, Tuple(idx))
697697

698698
@deprecate contains(eq::Function, itr, x) any(y->eq(y,x), itr)
@@ -1627,6 +1627,10 @@ export readandwrite
16271627
# PR #25196
16281628
@deprecate_binding ObjectIdDict IdDict{Any,Any}
16291629

1630+
# PR #25654
1631+
@deprecate indmin argmin
1632+
@deprecate indmax argmax
1633+
16301634
# END 0.7 deprecations
16311635

16321636
# BEGIN 1.0 deprecations

base/exports.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ export
393393
hcat,
394394
hvcat,
395395
indexin,
396-
indmax,
397-
indmin,
396+
argmax,
397+
argmin,
398398
invperm,
399399
invpermute!,
400400
isassigned,

base/pkg/resolve/fieldvalue.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ end
9393
# some hard constraint is being violated
9494
validmax(a::FieldValue) = a.l0 >= 0
9595

96-
# like usual indmax, but favors the highest indices
96+
# like usual argmax, but favors the highest indices
9797
# in case of a tie
98-
function Base.indmax(f::Field)
98+
function Base.argmax(f::Field)
9999
m = typemin(FieldValue)
100100
mi = 0
101101
for j = length(f):-1:1

base/pkg/resolve/maxsum.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function getsolution(msgs::Messages)
244244
sol = Vector{Int}(uninitialized, np)
245245
for p0 = 1:np
246246
fld0 = fld[p0]
247-
s0 = indmax(fld0)
247+
s0 = argmax(fld0)
248248
if !validmax(fld0[s0])
249249
throw(UnsatError(p0))
250250
end
@@ -387,12 +387,12 @@ function decimate1(p0::Int, graph::Graph, msgs::Messages)
387387

388388
@assert !decimated[p0]
389389
fld0 = fld[p0]
390-
s0 = indmax(fld0)
390+
s0 = argmax(fld0)
391391
# only do the decimation if it is consistent with
392392
# the previously decimated nodes
393393
for p1 in findall(decimated)
394394
haskey(adjdict[p0], p1) || continue
395-
s1 = indmax(fld[p1])
395+
s1 = argmax(fld[p1])
396396
j1 = adjdict[p0][p1]
397397
gmsk[p1][j1][s0,s1] || return false
398398
end

doc/src/base/collections.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ Base.minimum(::Any, ::Any)
9494
Base.minimum!
9595
Base.extrema(::Any)
9696
Base.extrema(::AbstractArray, ::Any)
97-
Base.indmax
98-
Base.indmin
97+
Base.argmax
98+
Base.argmin
9999
Base.findmax(::Any)
100100
Base.findmax(::AbstractArray, ::Any)
101101
Base.findmin(::Any)

stdlib/IterativeEigensolvers/test/runtests.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ using Test, LinearAlgebra, SparseArrays, Random
4343
# @test a*v[:,2] ≈ d[2]*b*v[:,2] atol=testtol
4444
# @test norm(v) > testtol # eigenvectors cannot be null vectors
4545
if elty <: LinearAlgebra.BlasComplex
46-
sr_ind = indmin(real.(a_evs))
46+
sr_ind = argmin(real.(a_evs))
4747
(d, v) = eigs(a, nev=1, which=:SR)
4848
@test d[1] a_evs[sr_ind]
49-
si_ind = indmin(imag.(a_evs))
49+
si_ind = argmin(imag.(a_evs))
5050
(d, v) = eigs(a, nev=1, which=:SI)
5151
@test d[1] a_evs[si_ind]
52-
lr_ind = indmax(real.(a_evs))
52+
lr_ind = argmax(real.(a_evs))
5353
(d, v) = eigs(a, nev=1, which=:LR)
5454
@test d[1] a_evs[lr_ind]
55-
li_ind = indmax(imag.(a_evs))
55+
li_ind = argmax(imag.(a_evs))
5656
(d, v) = eigs(a, nev=1, which=:LI)
5757
@test d[1] a_evs[li_ind]
5858
end

stdlib/LinearAlgebra/test/blas.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ srand(100)
7171
@testset "iamax" begin
7272
if elty <: Real
7373
x = convert(Vector{elty}, randn(n))
74-
@test BLAS.iamax(x) == indmax(abs.(x))
74+
@test BLAS.iamax(x) == argmax(abs.(x))
7575
else
7676
z = convert(Vector{elty}, complex.(randn(n),randn(n)))
77-
@test BLAS.iamax(z) == indmax(map(x -> abs(real(x)) + abs(imag(x)), z))
77+
@test BLAS.iamax(z) == argmax(map(x -> abs(real(x)) + abs(imag(x)), z))
7878
end
7979
end
8080
@testset "axp(b)y" begin
@@ -109,10 +109,10 @@ srand(100)
109109
@test BLAS.nrm2(b) norm(b)
110110
if elty <: Real
111111
@test BLAS.asum(b) sum(abs.(b))
112-
@test BLAS.iamax(b) indmax(abs.(b))
112+
@test BLAS.iamax(b) argmax(abs.(b))
113113
else
114114
@test BLAS.asum(b) sum(abs.(real(b))) + sum(abs.(imag(b)))
115-
@test BLAS.iamax(b) == indmax(map(x -> abs(real(x)) + abs(imag(x)), b))
115+
@test BLAS.iamax(b) == argmax(map(x -> abs(real(x)) + abs(imag(x)), b))
116116
end
117117
end
118118
# scal

stdlib/SparseArrays/src/SparseArrays.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import LinearAlgebra: mul!, ldiv!, rdiv!, chol, adjoint!, diag, diff, dot, eig,
1919
import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
2020
atan, atand, atanh, broadcast!, conj!, cos, cosc, cosd, cosh, cospi, cot,
2121
cotd, coth, count, csc, cscd, csch, done,
22-
exp10, exp2, findprev, findnext, floor, hash, indmin, inv,
22+
exp10, exp2, findprev, findnext, floor, hash, argmin, inv,
2323
log10, log2, next, sec, secd, sech, show,
2424
sin, sinc, sind, sinh, sinpi, squeeze, start, sum, summary, tan,
2525
tand, tanh, trunc, abs, abs2,
2626
broadcast, ceil, complex, conj, convert, copy, copyto!, adjoint,
2727
exp, expm1, findall, findmax, findmin, float, getindex,
28-
vcat, hcat, hvcat, cat, imag, indmax, kron, length, log, log1p, max, min,
28+
vcat, hcat, hvcat, cat, imag, argmax, kron, length, log, log1p, max, min,
2929
maximum, minimum, one, promote_eltype, real, reshape, rot180,
3030
rotl90, rotr90, round, setindex!, similar, size, transpose,
3131
vec, permute!, map, map!, Array

stdlib/SparseArrays/src/sparsematrix.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ function _mapreducecols!(f, op::typeof(+), R::AbstractArray, A::SparseMatrixCSC{
17611761
R
17621762
end
17631763

1764-
# findmax/min and indmax/min methods
1764+
# findmax/min and argmax/min methods
17651765
# find first zero value in sparse matrix - return linear index in full matrix
17661766
# non-structural zeros are identified by x == 0 in line with the sparse constructors.
17671767
function _findz(A::SparseMatrixCSC{Tv,Ti}, rows=1:A.m, cols=1:A.n) where {Tv,Ti}
@@ -1868,8 +1868,8 @@ findmax(A::SparseMatrixCSC{Tv,Ti}, region) where {Tv,Ti} = _findr(_isgreater_fm,
18681868
findmin(A::SparseMatrixCSC) = (r=findmin(A,(1,2)); (r[1][1], r[2][1]))
18691869
findmax(A::SparseMatrixCSC) = (r=findmax(A,(1,2)); (r[1][1], r[2][1]))
18701870

1871-
indmin(A::SparseMatrixCSC) = findmin(A)[2]
1872-
indmax(A::SparseMatrixCSC) = findmax(A)[2]
1871+
argmin(A::SparseMatrixCSC) = findmin(A)[2]
1872+
argmax(A::SparseMatrixCSC) = findmax(A)[2]
18731873

18741874
## getindex
18751875
function rangesearch(haystack::AbstractRange, needle)

stdlib/SparseArrays/test/sparse.jl

+9-9
Original file line numberDiff line numberDiff line change
@@ -1023,11 +1023,11 @@ end
10231023
@test_throws ArgumentError sparse([3], [5], 1.0, 3, 3)
10241024
end
10251025

1026-
@testset "indmax, indmin, findmax, findmin" begin
1026+
@testset "argmax, argmin, findmax, findmin" begin
10271027
S = sprand(100,80, 0.5)
10281028
A = Array(S)
1029-
@test indmax(S) == indmax(A)
1030-
@test indmin(S) == indmin(A)
1029+
@test argmax(S) == argmax(A)
1030+
@test argmin(S) == argmin(A)
10311031
@test findmin(S) == findmin(A)
10321032
@test findmax(S) == findmax(A)
10331033
for region in [(1,), (2,), (1,2)], m in [findmax, findmin]
@@ -1036,16 +1036,16 @@ end
10361036

10371037
S = spzeros(10,8)
10381038
A = Array(S)
1039-
@test indmax(S) == indmax(A) == CartesianIndex(1,1)
1040-
@test indmin(S) == indmin(A) == CartesianIndex(1,1)
1039+
@test argmax(S) == argmax(A) == CartesianIndex(1,1)
1040+
@test argmin(S) == argmin(A) == CartesianIndex(1,1)
10411041

10421042
A = Matrix{Int}(I, 0, 0)
10431043
S = sparse(A)
1044-
iA = try indmax(A) end
1045-
iS = try indmax(S) end
1044+
iA = try argmax(A) end
1045+
iS = try argmax(S) end
10461046
@test iA === iS === nothing
1047-
iA = try indmin(A) end
1048-
iS = try indmin(S) end
1047+
iA = try argmin(A) end
1048+
iS = try argmin(S) end
10491049
@test iA === iS === nothing
10501050
end
10511051

test/arrayops.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,9 @@ end
505505
@test findlast(equalto(2), g3) === nothing
506506
end
507507

508-
@testset "findmin findmax indmin indmax" begin
509-
@test indmax([10,12,9,11]) == 2
510-
@test indmin([10,12,9,11]) == 3
508+
@testset "findmin findmax argmin argmax" begin
509+
@test argmax([10,12,9,11]) == 2
510+
@test argmin([10,12,9,11]) == 3
511511
@test findmin([NaN,3.2,1.8]) === (NaN,1)
512512
@test findmax([NaN,3.2,1.8]) === (NaN,1)
513513
@test findmin([NaN,3.2,1.8,NaN]) === (NaN,1)
@@ -517,13 +517,13 @@ end
517517

518518
#14085
519519
@test findmax(4:9) == (9,6)
520-
@test indmax(4:9) == 6
520+
@test argmax(4:9) == 6
521521
@test findmin(4:9) == (4,1)
522-
@test indmin(4:9) == 1
522+
@test argmin(4:9) == 1
523523
@test findmax(5:-2:1) == (5,1)
524-
@test indmax(5:-2:1) == 1
524+
@test argmax(5:-2:1) == 1
525525
@test findmin(5:-2:1) == (1,3)
526-
@test indmin(5:-2:1) == 3
526+
@test argmin(5:-2:1) == 3
527527

528528
#23094
529529
@test_throws MethodError findmax(Set(["abc"]))

test/euler.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function euler14(m)
227227
d -= 1
228228
end
229229
end
230-
indmax(c)
230+
argmax(c)
231231
end
232232
@test euler14(999999) == 837799
233233

test/perf/perfcomp.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ function main()
3535
end
3636
println()
3737

38-
minname = names[indmin(change)]
39-
maxname = names[indmax(change)]
38+
minname = names[argmin(change)]
39+
maxname = names[argmax(change)]
4040

4141
minstd = baseline[minname][4]/baseline[minname][3]
4242
maxstd = baseline[maxname][4]/baseline[maxname][3]

0 commit comments

Comments
 (0)