Skip to content

Commit 6ca85cd

Browse files
committedApr 16, 2024·
Reland "add more methods and tests for reductions over empty arrays"
Refs #29919 Refs #52003 This reverts commit 2a84214.
1 parent 13d4f0e commit 6ca85cd

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed
 

‎base/reduce.jl

+6-3
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ reduce_empty(::typeof(*), ::Type{T}) where {T} = one(T)
338338
reduce_empty(::typeof(*), ::Type{<:AbstractChar}) = ""
339339
reduce_empty(::typeof(&), ::Type{Bool}) = true
340340
reduce_empty(::typeof(|), ::Type{Bool}) = false
341-
341+
reduce_empty(::typeof(max), T) = typemin(T)
342+
reduce_empty(::typeof(min), T) = typemax(T)
342343
reduce_empty(::typeof(add_sum), ::Type{T}) where {T} = reduce_empty(+, T)
343344
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:BitSignedSmall} = zero(Int)
344345
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:BitUnsignedSmall} = zero(UInt)
@@ -362,8 +363,10 @@ mapreduce_empty(::typeof(identity), op, T) = reduce_empty(op, T)
362363
mapreduce_empty(::typeof(abs), op, T) = abs(reduce_empty(op, T))
363364
mapreduce_empty(::typeof(abs2), op, T) = abs2(reduce_empty(op, T))
364365

365-
mapreduce_empty(f::typeof(abs), ::typeof(max), T) = abs(zero(T))
366-
mapreduce_empty(f::typeof(abs2), ::typeof(max), T) = abs2(zero(T))
366+
mapreduce_empty(::typeof(abs), ::typeof(max), T) = abs(zero(T))
367+
mapreduce_empty(::typeof(abs), ::typeof(min), T) = typemax(abs(zero(T)))
368+
mapreduce_empty(::typeof(abs2), ::typeof(max), T) = abs2(zero(T))
369+
mapreduce_empty(::typeof(abs2), ::typeof(min), T) = typemax(abs2(zero(T)))
367370

368371
# For backward compatibility:
369372
mapreduce_empty_iter(f, op, itr, ItrEltype) =

‎test/reduce.jl

+21-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ end
9191
@test mapreduce(abs2, *, Float64[]) === 1.0
9292
@test mapreduce(abs2, max, Float64[]) === 0.0
9393
@test mapreduce(abs, max, Float64[]) === 0.0
94+
@test mapreduce(abs2, min, Float64[]) === Inf
95+
@test mapreduce(abs, min, Float64[]) === Inf
96+
@test_throws ArgumentError mapreduce(abs2, &, Float64[])
97+
@test_throws ArgumentError mapreduce(abs2, |, Float64[])
9498
@test_throws "reducing over an empty collection is not allowed" mapreduce(abs2, &, Float64[])
9599
@test_throws str -> !occursin("Closest candidates are", str) mapreduce(abs2, &, Float64[])
96100
@test_throws "reducing over an empty collection is not allowed" mapreduce(abs2, |, Float64[])
@@ -246,8 +250,8 @@ prod2(itr) = invoke(prod, Tuple{Any}, itr)
246250

247251
# maximum & minimum & extrema
248252

249-
@test_throws "reducing over an empty" maximum(Int[])
250-
@test_throws "reducing over an empty" minimum(Int[])
253+
@test maximum(Int[]) === typemin(Int)
254+
@test minimum(Int[]) === typemax(Int)
251255
@test_throws "reducing over an empty" extrema(Int[])
252256

253257
@test maximum(Int[]; init=-1) == -1
@@ -258,6 +262,21 @@ prod2(itr) = invoke(prod, Tuple{Any}, itr)
258262
@test minimum(sin, []; init=1) == 1
259263
@test extrema(sin, []; init=(1, -1)) == (1, -1)
260264

265+
@test maximum(Float64[]) === -Inf
266+
@test minimum(Float64[]) === +Inf
267+
268+
@test maximum(Float32[]) === -Inf32
269+
@test minimum(Float32[]) === +Inf32
270+
271+
@test maximum(abs, Int[]) === 0
272+
@test minimum(abs, Int[]) === typemax(Int)
273+
274+
@test maximum(abs, Float64[]) === 0.0
275+
@test minimum(abs, Float64[]) === +Inf
276+
277+
@test maximum(abs, Float32[]) === 0.0f0
278+
@test minimum(abs, Float32[]) === +Inf32
279+
261280
@test maximum(5) == 5
262281
@test minimum(5) == 5
263282
@test extrema(5) == (5, 5)

0 commit comments

Comments
 (0)
Please sign in to comment.