Exemple #1
0
void evali(const char *str, char *err, int *eval)
{
	//   Mathematical Expression Evaluation Function
	//  -----------------------------------------------
	//   This functions solves mathematical equations.
	//   When a complex equation is passed via *str,
	//   the equation is broken into parts, and the
	//   simplest part is passed on recursively onto
	//   the function itself. This recurvise process is 
	//   repeated until the whole equation has been solved.
	//   Results of the evaluation are stored in int eval.

	if(!bcheck(str))
	{
		/* this is the core of eval where the calculations are done.
		at this level, the equation does not have any brackets. */

		const char symbols[]="^*/%+-&";
		const char se[]="Invalid Syntax";

		char *tmp = NULL;
		if((tmp=(char *)(malloc(sizeof(char)*
		(strlen(str)+1))))==NULL) allocerr(); *tmp='\0';
		/* check wether str has reached the absolute stage */
		if(prechar(tmp,str,symbols)==0)
		{
			printf("\n[simple]");
			if(!ncheck(str))
			{
				printf("\nequation solved!");
				*eval = strint(str);
				free(tmp);
				tmp = NULL;
				return;
			}
			else if(!id_check(str))
			{
				printf("\nit's a variable!");
				free(tmp);
				tmp = NULL;
				return;
			}
			else
			{
				strcpy(err,se);
				stradd(err,": ");
				stradd(err,str);
				free(tmp);
				tmp = NULL;
				return;
			}
		}
		else  /* there are symbols in str */
		{
			free(tmp);
			tmp = NULL;
		}

		/* now the real maths */
		printf("\n[complex]");
		char *pre = NULL; /* string preceding of operator */
		char *pos = NULL; /* string succeding the operator */
		/* now allocate the variables */
		if((pre=(char *)(malloc(sizeof(char)*
		(strlen(str)+1))))==NULL) allocerr(); *pre='\0';
		if((pos=(char *)(malloc(sizeof(char)*
		(strlen(str)+1))))==NULL) allocerr(); *pos='\0';
		char symbol = 0;
		if(prechar(pre,str,"^"))
		{ if(postchar(pos,str,"^"))
		symbol = '^'; }
		else if(prechar(pre,str,"*"))
		{ if(postchar(pos,str,"*"))
		symbol = '*'; }
		else if(prechar(pre,str,"/"))
		{ if(postchar(pos,str,"/"))
		symbol = '/'; }
		else if(prechar(pre,str,"%"))
		{ if(postchar(pos,str,"%"))
		symbol = '%'; }
		else if(prechar(pre,str,"+"))
		{ if(postchar(pos,str,"+"))
		symbol = '+'; }
		else if(prechar(pre,str,"-"))
		{ if(postchar(pos,str,"-"))
		symbol = '-'; }
		else if(prechar(pre,str,"&"))
		{ if(postchar(pos,str,"&"))
		symbol = '&'; }
		char *ax = NULL; /* value preceding of operator */
		char *bx = NULL; /* value succeding the operator */
		char *cx = NULL; /* value of ax and bx processed */
		/* now allocate the variables */
		if((ax=(char *)(malloc(sizeof(char)*
		(strlen(pre)+1))))==NULL) allocerr(); *ax='\0';
		if((bx=(char *)(malloc(sizeof(char)*
		(strlen(pos)+1))))==NULL) allocerr(); *bx='\0';
		if((cx=(char *)(malloc(sizeof(char)*
		(strlen(str)+1))))==NULL) allocerr(); *cx='\0';
		/* find out the contents of bx */
		char *ebx = NULL; /* temp string to build bx */
		if((ebx=(char *)(malloc(sizeof(char)*
		(strlen(pos)+1))))==NULL) allocerr(); *ebx='\0';
		strcpy(bx,pos);
		strcpy(ebx,bx);
		for(;;)  /* infinite loop */
		{
			if(!prechar(bx,ebx,symbols))
			{
				strcpy(bx,ebx);
				free(ebx);
				ebx = NULL;
				/* de-allocate ebx */
				break;
			}
			else /* here ebx is build */
			strcpy(ebx,bx);
		}
		/* find out the contents of ax */
		char *eax = NULL; /* temp string to build ax */
		if((eax=(char *)(malloc(sizeof(char)*
		(strlen(pre)+1))))==NULL) allocerr(); *eax='\0';
		strcpy(ax,pre);
		strcpy(eax,ax);
		for(;;)  /* infinite loop */
		{
			if(!postchar(ax,eax,symbols))
			{
				strcpy(ax,eax);
				free(eax);
				eax = NULL;
				/* de-allocate eax */
				break;
			}
			else /* here eax is build */
			strcpy(eax,ax);
		}
		/* variables to store (pre-ax) and (pre-bx) */
		char *prex = NULL;	/* string of (pre-ax) */
		char *posx = NULL;	/* string of (pos-ax) */
		/* now allocate prex and posx */
		if((prex=(char *)(malloc(sizeof(char)*
		(strlen(pre)+1))))==NULL) allocerr(); *prex='\0';
		if((posx=(char *)(malloc(sizeof(char)*
		(strlen(pos)+1))))==NULL) allocerr(); *posx='\0';
		/* find prex and posx */
		strlft(prex,pre,(strlen(pre)-strlen(ax)));
		strrht(posx,pos,(strlen(pos)-strlen(bx)));
		/* de-allocate pre & pos */
		printf("\nsym: %c",symbol);
		printf("\npre: %s",pre);
		printf("\npos: %s",pos);
		free(pre); pre = NULL;
		free(pos); pos = NULL;
		/* process ax and bx to find cx */
// *****************
		/* de-allocate ax & bx */
		printf("\n*ax: %s",ax);
		printf("\n*bx: %s",bx);
		printf("\n*cx: %s",cx);
		printf("\nprx: %s",prex);
		printf("\npox: %s",posx);
		free(ax); ax = NULL;
		free(bx); bx = NULL;
		/* variable to store one-step solved equation */
		char *ex = NULL;
		if((ex=(char *)(malloc(sizeof(char)*
		(strlen(str)+1))))==NULL) allocerr(); *ex='\0';
		/* find ex using cx in prex and posx */
// *****************
		/* now de-allocate cx, prex & posx */
		free(cx); cx = NULL;
		free(prex); cx = NULL;
		free(posx); cx = NULL;
		/* recurse ex on eval for next-step solving */
// *****************
		/* de-allocate ex & return */
		free(ex);
		ex = NULL;
		return;
	}
	else
	{
		if(!bcount(str))
		{
			printf("\nEquation has brackets.");
			return;
		}
		else
		{
			strcpy(err,"Illegal Equation, inequal number of brackets.");
			return;
		}
	}
}
/* Main program */
int main(int argc, char *argv[]){
	unsigned int g, i; 				/* Counters. Reps counter, geno counter */
	unsigned int reps;				/* Length of simulation (no. of introductions of neutral site) */
	double Bcheck = 0;				/* Frequency of B after each reproduction */
	double Acheck = 0;				/* Frequency of polymorphism */
	double Hsum = 0;				/* Summed heterozygosity over transit time of neutral allele */
	
	/* GSL random number definitions */
	const gsl_rng_type * T; 
	gsl_rng * r;
	
	/* This reads in data from command line. */
	if(argc != 8){
		fprintf(stderr,"Invalid number of input values.\n");
		exit(1);
	}
	N = strtod(argv[1],NULL);
	s = strtod(argv[2],NULL);
	rec = strtod(argv[3],NULL);
	sex = strtod(argv[4],NULL);
	self = strtod(argv[5],NULL);
	gc = strtod(argv[6],NULL);
	reps = strtod(argv[7],NULL);
	
	/* Arrays definition and memory assignment */
	double *genotype = calloc(10,sizeof(double));				/* Genotype frequencies */
	unsigned int *gensamp = calloc(10,sizeof(unsigned int));	/* New population samples */
	  
	/* create a generator chosen by the 
    environment variable GSL_RNG_TYPE */
     
	gsl_rng_env_setup();
	if (!getenv("GSL_RNG_SEED")) gsl_rng_default_seed = time(0);
	T = gsl_rng_default;
	r = gsl_rng_alloc(T);
	
	/* Initialising genotypes */
	geninit(genotype);
    
    /* Run simulation for 2000 generations to create a burn in */
    for(g = 0; g < 2000; g++){

    	/* Selection routine */
    	selection(genotype);
    	
    	/* Reproduction routine */
    	reproduction(genotype);
    	
       	/* Gene conversion routine */
       	gconv(genotype);
       	
       	/* Sampling based on new frequencies */
       	gsl_ran_multinomial(r,10,N,genotype,gensamp);
       	for(i = 0; i < 10; i++){
			*(genotype + i) = (*(gensamp + i))/(1.0*N);
       	}
       	
       	/* Printing out results (for testing) */
		/*
	   	for(i = 0; i < 10; i++){
			printf("%.10lf ", *(genotype + i));
		}
		printf("\n");
		*/
    }
    
	/* Reintroducing neutral genotype, resetting hap sum */	
	neutinit(genotype,r);
    Bcheck = ncheck(genotype);
    Hsum = Bcheck*(1-Bcheck);
    /* printf("%.10lf %.10lf\n",Bcheck,Hsum); */
    
    /* Introduce and track neutral mutations 'reps' times */
    g = 0;
    while(g < reps){

    	/* Selection routine */
    	selection(genotype);
    	
    	/* Reproduction routine */
    	reproduction(genotype);
    	
    	/* Gene conversion routine */
       	gconv(genotype);
       	
    	/* Sampling based on new frequencies */
       	gsl_ran_multinomial(r,10,N,genotype,gensamp);
       	for(i = 0; i < 10; i++){
       		*(genotype + i) = (*(gensamp + i))/(1.0*N);
       	}
       	
       	/* Checking state of haplotypes: if B fixed reset so can start fresh next time */
		Bcheck = ncheck(genotype);
		Hsum += Bcheck*(1-Bcheck);
		/* printf("%.10lf %.10lf\n",Bcheck,Hsum); */
		
		/* If polymorphism fixed then abandon simulation */
       	Acheck = pcheck(genotype);
       	if(Acheck == 0){
       		g = reps;
       	}
       	
       	if(Bcheck == 0 || Bcheck == 1){
       		printf("%.10lf\n",Hsum);
       		g++;
       		/* printf("Rep Number %d\n",g); */
       		
       		if(Bcheck == 1){
       			/* Reset genotypes so B becomes ancestral allele */
       			*(genotype + 0) = *(genotype + 7);
       			*(genotype + 1) = *(genotype + 8);
       			*(genotype + 4) = *(genotype + 9);
       			*(genotype + 7) = 0;
       			*(genotype + 8) = 0;
       			*(genotype + 9) = 0;      			
       		}
     		 
     		/* Reintroducing neutral genotype, resetting hap sum */     		
	    	neutinit(genotype,r);
			Bcheck = ncheck(genotype);
       		Hsum = Bcheck*(1-Bcheck);
       	}
    
	}	/* End of simulation */
	
	
	/* Freeing memory and wrapping up */
 	gsl_rng_free(r);
 	free(gensamp);
	free(genotype);
	/* printf("The End!\n"); */
	return 0;
}