Class CombinatoricsUtils
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Class for computing the natural logarithm of the factorial ofn
. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Maximum index of Bell number that fits into a long. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 ofk
-element subsets that can be selected from ann
-element set.static double
binomialCoefficientDouble
(int n, int k) Returns adouble
representation of the Binomial Coefficient, "n choose k
", the number ofk
-element subsets that can be selected from ann
-element set.static double
binomialCoefficientLog
(int n, int k) Returns the naturallog
of the Binomial Coefficient, "n choose k
", the number ofk
-element subsets that can be selected from ann
-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 asint[]
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 ofn
.partitions
(List<T> list) Generate a stream of partitions of a list.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 ann
-element set intok
non-empty subsets.
-
Field Details
-
MAX_BELL
public static final int MAX_BELLMaximum index of Bell number that fits into a long.- Since:
- 2.2
- See Also:
-
-
Method Details
-
binomialCoefficient
public static long binomialCoefficient(int n, int k) throws MathIllegalArgumentException, MathRuntimeException Returns an exact representation of the Binomial Coefficient, "n choose k
", the number ofk
-element subsets that can be selected from ann
-element set.Preconditions:
-
0 <= k <= n
(otherwiseMathIllegalArgumentException
is thrown) - The result is small enough to fit into a
long
. The largest value ofn
for which all coefficients are< Long.MAX_VALUE
is 66. If the computed value exceedsLong.MAX_VALUE
aMathRuntimeException
is thrown.
- Parameters:
n
- the size of the setk
- the size of the subsets to be counted- Returns:
n choose k
- Throws:
MathIllegalArgumentException
- ifn < 0
.MathIllegalArgumentException
- ifk > n
.MathRuntimeException
- if the result is too large to be represented by a long integer.
-
-
binomialCoefficientDouble
public static double binomialCoefficientDouble(int n, int k) throws MathIllegalArgumentException, MathRuntimeException Returns adouble
representation of the Binomial Coefficient, "n choose k
", the number ofk
-element subsets that can be selected from ann
-element set.* Preconditions:
-
0 <= k <= n
(otherwiseIllegalArgumentException
is thrown) - The result is small enough to fit into a
double
. The largest value ofn
for which all coefficients are < Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned
- Parameters:
n
- the size of the setk
- the size of the subsets to be counted- Returns:
n choose k
- Throws:
MathIllegalArgumentException
- ifn < 0
.MathIllegalArgumentException
- ifk > n
.MathRuntimeException
- if the result is too large to be represented by a long integer.
-
-
binomialCoefficientLog
public static double binomialCoefficientLog(int n, int k) throws MathIllegalArgumentException, MathRuntimeException Returns the naturallog
of the Binomial Coefficient, "n choose k
", the number ofk
-element subsets that can be selected from ann
-element set.* Preconditions:
-
0 <= k <= n
(otherwiseMathIllegalArgumentException
is thrown)
- Parameters:
n
- the size of the setk
- the size of the subsets to be counted- Returns:
n choose k
- Throws:
MathIllegalArgumentException
- ifn < 0
.MathIllegalArgumentException
- ifk > n
.MathRuntimeException
- if the result is too large to be represented by a long integer.
-
-
factorial
Returns n!. Shorthand forn
Factorial, the product of the numbers1,...,n
.* Preconditions:
-
n >= 0
(otherwiseMathIllegalArgumentException
is thrown) - The result is small enough to fit into a
long
. The largest value ofn
for whichn!
does not exceed Long.MAX_VALUE} is 20. If the computed value exceedsLong.MAX_VALUE
anMathRuntimeException
is thrown.
- Parameters:
n
- argument- Returns:
n!
- Throws:
MathRuntimeException
- if the result is too large to be represented by along
.MathIllegalArgumentException
- ifn < 0
.MathIllegalArgumentException
- ifn > 20
: The factorial value is too large to fit in along
.
-
-
factorialDouble
Compute n!, the factorial ofn
(the product of the numbers 1 to n), as adouble
. The result should be small enough to fit into adouble
: The largestn
for whichn!
does not exceedDouble.MAX_VALUE
is 170. If the computed value exceedsDouble.MAX_VALUE
,Double.POSITIVE_INFINITY
is returned.- Parameters:
n
- Argument.- Returns:
n!
- Throws:
MathIllegalArgumentException
- ifn < 0
.
-
factorialLog
Compute the natural logarithm of the factorial ofn
.- Parameters:
n
- Argument.- Returns:
log(n!)
- Throws:
MathIllegalArgumentException
- ifn < 0
.
-
stirlingS2
public static long stirlingS2(int n, int k) throws MathIllegalArgumentException, MathRuntimeException Returns the Stirling number of the second kind, "S(n,k)
", the number of ways of partitioning ann
-element set intok
non-empty subsets.The preconditions are
0 <= k <= n
(otherwiseMathIllegalArgumentException
is thrown)- Parameters:
n
- the size of the setk
- the number of non-empty subsets- Returns:
S(n,k)
- Throws:
MathIllegalArgumentException
- ifk < 0
.MathIllegalArgumentException
- ifk > 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)
-
combinationsIterator
Returns an iterator whose range is the k-element subsets of {0, ..., n - 1} represented asint[]
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 ifk == n
an Iterator containing [0, ..., n -1] is returned.- Parameters:
n
- Size of the set from which subsets are selected.k
- Size of the subsets to be enumerated.- Returns:
- an
iterator
over the k-sets in n. - Throws:
MathIllegalArgumentException
- ifn < 0
.MathIllegalArgumentException
- ifk > n
.
-
checkBinomial
Check binomial preconditions.- Parameters:
n
- Size of the set.k
- Size of the subsets to be counted.- Throws:
MathIllegalArgumentException
- ifn < 0
.MathIllegalArgumentException
- ifk > n
.
-
bellNumber
public static long bellNumber(int n) Compute the Bell number (number of partitions of a set).- Parameters:
n
- number of elements of the set- Returns:
- Bell number Bₙ
- Since:
- 2.2
-
partitions
Generate a stream of partitions of a 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
- Type Parameters:
T
- type of the list elements- Parameters:
list
- list to partition- Returns:
- stream of partitions of the list, each partition is an array or parts and each part is a list of elements
- Since:
- 2.2
-
permutations
Generate a stream of permutations of a list.This method implements the Steinhaus–Johnson–Trotter algorithm with Even's speedup Steinhaus–Johnson–Trotter algorithm
- Type Parameters:
T
- type of the list elements- Parameters:
list
- list to permute- Returns:
- stream of permutations of the list
- Since:
- 2.2
-