Incomplete beta function in raw C -


a friend of mine needs analogue of matlab's betainc function statistical calculations in programmable logic devices (pld's) (i'm not man of hardware , don't know details on project yet).

therefore using precompiled libraries not option. needs implementation in raw c considering each of 3 parameters variable.

is there 1 somewhere on web?

thank in advance!

i know i'm late answering, accepted answer (using code "numerical recipes") has terrible license. also, doesn't others don't own book.

here raw c99 code incomplete beta function released under zlib license:

#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 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 fact beta symmetrical.*/     }      /*find first part before 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 evaluate continued fraction.*/     double f = 1.0, c = 1.0, d = 0.0;      int i, m;     (i = 0; <= 200; ++i) {         m = i/2;          double numerator;         if (i == 0) {             numerator = 1.0; /*first numerator 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 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 stop.*/         if (fabs(1.0-cd) < stop) {             return front * (f-1.0);         }     }      return 1.0/0.0; /*needed more loops, did not converge.*/ } 

it taken github repo. there thorough write-up how works here.

hope find helpful.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -