@@ -591,7 +591,9 @@ Optionally specify a random number generator `rng` as the first argument
591
591
function sample (rng:: AbstractRNG , wv:: AbstractWeights )
592
592
1 == firstindex (wv) ||
593
593
throw (ArgumentError (" non 1-based arrays are not supported" ))
594
- t = rand (rng) * sum (wv)
594
+ wsum = sum (wv)
595
+ isfinite (wsum) || throw (ArgumentError (" only finite weights are supported" ))
596
+ t = rand (rng) * wsum
595
597
n = length (wv)
596
598
i = 1
597
599
cw = wv[1 ]
@@ -654,6 +656,7 @@ function alias_sample!(rng::AbstractRNG, a::AbstractArray, wv::AbstractWeights,
654
656
throw (ArgumentError (" output array x must not share memory with input array a" ))
655
657
1 == firstindex (a) == firstindex (wv) ||
656
658
throw (ArgumentError (" non 1-based arrays are not supported" ))
659
+ isfinite (sum (wv)) || throw (ArgumentError (" only finite weights are supported" ))
657
660
length (wv) == length (a) || throw (DimensionMismatch (" Inconsistent lengths." ))
658
661
659
662
# create alias table
@@ -688,13 +691,14 @@ function naive_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
688
691
throw (ArgumentError (" output array x must not share memory with weights array wv" ))
689
692
1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
690
693
throw (ArgumentError (" non 1-based arrays are not supported" ))
694
+ wsum = sum (wv)
695
+ isfinite (wsum) || throw (ArgumentError (" only finite weights are supported" ))
691
696
n = length (a)
692
697
length (wv) == n || throw (DimensionMismatch (" Inconsistent lengths." ))
693
698
k = length (x)
694
699
695
700
w = Vector {Float64} (undef, n)
696
701
copyto! (w, wv)
697
- wsum = sum (wv)
698
702
699
703
for i = 1 : k
700
704
u = rand (rng) * wsum
@@ -734,6 +738,7 @@ function efraimidis_a_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
734
738
throw (ArgumentError (" output array x must not share memory with weights array wv" ))
735
739
1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
736
740
throw (ArgumentError (" non 1-based arrays are not supported" ))
741
+ isfinite (sum (wv)) || throw (ArgumentError (" only finite weights are supported" ))
737
742
n = length (a)
738
743
length (wv) == n || throw (DimensionMismatch (" a and wv must be of same length (got $n and $(length (wv)) )." ))
739
744
k = length (x)
@@ -775,6 +780,7 @@ function efraimidis_ares_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
775
780
throw (ArgumentError (" output array x must not share memory with weights array wv" ))
776
781
1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
777
782
throw (ArgumentError (" non 1-based arrays are not supported" ))
783
+ isfinite (sum (wv)) || throw (ArgumentError (" only finite weights are supported" ))
778
784
n = length (a)
779
785
length (wv) == n || throw (DimensionMismatch (" a and wv must be of same length (got $n and $(length (wv)) )." ))
780
786
k = length (x)
@@ -848,6 +854,7 @@ function efraimidis_aexpj_wsample_norep!(rng::AbstractRNG, a::AbstractArray,
848
854
throw (ArgumentError (" output array x must not share memory with weights array wv" ))
849
855
1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
850
856
throw (ArgumentError (" non 1-based arrays are not supported" ))
857
+ isfinite (sum (wv)) || throw (ArgumentError (" only finite weights are supported" ))
851
858
n = length (a)
852
859
length (wv) == n || throw (DimensionMismatch (" a and wv must be of same length (got $n and $(length (wv)) )." ))
853
860
k = length (x)
0 commit comments