new version 5.22.4
[polylib.git] / applications / Zpolytest.c
blob07333ff7dacf6d6c6313913307c73676a393c8b6
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 #define WS 0
10 char s[128];
12 int main() {
14 Matrix *a=NULL, *b=NULL, *c=NULL, *d, *e, *g;
15 LatticeUnion *l1,*l2,*l3,*l4,*temp;
16 Polyhedron *A=NULL, *B=NULL, *C=NULL, *D;
17 ZPolyhedron *ZA, *ZB, *ZC, *ZD, *Zlast;
18 int nbPol, nbMat, func, rank ;
19 Vector *v=NULL;
21 /* The structure of the input file to this program is the following:
22 First a line containing
23 M nbMat
24 Where nbMat is an integer indicating how many Matrices will
25 be described in the following. temporary debugging. Next
26 the matrice are described. For each matrix, the first row is two
27 integers:
28 nbRows nbColumns
29 Then the matrix is written row by row. a line starting with
30 a `#' is considered as a comment
32 Then a line containing
33 D nbDomain
34 where nbDomain is an integer indicating how many domain will
35 be described in the following. Domains are describled as for
36 polylib, the first row is two integers:
37 nbConstraints dimension
38 then the constraints are described in the Polylib format.
39 The last line of the input file contains :
40 F numTest
41 which indicates which test will be performed on the data.
42 Warning, currently no more than 3 matrice of Polyhedra can be read*/
44 fgets(s, 128, stdin);
45 nbPol = nbMat = 0;
46 while ( (*s=='#') ||
47 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
48 fgets(s, 128, stdin);
51 /* debug */
52 /* fprintf(stdout,"nbMat=%d",nbMat);fflush(stdout); */
54 switch (nbMat) {
56 case 1:
57 a = Matrix_Read();
58 break;
60 case 2:
61 a = Matrix_Read();
62 b = Matrix_Read();
63 break;
65 case 3: a = Matrix_Read();
66 b = Matrix_Read();
67 c = Matrix_Read();
68 break;
71 fgets(s, 128, stdin);
72 while ((*s=='#') ||
73 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
74 fgets(s, 128, stdin);
77 /* debug */
78 /* fprintf(stdout,"nbPol=%d",nbPol);fflush(stdout); */
80 switch (nbPol) {
82 case 1:
83 g = Matrix_Read();
84 A = Constraints2Polyhedron(g,WS);
85 Matrix_Free(g);
86 break;
88 case 2:
89 g = Matrix_Read();
90 A = Constraints2Polyhedron(g,WS);
91 Matrix_Free(g);
92 g = Matrix_Read();
93 B = Constraints2Polyhedron(g,WS);
94 Matrix_Free(g);
95 break;
97 case 3:
98 g = Matrix_Read();
99 A = Constraints2Polyhedron(g,WS);
100 Matrix_Free(g);
101 g = Matrix_Read();
102 B = Constraints2Polyhedron(g,WS);
103 Matrix_Free(g);
104 g = Matrix_Read();
105 C = Constraints2Polyhedron(g,WS);
106 Matrix_Free(g);
107 break;
110 fgets(s, 128, stdin);
111 while ((*s=='#') || (sscanf(s, "F %d", &func)<1) ) fgets(s, 128, stdin);
114 switch (func) {
116 case 1:
118 /* just a test of polylib functions */
119 C = DomainUnion(A, B, 200);
120 D = DomainConvex(C, 200);
121 d = Polyhedron2Constraints(D);
122 Matrix_Print(stdout,P_VALUE_FMT, d);
123 Matrix_Free(d);
124 Domain_Free(D);
125 break;
127 case 2: /* AffineHermite */
129 AffineHermite(a,&b,&c);
130 Matrix_Print(stdout,P_VALUE_FMT, b);
131 Matrix_Print(stdout,P_VALUE_FMT, c);
132 break;
134 case 3: /* LatticeIntersection */
136 c = LatticeIntersection(a,b);
137 Matrix_Print(stdout,P_VALUE_FMT, c);
138 break;
140 case 4: /* LatticeDifference */
142 fprintf(stdout," 2 in 1 : %d\n",LatticeIncludes(b,a));
143 fprintf(stdout," 1 in 3 : %d\n",LatticeIncludes(c,a));
144 fprintf(stdout," 1 in 2 : %d\n",LatticeIncludes(a,b));
145 break;
147 case 5: /* LatticeDifference */
149 l1=LatticeDifference(a,b);
150 l2=LatticeDifference(b,a);
151 l3=LatticeDifference(c,a);
152 l4=LatticeDifference(b,c);
153 fprintf(stdout,"L1 - L2 :\n");
154 temp=l1;
155 while (temp!=NULL) {
157 Matrix_Print(stdout,P_VALUE_FMT,temp->M);
158 temp=temp->next;
160 fprintf(stdout,"Diff2:\n");
161 temp=l2;
162 while (temp!=NULL) {
163 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
164 temp=temp->next;
166 fprintf(stdout,"Diff3:\n");
167 temp=l3;
168 while (temp!=NULL) {
169 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
170 temp=temp->next;
172 fprintf(stdout,"Diff4:\n");
173 temp=l4;
174 while (temp!=NULL) {
175 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
176 temp=temp->next;
178 break;
180 case 6: /* isEmptyZPolyhedron */
182 ZA=ZPolyhedron_Alloc(a,A);
183 fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
184 ZDomain_Free(ZA);
185 break;
187 case 7: /* ZDomainIntersection */
189 ZA=ZPolyhedron_Alloc(a,A);
190 ZB=ZPolyhedron_Alloc(b,B);
191 ZC = ZDomainIntersection(ZA,ZB);
192 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
193 ZDomain_Free(ZA);
194 ZDomain_Free(ZB);
195 ZDomain_Free(ZC);
196 break;
198 case 8: /* ZDomainUnion */
200 ZA=ZPolyhedron_Alloc(a,A);
201 ZB=ZPolyhedron_Alloc(b,B);
202 ZC = ZDomainUnion(ZA,ZB);
203 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
204 break;
206 case 9: /* ZDomainDifference */
208 ZA=ZPolyhedron_Alloc(a,A);
209 ZB=ZPolyhedron_Alloc(b,B);
210 ZC = ZDomainDifference(ZA,ZB);
211 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
212 break;
214 case 10: /* ZDomainImage */
216 ZA=ZPolyhedron_Alloc(a,A);
217 ZC = ZDomainImage(ZA,b);
218 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
219 break;
221 case 11: /* ZDomainPreimage */
223 ZA=ZPolyhedron_Alloc(a,A);
224 ZC = ZDomainPreimage(ZA,b);
225 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
226 break;
228 case 12: /* ZDomainDifference */
229 ZA=ZPolyhedron_Alloc(a,A);
230 ZC = ZDomainPreimage(ZA,b);
231 ZD = ZDomainImage(ZC,b);
232 Zlast=ZDomainDifference(ZD,ZC);
233 fprintf(stdout,"the Two zpol are equal? :%d\n",
234 isEmptyZPolyhedron(Zlast));
235 break;
237 case 13: /* ZDomainSimplify */
239 ZA=ZPolyhedron_Alloc(a,A);
240 ZA->next = ZPolyhedron_Alloc(b,B);
241 ZDomainPrint(stdout,P_VALUE_FMT, ZA);
242 ZD = ZDomainSimplify(ZA);
243 ZDomainPrint(stdout,P_VALUE_FMT, ZD);
244 break;
246 case 14: /* EmptyZpolyhedron */
248 ZA=EmptyZPolyhedron(3);
249 fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
250 ZDomain_Free(ZA);
251 break;
253 case 15: /* ZDomainInclude */
255 ZA=ZPolyhedron_Alloc(a,A);
256 ZB=ZPolyhedron_Alloc(b,B);
257 fprintf(stdout,"A in B :%d \nB in A :%d \n",
258 ZPolyhedronIncludes(ZA,ZB),
259 ZPolyhedronIncludes(ZB,ZA));
260 break;
262 case 16: /* LatticePreimage */
264 c = LatticePreimage(a,b);
265 Matrix_Print(stdout,P_VALUE_FMT, c);
266 AffineHermite(c,&d,&e);
267 Matrix_Print(stdout,P_VALUE_FMT, d);
268 break;
270 case 17: /* LatticeImage */
272 c = LatticeImage(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 18: /* EmptyLattice */
280 fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(a));
281 fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(EmptyLattice(3)));
282 break;
284 case 19: /* CanonicalForm */
286 ZA=ZPolyhedron_Alloc(a,A);
287 ZB=ZPolyhedron_Alloc(a,B);
288 CanonicalForm(ZA,&ZC,&c);
289 CanonicalForm(ZB,&ZD,&d);
290 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
291 ZDomainPrint(stdout,P_VALUE_FMT, ZD);
292 break;
294 case 20: /* LatticeSimplify */
296 l1=LatticeUnion_Alloc();
297 l2=LatticeUnion_Alloc();
298 l1->M=Matrix_Copy(a);
299 l1->next=l2;
300 l2->M=Matrix_Copy(b);
301 l1=LatticeSimplify(l1);
302 PrintLatticeUnion(stdout,P_VALUE_FMT,l1);
303 LatticeUnion_Free(l1);
304 break;
306 case 21: /* AffineSmith */
308 AffineSmith(a,&b,&c, &d);
309 Matrix_Print(stdout,P_VALUE_FMT, b);
310 Matrix_Print(stdout,P_VALUE_FMT, c);
311 Matrix_Print(stdout,P_VALUE_FMT, d);
312 Matrix_Free(d);
313 break;
315 case 22: /* SolveDiophantine */
317 rank=SolveDiophantine(a,&d,&v);
318 Matrix_Print(stdout,P_VALUE_FMT, a);
319 fprintf(stdout," rank: %d \n ",rank);
320 Matrix_Print(stdout,P_VALUE_FMT, d);
321 Vector_Print(stdout,P_VALUE_FMT, v);
322 rank=SolveDiophantine(b,&d,&v);
323 Matrix_Print(stdout,P_VALUE_FMT, b);
324 fprintf(stdout," rank: %d \n ",rank);
325 Matrix_Print(stdout,P_VALUE_FMT, d);
326 Vector_Print(stdout,P_VALUE_FMT, v);
327 rank=SolveDiophantine(c,&d,&v);
328 Matrix_Print(stdout,P_VALUE_FMT, c);
329 fprintf(stdout," rank: %d \n ",rank);
330 Matrix_Print(stdout,P_VALUE_FMT, d);
331 Vector_Print(stdout,P_VALUE_FMT, v);
332 Vector_Free(v);
333 break;
335 case 23: /* SplitZPolyhedron */
337 ZA=ZPolyhedron_Alloc(a,A);
338 ZC = SplitZpolyhedron(ZA,b);
339 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
340 break;
343 case 100: /* debug */
345 ZA=ZPolyhedron_Alloc(a,A);
346 ZDomainPrint(stdout,P_VALUE_FMT, ZA);
347 ZDomain_Free(ZA);
348 break;
350 default:
351 printf("? unknown function\n");
354 /* Polyhedron_Free(A); */
355 if (a)
356 Matrix_Free(a);
357 if (b)
358 Matrix_Free(b);
359 if (c)
360 Matrix_Free(c);
362 if (A)
363 Domain_Free(A);
364 if (B)
365 Domain_Free(B);
366 if (C)
367 Domain_Free(C);
369 return 0;
370 } /* main */