From 1fb0ea96a0f674016c3ed442be927967fc12f4bc Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 08:38:21 +0200 Subject: [PATCH 01/35] Consistent comment style --- src/weights.jl | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 5affc704d..0a8abc74a 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -1,4 +1,5 @@ -###### Weight vector ##### +##### Weight vector ##### + abstract type AbstractWeights{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractVector{T} end """ @@ -247,7 +248,7 @@ eweights(n::Integer, λ::Real) = eweights(1:n, λ) eweights(t::AbstractVector, r::AbstractRange, λ::Real) = eweights(something.(indexin(t, r)), λ) -# NOTE: No variance correction is implemented for exponential weights +# NOTE: no variance correction is implemented for exponential weights struct UnitWeights{T<:Real} <: AbstractWeights{Int, T, V where V<:Vector{T}} len::Int @@ -392,7 +393,6 @@ end # (d) A is a general dense array with eltype <: BlasReal: # dim <= 2: delegate to (a) and (b) # otherwise, decompose A into multiple pages -# function _wsum1!(R::AbstractArray, A::AbstractVector, w::AbstractVector, init::Bool) r = wsum(A, w) @@ -455,7 +455,8 @@ function _wsumN!(R::StridedArray{T}, A::DenseArray{T,N}, w::StridedVector{T}, di return R end -# General Cartesian-based weighted sum across dimensions +## general Cartesian-based weighted sum across dimensions + @generated function _wsum_general!(R::AbstractArray{RT}, f::supertype(typeof(abs)), A::AbstractArray{T,N}, w::AbstractVector{WT}, dim::Int, init::Bool) where {T,RT,WT,N} quote @@ -512,7 +513,6 @@ end end end - # N = 1 _wsum!(R::StridedArray{T}, A::DenseArray{T,1}, w::StridedVector{T}, dim::Int, init::Bool) where {T<:BlasReal} = _wsum1!(R, A, w, init) @@ -533,7 +533,6 @@ _wsum!(R::AbstractArray, A::AbstractArray, w::AbstractVector, dim::Int, init::Bo wsumtype(::Type{T}, ::Type{W}) where {T,W} = typeof(zero(T) * zero(W) + zero(T) * zero(W)) wsumtype(::Type{T}, ::Type{T}) where {T<:BlasReal} = T - """ wsum!(R, A, w, dim; init=true) @@ -559,7 +558,7 @@ function wsum(A::AbstractArray{<:Number}, w::UnitWeights, dim::Int) return sum(A, dims=dim) end -# extended sum! and wsum +## extended sum! and wsum Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::Int; init::Bool=true) = wsum!(R, A, values(w), dim; init=init) @@ -571,7 +570,7 @@ function Base.sum(A::AbstractArray{<:Number}, w::UnitWeights, dim::Int) return sum(A, dims=dim) end -###### Weighted means ##### +##### Weighted means ##### """ wmean(v, w::AbstractVector) @@ -628,7 +627,8 @@ function _mean(A::AbstractArray, w::UnitWeights, dims::Int) return mean(A, dims=dims) end -###### Weighted quantile ##### +##### Weighted quantile ##### + """ quantile(v, w::AbstractWeights, p) @@ -723,9 +723,8 @@ end quantile(v::RealVector, w::AbstractWeights{<:Real}, p::Number) = quantile(v, w, [p])[1] +##### Weighted median ##### - -###### Weighted median ##### """ median(v::RealVector, w::AbstractWeights) From 516dee369130eadaa700079636da4f68f4335d76 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 13:33:47 +0200 Subject: [PATCH 02/35] Remove values as a parameter of weights --- src/weights.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 0a8abc74a..453141e85 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -1,6 +1,6 @@ ##### Weight vector ##### -abstract type AbstractWeights{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractVector{T} end +abstract type AbstractWeights{S<:Real, T<:Real} <: AbstractVector{T} end """ @weights name @@ -10,8 +10,8 @@ and stores the `values` (`V<:RealVector`) and `sum` (`S<:Real`). """ macro weights(name) return quote - mutable struct $name{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractWeights{S, T, V} - values::V + mutable struct $name{S<:Real, T<:Real} <: AbstractWeights{S, T} + values::AbstractVector{T} sum::S end $(esc(name))(vs) = $(esc(name))(vs, sum(vs)) @@ -250,7 +250,7 @@ eweights(t::AbstractVector, r::AbstractRange, λ::Real) = # NOTE: no variance correction is implemented for exponential weights -struct UnitWeights{T<:Real} <: AbstractWeights{Int, T, V where V<:Vector{T}} +struct UnitWeights{T<:Real} <: AbstractWeights{Int, T} len::Int end From bb738fc893767246d38b5406fae03e9f3e819d4f Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 13:36:49 +0200 Subject: [PATCH 03/35] Deprecate values for weights --- src/deprecates.jl | 3 +++ src/weights.jl | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/deprecates.jl b/src/deprecates.jl index cb9c55c9f..6b392129e 100644 --- a/src/deprecates.jl +++ b/src/deprecates.jl @@ -35,3 +35,6 @@ end @deprecate wmedian(v::RealVector, w::RealVector) median(v, weights(w)) @deprecate quantile(v::AbstractArray{<:Real}) quantile(v, [.0, .25, .5, .75, 1.0]) + +### Deprecated September 2019 +@deprecate values(wv::AbstractWeights) convert(Vector, wv) \ No newline at end of file diff --git a/src/weights.jl b/src/weights.jl index 453141e85..39213225e 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -19,11 +19,12 @@ macro weights(name) end length(wv::AbstractWeights) = length(wv.values) -values(wv::AbstractWeights) = wv.values sum(wv::AbstractWeights) = wv.sum isempty(wv::AbstractWeights) = isempty(wv.values) size(wv::AbstractWeights) = size(wv.values) +convert(::Type{Vector}, wv::AbstractWeights) = wv.values + Base.getindex(wv::AbstractWeights, i) = getindex(wv.values, i) @propagate_inbounds function Base.setindex!(wv::AbstractWeights, v::Real, i::Int) @@ -261,12 +262,13 @@ Construct a `UnitWeights` vector with length `s` and weight elements of type `T` All weight elements are identically one. """ UnitWeights -values(wv::UnitWeights{T}) where T = fill(one(T), length(wv)) sum(wv::UnitWeights{T}) where T = convert(T, length(wv)) isempty(wv::UnitWeights) = iszero(wv.len) length(wv::UnitWeights) = wv.len size(wv::UnitWeights) = Tuple(length(wv)) +convert(::Type{Vector}, wv::UnitWeights{T}) where T = ones(T, length(wv)) + @propagate_inbounds function Base.getindex(wv::UnitWeights{T}, i::Integer) where T @boundscheck checkbounds(wv, i) one(T) @@ -344,9 +346,9 @@ wsum(v::AbstractVector, w::AbstractVector) = dot(v, w) wsum(v::AbstractArray, w::AbstractVector) = dot(vec(v), w) # Note: the methods for BitArray and SparseMatrixCSC are to avoid ambiguities -Base.sum(v::BitArray, w::AbstractWeights) = wsum(v, values(w)) -Base.sum(v::SparseArrays.SparseMatrixCSC, w::AbstractWeights) = wsum(v, values(w)) -Base.sum(v::AbstractArray, w::AbstractWeights) = dot(v, values(w)) +Base.sum(v::BitArray, w::AbstractWeights) = wsum(v, w.values) +Base.sum(v::SparseArrays.SparseMatrixCSC, w::AbstractWeights) = wsum(v, w.values) +Base.sum(v::AbstractArray, w::AbstractWeights) = dot(v, w.values) for v in (AbstractArray{<:Number}, BitArray, SparseArrays.SparseMatrixCSC, AbstractArray) @eval begin @@ -561,9 +563,9 @@ end ## extended sum! and wsum Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::Int; init::Bool=true) = - wsum!(R, A, values(w), dim; init=init) + wsum!(R, A, w.values, dim; init=init) -Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}, dim::Int) = wsum(A, values(w), dim) +Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}, dim::Int) = wsum(A, w.values, dim) function Base.sum(A::AbstractArray{<:Number}, w::UnitWeights, dim::Int) size(A, dim) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) From 74ed6f98288e263c897b6c194cccaf04a1626985 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 14:10:59 +0200 Subject: [PATCH 04/35] dimension as keyword to sum for consistency --- src/weights.jl | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 39213225e..b8392e89f 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -562,14 +562,15 @@ end ## extended sum! and wsum -Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::Int; init::Bool=true) = - wsum!(R, A, w.values, dim; init=init) +Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}; + dims::Union{Nothing,Int}=nothing, init::Bool=true) = wsum!(R, A, w.values, dims; init=init) -Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}, dim::Int) = wsum(A, w.values, dim) +Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims::Union{Nothing,Int}=nothing) = + wsum(A, w.values, dims) -function Base.sum(A::AbstractArray{<:Number}, w::UnitWeights, dim::Int) - size(A, dim) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return sum(A, dims=dim) +function Base.sum(A::AbstractArray{<:Number}, w::UnitWeights; dims::Union{Nothing,Int}=nothing) + size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) + return sum(A, dims=dims) end ##### Weighted means ##### From 2efced68be7dd2211d76fb8a9246da0f2143120c Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 14:11:17 +0200 Subject: [PATCH 05/35] Clean up --- test/weights.jl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/weights.jl b/test/weights.jl index 5b3fd8bae..ed1b9c957 100644 --- a/test/weights.jl +++ b/test/weights.jl @@ -250,8 +250,6 @@ end end end - -# Quantile fweights @testset "Quantile fweights" begin data = ( [7, 1, 2, 4, 10], @@ -429,10 +427,9 @@ end v = [7, 1, 2, 4, 10] w = [1, 1/3, 1/3, 1/3, 1] answer = 6.0 - @test quantile(data[1], f(w), 0.5) ≈ answer atol = 1e-5 + @test quantile(data[1], f(w), 0.5) ≈ answer atol = 1e-5 end - @testset "Median $f" for f in weight_funcs data = [4, 3, 2, 1] wt = [0, 0, 0, 0] From 18545aa98f3689fc5cf94eef5d2ab87794700a2b Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 16:02:18 +0200 Subject: [PATCH 06/35] Drop unnecessary sum methods --- src/weights.jl | 22 +++------------------- test/weights.jl | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index b8392e89f..b8b095409 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -345,22 +345,6 @@ Compute the weighted sum of an array `v` with weights `w`, optionally over the d wsum(v::AbstractVector, w::AbstractVector) = dot(v, w) wsum(v::AbstractArray, w::AbstractVector) = dot(vec(v), w) -# Note: the methods for BitArray and SparseMatrixCSC are to avoid ambiguities -Base.sum(v::BitArray, w::AbstractWeights) = wsum(v, w.values) -Base.sum(v::SparseArrays.SparseMatrixCSC, w::AbstractWeights) = wsum(v, w.values) -Base.sum(v::AbstractArray, w::AbstractWeights) = dot(v, w.values) - -for v in (AbstractArray{<:Number}, BitArray, SparseArrays.SparseMatrixCSC, AbstractArray) - @eval begin - function Base.sum(v::$v, w::UnitWeights) - if length(v) != length(w) - throw(DimensionMismatch("Inconsistent array dimension.")) - end - return sum(v) - end - end -end - ## wsum along dimension # # Brief explanation of the algorithm: @@ -562,11 +546,11 @@ end ## extended sum! and wsum -Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}; - dims::Union{Nothing,Int}=nothing, init::Bool=true) = wsum!(R, A, w.values, dims; init=init) +Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::Int; init::Bool=true) = + wsum!(R, A, values(w), dim; init=init) Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims::Union{Nothing,Int}=nothing) = - wsum(A, w.values, dims) + dims == nothing ? wsum(A, w.values) : wsum(A, w.values, dims) function Base.sum(A::AbstractArray{<:Number}, w::UnitWeights; dims::Union{Nothing,Int}=nothing) size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) diff --git a/test/weights.jl b/test/weights.jl index ed1b9c957..f5595d315 100644 --- a/test/weights.jl +++ b/test/weights.jl @@ -232,9 +232,9 @@ a = reshape(1.0:27.0, 3, 3, 3) @test sum(1:3, f([1.0, 1.0, 0.5])) ≈ 4.5 for wt in ([1.0, 1.0, 1.0], [1.0, 0.2, 0.0], [0.2, 0.0, 1.0]) - @test sum(a, f(wt), 1) ≈ sum(a.*reshape(wt, length(wt), 1, 1), dims = 1) - @test sum(a, f(wt), 2) ≈ sum(a.*reshape(wt, 1, length(wt), 1), dims = 2) - @test sum(a, f(wt), 3) ≈ sum(a.*reshape(wt, 1, 1, length(wt)), dims = 3) + @test sum(a, f(wt), dims = 1) ≈ sum(a.*reshape(wt, length(wt), 1, 1), dims = 1) + @test sum(a, f(wt), dims = 2) ≈ sum(a.*reshape(wt, 1, length(wt), 1), dims = 2) + @test sum(a, f(wt), dims = 3) ≈ sum(a.*reshape(wt, 1, 1, length(wt)), dims = 3) end end @@ -467,17 +467,17 @@ end @test sum([1.0, 2.0, 3.0], wt) ≈ 6.0 @test mean([1.0, 2.0, 3.0], wt) ≈ 2.0 - @test sum(a, wt, 1) ≈ sum(a, dims=1) - @test sum(a, wt, 2) ≈ sum(a, dims=2) - @test sum(a, wt, 3) ≈ sum(a, dims=3) + @test sum(a, wt, dims = 1) ≈ sum(a, dims = 1) + @test sum(a, wt, dims = 2) ≈ sum(a, dims = 2) + @test sum(a, wt, dims = 3) ≈ sum(a, dims = 3) - @test wsum(a, wt, 1) ≈ sum(a, dims=1) - @test wsum(a, wt, 2) ≈ sum(a, dims=2) - @test wsum(a, wt, 3) ≈ sum(a, dims=3) + @test wsum(a, wt, dims = 1) ≈ sum(a, dims = 1) + @test wsum(a, wt, dims = 2) ≈ sum(a, dims = 2) + @test wsum(a, wt, dims = 3) ≈ sum(a, dims = 3) - @test mean(a, wt, dims=1) ≈ mean(a, dims=1) - @test mean(a, wt, dims=2) ≈ mean(a, dims=2) - @test mean(a, wt, dims=3) ≈ mean(a, dims=3) + @test mean(a, wt, dims = 1) ≈ mean(a, dims = 1) + @test mean(a, wt, dims = 2) ≈ mean(a, dims = 2) + @test mean(a, wt, dims = 3) ≈ mean(a, dims = 3) @test_throws DimensionMismatch sum(a, wt) @test_throws DimensionMismatch sum(a, wt, 4) From 94bfe944cbc11b950c6b5c88e74f32a800783588 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 17:30:09 +0200 Subject: [PATCH 07/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index b8b095409..cc72dcf8a 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -23,7 +23,8 @@ sum(wv::AbstractWeights) = wv.sum isempty(wv::AbstractWeights) = isempty(wv.values) size(wv::AbstractWeights) = size(wv.values) -convert(::Type{Vector}, wv::AbstractWeights) = wv.values +convert(::Type{AbstractVector}, wv::AbstractWeights) = wv.values +convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values) Base.getindex(wv::AbstractWeights, i) = getindex(wv.values, i) From a42f1e3d36018061f9274bc39ce7e5c784eae69c Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 17:33:01 +0200 Subject: [PATCH 08/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index cc72dcf8a..fb50aaf52 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -268,7 +268,8 @@ isempty(wv::UnitWeights) = iszero(wv.len) length(wv::UnitWeights) = wv.len size(wv::UnitWeights) = Tuple(length(wv)) -convert(::Type{Vector}, wv::UnitWeights{T}) where T = ones(T, length(wv)) +convert(::Type{AbstractVector}, wv::UnitWeights{T}) where {T} = ones(T, length(wv)) +convert(::Type{Vector}, wv::UnitWeights{T}) where {T} = ones(T, length(wv)) @propagate_inbounds function Base.getindex(wv::UnitWeights{T}, i::Integer) where T @boundscheck checkbounds(wv, i) From a061a2efbd97f11291272546151c28ad186e426b Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 17:37:23 +0200 Subject: [PATCH 09/35] Fix boundary check --- src/weights.jl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index b8b095409..d45ff3c03 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -553,8 +553,13 @@ Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims::Union{Not dims == nothing ? wsum(A, w.values) : wsum(A, w.values, dims) function Base.sum(A::AbstractArray{<:Number}, w::UnitWeights; dims::Union{Nothing,Int}=nothing) - size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return sum(A, dims=dims) + if dims == nothing + size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) + return sum(A) + else + length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) + return sum(A, dims=dims) + end end ##### Weighted means ##### From 20eaae5eac3b9a5d487ef086799ebba43221215c Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 17:37:42 +0200 Subject: [PATCH 10/35] Remove space around keyword arguments --- test/weights.jl | 66 ++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/test/weights.jl b/test/weights.jl index f5595d315..6136b74f9 100644 --- a/test/weights.jl +++ b/test/weights.jl @@ -133,8 +133,8 @@ w2 = rand(8) @test size(wsum(x, w1, 1)) == (1, 8) @test size(wsum(x, w2, 2)) == (6, 1) -@test wsum(x, w1, 1) ≈ sum(x .* w1, dims = 1) -@test wsum(x, w2, 2) ≈ sum(x .* w2', dims = 2) +@test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) +@test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) x = rand(6, 5, 4) w1 = rand(6) @@ -145,23 +145,23 @@ w3 = rand(4) @test size(wsum(x, w2, 2)) == (6, 1, 4) @test size(wsum(x, w3, 3)) == (6, 5, 1) -@test wsum(x, w1, 1) ≈ sum(x .* w1, dims = 1) -@test wsum(x, w2, 2) ≈ sum(x .* w2', dims = 2) -@test wsum(x, w3, 3) ≈ sum(x .* reshape(w3, 1, 1, 4), dims = 3) +@test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) +@test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) +@test wsum(x, w3, 3) ≈ sum(x .* reshape(w3, 1, 1, 4), dims=3) v = view(x, 2:4, :, :) -@test wsum(v, w1[1:3], 1) ≈ sum(v .* w1[1:3], dims = 1) -@test wsum(v, w2, 2) ≈ sum(v .* w2', dims = 2) -@test wsum(v, w3, 3) ≈ sum(v .* reshape(w3, 1, 1, 4), dims = 3) +@test wsum(v, w1[1:3], 1) ≈ sum(v .* w1[1:3], dims=1) +@test wsum(v, w2, 2) ≈ sum(v .* w2', dims=2) +@test wsum(v, w3, 3) ≈ sum(v .* reshape(w3, 1, 1, 4), dims=3) ## wsum for Arrays with non-BlasReal elements x = rand(1:100, 6, 8) w1 = rand(6) w2 = rand(8) -@test wsum(x, w1, 1) ≈ sum(x .* w1, dims = 1) -@test wsum(x, w2, 2) ≈ sum(x .* w2', dims = 2) +@test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) +@test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) ## wsum! x = rand(6) @@ -181,19 +181,19 @@ w2 = rand(8) r = ones(1, 8) @test wsum!(r, x, w1, 1; init=true) === r -@test r ≈ sum(x .* w1, dims = 1) +@test r ≈ sum(x .* w1, dims=1) r = ones(1, 8) @test wsum!(r, x, w1, 1; init=false) === r -@test r ≈ sum(x .* w1, dims = 1) .+ 1.0 +@test r ≈ sum(x .* w1, dims=1) .+ 1.0 r = ones(6) @test wsum!(r, x, w2, 2; init=true) === r -@test r ≈ sum(x .* w2', dims = 2) +@test r ≈ sum(x .* w2', dims=2) r = ones(6) @test wsum!(r, x, w2, 2; init=false) === r -@test r ≈ sum(x .* w2', dims = 2) .+ 1.0 +@test r ≈ sum(x .* w2', dims=2) .+ 1.0 x = rand(8, 6, 5) w1 = rand(8) @@ -202,27 +202,27 @@ w3 = rand(5) r = ones(1, 6, 5) @test wsum!(r, x, w1, 1; init=true) === r -@test r ≈ sum(x .* w1, dims = 1) +@test r ≈ sum(x .* w1, dims=1) r = ones(1, 6, 5) @test wsum!(r, x, w1, 1; init=false) === r -@test r ≈ sum(x .* w1, dims = 1) .+ 1.0 +@test r ≈ sum(x .* w1, dims=1) .+ 1.0 r = ones(8, 1, 5) @test wsum!(r, x, w2, 2; init=true) === r -@test r ≈ sum(x .* w2', dims = 2) +@test r ≈ sum(x .* w2', dims=2) r = ones(8, 1, 5) @test wsum!(r, x, w2, 2; init=false) === r -@test r ≈ sum(x .* w2', dims = 2) .+ 1.0 +@test r ≈ sum(x .* w2', dims=2) .+ 1.0 r = ones(8, 6) @test wsum!(r, x, w3, 3; init=true) === r -@test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims = 3) +@test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims=3) r = ones(8, 6) @test wsum!(r, x, w3, 3; init=false) === r -@test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims = 3) .+ 1.0 +@test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims=3) .+ 1.0 ## the sum and mean syntax a = reshape(1.0:27.0, 3, 3, 3) @@ -232,9 +232,9 @@ a = reshape(1.0:27.0, 3, 3, 3) @test sum(1:3, f([1.0, 1.0, 0.5])) ≈ 4.5 for wt in ([1.0, 1.0, 1.0], [1.0, 0.2, 0.0], [0.2, 0.0, 1.0]) - @test sum(a, f(wt), dims = 1) ≈ sum(a.*reshape(wt, length(wt), 1, 1), dims = 1) - @test sum(a, f(wt), dims = 2) ≈ sum(a.*reshape(wt, 1, length(wt), 1), dims = 2) - @test sum(a, f(wt), dims = 3) ≈ sum(a.*reshape(wt, 1, 1, length(wt)), dims = 3) + @test sum(a, f(wt), dims=1) ≈ sum(a.*reshape(wt, length(wt), 1, 1), dims=1) + @test sum(a, f(wt), dims=2) ≈ sum(a.*reshape(wt, 1, length(wt), 1), dims=2) + @test sum(a, f(wt), dims=3) ≈ sum(a.*reshape(wt, 1, 1, length(wt)), dims=3) end end @@ -467,20 +467,20 @@ end @test sum([1.0, 2.0, 3.0], wt) ≈ 6.0 @test mean([1.0, 2.0, 3.0], wt) ≈ 2.0 - @test sum(a, wt, dims = 1) ≈ sum(a, dims = 1) - @test sum(a, wt, dims = 2) ≈ sum(a, dims = 2) - @test sum(a, wt, dims = 3) ≈ sum(a, dims = 3) + @test sum(a, wt, dims=1) ≈ sum(a, dims=1) + @test sum(a, wt, dims=2) ≈ sum(a, dims=2) + @test sum(a, wt, dims=3) ≈ sum(a, dims=3) - @test wsum(a, wt, dims = 1) ≈ sum(a, dims = 1) - @test wsum(a, wt, dims = 2) ≈ sum(a, dims = 2) - @test wsum(a, wt, dims = 3) ≈ sum(a, dims = 3) + @test wsum(a, wt, 1) ≈ sum(a, dims=1) + @test wsum(a, wt, 2) ≈ sum(a, dims=2) + @test wsum(a, wt, 3) ≈ sum(a, dims=3) - @test mean(a, wt, dims = 1) ≈ mean(a, dims = 1) - @test mean(a, wt, dims = 2) ≈ mean(a, dims = 2) - @test mean(a, wt, dims = 3) ≈ mean(a, dims = 3) + @test mean(a, wt, dims=1) ≈ mean(a, dims=1) + @test mean(a, wt, dims=2) ≈ mean(a, dims=2) + @test mean(a, wt, dims=3) ≈ mean(a, dims=3) @test_throws DimensionMismatch sum(a, wt) - @test_throws DimensionMismatch sum(a, wt, 4) + @test_throws DimensionMismatch sum(a, wt, dims=4) @test_throws DimensionMismatch wsum(a, wt, 4) @test_throws DimensionMismatch mean(a, wt, dims=4) From 49576137023938546bdacd6e67f3fc865ed03d6b Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 21:16:21 +0200 Subject: [PATCH 11/35] Patches --- src/deprecates.jl | 4 +++- src/weights.jl | 40 ++++++++++++++++++++++++---------------- test/weights.jl | 14 ++++++++++---- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/deprecates.jl b/src/deprecates.jl index 6b392129e..bc87f4a0f 100644 --- a/src/deprecates.jl +++ b/src/deprecates.jl @@ -37,4 +37,6 @@ end @deprecate quantile(v::AbstractArray{<:Real}) quantile(v, [.0, .25, .5, .75, 1.0]) ### Deprecated September 2019 -@deprecate values(wv::AbstractWeights) convert(Vector, wv) \ No newline at end of file +@deprecate sum(A::AbstractArray, w::AbstractWeights, dims::Int) sum(A, w, dims=dims) +@deprecate values(wv::AbstractWeights{S, T, V}) where {S, T, V} convert(V, wv) +@deprecate values(wv::UnitWeights) convert(Vector, wv) \ No newline at end of file diff --git a/src/weights.jl b/src/weights.jl index e4fbb4f80..5e14c3e2a 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -1,6 +1,5 @@ ##### Weight vector ##### - -abstract type AbstractWeights{S<:Real, T<:Real} <: AbstractVector{T} end +abstract type AbstractWeights{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractVector{T} end """ @weights name @@ -10,8 +9,8 @@ and stores the `values` (`V<:RealVector`) and `sum` (`S<:Real`). """ macro weights(name) return quote - mutable struct $name{S<:Real, T<:Real} <: AbstractWeights{S, T} - values::AbstractVector{T} + mutable struct $name{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractWeights{S, T, V} + values::V sum::S end $(esc(name))(vs) = $(esc(name))(vs, sum(vs)) @@ -23,8 +22,8 @@ sum(wv::AbstractWeights) = wv.sum isempty(wv::AbstractWeights) = isempty(wv.values) size(wv::AbstractWeights) = size(wv.values) -convert(::Type{AbstractVector}, wv::AbstractWeights) = wv.values -convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values) +Base.convert(::Type{AbstractVector}, wv::AbstractWeights) = wv.values +Base.convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values) Base.getindex(wv::AbstractWeights, i) = getindex(wv.values, i) @@ -252,7 +251,7 @@ eweights(t::AbstractVector, r::AbstractRange, λ::Real) = # NOTE: no variance correction is implemented for exponential weights -struct UnitWeights{T<:Real} <: AbstractWeights{Int, T} +struct UnitWeights{T<:Real} <: AbstractWeights{Int, T, V where V<:Vector{T}} len::Int end @@ -268,8 +267,8 @@ isempty(wv::UnitWeights) = iszero(wv.len) length(wv::UnitWeights) = wv.len size(wv::UnitWeights) = Tuple(length(wv)) -convert(::Type{AbstractVector}, wv::UnitWeights{T}) where {T} = ones(T, length(wv)) -convert(::Type{Vector}, wv::UnitWeights{T}) where {T} = ones(T, length(wv)) +Base.convert(::Type{AbstractVector}, wv::UnitWeights{T}) where T = ones(T, length(wv)) +Base.convert(::Type{Vector}, wv::UnitWeights{T}) where T = ones(T, length(wv)) @propagate_inbounds function Base.getindex(wv::UnitWeights{T}, i::Integer) where T @boundscheck checkbounds(wv, i) @@ -320,7 +319,7 @@ This definition is equivalent to the correction applied to unweighted data. corrected ? (1 / (w.len - 1)) : (1 / w.len) end -##### Equality tests ##### +#### Equality tests ##### for w in (AnalyticWeights, FrequencyWeights, ProbabilityWeights, Weights) @eval begin @@ -381,6 +380,7 @@ wsum(v::AbstractArray, w::AbstractVector) = dot(vec(v), w) # (d) A is a general dense array with eltype <: BlasReal: # dim <= 2: delegate to (a) and (b) # otherwise, decompose A into multiple pages +# function _wsum1!(R::AbstractArray, A::AbstractVector, w::AbstractVector, init::Bool) r = wsum(A, w) @@ -521,6 +521,7 @@ _wsum!(R::AbstractArray, A::AbstractArray, w::AbstractVector, dim::Int, init::Bo wsumtype(::Type{T}, ::Type{W}) where {T,W} = typeof(zero(T) * zero(W) + zero(T) * zero(W)) wsumtype(::Type{T}, ::Type{T}) where {T<:BlasReal} = T + """ wsum!(R, A, w, dim; init=true) @@ -549,17 +550,24 @@ end ## extended sum! and wsum Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::Int; init::Bool=true) = - wsum!(R, A, values(w), dim; init=init) + wsum!(R, A, w.values, dim; init=init) -Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims::Union{Nothing,Int}=nothing) = - dims == nothing ? wsum(A, w.values) : wsum(A, w.values, dims) +function Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims::Union{Nothing,Int}=nothing) + if dims === nothing + length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) + return wsum(A, w.values) + else + size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) + return wsum(A, w.values, dims) + end +end function Base.sum(A::AbstractArray{<:Number}, w::UnitWeights; dims::Union{Nothing,Int}=nothing) - if dims == nothing - size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) + if dims === nothing + length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return sum(A) else - length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) + size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return sum(A, dims=dims) end end diff --git a/test/weights.jl b/test/weights.jl index 6136b74f9..102ebd616 100644 --- a/test/weights.jl +++ b/test/weights.jl @@ -4,7 +4,8 @@ using LinearAlgebra, Random, SparseArrays, Test @testset "StatsBase.Weights" begin weight_funcs = (weights, aweights, fweights, pweights) -# Construction +## Construction + @testset "$f" for f in weight_funcs @test isa(f([1, 2, 3]), AbstractWeights{Int}) @test isa(f([1., 2., 3.]), AbstractWeights{Float64}) @@ -17,7 +18,7 @@ weight_funcs = (weights, aweights, fweights, pweights) wv = f(w) @test eltype(wv) === Float64 @test length(wv) === 3 - @test values(wv) === w + @test values(wv) == w @test sum(wv) === 6.0 @test !isempty(wv) @@ -25,7 +26,7 @@ weight_funcs = (weights, aweights, fweights, pweights) bv = f(b) @test eltype(bv) === Bool @test length(bv) === 3 - @test values(bv) === b + @test values(bv) == b @test sum(bv) === 3 @test !isempty(bv) @@ -114,6 +115,7 @@ end end ## wsum + x = [6., 8., 9.] w = [2., 3., 4.] p = [1. 2. ; 3. 4.] @@ -124,6 +126,7 @@ q = [1., 2., 3., 4.] @test wsum(p, q) === 29.0 ## wsum along dimension + @test wsum(x, w, 1) == [72.0] x = rand(6, 8) @@ -156,6 +159,7 @@ v = view(x, 2:4, :, :) @test wsum(v, w3, 3) ≈ sum(v .* reshape(w3, 1, 1, 4), dims=3) ## wsum for Arrays with non-BlasReal elements + x = rand(1:100, 6, 8) w1 = rand(6) w2 = rand(8) @@ -164,6 +168,7 @@ w2 = rand(8) @test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) ## wsum! + x = rand(6) w = rand(6) @@ -224,7 +229,8 @@ r = ones(8, 6) @test wsum!(r, x, w3, 3; init=false) === r @test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims=3) .+ 1.0 -## the sum and mean syntax +## sum, mean and quantile + a = reshape(1.0:27.0, 3, 3, 3) @testset "Sum $f" for f in weight_funcs From d25dbf46ff317918a8960549d766012018aad533 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 22 Sep 2019 21:40:06 +0200 Subject: [PATCH 12/35] Indexing by array returns new weight vector --- src/weights.jl | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 5e14c3e2a..b6265b4c0 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -25,7 +25,18 @@ size(wv::AbstractWeights) = size(wv.values) Base.convert(::Type{AbstractVector}, wv::AbstractWeights) = wv.values Base.convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values) -Base.getindex(wv::AbstractWeights, i) = getindex(wv.values, i) +@propagate_inbounds function Base.getindex(wv::W, i::Integer) where W <: AbstractWeights + @boundscheck checkbounds(wv, i) + wv.values[i] +end + +@propagate_inbounds function Base.getindex(wv::W, i::AbstractArray{<:Int}) where W <: AbstractWeights + @boundscheck checkbounds(wv, i) + v = wv.values[i] + W(v, sum(v)) +end + +Base.getindex(wv::AbstractWeights, ::Colon) = wv @propagate_inbounds function Base.setindex!(wv::AbstractWeights, v::Real, i::Int) s = v - wv[i] @@ -277,10 +288,10 @@ end @propagate_inbounds function Base.getindex(wv::UnitWeights{T}, i::AbstractArray{<:Int}) where T @boundscheck checkbounds(wv, i) - fill(one(T), size(i)) + UnitWeights{T}(length(i)) end -Base.getindex(wv::UnitWeights{T}, ::Colon) where T = fill(one(T), length(wv)) +Base.getindex(wv::UnitWeights, ::Colon) = wv """ uweights(s::Integer) From 904abc561755683f0d8ef120481992318a356e1f Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:10:12 +0200 Subject: [PATCH 13/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index b6265b4c0..e9ee8988f 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -30,7 +30,7 @@ Base.convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values) wv.values[i] end -@propagate_inbounds function Base.getindex(wv::W, i::AbstractArray{<:Int}) where W <: AbstractWeights +@propagate_inbounds function Base.getindex(wv::W, i::AbstractArray) where W <: AbstractWeights @boundscheck checkbounds(wv, i) v = wv.values[i] W(v, sum(v)) From be6d6c7d66e64f2c1a2e200acbedcffa7a8f7a30 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:11:50 +0200 Subject: [PATCH 14/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index e9ee8988f..1aef9ddc3 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -25,7 +25,7 @@ size(wv::AbstractWeights) = size(wv.values) Base.convert(::Type{AbstractVector}, wv::AbstractWeights) = wv.values Base.convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values) -@propagate_inbounds function Base.getindex(wv::W, i::Integer) where W <: AbstractWeights +@propagate_inbounds function Base.getindex(wv::AbstractWeights, i::Integer) @boundscheck checkbounds(wv, i) wv.values[i] end From f98e4c0e3f4d8fcd944375eed66255477ae4f1a4 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:12:27 +0200 Subject: [PATCH 15/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 1aef9ddc3..6935134d3 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -279,7 +279,7 @@ length(wv::UnitWeights) = wv.len size(wv::UnitWeights) = Tuple(length(wv)) Base.convert(::Type{AbstractVector}, wv::UnitWeights{T}) where T = ones(T, length(wv)) -Base.convert(::Type{Vector}, wv::UnitWeights{T}) where T = ones(T, length(wv)) +Base.convert(::Type{Vector}, wv::UnitWeights{T}) where {T} = ones(T, length(wv)) @propagate_inbounds function Base.getindex(wv::UnitWeights{T}, i::Integer) where T @boundscheck checkbounds(wv, i) From fec6f5e67ffa77fcd00be22655f3a7ac72fc102a Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:12:47 +0200 Subject: [PATCH 16/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 6935134d3..59a0510ca 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -22,7 +22,6 @@ sum(wv::AbstractWeights) = wv.sum isempty(wv::AbstractWeights) = isempty(wv.values) size(wv::AbstractWeights) = size(wv.values) -Base.convert(::Type{AbstractVector}, wv::AbstractWeights) = wv.values Base.convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values) @propagate_inbounds function Base.getindex(wv::AbstractWeights, i::Integer) From 5277140c798901d9073327779a338e4d46a27585 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:12:57 +0200 Subject: [PATCH 17/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 59a0510ca..b820434b4 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -31,7 +31,7 @@ end @propagate_inbounds function Base.getindex(wv::W, i::AbstractArray) where W <: AbstractWeights @boundscheck checkbounds(wv, i) - v = wv.values[i] + @inbounds v = wv.values[i] W(v, sum(v)) end From 5fe88cc3f84f87cbfc600705dc8d434d2f4cc19f Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:13:14 +0200 Subject: [PATCH 18/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index b820434b4..2f8d22fa7 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -277,7 +277,6 @@ isempty(wv::UnitWeights) = iszero(wv.len) length(wv::UnitWeights) = wv.len size(wv::UnitWeights) = Tuple(length(wv)) -Base.convert(::Type{AbstractVector}, wv::UnitWeights{T}) where T = ones(T, length(wv)) Base.convert(::Type{Vector}, wv::UnitWeights{T}) where {T} = ones(T, length(wv)) @propagate_inbounds function Base.getindex(wv::UnitWeights{T}, i::Integer) where T From 6c707f631fccd8f81054f1074593ed3b058b2980 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:13:22 +0200 Subject: [PATCH 19/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 2f8d22fa7..23590935a 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -26,7 +26,7 @@ Base.convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values) @propagate_inbounds function Base.getindex(wv::AbstractWeights, i::Integer) @boundscheck checkbounds(wv, i) - wv.values[i] + @inbounds wv.values[i] end @propagate_inbounds function Base.getindex(wv::W, i::AbstractArray) where W <: AbstractWeights From dea5fc454cf2c8faf77af4f4074ad51dea011c20 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:13:46 +0200 Subject: [PATCH 20/35] Update src/deprecates.jl Co-Authored-By: Milan Bouchet-Valat --- src/deprecates.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/deprecates.jl b/src/deprecates.jl index bc87f4a0f..d4d605042 100644 --- a/src/deprecates.jl +++ b/src/deprecates.jl @@ -38,5 +38,5 @@ end ### Deprecated September 2019 @deprecate sum(A::AbstractArray, w::AbstractWeights, dims::Int) sum(A, w, dims=dims) -@deprecate values(wv::AbstractWeights{S, T, V}) where {S, T, V} convert(V, wv) -@deprecate values(wv::UnitWeights) convert(Vector, wv) \ No newline at end of file +@deprecate values(wv::AbstractWeights) convert(Vector, wv) +@deprecate values(wv::UnitWeights) convert(Vector, wv) From ea6eaf714c8871a285fea25244f5214a051e2f55 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:24:46 +0200 Subject: [PATCH 21/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 23590935a..98c5a7df6 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -35,7 +35,7 @@ end W(v, sum(v)) end -Base.getindex(wv::AbstractWeights, ::Colon) = wv +Base.getindex(wv::W, ::Colon) where {W <: AbstractWeights} = W(copy(wv.values), sum(wv)) @propagate_inbounds function Base.setindex!(wv::AbstractWeights, v::Real, i::Int) s = v - wv[i] From baa4b9cbfbc91eb0acd4eb5ad522a2343e8a77e1 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Mon, 23 Sep 2019 20:26:59 +0200 Subject: [PATCH 22/35] Update src/weights.jl --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 98c5a7df6..1744e7784 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -289,7 +289,7 @@ end UnitWeights{T}(length(i)) end -Base.getindex(wv::UnitWeights, ::Colon) = wv +Base.getindex(wv::UnitWeights{T}, ::Colon) where {T} = UnitWeights{T}(wv.len) """ uweights(s::Integer) From b22f5bd4523743f1fa4daa8ffbfff02cf1b85f3d Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sat, 28 Sep 2019 22:10:56 +0200 Subject: [PATCH 23/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 1744e7784..d8720332d 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -559,7 +559,7 @@ end ## extended sum! and wsum Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::Int; init::Bool=true) = - wsum!(R, A, w.values, dim; init=init) + wsum!(R, A, w, dim; init=init) function Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims::Union{Nothing,Int}=nothing) if dims === nothing From b225e846d6af16de431a29c1fa1043491e0fe9c5 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sat, 28 Sep 2019 22:11:04 +0200 Subject: [PATCH 24/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index d8720332d..dffa38df9 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -564,7 +564,7 @@ Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::I function Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims::Union{Nothing,Int}=nothing) if dims === nothing length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return wsum(A, w.values) + return wsum(A, w) else size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return wsum(A, w.values, dims) From 4b0fc7c6a5143dd671b0383eb2dec4feefa0b24d Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sat, 28 Sep 2019 22:11:12 +0200 Subject: [PATCH 25/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index dffa38df9..01fc4ec33 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -567,7 +567,7 @@ function Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims:: return wsum(A, w) else size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return wsum(A, w.values, dims) + return wsum(A, w, dims) end end From b7ea5acc865c744fc78cbf2e48927f8a971c6bf1 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 13:23:25 +0200 Subject: [PATCH 26/35] Simplify sum --- src/weights.jl | 21 ++---- test/weights.jl | 166 ++++++++++++++++++++++++------------------------ 2 files changed, 90 insertions(+), 97 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 01fc4ec33..6d4c406c5 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -354,6 +354,7 @@ Compute the weighted sum of an array `v` with weights `w`, optionally over the d """ wsum(v::AbstractVector, w::AbstractVector) = dot(v, w) wsum(v::AbstractArray, w::AbstractVector) = dot(vec(v), w) +wsum(v::AbstractArray, w::AbstractVector, dims::Colon) = wsum(v, w) ## wsum along dimension # @@ -389,7 +390,6 @@ wsum(v::AbstractArray, w::AbstractVector) = dot(vec(v), w) # (d) A is a general dense array with eltype <: BlasReal: # dim <= 2: delegate to (a) and (b) # otherwise, decompose A into multiple pages -# function _wsum1!(R::AbstractArray, A::AbstractVector, w::AbstractVector, init::Bool) r = wsum(A, w) @@ -530,7 +530,6 @@ _wsum!(R::AbstractArray, A::AbstractArray, w::AbstractVector, dim::Int, init::Bo wsumtype(::Type{T}, ::Type{W}) where {T,W} = typeof(zero(T) * zero(W) + zero(T) * zero(W)) wsumtype(::Type{T}, ::Type{T}) where {T<:BlasReal} = T - """ wsum!(R, A, w, dim; init=true) @@ -561,24 +560,16 @@ end Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::Int; init::Bool=true) = wsum!(R, A, w, dim; init=init) -function Base.sum(A::AbstractArray{<:Number}, w::AbstractWeights{<:Real}; dims::Union{Nothing,Int}=nothing) - if dims === nothing - length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return wsum(A, w) - else - size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return wsum(A, w, dims) - end -end +Base.sum(A::AbstractArray, w::AbstractWeights{<:Real}; dims::Union{Colon,Int}=Colon()) = + wsum(A, w, dims) -function Base.sum(A::AbstractArray{<:Number}, w::UnitWeights; dims::Union{Nothing,Int}=nothing) - if dims === nothing +function Base.sum(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=Colon()) + if dims === Colon() length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return sum(A) else size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return sum(A, dims=dims) end + return sum(A, dims=dims) end ##### Weighted means ##### diff --git a/test/weights.jl b/test/weights.jl index 102ebd616..4391cb511 100644 --- a/test/weights.jl +++ b/test/weights.jl @@ -116,118 +116,120 @@ end ## wsum -x = [6., 8., 9.] -w = [2., 3., 4.] -p = [1. 2. ; 3. 4.] -q = [1., 2., 3., 4.] +@testset "wsum" begin + x = [6., 8., 9.] + w = [2., 3., 4.] + p = [1. 2. ; 3. 4.] + q = [1., 2., 3., 4.] -@test wsum(Float64[], Float64[]) === 0.0 -@test wsum(x, w) === 72.0 -@test wsum(p, q) === 29.0 + @test wsum(Float64[], Float64[]) === 0.0 + @test wsum(x, w) === 72.0 + @test wsum(p, q) === 29.0 -## wsum along dimension + ## wsum along dimension -@test wsum(x, w, 1) == [72.0] + @test wsum(x, w, 1) == [72.0] -x = rand(6, 8) -w1 = rand(6) -w2 = rand(8) + x = rand(6, 8) + w1 = rand(6) + w2 = rand(8) -@test size(wsum(x, w1, 1)) == (1, 8) -@test size(wsum(x, w2, 2)) == (6, 1) + @test size(wsum(x, w1, 1)) == (1, 8) + @test size(wsum(x, w2, 2)) == (6, 1) -@test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) -@test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) + @test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) + @test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) -x = rand(6, 5, 4) -w1 = rand(6) -w2 = rand(5) -w3 = rand(4) + x = rand(6, 5, 4) + w1 = rand(6) + w2 = rand(5) + w3 = rand(4) -@test size(wsum(x, w1, 1)) == (1, 5, 4) -@test size(wsum(x, w2, 2)) == (6, 1, 4) -@test size(wsum(x, w3, 3)) == (6, 5, 1) + @test size(wsum(x, w1, 1)) == (1, 5, 4) + @test size(wsum(x, w2, 2)) == (6, 1, 4) + @test size(wsum(x, w3, 3)) == (6, 5, 1) -@test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) -@test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) -@test wsum(x, w3, 3) ≈ sum(x .* reshape(w3, 1, 1, 4), dims=3) + @test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) + @test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) + @test wsum(x, w3, 3) ≈ sum(x .* reshape(w3, 1, 1, 4), dims=3) -v = view(x, 2:4, :, :) + v = view(x, 2:4, :, :) -@test wsum(v, w1[1:3], 1) ≈ sum(v .* w1[1:3], dims=1) -@test wsum(v, w2, 2) ≈ sum(v .* w2', dims=2) -@test wsum(v, w3, 3) ≈ sum(v .* reshape(w3, 1, 1, 4), dims=3) + @test wsum(v, w1[1:3], 1) ≈ sum(v .* w1[1:3], dims=1) + @test wsum(v, w2, 2) ≈ sum(v .* w2', dims=2) + @test wsum(v, w3, 3) ≈ sum(v .* reshape(w3, 1, 1, 4), dims=3) -## wsum for Arrays with non-BlasReal elements + ## wsum for Arrays with non-BlasReal elements -x = rand(1:100, 6, 8) -w1 = rand(6) -w2 = rand(8) + x = rand(1:100, 6, 8) + w1 = rand(6) + w2 = rand(8) -@test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) -@test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) + @test wsum(x, w1, 1) ≈ sum(x .* w1, dims=1) + @test wsum(x, w2, 2) ≈ sum(x .* w2', dims=2) -## wsum! + ## wsum! -x = rand(6) -w = rand(6) + x = rand(6) + w = rand(6) -r = ones(1) -@test wsum!(r, x, w, 1; init=true) === r -@test r ≈ [dot(x, w)] + r = ones(1) + @test wsum!(r, x, w, 1; init=true) === r + @test r ≈ [dot(x, w)] -r = ones(1) -@test wsum!(r, x, w, 1; init=false) === r -@test r ≈ [dot(x, w) + 1.0] + r = ones(1) + @test wsum!(r, x, w, 1; init=false) === r + @test r ≈ [dot(x, w) + 1.0] -x = rand(6, 8) -w1 = rand(6) -w2 = rand(8) + x = rand(6, 8) + w1 = rand(6) + w2 = rand(8) -r = ones(1, 8) -@test wsum!(r, x, w1, 1; init=true) === r -@test r ≈ sum(x .* w1, dims=1) + r = ones(1, 8) + @test wsum!(r, x, w1, 1; init=true) === r + @test r ≈ sum(x .* w1, dims=1) -r = ones(1, 8) -@test wsum!(r, x, w1, 1; init=false) === r -@test r ≈ sum(x .* w1, dims=1) .+ 1.0 + r = ones(1, 8) + @test wsum!(r, x, w1, 1; init=false) === r + @test r ≈ sum(x .* w1, dims=1) .+ 1.0 -r = ones(6) -@test wsum!(r, x, w2, 2; init=true) === r -@test r ≈ sum(x .* w2', dims=2) + r = ones(6) + @test wsum!(r, x, w2, 2; init=true) === r + @test r ≈ sum(x .* w2', dims=2) -r = ones(6) -@test wsum!(r, x, w2, 2; init=false) === r -@test r ≈ sum(x .* w2', dims=2) .+ 1.0 + r = ones(6) + @test wsum!(r, x, w2, 2; init=false) === r + @test r ≈ sum(x .* w2', dims=2) .+ 1.0 -x = rand(8, 6, 5) -w1 = rand(8) -w2 = rand(6) -w3 = rand(5) + x = rand(8, 6, 5) + w1 = rand(8) + w2 = rand(6) + w3 = rand(5) -r = ones(1, 6, 5) -@test wsum!(r, x, w1, 1; init=true) === r -@test r ≈ sum(x .* w1, dims=1) + r = ones(1, 6, 5) + @test wsum!(r, x, w1, 1; init=true) === r + @test r ≈ sum(x .* w1, dims=1) -r = ones(1, 6, 5) -@test wsum!(r, x, w1, 1; init=false) === r -@test r ≈ sum(x .* w1, dims=1) .+ 1.0 + r = ones(1, 6, 5) + @test wsum!(r, x, w1, 1; init=false) === r + @test r ≈ sum(x .* w1, dims=1) .+ 1.0 -r = ones(8, 1, 5) -@test wsum!(r, x, w2, 2; init=true) === r -@test r ≈ sum(x .* w2', dims=2) + r = ones(8, 1, 5) + @test wsum!(r, x, w2, 2; init=true) === r + @test r ≈ sum(x .* w2', dims=2) -r = ones(8, 1, 5) -@test wsum!(r, x, w2, 2; init=false) === r -@test r ≈ sum(x .* w2', dims=2) .+ 1.0 + r = ones(8, 1, 5) + @test wsum!(r, x, w2, 2; init=false) === r + @test r ≈ sum(x .* w2', dims=2) .+ 1.0 -r = ones(8, 6) -@test wsum!(r, x, w3, 3; init=true) === r -@test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims=3) + r = ones(8, 6) + @test wsum!(r, x, w3, 3; init=true) === r + @test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims=3) -r = ones(8, 6) -@test wsum!(r, x, w3, 3; init=false) === r -@test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims=3) .+ 1.0 + r = ones(8, 6) + @test wsum!(r, x, w3, 3; init=false) === r + @test r ≈ sum(x .* reshape(w3, (1, 1, 5)), dims=3) .+ 1.0 +end ## sum, mean and quantile From cd2b28c3dec68957ed936dc4a3013044995e0b88 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 20:20:51 +0200 Subject: [PATCH 27/35] Simplify sum and mean for unit weights --- src/weights.jl | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 6d4c406c5..06a445ae7 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -564,11 +564,8 @@ Base.sum(A::AbstractArray, w::AbstractWeights{<:Real}; dims::Union{Colon,Int}=Co wsum(A, w, dims) function Base.sum(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=Colon()) - if dims === Colon() - length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - else - size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - end + a = dims === Colon() ? length(A) : size(A, dims) + a != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return sum(A, dims=dims) end @@ -590,9 +587,10 @@ end Compute the weighted mean of array `A` with weight vector `w` (of type `AbstractWeights`) along dimension `dims`, and write results to `R`. """ -mean!(R::AbstractArray, A::AbstractArray, w::AbstractWeights; - dims::Union{Nothing,Int}=nothing) = _mean!(R, A, w, dims) -_mean!(R::AbstractArray, A::AbstractArray, w::AbstractWeights, dims::Nothing) = throw(ArgumentError("dims argument must be provided")) +mean!(R::AbstractArray, A::AbstractArray, w::AbstractWeights; dims::Union{Nothing,Int}=nothing) = + _mean!(R, A, w, dims) +_mean!(R::AbstractArray, A::AbstractArray, w::AbstractWeights, dims::Nothing) = + throw(ArgumentError("dims argument must be provided")) _mean!(R::AbstractArray, A::AbstractArray, w::AbstractWeights, dims::Int) = rmul!(Base.sum!(R, A, w, dims), inv(sum(w))) @@ -612,20 +610,16 @@ w = rand(n) mean(x, weights(w)) ``` """ -mean(A::AbstractArray, w::AbstractWeights; dims::Union{Nothing,Int}=nothing) = +mean(A::AbstractArray, w::AbstractWeights; dims::Union{Colon,Int}=Colon()) = _mean(A, w, dims) -_mean(A::AbstractArray, w::AbstractWeights, dims::Nothing) = +_mean(A::AbstractArray, w::AbstractWeights, dims::Colon) = sum(A, w) / sum(w) _mean(A::AbstractArray{T}, w::AbstractWeights{W}, dims::Int) where {T,W} = _mean!(similar(A, wmeantype(T, W), Base.reduced_indices(axes(A), dims)), A, w, dims) -function _mean(A::AbstractArray, w::UnitWeights, dims::Nothing) - length(A) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) - return mean(A) -end - -function _mean(A::AbstractArray, w::UnitWeights, dims::Int) - size(A, dims) != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) +function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=Colon()) + a = dims === Colon() ? length(A) : size(A, dims) + a != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return mean(A, dims=dims) end From 9e8c5eee8e94b8f8945a829e38d294f1e57880cf Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 21:00:35 +0200 Subject: [PATCH 28/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 06a445ae7..3df481d65 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -560,7 +560,7 @@ end Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::Int; init::Bool=true) = wsum!(R, A, w, dim; init=init) -Base.sum(A::AbstractArray, w::AbstractWeights{<:Real}; dims::Union{Colon,Int}=Colon()) = +Base.sum(A::AbstractArray, w::AbstractWeights{<:Real}; dims::Union{Colon,Int}=:) = wsum(A, w, dims) function Base.sum(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=Colon()) From 2e22c432be89a8792caf748bfe15f8258f7dbb77 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 21:00:55 +0200 Subject: [PATCH 29/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 3df481d65..5b62dab9c 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -563,7 +563,7 @@ Base.sum!(R::AbstractArray, A::AbstractArray, w::AbstractWeights{<:Real}, dim::I Base.sum(A::AbstractArray, w::AbstractWeights{<:Real}; dims::Union{Colon,Int}=:) = wsum(A, w, dims) -function Base.sum(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=Colon()) +function Base.sum(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) a = dims === Colon() ? length(A) : size(A, dims) a != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return sum(A, dims=dims) From aa7c9626ce215c0b7f35f4a1a2ad77289d029d6a Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 21:01:28 +0200 Subject: [PATCH 30/35] Update src/deprecates.jl Co-Authored-By: Milan Bouchet-Valat --- src/deprecates.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/deprecates.jl b/src/deprecates.jl index d4d605042..9e409b33a 100644 --- a/src/deprecates.jl +++ b/src/deprecates.jl @@ -39,4 +39,3 @@ end ### Deprecated September 2019 @deprecate sum(A::AbstractArray, w::AbstractWeights, dims::Int) sum(A, w, dims=dims) @deprecate values(wv::AbstractWeights) convert(Vector, wv) -@deprecate values(wv::UnitWeights) convert(Vector, wv) From 3dfb0f658abc842de36e1dadabc1e7114b6cc126 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 21:01:38 +0200 Subject: [PATCH 31/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 5b62dab9c..61586a43c 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -610,7 +610,7 @@ w = rand(n) mean(x, weights(w)) ``` """ -mean(A::AbstractArray, w::AbstractWeights; dims::Union{Colon,Int}=Colon()) = +mean(A::AbstractArray, w::AbstractWeights; dims::Union{Colon,Int}=:) = _mean(A, w, dims) _mean(A::AbstractArray, w::AbstractWeights, dims::Colon) = sum(A, w) / sum(w) From 5da60a642ce53e85c91edd6b7eff355f5793cb63 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 21:01:45 +0200 Subject: [PATCH 32/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 61586a43c..594030f38 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -564,7 +564,7 @@ Base.sum(A::AbstractArray, w::AbstractWeights{<:Real}; dims::Union{Colon,Int}=:) wsum(A, w, dims) function Base.sum(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) - a = dims === Colon() ? length(A) : size(A, dims) + a = dims === : ? length(A) : size(A, dims) a != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return sum(A, dims=dims) end From 79cde8990da8a6ba1915ba73f4aa1b021ae58c40 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 21:01:53 +0200 Subject: [PATCH 33/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 594030f38..67ea8acaf 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -618,7 +618,7 @@ _mean(A::AbstractArray{T}, w::AbstractWeights{W}, dims::Int) where {T,W} = _mean!(similar(A, wmeantype(T, W), Base.reduced_indices(axes(A), dims)), A, w, dims) function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=Colon()) - a = dims === Colon() ? length(A) : size(A, dims) + a = dims === : ? length(A) : size(A, dims) a != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return mean(A, dims=dims) end From d65ca65b21d11799fd7c8cce01e9b84ea35f5459 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 21:01:59 +0200 Subject: [PATCH 34/35] Update src/weights.jl Co-Authored-By: Milan Bouchet-Valat --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 67ea8acaf..3d3f4d2ad 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -617,7 +617,7 @@ _mean(A::AbstractArray, w::AbstractWeights, dims::Colon) = _mean(A::AbstractArray{T}, w::AbstractWeights{W}, dims::Int) where {T,W} = _mean!(similar(A, wmeantype(T, W), Base.reduced_indices(axes(A), dims)), A, w, dims) -function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=Colon()) +function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) a = dims === : ? length(A) : size(A, dims) a != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return mean(A, dims=dims) From 6f974ea632f9f6ac17a7714fa7df0fc5bc59ed14 Mon Sep 17 00:00:00 2001 From: Luca Bittarello Date: Sun, 29 Sep 2019 21:13:45 +0200 Subject: [PATCH 35/35] Required parentheses added --- src/weights.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 3d3f4d2ad..e5df6b738 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -564,7 +564,7 @@ Base.sum(A::AbstractArray, w::AbstractWeights{<:Real}; dims::Union{Colon,Int}=:) wsum(A, w, dims) function Base.sum(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) - a = dims === : ? length(A) : size(A, dims) + a = (dims === :) ? length(A) : size(A, dims) a != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return sum(A, dims=dims) end @@ -618,7 +618,7 @@ _mean(A::AbstractArray{T}, w::AbstractWeights{W}, dims::Int) where {T,W} = _mean!(similar(A, wmeantype(T, W), Base.reduced_indices(axes(A), dims)), A, w, dims) function mean(A::AbstractArray, w::UnitWeights; dims::Union{Colon,Int}=:) - a = dims === : ? length(A) : size(A, dims) + a = (dims === :) ? length(A) : size(A, dims) a != length(w) && throw(DimensionMismatch("Inconsistent array dimension.")) return mean(A, dims=dims) end