polylib 7.01
Zpolytest.c
Go to the documentation of this file.
1/* zpolytest.c
2This is a testbench for the Zpolylib (part of polylib manipulating
3Z-polyhedra. */
4
5#include <stdio.h>
6#include <polylib/polylib.h>
7
8#define WS 0
9
10char s[128];
11
12int main() {
13
14 Matrix *a=NULL, *b=NULL, *c=NULL, *d=NULL, *e=NULL, *f=NULL, *g;
15 LatticeUnion *l1, *l2;
16 Polyhedron *A=NULL, *B=NULL, *C=NULL, *D = NULL;
17 LBL *ZA=NULL, *ZB=NULL, *ZC=NULL, *ZD=NULL;
18 int nbPol, nbMat, func;
19
20 // The structure of the input file to this program is the following:
21
22 // A line starting with a `#' is considered as a comment.
23
24 // - First a line containing:
25 // M nbMat
26 // Where nbMat is an integer indicating how many Matrices will be given in
27 // the following (max 3). Next the matrices are given in PolyLib format.
28 // For each matrix, the first row is two integers:
29 // nbRows nbColumns
30 // Then the matrix is given row by row.
31
32 // - Then a line containing:
33 // D nbDomain
34 // where nbDomain is an integer indicating how many domains will be given
35 // in the following (max 3). Domains are given in PolyLib format:
36 // the first row is two integers:
37 // nbConstraints dimension
38 // then the constraints line by line in Polylib format.
39
40 // - The last line of the input file contains:
41 // F numTest
42 // which indicates which test will be performed.
43
44 // All lines below are ignored.
45
46 nbPol = nbMat = 0;
47 // read matrices
48 do {
49 fgets(s, 128, stdin);
50 }
51 while ((*s=='#') ||
52 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)));
53
54 switch (nbMat) {
55
56 case 1:
57 a = Matrix_Read();
58 break;
59
60 case 2:
61 a = Matrix_Read();
62 b = Matrix_Read();
63 break;
64
65 case 3:
66 a = Matrix_Read();
67 b = Matrix_Read();
68 c = Matrix_Read();
69 break;
70 }
71
72 // read polyhedra
73 fgets(s, 128, stdin);
74 while ((*s=='#') ||
75 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
76 fgets(s, 128, stdin);
77
78 switch (nbPol) {
79
80 case 1:
81 g = Matrix_Read();
83 Matrix_Free(g);
84 break;
85
86 case 2:
87 g = Matrix_Read();
89 Matrix_Free(g);
90 g = Matrix_Read();
92 Matrix_Free(g);
93 break;
94
95 case 3:
96 g = Matrix_Read();
98 Matrix_Free(g);
99 g = Matrix_Read();
101 Matrix_Free(g);
102 g = Matrix_Read();
104 Matrix_Free(g);
105 break;
106 }
107
108 // read function
109 do {
110 fgets(s, 128, stdin);
111 }
112 while((*s=='#') || (sscanf(s, "F %d", &func)<1));
113
114 switch (func) {
115
116 case 1: /* just a test of polylib functions */
117 C = DomainUnion(A, B, 200);
118 D = DomainConvex(C, 200);
120 Matrix_Print(stdout, P_VALUE_FMT, d);
121 break;
122
123 case 2: /* AffineHermite */
124 if(isNormalLattice(a))
125 printf("input matrix is normal\n");
126 else
127 printf("input matrix is not normal\n");
128 AffineHermite(a,&b,&c);
129 Matrix_Print(stdout, P_VALUE_FMT, b);
130 Matrix_Print(stdout, P_VALUE_FMT, c);
131 break;
132
133 case 3: /* LatticeIntersection */
134 c = LatticeIntersection(a,b);
135 Matrix_Print(stdout, P_VALUE_FMT, c);
136 break;
137
138 case 4: /* LatticeIncluded */
139 AffineHermite(a, &d, NULL);
140 AffineHermite(b, &e, NULL);
141 AffineHermite(c, &f, NULL);
142 printf(" 2 in 1: %d\n", LatticeIncluded(e, d));
143 printf(" 1 in 3: %d\n", LatticeIncluded(f, d));
144 printf(" 1 in 2: %d\n", LatticeIncluded(d, e));
145 break;
146
147 case 5: /* LatticeDifference */
148 l1 = LatticeDifference(a, b);
149 l2 = LatticeDifference(b, a);
150 printf("L1 - L2:\n");
151 PrintLatticeUnion(stdout, P_VALUE_FMT, l1);
152 printf("L2 - L1:\n");
153 PrintLatticeUnion(stdout, P_VALUE_FMT, l2);
156 break;
157
158 case 6: /* isEmptyLBL */
159 ZA = LBLAlloc(a,A);
160 printf("is Empty? :%d\n", isEmptyLBL(ZA));
161 break;
162
163 case 7: /* LBLIntersection */
164 ZA = LBLAlloc(a, A);
165 ZB = LBLAlloc(b, B);
166 ZC = LBLIntersection(ZA, ZB);
167 LBLPrint(stdout, P_VALUE_FMT, ZC);
168 break;
169
170 case 8: /* LBLUnion */
171 ZA = LBLAlloc(a, A);
172 ZB = LBLAlloc(b, B);
173 ZC = LBLUnion(ZA,ZB);
174 LBLPrint(stdout, P_VALUE_FMT, ZC);
175 break;
176
177 case 9: /* LBLDifference */
178 ZA = LBLAlloc(a, A);
179 ZB = LBLAlloc(b, B);
180 ZC = LBLDifference(ZA, ZB);
181 ZD = LBLDifference(ZB, ZA);
184 printf("A - B = ");
185 LBLPrint(stdout, P_VALUE_FMT, ZC);
186 printf("\n\nB - A = ");
187 LBLPrint(stdout, P_VALUE_FMT, ZD);
188 break;
189
190 case 10: /* LBLImage */
191 ZA = LBLAlloc(a, A);
192 ZC = LBLImage(ZA, b);
193 LBLPrint(stdout, P_VALUE_FMT, ZC);
194 break;
195
196 case 11: /* LBLPreimage */
197 ZA = LBLAlloc(a, A);
198 ZC = LBLPreimage(ZA, b);
199 LBLPrint(stdout, P_VALUE_FMT, ZC);
200 break;
201
202 case 12: /* difference between image of preimage and original*/
203 ZA = LBLAlloc(a, A);
204 ZC = LBLPreimage(ZA, b);
205 ZD = LBLImage(ZC, b);
206
207 printf("PreIm(A) = ");
208 LBLPrint(stdout, P_VALUE_FMT, ZC);
209 printf("Im(PreIm(A)) = ");
210 LBLPrint(stdout, P_VALUE_FMT, ZC);
211 // ZD should be included in ZA
212 printf("The image of the preimage is included in the original LBL ");
213 printf("(should always be true)? %d\n", LBLIncluded(ZD, ZA));
214 ZB = LBLDifference(ZA, ZD);
215 printf("The image of the preimage is exactly the original LBL? %d\n",
216 isEmptyLBL(ZB));
217 break;
218
219 case 13: /* LBLSimplifyEmpty */
220 ZA = LBLAlloc(a, A);
221 printf("A = ");
222 LBLPrint(stdout, P_VALUE_FMT, ZA);
224 printf("LBLSimplifyEmpty(A) = ");
225 LBLPrint(stdout, P_VALUE_FMT, ZA);
226 break;
227
228 case 14: /* EmptyLBL */
229 ZA = EmptyLBL(3);
230 printf("is Empty? :%d\n", isEmptyLBL(ZA));
231 break;
232
233 case 15: /* LBLIncluded */
234 ZA = LBLAlloc(a, A);
235 ZB = LBLAlloc(b, B);
236 printf("A in B :%d\nB in A :%d\n",
237 LBLIncluded(ZA, ZB),
238 LBLIncluded(ZB, ZA));
239 break;
240
241 case 16: /* LBLComplement:
242 compute complement of complement and check equality with original */
243 ZA = LBLAlloc(a, A);
244 ZB = LBLComplement(ZA);
245 ZC = LBLComplement(ZB);
246 printf("A = ");
247 LBLPrint(stdout, P_VALUE_FMT, ZA);
248 printf("\nComplement(A) = ");
249 LBLPrint(stdout, P_VALUE_FMT, ZB);
250 printf("\nComplement(Complement(A)) = ");
251 LBLPrint(stdout, P_VALUE_FMT, ZC);
252 printf("\nIs the the complement of the complement the original?\n");
253 printf(" A is included in C(C(A)): %d\n", LBLIncluded(ZA, ZC));
254 printf(" C(C(A)) is included in A: %d\n", LBLIncluded(ZC, ZA));
255 break;
256
257 case 17: /* LBL2Zdomain and check equality with original */
258 ZA = LBLAlloc(a, A);
259 printf("A = ");
260 LBLPrint(stdout, P_VALUE_FMT, ZA);
261 ZB = LBL2ZDomain(ZA);
262 printf("LBL2ZDomain(A) = ");
263 LBLPrint(stdout, P_VALUE_FMT, ZB);
264
265 // check equality between ZA and ZB
266 ZC = LBLDifference(ZA, ZB);
268 printf("A - ZD(A) = ");
269 LBLPrint(stdout, P_VALUE_FMT, ZC);
270 ZD = LBLDifference(ZB, ZA);
272 printf("ZD(A) - A = ");
273 LBLPrint(stdout, P_VALUE_FMT, ZD);
274 break;
275
276 case 18: /* simple LBL2Zdomain + LBLSimplifyEmpty */
277 ZA = LBLAlloc(a, A);
278 printf("A = ");
279 LBLPrint(stdout, P_VALUE_FMT, ZA);
280 ZB = LBL2ZDomain(ZA);
282 printf("LBLSimplifyEmpty(LBL2ZDomain(A)) = ");
283 LBLPrint(stdout, P_VALUE_FMT, ZB);
284 break;
285
286 case 19: /* check that complement(A) inter A = empty */
287 /* and complement(A) union A = universe */
288 ZA = LBLAlloc(a, A);
289 ZB = LBLComplement(ZA);
290 printf("A = ");
291 LBLPrint(stdout, P_VALUE_FMT, ZA);
292 printf("\nComplement(A) = ");
293 LBLPrint(stdout, P_VALUE_FMT, ZB);
294
295 ZC = LBLIntersection(ZA, ZB);
297 printf("\nA inter Complement(A) (should be empty) = ");
298 LBLPrint(stdout, P_VALUE_FMT, ZC);
299 LBLFree(ZC);
300
301 ZC = UniverseLBL(ZA->Lat->NbRows - 1);
302 ZD = LBLUnion(ZA, ZB);
303 LBLFree(ZB);
304 ZB = LBLDifference(ZC, ZD);
306 printf("\nUniverse - (A union Complement(A)) (should be empty) = ");
307 LBLPrint(stdout, P_VALUE_FMT, ZB);
308 break;
309
310 case 20: /* LBLDisjointUnion of the difference between two LBLs */
311 ZA = LBLAlloc(a, A);
312 ZB = LBLAlloc(b, B);
313 ZC = LBLDifference(ZB, ZA);
314 printf("\nC = B - A = ");
315 LBLPrint(stdout, P_VALUE_FMT, ZC);
316 ZD = LBLDisjointUnion(ZC);
317 printf("\nLBLDisjointUnion(B - A) = ");
318 LBLPrint(stdout, P_VALUE_FMT, ZD);
319 break;
320
321 case 21: /* LBLDisjointUnion of the difference between two LBLs */
322 /* and check that the result is equal to the original */
323 ZA = LBLAlloc(a, A);
324 ZB = LBLAlloc(b, B);
325 printf("Computing C = B - A...\n");
326 ZC = LBLDifference(ZB, ZA);
327 // LBLPrint(stdout, P_VALUE_FMT, ZC);
328 ZD = LBLDisjointUnion(ZC);
329 printf("Computing D = LBLDisjointUnion(C)...\n");
330 // LBLPrint(stdout, P_VALUE_FMT, ZD);
331 LBLFree(ZA); ZA = NULL;
332 printf("Computing C - D...\n");
333 ZA = LBLDifference(ZC, ZD);
334 LBLSimplify(ZA);
335 printf("Checking that C - D is empty: %d\n", isEmptyLBL(ZA));
336 LBLFree(ZA); ZA = NULL;
337 printf("Computing D - C...\n");
338 ZA = LBLDifference(ZD, ZC);
339 LBLSimplify(ZA);
340 printf("Checking that D - C is empty: %d\n", isEmptyLBL(ZA));
341 break;
342
343 case 100: /* just alloc and normalize */
344 ZA = LBLAlloc(a,A);
345 LBLPrint(stdout, P_VALUE_FMT, ZA);
346 break;
347
348 default:
349 printf("? unknown function\n");
350 }
351
352 // free 4 allocated matrices, polyhedra, Z-polyhedra.
353 if (a) Matrix_Free(a);
354 if (b) Matrix_Free(b);
355 if (c) Matrix_Free(c);
356 if (d) Matrix_Free(d);
357
358 if (A) Domain_Free(A);
359 if (B) Domain_Free(B);
360 if (C) Domain_Free(C);
361 if (D) Domain_Free(D);
362
363 if (ZA) LBLFree(ZA);
364 if (ZB) LBLFree(ZB);
365 if (ZC) LBLFree(ZC);
366 if (ZD) LBLFree(ZD);
367
368 // free all remaining cache memory of PolyLib:
370
371 return 0;
372} /* main */
LatticeUnion * LatticeDifference(Matrix *A, Matrix *B)
Definition: Lattice.c:423
Matrix * LatticeIntersection(Matrix *A, Matrix *B)
Definition: Lattice.c:620
void AffineHermite(Matrix *A, Matrix **H, Matrix **U)
Definition: Lattice.c:221
Bool isNormalLattice(Matrix *A)
Definition: Lattice.c:136
void LatticeUnion_Free(LatticeUnion *Head)
Definition: Lattice.c:29
Bool LatticeIncluded(Matrix *A, Matrix *B)
Definition: Lattice.c:336
void PrintLatticeUnion(FILE *fp, char *format, LatticeUnion *Head)
Definition: Lattice.c:16
void LBLSimplifyEmpty(LBL *A)
Definition: Zpolyhedron.c:3235
LBL * LBLImage(LBL *A, Matrix *Func)
Definition: Zpolyhedron.c:475
LBL * LBLPreimage(LBL *A, Matrix *Func)
Definition: Zpolyhedron.c:497
LBL * LBL2ZDomain(LBL *A)
Definition: Zpolyhedron.c:3213
void LBLSimplify(LBL *A)
Definition: Zpolyhedron.c:3281
LBL * LBLUnion(LBL *A, LBL *B)
Definition: Zpolyhedron.c:344
LBL * LBLDisjointUnion(LBL *A)
Definition: Zpolyhedron.c:3480
LBL * LBLComplement(LBL *A)
Definition: Zpolyhedron.c:922
void LBLFree(LBL *L)
Definition: Zpolyhedron.c:122
LBL * UniverseLBL(int dimension)
Definition: Zpolyhedron.c:218
void LBLPrint(FILE *fp, const char *format, LBL *A)
Definition: Zpolyhedron.c:324
LBL * LBLDifference(LBL *A, LBL *B)
Definition: Zpolyhedron.c:406
Bool isEmptyLBL(LBL *A)
Definition: Zpolyhedron.c:54
Bool LBLIncluded(LBL *A, LBL *B)
Definition: Zpolyhedron.c:272
LBL * EmptyLBL(int dimension)
Definition: Zpolyhedron.c:189
LBL * LBLIntersection(LBL *A, LBL *B)
Definition: Zpolyhedron.c:364
LBL * LBLAlloc(Matrix *Lat, Polyhedron *Domain)
Definition: Zpolyhedron.c:75
char s[128]
Definition: Zpolytest.c:10
#define WS
Definition: Zpolytest.c:8
int main()
Definition: Zpolytest.c:12
Matrix * Matrix_Read(void)
Definition: matrix.c:218
void Matrix_Print(FILE *Dst, const char *Format, Matrix *Mat)
Definition: matrix.c:118
void Matrix_Free(Matrix *Mat)
Definition: matrix.c:69
Polyhedron * DomainConvex(Polyhedron *Pol, unsigned NbMaxConstrs)
Definition: polyhedron.c:3606
Polyhedron * DomainUnion(Polyhedron *Pol1, Polyhedron *Pol2, unsigned NbMaxRays)
Definition: polyhedron.c:3530
void polylib_close()
Free all cache allocated memory: call this function before exiting in a memory checker environment li...
Definition: polyhedron.c:4807
Matrix * Polyhedron2Constraints(Polyhedron *Pol)
Definition: polyhedron.c:2067
Polyhedron * Constraints2Polyhedron(Matrix *Constraints, unsigned NbMaxRays)
Given a matrix of constraints ('Constraints'), construct and return a polyhedron.
Definition: polyhedron.c:1912
void Domain_Free(Polyhedron *Pol)
Definition: polyhedron.c:1633
Definition: types.h:245
Matrix * Lat
Definition: types.h:246
Definition: types.h:88
unsigned NbRows
Definition: types.h:89
#define P_VALUE_FMT
Definition: types.h:42