Exemple #1
0
/// Calculate the variance of the power spectrum on a scale R in h^{-1} Mpc
/// using a Gaussian filter. 
double cosmology::var_G(double R, double z)
{
    //Smith et al. 2003 Eq. 54
    //Int_0^{\infty} dk/k Delta^2(k) exp(-(kR)^2)
    double result1, result2, error;

    gsl_function F;
    F.function = &(dvar_G);

    //Initialize parameter block
    cvar_params p;
    p.cptr = this;
    p.R=&R;
    p.z=&z;

    F.params = &p;

    gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
    gsl_integration_qags (&F, 0., 1./R, 0, 1e-6, 1000, w, &result1, &error); 
    gsl_integration_workspace_free (w);

    gsl_integration_workspace * v = gsl_integration_workspace_alloc (1000);
    gsl_integration_qagiu (&F, 1./R, 0, 1e-6, 1000, v, &result2, &error); 
    gsl_integration_workspace_free (v);

    //printf ("result          = % .18f\n", result);
    //printf ("estimated error = % .18f\n", error);
    //printf ("intervals =  %d\n", w->size);


    return result1+result2;
    
}
Exemple #2
0
double gslInt_Greens(double ri , double r, double root_dv, double rh){


	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	std::vector<double> greenParam (3);


	greenParam[0] = ri;
	greenParam[1] = r;
	greenParam[2] = root_dv;

	gsl_function F;
	F.function = &dGreens;
	F.params = &greenParam;
	gsl_set_error_handler_off();
	gsl_integration_qags (&F, 1e-16, rh, 0, 1e-3, 1000, //x?
	                w, &result, &error); 

	gsl_integration_workspace_free (w);

	return result;

}
Exemple #3
0
double gslInt_jsyn(double mx, double r){
		//std::cout << "jsyn " <<std::endl;
	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (5000);

	double result, error;

	std::vector<double> alpha (2);

	alpha[0] = mx;
	alpha[1] = r;


	gsl_function F;
	F.function = &djsyn;
	F.params = &alpha;

	gsl_integration_qags (&F, me, mx, 0, 1e-3, 5000,
	                    w, &result, &error); 


	gsl_integration_workspace_free (w);

	return result;

	//std::cout << "gslInt_jsyn: " << std::setprecision(19) << result << std::endl;
}
Exemple #4
0
double gslInt_jsyn(double r){ 				// int over E

	///////////
	std::clock_t start;
	double duration;
	start = std::clock();
	///////////

	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	gsl_function F;
	F.function = &djsyn;
	F.params = &r;

	gsl_integration_qags (&F, me, p.mx, 0, 1e-2, 1000,
	                    w, &result, &error); 

	gsl_integration_workspace_free (w);

	duration = (std::clock()  -  start)/(double) CLOCKS_PER_SEC;
	if (duration > 1)
	std::cout << "alpha = "<< c.alpha << ", jsyn( r = " << r/kpc2cm <<" ) = "<< result <<" duration: " << duration <<std::endl;  //      ~30s-60s

	return result;

}
Exemple #5
0
static VALUE rb_gsl_integration_qags(int argc, VALUE *argv, VALUE obj)
{
  double a, b, epsabs = EPSABS_DEFAULT, epsrel = EPSREL_DEFAULT;
  double result, abserr;
  size_t limit = LIMIT_DEFAULT;
  gsl_function *F = NULL;
  gsl_integration_workspace *w = NULL;
  int status, intervals, flag = 0, itmp;
  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(argc, argv, 1, &a, &b);
    break;
  default:
    Data_Get_Struct(obj, gsl_function, F);
    itmp = get_a_b(argc, argv, 0, &a, &b);
    break;
  }
  flag = get_epsabs_epsrel_limit_workspace(argc, argv, itmp, &epsabs, &epsrel,
                                           &limit, &w);

  status = gsl_integration_qags(F, a, b, epsabs, epsrel, limit, w,
                                &result, &abserr);
  intervals = w->size;
  if (flag == 1) gsl_integration_workspace_free(w);
  return rb_ary_new3(4, rb_float_new(result), rb_float_new(abserr),
                     INT2FIX(intervals), INT2FIX(status));
}
void normalize_sigma8()
{
	int WORKSP = 5000; 
	double s8, k_min, k_max, result, error, par[2]; 
	gsl_integration_workspace *w = gsl_integration_workspace_alloc(WORKSP);

	s8 = Cosmo.sigma8;
	par[0]=8.; 
	PK_INDEX=0;

	k_min = Pks[0].k[0]; 
	k_max = Pks[0].k[Pks[0].npts-1];

		gsl_function F;
		F.function = &sigma_integ;
		F.params = &par;

		gsl_integration_qags(&F, k_min, k_max, 0, 1e-6, WORKSP, w, &result, &error);
		result*=(1./(2*PI*PI));

		Cosmo.norm_sigma8 = s8*s8/result;

			fprintf(stdout, "Original sigma8:%lf, required sigma8: %lf, normalization factor:%lf\n", 
				sqrt(result), s8, Cosmo.norm_sigma8);

		gsl_integration_workspace_free (w);

	normalize_all_power_spectra_to_sigma8();
}
double BesselFunction::integrate( const Subpixel& pixel_center) const 
{
    gsl_error_handler_t * old_handler=gsl_set_error_handler_off();
    double val = -1, abserr;
    dStorm::threed_info::ZPosition plane_z
        = dynamic_cast<const dStorm::threed_info::Lens3D&>
                (*trafo.optics.depth_info(dStorm::Direction_X))
            .z_position();
    int_info->delta_z = plane_z * 1E-6f * si::meter - fluorophore.z();

    int_info->orig_position = pixel_center;
    int_info->function = this;

    gsl_function func;
    func.function = &BesselFunction::wavelength_callback;
    func.params = int_info.get();

    if ( false ) {
        int status = gsl_integration_qags( &func, lambda.value()-15E-9, lambda.value()+15E-9, 0, 1E-3,
            int_info->integration_size, int_info->lambda, &val, &abserr );

        assert( ! status );
        gsl_set_error_handler(old_handler);
        return val / 30E-9;
    } else {
        double val = wavelength_callback( lambda.value(), func.params );
        return val;
    }
}
Exemple #8
0
double gslInt_diffusion( double E, double r){			// int over Ep
	double E_scaled = (int)(E/c.vscale);

	double vE = c.vlookup[E_scaled];

	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	std::vector<double> diffusionParams (3);

	diffusionParams[0] = E;
	diffusionParams[1] = vE;
	diffusionParams[2] = r;

	gsl_function F;
	F.function = &ddiffusion;
	F.params = &diffusionParams; 								//pass Ep to rootdv(), pass r from dndeeq as well, 
	gsl_set_error_handler_off();
	gsl_integration_qags (&F, E, p.mx, 0, 1e-2, 1000, 
	                    w, &result, &error); 

	gsl_integration_workspace_free (w);

	return result;

}
Exemple #9
0
double gslInt_psyn(  double E, double r){			//int over theta

		
	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	std::vector<double> psynParams (2);

	psynParams[0] = E;
	psynParams[1] = r;


	gsl_function F;
	F.function = &dpsyn;
	F.params = &psynParams;

	gsl_integration_qags (&F, 1e-16, pi, 0, 1e-3, 1000, //x?
	                    w, &result, &error); 

	gsl_integration_workspace_free (w);

	return result;

}
Exemple #10
0
double washout(double eta,double T, double M)
{
	double Y1=1.0;

	gsl_integration_workspace * w = gsl_integration_workspace_alloc (5000);
	struct washoutParams params = {T,M};
	double result,error;
	gsl_function F;
	F.function = &washout_integrand;
	F.params = &params;
	
	
	double base = washout_integrand(T,&params);
	double iT = 2;

	for(iT=2;fabs(washout_integrand(iT*T,&params)/base)>=T_TRESHOLD;iT=iT+1) 
	{
		if(DEBUG_MODE){	std::cout<<std::setprecision(9)<<"# washout integral base "<<base<<" iT: "<<iT<<" rat:  "<<fabs(washout_integrand(iT*T,&params)/base)<<std::endl;}
	}


	gsl_integration_qags(&F,0.0, iT*T, ABS, REL, 5000, w, &result, &error); 
	gsl_integration_workspace_free (w);

	return -3*Y1*Y1*M*M*eta/(8*PI*PI*PI*T)*result;

}
Exemple #11
0
double gslInt_psyn(  double E, double r){
		
	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (5000);

	double result, error;

	std::vector<double> beta (2);

	beta[0] = E;
	beta[1] = r;


	gsl_function F;
	F.function = &dpsyn;
	F.params = &beta;

	gsl_integration_qags (&F, 1e-16, pi, 0, 1e-3, 5000, //x?
	                    w, &result, &error); 

	gsl_integration_workspace_free (w);

	//std::cout << "psyn(5): " << result <<std::endl; 

	return result;

}
Exemple #12
0
int
main (void)
{
  gsl_integration_workspace * w 
    = gsl_integration_workspace_alloc (1000);
  
  double result, error;
  double expected = -4.0;
  double alpha = 1.0;

  gsl_function F;
  F.function = &f;
  F.params = &alpha;

  gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                        w, &result, &error); 

  printf ("result          = % .18f\n", result);
  printf ("exact result    = % .18f\n", expected);
  printf ("estimated error = % .18f\n", error);
  printf ("actual error    = % .18f\n", result - expected);
  printf ("intervals       = %zu\n", w->size);

  gsl_integration_workspace_free (w);

  return 0;
}
    // ========================================================================
    // adaptive integration with singularities
    // ========================================================================
    double NumericalDefiniteIntegral::QAGS ( _Function* F ) const
    {
      if( 0 == F ) { Exception("QAG::invalid function") ; }

      if ( m_a == m_b )
        {
          m_result = 0    ;
          m_error  = 0    ;   // EXACT !
          return m_result ;
        }

      // allocate workspace
      if( 0 == ws () ) { allocate () ; }

      // integration limits
      const double low  = std::min ( m_a , m_b ) ;
      const double high = std::max ( m_a , m_b ) ;

      int ierror =
        gsl_integration_qags ( F->fn                ,
                               low       , high     ,
                               m_epsabs  , m_epsrel ,
                               size   () , ws()->ws ,
                               &m_result , &m_error );

      if( ierror ) { gsl_error( "NumericalDefiniteIntegral::QAGS " ,
                                __FILE__ , __LINE__ , ierror ) ; }

      // sign
      if ( m_a > m_b ) { m_result *= -1 ; }

      return m_result ;
    }
