原始 C 中的 beta 函数不完整



我的一个朋友需要一个类似 MatLAB 的 betainc 函数,用于可编程逻辑器件 (PLD) 中的一些统计计算(我不是硬件人员,还不知道他项目的任何细节)。

因此,使用预编译库不是一种选择。考虑到三个参数中的每一个都是可变的,她需要一个原始 C 中的实现。

网络上的某个地方有好的吗?

提前非常感谢!

我知道我回答得很晚,但是您目前接受的答案(使用"数字食谱"中的代码)有一个糟糕的许可证。此外,它对尚未拥有这本书的其他人没有帮助。

以下是在 Zlib 许可证下发布的不完整测试函数的原始 C99 代码:

#include <math.h>
#define STOP 1.0e-8
#define TINY 1.0e-30
double incbeta(double a, double b, double x) {
    if (x < 0.0 || x > 1.0) return 1.0/0.0;
    /*The continued fraction converges nicely for x < (a+1)/(a+b+2)*/
    if (x > (a+1.0)/(a+b+2.0)) {
        return (1.0-incbeta(b,a,1.0-x)); /*Use the fact that beta is symmetrical.*/
    }
    /*Find the first part before the continued fraction.*/
    const double lbeta_ab = lgamma(a)+lgamma(b)-lgamma(a+b);
    const double front = exp(log(x)*a+log(1.0-x)*b-lbeta_ab) / a;
    /*Use Lentz's algorithm to evaluate the continued fraction.*/
    double f = 1.0, c = 1.0, d = 0.0;
    int i, m;
    for (i = 0; i <= 200; ++i) {
        m = i/2;
        double numerator;
        if (i == 0) {
            numerator = 1.0; /*First numerator is 1.0.*/
        } else if (i % 2 == 0) {
            numerator = (m*(b-m)*x)/((a+2.0*m-1.0)*(a+2.0*m)); /*Even term.*/
        } else {
            numerator = -((a+m)*(a+b+m)*x)/((a+2.0*m)*(a+2.0*m+1)); /*Odd term.*/
        }
        /*Do an iteration of Lentz's algorithm.*/
        d = 1.0 + numerator * d;
        if (fabs(d) < TINY) d = TINY;
        d = 1.0 / d;
        c = 1.0 + numerator / c;
        if (fabs(c) < TINY) c = TINY;
        const double cd = c*d;
        f *= cd;
        /*Check for stop.*/
        if (fabs(1.0-cd) < STOP) {
            return front * (f-1.0);
        }
    }
    return 1.0/0.0; /*Needed more loops, did not converge.*/
}

它取自这个 Github 存储库。这里还有一篇关于它如何工作的非常彻底的文章。

希望对您有所帮助。

或者

你可以阅读"C中的数字配方"并找到完整的来源。 您将不得不担心许可问题,但它会对该功能及其实现的内容进行清晰的解释。

最新更新