@@ -10,6 +10,8 @@ using Random: Sampler, Random.GLOBAL_RNG
10
10
# ## Algorithms for sampling with replacement
11
11
12
12
function direct_sample! (rng:: AbstractRNG , a:: UnitRange , x:: AbstractArray )
13
+ 1 == firstindex (a) == firstindex (x) ||
14
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
13
15
s = Sampler (rng, 1 : length (a))
14
16
b = a[1 ] - 1
15
17
if b == 0
@@ -34,6 +36,10 @@ and set `x[j] = a[i]`, with `n=length(a)` and `k=length(x)`.
34
36
This algorithm consumes `k` random numbers.
35
37
"""
36
38
function direct_sample! (rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray )
39
+ 1 == firstindex (a) == firstindex (x) ||
40
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
41
+ Base. mightalias (a, x) &&
42
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
37
43
s = Sampler (rng, 1 : length (a))
38
44
for i = 1 : length (x)
39
45
@inbounds x[i] = a[rand (rng, s)]
@@ -55,6 +61,10 @@ storeindices(n, k, T) = false
55
61
56
62
# order results of a sampler that does not order automatically
57
63
function sample_ordered! (sampler!, rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray )
64
+ 1 == firstindex (a) == firstindex (x) ||
65
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
66
+ Base. mightalias (a, x) &&
67
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
58
68
n, k = length (a), length (x)
59
69
# todo: if eltype(x) <: Real && eltype(a) <: Real,
60
70
# in some cases it might be faster to check
@@ -130,6 +140,10 @@ memory space. Suitable for the case where memory is tight.
130
140
"""
131
141
function knuths_sample! (rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray ;
132
142
initshuffle:: Bool = true )
143
+ 1 == firstindex (a) == firstindex (x) ||
144
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
145
+ Base. mightalias (a, x) &&
146
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
133
147
n = length (a)
134
148
k = length (x)
135
149
k <= n || error (" length(x) should not exceed length(a)" )
@@ -186,6 +200,10 @@ faster than Knuth's algorithm especially when `n` is greater than `k`.
186
200
It is ``O(n)`` for initialization, plus ``O(k)`` for random shuffling
187
201
"""
188
202
function fisher_yates_sample! (rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray )
203
+ 1 == firstindex (a) == firstindex (x) ||
204
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
205
+ Base. mightalias (a, x) &&
206
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
189
207
n = length (a)
190
208
k = length (x)
191
209
k <= n || error (" length(x) should not exceed length(a)" )
@@ -222,6 +240,10 @@ However, if `k` is large and approaches ``n``, the rejection rate would increase
222
240
drastically, resulting in poorer performance.
223
241
"""
224
242
function self_avoid_sample! (rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray )
243
+ 1 == firstindex (a) == firstindex (x) ||
244
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
245
+ Base. mightalias (a, x) &&
246
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
225
247
n = length (a)
226
248
k = length (x)
227
249
k <= n || error (" length(x) should not exceed length(a)" )
@@ -260,6 +282,10 @@ This algorithm consumes ``O(n)`` random numbers, with `n=length(a)`.
260
282
The outputs are ordered.
261
283
"""
262
284
function seqsample_a! (rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray )
285
+ 1 == firstindex (a) == firstindex (x) ||
286
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
287
+ Base. mightalias (a, x) &&
288
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
263
289
n = length (a)
264
290
k = length (x)
265
291
k <= n || error (" length(x) should not exceed length(a)" )
@@ -298,6 +324,10 @@ This algorithm consumes ``O(k^2)`` random numbers, with `k=length(x)`.
298
324
The outputs are ordered.
299
325
"""
300
326
function seqsample_c! (rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray )
327
+ 1 == firstindex (a) == firstindex (x) ||
328
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
329
+ Base. mightalias (a, x) &&
330
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
301
331
n = length (a)
302
332
k = length (x)
303
333
k <= n || error (" length(x) should not exceed length(a)" )
@@ -340,6 +370,10 @@ This algorithm consumes ``O(k)`` random numbers, with `k=length(x)`.
340
370
The outputs are ordered.
341
371
"""
342
372
function seqsample_d! (rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray )
373
+ 1 == firstindex (a) == firstindex (x) ||
374
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
375
+ Base. mightalias (a, x) &&
376
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
343
377
N = length (a)
344
378
n = length (x)
345
379
n <= N || error (" length(x) should not exceed length(a)" )
@@ -451,8 +485,6 @@ nor share memory with them, or the result may be incorrect.
451
485
"""
452
486
function sample! (rng:: AbstractRNG , a:: AbstractArray , x:: AbstractArray ;
453
487
replace:: Bool = true , ordered:: Bool = false )
454
- Base. mightalias (a, x) &&
455
- throw (ArgumentError (" output array a must not share memory with input array x" ))
456
488
1 == firstindex (a) == firstindex (x) ||
457
489
throw (ArgumentError (" non 1-based arrays are not supported" ))
458
490
n = length (a)
@@ -550,6 +582,8 @@ Optionally specify a random number generator `rng` as the first argument
550
582
(defaults to `Random.GLOBAL_RNG`).
551
583
"""
552
584
function sample (rng:: AbstractRNG , wv:: AbstractWeights )
585
+ 1 == firstindex (wv) ||
586
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
553
587
t = rand (rng) * sum (wv)
554
588
n = length (wv)
555
589
i = 1
@@ -579,6 +613,12 @@ Noting `k=length(x)` and `n=length(a)`, this algorithm:
579
613
"""
580
614
function direct_sample! (rng:: AbstractRNG , a:: AbstractArray ,
581
615
wv:: AbstractWeights , x:: AbstractArray )
616
+ Base. mightalias (a, x) &&
617
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
618
+ Base. mightalias (x, wv) &&
619
+ throw (ArgumentError (" output array x must not share memory with weights array wv" ))
620
+ 1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
621
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
582
622
n = length (a)
583
623
length (wv) == n || throw (DimensionMismatch (" Inconsistent lengths." ))
584
624
for i = 1 : length (x)
@@ -662,6 +702,12 @@ Noting `k=length(x)` and `n=length(a)`, this algorithm takes ``O(n \\log n)`` ti
662
702
for building the alias table, and then ``O(1)`` to draw each sample. It consumes ``2 k`` random numbers.
663
703
"""
664
704
function alias_sample! (rng:: AbstractRNG , a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray )
705
+ Base. mightalias (a, x) &&
706
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
707
+ Base. mightalias (x, wv) &&
708
+ throw (ArgumentError (" output array x must not share memory with weights array wv" ))
709
+ 1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
710
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
665
711
n = length (a)
666
712
length (wv) == n || throw (DimensionMismatch (" Inconsistent lengths." ))
667
713
@@ -694,6 +740,12 @@ and has overall time complexity ``O(n k)``.
694
740
"""
695
741
function naive_wsample_norep! (rng:: AbstractRNG , a:: AbstractArray ,
696
742
wv:: AbstractWeights , x:: AbstractArray )
743
+ Base. mightalias (a, x) &&
744
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
745
+ Base. mightalias (x, wv) &&
746
+ throw (ArgumentError (" output array x must not share memory with weights array wv" ))
747
+ 1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
748
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
697
749
n = length (a)
698
750
length (wv) == n || throw (DimensionMismatch (" Inconsistent lengths." ))
699
751
k = length (x)
@@ -734,6 +786,12 @@ processing time to draw ``k`` elements. It consumes ``n`` random numbers.
734
786
"""
735
787
function efraimidis_a_wsample_norep! (rng:: AbstractRNG , a:: AbstractArray ,
736
788
wv:: AbstractWeights , x:: AbstractArray )
789
+ Base. mightalias (a, x) &&
790
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
791
+ Base. mightalias (x, wv) &&
792
+ throw (ArgumentError (" output array x must not share memory with weights array wv" ))
793
+ 1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
794
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
737
795
n = length (a)
738
796
length (wv) == n || throw (DimensionMismatch (" a and wv must be of same length (got $n and $(length (wv)) )." ))
739
797
k = length (x)
@@ -769,6 +827,12 @@ processing time to draw ``k`` elements. It consumes ``n`` random numbers.
769
827
"""
770
828
function efraimidis_ares_wsample_norep! (rng:: AbstractRNG , a:: AbstractArray ,
771
829
wv:: AbstractWeights , x:: AbstractArray )
830
+ Base. mightalias (a, x) &&
831
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
832
+ Base. mightalias (x, wv) &&
833
+ throw (ArgumentError (" output array x must not share memory with weights array wv" ))
834
+ 1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
835
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
772
836
n = length (a)
773
837
length (wv) == n || throw (DimensionMismatch (" a and wv must be of same length (got $n and $(length (wv)) )." ))
774
838
k = length (x)
@@ -836,6 +900,12 @@ processing time to draw ``k`` elements. It consumes ``O(k \\log(n / k))`` random
836
900
function efraimidis_aexpj_wsample_norep! (rng:: AbstractRNG , a:: AbstractArray ,
837
901
wv:: AbstractWeights , x:: AbstractArray ;
838
902
ordered:: Bool = false )
903
+ Base. mightalias (a, x) &&
904
+ throw (ArgumentError (" output array x must not share memory with input array a" ))
905
+ Base. mightalias (x, wv) &&
906
+ throw (ArgumentError (" output array x must not share memory with weights array wv" ))
907
+ 1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
908
+ throw (ArgumentError (" non 1-based arrays are not supported" ))
839
909
n = length (a)
840
910
length (wv) == n || throw (DimensionMismatch (" a and wv must be of same length (got $n and $(length (wv)) )." ))
841
911
k = length (x)
@@ -898,10 +968,6 @@ efraimidis_aexpj_wsample_norep!(a::AbstractArray, wv::AbstractWeights, x::Abstra
898
968
899
969
function sample! (rng:: AbstractRNG , a:: AbstractArray , wv:: AbstractWeights , x:: AbstractArray ;
900
970
replace:: Bool = true , ordered:: Bool = false )
901
- Base. mightalias (a, x) &&
902
- throw (ArgumentError (" output array a must not share memory with input array x" ))
903
- Base. mightalias (a, wv) &&
904
- throw (ArgumentError (" output array a must not share memory with weights array wv" ))
905
971
1 == firstindex (a) == firstindex (wv) == firstindex (x) ||
906
972
throw (ArgumentError (" non 1-based arrays are not supported" ))
907
973
n = length (a)
0 commit comments