Skip to content

Commit 351e519

Browse files
N5N3LilithHafner
authored andcommitted
Resolve instability from reducedim_init (JuliaLang#43467)
1 parent 6478348 commit 351e519

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

base/reducedim.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ for (f1, f2, initval, typeextreme) in ((:min, :max, :Inf, :typemax), (:max, :min
139139

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

150150
# but NaNs and missing need to be avoided as initial values
151-
if (v0 == v0) === false
151+
if v0 isa Number && isnan(v0)
152152
# v0 is NaN
153-
v0 = $initval
153+
v0 = oftype(v0, $initval)
154154
elseif isunordered(v0)
155155
# v0 is missing or a third-party unordered value
156156
Tnm = nonmissingtype(Tr)

test/reducedim.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ end
445445

446446
@testset "argmin/argmax" begin
447447
B = reshape(3^3:-1:1, (3, 3, 3))
448-
@test B[argmax(B, dims=[2, 3])] == maximum(B, dims=[2, 3])
449-
@test B[argmin(B, dims=[2, 3])] == minimum(B, dims=[2, 3])
448+
@test B[argmax(B, dims=[2, 3])] == @inferred(maximum(B, dims=[2, 3]))
449+
@test B[argmin(B, dims=[2, 3])] == @inferred(minimum(B, dims=[2, 3]))
450450
end
451451

452452
@testset "in-place reductions with mismatched dimensionalities" begin
@@ -506,3 +506,7 @@ end
506506
@test r_red == [3]
507507
end
508508
end
509+
510+
@testset "type stability (issue #43461)" begin
511+
@test (@inferred maximum(Float64, reshape(1:4,2,:); dims = 2)) == reshape([3,4],2,1)
512+
end

0 commit comments

Comments
 (0)