Ejemplo n.º 1
0
void
gsl_ran_poisson_test()
{
	int i, j;
	unsigned int k1 = gsl_ran_poisson(RAND_GSL, 5);
	unsigned int k2 = gsl_ran_poisson(RAND_GSL, 5);

	printf("k1 = %u , k2 = %u\n", k1, k2);

	//number of experiments
	const int nrolls = 10000;

	//maximum number of stars to distribute
	const int nstars = 100;

	int p[10] = {};
	for (i = 0; i < nrolls; ++i) {
		unsigned int number = gsl_ran_poisson(RAND_GSL, 4.1);

		if (number < 10)
			++p[number];
	}

	printf("poisson_distribution (mean=4.1):\n");

	for (i = 0; i < 10; ++i) {
		printf("%d, : ", i);
		for (j = 0; j < p[i] * nstars / nrolls; j++)
			printf("*");
		printf("\n");
	}
}
Ejemplo n.º 2
0
void korak_zajlis(int *Z, int *L, gsl_rng* r)
{
    int z = *Z;
    int l = *L;
    int dz = gsl_ran_poisson(r, 5*beta*z*dt) - gsl_ran_poisson(r, 4*beta*z*dt + beta*z*l/50.0*dt);
    int dl = gsl_ran_poisson(r, 4*beta*l*dt + beta*z*l/200.0*dt) - gsl_ran_poisson(r, 5*beta*l*dt);
    *Z += dz;
    *L += dl;
}
void reproduce(int timestep) {
  int i,j;
  
  if(noise < 2) {
    update_mean_fit();
  }else if(noise == 2) {
    current_mean_fitness += epsilon*wavespeed;
  }
  
  if((current_mean_fitness > shiftthreshold*dx) || (current_mean_fitness < -shiftthreshold*dx)) {
    shift_population(timestep);
  }

  tmpw[0] = 0;
  tmpw[space-1] = 0;
  
  tmpv[0] = 0;
  tmpv[space-1] = 0;

  for(i=1;i<space-1;i++) {
    tmpw[i] = ww[i]*(1.+(x[i]-current_mean_fitness)*epsilon);
    tmpv[i] = vv[i]*(1.+(x[i]-current_mean_fitness)*epsilon);
    tmpw[i] += mutation_2dx*(ww[i-1]-2.*ww[i]+ww[i+1]);
    tmpv[i] += mutation_2dx*(vv[i-1]-2.*vv[i]+vv[i+1]);
    if(tmpw[i]<0) {
      tmpw[i] = 0;
    }else if(noise > 0) {
      if(tmpw[i] < 1e9) { // Poisson-RNG breaks down for parameters > 1e9. see GSL doc.
			 // use smaller bins if this occurs too often or population size too large
            ww[i] = tmpw[i];
            tmpw[i] += twoepssqrt*(gsl_ran_poisson(rg,ww[i])-ww[i]);
      }
    }
    
    if(tmpv[i]<0) {
      tmpv[i] = 0;
    }else if(noise > 0) {
      if(tmpv[i] < 1e9) { // Poisson-RNG breaks down for parameters > 1e9. see GSL doc.
			 // use smaller bins if this occurs too often or population size too large
            vv[i] = tmpv[i];
            tmpv[i] += twoepssqrt*(gsl_ran_poisson(rg,vv[i])-vv[i]);
      }
    }
    
  }
  
  
  memcpy(ww,tmpw,space*sizeof(double));
  memcpy(vv,tmpv,space*sizeof(double));
}
Ejemplo n.º 4
0
Image * generate_from_poisson(Image * in,gsl_rng * r){
  Image * out = sp_image_alloc(sp_image_x(in),sp_image_y(in),sp_image_z(in));
  for(int i = 0;i<sp_image_size(in);i++){
    sp_real(out->image->data[i]) = gsl_ran_poisson(r,sp_real(in->image->data[i]));
  }
  return out;
}
Ejemplo n.º 5
0
int
main (void)
{
  const gsl_rng_type * T;
  gsl_rng * r;

  int i, n = 10;
  double mu = 3.0;

  /* create a generator chosen by the 
     environment variable GSL_RNG_TYPE */

  gsl_rng_env_setup();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);

  /* print n random variates chosen from 
     the poisson distribution with mean 
     parameter mu */

  for (i = 0; i < n; i++) 
    {
      unsigned int k = gsl_ran_poisson (r, mu);
      printf (" %u", k);
    }

  printf ("\n");
  gsl_rng_free (r);
  return 0;
}
Ejemplo n.º 6
0
int getNstars2create(struct CELL *cell, struct RUNPARAMS *param, REAL dt, REAL aexp, int level, REAL mstar){
// ----------------------------------------------------------//
/// Compute the number of stars to create in a given cell\n
/// And do a random draw in a Poisson law
// ----------------------------------------------------------//

  REAL dv=pow(0.5,3*level);
  REAL SFR=getSFR(cell,param,aexp,level);
  // Average number of stars created
	REAL lambda =  SFR  / mstar * dt * dv;

#ifdef GSLRAND
	unsigned int N = gsl_ran_poisson (param->stars->rpoiss, (double)lambda);
#else
	unsigned int N = gpoiss(lambda); //Poisson drawing
#endif // GSLRAND

	if(N){
	  //printf("tstar=%e lambda=%e\n",t0/(3600.*24.*365.*1e9),lambda);
	  //printf("AVG star creation =%e /eff %d  SFR=%e\n",lambda,N,SFR);
	}

	REAL M_in_cell = cell->field.d * POW(2.0,-3.0*level); // mass of the curent cell in code unit
	if(N * mstar >= M_in_cell) N = 0.9*M_in_cell / mstar ; // 0.9 to prevent void cells

#ifdef CONTINUOUSSTARS
  N=1;
#endif // CONTINUOUSSTARS

  return N;
}
Ejemplo n.º 7
0
vector< vector<double> > get_rand_fitScape(int N, int L, double mu, double s, gsl_rng* rng) {
    //Decide how many non-dark matter alleles are sorting through the population. 
    unsigned int polymx = gsl_ran_poisson(rng,N*L*mu); 
    if (polymx > N*L) {polymx =N*L;} 
    cout << polymx << "\n";
    //Decide placement of founding polymorphisms.
    pair<int,int>* polymx_loc = new pair<int,int>[polymx];
    pair<int,int>* genome_loc = new pair<int,int>[N*L];
    for (int i=0; i<N; i++) {
        for (int j=0; j<L; j++) {
            genome_loc[i*L+j]= pair<int,int> (i,j);
        }
    } 
    gsl_ran_choose(rng, polymx_loc, polymx, genome_loc, N*L, sizeof(pair<int,int>));
    delete [] genome_loc;
    //Decide on selective strength
    vector<double> polymx_s (polymx);
    for (int i=0; i<polymx; i++) {polymx_s[i] = gsl_ran_gaussian(rng,s);}
    //Build fitness landscape
    vector< vector<double> > fitScape_pop (N);
    for (int i=0; i<N; i++) {fitScape_pop[i] = vector<double> (L);}
    for (int i=0; i<polymx; i++) {fitScape_pop[polymx_loc[i].first][polymx_loc[i].second] = polymx_s[i];}
    delete [] polymx_loc;
    return fitScape_pop;
}
Ejemplo n.º 8
0
void pplfunc_frandomp  (ppl_context *c, pplObj *in, int nArgs, int *status, int *errType, char *errText)
 {
  char *FunctionDescription = "poisson(n)";
  if (rndgen==NULL) { rndgen = gsl_rng_alloc(gsl_rng_default); gsl_rng_set(rndgen, 0); }
  OUTPUT.real = gsl_ran_poisson(rndgen, in[0].real);
  CHECK_OUTPUT_OKAY;
 }
