Skip to content

Commit

Permalink
sample call fails with error (JuliaLang#272)
Browse files Browse the repository at this point in the history
* Array shouldn't be parameterised on T and change from sample! to sample

* Add simple tests for weighted sampling with a dimension
  • Loading branch information
claireh93 authored and andreasnoack committed Jun 2, 2017
1 parent cd9e4ab commit 41a76c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ sample{T}(rng::AbstractRNG, a::AbstractArray{T}, wv::AbstractWeights, dims::Dims
sample!(rng, a, wv, Array{T}(dims); replace=replace, ordered=ordered)
sample(a::AbstractArray, wv::AbstractWeights, dims::Dims;
replace::Bool=true, ordered::Bool=false) =
sample!(Base.GLOBAL_RNG, a, wv, Array{T}(dims); replace=replace, ordered=ordered)
sample(Base.GLOBAL_RNG, a, wv, dims; replace=replace, ordered=ordered)

# wsample interface

Expand Down
7 changes: 7 additions & 0 deletions test/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,10 @@ x = vcat([sample(a, wv, 4, replace=false) for j in 1:10000]...)

wv = Weights([zeros(5); 1:4; -1])
@test_throws ErrorException sample(a, wv, 1, replace=false)

#### weighted sampling with dimension
srand(1);

@test sample([1, 2], Weights([1, 1]), (2,2)) == ones(2,2)
@test sample([1, 2], Weights([0, 1]), (2,2)) == [2 2 ; 2 2]
@test sample(collect(1:4), Weights(1:4), (2,2), replace=false) == [4 1; 3 2]

0 comments on commit 41a76c2

Please sign in to comment.