From e6e1312a051d9b5511bf5b4f9ea0ae97528bb5ed Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Tue, 7 Jun 2016 13:33:48 -0400 Subject: [PATCH] remove varzm functions 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 --- base/statistics.jl | 35 ++++------------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/base/statistics.jl b/base/statistics.jl index a9220a43979f5..108b42740b149 100644 --- a/base/statistics.jl +++ b/base/statistics.jl @@ -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) @@ -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)