Ejemplo n.º 9
0
unsigned int
gsl_ran_negative_binomial (const gsl_rng * r, double p, double n)
{
  double X = gsl_ran_gamma (r, n, 1.0) ;
  unsigned int k = gsl_ran_poisson (r, X*(1-p)/p) ;
  return k ;
}
Ejemplo n.º 10
0
//Generates random number of randomly distributed according to dN/dE for parameters in cube, for detector det (0 or 1, corresponding to order in sampling.par), recoil energies [keV] are stored in MCdata
int generateUnbinnedData(WIMPpars *W, detector *det, int b, int simSeed)
{

    double signal = intWIMPrate( det->ErL, det->ErU, W, det) * det->exposure;   
    double background= b*intBgRate(*det, det->ErL, det->ErU) * det->exposure; 
     
    const gsl_rng_type * T;
    gsl_rng * r;
    gsl_rng_env_setup();
    T=gsl_rng_default;
    r=gsl_rng_alloc(T);
    gsl_rng_set(r, simSeed + SEED++);
    
    det->nEvents = gsl_ran_poisson(r,signal+background);
    
    det->unbinnedData = new double[(int)det->nEvents];
    
    double norm = diffWIMPrate( det->ErL, W, det) + b*diffBgRate(*det,det->ErL);
    int i = 0;
    double Er,y;
    while ( i < det->nEvents)
    {
        Er = gsl_rng_uniform(r)*(det->ErU-det->ErL) + det->ErL;
        y = gsl_rng_uniform(r);
        if( (diffWIMPrate( det->ErL, W, det) + b*diffBgRate(*det,det->ErL))/norm  > y )
        {
            det->unbinnedData[i] = Er;
            i++;
        }
    }
    
    gsl_rng_free(r);
    return 0;
}
Ejemplo n.º 11
0
static int WARN_UNUSED
mutgen_generate_record_mutations(mutgen_t *self, coalescence_record_t *cr)
{
    int ret = -1;
    size_t k, l, branch_mutations;
    double branch_length, position, mu;
    double distance = cr->right - cr->left;
    uint32_t child;

    self->times[cr->node] = cr->time;
    for (k = 0; k < cr->num_children; k++) {
        child = cr->children[k];
        branch_length = cr->time - self->times[child];
        mu = branch_length * distance * self->mutation_rate;
        branch_mutations = gsl_ran_poisson(self->rng, mu);
        for (l = 0; l < branch_mutations; l++) {
            position = gsl_ran_flat(self->rng, cr->left, cr->right);
            assert(cr->left <= position && position < cr->right);
            ret = mutgen_add_mutation(self, child, position);
            if (ret != 0) {
                goto out;
            }
        }
    }
    ret = 0;
out:
    return ret;
}
Ejemplo n.º 12
0
double apop_rng_GHgB3(gsl_rng * r, double* a){
    Apop_assert_nan((a[0]>0) && (a[1] > 0) && (a[2] > 0), "apop_GHgB3_rng took a zero parameter; bad.");
double		aa	= gsl_ran_gamma(r, a[0], 1),
		b	= gsl_ran_gamma(r, a[1], 1),
		c	= gsl_ran_gamma(r, a[2], 1);
int		p	= gsl_ran_poisson(r, aa*b/c);
	return p;
}
Ejemplo n.º 13
0
void librdist_poisson(gsl_rng *rng, int argc, void *argv, int bufc, float *buf){
	t_atom *av = (t_atom *)argv;
	if(argc != librdist_getnargs(ps_poisson)){
		return;
	}
	const double mu = librdist_atom_getfloat(av);
	int i;
	for(i = 0; i < bufc; i++)
	       	buf[i] = (float)gsl_ran_poisson(rng, mu);
}
Ejemplo n.º 14
0
void
gsl_ran_poisson_array (const gsl_rng * r, size_t n, unsigned int array[],
                       double mu)
{
  size_t i;

  for (i = 0; i < n; i++)
    {
      array[i] = gsl_ran_poisson (r, mu);
    }

  return;
}
Ejemplo n.º 15
0
int main(int argc, char *argv[])
{

const gsl_rng_type * T;
gsl_rng * r;
int i;
int n = atoi(argv[2]);
double mu = atof(argv[3]);	

gsl_rng_env_setup();

T = gsl_rng_default;
r = gsl_rng_alloc (T);


	//select the poisson distribution
	if (strcmp(argv[1], "-poisson") == 0)  
        	{
		for (i = 0; i < n; i++)
			{
			unsigned int k = gsl_ran_poisson(r,mu);
			printf(" %u",k);
			}	
		}
	//select the exponential distribution
	if (strcmp(argv[1], "-exponential") == 0) 
        	{
		for (i = 0; i < n; i++)
			{
			unsigned int k = gsl_ran_exponential(r,mu);
			printf(" %u",k);
			}
		}
	//select the gaussian distribution
	if (strcmp(argv[1], "-gaussian") == 0)  
        	{
		for (i = 0; i < n; i++)
			{
			unsigned int k = gsl_ran_gaussian(r,mu);
			printf(" %u",k);
			}
		}


	
printf("\n");

gsl_rng_free(r);
return 0;
}
Ejemplo n.º 16
0
void studyPop::addMutationToImmunityGenes(int whichLocus) {
    int avgNumberOfHostMutations = 2*POPULATION*U_HOST_IMMUNITY;
    int numOfHostMutations;

    // GSL objects to generate random numbers
    const gsl_rng_type * T;
    gsl_rng * r;
    gsl_rng_default_seed += time(NULL);
    T = gsl_rng_default;
    r = gsl_rng_alloc (T);
    gsl_rng_set(r, gsl_rng_default_seed);

    numOfHostMutations = gsl_ran_poisson(r,avgNumberOfHostMutations);

    // Find the alleles in the population to be mutated
    vector<int> indexOfMutations;
    for(int i=0; i<numOfHostMutations ;) {
        gsl_rng_default_seed += time(NULL);
        gsl_rng_set(r, gsl_rng_default_seed);
        int tempIndex = floor(gsl_ran_flat(r, -0.5, (POPULATION-1)+0.4999) + 0.5);
        int q=0;
        for(; q<indexOfMutations.size() ; q++) {
            if(indexOfMutations.at(q) == tempIndex)
                break;
        }
        if(q==indexOfMutations.size()) {
            indexOfMutations.push_back(tempIndex);
            i++;
        }
    }

    // Now we apply mutations to indiv with indices found above
    for(int i=0; i<indexOfMutations.size() ; i++) {
        gsl_rng_default_seed += time(NULL);
        gsl_rng_set(r, gsl_rng_default_seed);
        int whichAllele = floor(gsl_ran_flat(r, -0.5, 1.4999) + 0.5);
        if(whichLocus == 1) {
            thePop.at(indexOfMutations.at(i)).immunityGeneA[(int) whichAllele] += 1;
            thePop.at(indexOfMutations.at(i)).immunityGeneA[(int) whichAllele] %= 2;
        }
        else if(whichLocus == 2) {
            thePop.at(indexOfMutations.at(i)).immunityGeneB[(int) whichAllele] += 1;
            thePop.at(indexOfMutations.at(i)).immunityGeneB[(int) whichAllele] %= 2;
        }
    }
    gsl_rng_default_seed += time(NULL);

    // free up the space taken by the random number generator
    gsl_rng_free(r);
}
Ejemplo n.º 17
0
static inline void workloop(char* name)
{
	unsigned int i, loops;

#ifdef HAVE_LIBGSL
	loops = gsl_ran_poisson(r, (double)WORKLOOP_ITERS);
#else
	loops = WORKLOOP_ITERS;
#endif
	for (i = 0; i < loops; i++) {
		dummy *= 2.7;
	}

	printf("Node %s: Completed Workloop.\n", name);
}
Ejemplo n.º 18
0
int gslalg_rng_poisson_VM ( Word* args, Word& result,
                         int message, Word& local, Supplier s )
{
  result = qp->ResultStorage( s );
  CcInt *res = ((CcInt*) result.addr);
  long resD = 0;
  CcReal *cA = (CcReal*) args[0].addr;
  if( cA->IsDefined() )
  {
    double a = cA->GetRealval();
    resD = gsl_ran_poisson(the_gsl_randomgenerator.GetGenerator(), a);
    res->Set(true, resD);
  }
  else
    res->Set(false, 0);
  return (0);
}
Ejemplo n.º 19
0
int main(int argc, char *argv[])
{
  int i, k, po; gsl_rng *r;
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &k);
  r = gsl_rng_alloc(gsl_rng_sprng20);

  for (i = 0; i < 10; ++i)
  {
    po = gsl_ran_poisson(r, 2.0);
    printf("Process %d, random number %d: %d\n", k, i+1, po);
  }

  MPI_Finalize();

  return EXIT_SUCCESS;
}
Ejemplo n.º 20
0
void Colony::reproduction(const gsl_rng *rand){
        int k,j,new_pop_size;
        double pop_fitness;
        assign_fitness();

        pop_fitness = 0.;
        for(j=0;j<pop_size;j++)
            {
            pop_fitness += fitness[j];
            }
        pop_fitness = pop_fitness*max_fitness;
        new_pop_size = gsl_ran_poisson(rand,pop_fitness);
//	printf("fitness: %lf\tnew pop size: %i\n", pop_fitness, new_pop_size);

        if (new_pop_size==0)
            {
             printf("extinct\n");
            exit(0);
            }
        if (new_pop_size>limit)
            {
            printf("established\n");
            exit(0);
            }

        gsl_ran_multinomial(rand, pop_size, new_pop_size, fitness, off_numbers);

        next_generation(rand);
        pop_size = new_pop_size;
        mutate_population(rand);

        for(j=pop_size;j<limit;j++)
            {
            for(k=0; k<n_del; k++)
                {
                del_matrix[j][k] = 0;
                }
            for(k=0; k<n_env; k++)
                {
                env_matrix[j][k] = 0;
                }
            }
};
Ejemplo n.º 21
0
        std::vector<double>
        operator()(const gsl_rng * r) const
        {
            unsigned nbreaks
                = (recrate > 0) ? gsl_ran_poisson(r, recrate) : 0u;
            if (!nbreaks)
                return {};

            std::vector<double> pos;
            pos.reserve(nbreaks + 1);
            for (unsigned i = 0; i < nbreaks; ++i)
                {
                    pos.emplace_back(gsl_ran_flat(r, minpos, maxpos));
                }
            std::sort(pos.begin(), pos.end());
            // Note: this is required for all vectors of breakpoints!
            pos.emplace_back(std::numeric_limits<double>::max());
            return pos;
        }
