Skip to content

Commit 5a7fa11

Browse files
Rory Finneganararslan
authored andcommitted
Made WeightVec a subtype of RealVector (#248)
* Made `WeightVec` a subtype of `RealVector` * Added a few tests to weights.jl * 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" * Moved constructors out of version check. * Cleaned up WeightVec constructors. * Fixed thrown `MethodError` call in `median(::AbstractArray, ::WeightVec)`
1 parent f6b3c0e commit 5a7fa11

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/weights.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11

22
###### Weight vector #####
33

4-
immutable WeightVec{W,Vec<:RealVector}
5-
values::Vec
6-
sum::W
4+
if VERSION < v"0.6.0-dev.2123"
5+
immutable WeightVec{S<:Real, T<:Real, V<:RealVector} <: RealVector{T}
6+
values::V
7+
sum::S
8+
end
9+
else
10+
immutable WeightVec{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractVector{T}
11+
values::V
12+
sum::S
13+
end
714
end
815

916
"""
@@ -12,8 +19,9 @@ end
1219
Construct a `WeightVec` with weight values `vs` and sum of weights `wsum`.
1320
If omitted, `wsum` is computed.
1421
"""
15-
WeightVec{Vec<:RealVector,W<:Real}(vs::Vec,wsum::W) = WeightVec{W,Vec}(vs, wsum)
16-
WeightVec(vs::RealVector) = WeightVec(vs, sum(vs))
22+
function WeightVec{S<:Real, V<:RealVector}(vs::V, s::S=sum(vs))
23+
return WeightVec{S, eltype(vs), V}(vs, s)
24+
end
1725

1826
"""
1927
weights(vs)
@@ -30,6 +38,7 @@ sum(wv::WeightVec) = wv.sum
3038
isempty(wv::WeightVec) = isempty(wv.values)
3139

3240
Base.getindex(wv::WeightVec, i) = getindex(wv.values, i)
41+
Base.size(wv::WeightVec) = size(wv.values)
3342

3443

3544
##### Weighted sum #####
@@ -281,6 +290,9 @@ Base.mean{T<:Number,W<:Real}(A::AbstractArray{T}, w::WeightVec{W}, dim::Int) =
281290

282291

283292
###### Weighted median #####
293+
function Base.median(v::AbstractArray, w::WeightVec)
294+
throw(MethodError(median, (v, w)))
295+
end
284296

285297
function Base.median{W<:Real}(v::RealVector, w::WeightVec{W})
286298
isempty(v) && error("median of an empty array is undefined")

test/weights.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import Compat: view
55

66
@test isa(weights([1, 2, 3]), WeightVec{Int})
77
@test isa(weights([1., 2., 3.]), WeightVec{Float64})
8-
98
@test isa(weights([1 2 3; 4 5 6]), WeightVec{Int})
109

10+
@test isa(WeightVec([1, 2, 3], 6), WeightVec{Int})
11+
1112
@test isempty(weights(Float64[]))
13+
@test size(weights([1, 2, 3])) == (3,)
1214

1315
w = [1., 2., 3.]
1416
wv = weights(w)
@@ -26,6 +28,11 @@ bv = weights(b)
2628
@test sum(bv) === 3
2729
@test !isempty(bv)
2830

31+
ba = BitArray([true, false, true])
32+
sa = sparsevec([1., 0., 2.])
33+
34+
@test sum(ba, wv) === 4.0
35+
@test sum(sa, wv) === 7.0
2936

3037
## wsum
3138

0 commit comments

Comments
 (0)