Skip to content

Commit

Permalink
Reimplement reducedim_init for extrema
Browse files Browse the repository at this point in the history
Update reducedim.jl

Update reducedim.jl
  • Loading branch information
N5N3 committed Jan 15, 2022
1 parent e9e748e commit 05a1eae
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ function reducedim_init(f::F, op::Union{typeof(min),typeof(max)}, A::AbstractArr
return map!(f, reducedim_initarray(A,region,_InitialValue(),Tr), A1)
end

function reducedim_init(f::ExtremaMap, op::typeof(_extrema_rf), A::AbstractArray, region)
ri = reduced_indices(A, region)
any(i -> isempty(axes(A, i)), region) && _empty_reduce_error()
A1 = view(A, ri...)
T = _return_type(f.f, Tuple{eltype(A)})
if isempty(A) || (isconcretetype(nonmissingtype(T)) &&
T === _return_type(min, Tuple{T,T}) &&
T === _return_type(max, Tuple{T,T}) )
Tr = T isa Union ? Union{NTuple{2,T.a},NTuple{2,T.b}} : NTuple{2,T}
return map!(f, reducedim_initarray(A,region,_InitialValue(),Tr), A1)
end
v0 = mapreduce(f, op, A1) # Try to reduce A1
T = _realtype(f.f, promote_union(eltype(A))) # A possible wrong guess.
Tr = v0 isa NTuple{2,T} ? NTuple{2,T} : typeof(v0) # If NTuple{2,T} is wrong. Use typeof(v0) instead.
return map!(f, reducedim_initarray(A,region,_InitialValue(),Tr), A1)
end

reducedim_init(f::Union{typeof(abs),typeof(abs2)}, op::typeof(max), A::AbstractArray{T}, region) where {T} =
reducedim_initarray(A, region, zero(f(zero(T))), _realtype(f, T))

Expand Down

0 comments on commit 05a1eae

Please sign in to comment.