Ejemplo n.º 22
0
// Main starts from here
int main(int argc, char* argv[]){
  
  if (argc==1){
    help();
    exit(0);
  }
  if (argc != 7){
    fprintf(stderr,"Wrong amount of parameters!\n");
    exit(1);
  }
  int size_x = atoi(argv[1]); // Are size in centimeters.
  int size_y = atoi(argv[2]);
  double meters_x = size_x/100; //PPP's density is given in meters.
  double meters_y = size_y/100;
  int camera_x = atoi(argv[3]); //Camera's position from x=0 in cm.
  int good_x = atoi(argv[4]); // Observable item's position from x=0 in cm.
  double density = atof(argv[5]); // "lambda", the density given for PPP.
  unsigned rounds = atoi(argv[6]); // # of rounds this program is run.

  const gsl_rng_type* T; //Random number generator type
  T = gsl_rng_default;
  r = gsl_rng_alloc(T); //Now r is a pointer to random generator
  
  unsigned hits = 0; // How many times a blocker actually blocs the LOS.

  for (unsigned i=0; i<rounds; ++i){
    gsl_rng_set(r, random_seed()); //setting random generator seed to random
    //Get a number of blockers:
    unsigned blockerCount = gsl_ran_poisson(r, density*meters_x*meters_y); 
    //printf("Result of PPP: %u\n",blockerCount);
    Blocker blockers[blockerCount];
    generateBlockers(blockers, blockerCount, size_x, size_y, r);

    if (checkIntersection(camera_x, good_x, blockers, blockerCount, 
			  size_x, size_y)) { ++hits; }
  }
  double probs = (double)hits/rounds;
  printf("Hits: %u\n",hits);
  printf("Rounds: %u\n",rounds);
  printf("Propability: %f\n",probs);
  gsl_rng_free(r); // Free allocated memory by r
}
Ejemplo n.º 23
0
/* Function replicating a genome */
struct pathogen * replicate(struct pathogen *in, struct param *par){
	int i, nbmut=gsl_ran_poisson(par->rng, par->muL);
	struct pathogen *out = (struct pathogen *) malloc(sizeof(struct pathogen));
	if(out == NULL){
		fprintf(stderr, "\n[in: pathogen.c->reconstruct_genome]\nNo memory left to reconstruct pathogen genome. Exiting.\n");
		exit(1);
	}

