/************************************************************************* main.c Author: Gianfranco Ciardo Updates: 15 April 1997 (released) *************************************************************************/ #include #include #include "port.h" #include "utility.h" #include "matrix.h" #define ERROR_CODE 1 #define DFL_STATES 10 #define DFL_PERCOL 6 /* MUST BE <= DFL_STATES !!! */ #define DFL_RANGEV 1000 /************************************************************************* Adds an entry in position (i,j) with value v for matrix A, assuming that no entries have been added for columns higher than j, and that A->pointers[j] is the position where the value is to be placed. Mostly a shorthand... *************************************************************************/ void AddEntry(sparse *A, int i, int j, float v) { A->indices[A->pointers[j+1]] = i; A->values[A->pointers[j+1]] = v; A->pointers[j+1]++; } /************************************************************************* Fills matrix A with a random ergodic matrix (ergodicity is ensured by making 0 a home state: every state goes to it, and it goes to every state). *************************************************************************/ void fill(sparse *A) { int i,j,k,l,duplicate; /* allocate the space for a DFL_STATESx DFL_STATES matrix with DFL_PERCOL nonzero entries per column, except for column 0 */ A->n = DFL_STATES; A->pointers = (int *)NewMem(sizeof(int)*(A->n+1)); A->indices = (int *)NewMem(sizeof(int)*(A->n-1+(A->n-1)*DFL_PERCOL)); A->values = (float *)NewMem(sizeof(float)*(A->n-1+(A->n-1)*DFL_PERCOL)); /* fill with random values, except that column and row 0 are full */ srand(1); A->pointers[0] = 0; for (i = 1; i < A->n; i++) { AddEntry(A,i,0,(float)(rand() % DFL_RANGEV + 1)); } for (j = 1; j < A->n; j++) { A->pointers[j+1] = A->pointers[j]; AddEntry(A,0,j,(float)(rand() % DFL_RANGEV + 1)); for (k = 1; k < DFL_PERCOL;) { /* generate a potential "from" state for the "to" state j */ i = rand() % A->n; /* check that i != j */ if (i == j) { continue; } /* check that there isn't already an entry (i,j) in A */ for (duplicate = 0, l = 0; l < k; l++) { if (A->indices[A->pointers[j] + l] == i) { duplicate = 1; break; } } /* if there isn't already an entry (i,j) in A, add it */ if (duplicate == 0) { AddEntry(A,i,j,(float)(rand() % DFL_RANGEV + 1)); k++; } } } } /************************************************************************* Prints a matrix A. *************************************************************************/ void print(sparse *A) { int j,k; for (j = 0; j < A->n; j++) { printf("\nTo %3d from:\n",j); for (k = A->pointers[j]; k < A->pointers[j+1]; k++) { printf("%3d:%4.0f ",A->indices[k],A->values[k]); } } printf("\n"); } void main(int argc, char **argv) { sparse A; if (argc != 1) { fprintf(stderr,"usage: %s ",argv[0]); fprintf(stderr,"(no parameters required)\n"); exit(ERROR_CODE); } fprintf(stderr,"Filling the matrix\n"); fill(&A); fprintf(stderr,"Printing the matrix\n"); print(&A); fprintf(stderr,"Done\n"); exit(0); }