Example #1
0
int main() {
	printf("%f\n", exponential(-1.0));
  // Note that exp(1) = e
	printf("%f\n", exponential(1.0));
	printf("%f\n", exponential(5.0));
	printf("%f\n", exponential(12.3));
  return 0;
}
Example #2
0
File: a1c.c Project: llevar/uoft
void cust(){
  /* If simulation hasn't ended yet. */
	if(clock < MAX_TIME){
		TIME t1, t2, t3, t4;

		create("cust");

		/* Customer enters queue at facility 1. */
		t1 = enter_box(queue_box);
		reserve(f1);

		/* Customer exits queue at facility 1. */
		exit_box(queue_box, t1);

		/* Customer enters service at facility 1. */
		t2 = enter_box(service_box);

		/* Service customer at facility 1. */
		hold(exponential(SVTM1));

		/* Customer finished service at facility 1. */
		exit_box(service_box, t2);

		/* Customer enters queue at facility 2. */
		t3 = enter_box(queue_box2);

		/* Request facility 2 without releasing facility 1. */	
		reserve(f2);

		/* Customer exits queue at facility 2. */
		exit_box(queue_box2, t3);

		/* Customer enters service at facility 2. */
		t4 = enter_box(service_box2);

		/* Customer has been granted facility 2 so can now release facility 1. */
		release(f1);

		/* Service customer at facility 2. */
		hold(exponential(SVTM2));

		release(f2);

		/* Customer finished service at facility 2. */
		exit_box(service_box2, t4);

		/* Check if time to terminate. */
		if(clock >= 1000){
			set(done);
		}
	/* If time to end simulation, signal the main process. */
	}else{
		set(done);
	}
}
Example #3
0
void RANDOM_PROCESS::init(double f, double l) {
    frac = f;
    lambda = l;
    last_time = 0;
    off_lambda = lambda/frac - lambda;
    if (drand() > frac) {
        value = false;
        time_left = exponential(off_lambda);
    } else {
        value = true;
        time_left = exponential(lambda);
    }
}
Example #4
0
void randist_bang(t_randist *x)
{

	long possNum = x->possNum;
	float var1 = x->varFlt1;
	float var2 = x->varFlt2;
//	post("var1 = lf1 /n var2 = %f2", var1,var2);

	
	//Select which function to execute
	switch (x->distType) {
		case 0:
			x->randVar = equalDist(x,possNum);
			break;
		case 1:	
			x->randVar = highPass(possNum);
			break;
		case 2:
			x->randVar = lowPass(possNum);
			break;
		case 3:
			x->randVar = triangle(possNum);
			break;
		case 4:
			x->randVar = gaussian(possNum, var1, var2);
			break;
		case 5:
			x->randVar = exponential(possNum, var2); 
			break; 
		default:
			post("error with distType");
			break;
	}
	outlet_float(x->rand_out, x->randVar);
}	
Example #5
0
File: g3e7g3.c Project: pingicx/cx
int main (void)
{
	printf ("A continuaciÛn, se aproximar· la funciÛn\ne^%.2f utilizando los primeros 15 tÈrminos \ndel correspondiente polinomio de Taylor\n\n",x);
	double j= exponential(x);
	printf ("e^%.2f es aproximadamente %g\n",x,j);     /*cuando se llega al ˙ltimo tÈrmino*/
	return 0;
}
int
simplify_polar(void)
{
	int n;

	n = isquarterturn(p2);

	switch(n) {
	case 0:
		break;
	case 1:
		push_integer(1);
		return 1;
	case 2:
		push_integer(-1);
		return 1;
	case 3:
		push(imaginaryunit);
		return 1;
	case 4:
		push(imaginaryunit);
		negate();
		return 1;
	}

	if (car(p2) == symbol(ADD)) {
		p3 = cdr(p2);
		while (iscons(p3)) {
			n = isquarterturn(car(p3));
			if (n)
				break;
			p3 = cdr(p3);
		}
		switch (n) {
		case 0:
			return 0;
		case 1:
			push_integer(1);
			break;
		case 2:
			push_integer(-1);
			break;
		case 3:
			push(imaginaryunit);
			break;
		case 4:
			push(imaginaryunit);
			negate();
			break;
		}
		push(p2);
		push(car(p3));
		subtract();
		exponential();
		multiply();
		return 1;
	}

	return 0;
}
Example #7
0
File: a1c.c Project: llevar/uoft
void sim(){
	
	create("sim");
	
	/* Initialize simulation. */
	init();
	
	/* For the duration of the simulation generate customers with 
	   exponential inter-arrival times with mean IATM. */
	while(simtime() < 1000){
		hold(exponential(IATM));
		cust();
		
	}
	wait(done);
	
	/* Print reports. */
	printf("Server 1, expected average delay in queue of a customer: %f\n",
	table_mean(box_time_table(queue_box)));
	printf("Server 1, expected time-average number of customers in queue: %f\n",
	qtable_mean(box_number_qtable(queue_box)));
	printf("Server 1, expected utilization: %f\n\n",
	qtable_mean(box_number_qtable(service_box)));
	
	printf("Server 2, expected average delay in queue of a customer: %f\n",
	table_mean(box_time_table(queue_box2)));
	printf("Server 2, expected time-average number of customers in queue: %f\n",
	qtable_mean(box_number_qtable(queue_box2)));
	printf("Server 2, expected utilization: %f\n\n",
	qtable_mean(box_number_qtable(service_box2)));

}
void RANDOM_PROCESS::init(double f, double l) {
    frac = f;
    lambda = l;
    last_time = 0;
    value = true;
    time_left = exponential(lambda);
    off_lambda = lambda/frac - lambda;
}
Example #9
0
int exponential(int k,int n)
{
int a;
  if(k==0)
    return 0;
  if(n==0)
    return 1;
  if(n%2==1)
  {
    a=exponential(k,n-1);
    return a*k;
  }
  else
  {
    a=exponential(k,n/2);
    return a*a;
  }
}
int reconstructPadding(char *array) {
    int i, val=0;
    for (i=0;i<4;i++) {
        if(array[i]=='1') {
            val = val + exponential(2,i);
        }
    }
    return val;
}
/*Handle arrival event*/
void arrival() {
   /*schedule next arrival*/
   time_next_event[0] = sim_time + exponential(LAMBDA);

   if (server_status == BUSY) {
      num_in_q = num_in_q + 1;
      if (num_in_q > Q_LIMIT) {
         printf("Queue overflow\n");
         exit(2);
      }
   }
   else {
      server_status = BUSY;

      /*schedule event for departure*/
      time_next_event[1] = sim_time + exponential(MU);

   }
}  
Example #12
0
void main()
{
  printf("Calculating the value of k power n !!!!!!!");
  int k,n;
  printf("\nEnter the value of k = ");
  scanf("%d",&k);
  printf("\nEnter the value of n = ");
  scanf("%d",&n);
  printf("\nThe value of k power n = %d",exponential(k,n));
}
Example #13
0
bool RANDOM_PROCESS::sample(double diff) {
    if (frac==1) return true;
    time_left -= diff;
    if (time_left < 0) {
        if (value) {
            time_left += exponential(off_lambda);
            value = false;
        } else {
            time_left += exponential(lambda);
            value = true;
        }
    }
#if 0
    msg_printf(0, MSG_INFO,
        "value: %d lambda: %f time_left %f", value, lambda, time_left
    );
#endif
    return value;
}
Example #14
0
/**
 * Returns a Poisson distributed non-negative integer.
 * NOTE: use m > 0
 */
