Bernoulli_sum_evalue: cut off some redundant parts where sum should be zero
[barvinok.git] / test.c
blob1b88f39d49322e947fbdef0274cb2133ae8a3fcd
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 "argp.h"
8 #include "progname.h"
9 #include "config.h"
10 #include "evalue_read.h"
11 #include "lattice_width.h"
13 #ifdef HAVE_SYS_TIMES_H
15 #include <sys/times.h>
17 static void time_diff(struct tms *before, struct tms *after)
19 long ticks = sysconf(_SC_CLK_TCK);
20 printf("User: %g; Sys: %g\n",
21 (0.0 + after->tms_utime - before->tms_utime) / ticks,
22 (0.0 + after->tms_stime - before->tms_stime) / ticks);
25 #else
27 struct tms {};
28 static void times(struct tms* time)
31 static void time_diff(struct tms *before, struct tms *after)
35 #endif
37 int main(int argc, char **argv)
39 int i, nbPol, nbVec, nbMat, func, j, n;
40 Polyhedron *A, *B, *C, *D, *E, *F, *G;
41 char s[128];
42 struct barvinok_options *options = barvinok_options_new_with_defaults();
44 set_program_name(argv[0]);
45 argp_parse(&barvinok_argp, argc, argv, 0, 0, options);
47 nbPol = nbVec = nbMat = 0;
48 fgets(s, 128, stdin);
49 while ((*s=='#') ||
50 ((sscanf(s, "D %d", &nbPol) < 1) &&
51 (sscanf(s, "V %d", &nbVec) < 1) &&
52 (sscanf(s, "M %d", &nbMat) < 1)))
53 fgets(s, 128, stdin);
55 for (i = 0; i < nbPol; ++i) {
56 Matrix *M = Matrix_Read();
57 A = Constraints2Polyhedron(M, options->MaxRays);
58 Matrix_Free(M);
59 fgets(s, 128, stdin);
60 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
61 fgets(s, 128, stdin);
63 switch(func) {
64 case 0: {
65 Value cb, ck;
66 value_init(cb);
67 value_init(ck);
68 fgets(s, 128, stdin);
69 /* workaround for apparent bug in older gmps */
70 *strchr(s, '\n') = '\0';
71 while ((*s=='#') || (value_read(ck, s) != 0)) {
72 fgets(s, 128, stdin);
73 /* workaround for apparent bug in older gmps */
74 *strchr(s, '\n') = '\0';
76 barvinok_count_with_options(A, &cb, options);
77 if (value_ne(cb, ck))
78 return -1;
79 value_clear(cb);
80 value_clear(ck);
81 break;
83 case 1:
84 Polyhedron_Print(stdout, P_VALUE_FMT, A);
85 B = Polyhedron_Polar(A, options->MaxRays);
86 Polyhedron_Print(stdout, P_VALUE_FMT, B);
87 C = Polyhedron_Polar(B, options->MaxRays);
88 Polyhedron_Print(stdout, P_VALUE_FMT, C);
89 Polyhedron_Free(C);
90 Polyhedron_Free(B);
91 break;
92 case 2:
93 Polyhedron_Print(stdout, P_VALUE_FMT, A);
94 for (j = 0; j < A->NbRays; ++j) {
95 B = supporting_cone(A, j);
96 Polyhedron_Print(stdout, P_VALUE_FMT, B);
97 Polyhedron_Free(B);
99 break;
100 case 3:
101 Polyhedron_Print(stdout, P_VALUE_FMT, A);
102 C = B = NULL;
103 barvinok_decompose(A,&B,&C);
104 puts("Pos:");
105 Polyhedron_Print(stdout, P_VALUE_FMT, B);
106 puts("Neg:");
107 Polyhedron_Print(stdout, P_VALUE_FMT, C);
108 Domain_Free(B);
109 Domain_Free(C);
110 break;
111 case 4: {
112 Value cm, cb;
113 struct tms tms_before, tms_between, tms_after;
114 value_init(cm);
115 value_init(cb);
116 Polyhedron_Print(stdout, P_VALUE_FMT, A);
117 times(&tms_before);
118 manual_count(A, &cm);
119 times(&tms_between);
120 barvinok_count(A, &cb, 100);
121 times(&tms_after);
122 printf("manual: ");
123 value_print(stdout, P_VALUE_FMT, cm);
124 puts("");
125 time_diff(&tms_before, &tms_between);
126 printf("Barvinok: ");
127 value_print(stdout, P_VALUE_FMT, cb);
128 puts("");
129 time_diff(&tms_between, &tms_after);
130 value_clear(cm);
131 value_clear(cb);
132 break;
134 case 5:
135 Polyhedron_Print(stdout, P_VALUE_FMT, A);
136 B = triangulate_cone(A, 100);
137 Polyhedron_Print(stdout, P_VALUE_FMT, B);
138 check_triangulization(A, B);
139 Domain_Free(B);
140 break;
141 case 6:
142 Polyhedron_Print(stdout, P_VALUE_FMT, A);
143 B = remove_equalities(A, options->MaxRays);
144 Polyhedron_Print(stdout, P_VALUE_FMT, B);
145 Polyhedron_Free(B);
146 break;
147 case 8: {
148 evalue *EP;
149 Matrix *M = Matrix_Read();
150 char **param_name;
151 C = Constraints2Polyhedron(M, options->MaxRays);
152 Matrix_Free(M);
153 Polyhedron_Print(stdout, P_VALUE_FMT, A);
154 Polyhedron_Print(stdout, P_VALUE_FMT, C);
155 EP = barvinok_enumerate_with_options(A, C, options);
156 param_name = Read_ParamNames(stdin, C->Dimension);
157 print_evalue(stdout, EP, (const char**)param_name);
158 evalue_free(EP);
159 Polyhedron_Free(C);
161 case 9:
162 Polyhedron_Print(stdout, P_VALUE_FMT, A);
163 Polyhedron_Polarize(A);
164 C = B = NULL;
165 barvinok_decompose(A,&B,&C);
166 for (D = B; D; D = D->next)
167 Polyhedron_Polarize(D);
168 for (D = C; D; D = D->next)
169 Polyhedron_Polarize(D);
170 puts("Pos:");
171 Polyhedron_Print(stdout, P_VALUE_FMT, B);
172 puts("Neg:");
173 Polyhedron_Print(stdout, P_VALUE_FMT, C);
174 Domain_Free(B);
175 Domain_Free(C);
176 break;
177 case 10: {
178 evalue *EP;
179 Value cb, ck;
181 value_init(cb);
182 value_init(ck);
183 fgets(s, 128, stdin);
184 sscanf(s, "%d", &n);
185 for (j = 0; j < n; ++j) {
186 Polyhedron *P;
187 M = Matrix_Read();
188 P = Constraints2Polyhedron(M, options->MaxRays);
189 Matrix_Free(M);
190 A = DomainConcat(P, A);
192 fgets(s, 128, stdin);
193 /* workaround for apparent bug in older gmps */
194 *strchr(s, '\n') = '\0';
195 while ((*s=='#') || (value_read(ck, s) != 0)) {
196 fgets(s, 128, stdin);
197 /* workaround for apparent bug in older gmps */
198 *strchr(s, '\n') = '\0';
200 C = Universe_Polyhedron(0);
201 EP = barvinok_enumerate_union(A, C, options->MaxRays);
202 value_set_double(cb, compute_evalue(EP, &ck)+.25);
203 if (value_ne(cb, ck))
204 return -1;
205 Domain_Free(C);
206 value_clear(cb);
207 value_clear(ck);
208 evalue_free(EP);
209 break;
211 case 11: {
212 evalue *expected, *computed;
213 unsigned nvar, nparam;
214 char **pp;
216 expected = evalue_read_from_file(stdin, NULL, &pp, &nvar, &nparam,
217 options->MaxRays);
218 C = Universe_Polyhedron(0);
219 computed = Polyhedron_Lattice_Width(A, C, options);
220 assert(value_zero_p(computed->d));
221 assert(computed->x.p->type == partition);
222 if (!eequal(expected, &computed->x.p->arr[1]))
223 return -1;
224 free(pp);
225 Domain_Free(C);
226 evalue_free(computed);
227 evalue_free(expected);
228 break;
231 Domain_Free(A);
233 for (i = 0; i < nbVec; ++i) {
234 int ok;
235 Vector *V = Vector_Read();
236 Matrix *M = Matrix_Alloc(V->Size, V->Size);
237 Vector_Copy(V->p, M->p[0], V->Size);
238 ok = unimodular_complete(M, 1);
239 assert(ok);
240 Matrix_Print(stdout, P_VALUE_FMT, M);
241 Matrix_Free(M);
242 Vector_Free(V);
244 for (i = 0; i < nbMat; ++i) {
245 Matrix *U, *V, *S;
246 Matrix *M = Matrix_Read();
247 Smith(M, &U, &V, &S);
248 Matrix_Print(stdout, P_VALUE_FMT, U);
249 Matrix_Print(stdout, P_VALUE_FMT, V);
250 Matrix_Print(stdout, P_VALUE_FMT, S);
251 Matrix_Free(M);
252 Matrix_Free(U);
253 Matrix_Free(V);
254 Matrix_Free(S);
257 barvinok_options_free(options);
258 return 0;