Exemple #14
0
double rz (double red) {
  gsl_integration_workspace * w = gsl_integration_workspace_alloc(1000);
  double result, error;
  gsl_function F;
  F.function = &f;
  gsl_integration_qags(&F, 0, red, 0, 1e-7, 1000, w, &result, &error);
  gsl_integration_workspace_free (w);
  return result;
}
Exemple #15
0
void  integral(){
    gsl_function F={&f,0};
    double a=0, b=1;
    double abserr =0.0, reller =1e-3;
    int max_intervals =10;
    gsl_integration_cquad_workspace *w=gsl_integration_cquad_workspace_alloc(max_intervals);
    double result, error;
    gsl_integration_qags(&F, a, b, abserr, reller, max_intervals, w, &result, &error);
    gsl_integration_cquad_workspace_free(w);
}
Exemple #16
0
int evaluate_integral(double R, double beta, double gamma, double a_dm, double a_s, double M_dm, double M_s, double *sig_p, double *R_arcmin)
{
  
  int Nint = 1000; // Numero de intervalos
  double result, error, s, aux, X_s, I_R;
  
  gsl_integration_workspace *z = gsl_integration_workspace_alloc(Nint);
  gsl_function F1;
  
  struct param params;
  
  params.beta  =  beta;
  params.a_s   =  a_s;
  params.a_dm  =  a_dm;
  params.M_s   =  M_s;
  params.M_dm  =  M_dm;       
  params.R     =  R;
  
  F1.function = &integrando;
  F1.params = &params;
  
  gsl_integration_qags(&F1, R, LIMIT_RADIUS, 0, 1e-4, Nint, z, &result, &error); 
  
  s = R/a_s;
  
  if (s < (1.0-ZERO))
    { 
      aux = 1.0 - s*s;
      X_s = (1.0 / sqrt(aux)) * log((1.0 + sqrt(aux))/s);
      I_R = (M_s/(2.0*M_PI*a_s*a_s*gamma*aux*aux))*( (2.0+s*s)*X_s - 3.0 );
    }
  
  if(s >= (1.0-ZERO) && s <= (1.0+ZERO) )
    {
      X_s = 1.0;
      I_R = (2.0*M_s)/(15.0*M_PI*a_s*a_s*gamma);
    }
  
  if (s > (1.0 + ZERO))
    {
      X_s = (1.0 / sqrt(s*s - 1)) * acos(1.0/s);
      I_R = (M_s/(2.0*M_PI*a_s*a_s*gamma*(1.0-s*s)*(1.0-s*s)))*((2.0+s*s)*X_s - 3.0);
    }      
  
  // SE HACEN LOS CALCULOS UNA VEZ SE TIENE EL VALOR DE LA INTEGRAL
  
  *sig_p = sqrt( (G*result) / (gamma*I_R*M_PI) ) ;
  
  //CONVIERTO DE KILOPARSECS A MINUTOS DE ARCO
  *R_arcmin = R*3437.75/DISTANCE;
  
  gsl_integration_workspace_free(z);
  
  return 0;  
}
Exemple #17
0
double BesselFunction::compute_point( const Subpixel& position, IntegrationInfo& info ) const
{
    dStorm::samplepos sample;
    sample.head<2>() =
        trafo.projection().point_in_sample_space(
            dStorm::traits::Projection::SubpixelImagePosition(
                position.head<2>()));
    sample -= fluorophore;
    quantity<si::length> distance = sqrt( pow<2>(sample[0]) + pow<2>(sample[1]) );
    info.bessel_factor = double( distance * info.wavenumber);

    gsl_function func;
    func.params = &info;

    double relerr = 1E-3;
    double abserr, real_part, imag_part, value;
    func.function = &BesselFunction::theta_callback<false>;
    int status;
    do {
        status = gsl_integration_qags( &func, 0, theta_max, 0, relerr,
                            info.integration_size, info.theta, &real_part, &abserr );
        relerr *= 1.1;
    } while ( status );

    /* Optimization: If detection plane and fluorophore plane are identical,
     * just integrate the real part because the exp(z) term is always unit 1.
     * Actually, gsf_bessel_J1 might suffice here, but I'm too lazy to do the math. */
    if ( std::abs( info.z_offset ) <= 1E-3 ) {
        imag_part = 0;
    } else {
        func.function = &BesselFunction::theta_callback<true>;
        relerr = 1E-3;
        do {
            status = gsl_integration_qags( &func, 0, theta_max, 0, relerr, 
                                info.integration_size, info.theta, &imag_part, &abserr );
            relerr *= 1.1;
        } while ( status );
    }

    value = norm( std::complex<double>(real_part, imag_part) );
    return (value * pixel_size).value();
}
int Integration::integrateLU(gsl_function F, double lb, double ub) {
  gsl_set_error_handler_off();
  
  // https://www.gnu.org/software/gsl/manual/html_node/QAGI-adaptive-integration-on-infinite-intervals.html#QAGI-adaptive-integration-on-infinite-intervals
  int ret = gsl_integration_qags(&F, lb, ub, epsabs, epsrel, limit, workspace,
                                 &result, &abserr);
  if (ret) {
    fprintf(stderr, "Integration failed with a error [ %s ]\n", gsl_strerror(ret));
  }
  gsl_set_error_handler(gsl_error);
  return ret;
}
double integrate_comoving_volume(double z0, double z1)
{
	double result, error;

	gsl_function F;
	gsl_integration_workspace *w = gsl_integration_workspace_alloc(1000);
	F.function=&comoving_vol;
	F.params=&z0;

		gsl_integration_qags(&F, z0, z1, 0, 1e-5, 1000, w, &result, &error);
		gsl_integration_workspace_free (w);

	return result;
}
//----------------------------------------------------------------------------
int int_l2p(REAL alpha,REAL a,REAL b,REAL (*fp)(REAL, void*),REAL * result,REAL * error){
	gsl_integration_workspace * w 
	  = gsl_integration_workspace_alloc (1000);
	
	gsl_function F;
	F.function = fp;
	F.params = &alpha;
	
	gsl_integration_qags (&F, a, b, 0, 1e-7, 1000,w, result, error); 
	
	gsl_integration_workspace_free (w);
	
	return 0;
}
Exemple #21
0
double NormalizedPowerSpec::TopHatSigma2()
{
  gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
  double result,abserr;
  gsl_function F;
  F.function = &sigma2_int;
  F.params = this;

  /* note: 500/R is here chosen as integration boundary (infinity) */
  gsl_integration_qags (&F, 0, 500./this->R8, 0, 1e-4,1000,w,&result, &abserr);
//   printf("gsl_integration_qng in TopHatSigma2. Result %g, error: %g, intervals: %lu\n",result, abserr,w->size);
  gsl_integration_workspace_free (w);
  return result;
}
Exemple #22
0
Fichier : test.c Projet : CNMAT/gsl
double
integrate (pdf_func * pdf, double a, double b)
{
  double result, abserr;
  size_t n = 1000;
  gsl_function f;  
  gsl_integration_workspace * w = gsl_integration_workspace_alloc (n);
  f.function = &wrapper_function;
  f.params = (void *)pdf;
  pdf_errors = 0;
  gsl_integration_qags (&f, a, b, 1e-16, 1e-4, n, w, &result, &abserr);
  gsl_integration_workspace_free (w);
  if (pdf_errors) return pdf_errval;
  return result;
}
Exemple #23
0
double epsilon_denominator(double T, double M, double cutoff)
{
	
	gsl_integration_workspace * w = gsl_integration_workspace_alloc (5000);
	struct epsilonParams params = {T,M};
	double result,error;
	gsl_function F;
	F.function = &epsilon_denominator_integrand;
	F.params = &params;
	
	gsl_integration_qags(&F,cutoff,100*T,ABS, REL, 5000, w, &result, &error); 
	gsl_integration_workspace_free (w);

	return result;

}
double comoving_distance(double z0, double z1)
{
	double cd, fac, result, error;

	fac = D_H * pow(Cosmo.h,-1);
	gsl_function H;
	gsl_integration_workspace *w = gsl_integration_workspace_alloc(1000);
	H.function=&inv_H_z;
	H.params=0;

		gsl_integration_qags(&H, z0, z1, 0, 1e-3, 1000, w, &result, &error);
		cd = result*fac;
		gsl_integration_workspace_free (w);

	return cd;
}
Exemple #25
0
void Potential::computeInts(double **rhlH){

	gsl_integration_workspace * w
		= gsl_integration_workspace_alloc (WORKSIZE);

	gsl_function F,G;
	F.function = &I_internal;
	G.function = &I_external;

	double res,err;
	for (int np=0; np<NPOLY; np++){

		// array to be used by the linear interpolation scheme
		double *rhl_h = arr<double> (NR);

		for (int i=0; i<NR; i++) rhl_h[i] = rhlH[i][np];

		for (int nr=0; nr<NR; nr++){

			struct I_par par = {2*np, rhl_h};
			F.params = &par; G.params = &par;

			// Internal integral
			gsl_integration_qags (&F, 0., ar[nr], EPSABS, EPSREL, WORKSIZE, w, &res, &err);
			this->I_int[nr][np] = res;

			// External Integral
			gsl_set_error_handler_off();
			double epsabs=EPSABS,epsrel=EPSREL;
			int status=1;

			while(status!=GSL_SUCCESS){
				status = gsl_integration_qagiu (&G, ar[nr], epsabs, epsrel, WORKSIZE, w, &res, &err);
				epsabs*=2; epsrel*=2;
				if (isnan(res) == 1){
					exit(1);
				}
			}
			this->I_ext[nr][np] = res;

		}
		delArr(rhl_h);
	}

	gsl_integration_workspace_free (w);
}
//----------------------------------------------------------------------------
int int_l4p(REAL p1,REAL p2,REAL p3,REAL a,REAL b,REAL (*fp)(REAL, void*),
REAL * result,REAL * error){
        
        struct f_params_3 alpha = {p1,p2,p3};               

        gsl_integration_workspace * w 
         = gsl_integration_workspace_alloc (1000);
       
        gsl_function F;
        F.function = fp;
        F.params = &alpha;
     
        gsl_integration_qags (&F, a, b, 0, 1e-7, 1000,w, result, error); 
        gsl_integration_workspace_free (w);

	return 0;
}
Exemple #27
0
double gslInt_ssyn( double r ){				// int over r

	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;


	gsl_function F;
	F.function = &dssyn;

	gsl_integration_qags (&F, 1e-16 ,  r, 0, 1e-2, 1000,
	                w, &result, &error); 

	return result;

}
int int_l_piemd(double x,double y,double q,double b,double a,double s,double ul,double dl,double (*fp)(double, void*),
double * result,double * error){

	struct f_params_piemd p = {x,y,q,b,a,s};

	gsl_integration_workspace * w
	 = gsl_integration_workspace_alloc (1000);

	gsl_function F;
	F.function = fp;
	F.params = &p;

	gsl_integration_qags (&F, ul, dl, 0, 1e-7, 1000,w, result, error);
	gsl_integration_workspace_free (w);

	return 0;
}
Exemple #29
0
double integrate_solid_angle(double the1,double the2, double phi1, double phi2)
{
	double result, error;

	the1 *= PI/180;
	the2 *= PI/180;
	phi1 *= PI/180;
	phi2 *= PI/180;

		gsl_function F;
		gsl_integration_workspace *w = gsl_integration_workspace_alloc(1000);
		F.function=&solid_angle;
		F.params=0;

			gsl_integration_qags(&F, the1, the2, 0, 1e-4, 1000, w, &result, &error);

	return result*(phi2-phi1);
}
Exemple #30
0
//distance as a function of redshift
double Dist(){ 

	gsl_integration_workspace * w 
		= gsl_integration_workspace_alloc (1000);

	double result, error;

	gsl_function F;
	F.function = &ddist;

	gsl_integration_qags (&F, 0, c.z, 0, 1e-3, 1000,
	                w, &result, &error); 

	gsl_integration_workspace_free (w);

	return result;
 		
}