polylib: ehrhart_union.c Source File

- Main Page
- Classes
- Files

- File List
- File Members

# ehrhart_union.c

Go to the documentation of this file.

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