java广义超几何函数

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (56)

我正在寻找一个可以计算广义超几何函数的java库(http://en.wikipedia.org/wiki/Generalized_hypergeometric_series)。我看着Apach Common Math,但没有找到这个功能。实际上,我需要函数来计算beta-binomial分布的累积概率函数(http://en.wikipedia.org/wiki/Beta-binomial_distribution)。如果有人知道包括发行版在内的java包,那对我来说会很好。

提问于
用户回答回答于

我认为可以使用我编写的代码来近似超几何函数的值:

作为下一步,可以估计近似值的误差。

/**
 * The generalized hypergeometric function is a convergent power series \sum_{i=0}^{\infty} c_i x^i
 * where the coefficients satisfy c_{n+1}/c_n = A(n)/B(n) for some polynomials A and B in n.
 * It is customary to factor out the leading term, so c_0 is assumed to be 1
 */

public class HypergeometricFunction {
    private final int degreeOfApproximation;
    private final double[] coefficientsOfA;
    private final double[] coefficientsOfB;
    private final double[] coefficientsOfHypergeometricFunction;

    public HypergeometricFunction(int degreeOfApproximation, double[] coefficientsOfA, double[] coefficientsOfB) {
        this.degreeOfApproximation = degreeOfApproximation;
        this.coefficientsOfA = coefficientsOfA;
        this.coefficientsOfB = coefficientsOfB;
        this.coefficientsOfHypergeometricFunction = generateCoefficients();
    }

    /**
     * @param x input
     * @return Approximation to the hypergeometric function by taking the first
     * {@code degreeOfApproximation} terms from the series.
     */
    public double approximate(double x){
        return evaluatePolynomial(x, coefficientsOfHypergeometricFunction);
    }


    private double[] generateCoefficients() {
        double[] coefficients = new double[degreeOfApproximation];
        coefficients[0] = 1;
        for (int i = 1; i < degreeOfApproximation; i++)
            coefficients[i] = (evaluatePolynomial(i, coefficientsOfA) / evaluatePolynomial(i, coefficientsOfB)) * coefficients[i - 1];
        return coefficients;
    }

    private double evaluatePolynomial(double n, double[] coefficients) {
        int length = coefficients.length;
        double out = 0.0D;
        for (int i = 0; i < length; i++) {
            out += coefficients[i] * pow(n, i);
        }
        return out;
    }

    private double pow(double a, int b) {
        double out = 1;
        for (int i = 0; i < b; i++) out *= a;
        return out;
    }

}

如果系列收敛(因此提供了适当的超几何函数),那么lim[c_i*x^i]必须为零,所以如果你degreeOfApproximation足够大,这应该提供合理的近似值。

多项式A和B是wiki文章中提到的多项式,为了使用这些代码,您必须为构造函数提供这些多项式的系数数组,以及所需的近似度。

用户回答回答于

扫码关注云+社区