handle case where first part does not have a solution
[barvinok.git] / test.c
blob173ba0df1ef3bff49a28dc54652f872af40b799f
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/times.h>
6 #include <polylib/polylibgmp.h>
7 #include <util.h>
8 #include <barvinok.h>
9 #include "config.h"
11 #ifdef HAVE_GROWING_CHERNIKOVA
12 #define MAXRAYS 0
13 #else
14 #define MAXRAYS 600
15 #endif
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 int main()
27 int i, nbPol, nbVec, func, j;
28 Polyhedron *A, *B, *C, *D, *E, *F, *G;
29 char s[128];
31 nbPol = nbVec = 0;
32 fgets(s, 128, stdin);
33 while ((*s=='#') ||
34 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "V %d", &nbVec)<1)) )
35 fgets(s, 128, stdin);
37 for (i = 0; i < nbPol; ++i) {
38 Matrix *M = Matrix_Read();
39 A = Constraints2Polyhedron(M, MAXRAYS);
40 Matrix_Free(M);
41 fgets(s, 128, stdin);
42 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
43 fgets(s, 128, stdin);
45 switch(func) {
46 case 0: {
47 Value cb, ck;
48 value_init(cb);
49 value_init(ck);
50 fgets(s, 128, stdin);
51 /* workaround for apparent bug in older gmps */
52 *strchr(s, '\n') = '\0';
53 while ((*s=='#') || (value_read(ck, s) != 0)) {
54 fgets(s, 128, stdin);
55 /* workaround for apparent bug in older gmps */
56 *strchr(s, '\n') = '\0';
58 barvinok_count(A, &cb, 100);
59 if (value_ne(cb, ck))
60 return -1;
61 value_clear(cb);
62 value_clear(ck);
63 break;
65 case 1:
66 Polyhedron_Print(stdout, P_VALUE_FMT, A);
67 B = Polyhedron_Polar(A, MAXRAYS);
68 Polyhedron_Print(stdout, P_VALUE_FMT, B);
69 C = Polyhedron_Polar(B, MAXRAYS);
70 Polyhedron_Print(stdout, P_VALUE_FMT, C);
71 Polyhedron_Free(C);
72 Polyhedron_Free(B);
73 break;
74 case 2:
75 Polyhedron_Print(stdout, P_VALUE_FMT, A);
76 for (j = 0; j < A->NbRays; ++j) {
77 B = supporting_cone(A, j);
78 Polyhedron_Print(stdout, P_VALUE_FMT, B);
79 Polyhedron_Free(B);
81 break;
82 case 3:
83 Polyhedron_Print(stdout, P_VALUE_FMT, A);
84 C = B = NULL;
85 barvinok_decompose(A,&B,&C);
86 puts("Pos:");
87 Polyhedron_Print(stdout, P_VALUE_FMT, B);
88 puts("Neg:");
89 Polyhedron_Print(stdout, P_VALUE_FMT, C);
90 Domain_Free(B);
91 Domain_Free(C);
92 break;
93 case 4: {
94 Value cm, cb;
95 struct tms tms_before, tms_between, tms_after;
96 value_init(cm);
97 value_init(cb);
98 Polyhedron_Print(stdout, P_VALUE_FMT, A);
99 times(&tms_before);
100 manual_count(A, &cm);
101 times(&tms_between);
102 barvinok_count(A, &cb, 100);
103 times(&tms_after);
104 printf("manual: ");
105 value_print(stdout, P_VALUE_FMT, cm);
106 puts("");
107 time_diff(&tms_before, &tms_between);
108 printf("Barvinok: ");
109 value_print(stdout, P_VALUE_FMT, cb);
110 puts("");
111 time_diff(&tms_between, &tms_after);
112 value_clear(cm);
113 value_clear(cb);
114 break;
116 case 5:
117 Polyhedron_Print(stdout, P_VALUE_FMT, A);
118 B = triangularize_cone(A, 100);
119 Polyhedron_Print(stdout, P_VALUE_FMT, B);
120 check_triangulization(A, B);
121 Domain_Free(B);
122 break;
123 case 6:
124 Polyhedron_Print(stdout, P_VALUE_FMT, A);
125 B = remove_equalities(A);
126 Polyhedron_Print(stdout, P_VALUE_FMT, B);
127 Polyhedron_Free(B);
128 break;
129 case 7: {
130 Value f;
131 value_init(f);
132 Polyhedron_Print(stdout, P_VALUE_FMT, A);
133 B = Polyhedron_Reduce(A, &f);
134 Polyhedron_Print(stdout, P_VALUE_FMT, B);
135 Polyhedron_Free(B);
136 printf("factor: ");
137 value_print(stdout, P_VALUE_FMT, f);
138 puts("");
139 value_clear(f);
140 break;
142 case 8: {
143 Enumeration *en;
144 Matrix *M = Matrix_Read();
145 char **param_name;
146 C = Constraints2Polyhedron(M, MAXRAYS);
147 Matrix_Free(M);
148 Polyhedron_Print(stdout, P_VALUE_FMT, A);
149 Polyhedron_Print(stdout, P_VALUE_FMT, C);
150 en = barvinok_enumerate(A, C, MAXRAYS);
151 param_name = Read_ParamNames(stdin, C->Dimension);
152 print_evalue(stdout, &en->EP, param_name);
153 Enumeration_Free(en);
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 return 0;