fixed a small bug in eval ehrhart
[polylib.git] / applications / Zpolytest.c
blobd7e831833038c6f97a24b09cf6f62cafe9d1bc7d
1 /* zpolytest.c
2 This is a testbench for the Zpolylib (part of polylib manipulating
3 Z-polyhedra. */
5 #include <stdio.h>
6 #include <polylib/polylib.h>
8 char s[128];
10 int main() {
12 Matrix *a=NULL, *b, *c, *d, *e, *g;
13 LatticeUnion *l1,*l2,*l3,*l4,*temp;
14 Polyhedron *A=NULL, *B=NULL, *C, *D;
15 ZPolyhedron *ZA, *ZB, *ZC, *ZD, *Zlast;
16 int nbPol, nbMat, func, rank ;
17 Vector *v=NULL;
19 /* The structure of the input file to this program is the following:
20 First a line containing
21 M nbMat
22 Where nbMat is an integer indicating how many Matrices will
23 be described in the following. temporary debugging. Next
24 the matrice are described. For each matrix, the first row is two
25 integers:
26 nbRows nbColumns
27 Then the matrix is written row by row. a line starting with
28 a `#' is considered as a comment
30 Then a line containing
31 D nbDomain
32 where nbDomain is an integer indicating how many domain will
33 be described in the following. Domains are describled as for
34 polylib, the first row is two integers:
35 nbConstraints dimension
36 then the constraints are described in the Polylib format.
37 The last line of the input file contains :
38 F numTest
39 which indicates which test will be performed on the data.
40 Warning, currently no more than 3 matrice of Polyhedra can be read*/
42 fgets(s, 128, stdin);
43 nbPol = nbMat = 0;
44 while ( (*s=='#') ||
45 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
46 fgets(s, 128, stdin);
49 /* debug */
50 /* fprintf(stdout,"nbMat=%d",nbMat);fflush(stdout); */
52 switch (nbMat) {
54 case 1:
55 a = Matrix_Read();
56 break;
58 case 2:
59 a = Matrix_Read();
60 b = Matrix_Read();
61 break;
63 case 3: a = Matrix_Read();
64 b = Matrix_Read();
65 c = Matrix_Read();
66 break;
69 fgets(s, 128, stdin);
70 while ((*s=='#') ||
71 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
72 fgets(s, 128, stdin);
75 /* debug */
76 /* fprintf(stdout,"nbPol=%d",nbPol);fflush(stdout); */
78 switch (nbPol) {
80 case 1:
81 g = Matrix_Read();
82 A = Constraints2Polyhedron(g,2000);
83 Matrix_Free(g);
84 break;
86 case 2:
87 g = Matrix_Read();
88 A = Constraints2Polyhedron(g,2000);
89 Matrix_Free(g);
90 g = Matrix_Read();
91 B = Constraints2Polyhedron(g,2000);
92 Matrix_Free(g);
93 break;
95 case 3:
96 g = Matrix_Read();
97 A = Constraints2Polyhedron(g,2000);
98 Matrix_Free(g);
99 g = Matrix_Read();
100 B = Constraints2Polyhedron(g,2000);
101 Matrix_Free(g);
102 g = Matrix_Read();
103 C = Constraints2Polyhedron(g,2000);
104 Matrix_Free(g);
105 break;
108 fgets(s, 128, stdin);
109 while ((*s=='#') || (sscanf(s, "F %d", &func)<1) ) fgets(s, 128, stdin);
112 switch (func) {
114 case 1:
116 /* just a test of polylib functions */
117 C = DomainUnion(A, B, 200);
118 D = DomainConvex(C, 200);
119 d = Polyhedron2Constraints(D);
120 Matrix_Print(stdout,P_VALUE_FMT, d);
121 Matrix_Free(d);
122 Domain_Free(C);
123 Domain_Free(D);
124 break;
126 case 2: /* AffineHermite */
128 AffineHermite(a,&b,&c);
129 Matrix_Print(stdout,P_VALUE_FMT, b);
130 Matrix_Print(stdout,P_VALUE_FMT, c);
131 Matrix_Free(a);
132 Matrix_Free(b);
133 Matrix_Free(c);
134 break;
136 case 3: /* LatticeIntersection */
138 c = LatticeIntersection(a,b);
139 Matrix_Print(stdout,P_VALUE_FMT, c);
140 Matrix_Free(a);
141 Matrix_Free(b);
142 Matrix_Free(c);
143 break;
145 case 4: /* LatticeDifference */
147 fprintf(stdout," 2 in 1 : %d\n",LatticeIncludes(b,a));
148 fprintf(stdout," 1 in 3 : %d\n",LatticeIncludes(c,a));
149 fprintf(stdout," 1 in 2 : %d\n",LatticeIncludes(a,b));
150 Matrix_Free(a);
151 Matrix_Free(b);
152 Matrix_Free(c);
153 break;
155 case 5: /* LatticeDifference */
157 l1=LatticeDifference(a,b);
158 l2=LatticeDifference(b,a);
159 l3=LatticeDifference(c,a);
160 l4=LatticeDifference(b,c);
161 fprintf(stdout,"L1 - L2 :\n");
162 temp=l1;
163 while (temp!=NULL) {
165 Matrix_Print(stdout,P_VALUE_FMT,temp->M);
166 temp=temp->next;
168 fprintf(stdout,"Diff2:\n");
169 temp=l2;
170 while (temp!=NULL) {
171 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
172 temp=temp->next;
174 fprintf(stdout,"Diff3:\n");
175 temp=l3;
176 while (temp!=NULL) {
177 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
178 temp=temp->next;
180 fprintf(stdout,"Diff4:\n");
181 temp=l4;
182 while (temp!=NULL) {
183 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
184 temp=temp->next;
186 Matrix_Free(a);
187 Matrix_Free(b);
188 Matrix_Free(c);
189 break;
191 case 6: /* isEmptyZPolyhedron */
193 ZA=ZPolyhedron_Alloc(a,A);
194 fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
195 ZDomain_Free(ZA);
196 break;
198 case 7: /* ZDomainIntersection */
200 ZA=ZPolyhedron_Alloc(a,A);
201 ZB=ZPolyhedron_Alloc(b,B);
202 ZC = ZDomainIntersection(ZA,ZB);
203 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
204 break;
206 case 8: /* ZDomainUnion */
208 ZA=ZPolyhedron_Alloc(a,A);
209 ZB=ZPolyhedron_Alloc(b,B);
210 ZC = ZDomainUnion(ZA,ZB);
211 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
212 break;
214 case 9: /* ZDomainDifference */
216 ZA=ZPolyhedron_Alloc(a,A);
217 ZB=ZPolyhedron_Alloc(b,B);
218 ZC = ZDomainDifference(ZA,ZB);
219 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
220 break;
222 case 10: /* ZDomainImage */
224 ZA=ZPolyhedron_Alloc(a,A);
225 ZC = ZDomainImage(ZA,b);
226 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
227 break;
229 case 11: /* ZDomainPreimage */
231 ZA=ZPolyhedron_Alloc(a,A);
232 ZC = ZDomainPreimage(ZA,b);
233 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
234 break;
236 case 12: /* ZDomainDifference */
237 ZA=ZPolyhedron_Alloc(a,A);
238 ZC = ZDomainPreimage(ZA,b);
239 ZD = ZDomainImage(ZC,b);
240 Zlast=ZDomainDifference(ZD,ZC);
241 fprintf(stdout,"the Two zpol are equal? :%d\n",
242 isEmptyZPolyhedron(Zlast));
243 break;
245 case 13: /* ZDomainSimplify */
247 ZA=ZPolyhedron_Alloc(a,A);
248 ZA->next = ZPolyhedron_Alloc(b,B);
249 ZDomainPrint(stdout,P_VALUE_FMT, ZA);
250 ZD = ZDomainSimplify(ZA);
251 ZDomainPrint(stdout,P_VALUE_FMT, ZD);
252 break;
254 case 14: /* EmptyZpolyhedron */
256 ZA=EmptyZPolyhedron(3);
257 fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
258 ZDomain_Free(ZA);
259 break;
261 case 15: /* ZDomainInclude */
263 ZA=ZPolyhedron_Alloc(a,A);
264 ZB=ZPolyhedron_Alloc(b,B);
265 fprintf(stdout,"A in B :%d \nB in A :%d \n",
266 ZPolyhedronIncludes(ZA,ZB),
267 ZPolyhedronIncludes(ZB,ZA));
268 break;
270 case 16: /* LatticePreimage */
272 c = LatticePreimage(a,b);
273 Matrix_Print(stdout,P_VALUE_FMT, c);
274 AffineHermite(c,&d,&e);
275 Matrix_Print(stdout,P_VALUE_FMT, d);
276 break;
278 case 17: /* LatticeImage */
280 c = LatticeImage(a,b);
281 Matrix_Print(stdout,P_VALUE_FMT, c);
282 AffineHermite(c,&d,&e);
283 Matrix_Print(stdout,P_VALUE_FMT, d);
284 break;
286 case 18: /* EmptyLattice */
288 fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(a));
289 fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(EmptyLattice(3)));
290 break;
292 case 19: /* CanonicalForm */
294 ZA=ZPolyhedron_Alloc(a,A);
295 ZB=ZPolyhedron_Alloc(a,B);
296 CanonicalForm(ZA,&ZC,&c);
297 CanonicalForm(ZB,&ZD,&d);
298 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
299 ZDomainPrint(stdout,P_VALUE_FMT, ZD);
300 break;
302 case 20: /* LatticeSimplify */
304 l1=LatticeUnion_Alloc();
305 l2=LatticeUnion_Alloc();
306 l1->M=Matrix_Copy(a);
307 l1->next=l2;
308 l2->M=Matrix_Copy(b);
309 l1=LatticeSimplify(l1);
310 PrintLatticeUnion(stdout,P_VALUE_FMT,l1);
311 LatticeUnion_Free(l1);
312 break;
314 case 21: /* AffineSmith */
316 AffineSmith(a,&b,&c, &d);
317 Matrix_Print(stdout,P_VALUE_FMT, b);
318 Matrix_Print(stdout,P_VALUE_FMT, c);
319 Matrix_Print(stdout,P_VALUE_FMT, d);
320 Matrix_Free(a);
321 Matrix_Free(b);
322 Matrix_Free(c);
323 Matrix_Free(d);
324 break;
326 case 22: /* SolveDiophantine */
328 rank=SolveDiophantine(a,&d,&v);
329 Matrix_Print(stdout,P_VALUE_FMT, a);
330 fprintf(stdout," rank: %d \n ",rank);
331 Matrix_Print(stdout,P_VALUE_FMT, d);
332 Vector_Print(stdout,P_VALUE_FMT, v);
333 rank=SolveDiophantine(b,&d,&v);
334 Matrix_Print(stdout,P_VALUE_FMT, b);
335 fprintf(stdout," rank: %d \n ",rank);
336 Matrix_Print(stdout,P_VALUE_FMT, d);
337 Vector_Print(stdout,P_VALUE_FMT, v);
338 rank=SolveDiophantine(c,&d,&v);
339 Matrix_Print(stdout,P_VALUE_FMT, c);
340 fprintf(stdout," rank: %d \n ",rank);
341 Matrix_Print(stdout,P_VALUE_FMT, d);
342 Vector_Print(stdout,P_VALUE_FMT, v);
343 Matrix_Free(a);
344 Matrix_Free(b);
345 Matrix_Free(c);
346 Vector_Free(v);
347 break;
349 case 23: /* SplitZPolyhedron */
351 ZA=ZPolyhedron_Alloc(a,A);
352 ZC = SplitZpolyhedron(ZA,b);
353 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
354 break;
357 case 100: /* debug */
359 ZA=ZPolyhedron_Alloc(a,A);
360 ZDomainPrint(stdout,P_VALUE_FMT, ZA);
361 ZDomain_Free(ZA);
362 break;
364 default:
365 printf("? unknown function\n");
368 /* Polyhedron_Free(A); */
370 return 0;
371 } /* main */