实数值的二项式系数

时间:2011-12-01 23:23:35

标签: java binomial-coefficients

我正在寻找为所有实数n和整数k定义的二项式系数(选择(n,k))的高效Java实现,即定义为:

enter image description here

1 个答案:

答案 0 :(得分:1)

使用Apache Commons Math 3

import org.apache.commons.math3.special.Gamma;

/**
 * Binomial coefficient for real numbers - the number of ways of picking y
 * unordered outcomes from x possibilities
 *
 * @see http://mathworld.wolfram.com/BinomialCoefficient.html
 *
 * @param x
 * @param y
 * @return binomial coefficient to be generalized to noninteger arguments
 */
public double binomial(double x, double y) {
    double res = Gamma.gamma(x + 1) / (Gamma.gamma(y + 1) * Gamma.gamma(x - y + 1));
    if(Double.isNaN(res)){
        return 0.0;
    }
    return res;
}

因此,对于输入binomial(0.5, 1.0),您应该获得0.5,例如Wolfram Alpha

binomial(2.5, 3) = 0.3125
binomial(2.0, 3) = 0.0
binomial(1.5, 3) = -0.0625