polylib: ehrhart_union.c Source File

- Main Page
- Classes
- Files

# ehrhart_union.c

Go to the documentation of this file.

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