public final class CombinatoricsUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
CombinatoricsUtils.FactorialLog
Class for computing the natural logarithm of the factorial of
n . |
Modifier and Type | Field and Description |
---|---|
static int |
MAX_BELL
Maximum index of Bell number that fits into a long.
|
Modifier and Type | Method and Description |
---|---|
static long |
bellNumber(int n)
Compute the Bell number (number of partitions of a set).
|
static long |
binomialCoefficient(int n,
int k)
Returns an exact representation of the Binomial
Coefficient, "
n choose k ", the number of
k -element subsets that can be selected from an
n -element set. |
static double |
binomialCoefficientDouble(int n,
int k)
Returns a
double representation of the Binomial
Coefficient, "n choose k ", the number of
k -element subsets that can be selected from an
n -element set. |
static double |
binomialCoefficientLog(int n,
int k)
Returns the natural
log of the Binomial
Coefficient, "n choose k ", the number of
k -element subsets that can be selected from an
n -element set. |
static void |
checkBinomial(int n,
int k)
Check binomial preconditions.
|
static Iterator<int[]> |
combinationsIterator(int n,
int k)
Returns an iterator whose range is the k-element subsets of {0, ..., n - 1}
represented as
int[] arrays. |
static long |
factorial(int n)
Returns n!.
|
static double |
factorialDouble(int n)
|
static double |
factorialLog(int n)
Compute the natural logarithm of the factorial of
n . |
static <T> Stream<List<T>[]> |
partitions(List<T> list)
Generate a stream of partitions of a list.
|
static <T> Stream<List<T>> |
permutations(List<T> list)
Generate a stream of permutations of a list.
|
static long |
stirlingS2(int n,
int k)
Returns the
Stirling number of the second kind, "
S(n,k) ", the number of
ways of partitioning an n -element set into k non-empty
subsets. |
public static final int MAX_BELL
public static long binomialCoefficient(int n, int k) throws MathIllegalArgumentException, MathRuntimeException
n choose k
", the number of
k
-element subsets that can be selected from an
n
-element set.
Preconditions:
0 <= k <= n
(otherwise
MathIllegalArgumentException
is thrown)long
. The
largest value of n
for which all coefficients are
< Long.MAX_VALUE
is 66. If the computed value exceeds
Long.MAX_VALUE
a MathRuntimeException
is
thrown.n
- the size of the setk
- the size of the subsets to be countedn choose k
MathIllegalArgumentException
- if n < 0
.MathIllegalArgumentException
- if k > n
.MathRuntimeException
- if the result is too large to be
represented by a long integer.public static double binomialCoefficientDouble(int n, int k) throws MathIllegalArgumentException, MathRuntimeException
double
representation of the Binomial
Coefficient, "n choose k
", the number of
k
-element subsets that can be selected from an
n
-element set.
Preconditions:
0 <= k <= n
(otherwise
IllegalArgumentException
is thrown)double
. The
largest value of n
for which all coefficients are <
Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE,
Double.POSITIVE_INFINITY is returnedn
- the size of the setk
- the size of the subsets to be countedn choose k
MathIllegalArgumentException
- if n < 0
.MathIllegalArgumentException
- if k > n
.MathRuntimeException
- if the result is too large to be
represented by a long integer.public static double binomialCoefficientLog(int n, int k) throws MathIllegalArgumentException, MathRuntimeException
log
of the Binomial
Coefficient, "n choose k
", the number of
k
-element subsets that can be selected from an
n
-element set.
Preconditions:
0 <= k <= n
(otherwise
MathIllegalArgumentException
is thrown)n
- the size of the setk
- the size of the subsets to be countedn choose k
MathIllegalArgumentException
- if n < 0
.MathIllegalArgumentException
- if k > n
.MathRuntimeException
- if the result is too large to be
represented by a long integer.public static long factorial(int n) throws MathIllegalArgumentException
n
Factorial, the
product of the numbers 1,...,n
.
Preconditions:
n >= 0
(otherwise
MathIllegalArgumentException
is thrown)long
. The
largest value of n
for which n!
does not exceed
Long.MAX_VALUE} is 20. If the computed value exceeds Long.MAX_VALUE
an MathRuntimeException
is thrown.n
- argumentn!
MathRuntimeException
- if the result is too large to be represented
by a long
.MathIllegalArgumentException
- if n < 0
.MathIllegalArgumentException
- if n > 20
: The factorial value is too
large to fit in a long
.public static double factorialDouble(int n) throws MathIllegalArgumentException
n
(the product of the numbers 1 to n), as a
double
.
The result should be small enough to fit into a double
: The
largest n
for which n!
does not exceed
Double.MAX_VALUE
is 170. If the computed value exceeds
Double.MAX_VALUE
, Double.POSITIVE_INFINITY
is returned.n
- Argument.n!
MathIllegalArgumentException
- if n < 0
.public static double factorialLog(int n) throws MathIllegalArgumentException
n
.n
- Argument.log(n!)
MathIllegalArgumentException
- if n < 0
.public static long stirlingS2(int n, int k) throws MathIllegalArgumentException, MathRuntimeException
S(n,k)
", the number of
ways of partitioning an n
-element set into k
non-empty
subsets.
The preconditions are 0 <= k <= n
(otherwise
MathIllegalArgumentException
is thrown)
n
- the size of the setk
- the number of non-empty subsetsS(n,k)
MathIllegalArgumentException
- if k < 0
.MathIllegalArgumentException
- if k > n
.MathRuntimeException
- if some overflow happens, typically for n exceeding 25 and
k between 20 and n-2 (S(n,n-1) is handled specifically and does not overflow)public static Iterator<int[]> combinationsIterator(int n, int k)
int[]
arrays.
The arrays returned by the iterator are sorted in descending order and
they are visited in lexicographic order with significance from right to
left. For example, combinationsIterator(4, 2) returns an Iterator that
will generate the following sequence of arrays on successive calls to
next()
:
[0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]
If k == 0
an Iterator containing an empty array is returned and
if k == n
an Iterator containing [0, ..., n -1] is returned.
n
- Size of the set from which subsets are selected.k
- Size of the subsets to be enumerated.iterator
over the k-sets in n.MathIllegalArgumentException
- if n < 0
.MathIllegalArgumentException
- if k > n
.public static void checkBinomial(int n, int k) throws MathIllegalArgumentException
n
- Size of the set.k
- Size of the subsets to be counted.MathIllegalArgumentException
- if n < 0
.MathIllegalArgumentException
- if k > n
.public static long bellNumber(int n)
n
- number of elements of the setpublic static <T> Stream<List<T>[]> partitions(List<T> list)
This method implements the iterative algorithm described in Short Note: A Fast Iterative Algorithm for Generating Set Partitions by B. Djokić, M. Miyakawa, S. Sekiguchi, I. Semba, and I. Stojmenović (The Computer Journal, Volume 32, Issue 3, 1989, Pages 281–282, https://doi.org/10.1093/comjnl/32.3.281
T
- type of the list elementslist
- list to partitionpublic static <T> Stream<List<T>> permutations(List<T> list)
This method implements the Steinhaus–Johnson–Trotter algorithm with Even's speedup Steinhaus–Johnson–Trotter algorithm
T
- type of the list elementslist
- list to permuteCopyright © 2016-2022 CS GROUP. All rights reserved.