Skip to content

Commit

Permalink
remove varzm functions
Browse files Browse the repository at this point in the history
the external abi is to call var,
the internal abi doesn' need to branch to alternative functions based on whether mean is given as zero
that simply made the dispatch less straightforward to understand
even though doing an exact comparison to 0 isn't generally reliable

ref #6273, which initially introduced these functions as the public API, before changing them to be the internal implementation
  • Loading branch information
vtjnash committed Jun 7, 2016
1 parent eae1746 commit e6e1312
Showing 1 changed file with 4 additions and 31 deletions.
35 changes: 4 additions & 31 deletions base/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,6 @@ function var(iterable; corrected::Bool=true, mean=nothing)
end
end

function varzm{T}(A::AbstractArray{T}; corrected::Bool=true)
n = length(A)
n == 0 && return convert(real(momenttype(T)), NaN)
return sumabs2(A) / (n - Int(corrected))
end

function varzm!{S}(R::AbstractArray{S}, A::AbstractArray; corrected::Bool=true)
if isempty(A)
fill!(R, convert(S, NaN))
else
rn = div(length(A), length(r)) - Int(corrected)
scale!(sumabs2!(R, A; init=true), convert(S, 1/rn))
end
return R
end

varzm{T}(A::AbstractArray{T}, region; corrected::Bool=true) =
varzm!(reducedim_initarray(A, region, 0, real(momenttype(T))), A; corrected=corrected)

centralizedabs2fun(m::Number) = x -> abs2(x - m)
centralize_sumabs2(A::AbstractArray, m::Number) =
mapreduce(centralizedabs2fun(m), +, A)
Expand Down Expand Up @@ -159,20 +140,12 @@ varm{T}(A::AbstractArray{T}, m::AbstractArray, region; corrected::Bool=true) =
varm!(reducedim_initarray(A, region, 0, real(momenttype(T))), A, m; corrected=corrected)


function var{T}(A::AbstractArray{T}; corrected::Bool=true, mean=nothing)
var{T}(A::AbstractArray{T}; corrected::Bool=true, mean=nothing) =
convert(real(momenttype(T)),
mean == 0 ? varzm(A; corrected=corrected) :
mean === nothing ? varm(A, Base.mean(A); corrected=corrected) :
isa(mean, Number) ? varm(A, mean::Number; corrected=corrected) :
throw(ArgumentError("invalid value of mean, $(mean)::$(typeof(mean))")))::real(momenttype(T))
end
varm(A, mean === nothing ? Base.mean(A) : mean; corrected=corrected))

function var(A::AbstractArray, region; corrected::Bool=true, mean=nothing)
mean == 0 ? varzm(A, region; corrected=corrected) :
mean === nothing ? varm(A, Base.mean(A, region), region; corrected=corrected) :
isa(mean, AbstractArray) ? varm(A, mean::AbstractArray, region; corrected=corrected) :
throw(ArgumentError("invalid value of mean, $(mean)::$(typeof(mean))"))
end
var(A::AbstractArray, region; corrected::Bool=true, mean=nothing) =
varm(A, mean === nothing ? Base.mean(A, region) : mean, region; corrected=corrected)

varm(iterable, m::Number; corrected::Bool=true) =
var(iterable, corrected=corrected, mean=m)
Expand Down

0 comments on commit e6e1312

Please sign in to comment.