From dc17308474c49d0ab4c6e79161bf53644f50b080 Mon Sep 17 00:00:00 2001 From: Rory-Finnegan Date: Thu, 20 Apr 2017 16:56:09 -0500 Subject: [PATCH 1/6] Made `WeightVec` a subtype of `RealVector` --- src/weights.jl | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 196a6cd37..9d2d1ddaa 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -1,9 +1,9 @@ ###### Weight vector ##### -immutable WeightVec{W,Vec<:RealVector} - values::Vec - sum::W +immutable WeightVec{T, V<:RealVector, S} <: RealVector{T} + values::V + sum::S end """ @@ -12,8 +12,14 @@ end Construct a `WeightVec` with weight values `vs` and sum of weights `wsum`. If omitted, `wsum` is computed. """ -WeightVec{Vec<:RealVector,W<:Real}(vs::Vec,wsum::W) = WeightVec{W,Vec}(vs, wsum) -WeightVec(vs::RealVector) = WeightVec(vs, sum(vs)) +function WeightVec{V<:RealVector}(vs::V) + sum_ = sum(vs) + return WeightVec{eltype(vs), V, typeof(sum_)}(vs, sum_) +end + +function WeightVec{S, V<:RealVector}(vs::V, s::S) + return WeightVec{eltype(vs), V, S}(vs, s) +end """ weights(vs) @@ -30,6 +36,7 @@ sum(wv::WeightVec) = wv.sum isempty(wv::WeightVec) = isempty(wv.values) Base.getindex(wv::WeightVec, i) = getindex(wv.values, i) +Base.size(wv::WeightVec) = size(wv.values) ##### Weighted sum ##### @@ -281,6 +288,9 @@ Base.mean{T<:Number,W<:Real}(A::AbstractArray{T}, w::WeightVec{W}, dim::Int) = ###### Weighted median ##### +function Base.median(v::AbstractArray, w::WeightVec) + throw(MethodError(median, (typeof(v), typeof(w)))) +end function Base.median{W<:Real}(v::RealVector, w::WeightVec{W}) isempty(v) && error("median of an empty array is undefined") From 0248bac47cc78e29f16ad82876d5c43d4707ef9a Mon Sep 17 00:00:00 2001 From: Rory-Finnegan Date: Thu, 20 Apr 2017 18:17:14 -0500 Subject: [PATCH 2/6] Added a few tests to weights.jl --- test/weights.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/weights.jl b/test/weights.jl index a7a873799..f712dc01f 100644 --- a/test/weights.jl +++ b/test/weights.jl @@ -5,10 +5,12 @@ import Compat: view @test isa(weights([1, 2, 3]), WeightVec{Int}) @test isa(weights([1., 2., 3.]), WeightVec{Float64}) - @test isa(weights([1 2 3; 4 5 6]), WeightVec{Int}) +@test isa(WeightVec([1, 2, 3], 6), WeightVec{Int}) + @test isempty(weights(Float64[])) +@test size(weights([1, 2, 3])) == (3,) w = [1., 2., 3.] wv = weights(w) @@ -26,6 +28,11 @@ bv = weights(b) @test sum(bv) === 3 @test !isempty(bv) +ba = BitArray([true, false, true]) +sa = sparsevec([1., 0., 2.]) + +@test sum(ba, wv) === 4.0 +@test sum(sa, wv) === 7.0 ## wsum From 3cfbc211a1609c8dc0cc6b7ad9e04c39355bc266 Mon Sep 17 00:00:00 2001 From: Rory-Finnegan Date: Fri, 21 Apr 2017 15:59:22 -0500 Subject: [PATCH 3/6] Changed parameterization of WeightVec. - Parameterization to `WeightVec{S<:Real, T<:Real, T<:AbstractVector}` - Uses triangular dispatch for julia version passed v"0.6.0-dev.2123" --- src/weights.jl | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 9d2d1ddaa..5b3574a59 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -1,24 +1,34 @@ ###### Weight vector ##### -immutable WeightVec{T, V<:RealVector, S} <: RealVector{T} - values::V - sum::S -end +if VERSION < v"0.6.0-dev.2123" + immutable WeightVec{S<:Real, T<:Real, V<:RealVector} <: RealVector{T} + values::V + sum::S + end -""" - WeightVec(vs, [wsum]) + function WeightVec{S<:Real, V<:RealVector}(vs::V, s::S) + return WeightVec{S, eltype(vs), V}(vs, s) + end -Construct a `WeightVec` with weight values `vs` and sum of weights `wsum`. -If omitted, `wsum` is computed. -""" -function WeightVec{V<:RealVector}(vs::V) - sum_ = sum(vs) - return WeightVec{eltype(vs), V, typeof(sum_)}(vs, sum_) -end + function WeightVec{V<:RealVector}(vs::V) + sum_ = sum(vs) + return WeightVec{typeof(sum_), eltype(vs), V}(vs, sum_) + end +else + immutable WeightVec{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractVector{T} + values::V + sum::S + end -function WeightVec{S, V<:RealVector}(vs::V, s::S) - return WeightVec{eltype(vs), V, S}(vs, s) + function WeightVec{S<:Real, T<:Real, V<:AbstractVector{T}}(vs::V, s::S) + return WeightVec{S, T, V}(vs, s) + end + + function WeightVec{T<:Real, V<:AbstractVector{T}}(vs::V) + sum_ = sum(vs) + return WeightVec{typeof(sum_), T, V}(vs, sum_) + end end """ From 5535cad57db258774db770dee8ca6a5072666124 Mon Sep 17 00:00:00 2001 From: Rory-Finnegan Date: Sat, 22 Apr 2017 13:02:40 -0500 Subject: [PATCH 4/6] Moved constructors out of version check. --- src/weights.jl | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 5b3574a59..79ab89fb3 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -6,29 +6,26 @@ if VERSION < v"0.6.0-dev.2123" values::V sum::S end - - function WeightVec{S<:Real, V<:RealVector}(vs::V, s::S) - return WeightVec{S, eltype(vs), V}(vs, s) - end - - function WeightVec{V<:RealVector}(vs::V) - sum_ = sum(vs) - return WeightVec{typeof(sum_), eltype(vs), V}(vs, sum_) - end else immutable WeightVec{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractVector{T} values::V sum::S end +end - function WeightVec{S<:Real, T<:Real, V<:AbstractVector{T}}(vs::V, s::S) - return WeightVec{S, T, V}(vs, s) - end +""" + function WeightVec{S<:Real, V<:RealVector}(vs::V, s::S) - function WeightVec{T<:Real, V<:AbstractVector{T}}(vs::V) - sum_ = sum(vs) - return WeightVec{typeof(sum_), T, V}(vs, sum_) - end +Construct a `WeightVec` with weight values `vs` and sum of weights `wsum`. +If omitted, `wsum` is computed. +""" +function WeightVec{S<:Real, V<:RealVector}(vs::V, s::S) + return WeightVec{S, eltype(vs), V}(vs, s) +end + +function WeightVec{V<:RealVector}(vs::V) + s = sum(vs) + return WeightVec{typeof(s), eltype(vs), V}(vs, s) end """ From f4bffc4de5db6cbb68d8c563e232ac988634cbff Mon Sep 17 00:00:00 2001 From: Rory-Finnegan Date: Sun, 23 Apr 2017 11:20:24 -0500 Subject: [PATCH 5/6] Cleaned up WeightVec constructors. --- src/weights.jl | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/weights.jl b/src/weights.jl index 79ab89fb3..7ee4276e3 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -14,20 +14,15 @@ else end """ - function WeightVec{S<:Real, V<:RealVector}(vs::V, s::S) + WeightVec(vs, [wsum]) Construct a `WeightVec` with weight values `vs` and sum of weights `wsum`. If omitted, `wsum` is computed. """ -function WeightVec{S<:Real, V<:RealVector}(vs::V, s::S) +function WeightVec{S<:Real, V<:RealVector}(vs::V, s::S=sum(vs)) return WeightVec{S, eltype(vs), V}(vs, s) end -function WeightVec{V<:RealVector}(vs::V) - s = sum(vs) - return WeightVec{typeof(s), eltype(vs), V}(vs, s) -end - """ weights(vs) From e34ebb4a1e03c30dcacb71ebd19c318420557e6c Mon Sep 17 00:00:00 2001 From: Rory-Finnegan Date: Mon, 24 Apr 2017 12:06:26 -0500 Subject: [PATCH 6/6] Fixed thrown `MethodError` call in `median(::AbstractArray, ::WeightVec)` --- src/weights.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/weights.jl b/src/weights.jl index 7ee4276e3..b542942b3 100644 --- a/src/weights.jl +++ b/src/weights.jl @@ -291,7 +291,7 @@ Base.mean{T<:Number,W<:Real}(A::AbstractArray{T}, w::WeightVec{W}, dim::Int) = ###### Weighted median ##### function Base.median(v::AbstractArray, w::WeightVec) - throw(MethodError(median, (typeof(v), typeof(w)))) + throw(MethodError(median, (v, w))) end function Base.median{W<:Real}(v::RealVector, w::WeightVec{W})