short_rat::print: correctly print out terms with a zero coefficient
[barvinok.git] / test.c
blob863f5ce5944ee6b18d6990c7beedf7a07a662d1f
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"
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 int i, nbPol, nbVec, nbMat, func, j, n;
38 Polyhedron *A, *B, *C, *D, *E, *F, *G;
39 char s[128];
40 struct barvinok_options *options = barvinok_options_new_with_defaults();
42 set_program_name(argv[0]);
43 argp_parse(&barvinok_argp, argc, argv, 0, 0, options);
45 nbPol = nbVec = nbMat = 0;
46 fgets(s, 128, stdin);
47 while ((*s=='#') ||
48 ((sscanf(s, "D %d", &nbPol) < 1) &&
49 (sscanf(s, "V %d", &nbVec) < 1) &&
50 (sscanf(s, "M %d", &nbMat) < 1)))
51 fgets(s, 128, stdin);
53 for (i = 0; i < nbPol; ++i) {
54 Matrix *M = Matrix_Read();
55 A = Constraints2Polyhedron(M, options->MaxRays);
56 Matrix_Free(M);
57 fgets(s, 128, stdin);
58 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
59 fgets(s, 128, stdin);
61 switch(func) {
62 case 0: {
63 Value cb, ck;
64 value_init(cb);
65 value_init(ck);
66 fgets(s, 128, stdin);
67 /* workaround for apparent bug in older gmps */
68 *strchr(s, '\n') = '\0';
69 while ((*s=='#') || (value_read(ck, s) != 0)) {
70 fgets(s, 128, stdin);
71 /* workaround for apparent bug in older gmps */
72 *strchr(s, '\n') = '\0';
74 barvinok_count_with_options(A, &cb, options);
75 if (value_ne(cb, ck))
76 return -1;
77 value_clear(cb);
78 value_clear(ck);
79 break;
81 case 1:
82 Polyhedron_Print(stdout, P_VALUE_FMT, A);
83 B = Polyhedron_Polar(A, options->MaxRays);
84 Polyhedron_Print(stdout, P_VALUE_FMT, B);
85 C = Polyhedron_Polar(B, options->MaxRays);
86 Polyhedron_Print(stdout, P_VALUE_FMT, C);
87 Polyhedron_Free(C);
88 Polyhedron_Free(B);
89 break;
90 case 2:
91 Polyhedron_Print(stdout, P_VALUE_FMT, A);
92 for (j = 0; j < A->NbRays; ++j) {
93 B = supporting_cone(A, j);
94 Polyhedron_Print(stdout, P_VALUE_FMT, B);
95 Polyhedron_Free(B);
97 break;
98 case 3:
99 Polyhedron_Print(stdout, P_VALUE_FMT, A);
100 C = B = NULL;
101 barvinok_decompose(A,&B,&C);
102 puts("Pos:");
103 Polyhedron_Print(stdout, P_VALUE_FMT, B);
104 puts("Neg:");
105 Polyhedron_Print(stdout, P_VALUE_FMT, C);
106 Domain_Free(B);
107 Domain_Free(C);
108 break;
109 case 4: {
110 Value cm, cb;
111 struct tms tms_before, tms_between, tms_after;
112 value_init(cm);
113 value_init(cb);
114 Polyhedron_Print(stdout, P_VALUE_FMT, A);
115 times(&tms_before);
116 manual_count(A, &cm);
117 times(&tms_between);
118 barvinok_count(A, &cb, 100);
119 times(&tms_after);
120 printf("manual: ");
121 value_print(stdout, P_VALUE_FMT, cm);
122 puts("");
123 time_diff(&tms_before, &tms_between);
124 printf("Barvinok: ");
125 value_print(stdout, P_VALUE_FMT, cb);
126 puts("");
127 time_diff(&tms_between, &tms_after);
128 value_clear(cm);
129 value_clear(cb);
130 break;
132 case 5:
133 Polyhedron_Print(stdout, P_VALUE_FMT, A);
134 B = triangulate_cone(A, 100);
135 Polyhedron_Print(stdout, P_VALUE_FMT, B);
136 check_triangulization(A, B);
137 Domain_Free(B);
138 break;
139 case 6:
140 Polyhedron_Print(stdout, P_VALUE_FMT, A);
141 B = remove_equalities(A, options->MaxRays);
142 Polyhedron_Print(stdout, P_VALUE_FMT, B);
143 Polyhedron_Free(B);
144 break;
145 case 8: {
146 evalue *EP;
147 Matrix *M = Matrix_Read();
148 char **param_name;
149 C = Constraints2Polyhedron(M, options->MaxRays);
150 Matrix_Free(M);
151 Polyhedron_Print(stdout, P_VALUE_FMT, A);
152 Polyhedron_Print(stdout, P_VALUE_FMT, C);
153 EP = barvinok_enumerate_with_options(A, C, options);
154 param_name = Read_ParamNames(stdin, C->Dimension);
155 print_evalue(stdout, EP, (const char**)param_name);
156 evalue_free(EP);
157 Polyhedron_Free(C);
159 case 9:
160 Polyhedron_Print(stdout, P_VALUE_FMT, A);
161 Polyhedron_Polarize(A);
162 C = B = NULL;
163 barvinok_decompose(A,&B,&C);
164 for (D = B; D; D = D->next)
165 Polyhedron_Polarize(D);
166 for (D = C; D; D = D->next)
167 Polyhedron_Polarize(D);
168 puts("Pos:");
169 Polyhedron_Print(stdout, P_VALUE_FMT, B);
170 puts("Neg:");
171 Polyhedron_Print(stdout, P_VALUE_FMT, C);
172 Domain_Free(B);
173 Domain_Free(C);
174 break;
175 case 10: {
176 evalue *EP;
177 Value cb, ck;
179 value_init(cb);
180 value_init(ck);
181 fgets(s, 128, stdin);
182 sscanf(s, "%d", &n);
183 for (j = 0; j < n; ++j) {
184 Polyhedron *P;
185 M = Matrix_Read();
186 P = Constraints2Polyhedron(M, options->MaxRays);
187 Matrix_Free(M);
188 A = DomainConcat(P, A);
190 fgets(s, 128, stdin);
191 /* workaround for apparent bug in older gmps */
192 *strchr(s, '\n') = '\0';
193 while ((*s=='#') || (value_read(ck, s) != 0)) {
194 fgets(s, 128, stdin);
195 /* workaround for apparent bug in older gmps */
196 *strchr(s, '\n') = '\0';
198 C = Universe_Polyhedron(0);
199 EP = barvinok_enumerate_union(A, C, options->MaxRays);
200 value_set_double(cb, compute_evalue(EP, &ck)+.25);
201 if (value_ne(cb, ck))
202 return -1;
203 Domain_Free(C);
204 value_clear(cb);
205 value_clear(ck);
206 evalue_free(EP);
209 Domain_Free(A);
211 for (i = 0; i < nbVec; ++i) {
212 int ok;
213 Vector *V = Vector_Read();
214 Matrix *M = Matrix_Alloc(V->Size, V->Size);
215 Vector_Copy(V->p, M->p[0], V->Size);
216 ok = unimodular_complete(M, 1);
217 assert(ok);
218 Matrix_Print(stdout, P_VALUE_FMT, M);
219 Matrix_Free(M);
220 Vector_Free(V);
222 for (i = 0; i < nbMat; ++i) {
223 Matrix *U, *V, *S;
224 Matrix *M = Matrix_Read();
225 Smith(M, &U, &V, &S);
226 Matrix_Print(stdout, P_VALUE_FMT, U);
227 Matrix_Print(stdout, P_VALUE_FMT, V);
228 Matrix_Print(stdout, P_VALUE_FMT, S);
229 Matrix_Free(M);
230 Matrix_Free(U);
231 Matrix_Free(V);
232 Matrix_Free(S);
235 barvinok_options_free(options);
236 return 0;