Here are some basic combinatorial functions.
Counting integer partitions:
NumPartitions(n)
computes number of partitions of n
, i.e. how many distinct ways to write n
as a sum of positive integers (error if n
is negative)
Random subsets and random tuples:
RandomSubsetIndices(n)
-- returns a random subset of {0,1,2...,n-1
}
RandomSubsetIndices(n,r)
-- returns a size r
random subset of {0,1,2...,n-1
}
RandomTupleIndices(n,r)
-- returns a random r
-tuple from {0,1,2,...,n-1
}
RandomPermutation(n)
-- return vector<long>
being a random permutation of {0,1,2,...,n-1
}
signature(perm)
-- return the signature of a permutation (of type vector<int>
or vector<long>
) of the values 0,1,2,..,n-1
Notes:
n
indicates the range {0,1,2,...,n-1
} so that the integers produced are valid indices into a C++ vector of size n
.
vector<long>
The algorithm for RandomSubsetIndices(n,r)
was taken from the
Wikipedia page on "Reservoir Sorting". Also RandomPermutation
was taken from Wikipedia (which page?)
Ugly fn names RandomSubsetIndices
and RandomTupleIndices
For thread-safety the fns should also accept as input a random source!
2022
RandomPermutation
(fn has been there for a while)
2015