Skip to content

Commit 439f7f4

Browse files
committed
Add randomSubset method
1 parent 121cebf commit 439f7f4

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

M2/Macaulay2/m2/exports.m2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ export {
10451045
"random",
10461046
"randomKRationalPoint",
10471047
"randomMutableMatrix",
1048+
"randomSubset",
10481049
"rank",
10491050
"rays",
10501051
"read",

M2/Macaulay2/m2/lists.m2

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,34 @@ random List := opts -> s -> (
237237
t := s#i ; s#i = s#j ; s#j = t;
238238
);
239239
new List from s)
240+
241+
randomSubset = method()
242+
-- Knuth Algorithm S, Art of Computer Programming, Section 3.4.2
243+
randomSubset(ZZ, ZZ) := (N, n) -> (
244+
if n < 0 or n > N then error("expected an integer between 0 and ", N);
245+
t := 0;
246+
apply(n, m -> (
247+
while (N - t) * rawRandomRRUniform defaultPrecision >= n - m
248+
do t += 1;
249+
first (t, t += 1))))
250+
-- when size not given, use binomial distribution first to determine size
251+
randomSubset ZZ := N -> (
252+
if N < 0 then error "expected a nonnegative integer"
253+
else if N < 30 then (
254+
-- for small N, sum up N Bernoulli variates
255+
n := 0;
256+
scan(N, i -> n += random 2))
257+
else (
258+
-- for large N, use normal approximation
259+
n = round(n/2 + sqrt(n/4)*rawRandomRRNormal defaultPrecision);
260+
-- in the vanishingly unlikely case we end up outside [0, N]:
261+
n = min(N, max(0, n)));
262+
randomSubset(N, n))
263+
randomSubset(VisibleList, ZZ) := (x, n) -> x_(randomSubset(#x, n))
264+
randomSubset VisibleList := x -> x_(randomSubset(#x))
265+
randomSubset(Set, ZZ) := (x, n) -> set randomSubset(toList x, n)
266+
randomSubset Set := x -> set randomSubset toList x
267+
240268
-----------------------------------------------------------------------------
241269
-- sublists
242270
-----------------------------------------------------------------------------

M2/Macaulay2/packages/Macaulay2Doc/functions.m2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ load "./functions/pushForward-doc.m2"
219219
load "./functions/QRDecomposition-doc.m2"
220220
load "./functions/quotient-remainder-doc.m2"
221221
load "./functions/random-doc.m2"
222+
load "./functions/randomSubset-doc.m2"
222223
load "./functions/rank-doc.m2"
223224
load "./functions/readDirectory-doc.m2"
224225
load "./functions/readlink-doc.m2"

M2/Macaulay2/packages/Macaulay2Doc/functions/random-doc.m2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Node
2323
(random, Module)
2424
(random, Module, Module)
2525
setRandomSeed
26+
randomSubset
2627

2728
Node
2829
Key
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
doc ///
2+
Key
3+
randomSubset
4+
(randomSubset, VisibleList, ZZ)
5+
(randomSubset, Set, ZZ)
6+
(randomSubset, ZZ, ZZ)
7+
(randomSubset, VisibleList)
8+
(randomSubset, Set)
9+
(randomSubset, ZZ)
10+
Usage
11+
randomSubset(x, n)
12+
randomSubset x
13+
Inputs
14+
x:{VisibleList, Set, ZZ}
15+
n:ZZ
16+
Description
17+
Text
18+
When @VAR "n"@ is given, then a random subset @VAR "x"@ of cardinality
19+
@VAR "n"@ is returned.
20+
Example
21+
randomSubset({2, 3, 5, 7, 11}, 2)
22+
Text
23+
Otherwise, a random subset of arbitrary cardinality is returned.
24+
Example
25+
randomSubset {2, 3, 5, 7, 11}
26+
randomSubset {2, 3, 5, 7, 11}
27+
randomSubset {2, 3, 5, 7, 11}
28+
Text
29+
If @VAR "x"@ is an integer, then a subset of $\{0,\ldots,x - 1\}$ is
30+
returned.
31+
Example
32+
randomSubset(4, 2)
33+
randomSubset 5
34+
References
35+
Knuth, Donald E.
36+
@EM "The Art of Computer Programming: Seminumerical Algorithms, Volume 2"@.
37+
Addison-Wesley Professional, 2014. (Algorithm S, Section 3.4.2)
38+
SeeAlso
39+
random
40+
///

0 commit comments

Comments
 (0)