Package org.apache.commons.math3.util
Class CombinatoricsUtils
- java.lang.Object
-
- org.apache.commons.math3.util.CombinatoricsUtils
-
public final class CombinatoricsUtils extends java.lang.ObjectCombinatorial utilities.- Since:
- 3.3
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static long[]FACTORIALSAll long-representable factorials(package private) static java.util.concurrent.atomic.AtomicReference<long[][]>STIRLING_S2Stirling numbers of the second kind.
-
Constructor Summary
Constructors Modifier Constructor Description privateCombinatoricsUtils()Private constructor (class contains only static methods).
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longbinomialCoefficient(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 doublebinomialCoefficientDouble(int n, int k)Returns adoublerepresentation of the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.static doublebinomialCoefficientLog(int n, int k)Returns the naturallogof the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.static voidcheckBinomial(int n, int k)Check binomial preconditions.static java.util.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 longfactorial(int n)Returns n!.static doublefactorialDouble(int n)static doublefactorialLog(int n)Compute the natural logarithm of the factorial ofn.static longstirlingS2(int n, int k)Returns the Stirling number of the second kind, "S(n,k)", the number of ways of partitioning ann-element set intoknon-empty subsets.
-
-
-
Method Detail
-
binomialCoefficient
public static long binomialCoefficient(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticExceptionReturns 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(otherwiseMathIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
long. The largest value ofnfor which all coefficients are< Long.MAX_VALUEis 66. If the computed value exceedsLong.MAX_VALUEaMathArithMeticExceptionis thrown.
- Parameters:
n- the size of the setk- the size of the subsets to be counted- Returns:
n choose k- Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.MathArithmeticException- if the result is too large to be represented by a long integer.
-
-
binomialCoefficientDouble
public static double binomialCoefficientDouble(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticExceptionReturns adoublerepresentation of the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.Preconditions:
-
0 <= k <= n(otherwiseIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
double. The largest value ofnfor which all coefficients are less than 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:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.MathArithmeticException- if the result is too large to be represented by a long integer.
-
-
binomialCoefficientLog
public static double binomialCoefficientLog(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticExceptionReturns the naturallogof the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.Preconditions:
-
0 <= k <= n(otherwiseMathIllegalArgumentExceptionis thrown)
- Parameters:
n- the size of the setk- the size of the subsets to be counted- Returns:
n choose k- Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.MathArithmeticException- if the result is too large to be represented by a long integer.
-
-
factorial
public static long factorial(int n) throws NotPositiveException, MathArithmeticExceptionReturns n!. Shorthand fornFactorial, the product of the numbers1,...,n.Preconditions:
-
n >= 0(otherwiseMathIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
long. The largest value ofnfor whichn!does not exceed Long.MAX_VALUE} is 20. If the computed value exceedsLong.MAX_VALUEanMathArithMeticExceptionis thrown.
- Parameters:
n- argument- Returns:
n!- Throws:
MathArithmeticException- if the result is too large to be represented by along.NotPositiveException- ifn < 0.MathArithmeticException- ifn > 20: The factorial value is too large to fit in along.
-
-
factorialDouble
public static double factorialDouble(int n) throws NotPositiveExceptionCompute 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 largestnfor whichn!does not exceedDouble.MAX_VALUEis 170. If the computed value exceedsDouble.MAX_VALUE,Double.POSITIVE_INFINITYis returned.- Parameters:
n- Argument.- Returns:
n!- Throws:
NotPositiveException- ifn < 0.
-
factorialLog
public static double factorialLog(int n) throws NotPositiveExceptionCompute the natural logarithm of the factorial ofn.- Parameters:
n- Argument.- Returns:
n!- Throws:
NotPositiveException- ifn < 0.
-
stirlingS2
public static long stirlingS2(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticExceptionReturns the Stirling number of the second kind, "S(n,k)", the number of ways of partitioning ann-element set intoknon-empty subsets.The preconditions are
0 <= k <= n(otherwiseNotPositiveExceptionis thrown)- Parameters:
n- the size of the setk- the number of non-empty subsets- Returns:
S(n,k)- Throws:
NotPositiveException- ifk < 0.NumberIsTooLargeException- ifk > n.MathArithmeticException- 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)- Since:
- 3.1
-
combinationsIterator
public static java.util.Iterator<int[]> combinationsIterator(int n, int k)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 == 0an Iterator containing an empty array is returned and ifk == nan 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
iteratorover the k-sets in n. - Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.
-
checkBinomial
public static void checkBinomial(int n, int k) throws NumberIsTooLargeException, NotPositiveExceptionCheck binomial preconditions.- Parameters:
n- Size of the set.k- Size of the subsets to be counted.- Throws:
NotPositiveException- ifn < 0.NumberIsTooLargeException- ifk > n.
-
-