Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more methods and tests for reductions over empty arrays #29919

Merged
merged 7 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ reduce_empty(::typeof(*), ::Type{<:AbstractChar}) = ""
reduce_empty(::typeof(&), ::Type{Bool}) = true
reduce_empty(::typeof(|), ::Type{Bool}) = false

reduce_empty(::typeof(max), ::Type{<:AbstractFloat}) where {T} = typemin(T)
reduce_empty(::typeof(min), ::Type{<:AbstractFloat}) where {T} = typemax(T)
vtjnash marked this conversation as resolved.
Show resolved Hide resolved

reduce_empty(::typeof(add_sum), ::Type{Union{}}) = _empty_reduce_error()
reduce_empty(::typeof(add_sum), ::Type{T}) where {T} = reduce_empty(+, T)
reduce_empty(::typeof(add_sum), ::Type{T}) where {T<:SmallSigned} = zero(Int)
Expand Down Expand Up @@ -346,8 +349,10 @@ mapreduce_empty(::typeof(identity), op, T) = reduce_empty(op, T)
mapreduce_empty(::typeof(abs), op, T) = abs(reduce_empty(op, T))
mapreduce_empty(::typeof(abs2), op, T) = abs2(reduce_empty(op, T))

mapreduce_empty(f::typeof(abs), ::typeof(max), T) = abs(zero(T))
mapreduce_empty(f::typeof(abs2), ::typeof(max), T) = abs2(zero(T))
mapreduce_empty(::typeof(abs), ::typeof(max), T) = abs(zero(T))
mapreduce_empty(::typeof(abs), ::typeof(min), T::Type{<:AbstractFloat}) = typemax(T)
simonbyrne marked this conversation as resolved.
Show resolved Hide resolved
mapreduce_empty(::typeof(abs2), ::typeof(max), T) = abs2(zero(T))
mapreduce_empty(::typeof(abs2), ::typeof(min), T::Type{<:AbstractFloat}) = typemax(T)
simonbyrne marked this conversation as resolved.
Show resolved Hide resolved

# For backward compatibility:
mapreduce_empty_iter(f, op, itr, ItrEltype) =
Expand Down
17 changes: 17 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ end
@test mapreduce(abs2, *, Float64[]) === 1.0
@test mapreduce(abs2, max, Float64[]) === 0.0
@test mapreduce(abs, max, Float64[]) === 0.0
@test mapreduce(abs2, min, Float64[]) === Inf
@test mapreduce(abs, min, Float64[]) === Inf
@test_throws ArgumentError mapreduce(abs2, &, Float64[])
@test_throws ArgumentError mapreduce(abs2, |, Float64[])

Expand Down Expand Up @@ -245,6 +247,21 @@ prod2(itr) = invoke(prod, Tuple{Any}, itr)
@test maximum(Int[]; init=-1) == -1
@test minimum(Int[]; init=-1) == -1

@test maximum(Float64[]) === -Inf
@test minimum(Float64[]) === +Inf

@test maximum(Float32[]) === -Inf32
@test minimum(Float32[]) === +Inf32

@test maximum(abs, Int[]) === 0
@test_throws ArgumentError minimum(abs, Int[])

@test maximum(abs, Float64[]) === 0.0
@test minimum(abs, Float64[]) === +Inf

@test maximum(abs, Float32[]) === 0.0f0
@test minimum(abs, Float32[]) === +Inf32

@test maximum(5) == 5
@test minimum(5) == 5
@test extrema(5) == (5, 5)
Expand Down