polylib: Zpolytest.c Source File

- Main Page
- Classes
- Files

- File List
- File Members

# Zpolytest.c

Go to the documentation of this file.

    00001 /* zpolytest.c
    00002 This is a testbench for the Zpolylib (part of polylib manipulating
    00003 Z-polyhedra. */
    00004 /*
    00005     This file is part of PolyLib.
    00006
    00007     PolyLib is free software: you can redistribute it and/or modify
    00008     it under the terms of the GNU General Public License as published by
    00009     the Free Software Foundation, either version 3 of the License, or
    00010     (at your option) any later version.
    00011
    00012     PolyLib is distributed in the hope that it will be useful,
    00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015     GNU General Public License for more details.
    00016
    00017     You should have received a copy of the GNU General Public License
    00018     along with PolyLib.  If not, see <http://www.gnu.org/licenses/>.
    00019 */
    00020
    00021
    00022 #include <stdio.h>
    00023 #include <polylib/polylib.h>
    00024
    00025 #define WS 0
    00026
    00027 char s[128];
    00028
    00029 int main() {
    00030
    00031   Matrix *a=NULL, *b=NULL, *c=NULL, *d, *e, *g;
    00032   LatticeUnion *l1,*l2,*l3,*l4,*temp;
    00033   Polyhedron *A=NULL, *B=NULL, *C=NULL, *D;
    00034   ZPolyhedron *ZA, *ZB, *ZC, *ZD, *Zlast;
    00035   int  nbPol, nbMat, func, rank ;
    00036   Vector *v=NULL;
    00037
    00038   /* The structure of the input file to this program  is the following:
    00039        First a line containing
    00040            M nbMat
    00041        Where nbMat is an integer indicating how many Matrices will
    00042        be described in the following. temporary debugging. Next
    00043        the matrice are described. For each matrix, the first row is two
    00044        integers:
    00045            nbRows nbColumns
    00046        Then the matrix is written row by row. a line starting with
    00047       a `#' is considered as a comment
    00048
    00049       Then a line containing
    00050            D nbDomain
    00051       where nbDomain is an integer indicating how many domain will
    00052        be described in the following. Domains are describled as for
    00053        polylib,  the first row is two integers:
    00054           nbConstraints dimension
    00055       then the constraints are described in the Polylib format.
    00056       The last line of the input file contains :
    00057           F numTest
    00058           which indicates which test will be performed on the data.
    00059       Warning, currently no more than 3 matrice of Polyhedra can be read*/
    00060
    00061   fgets(s, 128, stdin);
    00062   nbPol = nbMat = 0;
    00063   while ( (*s=='#') ||
    00064           ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
    00065     fgets(s, 128, stdin);
    00066
    00067
    00068   /* debug */
    00069   /*     fprintf(stdout,"nbMat=%d",nbMat);fflush(stdout); */
    00070
    00071   switch (nbMat) {
    00072
    00073   case 1:
    00074     a = Matrix_Read();
    00075     break;
    00076
    00077   case 2:
    00078     a = Matrix_Read();
    00079     b = Matrix_Read();
    00080     break;
    00081
    00082   case 3: a = Matrix_Read();
    00083     b = Matrix_Read();
    00084     c = Matrix_Read();
    00085     break;
    00086   }
    00087
    00088   fgets(s, 128, stdin);
    00089   while ((*s=='#') ||
    00090          ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
    00091     fgets(s, 128, stdin);
    00092
    00093
    00094   /* debug */
    00095   /*  fprintf(stdout,"nbPol=%d",nbPol);fflush(stdout);  */
    00096
    00097   switch (nbPol) {
    00098
    00099   case 1:
    00100     g = Matrix_Read();
    00101     A = Constraints2Polyhedron(g,WS);
    00102     Matrix_Free(g);
    00103     break;
    00104
    00105   case 2:
    00106     g = Matrix_Read();
    00107     A = Constraints2Polyhedron(g,WS);
    00108     Matrix_Free(g);
    00109     g = Matrix_Read();
    00110     B = Constraints2Polyhedron(g,WS);
    00111     Matrix_Free(g);
    00112     break;
    00113
    00114   case 3:
    00115     g = Matrix_Read();
    00116     A = Constraints2Polyhedron(g,WS);
    00117     Matrix_Free(g);
    00118     g = Matrix_Read();
    00119     B = Constraints2Polyhedron(g,WS);
    00120     Matrix_Free(g);
    00121     g = Matrix_Read();
    00122     C = Constraints2Polyhedron(g,WS);
    00123     Matrix_Free(g);
    00124     break;
    00125   }
    00126
    00127   fgets(s, 128, stdin);
    00128   while ((*s=='#') || (sscanf(s, "F %d", &func)<1) ) fgets(s, 128, stdin);
    00129
    00130
    00131   switch (func) {
    00132
    00133   case 1:
    00134
    00135     /* just a  test of polylib functions */
    00136     C = DomainUnion(A, B, 200);
    00137     D = DomainConvex(C, 200);
    00138     d = Polyhedron2Constraints(D);
    00139     Matrix_Print(stdout,P_VALUE_FMT, d);
    00140     Matrix_Free(d);
    00141     Domain_Free(D);
    00142     break;
    00143
    00144   case 2: /* AffineHermite */
    00145
    00146     AffineHermite(a,&b,&c);
    00147     Matrix_Print(stdout,P_VALUE_FMT, b);
    00148     Matrix_Print(stdout,P_VALUE_FMT, c);
    00149     break;
    00150
    00151   case 3: /* LatticeIntersection */
    00152
    00153     c = LatticeIntersection(a,b);
    00154     Matrix_Print(stdout,P_VALUE_FMT, c);
    00155     break;
    00156
    00157   case 4: /* LatticeDifference */
    00158
    00159     fprintf(stdout," 2 in 1 : %d\n",LatticeIncludes(b,a));
    00160     fprintf(stdout," 1 in 3 : %d\n",LatticeIncludes(c,a));
    00161     fprintf(stdout," 1 in 2 : %d\n",LatticeIncludes(a,b));
    00162     break;
    00163
    00164   case 5: /* LatticeDifference */
    00165
    00166     l1=LatticeDifference(a,b);
    00167     l2=LatticeDifference(b,a);
    00168     l3=LatticeDifference(c,a);
    00169     l4=LatticeDifference(b,c);
    00170     fprintf(stdout,"L1 - L2 :\n");
    00171     temp=l1;
    00172     while (temp!=NULL) {
    00173
    00174       Matrix_Print(stdout,P_VALUE_FMT,temp->M);
    00175       temp=temp->next;
    00176     };
    00177     fprintf(stdout,"Diff2:\n");
    00178     temp=l2;
    00179     while (temp!=NULL) {
    00180       Matrix_Print(stdout,P_VALUE_FMT, temp->M);
    00181       temp=temp->next;
    00182     };
    00183     fprintf(stdout,"Diff3:\n");
    00184     temp=l3;
    00185     while (temp!=NULL) {
    00186       Matrix_Print(stdout,P_VALUE_FMT, temp->M);
    00187       temp=temp->next;
    00188     };
    00189     fprintf(stdout,"Diff4:\n");
    00190     temp=l4;
    00191     while (temp!=NULL) {
    00192       Matrix_Print(stdout,P_VALUE_FMT, temp->M);
    00193       temp=temp->next;
    00194     };
    00195     break;
    00196
    00197   case 6: /* isEmptyZPolyhedron */
    00198
    00199     ZA=ZPolyhedron_Alloc(a,A);
    00200     fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
    00201     ZDomain_Free(ZA);
    00202     break;
    00203
    00204   case 7: /* ZDomainIntersection */
    00205
    00206     ZA=ZPolyhedron_Alloc(a,A);
    00207     ZB=ZPolyhedron_Alloc(b,B);
    00208     ZC = ZDomainIntersection(ZA,ZB);
    00209     ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    00210     ZDomain_Free(ZA);
    00211     ZDomain_Free(ZB);
    00212     ZDomain_Free(ZC);
    00213     break;
    00214
    00215   case 8: /* ZDomainUnion */
    00216
    00217     ZA=ZPolyhedron_Alloc(a,A);
    00218     ZB=ZPolyhedron_Alloc(b,B);
    00219     ZC = ZDomainUnion(ZA,ZB);
    00220     ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    00221     break;
    00222
    00223   case 9: /* ZDomainDifference */
    00224
    00225     ZA=ZPolyhedron_Alloc(a,A);
    00226     ZB=ZPolyhedron_Alloc(b,B);
    00227     ZC = ZDomainDifference(ZA,ZB);
    00228     ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    00229     break;
    00230
    00231   case 10: /* ZDomainImage */
    00232
    00233     ZA=ZPolyhedron_Alloc(a,A);
    00234     ZC = ZDomainImage(ZA,b);
    00235     ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    00236     break;
    00237
    00238   case 11: /* ZDomainPreimage */
    00239
    00240     ZA=ZPolyhedron_Alloc(a,A);
    00241     ZC = ZDomainPreimage(ZA,b);
    00242     ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    00243     break;
    00244
    00245   case 12: /* ZDomainDifference */
    00246     ZA=ZPolyhedron_Alloc(a,A);
    00247     ZC = ZDomainPreimage(ZA,b);
    00248     ZD = ZDomainImage(ZC,b);
    00249     Zlast=ZDomainDifference(ZD,ZC);
    00250     fprintf(stdout,"the Two zpol are equal? :%d\n",
    00251             isEmptyZPolyhedron(Zlast));
    00252     break;
    00253
    00254   case 13:  /* ZDomainSimplify */
    00255
    00256     ZA=ZPolyhedron_Alloc(a,A);
    00257     ZA->next = ZPolyhedron_Alloc(b,B);
    00258     ZDomainPrint(stdout,P_VALUE_FMT, ZA);
    00259     ZD = ZDomainSimplify(ZA);
    00260     ZDomainPrint(stdout,P_VALUE_FMT, ZD);
    00261     break;
    00262
    00263   case 14:  /* EmptyZpolyhedron */
    00264
    00265     ZA=EmptyZPolyhedron(3);
    00266     fprintf(stdout,"is Empty? :%d \n", isEmptyZPolyhedron(ZA));
    00267     ZDomain_Free(ZA);
    00268     break;
    00269
    00270   case 15:  /* ZDomainInclude */
    00271
    00272     ZA=ZPolyhedron_Alloc(a,A);
    00273     ZB=ZPolyhedron_Alloc(b,B);
    00274     fprintf(stdout,"A in B  :%d \nB in A  :%d \n",
    00275             ZPolyhedronIncludes(ZA,ZB),
    00276             ZPolyhedronIncludes(ZB,ZA));
    00277     break;
    00278
    00279   case 16: /* LatticePreimage */
    00280
    00281     c = LatticePreimage(a,b);
    00282     Matrix_Print(stdout,P_VALUE_FMT, c);
    00283     AffineHermite(c,&d,&e);
    00284     Matrix_Print(stdout,P_VALUE_FMT, d);
    00285     break;
    00286
    00287   case 17: /* LatticeImage */
    00288
    00289     c = LatticeImage(a,b);
    00290     Matrix_Print(stdout,P_VALUE_FMT, c);
    00291     AffineHermite(c,&d,&e);
    00292     Matrix_Print(stdout,P_VALUE_FMT, d);
    00293     break;
    00294
    00295   case 18:  /* EmptyLattice */
    00296
    00297     fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(a));
    00298     fprintf(stdout,"is Empty? :%d \n", isEmptyLattice(EmptyLattice(3)));
    00299     break;
    00300
    00301   case 19:  /* CanonicalForm */
    00302
    00303     ZA=ZPolyhedron_Alloc(a,A);
    00304     ZB=ZPolyhedron_Alloc(a,B);
    00305     CanonicalForm(ZA,&ZC,&c);
    00306     CanonicalForm(ZB,&ZD,&d);
    00307     ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    00308     ZDomainPrint(stdout,P_VALUE_FMT, ZD);
    00309     break;
    00310
    00311   case 20: /* LatticeSimplify */
    00312
    00313     l1=LatticeUnion_Alloc();
    00314     l2=LatticeUnion_Alloc();
    00315     l1->M=Matrix_Copy(a);
    00316     l1->next=l2;
    00317     l2->M=Matrix_Copy(b);
    00318     l1=LatticeSimplify(l1);
    00319     PrintLatticeUnion(stdout,P_VALUE_FMT,l1);
    00320     LatticeUnion_Free(l1);
    00321     break;
    00322
    00323   case 21: /* AffineSmith */
    00324
    00325     AffineSmith(a,&b,&c, &d);
    00326     Matrix_Print(stdout,P_VALUE_FMT, b);
    00327     Matrix_Print(stdout,P_VALUE_FMT, c);
    00328     Matrix_Print(stdout,P_VALUE_FMT, d);
    00329     Matrix_Free(d);
    00330     break;
    00331
    00332   case 22: /* SolveDiophantine */
    00333
    00334     rank=SolveDiophantine(a,&d,&v);
    00335     Matrix_Print(stdout,P_VALUE_FMT, a);
    00336     fprintf(stdout," rank: %d \n ",rank);
    00337     Matrix_Print(stdout,P_VALUE_FMT, d);
    00338     Vector_Print(stdout,P_VALUE_FMT, v);
    00339     rank=SolveDiophantine(b,&d,&v);
    00340     Matrix_Print(stdout,P_VALUE_FMT, b);
    00341     fprintf(stdout," rank: %d \n ",rank);
    00342     Matrix_Print(stdout,P_VALUE_FMT, d);
    00343     Vector_Print(stdout,P_VALUE_FMT, v);
    00344     rank=SolveDiophantine(c,&d,&v);
    00345     Matrix_Print(stdout,P_VALUE_FMT, c);
    00346     fprintf(stdout," rank: %d \n ",rank);
    00347     Matrix_Print(stdout,P_VALUE_FMT, d);
    00348     Vector_Print(stdout,P_VALUE_FMT, v);
    00349     Vector_Free(v);
    00350     break;
    00351
    00352   case 23: /* SplitZPolyhedron */
    00353
    00354     ZA=ZPolyhedron_Alloc(a,A);
    00355     ZC = SplitZpolyhedron(ZA,b);
    00356     ZDomainPrint(stdout,P_VALUE_FMT, ZC);
    00357     break;
    00358
    00359
    00360   case 100: /* debug */
    00361
    00362     ZA=ZPolyhedron_Alloc(a,A);
    00363     ZDomainPrint(stdout,P_VALUE_FMT, ZA);
    00364     ZDomain_Free(ZA);
    00365     break;
    00366
    00367   default:
    00368     printf("? unknown function\n");
    00369   }
    00370
    00371   /*    Polyhedron_Free(A); */
    00372   if (a)
    00373     Matrix_Free(a);
    00374   if (b)
    00375     Matrix_Free(b);
    00376   if (c)
    00377     Matrix_Free(c);
    00378
    00379   if (A)
    00380     Domain_Free(A);
    00381   if (B)
    00382     Domain_Free(B);
    00383   if (C)
    00384     Domain_Free(C);
    00385
    00386   return 0;
    00387 } /* main */