Files GPLv3 headers removed
[polylib.git] / applications / ehrhart_union.c
blobd3c908866661cb6d67ed19ba513584ceb0514cf9
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
6 #include <polylib/polylib.h>
9 void Union_Read(Polyhedron **P, Polyhedron **C, const char ***param_name)
11 Matrix *pm;
12 Polyhedron *ptmp;
13 unsigned NbRows, NbColumns;
14 char s[1025], param[1025];
15 int i, j, c, f;
17 *P = NULL;
18 pm = Matrix_Read();
19 f=1;
20 while( f )
24 if( fgets(s, 1024, stdin) == 0 )
25 f=0;
27 while ( (*s=='#' || *s=='\n') && f );
29 if( f && sscanf(s, "%d %d", &NbRows, &NbColumns)==2 )
31 /* gets old pm and add it to the union */
32 if( *P )
33 if( pm->NbColumns != ((*P)->Dimension)+2 )
35 fprintf( stderr,
36 "Polyhedra must be in the same dimension space !\n");
37 exit(0);
39 ptmp = Constraints2Polyhedron(pm, 200);
40 ptmp->next = *P;
41 *P = ptmp;
42 Matrix_Free(pm);
44 /* reads the new pm */
45 pm = Matrix_Alloc(NbRows, NbColumns);
46 Matrix_Read_Input( pm );
48 else
49 break;
52 /* Context : last read pm */
53 *C = Constraints2Polyhedron(pm, 200);
54 Matrix_Free(pm);
57 if( f )
59 char **pp = (char **)malloc((*C)->Dimension*sizeof(char *));
60 *param_name = (const char **)pp;
61 /* read the parameter names */
62 c = 0;
63 for( i=0 ; i<(*C)->Dimension ; ++i )
65 j=0;
66 for( ; ; ++c )
68 if( s[c]==' ' || s[c]=='\n' || s[c]==0 ) {
69 if( j==0 )
70 continue;
71 else
72 break;
74 param[j++] = s[c];
77 /* else, no parameters (use default) */
78 if( j==0 )
79 break;
80 param[j] = 0;
81 pp[i] = (char *)malloc(j);
82 strcpy(pp[i], param);
84 if( i != (*C)->Dimension )
86 free( *param_name );
87 *param_name = Read_ParamNames(NULL,(*C)->Dimension);
90 else
91 *param_name = Read_ParamNames(NULL,(*C)->Dimension);
95 void recurse(Polyhedron *C, const char **param_name, Enumeration *e,
96 Value *pmin, Value *pmax, Value *p, int l )
98 Value z, *tmp; int k;
99 value_init( z );
100 if( l == C->Dimension )
102 fprintf(stdout,"EP( ");
103 value_print(stdout,VALUE_FMT,p[0]);
104 for(k=1;k<C->Dimension;++k) {
105 fprintf(stdout,",");
106 value_print(stdout,VALUE_FMT,p[k]);
108 fprintf(stdout," ) = ");
109 value_print(stdout,VALUE_FMT,*(tmp=compute_poly(e,p)));
110 value_clear( *tmp );
111 free(tmp);
112 fprintf(stdout,"\n");
114 else
116 for( value_assign( z, pmin[l]) ; value_le(z,pmax[l]) ; value_increment(z,z) )
118 value_assign( p[l], z );
119 recurse ( C, param_name, e, pmin, pmax, p, l+1 );
127 int main( int argc, char **argv)
129 Polyhedron *P, *C;
130 const char **param_name;
131 Enumeration *e, *en;
132 Value *pmin, *pmax, *p; int i, k; char str[256], *s;
134 if( argc != 1 )
136 fprintf( stderr, " Usage : %s [< file]\n", argv[0] );
137 return( -1 );
140 Union_Read( &P, &C, &param_name );
142 e = Domain_Enumerate( P, C, 200, param_name );
144 for( en=e ; en ; en=en->next )
146 Print_Domain(stdout,en->ValidityDomain, param_name);
147 print_evalue(stdout,&en->EP, param_name);
148 printf( "\n-----------------------------------\n" );
151 if( isatty(0) && C->Dimension != 0)
152 { /* no tty input or no polyhedron -> no evaluation. */
153 printf("Evaluation of the Ehrhart polynomial :\n");
154 pmin = (Value *)malloc(sizeof(Value) * (C->Dimension));
155 pmax = (Value *)malloc(sizeof(Value) * (C->Dimension));
156 p = (Value *)malloc(sizeof(Value) * (C->Dimension));
157 for(i=0;i<C->Dimension;i++)
159 value_init(pmin[i]);
160 value_init(pmax[i]);
161 value_init(p[i]);
163 FOREVER {
164 fflush(stdin);
165 printf("Enter %d parameters (or intervals, comma separated) : ",C->Dimension);
166 for(k=0;k<C->Dimension;++k)
168 scanf("%s",str);
169 if( (s=strpbrk(str,",")) )
170 { *s = 0;
171 value_read(pmin[k],str);
172 value_read(pmax[k],(s+1));
174 else
175 { value_read(pmin[k],str);
176 value_assign(pmax[k],pmin[k]);
180 recurse( C, param_name, e, pmin, pmax, p, 0 );
185 return( 0 );