dpoly: add some documentation
[barvinok.git] / test.c
blob33cc29b5b1598af59572ba735f6a93195b69d7d0
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 "config.h"
10 #ifdef HAVE_SYS_TIMES_H
12 #include <sys/times.h>
14 static void time_diff(struct tms *before, struct tms *after)
16 long ticks = sysconf(_SC_CLK_TCK);
17 printf("User: %g; Sys: %g\n",
18 (0.0 + after->tms_utime - before->tms_utime) / ticks,
19 (0.0 + after->tms_stime - before->tms_stime) / ticks);
22 #else
24 struct tms {};
25 static void times(struct tms* time)
28 static void time_diff(struct tms *before, struct tms *after)
32 #endif
34 int main(int argc, char **argv)
36 int i, nbPol, nbVec, func, j;
37 Polyhedron *A, *B, *C, *D, *E, *F, *G;
38 char s[128];
39 struct barvinok_options *options = barvinok_options_new_with_defaults();
41 argp_parse(&barvinok_argp, argc, argv, 0, 0, options);
43 nbPol = nbVec = 0;
44 fgets(s, 128, stdin);
45 while ((*s=='#') ||
46 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "V %d", &nbVec)<1)) )
47 fgets(s, 128, stdin);
49 for (i = 0; i < nbPol; ++i) {
50 Matrix *M = Matrix_Read();
51 A = Constraints2Polyhedron(M, options->MaxRays);
52 Matrix_Free(M);
53 fgets(s, 128, stdin);
54 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
55 fgets(s, 128, stdin);
57 switch(func) {
58 case 0: {
59 Value cb, ck;
60 value_init(cb);
61 value_init(ck);
62 fgets(s, 128, stdin);
63 /* workaround for apparent bug in older gmps */
64 *strchr(s, '\n') = '\0';
65 while ((*s=='#') || (value_read(ck, s) != 0)) {
66 fgets(s, 128, stdin);
67 /* workaround for apparent bug in older gmps */
68 *strchr(s, '\n') = '\0';
70 barvinok_count_with_options(A, &cb, options);
71 if (value_ne(cb, ck))
72 return -1;
73 value_clear(cb);
74 value_clear(ck);
75 break;
77 case 1:
78 Polyhedron_Print(stdout, P_VALUE_FMT, A);
79 B = Polyhedron_Polar(A, options->MaxRays);
80 Polyhedron_Print(stdout, P_VALUE_FMT, B);
81 C = Polyhedron_Polar(B, options->MaxRays);
82 Polyhedron_Print(stdout, P_VALUE_FMT, C);
83 Polyhedron_Free(C);
84 Polyhedron_Free(B);
85 break;
86 case 2:
87 Polyhedron_Print(stdout, P_VALUE_FMT, A);
88 for (j = 0; j < A->NbRays; ++j) {
89 B = supporting_cone(A, j);
90 Polyhedron_Print(stdout, P_VALUE_FMT, B);
91 Polyhedron_Free(B);
93 break;
94 case 3:
95 Polyhedron_Print(stdout, P_VALUE_FMT, A);
96 C = B = NULL;
97 barvinok_decompose(A,&B,&C);
98 puts("Pos:");
99 Polyhedron_Print(stdout, P_VALUE_FMT, B);
100 puts("Neg:");
101 Polyhedron_Print(stdout, P_VALUE_FMT, C);
102 Domain_Free(B);
103 Domain_Free(C);
104 break;
105 case 4: {
106 Value cm, cb;
107 struct tms tms_before, tms_between, tms_after;
108 value_init(cm);
109 value_init(cb);
110 Polyhedron_Print(stdout, P_VALUE_FMT, A);
111 times(&tms_before);
112 manual_count(A, &cm);
113 times(&tms_between);
114 barvinok_count(A, &cb, 100);
115 times(&tms_after);
116 printf("manual: ");
117 value_print(stdout, P_VALUE_FMT, cm);
118 puts("");
119 time_diff(&tms_before, &tms_between);
120 printf("Barvinok: ");
121 value_print(stdout, P_VALUE_FMT, cb);
122 puts("");
123 time_diff(&tms_between, &tms_after);
124 value_clear(cm);
125 value_clear(cb);
126 break;
128 case 5:
129 Polyhedron_Print(stdout, P_VALUE_FMT, A);
130 B = triangulate_cone(A, 100);
131 Polyhedron_Print(stdout, P_VALUE_FMT, B);
132 check_triangulization(A, B);
133 Domain_Free(B);
134 break;
135 case 6:
136 Polyhedron_Print(stdout, P_VALUE_FMT, A);
137 B = remove_equalities(A);
138 Polyhedron_Print(stdout, P_VALUE_FMT, B);
139 Polyhedron_Free(B);
140 break;
141 case 8: {
142 evalue *EP;
143 Matrix *M = Matrix_Read();
144 char **param_name;
145 C = Constraints2Polyhedron(M, options->MaxRays);
146 Matrix_Free(M);
147 Polyhedron_Print(stdout, P_VALUE_FMT, A);
148 Polyhedron_Print(stdout, P_VALUE_FMT, C);
149 EP = barvinok_enumerate_with_options(A, C, options);
150 param_name = Read_ParamNames(stdin, C->Dimension);
151 print_evalue(stdout, EP, param_name);
152 free_evalue_refs(EP);
153 free(EP);
154 Polyhedron_Free(C);
156 case 9:
157 Polyhedron_Print(stdout, P_VALUE_FMT, A);
158 Polyhedron_Polarize(A);
159 C = B = NULL;
160 barvinok_decompose(A,&B,&C);
161 for (D = B; D; D = D->next)
162 Polyhedron_Polarize(D);
163 for (D = C; D; D = D->next)
164 Polyhedron_Polarize(D);
165 puts("Pos:");
166 Polyhedron_Print(stdout, P_VALUE_FMT, B);
167 puts("Neg:");
168 Polyhedron_Print(stdout, P_VALUE_FMT, C);
169 Domain_Free(B);
170 Domain_Free(C);
171 break;
173 Polyhedron_Free(A);
175 for (i = 0; i < nbVec; ++i) {
176 Vector *V = Vector_Read();
177 Matrix *M = unimodular_complete(V);
178 Matrix_Print(stdout, P_VALUE_FMT, M);
179 Matrix_Free(M);
180 Vector_Free(V);
183 free(options);
184 return 0;