Package org.evoludo.math
Class Combinatorics
Object
Combinatorics
Collection of convenience methods for mathematical operations dealing with
combinatorics.
- Author:
- Christoph Hauert
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic double
combinationFrac
(int n, int m, int k) Efficient calculation of \[\dfrac{\binom{n}{k}}{\binom{m}{k}}=\frac{n!(m-k)!}{m!(n-k)!},\] orBinomial[n,k]/Binomial[m,k]
for Mathematica:, while reducing the risk of numerical overflow.static int
combinations
(int n, int k) Combinations: number of ways to drawk
elements from pool of sizen
.static long
combinations
(long n, long k) Combinations: number of ways to drawk
elements from pool of sizen
.static double
H2
(int X, int x, int Y, int y) Efficient calculation of the hypergeometric probability distributionH<sub>2</sub>(X,x,Y,y)
for samplingx
individuals from pool of sizeX
andy
individuals from pool of sizeY
.static double
pow
(double x, int n) Calculatex^n
for doublex
and integern
.static double
pow
(int x, int n) Calculatex^n
for integerx
and integern
.private static double
powabs
(double x, int n) Helper method to calculate \(x^n\) with \(n>0\).private static int
powabs
(int x, int n) Helper method to calculate \(x^n\) with \(n>0\).
-
Constructor Details
-
Combinatorics
public Combinatorics()Ensure non-instantiability with private default constructor
-
-
Method Details
-
pow
public static double pow(int x, int n) Calculatex^n
for integerx
and integern
. Forn≥0
the result is an integer but returned as a double, which is necessary to deal withn<0
. All intermediate calculations are perfomed as integers and will throw the corresponding exceptions, e.g. if numbers exceedInteger#MAX_VALUE
. This is an optimization for the CPU intenseMath.pow(double, double)
.- Parameters:
x
- the basis of the powern
- the integer exponent of the power- Returns:
x^n
-
powabs
private static int powabs(int x, int n) Helper method to calculate \(x^n\) with \(n>0\).- Parameters:
x
- the basis of the powern
- the (positive) integer exponent of the power- Returns:
x^n
-
pow
public static double pow(double x, int n) Calculatex^n
for doublex
and integern
. This is an optimization for the CPU intenseMath.pow(double, double)
.- Parameters:
x
- the basis of the powern
- the exponent of the power- Returns:
x^n
-
powabs
private static double powabs(double x, int n) Helper method to calculate \(x^n\) with \(n>0\).- Parameters:
x
- the basis of the powern
- the (positive) integer exponent of the power- Returns:
x^n
-
combinations
public static int combinations(int n, int k) Combinations: number of ways to drawk
elements from pool of sizen
.Mathematica:
Binomial[n,k] = n!/(k!(n-k)!)
- Parameters:
n
- the pool sizek
- the number of samples- Returns:
Binomial[n,k]
- Throws:
ArithmeticException
- if result>Integer.MAX_VALUE
-
combinations
public static long combinations(long n, long k) Combinations: number of ways to drawk
elements from pool of sizen
.Mathematica:
Binomial[n,k] = n!/(k!(n-k)!)
- Parameters:
n
- the pool sizek
- the number of samples- Returns:
Binomial[n,k]
- Throws:
ArithmeticException
- if result>Long.MAX_VALUE
-
combinationFrac
public static double combinationFrac(int n, int m, int k) Efficient calculation of \[\dfrac{\binom{n}{k}}{\binom{m}{k}}=\frac{n!(m-k)!}{m!(n-k)!},\] orBinomial[n,k]/Binomial[m,k]
for Mathematica:, while reducing the risk of numerical overflow.Note, this is the same as
H2(n, 0, m-n, k)
withm>n
.- Parameters:
n
- the size of first poolm
- the size of the second poolk
- the number of samples to draw from each pool (without replacement)- Returns:
Binomial[n,k]/Binomial[m,k]
- See Also:
-
H2
public static double H2(int X, int x, int Y, int y) Efficient calculation of the hypergeometric probability distributionH<sub>2</sub>(X,x,Y,y)
for samplingx
individuals from pool of sizeX
andy
individuals from pool of sizeY
.Mathematica:
H_2[X, x, Y, y] = Binomial[X,x] Binomial[Y,y] / Binomial[X+Y,x+y]
- Parameters:
X
- the size of first poolx
- the number samples from first poolY
- the size of second pooly
- the number samples from second pool- Returns:
H_2[X, x, Y, y]
-