Rand::intType Rand::poisson(floatType m){
	floatType t = 0.0;
	intType   x = 0;

	while (t < m) {
		t += exponential(1.0);
		x++;
	}
	return x - 1;
}
Example #15
0
// Definition of the poisson processes is given by Gurevich (Books/Math/Teorija sluchainyx prozessov)
// This is in fact a Poisson process with given time t
int poiss(double lambda,double t){
  // if t==1 => we obtain random variable with Poisson distribution!
  double Sn = 0.0;
  int n = 0;
  while(Sn<t){
    Sn += exponential(lambda);
    n++;
  }
  n--;
  return n;  
}
Example #16
0
int main( int argc, char** argv ){
	srand( time( 0 ) );
	
	int number_of_VNs;
	float arrival_rate;
	int average_lifetime;
	int schedule_window;
	int min_windows_wait;
	int max_windows_wait;
	
	sscanf( argv[1], "%d", &number_of_VNs );
	sscanf( argv[2], "%f", &arrival_rate );
	sscanf( argv[3], "%d", &average_lifetime );
	sscanf( argv[4], "%d", &schedule_window );
	sscanf( argv[5], "%d", &min_windows_wait );
	sscanf( argv[6], "%d", &max_windows_wait );
	
	FILE *out;
	
	out = fopen( "events/events.evt", "w" );
	if( !out ){
		printf( "Error in file generate_events.cpp, function main: Could not open file events/events.evt\n" );
		exit(-1);
	}
	
	int clock = 0;
	int last_event_time = 0;
	
	//this code generates the arrive departure and expire network events
	for( int i = 0; i< number_of_VNs; ++i ){
		int arrival_time = poisson( arrival_rate );
		int life_time = exponential( average_lifetime );
		
		clock = clock + arrival_time;
		
		int expire_time = ( clock-( clock % schedule_window )) + schedule_window * uniform( min_windows_wait, max_windows_wait );
		
		int departure_time = clock + life_time;
		
		last_event_time = ( last_event_time > expire_time ) ? last_event_time : clock;
		
		fprintf( out, "%d A %d\n", i, clock );
		fprintf( out, "%d D %d\n", i, departure_time );
		fprintf( out, "%d E %d\n", i, expire_time );
	}
	//this code generates the schedule events
	last_event_time = last_event_time + schedule_window;
	for( int i = schedule_window; i < last_event_time; i += schedule_window )
		fprintf( out,  "-1 S %d\n", i );
	
	fclose( out);
}
Example #17
0
int main(int argc, char* argv[]) {
  if (argc > 1) { // If there is a test argument

  }
  else { // Normal mode
    std::cout << "Testing exponential... ";
    if (exponential(2, 1) != 2) {
      print_fail();
      return 0;
    }
    if (exponential(3, 4) != 81) {
      print_fail();
      return 0;
    }
    if (exponential(6, 3) != 216) {
      print_fail();
      return 0;
    }
    print_pass();
  }
  return 0;
}
/*Handle departure*/
void departure() {
   
   if (num_in_q == 0) { /*none in queue!*/
      server_status = IDLE;
      time_next_event[1] = -1.0; /*no further departure*/
   }
   else {
      num_in_q = num_in_q - 1; /*fetch a customer for the queue*/

      /*schedule next departure*/
      time_next_event[1] = sim_time + exponential(MU); 
   }

}
Example #19
0
list_events* Generate_calls(SQM_instance &Inst,double lambda) {
  int m;
  int events;
  double current_time,etime;
  double demand,lambda_j;
  list_events *calls;
  list_events::iterator it;
  call *incoming_call;

  logDebug(cout << "Start Generate_calls" << endl);
  calls = new list_events;
  m = Inst.demand_points();
  demand = Inst.total_demand();
  for (int j = 0;j < m;j++) {
    lambda_j = lambda * Inst.get_demand(j) / demand;
    current_time = exponential(lambda_j);
    it = calls->begin();
    events = 0;
    while (current_time < Simulation_Time) {
      /* Create Call */
      logDebug(cout << "Create event" << endl);
      incoming_call = new call(current_time,j);
      /* Insert Call */
      logDebug(cout << "Insert event with lambda_j = " << lambda_j << endl);
      while (it != calls->end() && current_time > (*it)->get_time()) it++;
      calls->insert(it,incoming_call);
      /* Determine time of next Call */
      etime = exponential(lambda_j);
      current_time += etime;
      events++;
      logDebug(cout << "Determine time of next event " << current_time << endl);
    }
    logDebug(cout << "Create " << events << " for demand point " << j << endl);
  }
  
  return calls;
}
Example #20
0
//Create 20 random src-dst pairs 
void select()
{
  int srce, dstn, i=0;
  while(i < 20)
  {
    srce = rand() % N;
    dstn = rand() % N;
    if ((dstn == srce)||(node[srce].src && (node[srce].dst == dstn)))
    	continue;
    node[srce].dst = dstn;
    node[srce].src = true;
    node[srce].create_time = exponential(0.5);
    i++;
  }
 }
