Merge ssh://repo.or.cz/srv/git/polylib
[polylib.git] / applications / ehrhart_union.c
blob1f17ae50233b15873e1847e8695ec11ec2eabcf5
1 /*
2 This file is part of PolyLib.
4 PolyLib is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 PolyLib is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with PolyLib. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
23 #include <polylib/polylib.h>
26 void Union_Read(Polyhedron **P, Polyhedron **C, const char ***param_name)
28 Matrix *pm;
29 Polyhedron *ptmp;
30 unsigned NbRows, NbColumns;
31 char s[1025], param[1025];
32 int i, j, c, f;
34 *P = NULL;
35 pm = Matrix_Read();
36 f=1;
37 while( f )
41 if( fgets(s, 1024, stdin) == 0 )
42 f=0;
44 while ( (*s=='#' || *s=='\n') && f );
46 if( f && sscanf(s, "%d %d", &NbRows, &NbColumns)==2 )
48 /* gets old pm and add it to the union */
49 if( *P )
50 if( pm->NbColumns != ((*P)->Dimension)+2 )
52 fprintf( stderr,
53 "Polyhedra must be in the same dimension space !\n");
54 exit(0);
56 ptmp = Constraints2Polyhedron(pm, 200);
57 ptmp->next = *P;
58 *P = ptmp;
59 Matrix_Free(pm);
61 /* reads the new pm */
62 pm = Matrix_Alloc(NbRows, NbColumns);
63 Matrix_Read_Input( pm );
65 else
66 break;
69 /* Context : last read pm */
70 *C = Constraints2Polyhedron(pm, 200);
71 Matrix_Free(pm);
74 if( f )
76 char **pp = (char **)malloc((*C)->Dimension*sizeof(char *));
77 *param_name = (const char **)pp;
78 /* read the parameter names */
79 c = 0;
80 for( i=0 ; i<(*C)->Dimension ; ++i )
82 j=0;
83 for( ; ; ++c )
85 if( s[c]==' ' || s[c]=='\n' || s[c]==0 ) {
86 if( j==0 )
87 continue;
88 else
89 break;
91 param[j++] = s[c];
94 /* else, no parameters (use default) */
95 if( j==0 )
96 break;
97 param[j] = 0;
98 pp[i] = (char *)malloc(j);
99 strcpy(pp[i], param);
101 if( i != (*C)->Dimension )
103 free( *param_name );
104 *param_name = Read_ParamNames(NULL,(*C)->Dimension);
107 else
108 *param_name = Read_ParamNames(NULL,(*C)->Dimension);
112 void recurse(Polyhedron *C, const char **param_name, Enumeration *e,
113 Value *pmin, Value *pmax, Value *p, int l )
115 Value z, *tmp; int k;
116 value_init( z );
117 if( l == C->Dimension )
119 fprintf(stdout,"EP( ");
120 value_print(stdout,VALUE_FMT,p[0]);
121 for(k=1;k<C->Dimension;++k) {
122 fprintf(stdout,",");
123 value_print(stdout,VALUE_FMT,p[k]);
125 fprintf(stdout," ) = ");
126 value_print(stdout,VALUE_FMT,*(tmp=compute_poly(e,p)));
127 value_clear( *tmp );
128 free(tmp);
129 fprintf(stdout,"\n");
131 else
133 for( value_assign( z, pmin[l]) ; value_le(z,pmax[l]) ; value_increment(z,z) )
135 value_assign( p[l], z );
136 recurse ( C, param_name, e, pmin, pmax, p, l+1 );
144 int main( int argc, char **argv)
146 Polyhedron *P, *C;
147 const char **param_name;
148 Enumeration *e, *en;
149 Value *pmin, *pmax, *p; int i, k; char str[256], *s;
151 if( argc != 1 )
153 fprintf( stderr, " Usage : %s [< file]\n", argv[0] );
154 return( -1 );
157 Union_Read( &P, &C, &param_name );
159 e = Domain_Enumerate( P, C, 200, param_name );
161 for( en=e ; en ; en=en->next )
163 Print_Domain(stdout,en->ValidityDomain, param_name);
164 print_evalue(stdout,&en->EP, param_name);
165 printf( "\n-----------------------------------\n" );
168 if( isatty(0) && C->Dimension != 0)
169 { /* no tty input or no polyhedron -> no evaluation. */
170 printf("Evaluation of the Ehrhart polynomial :\n");
171 pmin = (Value *)malloc(sizeof(Value) * (C->Dimension));
172 pmax = (Value *)malloc(sizeof(Value) * (C->Dimension));
173 p = (Value *)malloc(sizeof(Value) * (C->Dimension));
174 for(i=0;i<C->Dimension;i++)
176 value_init(pmin[i]);
177 value_init(pmax[i]);
178 value_init(p[i]);
180 FOREVER {
181 fflush(stdin);
182 printf("Enter %d parameters (or intervals, comma separated) : ",C->Dimension);
183 for(k=0;k<C->Dimension;++k)
185 scanf("%s",str);
186 if( (s=strpbrk(str,",")) )
187 { *s = 0;
188 value_read(pmin[k],str);
189 value_read(pmax[k],(s+1));
191 else
192 { value_read(pmin[k],str);
193 value_assign(pmax[k],pmin[k]);
197 recurse( C, param_name, e, pmin, pmax, p, 0 );
202 return( 0 );