Add polymake/README to distribution.
[barvinok.git] / test.c
blob04b86f1189ccf746c5f57acba51480ba88dbb7db
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <polylib/polylibgmp.h>
6 #include <util.h>
7 #include <barvinok.h>
8 #include "config.h"
10 #ifdef HAVE_GROWING_CHERNIKOVA
11 #define MAXRAYS 0
12 #else
13 #define MAXRAYS 600
14 #endif
16 #ifdef HAVE_SYS_TIMES_H
18 #include <sys/times.h>
20 static void time_diff(struct tms *before, struct tms *after)
22 long ticks = sysconf(_SC_CLK_TCK);
23 printf("User: %g; Sys: %g\n",
24 (0.0 + after->tms_utime - before->tms_utime) / ticks,
25 (0.0 + after->tms_stime - before->tms_stime) / ticks);
28 #else
30 struct tms {};
31 static void times(struct tms* time)
34 static void time_diff(struct tms *before, struct tms *after)
38 #endif
40 int main()
42 int i, nbPol, nbVec, func, j;
43 Polyhedron *A, *B, *C, *D, *E, *F, *G;
44 char s[128];
46 nbPol = nbVec = 0;
47 fgets(s, 128, stdin);
48 while ((*s=='#') ||
49 ((sscanf(s, "D %d", &nbPol)<1) && (sscanf(s, "V %d", &nbVec)<1)) )
50 fgets(s, 128, stdin);
52 for (i = 0; i < nbPol; ++i) {
53 Matrix *M = Matrix_Read();
54 A = Constraints2Polyhedron(M, MAXRAYS);
55 Matrix_Free(M);
56 fgets(s, 128, stdin);
57 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
58 fgets(s, 128, stdin);
60 switch(func) {
61 case 0: {
62 Value cb, ck;
63 value_init(cb);
64 value_init(ck);
65 fgets(s, 128, stdin);
66 /* workaround for apparent bug in older gmps */
67 *strchr(s, '\n') = '\0';
68 while ((*s=='#') || (value_read(ck, s) != 0)) {
69 fgets(s, 128, stdin);
70 /* workaround for apparent bug in older gmps */
71 *strchr(s, '\n') = '\0';
73 barvinok_count(A, &cb, 100);
74 if (value_ne(cb, ck))
75 return -1;
76 value_clear(cb);
77 value_clear(ck);
78 break;
80 case 1:
81 Polyhedron_Print(stdout, P_VALUE_FMT, A);
82 B = Polyhedron_Polar(A, MAXRAYS);
83 Polyhedron_Print(stdout, P_VALUE_FMT, B);
84 C = Polyhedron_Polar(B, MAXRAYS);
85 Polyhedron_Print(stdout, P_VALUE_FMT, C);
86 Polyhedron_Free(C);
87 Polyhedron_Free(B);
88 break;
89 case 2:
90 Polyhedron_Print(stdout, P_VALUE_FMT, A);
91 for (j = 0; j < A->NbRays; ++j) {
92 B = supporting_cone(A, j);
93 Polyhedron_Print(stdout, P_VALUE_FMT, B);
94 Polyhedron_Free(B);
96 break;
97 case 3:
98 Polyhedron_Print(stdout, P_VALUE_FMT, A);
99 C = B = NULL;
100 barvinok_decompose(A,&B,&C);
101 puts("Pos:");
102 Polyhedron_Print(stdout, P_VALUE_FMT, B);
103 puts("Neg:");
104 Polyhedron_Print(stdout, P_VALUE_FMT, C);
105 Domain_Free(B);
106 Domain_Free(C);
107 break;
108 case 4: {
109 Value cm, cb;
110 struct tms tms_before, tms_between, tms_after;
111 value_init(cm);
112 value_init(cb);
113 Polyhedron_Print(stdout, P_VALUE_FMT, A);
114 times(&tms_before);
115 manual_count(A, &cm);
116 times(&tms_between);
117 barvinok_count(A, &cb, 100);
118 times(&tms_after);
119 printf("manual: ");
120 value_print(stdout, P_VALUE_FMT, cm);
121 puts("");
122 time_diff(&tms_before, &tms_between);
123 printf("Barvinok: ");
124 value_print(stdout, P_VALUE_FMT, cb);
125 puts("");
126 time_diff(&tms_between, &tms_after);
127 value_clear(cm);
128 value_clear(cb);
129 break;
131 case 5:
132 Polyhedron_Print(stdout, P_VALUE_FMT, A);
133 B = triangularize_cone(A, 100);
134 Polyhedron_Print(stdout, P_VALUE_FMT, B);
135 check_triangulization(A, B);
136 Domain_Free(B);
137 break;
138 case 6:
139 Polyhedron_Print(stdout, P_VALUE_FMT, A);
140 B = remove_equalities(A);
141 Polyhedron_Print(stdout, P_VALUE_FMT, B);
142 Polyhedron_Free(B);
143 break;
144 case 7: {
145 Value f;
146 value_init(f);
147 Polyhedron_Print(stdout, P_VALUE_FMT, A);
148 B = Polyhedron_Reduce(A, &f);
149 Polyhedron_Print(stdout, P_VALUE_FMT, B);
150 Polyhedron_Free(B);
151 printf("factor: ");
152 value_print(stdout, P_VALUE_FMT, f);
153 puts("");
154 value_clear(f);
155 break;
157 case 8: {
158 Enumeration *en;
159 Matrix *M = Matrix_Read();
160 char **param_name;
161 C = Constraints2Polyhedron(M, MAXRAYS);
162 Matrix_Free(M);
163 Polyhedron_Print(stdout, P_VALUE_FMT, A);
164 Polyhedron_Print(stdout, P_VALUE_FMT, C);
165 en = barvinok_enumerate(A, C, MAXRAYS);
166 param_name = Read_ParamNames(stdin, C->Dimension);
167 print_evalue(stdout, &en->EP, param_name);
168 Enumeration_Free(en);
169 Polyhedron_Free(C);
171 case 9:
172 Polyhedron_Print(stdout, P_VALUE_FMT, A);
173 Polyhedron_Polarize(A);
174 C = B = NULL;
175 barvinok_decompose(A,&B,&C);
176 for (D = B; D; D = D->next)
177 Polyhedron_Polarize(D);
178 for (D = C; D; D = D->next)
179 Polyhedron_Polarize(D);
180 puts("Pos:");
181 Polyhedron_Print(stdout, P_VALUE_FMT, B);
182 puts("Neg:");
183 Polyhedron_Print(stdout, P_VALUE_FMT, C);
184 Domain_Free(B);
185 Domain_Free(C);
186 break;
188 Polyhedron_Free(A);
190 for (i = 0; i < nbVec; ++i) {
191 Vector *V = Vector_Read();
192 Matrix *M = unimodular_complete(V);
193 Matrix_Print(stdout, P_VALUE_FMT, M);
194 Matrix_Free(M);
195 Vector_Free(V);
198 return 0;