Top Description Fields Constructors Methods
org.apache.commons.math3.util

public final Class CombinatoricsUtils

extends Object
Class Inheritance
Imports
java.util.Iterator, java.util.concurrent.atomic.AtomicReference, org.apache.commons.math3.exception.MathArithmeticException, .NotPositiveException, .NumberIsTooLargeException, org.apache.commons.math3.exception.util.LocalizedFormats

Combinatorial utilities.
Since
3.3

Field Summary

Modifier and TypeField and Description
pack-priv static final long[]
FACTORIALS

All long-representable factorials

pack-priv static final AtomicReference<long[][]>
STIRLING_S2

Stirling numbers of the second kind.

Constructor Summary

AccessConstructor and Description
private
CombinatoricsUtils()

Private constructor (class contains only static methods).

Method Summary

Modifier and TypeMethod and Description
public static long

Returns:

n choose k
binomialCoefficient
(final int
the size of the set
n
,
final int
the size of the subsets to be counted
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.

public static double

Returns:

n choose k
binomialCoefficientDouble
(final int
the size of the set
n
,
final int
the size of the subsets to be counted
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.

public static double

Returns:

n choose k
binomialCoefficientLog
(final int
the size of the set
n
,
final int
the size of the subsets to be counted
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.

public static void
checkBinomial(final int
Size of the set.
n
,
final int
Size of the subsets to be counted.
k
)

Check binomial preconditions.

public static Iterator<int[]>

Returns:

an iterator over the k-sets in n.
combinationsIterator
(int
Size of the set from which subsets are selected.
n
,
int
Size of the subsets to be enumerated.
k
)

Returns an iterator whose range is the k-element subsets of {0, ..., n - 1} represented as int[] arrays.

public static long

Returns:

n!
factorial
(final int
argument
n
)

Returns n!.

public static double

Returns:

n!
factorialDouble
(final int
Argument.
n
)

Compute n!, the factorial of n (the product of the numbers 1 to n), as a double.

public static double

Returns:

n!
factorialLog
(final int
Argument.
n
)

Compute the natural logarithm of the factorial of n.

public static long

Returns:

S(n,k)
stirlingS2
(final int
the size of the set
n
,
final int
the number of non-empty subsets
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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FACTORIALSback to summary
pack-priv static final long[] FACTORIALS

All long-representable factorials

STIRLING_S2back to summary
pack-priv static final AtomicReference<long[][]> STIRLING_S2

Stirling numbers of the second kind.

Constructor Detail

CombinatoricsUtilsback to summary
private CombinatoricsUtils()

Private constructor (class contains only static methods).

Method Detail

binomialCoefficientback to summary
public static long binomialCoefficient(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException

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.

Preconditions:

  • 0 <= k <= n (otherwise MathIllegalArgumentException is thrown)
  • The result is small enough to fit into a 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 MathArithMeticException is thrown.

Parameters
n:int

the size of the set

k:int

the size of the subsets to be counted

Returns:long

n choose k

Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.
MathArithmeticException:
if the result is too large to be represented by a long integer.
binomialCoefficientDoubleback to summary
public static double binomialCoefficientDouble(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException

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.

Preconditions:

  • 0 <= k <= n (otherwise IllegalArgumentException is thrown)
  • The result is small enough to fit into a double. The largest value of n for 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:int

the size of the set

k:int

the size of the subsets to be counted

Returns:double

n choose k

Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.
MathArithmeticException:
if the result is too large to be represented by a long integer.
binomialCoefficientLogback to summary
public static double binomialCoefficientLog(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException

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.

Preconditions:

  • 0 <= k <= n (otherwise MathIllegalArgumentException is thrown)

Parameters
n:int

the size of the set

k:int

the size of the subsets to be counted

Returns:double

n choose k

Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.
MathArithmeticException:
if the result is too large to be represented by a long integer.
checkBinomialback to summary
public static void checkBinomial(final int n, final int k) throws NumberIsTooLargeException, NotPositiveException

Check binomial preconditions.

Parameters
n:int

Size of the set.

k:int

Size of the subsets to be counted.

Exceptions
NumberIsTooLargeException:
if k > n.
NotPositiveException:
if n < 0.
combinationsIteratorback to summary
public 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.

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.

Parameters
n:int

Size of the set from which subsets are selected.

k:int

Size of the subsets to be enumerated.

Returns:Iterator<int[]>

an iterator over the k-sets in n.

Exceptions
NotPositiveException:
if n < 0.
NumberIsTooLargeException:
if k > n.
factorialback to summary
public static long factorial(final int n) throws NotPositiveException, MathArithmeticException

Returns n!. Shorthand for n Factorial, the product of the numbers 1,...,n.

Preconditions:

  • n >= 0 (otherwise MathIllegalArgumentException is thrown)
  • The result is small enough to fit into a 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 MathArithMeticException is thrown.

Parameters
n:int

argument

Returns:long

n!

Exceptions
NotPositiveException:
if n < 0.
MathArithmeticException:
  • if the result is too large to be represented by a long.
  • if n > 20: The factorial value is too large to fit in a long.
factorialDoubleback to summary
public static double factorialDouble(final int n) throws NotPositiveException

Compute n!, the factorial of 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.

Parameters
n:int

Argument.

Returns:double

n!

Exceptions
NotPositiveException:
if n < 0.
factorialLogback to summary
public static double factorialLog(final int n) throws NotPositiveException

Compute the natural logarithm of the factorial of n.

Parameters
n:int

Argument.

Returns:double

n!

Exceptions
NotPositiveException:
if n < 0.
stirlingS2back to summary
public static long stirlingS2(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException

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.

The preconditions are 0 <= k <= n (otherwise NotPositiveException is thrown)

Parameters
n:int

the size of the set

k:int

the number of non-empty subsets

Returns:long

S(n,k)

Exceptions
NotPositiveException:
if k < 0.
NumberIsTooLargeException:
if k > 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