File: Zpolytest.c

package info (click to toggle)
polylib 5.22.5-3%2Bdfsg
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 14,444 kB
  • ctags: 52,958
  • sloc: ansic: 16,342; sh: 10,134; makefile: 560
file content (387 lines) | stat: -rw-r--r-- 9,789 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* zpolytest.c
This is a testbench for the Zpolylib (part of polylib manipulating 
Z-polyhedra. */
/*
    This file is part of PolyLib.

    PolyLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PolyLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PolyLib.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>
#include <polylib/polylib.h>

#define WS 0

char s[128];

int main() {
  
  Matrix *a=NULL, *b=NULL, *c=NULL, *d, *e, *g;
  LatticeUnion *l1,*l2,*l3,*l4,*temp;
  Polyhedron *A=NULL, *B=NULL, *C=NULL, *D;
  ZPolyhedron *ZA, *ZB, *ZC, *ZD, *Zlast;
  int  nbPol, nbMat, func, rank ;
  Vector *v=NULL;
    
  /* The structure of the input file to this program  is the following: 
       First a line containing        
           M nbMat
       Where nbMat is an integer indicating how many Matrices will 
       be described in the following. temporary debugging. Next 
       the matrice are described. For each matrix, the first row is two
       integers:
           nbRows nbColumns 
       Then the matrix is written row by row. a line starting with 
      a `#' is considered as a comment 

      Then a line containing 
           D nbDomain
      where nbDomain is an integer indicating how many domain will 
       be described in the following. Domains are describled as for 
       polylib,  the first row is two integers:
          nbConstraints dimension
      then the constraints are described in the Polylib format.
      The last line of the input file contains :
          F numTest
          which indicates which test will be performed on the data.
      Warning, currently no more than 3 matrice of Polyhedra can be read*/
  
  fgets(s, 128, stdin);
  nbPol = nbMat = 0;
  while ( (*s=='#') ||
	  ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
    fgets(s, 128, stdin);
  
  
  /* debug */
  /*     fprintf(stdout,"nbMat=%d",nbMat);fflush(stdout); */
  
  switch (nbMat) {
    
  case 1: 
    a = Matrix_Read();
    break;
  
  case 2: 
    a = Matrix_Read();
    b = Matrix_Read();
    break;
  
  case 3: a = Matrix_Read();
    b = Matrix_Read();
    c = Matrix_Read();
    break;
  }
  
  fgets(s, 128, stdin);
  while ((*s=='#') ||
	 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
    fgets(s, 128, stdin);
  
  
  /* debug */
  /*  fprintf(stdout,"nbPol=%d",nbPol);fflush(stdout);  */
  
  switch (nbPol) { 
  
  case 1:  
    g = Matrix_Read();
    A = Constraints2Polyhedron(g,WS);
    Matrix_Free(g);
    break;
  
  case 2:         
    g = Matrix_Read();
    A = Constraints2Polyhedron(g,WS);
    Matrix_Free(g);
    g = Matrix_Read();
    B = Constraints2Polyhedron(g,WS);
    Matrix_Free(g);
    break;
  
  case 3:
    g = Matrix_Read();
    A = Constraints2Polyhedron(g,WS);
    Matrix_Free(g);
    g = Matrix_Read();
    B = Constraints2Polyhedron(g,WS);
    Matrix_Free(g);
    g = Matrix_Read();
    C = Constraints2Polyhedron(g,WS);
    Matrix_Free(g);
    break;
  }
  
  fgets(s, 128, stdin);
  while ((*s=='#') || (sscanf(s, "F %d", &func)<1) ) fgets(s, 128, stdin);
  

  switch (func) {
    
  case 1:
    
    /* just a  test of polylib functions */
    C = DomainUnion(A, B, 200);
    D = DomainConvex(C, 200);
    d = Polyhedron2Constraints(D);
    Matrix_Print(stdout,P_VALUE_FMT, d);
    Matrix_Free(d);
    Domain_Free(D);
    break;
    
  case 2: /* AffineHermite */
    
    AffineHermite(a,&b,&c);
    Matrix_Print(stdout,P_VALUE_FMT, b);
    Matrix_Print(stdout,P_VALUE_FMT, c);
    break;
    
  case 3: /* LatticeIntersection */
    
    c = LatticeIntersection(a,b);
    Matrix_Print(stdout,P_VALUE_FMT, c);
    break;
    
  case 4: /* LatticeDifference */
        
    fprintf(stdout," 2 in 1 : %d\n",LatticeIncludes(b,a));
    fprintf(stdout," 1 in 3 : %d\n",LatticeIncludes(c,a));
    fprintf(stdout," 1 in 2 : %d\n",LatticeIncludes(a,b));
    break;
  
  case 5: /* LatticeDifference */
    
    l1=LatticeDifference(a,b);
    l2=LatticeDifference(b,a);
    l3=LatticeDifference(c,a);
    l4=LatticeDifference(b,c);
    fprintf(stdout,"L1 - L2 :\n");
    temp=l1;
    while (temp!=NULL) {
      
      Matrix_Print(stdout,P_VALUE_FMT,temp->M);
      temp=temp->next; 
    };
    fprintf(stdout,"Diff2:\n");
    temp=l2;
    while (temp!=NULL) {
      Matrix_Print(stdout,P_VALUE_FMT, temp->M);
      temp=temp->next; 
    };
    fprintf(stdout,"Diff3:\n");
    temp=l3;
    while (temp!=NULL) {
      Matrix_Print(stdout,P_VALUE_FMT, temp->M);
      temp=temp->next; 
    };
    fprintf(stdout,"Diff4:\n");
    temp=l4;
    while (temp!=NULL) {
      Matrix_Print(stdout,P_VALUE_FMT, temp->M);
      temp=temp->next; 
    };
    break;
    
  case 6: /* isEmptyZPolyhedron */
    
    ZA=ZPolyhedron_Alloc(a,A);
    fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
    ZDomain_Free(ZA);
    break;
    
  case 7: /* ZDomainIntersection */
        
    ZA=ZPolyhedron_Alloc(a,A);
    ZB=ZPolyhedron_Alloc(b,B);
    ZC = ZDomainIntersection(ZA,ZB);
    ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    ZDomain_Free(ZA);
    ZDomain_Free(ZB);
    ZDomain_Free(ZC);
    break;
    
  case 8: /* ZDomainUnion */
    
    ZA=ZPolyhedron_Alloc(a,A);
    ZB=ZPolyhedron_Alloc(b,B);
    ZC = ZDomainUnion(ZA,ZB);
    ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    break;
    
  case 9: /* ZDomainDifference */
    
    ZA=ZPolyhedron_Alloc(a,A);
    ZB=ZPolyhedron_Alloc(b,B);
    ZC = ZDomainDifference(ZA,ZB);
    ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    break;
    
  case 10: /* ZDomainImage */
    
    ZA=ZPolyhedron_Alloc(a,A);
    ZC = ZDomainImage(ZA,b); 
    ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    break;
    
  case 11: /* ZDomainPreimage */
    
    ZA=ZPolyhedron_Alloc(a,A);
    ZC = ZDomainPreimage(ZA,b); 
    ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    break;
    
  case 12: /* ZDomainDifference */
    ZA=ZPolyhedron_Alloc(a,A);
    ZC = ZDomainPreimage(ZA,b); 
    ZD = ZDomainImage(ZC,b); 
    Zlast=ZDomainDifference(ZD,ZC);
    fprintf(stdout,"the Two zpol are equal? :%d\n",
	    isEmptyZPolyhedron(Zlast));
    break;
  
  case 13:  /* ZDomainSimplify */
    
    ZA=ZPolyhedron_Alloc(a,A);
    ZA->next = ZPolyhedron_Alloc(b,B);
    ZDomainPrint(stdout,P_VALUE_FMT, ZA);
    ZD = ZDomainSimplify(ZA);
    ZDomainPrint(stdout,P_VALUE_FMT, ZD);
    break;
    
  case 14:  /* EmptyZpolyhedron */
        
    ZA=EmptyZPolyhedron(3);
    fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
    ZDomain_Free(ZA);
    break;
    
  case 15:  /* ZDomainInclude */
  
    ZA=ZPolyhedron_Alloc(a,A);
    ZB=ZPolyhedron_Alloc(b,B);
    fprintf(stdout,"A in B  :%d \nB in A  :%d \n", 
	    ZPolyhedronIncludes(ZA,ZB),
	    ZPolyhedronIncludes(ZB,ZA));
    break;
  
  case 16: /* LatticePreimage */
        
    c = LatticePreimage(a,b);
    Matrix_Print(stdout,P_VALUE_FMT, c);
    AffineHermite(c,&d,&e);
    Matrix_Print(stdout,P_VALUE_FMT, d);
    break;
    
  case 17: /* LatticeImage */
    
    c = LatticeImage(a,b);
    Matrix_Print(stdout,P_VALUE_FMT, c);
    AffineHermite(c,&d,&e);
    Matrix_Print(stdout,P_VALUE_FMT, d);
    break;
      
  case 18:  /* EmptyLattice */
        
    fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(a));
    fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(EmptyLattice(3)));
    break;
  
  case 19:  /* CanonicalForm */
     
    ZA=ZPolyhedron_Alloc(a,A);
    ZB=ZPolyhedron_Alloc(a,B);
    CanonicalForm(ZA,&ZC,&c);
    CanonicalForm(ZB,&ZD,&d);
    ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    ZDomainPrint(stdout,P_VALUE_FMT, ZD);
    break;
    
  case 20: /* LatticeSimplify */
    
    l1=LatticeUnion_Alloc();
    l2=LatticeUnion_Alloc();
    l1->M=Matrix_Copy(a);
    l1->next=l2;
    l2->M=Matrix_Copy(b);
    l1=LatticeSimplify(l1);
    PrintLatticeUnion(stdout,P_VALUE_FMT,l1); 
    LatticeUnion_Free(l1);
    break;
    
  case 21: /* AffineSmith */
  
    AffineSmith(a,&b,&c, &d);
    Matrix_Print(stdout,P_VALUE_FMT, b); 
    Matrix_Print(stdout,P_VALUE_FMT, c);
    Matrix_Print(stdout,P_VALUE_FMT, d);
    Matrix_Free(d);
    break;
  
  case 22: /* SolveDiophantine */
        
    rank=SolveDiophantine(a,&d,&v); 
    Matrix_Print(stdout,P_VALUE_FMT, a);
    fprintf(stdout," rank: %d \n ",rank);
    Matrix_Print(stdout,P_VALUE_FMT, d); 
    Vector_Print(stdout,P_VALUE_FMT, v);
    rank=SolveDiophantine(b,&d,&v); 
    Matrix_Print(stdout,P_VALUE_FMT, b);
    fprintf(stdout," rank: %d \n ",rank);
    Matrix_Print(stdout,P_VALUE_FMT, d); 
    Vector_Print(stdout,P_VALUE_FMT, v);
    rank=SolveDiophantine(c,&d,&v); 
    Matrix_Print(stdout,P_VALUE_FMT, c);
    fprintf(stdout," rank: %d \n ",rank);
    Matrix_Print(stdout,P_VALUE_FMT, d); 
    Vector_Print(stdout,P_VALUE_FMT, v);
    Vector_Free(v);
    break;

  case 23: /* SplitZPolyhedron */
        
    ZA=ZPolyhedron_Alloc(a,A);
    ZC = SplitZpolyhedron(ZA,b);
    ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    break;


  case 100: /* debug */
    
    ZA=ZPolyhedron_Alloc(a,A);
    ZDomainPrint(stdout,P_VALUE_FMT, ZA);
    ZDomain_Free(ZA);
    break;
    
  default:
    printf("? unknown function\n");
  }

  /*    Polyhedron_Free(A); */
  if (a)
    Matrix_Free(a);
  if (b)
    Matrix_Free(b);
  if (c)
    Matrix_Free(c);

  if (A)
    Domain_Free(A);
  if (B)
    Domain_Free(B);
  if (C)
    Domain_Free(C);
  
  return 0;
} /* main */