update isl-polylib for change in pkg-config file
[barvinok.git] / test.c
blob816ee2e9b5dd936da4f1d158b9407ad779a2890e
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <barvinok/util.h>
6 #include <barvinok/barvinok.h>
7 #include <barvinok/sample.h>
8 #include "argp.h"
9 #include "progname.h"
10 #include "config.h"
11 #include "evalue_read.h"
12 #include "lattice_width.h"
14 #ifdef HAVE_SYS_TIMES_H
16 #include <sys/times.h>
18 static void time_diff(struct tms *before, struct tms *after)
20 long ticks = sysconf(_SC_CLK_TCK);
21 printf("User: %g; Sys: %g\n",
22 (0.0 + after->tms_utime - before->tms_utime) / ticks,
23 (0.0 + after->tms_stime - before->tms_stime) / ticks);
26 #else
28 struct tms {};
29 static void times(struct tms* time)
32 static void time_diff(struct tms *before, struct tms *after)
36 #endif
38 int main(int argc, char **argv)
40 int i, nbPol, nbVec, nbMat, func, j, n;
41 Polyhedron *A, *B, *C, *D, *E, *F, *G;
42 char s[128];
43 struct barvinok_options *options = barvinok_options_new_with_defaults();
45 set_program_name(argv[0]);
46 argp_parse(&barvinok_argp, argc, argv, 0, 0, options);
48 nbPol = nbVec = nbMat = 0;
49 fgets(s, 128, stdin);
50 while ((*s=='#') ||
51 ((sscanf(s, "D %d", &nbPol) < 1) &&
52 (sscanf(s, "V %d", &nbVec) < 1) &&
53 (sscanf(s, "M %d", &nbMat) < 1)))
54 fgets(s, 128, stdin);
56 for (i = 0; i < nbPol; ++i) {
57 Matrix *M = Matrix_Read();
58 A = Constraints2Polyhedron(M, options->MaxRays);
59 Matrix_Free(M);
60 fgets(s, 128, stdin);
61 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
62 fgets(s, 128, stdin);
64 switch(func) {
65 case 0: {
66 Value cb, ck;
67 value_init(cb);
68 value_init(ck);
69 fgets(s, 128, stdin);
70 /* workaround for apparent bug in older gmps */
71 *strchr(s, '\n') = '\0';
72 while ((*s=='#') || (value_read(ck, s) != 0)) {
73 fgets(s, 128, stdin);
74 /* workaround for apparent bug in older gmps */
75 *strchr(s, '\n') = '\0';
77 barvinok_count_with_options(A, &cb, options);
78 if (value_ne(cb, ck))
79 return -1;
80 value_clear(cb);
81 value_clear(ck);
82 break;
84 case 1:
85 Polyhedron_Print(stdout, P_VALUE_FMT, A);
86 B = Polyhedron_Polar(A, options->MaxRays);
87 Polyhedron_Print(stdout, P_VALUE_FMT, B);
88 C = Polyhedron_Polar(B, options->MaxRays);
89 Polyhedron_Print(stdout, P_VALUE_FMT, C);
90 Polyhedron_Free(C);
91 Polyhedron_Free(B);
92 break;
93 case 2:
94 Polyhedron_Print(stdout, P_VALUE_FMT, A);
95 for (j = 0; j < A->NbRays; ++j) {
96 B = supporting_cone(A, j);
97 Polyhedron_Print(stdout, P_VALUE_FMT, B);
98 Polyhedron_Free(B);
100 break;
101 case 3:
102 Polyhedron_Print(stdout, P_VALUE_FMT, A);
103 C = B = NULL;
104 barvinok_decompose(A,&B,&C);
105 puts("Pos:");
106 Polyhedron_Print(stdout, P_VALUE_FMT, B);
107 puts("Neg:");
108 Polyhedron_Print(stdout, P_VALUE_FMT, C);
109 Domain_Free(B);
110 Domain_Free(C);
111 break;
112 case 4: {
113 Value cm, cb;
114 struct tms tms_before, tms_between, tms_after;
115 value_init(cm);
116 value_init(cb);
117 Polyhedron_Print(stdout, P_VALUE_FMT, A);
118 times(&tms_before);
119 manual_count(A, &cm);
120 times(&tms_between);
121 barvinok_count(A, &cb, 100);
122 times(&tms_after);
123 printf("manual: ");
124 value_print(stdout, P_VALUE_FMT, cm);
125 puts("");
126 time_diff(&tms_before, &tms_between);
127 printf("Barvinok: ");
128 value_print(stdout, P_VALUE_FMT, cb);
129 puts("");
130 time_diff(&tms_between, &tms_after);
131 value_clear(cm);
132 value_clear(cb);
133 break;
135 case 5:
136 Polyhedron_Print(stdout, P_VALUE_FMT, A);
137 B = triangulate_cone(A, 100);
138 Polyhedron_Print(stdout, P_VALUE_FMT, B);
139 check_triangulization(A, B);
140 Domain_Free(B);
141 break;
142 case 6:
143 Polyhedron_Print(stdout, P_VALUE_FMT, A);
144 B = remove_equalities(A, options->MaxRays);
145 Polyhedron_Print(stdout, P_VALUE_FMT, B);
146 Polyhedron_Free(B);
147 break;
148 case 8: {
149 evalue *EP;
150 Matrix *M = Matrix_Read();
151 const char **param_name;
152 C = Constraints2Polyhedron(M, options->MaxRays);
153 Matrix_Free(M);
154 Polyhedron_Print(stdout, P_VALUE_FMT, A);
155 Polyhedron_Print(stdout, P_VALUE_FMT, C);
156 EP = barvinok_enumerate_with_options(A, C, options);
157 param_name = Read_ParamNames(stdin, C->Dimension);
158 print_evalue(stdout, EP, (const char**)param_name);
159 evalue_free(EP);
160 Polyhedron_Free(C);
162 case 9:
163 Polyhedron_Print(stdout, P_VALUE_FMT, A);
164 Polyhedron_Polarize(A);
165 C = B = NULL;
166 barvinok_decompose(A,&B,&C);
167 for (D = B; D; D = D->next)
168 Polyhedron_Polarize(D);
169 for (D = C; D; D = D->next)
170 Polyhedron_Polarize(D);
171 puts("Pos:");
172 Polyhedron_Print(stdout, P_VALUE_FMT, B);
173 puts("Neg:");
174 Polyhedron_Print(stdout, P_VALUE_FMT, C);
175 Domain_Free(B);
176 Domain_Free(C);
177 break;
178 case 10: {
179 evalue *EP;
180 Value cb, ck;
182 value_init(cb);
183 value_init(ck);
184 fgets(s, 128, stdin);
185 sscanf(s, "%d", &n);
186 for (j = 0; j < n; ++j) {
187 Polyhedron *P;
188 M = Matrix_Read();
189 P = Constraints2Polyhedron(M, options->MaxRays);
190 Matrix_Free(M);
191 A = DomainConcat(P, A);
193 fgets(s, 128, stdin);
194 /* workaround for apparent bug in older gmps */
195 *strchr(s, '\n') = '\0';
196 while ((*s=='#') || (value_read(ck, s) != 0)) {
197 fgets(s, 128, stdin);
198 /* workaround for apparent bug in older gmps */
199 *strchr(s, '\n') = '\0';
201 C = Universe_Polyhedron(0);
202 EP = barvinok_enumerate_union(A, C, options->MaxRays);
203 value_set_double(cb, compute_evalue(EP, &ck)+.25);
204 if (value_ne(cb, ck))
205 return -1;
206 Domain_Free(C);
207 value_clear(cb);
208 value_clear(ck);
209 evalue_free(EP);
210 break;
212 case 11: {
213 evalue *expected, *computed;
214 unsigned nvar, nparam;
215 const char **pp;
217 expected = evalue_read_from_file(stdin, NULL, &pp, &nvar, &nparam,
218 options->MaxRays);
219 C = Universe_Polyhedron(0);
220 computed = Polyhedron_Lattice_Width(A, C, options);
221 assert(value_zero_p(computed->d));
222 assert(computed->x.p->type == partition);
223 if (!eequal(expected, &computed->x.p->arr[1]))
224 return -1;
225 free(pp);
226 Domain_Free(C);
227 evalue_free(computed);
228 evalue_free(expected);
229 break;
231 case 12: {
232 Vector *sample;
233 int has_sample;
234 fgets(s, 128, stdin);
235 sscanf(s, "%d", &has_sample);
237 sample = Polyhedron_Sample(A, options);
238 if (!sample && has_sample)
239 return -1;
240 if (sample && !has_sample)
241 return -1;
242 if (sample && !in_domain(A, sample->p))
243 return -1;
244 Vector_Free(sample);
247 Domain_Free(A);
249 for (i = 0; i < nbVec; ++i) {
250 int ok;
251 Vector *V = Vector_Read();
252 Matrix *M = Matrix_Alloc(V->Size, V->Size);
253 Vector_Copy(V->p, M->p[0], V->Size);
254 ok = unimodular_complete(M, 1);
255 assert(ok);
256 Matrix_Print(stdout, P_VALUE_FMT, M);
257 Matrix_Free(M);
258 Vector_Free(V);
260 for (i = 0; i < nbMat; ++i) {
261 Matrix *U, *V, *S;
262 Matrix *M = Matrix_Read();
263 Smith(M, &U, &V, &S);
264 Matrix_Print(stdout, P_VALUE_FMT, U);
265 Matrix_Print(stdout, P_VALUE_FMT, V);
266 Matrix_Print(stdout, P_VALUE_FMT, S);
267 Matrix_Free(M);
268 Matrix_Free(U);
269 Matrix_Free(V);
270 Matrix_Free(S);
273 barvinok_options_free(options);
274 return 0;