Example #21
0
void
polar(void)
{
	save();
	p1 = pop();
	push(p1);
	mag();
	push(imaginaryunit);
	push(p1);
	arg();
	multiply();
	exponential();
	multiply();
	restore();
}
Example #22
0
void
yymag(void)
{
	save();
	p1 = pop();
	if (isnegativenumber(p1)) {
		push(p1);
		negate();
	} else if (car(p1) == symbol(POWER) && equaln(cadr(p1), -1))
		// -1 to a power
		push_integer(1);
	else if (car(p1) == symbol(POWER) && cadr(p1) == symbol(E)) {
		// exponential
		push(caddr(p1));
		real();
		exponential();
	} else if (car(p1) == symbol(MULTIPLY)) {
		// product
		push_integer(1);
		p1 = cdr(p1);
		while (iscons(p1)) {
			push(car(p1));
			mag();
			multiply();
			p1 = cdr(p1);
		}
	} else if (car(p1) == symbol(ADD)) {
		// sum
		push(p1);
		rect(); // convert polar terms, if any
		p1 = pop();
		push(p1);
		real();
		push_integer(2);
		power();
		push(p1);
		imag();
		push_integer(2);
		power();
		add();
		push_rational(1, 2);
		power();
		simplify_trig();
	} else
		// default (all real)
		push(p1);
	restore();
}
Example #23
0
static PyObject *
d2func_exp(PyObject *self, PyObject *args) {
    /* Target function for the two parameter exponential for calculating and returning the chi-squared Hessian.
     * 
     */

    /* Declarations. */
    PyObject *params_arg;
    PyObject *list, *list2;
    int j, k;

    /* Parse the function arguments, the only argument should be the parameter array. */
    if (!PyArg_ParseTuple(args, "O", &params_arg))
        return NULL;

    /* Convert the parameters Python list to a C array. */
    param_to_c(params_arg);

    /* Back calculated the peak intensities. */
    exponential(params[index_I0], params[index_R], relax_times, back_calc, num_times);

    /* The partial derivatives. */
    exponential_dR(params[index_I0], params[index_R], index_R, relax_times, back_calc_grad, num_times);
    exponential_dI0(params[index_I0], params[index_R], index_I0, relax_times, back_calc_grad, num_times);

    /* The second partial derivatives. */
    exponential_dR2(params[index_I0], params[index_R], index_R, relax_times, back_calc_hess, num_times);
    exponential_dI02(params[index_I0], params[index_R], index_I0, relax_times, back_calc_hess, num_times);
    exponential_dR_dI0(params[index_I0], params[index_R], index_R, index_I0, relax_times, back_calc_hess, num_times);

    /* The chi-squared Hessian. */
    d2chi2(d2chi2_vals, values, back_calc, back_calc_grad, back_calc_hess, variance, num_times, num_params);

    /* Convert to a Python list, and scale the values. */
    list = PyList_New(0);
    Py_INCREF(list);
    for (j = 0; j < num_params; j++) {
        list2 = PyList_New(0);
        Py_INCREF(list2);
        for (k = 0; k < num_params; k++) {
            PyList_Append(list2, PyFloat_FromDouble(d2chi2_vals[j][k] * scaling_matrix[j] * scaling_matrix[k]));
        }
        PyList_Append(list, list2);
    }

    /* Return the Hessian. */
    return list;
}
Example #24
0
// This function actually applies the specified noise to the given image
// in the given amounts
IplImage* GenerateNoise(IplImage* img, int noiseType, float amount=255)
{
	CvSize imgSize = cvGetSize(img);
	IplImage* imgTemp = cvCloneImage(img);	// This will hold the noisy image

	// Go through each pixel
	for(int y=0;y<imgSize.height;y++)
	{
		for(int x=0;x<imgSize.width;x++)
		{
			int randomValue=0;				// Our noise is additive.. this holds
			switch(noiseType)				// the amount to add/subtract
			{
			case NOISE_UNIFORM:				// I chose UNIFORM, so give me a uniform random number
				randomValue = (char)(uniform()*amount);
				break;

			case NOISE_EXPONENTIAL:			// I chose EXPONENTIAL... so exp random number please
				randomValue = (int)(exponential()*amount);
				break;

			case NOISE_GAUSSIAN:			// same here
				randomValue = (int)(gaussian()*amount);
				break;

			case NOISE_RAYLEIGH:			// ... guess!!
				randomValue = (int)(rayleigh()*amount);
				break;

			case NOISE_GAMMA:				// I chose gamma... give me a gamma random number
				randomValue = (int)(gamma()*amount);
				break;

			case NOISE_IMPULSE:				// I need salt and pepper.. pass the shakers please
				randomValue = (int)(impulse((float)amount/256)*amount);
			}
			
			// Here we "apply" the noise to the current pixel
			int pixelValue = cvGetReal2D(imgTemp, y, x)+randomValue;

			// And set this value in our noisy image
			cvSetReal2D(imgTemp, y, x, pixelValue);
		}
	}

	// return
	return imgTemp;
}
Example #25
0
// Definition of the poisson processes is given by Gurevich (Books/Math/Teorija sluchainyx prozessov)
// This is in fact a complete Poisson trajectory of length maxT and time steps dt
void poiss(double lambda,double maxT,double dt,vector< pair<double,int> >& out){
  if(out.size()>0){ out.clear(); }

  double Sn = 0.0;  int n = 0;  double t = 0.0;

  while(t<maxT){
    while(Sn<t){
      Sn += exponential(lambda);
      n++;
    }
    n--;
    std::pair<double,int> pr(t,n);
    out.push_back(pr);
    t += dt;
  }
}
Example #26
0
laundry init_laundry(laundry l){
    int i;
    double t;
    event e;
    int n = l->n;
    
    for(i=0; i<n; i++){
        /*Se generan los `n` tiempos iniciales
        de falla y se los agrega a la lista de eventos
        */
        t = exponential(l->rg, 1./(l->tfail));     
        e = create_event(t, MACHINE_BROKEN);
        l->events_list = insert_event(l->events_list, e);
    }
    return l;
}
//=============================================================================
//==  Function to generate Poisson customers                                 ==
//==   - Random split to queue1() and queue2()                               ==
//=============================================================================
void generate(double lambda, double mu)
{
  double   interarrival_time;    // Interarrival time to next send
  double   service_time;         // Service time for this customer
  int      select_queue = 1;         // if 1 go to queue1

  create("generate");

  // Loop forever to create customers
  while(1)
  {
    // Pull an interarrival time and hold for it
    interarrival_time = exponential(1.0 / lambda);
    hold(interarrival_time);

    // Pull a service time
    //service_time = exponential(1.0 / mu);
    service_time = mu;

    // Send the customer to a randomly selected queue
    if (select_queue == 1)
    {
      queue1(service_time, clock);
      select_queue++;
    }
    else if (select_queue == 2)
    {
      queue2(service_time, clock);
      select_queue++;
    }
    else if (select_queue == 3)
    {
      queue3(service_time, clock);
      select_queue++;
    }
    else if (select_queue == 4)
    {
      queue4(service_time, clock);
      select_queue++;
    }
    else
    {
      queue5(service_time, clock);
      select_queue = 1;
    }
  }
}
/*Initialize the simulation with empty queue*/
void initialize(void) {
  srand(time(NULL));  /*Not to be done in serious simulations*/
  
  sim_time = 0.0;

  /*model state*/
  server_status = IDLE;
  num_in_q = 0;

  /*statistics*/
  time_last_event = 0.0;
  area_num_in_q = 0.0;

  /*Event list*/
  time_next_event[0] = sim_time + exponential(LAMBDA);
  time_next_event[1] = -1.0; /*negative denotes no event scheduled*/
}
Example #29
0
// This function actually applies the specified noise to the given
// in the given amounts
IplImage* GenerateNoise(IplImage* img, int noiseType, float amount=255)
{
    CvSize imgSize = cvGetSize(img);
    IplImage* imgTemp = cvCloneImage(img); // This will hold the n

    // Go through each pixel
    for(int y=0; y<imgSize.height; y++)
    {
        for(int x=0; x<imgSize.width; x++)
        {
            int randomValue=0; // our noise is additivwe
            switch(noiseType) // the amount to add/substract
            {
            case NOISE_UNIFORM:
                randomValue = (char)(uniform()*amount);
                break;

            case NOISE_EXPONENTIAL:
                randomValue = (int)(exponential()*amount);
                break;

            case NOISE_GAUSSIAN:
                randomValue = (int)(gaussian()*amount);
                break;

            case NOISE_RAYLEIGH:
                randomValue = (int)(rayleigh()*amount);
                break;

            case NOISE_GAMMA:
                break;

            case NOISE_IMPULSE:
                randomValue = (int)(impulse((float)amount/256)*amount);
            }

            int pixelValue = cvGetReal2D(imgTemp, y, x)+randomValue;

            // And set this value in our noisy image
            cvSetReal2D(imgTemp, y, x, pixelValue);
        }
    }

    // return
    return imgTemp;
}
Example #30
0
	void calculate(LinkedList<int> *destination, LinkedList<int> *source1, LinkedList<int> *source2, string op){
		destination -> clear();
		switch(op[0]){
			case '+': 
				addition(destination, source1, source2);
				break;
			case '-' :
				subtraction(destination, source1, source2);
				break;
			case '*' :
				multiply(destination, source1, source2);
				break;
			case '^':
				exponential(destination, source1, source2); 
				break;
		}
	}