Main Page | Class List | File List | Class Members | File Members

ehrhart_union.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 
00005 #include <polylib/polylib.h>
00006 
00007 
00008 void Union_Read( Polyhedron **P, Polyhedron **C, char ***param_name )
00009 {
00010         Matrix *pm;
00011         Polyhedron *ptmp;
00012         unsigned NbRows, NbColumns;
00013         char s[1025], param[1025];
00014         int i, j, c, f;
00015 
00016         *P = NULL;
00017         pm = Matrix_Read();
00018         f=1;
00019         while( f )
00020         {
00021                 do
00022                 {
00023                         if( fgets(s, 1024, stdin) == 0  )
00024                                 f=0;
00025                 }
00026                 while ( (*s=='#' || *s=='\n') && f );
00027 
00028                 if( f && sscanf(s, "%d %d", &NbRows, &NbColumns)==2 )
00029                 {
00030                         /* gets old pm and add it to the union */
00031                         if( *P )
00032                                 if( pm->NbColumns != ((*P)->Dimension)+2 )
00033                                 {
00034                                         fprintf( stderr,
00035                                                 "Polyhedra must be in the same dimension space !\n");
00036                                         exit(0);
00037                                 }
00038                         ptmp = Constraints2Polyhedron(pm, 200);
00039                         ptmp->next = *P;
00040                         *P = ptmp;
00041                         Matrix_Free(pm);
00042 
00043                         /* reads the new pm */
00044                         pm = Matrix_Alloc(NbRows, NbColumns);
00045                         Matrix_Read_Input( pm );
00046                 }
00047                 else
00048                         break;
00049         }
00050 
00051         /* Context : last read pm */
00052         *C = Constraints2Polyhedron(pm, 200);
00053         Matrix_Free(pm);
00054 
00055 
00056         if( f )
00057         {
00058                 /* read the parameter names */
00059                 *param_name = (char **)malloc( (*C)->Dimension*sizeof(char *) );
00060                 c = 0;
00061                 for( i=0 ; i<(*C)->Dimension ; ++i )
00062                 {
00063                         j=0;
00064                         for( ; ; ++c )
00065                         {
00066                                 if( s[c]==' ' || s[c]=='\n' || s[c]==0 ) {
00067                                         if( j==0 )
00068                                                 continue;
00069                                         else
00070                                                 break;
00071                                 }
00072                                 param[j++] = s[c];
00073                         }
00074 
00075                         /* else, no parameters (use default) */
00076                         if( j==0 )
00077                                 break;
00078                         param[j] = 0;
00079                         (*param_name)[i] = (char *)malloc( j );
00080                         strcpy( (*param_name)[i], param );
00081                 }
00082                 if( i != (*C)->Dimension )
00083                 {
00084                         free( *param_name );
00085                         *param_name = Read_ParamNames(NULL,(*C)->Dimension);
00086                 }
00087         }
00088         else
00089                 *param_name = Read_ParamNames(NULL,(*C)->Dimension);
00090 
00091 }
00092 
00093 void recurse (  Polyhedron *C, char **param_name, Enumeration *e, 
00094                   Value *pmin, Value *pmax, Value *p, int l )
00095 {
00096         Value z, *tmp; int k;
00097         value_init( z );
00098         if( l == C->Dimension )
00099         {
00100                 fprintf(stdout,"EP( ");
00101                 value_print(stdout,VALUE_FMT,p[0]);
00102                 for(k=1;k<C->Dimension;++k) {
00103                   fprintf(stdout,",");
00104                   value_print(stdout,VALUE_FMT,p[k]);
00105                 }  
00106                 fprintf(stdout," ) = ");
00107                 value_print(stdout,VALUE_FMT,*(tmp=compute_poly(e,p)));
00108                 value_clear( *tmp );
00109                 free(tmp);
00110                 fprintf(stdout,"\n");  
00111         }
00112         else
00113         {
00114                 for( value_assign( z, pmin[l]) ; value_le(z,pmax[l]) ; value_increment(z,z) )
00115                 {
00116                         value_assign( p[l], z );
00117                         recurse (       C, param_name, e, pmin, pmax, p, l+1 );
00118                 }
00119         }
00120 
00121 }
00122 
00123 
00124 
00125 int main( int argc, char **argv)
00126 {
00127         Polyhedron *P, *C;
00128         char **param_name;
00129         Enumeration *e, *en;
00130         Value *pmin, *pmax, *p; int i, k; char str[256], *s;
00131 
00132         if( argc != 1 )
00133         {
00134                 fprintf( stderr, " Usage : %s [< file]\n", argv[0] );
00135                 return( -1 );
00136         }
00137 
00138         Union_Read( &P, &C, &param_name );
00139 
00140         e = Domain_Enumerate( P, C, 200, param_name );
00141 
00142         for( en=e ; en ; en=en->next )
00143         {
00144           Print_Domain(stdout,en->ValidityDomain, param_name);
00145           print_evalue(stdout,&en->EP, param_name);
00146           printf( "\n-----------------------------------\n" );
00147         }
00148 
00149     if( isatty(0) && C->Dimension != 0)
00150         {  /* no tty input or no polyhedron -> no evaluation. */
00151             printf("Evaluation of the Ehrhart polynomial :\n");
00152             pmin = (Value *)malloc(sizeof(Value) * (C->Dimension));
00153             pmax = (Value *)malloc(sizeof(Value) * (C->Dimension));
00154             p = (Value *)malloc(sizeof(Value) * (C->Dimension));
00155             for(i=0;i<C->Dimension;i++) 
00156             {
00157                value_init(pmin[i]);
00158                value_init(pmax[i]);
00159                value_init(p[i]);
00160             }
00161             FOREVER {
00162                 fflush(stdin);
00163                 printf("Enter %d parameters (or intervals, comma separated) : ",C->Dimension);
00164                 for(k=0;k<C->Dimension;++k)
00165                 {
00166                     scanf("%s",str);
00167                     if( (s=strpbrk(str,",")) )
00168                     {  *s = 0;
00169                        value_read(pmin[k],str);
00170                        value_read(pmax[k],(s+1));
00171                     }
00172                     else
00173                     {  value_read(pmin[k],str);
00174                        value_assign(pmax[k],pmin[k]);
00175                     }
00176                 }
00177 
00178                 recurse( C, param_name, e, pmin, pmax, p, 0 );
00179 
00180             }
00181         }
00182 
00183         return( 0 );
00184 }
00185 

Generated on Mon Apr 23 19:23:52 2007 for polylib by doxygen 1.3.5