REAL8 cov_Phi_ij ( const cov_params_t *params ) { gsl_function integrand; cov_params_t par; int stat; double epsabs = 1e-6; double epsrel = 1e-6; double abserr; size_t neval; double av_ij, av_i, av_j; par = (*params); /* struct-copy */ integrand.params = (void*)∥ /* compute <phi_i phi_j> */ integrand.function = &Phi_i_Phi_j; stat = gsl_integration_qng (&integrand, 0, 1, epsabs, epsrel, &av_ij, &abserr, &neval); if ( stat != 0 ) { XLALPrintError ( "\nGSL-integration 'gsl_integration_qng()' of <Phi_i Phi_j> failed!\n"); XLAL_ERROR_REAL8( XLAL_EFUNC ); } /* printf ( "Integration of <Phi_i Phi_j> succeeded with abserr = %g, neval = %d: result = %g\n", abserr, neval, av_ij ); */ /* compute <phi_i> */ integrand.function = &Phi_i; par.comp = par.comp1; stat = gsl_integration_qng (&integrand, 0, 1, epsabs, epsrel, &av_i, &abserr, &neval); if ( stat != 0 ) { XLALPrintError ( "\nGSL-integration 'gsl_integration_qng()' of <Phi_i> failed!\n"); XLAL_ERROR_REAL8( XLAL_EFUNC ); } /* printf ( "Integration of <Phi_i> succeeded with abserr = %g, neval = %d: result = %g\n", abserr, neval, av_i ); */ /* compute <phi_i> */ integrand.function = &Phi_i; par.comp = par.comp2; stat = gsl_integration_qng (&integrand, 0, 1, epsabs, epsrel, &av_j, &abserr, &neval); if ( stat != 0 ) { XLALPrintError ( "\nGSL-integration 'gsl_integration_qng()' of <Phi_j> failed!\n"); XLAL_ERROR_REAL8( XLAL_EFUNC ); } /* printf ( "Integration of <Phi_i> succeeded with abserr = %g, neval = %d: result = %g\n", abserr, neval, av_j ); */ return ( av_ij - av_i * av_j ); /* return covariance */ } /* cov_Phi_ij() */
/* * params should give the parameters to integrate over X: * params[0]: xlower; * params[1]: xupper; * params[2]: abseps; * params[3]: releps; * */ double F_Qng2d(double y, void * params) { double *array; double result,abserr; size_t neval; double valy; size_t returnval; gsl_function F; valy=y; array=(double *)params; F.function=F_Qng2d_FixY; F.params=&valy; gsl_integration_qng (&F, array[0], array[1], array[2], array[3], &result, &abserr, &neval); #ifdef DEBUG_QNG2D fprintf(stderr,"F_Qng2d: returnval = %d\n", returnval); fprintf(stderr,"F_Qng2d: intervals = %d\n", neval); fprintf(stderr,"F_Qng2d: result = % .18f\n", result); fprintf(stderr,"F_Qng2d: abserr = % .18f\n", abserr); #endif return result; }
size_t qng2d(DoubleFunc2d func, double xlower, double xupper, double ylower, double yupper, double abseps,double releps, double *result, double *abserr) { gsl_function F; int returnval; size_t neval; double params[4]; /* set the 2d integrand globally */ Func_QNG2D = func; /* init the function */ F.function=F_Qng2d; params[0]=xlower; params[1]=xupper; params[2]=abseps; params[3]=releps; F.params=params; returnval=gsl_integration_qng (&F, ylower, yupper, abseps, releps, result, abserr, &neval); #ifdef DEBUG_QNG2D fprintf(stderr,"qng2d: returnval = %d\n", returnval); fprintf(stderr,"qng2d: intervals = %d\n", neval); fprintf(stderr,"qng2d: result = % .18f\n", *result); fprintf(stderr,"qng2d: abserr = % .18f\n", *abserr); #endif return returnval; }
static VALUE rb_gsl_integration_qng(int argc, VALUE *argv, VALUE obj) { double a, b, epsabs = EPSABS_DEFAULT, epsrel = EPSREL_DEFAULT; double result, abserr; size_t neval; gsl_function *F = NULL; int status; // local variable 'itmp' declared and set, but never used //int itmp; if (argc < 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for >= 1)", argc); switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: CHECK_FUNCTION(argv[0]); Data_Get_Struct(argv[0], gsl_function, F); /*itmp =*/ get_a_b_epsabs_epsrel(argc, argv, 1, &a, &b, &epsabs, &epsrel); break; default: /*itmp =*/ get_a_b_epsabs_epsrel(argc, argv, 0, &a, &b, &epsabs, &epsrel); Data_Get_Struct(obj, gsl_function, F); break; } status = gsl_integration_qng(F, a, b, epsabs, epsrel, &result, &abserr, &neval); return rb_ary_new3(4, rb_float_new(result), rb_float_new(abserr), INT2FIX(neval), INT2FIX(status)); }
int main (void) { gsl_integration_workspace * w = gsl_integration_workspace_alloc (4000); size_t neval; double result, error; double a=0.0, b=cos(1.27); gsl_function F; F.function = &f; /* gsl_integration_qags (&F, a, b, 1.0e-9, 1e-9, 4000, w, &result, &error); */ /* gsl_integration_qag(&F, a, b, 1.0e-7, 1.0e-7, 4000, 1, w, &result, &error); */ gsl_integration_qng(&F, a, b, 1.0e-6, 1.0e-6, &result, &error, &neval); printf ("result = % .18f\n", result); printf ("estimated error = % .18f\n", error); gsl_integration_workspace_free (w); return 0; }
double d2Nglasma1(double pT, double yp, double yq, double rts) { double sinres, cosres; static struct glasma_params params; params.pT = pT; params.yp = yp; params.yq = yq; params.rts = rts; phiIntKernel.function = &phiKernel; (params.Kernel).function = &cosKernel; phiIntKernel.params = ¶ms; gsl_integration_qng(&phiIntKernel, -M_PI, M_PI, abserr, relerr, &cosres, &error, &neval); (params.Kernel).function = &sinKernel; phiIntKernel.params = ¶ms; gsl_integration_qng(&phiIntKernel, -M_PI, M_PI, abserr, relerr, &sinres, &error, &neval); double norm = (Nc*Nc)/gsl_pow_3(Nc*Nc-1.)/(4.*pow(M_PI,10.))/gsl_pow_2(pT*pT)*alpha(pT)*alpha(pT) ; return norm*(cosres*cosres + sinres*sinres); }
double FermiDiracIntegral(double &w) { double result; gsl_function f; double err; size_t n; f.function = &FermiDiracIntegrand; f.params = &w; gsl_integration_qng(&f, 0.0l, 24.0l, 5.0e-7, 1.0e-7, &result, &err, &n); return result; }
//does phi integration over d^2k_\perp double phiKernel(double x, void *p) { struct glasma_params params = *(struct glasma_params *)p; double result, error; params.phi = x; gsl_function F = params.Kernel; F.params = ¶ms; gsl_integration_qng(&F, kT_min, 2.0*kT_max, abserr, relerr,\ &result, &error, &neval); return result; }
double Model::n1CDF(Eigen::VectorXd &d) { gsl_function F; F.function = &n1PDF; F.params = &d; double result, error; // integration sometimes fails gsl_set_error_handler_off(); int limit = 10000; gsl_integration_workspace *w = gsl_integration_workspace_alloc(limit); double epsabs = 0, epsrel = 1e-7; double lower = 0, upper; const double chi = 2, sdI = 1; // following assumes these values. do not change these. int status = gsl_integration_qagiu(&F, lower, epsabs, epsrel, limit, w, &result, &error); if (status != GSL_SUCCESS) { double mean_drift = d.mean(); if (mean_drift > d(0)) { lower = (chi - 0.98) / (mean_drift + 2 * sdI); } else { lower = (chi - 0.98) / (d(0) + 2 * sdI); } upper = 0.02 * chi / (mean_drift - 2 * sdI); size_t neval; status = gsl_integration_qng(&F, lower, upper, epsabs, epsrel, &result, &error, &neval); } if (status != GSL_SUCCESS) { // std::cout << "Integration Failed" << std::endl; // result = GSL_NAN; result = 0.0; } gsl_integration_workspace_free(w); return result; }
/* * call the integration function according to the parameters. */ static int call_integration_func(gmdi_multi_dim_inte_param* params) { gmdi_one_inte_param * oip = params->oip + params->intern.dim; int ret; double inte_limit_low = call_gmdi_function_or_constant(&oip->x0, params->intern.x); double inte_limit_high = call_gmdi_function_or_constant(&oip->x1, params->intern.x); gsl_function gf; gf.function = big_g; gf.params = params; switch (oip->inte_func) { case GMDI_INTE_FUNCTIONS_QNG: ret = gsl_integration_qng(&gf, inte_limit_low, inte_limit_high, oip->epsabs, oip->epsrel, params->intern.results + params->intern.dim, params->intern.abserrs + params->intern.dim, ¶ms->neval); break; case GMDI_INTE_FUNCTIONS_QAG: ret = gsl_integration_qag(&gf, inte_limit_low, inte_limit_high, oip->epsabs, oip->epsrel, oip->limit, oip->key, oip->intern.giw, params->intern.results + params->intern.dim, params->intern.abserrs + params->intern.dim); break; default: /* No proper inte_func is found */ GSL_ERROR("Invalid inte_func is specified", GSL_EINVAL); } return ret; }
double multiIntegrate(double *range,int dimention,double (*func)(void *,const double *x),void *arg){ double result,error; size_t neval; ingArg info; gsl_function F; info.dimention=dimention; info.rank=1; info.range=range; info.func=func; info.arg=arg; info.stack=malloc(sizeof(double)*dimention); F.function = __subMultiIntegrate; F.params=&info; gsl_integration_qng(&F,range[0],range[1],EPSABS,EPSREL,&result,&error,&neval); return result; }
double __subMultiIntegrate(double x,void *arg){ double result,error; size_t neval; ingArg *info=arg; gsl_function F; if(info->rank==info->dimention){ info->stack[info->rank-1]=x; return info->func(info->arg,info->stack); }else{ info->stack[info->rank-1]=x; info->rank++; F.function = &__subMultiIntegrate; F.params=info; gsl_integration_qng(&F,info->range[(info->rank-1)*info->dimention],info->range[(info->rank-1)*info->dimention+1],EPSABS,EPSREL,&result,&error,&neval); info->rank--; return result; } }
double d2Nglasma0(double pT, double qT, double phipq, double yp, double yq, double rts) { static struct glasma_params params; params.pT = pT; params.qT = qT; params.yp = yp; params.yq = yq; params.rts = rts; params.phipq = phipq; phiIntKernel.function = &phiKernel; (params.Kernel).function = &doubleKernel; phiIntKernel.params = ¶ms; gsl_integration_qng(&phiIntKernel, -M_PI, M_PI, abserr, relerr,\ &result, &error, &neval); double norm = (Nc*Nc)/gsl_pow_3(Nc*Nc-1.)/(4.*pow(M_PI,10.))/gsl_pow_2(pT* qT)*alpha(pT)*alpha(qT); return norm*result; }
// ======================================================================== // non-adaptive integration // ======================================================================== double NumericalDefiniteIntegral::QNG ( _Function* F ) const { if( 0 == F ) { Exception("QNG::invalid function") ; } // integration limits const double low = std::min ( m_a , m_b ) ; const double high = std::max ( m_a , m_b ) ; size_t neval = 0 ; int ierror = gsl_integration_qng ( F->fn , low , high , m_epsabs , m_epsrel , &m_result , &m_error , &neval ) ; if( ierror ) { gsl_error( "NumericalIndefiniteIntegral::QNG " , __FILE__ , __LINE__ , ierror ) ; } // sign if ( m_a > m_b ) { m_result *= -1 ; } return m_result ; }
int main() { const double Lx = 8; const double Ly = 4; const double Lz = 1; const int Nx = 32*4*4*2; const int Ny = 16*4*4*2; const int Nz = 8*4*4*2; const double dx = Lx/Nx; const double dy = Ly/Ny; const double dz = Lz/Nz; const double tau_tail = 1.52; const double a0 = 1.29, a1 = -22.57, a2 = 78.39, a3 = -52.83; const double t = 0.0; const double time_period = 1.0; double interp_coefs[4]; interp_coefs[0] = a0; interp_coefs[1] = a1; interp_coefs[2] = a2; interp_coefs[3] = a3; //No. of points on the backbone and till head. const int headNs = int(ceil(LENGTH_TILLHEAD/dx)); const int tailNs = int(ceil((LENGTH_FISH - LENGTH_TILLHEAD)/dx) ); const int bodyNs = headNs + tailNs + 1; std::vector<std::pair<int,int> > immersedBodyData(bodyNs); std::vector<std::pair<double,double> > immersedBodyWidthHeight(bodyNs); for (int i = 1; i <= headNs + 1; ++i) { const double s = (i-1)*dx; const double section = sqrt(2*WIDTH_HEAD*s - s*s); const double height = MINOR_AXIS*std::sqrt( 1 - pow((s - MAJOR_AXIS)/MAJOR_AXIS,2) ); const int numPtsInSection = int( ceil(section/dy) ); const int numPtsInHeight = int( ceil(height/dz) ); immersedBodyData[i-1] = std::make_pair(numPtsInSection,numPtsInHeight); immersedBodyWidthHeight[i-1] = std::make_pair(section,height); } for (int i = headNs + 2; i <= bodyNs; ++i) { const double s = (i-1)*dx; const double section = WIDTH_HEAD*(LENGTH_FISH - s)/(LENGTH_FISH - LENGTH_TILLHEAD); const double height = MINOR_AXIS*std::sqrt( 1 - pow((s - MAJOR_AXIS)/MAJOR_AXIS,2) ); const int numPtsInSection = int( ceil(section/dy) ); const int numPtsInHeight = int( ceil(height/dz) ); immersedBodyData[i-1] = std::make_pair(numPtsInSection,numPtsInHeight); immersedBodyWidthHeight[i-1] = std::make_pair(section,height); } int total_lag_pts = 0; double input[7]; input[0] = interp_coefs[0]; input[1] = interp_coefs[1]; input[2] = interp_coefs[2]; input[3] = interp_coefs[3]; input[4] = tau_tail; input[5] = t; input[6] = time_period; gsl_function Fx, Fy; Fx.function = xPosition; Fx.params = input; Fy.function = yPosition; Fy.params = input; double ybase, xbase, errory, errorx; size_t nevalsy, nevalsx; //Find the deformed shape. Rotate the shape about center of mass. std::vector<std::vector<double> > shape_new(3); for (int i = 1; i <= bodyNs; ++i) { const int numPtsInSection = immersedBodyData[i-1].first; const int numPtsInHeight = immersedBodyData[i-1].second; const double width = immersedBodyWidthHeight[i-1].first; const double depth = immersedBodyWidthHeight[i-1].second; const double s = (i-1)*dx; gsl_integration_qng(&Fx,0,s,1e-8,0.0,&xbase,&errorx,&nevalsx); gsl_integration_qng(&Fy,0,s,1e-8,0.0,&ybase,&errory,&nevalsy); if (numPtsInSection && numPtsInHeight) { //Fill the middle line first. for (int k = -numPtsInHeight; k <= numPtsInHeight; ++k) { shape_new[0].push_back(xbase); shape_new[1].push_back(ybase); shape_new[2].push_back(k*dz); }//middle line filled. total_lag_pts += 2*numPtsInHeight + 1; //Fill the rest of the cross section next. for (int j = 1; j <= numPtsInSection; ++j) { const double y = j*dy; for (int k = -numPtsInHeight; k <= numPtsInHeight; ++k) { const double z = k*dz; if ( (std::pow(y/width,2) + std::pow(z/depth,2)) <= 1 ) //use elliptical cross sections { shape_new[0].push_back(xbase); //right side. shape_new[1].push_back(ybase + y); shape_new[2].push_back(z); shape_new[0].push_back(xbase); //left side. shape_new[1].push_back(ybase - y); shape_new[2].push_back(z); total_lag_pts += 2; } } }//cross section filled } } std::fstream eelstream; eelstream.open("eel3d.vertex",std::fstream::out); assert((unsigned)total_lag_pts == shape_new[0].size()); eelstream << total_lag_pts << "\n"; for (int k = 1; k <= total_lag_pts ; ++k) eelstream << shape_new[0][k-1] + 5.5 << "\t" << shape_new[1][k-1] << "\t" << shape_new[2][k-1] << "\n"; return 0; }
int lua_integration_integrate(lua_State * L) { double a=0.0; double b=1.0; double c=0.5; double epsabs=0.0; double epsrel=0.0000001; double alpha=0.0; double beta=0.0; int mu=0; int nu=0; size_t limit=100; size_t n=0; int key=1; double result=0; double abserr=0; size_t neval=0; gsl_integration_workspace * w=0; multi_param mp; mp.L=L; lua_pushstring(L,"f"); lua_gettable(L,-2); if(lua_isfunction(L,-1)) { mp.f_index=luaL_ref(L, LUA_REGISTRYINDEX); } else { luaL_error(L,"%s\n","missing function"); } gsl_function F; F.function = &int_f_cb; F.params = ∓ lua_pushstring(L,"epsabs"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { epsabs=lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"epsrel"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { epsrel=lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"a"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { a=lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"b"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { b=lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"c"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { c=lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"limit"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { limit=(size_t)lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"n"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { n=(size_t)lua_tonumber(L,-1); } lua_pop(L,1); if(limit>n) n=limit; lua_pushstring(L,"key"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { key=(int)lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"alpha"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { alpha=(double)lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"beta"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { beta=(double)lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"mu"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { mu=(int)lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"nu"); lua_gettable(L,-2); if(lua_isnumber(L,-1)) { nu=(int)lua_tonumber(L,-1); } lua_pop(L,1); lua_pushstring(L,"algorithm"); lua_gettable(L,-2); if(lua_isstring(L,-1)) { if(!strcmp(lua_tostring(L,-1),"qng")) { gsl_integration_qng(&F,a,b,epsabs,epsrel,&result,&abserr,&neval); } else if(!strcmp(lua_tostring(L,-1),"qag")) { w=gsl_integration_workspace_alloc(n); gsl_integration_qag(&F,a,b,epsabs,epsrel,limit,key,w,&result,&abserr); } else if(!strcmp(lua_tostring(L,-1),"qags")) { w=gsl_integration_workspace_alloc(n); gsl_integration_qags(&F,a,b,epsabs,epsrel,limit,w,&result,&abserr); } else if(!strcmp(lua_tostring(L,-1),"qagi")) { w=gsl_integration_workspace_alloc(n); gsl_integration_qagi(&F,epsabs,epsrel,limit,w,&result,&abserr); } else if(!strcmp(lua_tostring(L,-1),"qagiu")) { w=gsl_integration_workspace_alloc(n); gsl_integration_qagiu(&F,a,epsabs,epsrel,limit,w,&result,&abserr); } else if(!strcmp(lua_tostring(L,-1),"qagil")) { w=gsl_integration_workspace_alloc(n); gsl_integration_qagil(&F,b,epsabs,epsrel,limit,w,&result,&abserr); } else if(!strcmp(lua_tostring(L,-1),"qawc")) { w=gsl_integration_workspace_alloc(n); gsl_integration_qawc(&F,a,b,c,epsabs,epsrel,limit,w,&result,&abserr); } else if(!strcmp(lua_tostring(L,-1),"qaws")) { w=gsl_integration_workspace_alloc(n); gsl_integration_qaws_table * table=gsl_integration_qaws_table_alloc(alpha,beta,mu,nu); gsl_integration_qaws(&F,a,b,table,epsabs,epsrel,limit,w,&result,&abserr); gsl_integration_qaws_table_free(table); } else if(!strcmp(lua_tostring(L,-1),"cquad")) { gsl_integration_cquad_workspace * w=gsl_integration_cquad_workspace_alloc(n); gsl_integration_cquad(&F,a,b,epsabs,epsrel,w,&result,&abserr,&neval); gsl_integration_cquad_workspace_free(w); } else { luaL_error(L,"%s\n","invalid algorithm"); } } else { gsl_integration_cquad_workspace * w=gsl_integration_cquad_workspace_alloc(n); gsl_integration_cquad(&F,a,b,epsabs,epsrel,w,&result,&abserr,&neval); gsl_integration_cquad_workspace_free(w); } lua_pop(L,1); lua_pop(L,1); lua_pushnumber(L,result); lua_pushnumber(L,abserr); lua_pushnumber(L,neval); if(mp.fdf_index>=0) luaL_unref(L, LUA_REGISTRYINDEX, mp.fdf_index); if(w) gsl_integration_workspace_free(w); return 3; }