int calculateFullEntry(int mat[SIZE1][SIZE2],struct fullentry * bob)
{
	int n1,n2; 
	int autocor[(2*SIZE1-1)*(2*SIZE2-1)];
	//int disps[SIZE1*(2*SIZE2-1)]; 
	calculateEntry(mat,&(bob[0].intrep)); //calculate entry part of full entry
	calculateAutocor((autocor),mat,&(bob[0].ones));
	calculateSpectrum(autocor,&(bob[0].spectrum[0]),bob[0].ones,&(bob[0].peaksidelobe),2*SIZE1-1,2*SIZE2-1);
	//display info for debugging	
	if(bob[0].ones>=25)
	{
		printf("ones %d \n",bob[0].ones); 
		printf("calculating full entry \n"); 
		displaymatrix(mat,NULL); 		
		printf("calculating autocor \n"); 
		displaygeneral(autocor,NULL,2*SIZE1-1,2*SIZE2-1); 
		printf("peak sidelobe %d \n",bob[0].peaksidelobe);
	}
	/*printf("calculating displacements \n"); 
	calculateDisplacements(mat,disps); 
	displaygeneral(disps,NULL,SIZE1,2*SIZE2-1); 
	printf("calculating displacement spectrum \n"); 
	calculateSpectrum((disps),&(bob[0].dispspectrum[0]),&(bob[0].peakdisp),SIZE1,2*SIZE2-1); */
	return 0; 
}
//take input file from fortran code and convert into format which can be processed by this code, so we can check if equivalent
int convert(int mat[SIZE1][SIZE2],char *infile,char *outfile,int * numbers)
{
	printf("reading in data file \n"); 
	int swit2,count,n,val;
	struct entry bob; 
	FILE * fpin;
	FILE * fpout;  
	char text [1000]; 
	fpin=fopen(infile,"r"); 
	fpout=fopen(outfile,"w");
	(*numbers)=0; //we don't know how many results we are reading in

	fgets(&text[0],1000,fpin); //get first two lines
        fgets(&text[0],1000,fpin);  
	swit2=0; 
	while(1)
	{
		//code for locating set of values from line and extracting
		count=0; 
		while(count<SIZE1)
		{
                        //second method for simpler output format
                        fscanf(fpin,"%d",&val); 
                        if(feof(fpin)) //end of file indicator, read in all words which are part of matrices
			{	
				swit2=1; 
				break;
			} 
			//printf("%d \n",val); 
        		bob.rows[count]=val;
			//printf("%d \n",bob.rows[count]);  
			count++; 
		}	
                if(swit2==1)
		{
			break;
		}
		if(count==SIZE1)  //if there are SIZE1 components read in, print out matrix to outfile              
		{
			calculateMatrix(bob,mat); 
			//displayentry(bob,NULL); 
			displaymatrix(mat,fpout);
			(*numbers)++; 
			fgets(&text[0],1000,fpin); //go to next line
		}
	}
	//printdata(data,size1,(*numbers)); 
	printf("read data in \n"); 
	printf("numbers %d \n",(*numbers)); 
	
	fclose(fpin); 
	fclose(fpout); 
	
	return 0; 
}
Esempio n. 3
0
int main()
{
	char done;
	printf("This is the game of the Tic Tac Toe.\n ");
	printf("You will be playing against the comuter.\n");
	done= ' ';
	void initmatrix();
	do
	{
		displaymatrix();
		getplayermove();
		done=check();
		if(done!= ' ') break;
		getcomputermove();
		done=check();
	}while(done== ' ');
	if(done=='x') printf("You won!\n");
	else printf("I won!!!\n");
	displaymatrix();
	return 0;
}
int calculateDisplacements(int mat[SIZE1][SIZE2],int * disps)
{
	int n1,n2,x1,y1,count; 
	struct coordinate listones[MAXONES];
	count=0; 
	for(n1=0; n1<SIZE1; n1++)
	{
		for(n2=0; n2<SIZE2; n2++)
		{
			if(mat[n1][n2]==1)
			{
				listones[count].x=n1; 
				listones[count].y=n2; 
				count++;
			} 
		}
	} 
	displaymatrix(mat,NULL); 
	displaylist(listones,count); 
	
	for(n1=0; n1<SIZE1; n1++)
	{
		for(n2=0; n2<(2*SIZE2-1); n2++)
		{
			disps[n1*(2*SIZE1-1)+n2]=0; 
		}
	}	
	
	for(n2=0; n2<count; n2++)
	{
		for(n1=0; n1< n2; n1++)   //entry in counter-1 position is newest 1, we need to verify its displacement with the older ones is not already in the matrix
		{
			y1=listones[n2].y-listones[n1].y; 
			x1=listones[n2].x-listones[n1].x; 
			if(x1<0)  //only half of displacements are unique, we use this convention to pick half
			{
				x1=x1*-1; //just inverting lines, b/c displacements unique up to inversion
				y1=y1*-1; 
			} 
			y1=y1+SIZE2-1; 
		
			//printf("next displacement added %d %d \n",x1,y1); 
			disps[x1*(2*SIZE2-1)+y1]++;
		}
	}
	return 0; 
}
Esempio n. 5
0
void GlmTest::displayAnova(void) {
  unsigned int nVars = tm->nVars;
  unsigned int i, j;
  const char *testname[3]              // string array, only pointers stored
      = {"sqrt(WALD)", "SCORE", "LR"}; // 2, 3, 4

  displaymatrix(bootID, "bootID");

  printf("\n ========================= \n");
  printf("\nAnova Table (resampling under ");
  if (tm->resamp == CASEBOOT)
    printf("H1):\n");
  else
    printf("H0):\n");

  if (tm->corr == SHRINK)
    displayvector(tm->anova_lambda, "Est. shrink.param in anova");

  unsigned int test = tm->test - 2;
  printf("Hypo\t Alter\t dff\t %s\t  P-value \n", testname[test]);
  for (i = 0; i < nModels - 1; i++)
    printf("M%d\t M%d\t %d\t %.3f   %.3f\t\t \n", (int)i + 1, (int)i, dfDiff[i],
           gsl_matrix_get(anovaStat, i, 0), gsl_matrix_get(Panova, i, 0));

  if (tm->punit != NONE) {
    if (tm->punit == FREESTEP)
      printf("\nUnivariate Tests (FREESTEP adjusted):\n\t\t");
    else
      printf("\nUnivariate Tests:\n\t\t");
    for (i = 0; i < nModels - 1; i++)
      printf("\tM%d v. M%d\t", (unsigned int)i + 1, (unsigned int)i);
    printf("\n");

    for (j = 1; j < nVars + 1; j++) {
      printf("[Response %d]:", (unsigned int)j);
      for (i = 0; i < nModels - 1; i++)
        printf("\t%.3f (%.3f)", gsl_matrix_get(anovaStat, i, j),
               gsl_matrix_get(Panova, i, j));
      printf("\n");
    }
    printf("\n");
  }
}
int sort(char * fname, int counts, int mat[SIZE1][SIZE2])
{
	FILE * fp; 
	int n,n1,n2, inti,max,start,maxdiff;
	struct fullentry * matrices;
	int minsidelobes [MAXONES]; 
	fp=fopen(fname,"r");
	matrices=(struct fullentry *)calloc(counts,sizeof(struct fullentry)); 
	printf("Beginning to sort results \n");
	max=0;
 
	for(n=0; n<MAXONES; n++)
	{
		minsidelobes[n]=100;	
	}
	for(n=0; n<counts; n++)
	{
		for(n2=0; n2<SIZE2; n2++)
		{				
			for(n1=0; n1<SIZE1; n1++)
			{
				fscanf(fp,"%d ",&inti); 
				mat[n1][n2]=inti;
			}
			fscanf(fp,"\n ",&inti); 
		}
		lexleast(mat,1);  
		calculateFullEntry(mat,&matrices[n]);
		//displayentry(matrices[n].intrep,NULL); 
		if(matrices[n].ones>max)
			max=matrices[n].ones;
		if(minsidelobes[matrices[n].ones]>matrices[n].peaksidelobe)
			minsidelobes[matrices[n].ones]=matrices[n].peaksidelobe;   
	}
	fclose(fp);
	printf("Maximum ones reached in search is %d \n",max);
	printf("optimal sidelobe for each level \n"); 
	start=-1; 
	//
	//printf("minones %d max %d \n",MINONES,max);
	int min=100; 
	//need to be careful here, get start of range and minimum sidelobe independently
	for(n=MINONES; n<=max; n++)
	{
		//printf("%d \n",minsidelobes[n]); 
		if(minsidelobes[n]<min)
		{
			min=minsidelobes[n]; 
		}
		if(start==-1 && minsidelobes[n]!=100)
			start=n; 
		if(start!=-1)
			printf("%d ",minsidelobes[n]); 
	} 
	printf(" \n"); 
	//create matrix for storing sidelobe and length statistics, if find lower sidelobe, need to also break down by sidelobe		 
	int * counttype=(int *)calloc((max-start+1)*(MAXSIDELOBE-min+1),sizeof(int)); 
	qsort((void *)matrices,counts,sizeof(struct fullentry),comparefullentry);
	n2=0;


	//now print out only the unique entries 
	fp=fopen(fname,"w"); 
	fprintf(fp,"Matrices for maximum sidelobe %d with dimensions %d by %d \n",MAXSIDELOBE,SIZE1,SIZE2); 

        //need to print out 1st matrix in list first, code works by printing out 1st unique matrix of sorted group and ignoring the rest
	calculateMatrix(matrices[0].intrep,mat); 
	displaymatrix(mat,fp); 	
	displayentry(matrices[0].intrep,fp);
	fprintf(fp,"\n");

	
	//printf("start %d \n",start);  
	counttype[(max-start+1)*(matrices[0].peaksidelobe-min)+matrices[0].ones-start]++; 
	for(n1=0; n1<=MAXSIDELOBE; n1++)
	{
		fprintf(fp,"%d ",matrices[0].spectrum[n1]); 
	}
	fprintf(fp," \n"); 
	fprintf(fp, "ones %d, peak sidelobe %d \n \n",matrices[0].ones,matrices[0].peaksidelobe);  
	n2++;  
	maxdiff=matrices[0].ones-matrices[0].peaksidelobe;

	printf("range for table is %d ones to %d, and sidelobe %d to %d \n",max,start,MAXSIDELOBE,min); 
	printf("entering loop %d counts \n",counts); 	
 
	for(n=1; n<counts; n++)
	{
		if(compareentry(&matrices[n],&matrices[n-1])!=0) //do not print out as long as previous entry same as current entry
		{

			calculateMatrix(matrices[n].intrep,mat);
			if(matrices[n].peaksidelobe>MAXSIDELOBE)
				continue;  
			displaymatrix(mat,fp); 			
			displayentry(matrices[n].intrep,fp);
			fprintf(fp,"\n"); 
			for(n1=0; n1<=MAXSIDELOBE; n1++)
			{
				fprintf(fp,"%d ",matrices[n].spectrum[n1]); 
			}
			fprintf(fp," \n"); 
			fprintf(fp, "ones %d, peak sidelobe %d \n \n",matrices[n].ones,matrices[n].peaksidelobe);  
			//printf("%d %d \n",(max-start+1)*(matrices[n].peaksidelobe-min)+matrices[n].ones-start,(max-start+1)*(MAXSIDELOBE-min+1)); 
			//printf("%d peaksidelobe, %d ones \n",matrices[n].peaksidelobe,matrices[n].ones); 
			counttype[(max-start+1)*(matrices[n].peaksidelobe-min)+matrices[n].ones-start]++; 
			n2++; 
			if((matrices[0].ones-matrices[0].peaksidelobe)>maxdiff)
				maxdiff=(matrices[0].ones-matrices[0].peaksidelobe);
		}		
	} 
	fprintf(fp,"Total Unique matrices are %d \n",n2); 
	printf("sorted through matrices and eliminated redundant elements, have %d unique matrices out of %d \n",n2,counts); 
	fprintf(fp,"Maximum ones reached in search is %d \n",max);
	fprintf(fp,"optimal sidelobe for each level \n"); 
	for(n=start; n<=max; n++)
		fprintf(fp,"%d : %d ",n,minsidelobes[n]); 
	fprintf(fp," \n"); 
	printf("maxdiff is %d, start is %d, max is %d \n",maxdiff,start,max); 
	printf("printing out table \n"); 
	//print out table of counts to give breakdown and sidelobe and int counts for given max
	for(n1=0; n1<=(max-start); n1++)
	{
		fprintf(fp," \n");
		if(n1==0) 
			fprintf(fp,"   ");
		else
			fprintf(fp,"%d  ",n1+start);  	
		for(n2=0; n2<=(MAXSIDELOBE-min); n2++)
		{
			if(n1==0)
				fprintf(fp,"%d  ",n2+min); 
			else
				fprintf(fp,"%d  ",counttype[(max-start+1)*n2+(n1)]); 			
		}
	}
	printf("finished loop \n"); 
	//needs to also give sum of lowest diagonal corresponding to highest max
	int sum=0; 
	for(n1=0; n1<=(max-start); n1++)
	{
		//printf("%d \n",start+n1-maxdiff-minsidelobes[start]); 
		if((start+n1-maxdiff-min)>=0) //if not lined up properly can give sidelobe we did not include before
			sum=sum+counttype[(max-start+1)*(start+n1-maxdiff-min)+n1]; //need to sweep constant diffmax contour	
	}
	printf("\n \n There are %d results with optimal difference of %d \n",sum,maxdiff); 
	fprintf(fp,"\n \n There are %d results with optimal difference of %d \n",sum,maxdiff);  
	fclose(fp);
	printf("reached end \n");  
	return 0; 
}
//1st thing need to do is add new differences to displacements and confirm that these displacements are new
int addones(int mat[SIZE1][SIZE2], struct coordinate disps [MAXONES][MAXONES], struct coordinate listones[MAXONES], int counter, int displacements[SIZE1][2*SIZE2-1], FILE * fp, int * outcount )
{
	int n1, n2, minx, miny,x1,y1;
	/*if(counter>=11)
	{
		printf("\n \n"); 
		printf("level %d \n",counter);
		displaylist(listones,counter);  
		displaymatrix(mat,NULL); 
		printf("added one %d %d \n",listones[counter-1].x,listones[counter-1].y);  
	}*/

	//1st check whether or not matrix is lexographically least, criteria is if less than half of max ones in upper half
	
	//need mod offset for odd dimensions so can strictly define with bigger half of matrix, otherwise can't gaurantee anything with given class
	if(SIZE2*SIZE1/2+MOD <(listones[counter-1].y*SIZE1+listones[counter-1].x) && counter <MINONES/2 )
	{
		return 0;
	} 	
	//2nd check whether or not we can add displacements to list
	for(n1=0; n1< (counter-1); n1++)   //entry in counter-1 position is newest 1, we need to verify its displacement with the older ones is not already in the matrix
	{
		y1=listones[counter-1].y-listones[n1].y; 
		x1=listones[counter-1].x-listones[n1].x; 
		if(x1<0)  //only half of displacements are unique, we use this convention to pick half
		{
			x1=x1*-1; //just inverting lines, b/c displacements unique up to inversion
			y1=y1*-1; 
		} 
		y1=y1+SIZE2-1; 
		
		//printf("next displacement added %d %d \n",x1,y1); 
		displacements[x1][y1]++;
		disps[n1][counter-1].x=x1;  //add new displacements to displacement list 
		disps[n1][counter-1].y=y1; 
 
		if(displacements[x1][y1]>1)
		{
			mat[listones[counter-1].x][listones[counter-1].y]=0; //need to remove 1 from matrix
			for(n2=0; n2<=n1; n2++)
			{
				displacements[disps[n2][counter-1].x][disps[n2][counter-1].y]--; //remove all previously added displacements
			}
			return 0; 
		}
	}
	
	//3rd check if satisfies the minimum ones requirement, if so print out
	if(counter>=MINONES)
	{
		//if(lexleast(mat,0)==1) //only display matrix if lex least
		//{
			(*outcount)++; 	
	 		displaymatrix(mat,fp);
		//}
		//displaydisplacements(displacements,fp); 
	}  

	minx=listones[counter-1].x; 
	miny=listones[counter-1].y; 
	//iterate deeper by adding more ones 
	for(n2=miny; n2<SIZE2; n2++)
	{
		for(n1=minx; n1<SIZE1; n1++)
		{
			listones[counter].x=n1; //put one at spot in matrix
			listones[counter].y=n2;	
			if(mat[n1][n2]==0)  //only add 1 to spot if none already there, don't touch ones from other levels
			{
				mat[n1][n2]=1; 	
				addones(mat,disps,listones,counter+1,displacements,fp,outcount); 
			 	mat[n1][n2]=0; //have to conserve ones at a given level, once test with one take it away and add another place
			}
		}
		minx=0; //allow to access any column in row after restriction on row with original entry
	}	
	//needs to remove displacements when returning so it will not effect the operation of the next branch
	for(n2=0; n2< (counter-1); n2++)
	{
		displacements[disps[n2][counter-1].x][disps[n2][counter-1].y]--; //remove all previously added displacements
	}
	return 0; //shouldn't need to return whether or successful or not for code to contineu
}
//same method as before except in loop form for increased speed, minimize the number of function calls
int imperativeform(int mat[SIZE1][SIZE2], struct coordinate disps[MAXONES][MAXONES], struct coordinate listones[MAXONES], int displacements[SIZE1][2*SIZE2-1],int n1[MAXONES],int n2[MAXONES],int minx[MAXONES], FILE *fp, int *outcount,int counter,int * minsidelobes, int * maxsidelobes,struct fullentry * bob)
{
	int inc,inc2,x1,y1;
	int matp[SIZE1][SIZE2];
        //when 1st comes in counter=1 
	struct entry ref; 
	ref.rows[0]=2; 
	ref.rows[1]=17; 
	ref.rows[2]=22; 	 


	//first loop needs to be of special form because of restriction on search space, but only amounts to Size/4 function calls anyways so don't need here
	start: ;
	
	//printf("counter %d %d %d \n",counter,listones[counter].x,listones[counter].y); 

	/*printf("\n \n"); 
	printf("level %d \n",counter+1);
	displaylist(listones,counter+1);  
	displaymatrix(mat,NULL); 
	printf("added one %d %d \n",listones[counter].x,listones[counter].y);  */

	/*if((counter+1)==MINONES)
	{
		identity(mat,matp,SIZE1,SIZE2); 
		lexleast(matp,1);  
		calculateFullEntry(matp,bob); 
		//displayentry((*bob).intrep,NULL); 
		if(compareentry(&ref,&((*bob).intrep))==0)
		{
			printf("Have match! \n"); 
		}
	}*/

	//need mod offset for odd dimensions so can strictly define with bigger half of matrix, otherwise can't gaurantee anything with given class
	if(SIZE2*SIZE1/2+MOD <(listones[counter].y*SIZE1+listones[counter].x) && counter <MINONES/2 )
	{
		//will never satisfy this for counter =1, can just goto loop here
		counter=counter-1; 
		goto loop; 
	} 
	
	//printf("2nd part \n"); 
	//2nd check whether or not we can add displacements to list
	maxsidelobes[counter+1]=maxsidelobes[counter]; //start with max from previous level 
	for(inc=0; inc<(counter); inc++)   //entry in counter-1 position is newest 1, we need to verify its displacement with the older ones is not already in the matrix
	{
		y1=listones[counter].y-listones[inc].y; 
		x1=listones[counter].x-listones[inc].x; 
		if(x1<0)  //only half of displacements are unique, we use this convention to pick half
		{
			x1=x1*-1; //just inverting lines, b/c displacements unique up to inversion
			y1=y1*-1; 
		} 
		y1=y1+SIZE1-1; 
		
		//printf("next displacement added %d %d \n",x1,y1); 
		displacements[x1][y1]++;
		disps[inc][counter].x=x1;  //add new displacements to displacement list 
		disps[inc][counter].y=y1; 
 		
		if(displacements[x1][y1]>maxsidelobes[counter+1])
			maxsidelobes[counter+1]=displacements[x1][y1]; 
		if(displacements[x1][y1]>MAXSIDELOBE  ) //use sidelobe criteria and previous maxdistance to sort results
		{
			mat[listones[counter].x][listones[counter].y]=0; //need to remove 1 from matrix
			for(inc2=0; inc2<=inc; inc2++)
			{
				displacements[disps[inc2][counter].x][disps[inc2][counter].y]--; //remove all previously added displacements
			}
			counter=counter-1; //go back up level, again will never satisfy for counter=0 here
			goto loop; 
		}
	}
	
	//printf("3rd part \n"); 
	//3rd check if satisfies the minimum ones requirement, if so print out
	if( (counter+1)>=MINONES && ((counter+1-maxsidelobes[counter+1])>=MINDISTANCE) )
	{
		(*outcount)++; 	
	 	displaymatrix(mat,fp);
		//if(minsidelobes[bob[0].ones]>bob[0].peaksidelobe)
		//	minsidelobes[bob[0].ones]=bob[0].peaksidelobe; 
	}  
	minx[counter]=listones[counter].x; 
	//iterate deeper by adding more ones 
	for(n2[counter]=listones[counter].y; n2[counter]<SIZE2; n2[counter]++)
	{
		for(n1[counter]=minx[counter]; n1[counter]<SIZE1; n1[counter]++)
		{	
			if(mat[n1[counter]][n2[counter]]==0)  //only add 1 to spot if none already there, don't touch ones from other levels
			{
				//need to put one more spot in matrix, needs to put list coordinate at counter+1, because loop currently on counter interation
				listones[counter+1].x=n1[counter]; 
				listones[counter+1].y=n2[counter];
				mat[n1[counter]][n2[counter]]=1; 	
				counter=counter+1; 
				goto start;   //only can go to start from this point
 				//bad cases, or terminations will return here, decrementing counter as they come back up
				loop: ;				
			 	mat[n1[counter]][n2[counter]]=0; //have to conserve ones at a given level, once test with one take it away and add another place
			}
		}
		minx[counter]=0; //allow to access any column in row after restriction on row with original entry
	}	
	//needs to remove displacements when returning so it will not effect the operation of the next branch
	for(inc=0; inc< counter; inc++)
	{
		displacements[disps[inc][counter].x][disps[inc][counter].y]--; //remove all previously added displacements
	}
	//go back up level
	counter=counter-1; 
	if(counter!=-1)
		goto loop;  //done checking cases needs to go back to place where called from 
	else
		return 0;
}