Skip to content

Commit 05a1eae

Browse files
committed
Reimplement reducedim_init for extrema
Update reducedim.jl Update reducedim.jl
1 parent e9e748e commit 05a1eae

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

base/reducedim.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ function reducedim_init(f::F, op::Union{typeof(min),typeof(max)}, A::AbstractArr
151151
return map!(f, reducedim_initarray(A,region,_InitialValue(),Tr), A1)
152152
end
153153

154+
function reducedim_init(f::ExtremaMap, op::typeof(_extrema_rf), A::AbstractArray, region)
155+
ri = reduced_indices(A, region)
156+
any(i -> isempty(axes(A, i)), region) && _empty_reduce_error()
157+
A1 = view(A, ri...)
158+
T = _return_type(f.f, Tuple{eltype(A)})
159+
if isempty(A) || (isconcretetype(nonmissingtype(T)) &&
160+
T === _return_type(min, Tuple{T,T}) &&
161+
T === _return_type(max, Tuple{T,T}) )
162+
Tr = T isa Union ? Union{NTuple{2,T.a},NTuple{2,T.b}} : NTuple{2,T}
163+
return map!(f, reducedim_initarray(A,region,_InitialValue(),Tr), A1)
164+
end
165+
v0 = mapreduce(f, op, A1) # Try to reduce A1
166+
T = _realtype(f.f, promote_union(eltype(A))) # A possible wrong guess.
167+
Tr = v0 isa NTuple{2,T} ? NTuple{2,T} : typeof(v0) # If NTuple{2,T} is wrong. Use typeof(v0) instead.
168+
return map!(f, reducedim_initarray(A,region,_InitialValue(),Tr), A1)
169+
end
170+
154171
reducedim_init(f::Union{typeof(abs),typeof(abs2)}, op::typeof(max), A::AbstractArray{T}, region) where {T} =
155172
reducedim_initarray(A, region, zero(f(zero(T))), _realtype(f, T))
156173

0 commit comments

Comments
 (0)