1 /* polytest.c */
2 /*
3     This file is part of PolyLib.
4 
5     PolyLib is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     PolyLib is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with PolyLib.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <stdio.h>
20 #include <polylib/polylib.h>
21 
22 #define WS 0
23 
24 char s[128];
25 
main()26 int main() {
27 
28   Matrix *a=NULL, *b=NULL, *c, *d, *e, *f;
29   Polyhedron *A, *B, *C, *D, *last, *tmp;
30   int i, nbPol, nbMat, func;
31 
32   fgets(s, 128, stdin);
33   nbPol = nbMat = 0;
34   while ((*s=='#') ||
35 	  ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
36     fgets(s, 128, stdin);
37 
38   for (i=0, A=last=(Polyhedron *)0; i<nbPol; i++) {
39     a = Matrix_Read();
40     tmp = Constraints2Polyhedron(a,WS);
41     Matrix_Free(a);
42     if (!last) A = last = tmp;
43     else {
44       last->next = tmp;
45       last = tmp;
46     }
47     }
48 
49     if (nbMat)
50     {  a = Matrix_Read(); }
51 
52     fgets(s,128,stdin);
53     nbPol = nbMat = 0;
54     while ( (*s=='#') ||
55         ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "M %d", &nbMat)<1)) )
56       fgets(s, 128, stdin);
57 
58     for (i=0, B=last=(Polyhedron *)0; i<nbPol; i++) {
59       b = Matrix_Read();
60       tmp = Constraints2Polyhedron(b,WS);
61       Matrix_Free(b);
62       if (!last) B = last = tmp;
63       else {
64 	last->next = tmp;
65 	last = tmp;
66       }
67     }
68 
69     if (nbMat)
70       {  b = Matrix_Read(); }
71 
72     fgets(s, 128, stdin);
73     while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
74       fgets(s, 128, stdin);
75 
76     switch (func) {
77     case 1:
78       C = DomainUnion(A, B, WS);
79       D = DomainConvex(C, WS);
80       d = Polyhedron2Constraints(D);
81       Matrix_Print(stdout,P_VALUE_FMT,d);
82       Matrix_Free(d);
83       Domain_Free(C);
84       Domain_Free(D);
85       break;
86     case 2:
87       D = DomainSimplify(A, B, WS);
88       d = Polyhedron2Constraints(D);
89       Matrix_Print(stdout,P_VALUE_FMT,d);
90       Matrix_Free(d);
91       Domain_Free(D);
92       break;
93     case 3:
94       a = Polyhedron2Constraints(A);
95       Matrix_Print(stdout,P_VALUE_FMT,a);
96       b = Polyhedron2Constraints(B);
97       Matrix_Print(stdout,P_VALUE_FMT,b);
98       break;
99     case 4:
100       a = Polyhedron2Rays(A);
101       Matrix_Print(stdout,P_VALUE_FMT,a);
102       break;
103     case 5:
104 
105       /* a = ec , da = c , ed = 1 */
106       right_hermite(a,&c,&d,&e);
107       Matrix_Print(stdout,P_VALUE_FMT,c);
108       Matrix_Print(stdout,P_VALUE_FMT,d);
109       Matrix_Print(stdout,P_VALUE_FMT,e);
110       f = Matrix_Alloc(e->NbRows,c->NbColumns);
111       Matrix_Product(e,c,f);
112       Matrix_Print(stdout,P_VALUE_FMT,f);
113       Matrix_Free(f);
114       f = Matrix_Alloc(d->NbRows,a->NbColumns);
115       Matrix_Product(d,a,f);
116       Matrix_Print(stdout,P_VALUE_FMT,f);
117       Matrix_Free(f);
118       f = Matrix_Alloc(e->NbRows, d->NbColumns);
119       Matrix_Product(e,d,f);
120       Matrix_Print(stdout,P_VALUE_FMT,f);
121       break;
122     case 6:
123 
124       /* a = ce , ad = c , de = 1 */
125       left_hermite(a,&c,&d,&e);
126       Matrix_Print(stdout,P_VALUE_FMT,c);
127       Matrix_Print(stdout,P_VALUE_FMT,d);
128       Matrix_Print(stdout,P_VALUE_FMT,e);
129       f = Matrix_Alloc(c->NbRows, e->NbColumns);
130       Matrix_Product(c,e,f);
131       Matrix_Print(stdout,P_VALUE_FMT,f);
132       Matrix_Free(f);
133       f = Matrix_Alloc(a->NbRows, d->NbColumns);
134       Matrix_Product(a,d,f);
135       Matrix_Print(stdout,P_VALUE_FMT,f);
136       Matrix_Free(f);
137       f = Matrix_Alloc(d->NbRows, e->NbColumns);
138       Matrix_Product(d,e,f);
139       Matrix_Print(stdout,P_VALUE_FMT,f);
140       break;
141     case 7:
142 
143       /* Polyhedron_Print(stdout,"%5d", A); */
144       /* Matrix_Print(stdout,"%4d", b);     */
145 
146       C = Polyhedron_Image(A, b, WS);
147       Polyhedron_Print(stdout,P_VALUE_FMT,C);
148       break;
149     case 8:
150 
151       printf("%s\n",
152 	     Polyhedron_Not_Empty(A,B,WS) ? "Not Empty" : "Empty");
153       break;
154     case 9:
155 
156       i = PolyhedronLTQ(A,B,1,0,WS);
157       printf("%s\n",
158 	     i==-1 ? "A<B" : i==1 ? "A>B" : i==0 ? "A><B" : "error");
159       i = PolyhedronLTQ(B,A,1,0,WS);
160       printf("%s\n",
161 	     i==-1 ? "A<B" : i==1 ? "A>B" : i==0 ? "A><B" : "error");
162       break;
163     case 10:
164       i = GaussSimplify(a,b);
165       Matrix_Print(stdout,P_VALUE_FMT,b);
166       break;
167 
168     default:
169       printf("? unknown function\n");
170     }
171 
172     Domain_Free(A);
173     Domain_Free(B);
174 
175     return 0;
176 }
177 
178 
179