barvinok.cc: basic_map_card: rename "dim" variable to "space"
[barvinok.git] / test.c
blob550accd5d7da9295a83c58e317915b6693d00b08
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <isl_set_polylib.h>
6 #include <barvinok/util.h>
7 #include <barvinok/barvinok.h>
8 #include <barvinok/sample.h>
9 #include "config.h"
11 #ifdef HAVE_SYS_TIMES_H
13 #include <sys/times.h>
15 static void time_diff(struct tms *before, struct tms *after)
17 long ticks = sysconf(_SC_CLK_TCK);
18 printf("User: %g; Sys: %g\n",
19 (0.0 + after->tms_utime - before->tms_utime) / ticks,
20 (0.0 + after->tms_stime - before->tms_stime) / ticks);
23 #else
25 struct tms {};
26 static void times(struct tms* time)
29 static void time_diff(struct tms *before, struct tms *after)
33 #endif
35 int main(int argc, char **argv)
37 isl_ctx *ctx;
38 int i, nbPol, nbVec, nbMat, func, j, n;
39 Polyhedron *A, *B, *C, *D;
40 char s[128];
41 struct barvinok_options *options = barvinok_options_new_with_defaults();
43 argc = barvinok_options_parse(options, argc, argv, ISL_ARG_ALL);
44 ctx = isl_ctx_alloc_with_options(&barvinok_options_args, options);
46 nbPol = nbVec = nbMat = 0;
47 fgets(s, 128, stdin);
48 while ((*s=='#') ||
49 ((sscanf(s, "D %d", &nbPol) < 1) &&
50 (sscanf(s, "V %d", &nbVec) < 1) &&
51 (sscanf(s, "M %d", &nbMat) < 1)))
52 fgets(s, 128, stdin);
54 for (i = 0; i < nbPol; ++i) {
55 Matrix *M = Matrix_Read();
56 A = Constraints2Polyhedron(M, options->MaxRays);
57 Matrix_Free(M);
58 fgets(s, 128, stdin);
59 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
60 fgets(s, 128, stdin);
62 switch(func) {
63 case 0: {
64 Value cb, ck;
65 value_init(cb);
66 value_init(ck);
67 fgets(s, 128, stdin);
68 /* workaround for apparent bug in older gmps */
69 *strchr(s, '\n') = '\0';
70 while ((*s=='#') || (value_read(ck, s) != 0)) {
71 fgets(s, 128, stdin);
72 /* workaround for apparent bug in older gmps */
73 *strchr(s, '\n') = '\0';
75 barvinok_count_with_options(A, &cb, options);
76 if (value_ne(cb, ck))
77 return -1;
78 value_clear(cb);
79 value_clear(ck);
80 break;
82 case 1:
83 Polyhedron_Print(stdout, P_VALUE_FMT, A);
84 B = Polyhedron_Polar(A, options->MaxRays);
85 Polyhedron_Print(stdout, P_VALUE_FMT, B);
86 C = Polyhedron_Polar(B, options->MaxRays);
87 Polyhedron_Print(stdout, P_VALUE_FMT, C);
88 Polyhedron_Free(C);
89 Polyhedron_Free(B);
90 break;
91 case 2:
92 Polyhedron_Print(stdout, P_VALUE_FMT, A);
93 for (j = 0; j < A->NbRays; ++j) {
94 B = supporting_cone(A, j);
95 Polyhedron_Print(stdout, P_VALUE_FMT, B);
96 Polyhedron_Free(B);
98 break;
99 case 3:
100 Polyhedron_Print(stdout, P_VALUE_FMT, A);
101 C = B = NULL;
102 barvinok_decompose(A,&B,&C);
103 puts("Pos:");
104 Polyhedron_Print(stdout, P_VALUE_FMT, B);
105 puts("Neg:");
106 Polyhedron_Print(stdout, P_VALUE_FMT, C);
107 Domain_Free(B);
108 Domain_Free(C);
109 break;
110 case 4: {
111 Value cm, cb;
112 struct tms tms_before, tms_between, tms_after;
113 value_init(cm);
114 value_init(cb);
115 Polyhedron_Print(stdout, P_VALUE_FMT, A);
116 times(&tms_before);
117 manual_count(A, &cm);
118 times(&tms_between);
119 barvinok_count(A, &cb, 100);
120 times(&tms_after);
121 printf("manual: ");
122 value_print(stdout, P_VALUE_FMT, cm);
123 puts("");
124 time_diff(&tms_before, &tms_between);
125 printf("Barvinok: ");
126 value_print(stdout, P_VALUE_FMT, cb);
127 puts("");
128 time_diff(&tms_between, &tms_after);
129 value_clear(cm);
130 value_clear(cb);
131 break;
133 case 5:
134 Polyhedron_Print(stdout, P_VALUE_FMT, A);
135 B = triangulate_cone(A, 100);
136 Polyhedron_Print(stdout, P_VALUE_FMT, B);
137 check_triangulization(A, B);
138 Domain_Free(B);
139 break;
140 case 6:
141 Polyhedron_Print(stdout, P_VALUE_FMT, A);
142 B = remove_equalities(A, options->MaxRays);
143 Polyhedron_Print(stdout, P_VALUE_FMT, B);
144 Polyhedron_Free(B);
145 break;
146 case 8: {
147 evalue *EP;
148 Matrix *M = Matrix_Read();
149 const char **param_name;
150 C = Constraints2Polyhedron(M, options->MaxRays);
151 Matrix_Free(M);
152 Polyhedron_Print(stdout, P_VALUE_FMT, A);
153 Polyhedron_Print(stdout, P_VALUE_FMT, C);
154 EP = barvinok_enumerate_with_options(A, C, options);
155 param_name = Read_ParamNames(stdin, C->Dimension);
156 print_evalue(stdout, EP, (const char**)param_name);
157 evalue_free(EP);
158 Polyhedron_Free(C);
160 case 9:
161 Polyhedron_Print(stdout, P_VALUE_FMT, A);
162 Polyhedron_Polarize(A);
163 C = B = NULL;
164 barvinok_decompose(A,&B,&C);
165 for (D = B; D; D = D->next)
166 Polyhedron_Polarize(D);
167 for (D = C; D; D = D->next)
168 Polyhedron_Polarize(D);
169 puts("Pos:");
170 Polyhedron_Print(stdout, P_VALUE_FMT, B);
171 puts("Neg:");
172 Polyhedron_Print(stdout, P_VALUE_FMT, C);
173 Domain_Free(B);
174 Domain_Free(C);
175 break;
176 case 10: {
177 evalue *EP;
178 Value cb, ck;
180 value_init(cb);
181 value_init(ck);
182 fgets(s, 128, stdin);
183 sscanf(s, "%d", &n);
184 for (j = 0; j < n; ++j) {
185 Polyhedron *P;
186 M = Matrix_Read();
187 P = Constraints2Polyhedron(M, options->MaxRays);
188 Matrix_Free(M);
189 A = DomainConcat(P, A);
191 fgets(s, 128, stdin);
192 /* workaround for apparent bug in older gmps */
193 *strchr(s, '\n') = '\0';
194 while ((*s=='#') || (value_read(ck, s) != 0)) {
195 fgets(s, 128, stdin);
196 /* workaround for apparent bug in older gmps */
197 *strchr(s, '\n') = '\0';
199 C = Universe_Polyhedron(0);
200 EP = barvinok_enumerate_union(A, C, options->MaxRays);
201 value_set_double(cb, compute_evalue(EP, &ck)+.25);
202 if (value_ne(cb, ck))
203 return -1;
204 Domain_Free(C);
205 value_clear(cb);
206 value_clear(ck);
207 evalue_free(EP);
208 break;
210 case 12: {
211 Vector *sample;
212 int has_sample;
213 fgets(s, 128, stdin);
214 sscanf(s, "%d", &has_sample);
216 sample = Polyhedron_Sample(A, options);
217 if (!sample && has_sample)
218 return -1;
219 if (sample && !has_sample)
220 return -1;
221 if (sample && !in_domain(A, sample->p))
222 return -1;
223 Vector_Free(sample);
226 Domain_Free(A);
228 for (i = 0; i < nbVec; ++i) {
229 int ok;
230 Vector *V = Vector_Read();
231 Matrix *M = Matrix_Alloc(V->Size, V->Size);
232 Vector_Copy(V->p, M->p[0], V->Size);
233 ok = unimodular_complete(M, 1);
234 assert(ok);
235 Matrix_Print(stdout, P_VALUE_FMT, M);
236 Matrix_Free(M);
237 Vector_Free(V);
239 for (i = 0; i < nbMat; ++i) {
240 Matrix *U, *V, *S;
241 Matrix *M = Matrix_Read();
242 Smith(M, &U, &V, &S);
243 Matrix_Print(stdout, P_VALUE_FMT, U);
244 Matrix_Print(stdout, P_VALUE_FMT, V);
245 Matrix_Print(stdout, P_VALUE_FMT, S);
246 Matrix_Free(M);
247 Matrix_Free(U);
248 Matrix_Free(V);
249 Matrix_Free(S);
252 isl_ctx_free(ctx);
253 return 0;