test: simple test for PolyLib's Smith
[barvinok.git] / test.c
blobe99a7c070d31ce9d2cbc21fccf163f479d4bb95d
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, nbMat, 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) &&
47 (sscanf(s, "V %d", &nbVec) < 1) &&
48 (sscanf(s, "M %d", &nbMat) < 1)))
49 fgets(s, 128, stdin);
51 for (i = 0; i < nbPol; ++i) {
52 Matrix *M = Matrix_Read();
53 A = Constraints2Polyhedron(M, options->MaxRays);
54 Matrix_Free(M);
55 fgets(s, 128, stdin);
56 while ((*s=='#') || (sscanf(s, "F %d", &func)<1))
57 fgets(s, 128, stdin);
59 switch(func) {
60 case 0: {
61 Value cb, ck;
62 value_init(cb);
63 value_init(ck);
64 fgets(s, 128, stdin);
65 /* workaround for apparent bug in older gmps */
66 *strchr(s, '\n') = '\0';
67 while ((*s=='#') || (value_read(ck, s) != 0)) {
68 fgets(s, 128, stdin);
69 /* workaround for apparent bug in older gmps */
70 *strchr(s, '\n') = '\0';
72 barvinok_count_with_options(A, &cb, options);
73 if (value_ne(cb, ck))
74 return -1;
75 value_clear(cb);
76 value_clear(ck);
77 break;
79 case 1:
80 Polyhedron_Print(stdout, P_VALUE_FMT, A);
81 B = Polyhedron_Polar(A, options->MaxRays);
82 Polyhedron_Print(stdout, P_VALUE_FMT, B);
83 C = Polyhedron_Polar(B, options->MaxRays);
84 Polyhedron_Print(stdout, P_VALUE_FMT, C);
85 Polyhedron_Free(C);
86 Polyhedron_Free(B);
87 break;
88 case 2:
89 Polyhedron_Print(stdout, P_VALUE_FMT, A);
90 for (j = 0; j < A->NbRays; ++j) {
91 B = supporting_cone(A, j);
92 Polyhedron_Print(stdout, P_VALUE_FMT, B);
93 Polyhedron_Free(B);
95 break;
96 case 3:
97 Polyhedron_Print(stdout, P_VALUE_FMT, A);
98 C = B = NULL;
99 barvinok_decompose(A,&B,&C);
100 puts("Pos:");
101 Polyhedron_Print(stdout, P_VALUE_FMT, B);
102 puts("Neg:");
103 Polyhedron_Print(stdout, P_VALUE_FMT, C);
104 Domain_Free(B);
105 Domain_Free(C);
106 break;
107 case 4: {
108 Value cm, cb;
109 struct tms tms_before, tms_between, tms_after;
110 value_init(cm);
111 value_init(cb);
112 Polyhedron_Print(stdout, P_VALUE_FMT, A);
113 times(&tms_before);
114 manual_count(A, &cm);
115 times(&tms_between);
116 barvinok_count(A, &cb, 100);
117 times(&tms_after);
118 printf("manual: ");
119 value_print(stdout, P_VALUE_FMT, cm);
120 puts("");
121 time_diff(&tms_before, &tms_between);
122 printf("Barvinok: ");
123 value_print(stdout, P_VALUE_FMT, cb);
124 puts("");
125 time_diff(&tms_between, &tms_after);
126 value_clear(cm);
127 value_clear(cb);
128 break;
130 case 5:
131 Polyhedron_Print(stdout, P_VALUE_FMT, A);
132 B = triangulate_cone(A, 100);
133 Polyhedron_Print(stdout, P_VALUE_FMT, B);
134 check_triangulization(A, B);
135 Domain_Free(B);
136 break;
137 case 6:
138 Polyhedron_Print(stdout, P_VALUE_FMT, A);
139 B = remove_equalities(A);
140 Polyhedron_Print(stdout, P_VALUE_FMT, B);
141 Polyhedron_Free(B);
142 break;
143 case 8: {
144 evalue *EP;
145 Matrix *M = Matrix_Read();
146 char **param_name;
147 C = Constraints2Polyhedron(M, options->MaxRays);
148 Matrix_Free(M);
149 Polyhedron_Print(stdout, P_VALUE_FMT, A);
150 Polyhedron_Print(stdout, P_VALUE_FMT, C);
151 EP = barvinok_enumerate_with_options(A, C, options);
152 param_name = Read_ParamNames(stdin, C->Dimension);
153 print_evalue(stdout, EP, param_name);
154 free_evalue_refs(EP);
155 free(EP);
156 Polyhedron_Free(C);
158 case 9:
159 Polyhedron_Print(stdout, P_VALUE_FMT, A);
160 Polyhedron_Polarize(A);
161 C = B = NULL;
162 barvinok_decompose(A,&B,&C);
163 for (D = B; D; D = D->next)
164 Polyhedron_Polarize(D);
165 for (D = C; D; D = D->next)
166 Polyhedron_Polarize(D);
167 puts("Pos:");
168 Polyhedron_Print(stdout, P_VALUE_FMT, B);
169 puts("Neg:");
170 Polyhedron_Print(stdout, P_VALUE_FMT, C);
171 Domain_Free(B);
172 Domain_Free(C);
173 break;
175 Polyhedron_Free(A);
177 for (i = 0; i < nbVec; ++i) {
178 Vector *V = Vector_Read();
179 Matrix *M = unimodular_complete(V);
180 Matrix_Print(stdout, P_VALUE_FMT, M);
181 Matrix_Free(M);
182 Vector_Free(V);
184 for (i = 0; i < nbMat; ++i) {
185 Matrix *U, *V, *S;
186 Matrix *M = Matrix_Read();
187 Smith(M, &U, &V, &S);
188 Matrix_Print(stdout, P_VALUE_FMT, U);
189 Matrix_Print(stdout, P_VALUE_FMT, V);
190 Matrix_Print(stdout, P_VALUE_FMT, S);
191 Matrix_Free(M);
192 Matrix_Free(U);
193 Matrix_Free(V);
194 Matrix_Free(S);
197 barvinok_options_free(options);
198 return 0;