	/* FILL IN OUTPUT CONTENT */
	out->age = 0;
	out->ances = in;

	/* allocate memory for new vector */
	out->snps = create_vec_int(nbmut);

	/* add new mutations */
	for(i=0;i<nbmut;i++){
		out->snps->values[i] = make_mutation(par);
	}


	return out;
} /*end replicate*/
Ejemplo n.º 24
0
double regular_jump(double dt, params *p, gsl_rng *rg)
{
  double ret = 0.0;

  if (p->Dp != 0.0) {
    double mu = (p->lambda)*dt;
    double ampmean = sqrt((p->lambda)/(p->Dp));
    double comp = sqrt((p->Dp)*(p->lambda))*dt;
    unsigned int n = gsl_ran_poisson(rg, mu);
    
    double s = 0.0;
    int i;
    for (i = 0; i < n; ++i) 
      s -= log(gsl_ran_flat(rg,0,1))/ampmean;
    
    if (p->biased) 
      s -= comp;
    
    ret = s;
  } 

  return ret;
}
Ejemplo n.º 25
0
unsigned int Poissonrnd(double mu)
{
#if EXTLIB_EXIST(GSL)
  gsl_rng_init();
  return gsl_ran_poisson(Random_Generator, mu);
#else
  /*
  TZ_ERROR(ERROR_NA_FUNC);
  return 0.0;
*/

  double L = exp(-mu);
  unsigned int k = 0;
  double p = 1;

  do {
    k++;
    p *= Unifrnd();
  } while (p > L);

  return k - 1;
#endif  
}
void reproduce(int timestep) {
  int i,j;
  double tmpn;
  
  
  if(noise == 2) {
    current_mean_fitness += epsilon*(wavespeed + dv);
  }else{
    update_mean_fit();
  }
  
  if((current_mean_fitness/dx >= shiftthreshold) || (current_mean_fitness/dx <= -shiftthreshold)) {
    shift_population(timestep);
  }
      
  tmp[0] = 0;
  tmp[space-1] = 0;

  for(i=1;i<space-1;i++) {
    tmp[i] = nn[i]*(1.+(x[i]-current_mean_fitness)*epsilon);
    tmp[i] += mutation_diff2dx*(nn[i-1]-2.*nn[i]+nn[i+1]);
    if(tmp[i]<0) {
      tmp[i] = 0;
    }else if(noise > 0) {
      if(tmp[i] < 1e9) { // Poisson-RNG breaks down for parameters > 1e9. see GSL doc.
			 // use smaller bins if this occurs too often or population size too large
			 // don't add noise if occupancy too large, it's anyway almost deterministic then
	nn[i] = tmp[i];
	tmp[i] += twoepssqrt*(gsl_ran_poisson(rg,nn[i])-nn[i]);
      }
    }
  }
  
  memcpy(nn,tmp,space*sizeof(double));
  
}
Ejemplo n.º 27
0
///gsl_ran_poisson Should return the number of events that occured in a timestep with mu lamda
double PoissonSource::getTimeUntilNextEvent ()
{
	//New method:
	uint timesteps = gsl_ran_poisson(rng_r,mlamda); //<-This is wrong
	return timesteps*h; //The return value is the time until next event occurs
}
Ejemplo n.º 28
0
int
main (int argc, char *argv[])
{
  size_t i,j;
  size_t n = 0;
  double mu = 0, nu = 0, nu1 = 0, nu2 = 0, sigma = 0, a = 0, b = 0, c = 0;
  double zeta = 0, sigmax = 0, sigmay = 0, rho = 0;
  double p = 0;
  double x = 0, y =0, z=0  ;
  unsigned int N = 0, t = 0, n1 = 0, n2 = 0 ;
  unsigned long int seed = 0 ;
  const char * name ;
  gsl_rng * r ;

  if (argc < 4) 
    {
      printf (
"Usage: gsl-randist seed n DIST param1 param2 ...\n"
"Generates n samples from the distribution DIST with parameters param1,\n"
"param2, etc. Valid distributions are,\n"
"\n"
"  beta\n"
"  binomial\n"
"  bivariate-gaussian\n"
"  cauchy\n"
"  chisq\n"
"  dir-2d\n"
"  dir-3d\n"
"  dir-nd\n"
"  erlang\n"
"  exponential\n"
"  exppow\n"
"  fdist\n"
"  flat\n"
"  gamma\n"
"  gaussian-tail\n"
"  gaussian\n"
"  geometric\n"
"  gumbel1\n"
"  gumbel2\n"
"  hypergeometric\n"
"  laplace\n"
"  landau\n"
"  levy\n"
"  levy-skew\n"
"  logarithmic\n"
"  logistic\n"
"  lognormal\n"
"  negative-binomial\n"
"  pareto\n"
"  pascal\n"
"  poisson\n"
"  rayleigh-tail\n"
"  rayleigh\n"
"  tdist\n"
"  ugaussian-tail\n"
"  ugaussian\n"
"  weibull\n") ;
      exit (0);
    }

  argv++ ; seed = atol (argv[0]); argc-- ;
  argv++ ; n = atol (argv[0]); argc-- ;
  argv++ ; name = argv[0] ; argc-- ; argc-- ;

  gsl_rng_env_setup() ;

  if (gsl_rng_default_seed != 0) {
    fprintf(stderr, 
            "overriding GSL_RNG_SEED with command line value, seed = %ld\n", 
            seed) ;
  }
  
  gsl_rng_default_seed = seed ;

  r = gsl_rng_alloc(gsl_rng_default) ;


#define NAME(x) !strcmp(name,(x))
#define OUTPUT(x) for (i = 0; i < n; i++) { printf("%g\n", (x)) ; }
#define OUTPUT1(a,x) for(i = 0; i < n; i++) { a ; printf("%g\n", x) ; }
#define OUTPUT2(a,x,y) for(i = 0; i < n; i++) { a ; printf("%g %g\n", x, y) ; }
#define OUTPUT3(a,x,y,z) for(i = 0; i < n; i++) { a ; printf("%g %g %g\n", x, y, z) ; }
#define INT_OUTPUT(x) for (i = 0; i < n; i++) { printf("%d\n", (x)) ; }
#define ARGS(x,y) if (argc != x) error(y) ;
#define DBL_ARG(x) if (argc) { x=atof((++argv)[0]);argc--;} else {error( #x);};
#define INT_ARG(x) if (argc) { x=atoi((++argv)[0]);argc--;} else {error( #x);};

  if (NAME("bernoulli"))
    {
      ARGS(1, "p = probability of success");
      DBL_ARG(p)
      INT_OUTPUT(gsl_ran_bernoulli (r, p));
    }
  else if (NAME("beta"))
    {
      ARGS(2, "a,b = shape parameters");
      DBL_ARG(a)
      DBL_ARG(b)
      OUTPUT(gsl_ran_beta (r, a, b));
    }
  else if (NAME("binomial"))
    {
      ARGS(2, "p = probability, N = number of trials");
      DBL_ARG(p)
      INT_ARG(N)
      INT_OUTPUT(gsl_ran_binomial (r, p, N));
    }
  else if (NAME("cauchy"))
    {
      ARGS(1, "a = scale parameter");
      DBL_ARG(a)
      OUTPUT(gsl_ran_cauchy (r, a));
    }
  else if (NAME("chisq"))
    {
      ARGS(1, "nu = degrees of freedom");
      DBL_ARG(nu)
      OUTPUT(gsl_ran_chisq (r, nu));
    }
  else if (NAME("erlang"))
    {
      ARGS(2, "a = scale parameter, b = order");
      DBL_ARG(a)
      DBL_ARG(b)
      OUTPUT(gsl_ran_erlang (r, a, b));
    }
  else if (NAME("exponential"))
    {
      ARGS(1, "mu = mean value");
      DBL_ARG(mu) ;
      OUTPUT(gsl_ran_exponential (r, mu));
    }
  else if (NAME("exppow"))
    {
      ARGS(2, "a = scale parameter, b = power (1=exponential, 2=gaussian)");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_exppow (r, a, b));
    }
  else if (NAME("fdist"))
    {
      ARGS(2, "nu1, nu2 = degrees of freedom parameters");
      DBL_ARG(nu1) ;
      DBL_ARG(nu2) ;
      OUTPUT(gsl_ran_fdist (r, nu1, nu2));
    }
  else if (NAME("flat"))
    {
      ARGS(2, "a = lower limit, b = upper limit");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_flat (r, a, b));
    }
  else if (NAME("gamma"))
    {
      ARGS(2, "a = order, b = scale");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_gamma (r, a, b));
    }
  else if (NAME("gaussian"))
    {
      ARGS(1, "sigma = standard deviation");
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_gaussian (r, sigma));
    }
  else if (NAME("gaussian-tail"))
    {
      ARGS(2, "a = lower limit, sigma = standard deviation");
      DBL_ARG(a) ;
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_gaussian_tail (r, a, sigma));
    }
  else if (NAME("ugaussian"))
    {
      ARGS(0, "unit gaussian, no parameters required");
      OUTPUT(gsl_ran_ugaussian (r));
    }
  else if (NAME("ugaussian-tail"))
    {
      ARGS(1, "a = lower limit");
      DBL_ARG(a) ;
      OUTPUT(gsl_ran_ugaussian_tail (r, a));
    }
  else if (NAME("bivariate-gaussian"))
    {
      ARGS(3, "sigmax = x std.dev., sigmay = y std.dev., rho = correlation");
      DBL_ARG(sigmax) ;
      DBL_ARG(sigmay) ;
      DBL_ARG(rho) ;
      OUTPUT2(gsl_ran_bivariate_gaussian (r, sigmax, sigmay, rho, &x, &y), 
              x, y);
    }
  else if (NAME("dir-2d"))
    {
      OUTPUT2(gsl_ran_dir_2d (r, &x, &y), x, y);
    }
  else if (NAME("dir-3d"))
    {
      OUTPUT3(gsl_ran_dir_3d (r, &x, &y, &z), x, y, z);
    }
  else if (NAME("dir-nd"))
    {
      double *xarr;  
      ARGS(1, "n1 = number of dimensions of hypersphere"); 
      INT_ARG(n1) ;
      xarr = (double *)malloc(n1*sizeof(double));

      for(i = 0; i < n; i++) { 
        gsl_ran_dir_nd (r, n1, xarr) ; 
        for (j = 0; j < n1; j++) { 
          if (j) putchar(' '); 
          printf("%g", xarr[j]) ; 
        } 
        putchar('\n'); 
      } ;

      free(xarr);
    }  
  else if (NAME("geometric"))
    {
      ARGS(1, "p = bernoulli trial probability of success");
      DBL_ARG(p) ;
      INT_OUTPUT(gsl_ran_geometric (r, p));
    }
  else if (NAME("gumbel1"))
    {
      ARGS(2, "a = order, b = scale parameter");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_gumbel1 (r, a, b));
    }
  else if (NAME("gumbel2"))
    {
      ARGS(2, "a = order, b = scale parameter");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_gumbel2 (r, a, b));
    }
  else if (NAME("hypergeometric"))
    {
      ARGS(3, "n1 = tagged population, n2 = untagged population, t = number of trials");
      INT_ARG(n1) ;
      INT_ARG(n2) ;
      INT_ARG(t) ;
      INT_OUTPUT(gsl_ran_hypergeometric (r, n1, n2, t));
    }
  else if (NAME("laplace"))
    {
      ARGS(1, "a = scale parameter");
      DBL_ARG(a) ;
      OUTPUT(gsl_ran_laplace (r, a));
    }
  else if (NAME("landau"))
    {
      ARGS(0, "no arguments required");
      OUTPUT(gsl_ran_landau (r));
    }
  else if (NAME("levy"))
    {
      ARGS(2, "c = scale, a = power (1=cauchy, 2=gaussian)");
      DBL_ARG(c) ;
      DBL_ARG(a) ;
      OUTPUT(gsl_ran_levy (r, c, a));
    }
  else if (NAME("levy-skew"))
    {
      ARGS(3, "c = scale, a = power (1=cauchy, 2=gaussian), b = skew");
      DBL_ARG(c) ;
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_levy_skew (r, c, a, b));
    }
  else if (NAME("logarithmic"))
    {
      ARGS(1, "p = probability");
      DBL_ARG(p) ;
      INT_OUTPUT(gsl_ran_logarithmic (r, p));
    }
  else if (NAME("logistic"))
    {
      ARGS(1, "a = scale parameter");
      DBL_ARG(a) ;
      OUTPUT(gsl_ran_logistic (r, a));
    }
  else if (NAME("lognormal"))
    {
      ARGS(2, "zeta = location parameter, sigma = scale parameter");
      DBL_ARG(zeta) ;
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_lognormal (r, zeta, sigma));
    }
  else if (NAME("negative-binomial"))
    {
      ARGS(2, "p = probability, a = order");
      DBL_ARG(p) ;
      DBL_ARG(a) ;
      INT_OUTPUT(gsl_ran_negative_binomial (r, p, a));
    }
  else if (NAME("pareto"))
    {
      ARGS(2, "a = power, b = scale parameter");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_pareto (r, a, b));
    }
  else if (NAME("pascal"))
    {
      ARGS(2, "p = probability, n = order (integer)");
      DBL_ARG(p) ;
      INT_ARG(N) ;
      INT_OUTPUT(gsl_ran_pascal (r, p, N));
    }
  else if (NAME("poisson"))
    {
      ARGS(1, "mu = scale parameter");
      DBL_ARG(mu) ;
      INT_OUTPUT(gsl_ran_poisson (r, mu));
    }
  else if (NAME("rayleigh"))
    {
      ARGS(1, "sigma = scale parameter");
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_rayleigh (r, sigma));
    }
  else if (NAME("rayleigh-tail"))
    {
      ARGS(2, "a = lower limit, sigma = scale parameter");
      DBL_ARG(a) ;
      DBL_ARG(sigma) ;
      OUTPUT(gsl_ran_rayleigh_tail (r, a, sigma));
    }
  else if (NAME("tdist"))
    {
      ARGS(1, "nu = degrees of freedom");
      DBL_ARG(nu) ;
      OUTPUT(gsl_ran_tdist (r, nu));
    }
  else if (NAME("weibull"))
    {
      ARGS(2, "a = scale parameter, b = exponent");
      DBL_ARG(a) ;
      DBL_ARG(b) ;
      OUTPUT(gsl_ran_weibull (r, a, b));
    }
  else
    {
      fprintf(stderr,"Error: unrecognized distribution: %s\n", name) ;
    }

  return 0 ;
}
Ejemplo n.º 29
0
int GSLRNG_poisson(stEval *args, stEval *result, void *i) {
    gsl_rng *r = STPOINTER(&args[0]);
    double mu = STDOUBLE(&args[1]);
    STINT(result) = gsl_ran_poisson(r,mu);
    return EC_OK;
}
Ejemplo n.º 30
0
unsigned int GslRandGen::poisson(const double & mu)
{
  return gsl_ran_poisson(r, mu);
}