Skip to content

Commit

Permalink
Resolve instability from reducedim_init (#43467)
Browse files Browse the repository at this point in the history
  • Loading branch information
N5N3 authored Jan 6, 2022
1 parent f357c47 commit 4b83a64
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ for (f1, f2, initval, typeextreme) in ((:min, :max, :Inf, :typemax), (:max, :min

if isempty(A1)
# If the slice is empty just return non-view version as the initial array
return copy(A1)
return map(f, A1)
else
# otherwise use the min/max of the first slice as initial value
v0 = mapreduce(f, $f2, A1)
Expand All @@ -148,9 +148,9 @@ for (f1, f2, initval, typeextreme) in ((:min, :max, :Inf, :typemax), (:max, :min
Tr = v0 isa T ? T : typeof(v0)

# but NaNs and missing need to be avoided as initial values
if (v0 == v0) === false
if v0 isa Number && isnan(v0)
# v0 is NaN
v0 = $initval
v0 = oftype(v0, $initval)
elseif isunordered(v0)
# v0 is missing or a third-party unordered value
Tnm = nonmissingtype(Tr)
Expand Down
8 changes: 6 additions & 2 deletions test/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ end

@testset "argmin/argmax" begin
B = reshape(3^3:-1:1, (3, 3, 3))
@test B[argmax(B, dims=[2, 3])] == maximum(B, dims=[2, 3])
@test B[argmin(B, dims=[2, 3])] == minimum(B, dims=[2, 3])
@test B[argmax(B, dims=[2, 3])] == @inferred(maximum(B, dims=[2, 3]))
@test B[argmin(B, dims=[2, 3])] == @inferred(minimum(B, dims=[2, 3]))
end

@testset "in-place reductions with mismatched dimensionalities" begin
Expand Down Expand Up @@ -506,3 +506,7 @@ end
@test r_red == [3]
end
end

@testset "type stability (issue #43461)" begin
@test (@inferred maximum(Float64, reshape(1:4,2,:); dims = 2)) == reshape([3,4],2,1)
end

0 comments on commit 4b83a64

Please sign in to comment.