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

Call user function only once in mean #151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
28 changes: 25 additions & 3 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ if !isdefined(Base, :mean)
"""
mean(itr) = mean(identity, itr)

_mean_promote(x::T, y::S) where {T,S} = convert(promote_type(T, S), y)

"""
mean(f, itr)

Expand Down Expand Up @@ -178,7 +180,28 @@ if !isdefined(Base, :mean)
"""
mean(A::AbstractArray; dims=:) = _mean(identity, A, dims)

_mean_promote(x::T, y::S) where {T,S} = convert(promote_type(T, S), y)
promote_add_type(x::S, y::T) where {S, T} =
promote_type(typeof(zero(S)/1), typeof(zero(T)/1))
function promote_add(x::Any, y::Any)
T = promote_add_type(x, y)
return Base.add_sum(convert(T, x), convert(T, y))
end

function Base.reducedim_init(f, op::typeof(promote_add), A::AbstractArray, region)
Base._reducedim_init(f, op, zero, mean, A, region)
end
function Base._reducedim_init(f, op::typeof(promote_add), fv, fop, A, region)
T = Base._realtype(f, Base.promote_union(eltype(A)))
if T !== Any && applicable(zero, T)
x = f(zero(T))/1 # /1 added for mean
z = op(fv(x), fv(x))
Tr = z isa T ? T : typeof(z)
else
z = fv(fop(f, A))
Tr = typeof(z)
end
return Base.reducedim_initarray(A, region, z, Tr)
end

# ::Dims is there to force specializing on Colon (as it is a Function)
function _mean(f, A::AbstractArray, dims::Dims=:) where Dims
Expand All @@ -188,8 +211,7 @@ if !isdefined(Base, :mean)
else
n = mapreduce(i -> size(A, i), *, unique(dims); init=1)
end
x1 = f(first(A)) / 1
result = sum(x -> _mean_promote(x1, f(x)), A, dims=dims)
result = mapreduce(f, promote_add, A, dims=dims)
if dims === (:)
return result / n
else
Expand Down
Loading