thread safe polylib configuration
[polylib.git] / applications / Zpolytest.c
blobe0006bfbffcba06cf4cfe640c96a61abc3799a3f
1 /* zpolytest.c
2 This is a testbench for the Zpolylib (part of polylib manipulating
3 Z-polyhedra. */
4 /*
5 This file is part of PolyLib.
7 PolyLib is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 PolyLib is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with PolyLib. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdio.h>
23 #include <polylib/polylib.h>
25 #define WS 0
27 char s[128];
29 int main() {
31 Matrix *a=NULL, *b=NULL, *c=NULL, *d, *e, *g;
32 LatticeUnion *l1,*l2,*l3,*l4,*temp;
33 Polyhedron *A=NULL, *B=NULL, *C=NULL, *D;
34 ZPolyhedron *ZA, *ZB, *ZC, *ZD, *Zlast;
35 int nbPol, nbMat, func, rank ;
36 Vector *v=NULL;
38 /* The structure of the input file to this program is the following:
39 First a line containing
40 M nbMat
41 Where nbMat is an integer indicating how many Matrices will
42 be described in the following. temporary debugging. Next
43 the matrice are described. For each matrix, the first row is two
44 integers:
45 nbRows nbColumns
46 Then the matrix is written row by row. a line starting with
47 a `#' is considered as a comment
49 Then a line containing
50 D nbDomain
51 where nbDomain is an integer indicating how many domain will
52 be described in the following. Domains are describled as for
53 polylib, the first row is two integers:
54 nbConstraints dimension
55 then the constraints are described in the Polylib format.
56 The last line of the input file contains :
57 F numTest
58 which indicates which test will be performed on the data.
59 Warning, currently no more than 3 matrice of Polyhedra can be read*/
61 fgets(s, 128, stdin);
62 nbPol = nbMat = 0;
63 while ( (*s=='#') ||
64 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
65 fgets(s, 128, stdin);
68 /* debug */
69 /* fprintf(stdout,"nbMat=%d",nbMat);fflush(stdout); */
71 switch (nbMat) {
73 case 1:
74 a = Matrix_Read();
75 break;
77 case 2:
78 a = Matrix_Read();
79 b = Matrix_Read();
80 break;
82 case 3: a = Matrix_Read();
83 b = Matrix_Read();
84 c = Matrix_Read();
85 break;
88 fgets(s, 128, stdin);
89 while ((*s=='#') ||
90 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
91 fgets(s, 128, stdin);
94 /* debug */
95 /* fprintf(stdout,"nbPol=%d",nbPol);fflush(stdout); */
97 switch (nbPol) {
99 case 1:
100 g = Matrix_Read();
101 A = Constraints2Polyhedron(g,WS);
102 Matrix_Free(g);
103 break;
105 case 2:
106 g = Matrix_Read();
107 A = Constraints2Polyhedron(g,WS);
108 Matrix_Free(g);
109 g = Matrix_Read();
110 B = Constraints2Polyhedron(g,WS);
111 Matrix_Free(g);
112 break;
114 case 3:
115 g = Matrix_Read();
116 A = Constraints2Polyhedron(g,WS);
117 Matrix_Free(g);
118 g = Matrix_Read();
119 B = Constraints2Polyhedron(g,WS);
120 Matrix_Free(g);
121 g = Matrix_Read();
122 C = Constraints2Polyhedron(g,WS);
123 Matrix_Free(g);
124 break;
127 fgets(s, 128, stdin);
128 while ((*s=='#') || (sscanf(s, "F %d", &func)<1) ) fgets(s, 128, stdin);
131 switch (func) {
133 case 1:
135 /* just a test of polylib functions */
136 C = DomainUnion(A, B, 200);
137 D = DomainConvex(C, 200);
138 d = Polyhedron2Constraints(D);
139 Matrix_Print(stdout,P_VALUE_FMT, d);
140 Matrix_Free(d);
141 Domain_Free(D);
142 break;
144 case 2: /* AffineHermite */
146 AffineHermite(a,&b,&c);
147 Matrix_Print(stdout,P_VALUE_FMT, b);
148 Matrix_Print(stdout,P_VALUE_FMT, c);
149 break;
151 case 3: /* LatticeIntersection */
153 c = LatticeIntersection(a,b);
154 Matrix_Print(stdout,P_VALUE_FMT, c);
155 break;
157 case 4: /* LatticeDifference */
159 fprintf(stdout," 2 in 1 : %d\n",LatticeIncludes(b,a));
160 fprintf(stdout," 1 in 3 : %d\n",LatticeIncludes(c,a));
161 fprintf(stdout," 1 in 2 : %d\n",LatticeIncludes(a,b));
162 break;
164 case 5: /* LatticeDifference */
166 l1=LatticeDifference(a,b);
167 l2=LatticeDifference(b,a);
168 l3=LatticeDifference(c,a);
169 l4=LatticeDifference(b,c);
170 fprintf(stdout,"L1 - L2 :\n");
171 temp=l1;
172 while (temp!=NULL) {
174 Matrix_Print(stdout,P_VALUE_FMT,temp->M);
175 temp=temp->next;
177 fprintf(stdout,"Diff2:\n");
178 temp=l2;
179 while (temp!=NULL) {
180 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
181 temp=temp->next;
183 fprintf(stdout,"Diff3:\n");
184 temp=l3;
185 while (temp!=NULL) {
186 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
187 temp=temp->next;
189 fprintf(stdout,"Diff4:\n");
190 temp=l4;
191 while (temp!=NULL) {
192 Matrix_Print(stdout,P_VALUE_FMT, temp->M);
193 temp=temp->next;
195 break;
197 case 6: /* isEmptyZPolyhedron */
199 ZA=ZPolyhedron_Alloc(a,A);
200 fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
201 ZDomain_Free(ZA);
202 break;
204 case 7: /* ZDomainIntersection */
206 ZA=ZPolyhedron_Alloc(a,A);
207 ZB=ZPolyhedron_Alloc(b,B);
208 ZC = ZDomainIntersection(ZA,ZB);
209 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
210 ZDomain_Free(ZA);
211 ZDomain_Free(ZB);
212 ZDomain_Free(ZC);
213 break;
215 case 8: /* ZDomainUnion */
217 ZA=ZPolyhedron_Alloc(a,A);
218 ZB=ZPolyhedron_Alloc(b,B);
219 ZC = ZDomainUnion(ZA,ZB);
220 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
221 break;
223 case 9: /* ZDomainDifference */
225 ZA=ZPolyhedron_Alloc(a,A);
226 ZB=ZPolyhedron_Alloc(b,B);
227 ZC = ZDomainDifference(ZA,ZB);
228 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
229 break;
231 case 10: /* ZDomainImage */
233 ZA=ZPolyhedron_Alloc(a,A);
234 ZC = ZDomainImage(ZA,b);
235 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
236 break;
238 case 11: /* ZDomainPreimage */
240 ZA=ZPolyhedron_Alloc(a,A);
241 ZC = ZDomainPreimage(ZA,b);
242 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
243 break;
245 case 12: /* ZDomainDifference */
246 ZA=ZPolyhedron_Alloc(a,A);
247 ZC = ZDomainPreimage(ZA,b);
248 ZD = ZDomainImage(ZC,b);
249 Zlast=ZDomainDifference(ZD,ZC);
250 fprintf(stdout,"the Two zpol are equal? :%d\n",
251 isEmptyZPolyhedron(Zlast));
252 break;
254 case 13: /* ZDomainSimplify */
256 ZA=ZPolyhedron_Alloc(a,A);
257 ZA->next = ZPolyhedron_Alloc(b,B);
258 ZDomainPrint(stdout,P_VALUE_FMT, ZA);
259 ZD = ZDomainSimplify(ZA);
260 ZDomainPrint(stdout,P_VALUE_FMT, ZD);
261 break;
263 case 14: /* EmptyZpolyhedron */
265 ZA=EmptyZPolyhedron(3);
266 fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
267 ZDomain_Free(ZA);
268 break;
270 case 15: /* ZDomainInclude */
272 ZA=ZPolyhedron_Alloc(a,A);
273 ZB=ZPolyhedron_Alloc(b,B);
274 fprintf(stdout,"A in B :%d \nB in A :%d \n",
275 ZPolyhedronIncludes(ZA,ZB),
276 ZPolyhedronIncludes(ZB,ZA));
277 break;
279 case 16: /* LatticePreimage */
281 c = LatticePreimage(a,b);
282 Matrix_Print(stdout,P_VALUE_FMT, c);
283 AffineHermite(c,&d,&e);
284 Matrix_Print(stdout,P_VALUE_FMT, d);
285 break;
287 case 17: /* LatticeImage */
289 c = LatticeImage(a,b);
290 Matrix_Print(stdout,P_VALUE_FMT, c);
291 AffineHermite(c,&d,&e);
292 Matrix_Print(stdout,P_VALUE_FMT, d);
293 break;
295 case 18: /* EmptyLattice */
297 fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(a));
298 fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(EmptyLattice(3)));
299 break;
301 case 19: /* CanonicalForm */
303 ZA=ZPolyhedron_Alloc(a,A);
304 ZB=ZPolyhedron_Alloc(a,B);
305 CanonicalForm(ZA,&ZC,&c);
306 CanonicalForm(ZB,&ZD,&d);
307 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
308 ZDomainPrint(stdout,P_VALUE_FMT, ZD);
309 break;
311 case 20: /* LatticeSimplify */
313 l1=LatticeUnion_Alloc();
314 l2=LatticeUnion_Alloc();
315 l1->M=Matrix_Copy(a);
316 l1->next=l2;
317 l2->M=Matrix_Copy(b);
318 l1=LatticeSimplify(l1);
319 PrintLatticeUnion(stdout,P_VALUE_FMT,l1);
320 LatticeUnion_Free(l1);
321 break;
323 case 21: /* AffineSmith */
325 AffineSmith(a,&b,&c, &d);
326 Matrix_Print(stdout,P_VALUE_FMT, b);
327 Matrix_Print(stdout,P_VALUE_FMT, c);
328 Matrix_Print(stdout,P_VALUE_FMT, d);
329 Matrix_Free(d);
330 break;
332 case 22: /* SolveDiophantine */
334 rank=SolveDiophantine(a,&d,&v);
335 Matrix_Print(stdout,P_VALUE_FMT, a);
336 fprintf(stdout," rank: %d \n ",rank);
337 Matrix_Print(stdout,P_VALUE_FMT, d);
338 Vector_Print(stdout,P_VALUE_FMT, v);
339 rank=SolveDiophantine(b,&d,&v);
340 Matrix_Print(stdout,P_VALUE_FMT, b);
341 fprintf(stdout," rank: %d \n ",rank);
342 Matrix_Print(stdout,P_VALUE_FMT, d);
343 Vector_Print(stdout,P_VALUE_FMT, v);
344 rank=SolveDiophantine(c,&d,&v);
345 Matrix_Print(stdout,P_VALUE_FMT, c);
346 fprintf(stdout," rank: %d \n ",rank);
347 Matrix_Print(stdout,P_VALUE_FMT, d);
348 Vector_Print(stdout,P_VALUE_FMT, v);
349 Vector_Free(v);
350 break;
352 case 23: /* SplitZPolyhedron */
354 ZA=ZPolyhedron_Alloc(a,A);
355 ZC = SplitZpolyhedron(ZA,b);
356 ZDomainPrint(stdout,P_VALUE_FMT, ZC);
357 break;
360 case 100: /* debug */
362 ZA=ZPolyhedron_Alloc(a,A);
363 ZDomainPrint(stdout,P_VALUE_FMT, ZA);
364 ZDomain_Free(ZA);
365 break;
367 default:
368 printf("? unknown function\n");
371 /* Polyhedron_Free(A); */
372 if (a)
373 Matrix_Free(a);
374 if (b)
375 Matrix_Free(b);
376 if (c)
377 Matrix_Free(c);
379 if (A)
380 Domain_Free(A);
381 if (B)
382 Domain_Free(B);
383 if (C)
384 Domain_Free(C);
386 return 0;
387 } /* main */