Skip to content

Commit aae32f1

Browse files
authored
Fix 2-arg argmin/argmax/findmin/findmax (#748)
* Fix 2-arg argmin/argmax/findmin/findmax * Add NEWS and bump version
1 parent 78bba10 commit aae32f1

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "3.31.0"
3+
version = "3.31.1"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ changes in `julia`.
5454

5555
## Supported features
5656

57+
* Two argument methods `findmax(f, domain)`, `argmax(f, domain)` and the corresponding `min` versions ([#35316], [#41076]) (since Compat 3.31.1)
58+
59+
* `isunordered(x)` returns true if `x` is value that is normally unordered, such as `NaN` or `missing` ([#35316]) (since Compat 3.31.1)
60+
5761
* `get` accepts tuples and numbers ([#41007], [#41032]) (since Compat 3.31)
5862

5963
* `muladd(A,B,z)` now accepts arrays ([#37065]) (since Compat 3.30)
@@ -252,3 +256,5 @@ Note that you should specify the correct minimum version for `Compat` in the
252256
[#37065]: https://github.com/JuliaLang/julia/pull/37065
253257
[#41007]: https://github.com/JuliaLang/julia/pull/41007
254258
[#41032]: https://github.com/JuliaLang/julia/pull/41032
259+
[#35316]: https://github.com/JuliaLang/julia/pull/35316
260+
[#41076]: https://github.com/JuliaLang/julia/pull/41076

src/Compat.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -957,21 +957,23 @@ if VERSION < v"1.2.0-DEV.246"
957957
end
958958

959959
if VERSION < v"1.7.0-DEV.119"
960-
# Part of https://github.com/JuliaLang/julia/pull/35316
960+
# Part of:
961+
# https://github.com/JuliaLang/julia/pull/35316
962+
# https://github.com/JuliaLang/julia/pull/41076
961963
isunordered(x) = false
962964
isunordered(x::AbstractFloat) = isnan(x)
963965
isunordered(x::Missing) = true
964966

965967
isgreater(x, y) = isunordered(x) || isunordered(y) ? isless(x, y) : isless(y, x)
966968

967-
Base.findmax(f, domain) = mapfoldl(x -> (f(x), x), _rf_findmax, domain)
968-
_rf_findmax((fm, m), (fx, x)) = isless(fm, fx) ? (fx, x) : (fm, m)
969+
Base.findmax(f, domain) = mapfoldl( ((k, v),) -> (f(v), k), _rf_findmax, pairs(domain) )
970+
_rf_findmax((fm, im), (fx, ix)) = isless(fm, fx) ? (fx, ix) : (fm, im)
969971

970-
Base.findmin(f, domain) = mapfoldl(x -> (f(x), x), _rf_findmin, domain)
971-
_rf_findmin((fm, m), (fx, x)) = isgreater(fm, fx) ? (fx, x) : (fm, m)
972+
Base.findmin(f, domain) = mapfoldl( ((k, v),) -> (f(v), k), _rf_findmin, pairs(domain) )
973+
_rf_findmin((fm, im), (fx, ix)) = isgreater(fm, fx) ? (fx, ix) : (fm, im)
972974

973-
Base.argmax(f, domain) = findmax(f, domain)[2]
974-
Base.argmin(f, domain) = findmin(f, domain)[2]
975+
Base.argmax(f, domain) = mapfoldl(x -> (f(x), x), _rf_findmax, domain)[2]
976+
Base.argmin(f, domain) = mapfoldl(x -> (f(x), x), _rf_findmin, domain)[2]
975977
end
976978

977979
# Part of: https://github.com/JuliaLang/julia/pull/36018

test/runtests.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -946,25 +946,26 @@ end
946946
end
947947

948948
# https://github.com/JuliaLang/julia/pull/35316
949+
# https://github.com/JuliaLang/julia/pull/41076
949950
@testset "2arg" begin
950951
@testset "findmin(f, domain)" begin
951952
@test findmin(-, 1:10) == (-10, 10)
952-
@test findmin(identity, [1, 2, 3, missing]) === (missing, missing)
953-
@test findmin(identity, [1, NaN, 3, missing]) === (missing, missing)
954-
@test findmin(identity, [1, missing, NaN, 3]) === (missing, missing)
955-
@test findmin(identity, [1, NaN, 3]) === (NaN, NaN)
956-
@test findmin(identity, [1, 3, NaN]) === (NaN, NaN)
957-
@test all(findmin(cos, 0:π/2:2π) .≈ (-1.0, π))
953+
@test findmin(identity, [1, 2, 3, missing]) === (missing, 4)
954+
@test findmin(identity, [1, NaN, 3, missing]) === (missing, 4)
955+
@test findmin(identity, [1, missing, NaN, 3]) === (missing, 2)
956+
@test findmin(identity, [1, NaN, 3]) === (NaN, 2)
957+
@test findmin(identity, [1, 3, NaN]) === (NaN, 3)
958+
@test all(findmin(cos, 0:π/2:2π) .≈ (-1.0, 3))
958959
end
959960

960961
@testset "findmax(f, domain)" begin
961962
@test findmax(-, 1:10) == (-1, 1)
962-
@test findmax(identity, [1, 2, 3, missing]) === (missing, missing)
963-
@test findmax(identity, [1, NaN, 3, missing]) === (missing, missing)
964-
@test findmax(identity, [1, missing, NaN, 3]) === (missing, missing)
965-
@test findmax(identity, [1, NaN, 3]) === (NaN, NaN)
966-
@test findmax(identity, [1, 3, NaN]) === (NaN, NaN)
967-
@test findmax(cos, 0:π/2:2π) == (1.0, 0.0)
963+
@test findmax(identity, [1, 2, 3, missing]) === (missing, 4)
964+
@test findmax(identity, [1, NaN, 3, missing]) === (missing, 4)
965+
@test findmax(identity, [1, missing, NaN, 3]) === (missing, 2)
966+
@test findmax(identity, [1, NaN, 3]) === (NaN, 2)
967+
@test findmax(identity, [1, 3, NaN]) === (NaN, 3)
968+
@test findmax(cos, 0:π/2:2π) == (1.0, 1)
968969
end
969970

970971
@testset "argmin(f, domain)" begin

0 commit comments

Comments
 (0)