static real ChiSquare(creal x, cint df) { real y; if( df <= 0 ) return -999; if( x <= 0 ) return 0; if( x > 1000*df ) return 1; if( df > 1000 ) { if( x < 2 ) return 0; y = 2./(9*df); y = (powx(x/df, 1/3.) - (1 - y))/sqrtx(y); if( y > 5 ) return 1; if( y < -18.8055 ) return 0; return Normal(y); } y = .5*x; if( df & 1 ) { creal sqrty = sqrtx(y); real h = Erf(sqrty); count i; if( df == 1 ) return h; y = sqrty*expx(-y)/.8862269254527579825931; for( i = 3; i < df; i += 2 ) { h -= y; y *= x/i; } y = h - y; } else { real term = expx(-y), sum = term; count i; for( i = 1; i < df/2; ++i ) sum += term *= y/i; y = 1 - sum; } return Max(0., y); }
void test(Power power) { int i = 0; clock_t t1 = clock(); for(i; i < sizeof(cases)/sizeof(testcase); i++) { testcase c = cases[i]; printf("powx(%d, %d) expect %g, got %g\n", c.x, c.n, c.expect, powx(c.x, c.n, power)); } clock_t t2 = clock(); printf("cost time: %fs\n\n", difftime(t2, t1)/CLOCKS_PER_SEC); }
static void RefineGrid(cThis *t, Grid grid, Grid margsum) { real avgperbin, thisbin, newcur, delta; Grid imp, newgrid; int bin, newbin; /* smooth the f^2 value stored for each bin */ real prev = margsum[0]; real cur = margsum[1]; real norm = margsum[0] = .5*(prev + cur); for( bin = 1; bin < NBINS - 1; ++bin ) { creal s = prev + cur; prev = cur; cur = margsum[bin + 1]; norm += margsum[bin] = (s + cur)/3.; } norm += margsum[NBINS - 1] = .5*(prev + cur); if( norm == 0 ) return; norm = 1/norm; /* compute the importance function for each bin */ avgperbin = 0; for( bin = 0; bin < NBINS; ++bin ) { real impfun = 0; if( margsum[bin] > 0 ) { creal r = margsum[bin]*norm; avgperbin += impfun = powx((r - 1)/log(r), 1.5); } imp[bin] = impfun; } avgperbin /= NBINS; /* redefine the size of each bin */ cur = newcur = 0; thisbin = 0; bin = -1; for( newbin = 0; newbin < NBINS - 1; ++newbin ) { while( thisbin < avgperbin ) { thisbin += imp[++bin]; prev = cur; cur = grid[bin]; } thisbin -= avgperbin; delta = (cur - prev)*thisbin; newgrid[newbin] = SHARPEDGES ? cur - delta/imp[bin] : (newcur = Max(newcur + 16*DBL_EPSILON, cur - 2*delta/(imp[bin] + imp[IDim(bin - 1)]))); } Copy(grid, newgrid, NBINS - 1); grid[NBINS - 1] = 1; }
int main(){ int a = powx(2,8); return 0; }