Merge branch 'topcom'
[barvinok.git] / testlib.cc
blob067f79f495d8b4e814977d27a8495c09d0b1db12
1 #include <assert.h>
2 #include <sstream>
3 #include <barvinok/set.h>
4 #include <barvinok/options.h>
5 #include <barvinok/evalue.h>
6 #include <barvinok/util.h>
7 #include "conversion.h"
8 #include "evalue_read.h"
9 #include "dpoly.h"
10 #include "lattice_point.h"
11 #include "tcounter.h"
12 #include "bernoulli.h"
14 using std::cout;
15 using std::cerr;
16 using std::endl;
18 template <typename T>
19 void set_from_string(T& v, const char *s)
21 std::istringstream str(s);
22 str >> v;
25 int test_evalue(struct barvinok_options *options)
27 unsigned nvar, nparam;
28 char **all_vars;
29 evalue *poly1, poly2;
31 poly1 = evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
32 NULL, &all_vars, &nvar, &nparam,
33 options->MaxRays);
34 Free_ParamNames(all_vars, nvar+nparam);
36 value_init(poly2.d);
37 evalue_copy(&poly2, poly1);
38 evalue_negate(poly1);
39 eadd(&poly2, poly1);
40 reduce_evalue(poly1);
41 assert(EVALUE_IS_ZERO(*poly1));
42 free_evalue_refs(poly1);
43 free_evalue_refs(&poly2);
44 free(poly1);
47 int test_split_periods(struct barvinok_options *options)
49 unsigned nvar, nparam;
50 char **all_vars;
51 evalue *e;
53 e = evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
54 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
55 NULL, &all_vars, &nvar, &nparam,
56 options->MaxRays);
57 Free_ParamNames(all_vars, nvar+nparam);
59 evalue_split_periods(e, 2, options->MaxRays);
60 assert(value_zero_p(e->d));
61 assert(e->x.p->type == partition);
62 assert(e->x.p->size == 4);
63 assert(value_zero_p(e->x.p->arr[1].d));
64 assert(e->x.p->arr[1].x.p->type == polynomial);
65 assert(value_zero_p(e->x.p->arr[3].d));
66 assert(e->x.p->arr[3].x.p->type == polynomial);
67 free_evalue_refs(e);
68 free(e);
71 int test_specialization(struct barvinok_options *options)
73 Value v;
74 value_init(v);
75 mpq_t count;
76 mpq_init(count);
78 value_set_si(v, 5);
79 dpoly n(2, v);
80 assert(value_cmp_si(n.coeff->p[0], 1) == 0);
81 assert(value_cmp_si(n.coeff->p[1], 5) == 0);
82 assert(value_cmp_si(n.coeff->p[2], 10) == 0);
84 value_set_si(v, 1);
85 dpoly d(2, v, 1);
86 value_set_si(v, 2);
87 dpoly d2(2, v, 1);
88 d *= d2;
89 assert(value_cmp_si(d.coeff->p[0], 2) == 0);
90 assert(value_cmp_si(d.coeff->p[1], 1) == 0);
91 assert(value_cmp_si(d.coeff->p[2], 0) == 0);
93 n.div(d, count, 1);
94 mpq_canonicalize(count);
95 assert(value_cmp_si(mpq_numref(count), 31) == 0);
96 assert(value_cmp_si(mpq_denref(count), 8) == 0);
98 value_set_si(v, -2);
99 dpoly n2(2, v);
100 assert(value_cmp_si(n2.coeff->p[0], 1) == 0);
101 assert(value_cmp_si(n2.coeff->p[1], -2) == 0);
102 assert(value_cmp_si(n2.coeff->p[2], 3) == 0);
104 n2.div(d, count, 1);
105 mpq_canonicalize(count);
106 assert(value_cmp_si(mpq_numref(count), 6) == 0);
107 assert(value_cmp_si(mpq_denref(count), 1) == 0);
109 value_clear(v);
110 mpq_clear(count);
113 int test_lattice_points(struct barvinok_options *options)
115 Param_Vertices V;
116 mat_ZZ tmp;
117 set_from_string(tmp, "[[0 0 0 0 4][0 0 0 0 4][-1 0 1 0 4]]");
118 V.Vertex = zz2matrix(tmp);
119 vec_ZZ lambda;
120 set_from_string(lambda, "[3 5 7]");
121 mat_ZZ rays;
122 set_from_string(rays, "[[0 1 0][4 0 1][0 0 -1]]");
123 term_info num;
124 evalue *point[4];
126 unsigned nvar, nparam;
127 char **all_vars;
128 point[0] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
129 "( 7 * {( 1/4 * a + ( 3/4 * c + 3/4 ) ) } + -21/4 ) ) )",
130 "a,b,c", &all_vars, &nvar, &nparam, options->MaxRays);
131 Free_ParamNames(all_vars, nvar+nparam);
132 point[1] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
133 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/2 ) ) } + -1/2 ) ) )",
134 "a,b,c", &all_vars, &nvar, &nparam, options->MaxRays);
135 Free_ParamNames(all_vars, nvar+nparam);
136 point[2] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
137 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/4 ) ) } + 17/4 ) ) )",
138 "a,b,c", &all_vars, &nvar, &nparam, options->MaxRays);
139 Free_ParamNames(all_vars, nvar+nparam);
140 point[3] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
141 "( 7 * {( 1/4 * a + ( 3/4 * c + 0 ) ) } + 9 ) ) )",
142 "a,b,c", &all_vars, &nvar, &nparam, options->MaxRays);
143 Free_ParamNames(all_vars, nvar+nparam);
145 lattice_point(&V, rays, lambda, &num, 4, options);
146 Matrix_Free(V.Vertex);
148 for (int i = 0; i < 4; ++i) {
149 assert(eequal(num.E[i], point[i]));
150 free_evalue_refs(point[i]);
151 free(point[i]);
152 free_evalue_refs(num.E[i]);
153 delete num.E[i];
155 delete [] num.E;
158 int test_todd(struct barvinok_options *options)
160 tcounter t(2, options->max_index);
161 assert(value_cmp_si(t.todd.coeff->p[0], 1) == 0);
162 assert(value_cmp_si(t.todd.coeff->p[1], -3) == 0);
163 assert(value_cmp_si(t.todd.coeff->p[2], 3) == 0);
164 assert(value_cmp_si(t.todd_denom->p[0], 1) == 0);
165 assert(value_cmp_si(t.todd_denom->p[1], 6) == 0);
166 assert(value_cmp_si(t.todd_denom->p[2], 36) == 0);
168 vec_ZZ lambda;
169 set_from_string(lambda, "[1 -1]");
170 zz2values(lambda, t.lambda->p);
172 mat_ZZ rays;
173 set_from_string(rays, "[[-1 0][-1 1]]");
175 QQ one(1, 1);
177 vec_ZZ v;
178 set_from_string(v, "[2 0 1]");
179 Vector *vertex = Vector_Alloc(3);
180 zz2values(v, vertex->p);
182 t.handle(rays, vertex->p, one, 1, options);
183 assert(value_cmp_si(mpq_numref(t.count), 71) == 0);
184 assert(value_cmp_si(mpq_denref(t.count), 24) == 0);
186 set_from_string(rays, "[[0 -1][1 -1]]");
187 set_from_string(v, "[0 2 1]");
188 zz2values(v, vertex->p);
190 t.handle(rays, vertex->p, one, 1, options);
191 assert(value_cmp_si(mpq_numref(t.count), 71) == 0);
192 assert(value_cmp_si(mpq_denref(t.count), 12) == 0);
194 set_from_string(rays, "[[1 0][0 1]]");
195 set_from_string(v, "[0 0 1]");
196 zz2values(v, vertex->p);
198 t.handle(rays, vertex->p, one, 1, options);
199 assert(value_cmp_si(mpq_numref(t.count), 6) == 0);
200 assert(value_cmp_si(mpq_denref(t.count), 1) == 0);
202 Vector_Free(vertex);
205 int test_bernoulli(struct barvinok_options *options)
207 struct bernoulli_coef *bernoulli_coef;
208 struct poly_list *faulhaber, *bernoulli;
209 bernoulli_coef = bernoulli_coef_compute(2);
210 faulhaber = faulhaber_compute(4);
211 bernoulli_coef = bernoulli_coef_compute(8);
212 assert(value_cmp_si(bernoulli_coef->num->p[6], 1) == 0);
213 assert(value_cmp_si(bernoulli_coef->den->p[6], 42) == 0);
214 assert(value_cmp_si(faulhaber->poly[3]->p[0], 0) == 0);
215 assert(value_cmp_si(faulhaber->poly[3]->p[1], 0) == 0);
216 assert(value_cmp_si(faulhaber->poly[3]->p[2], 1) == 0);
217 assert(value_cmp_si(faulhaber->poly[3]->p[3], -2) == 0);
218 assert(value_cmp_si(faulhaber->poly[3]->p[4], 1) == 0);
219 assert(value_cmp_si(faulhaber->poly[3]->p[5], 4) == 0);
221 bernoulli = bernoulli_compute(6);
222 assert(value_cmp_si(bernoulli->poly[6]->p[0], 1) == 0);
223 assert(value_cmp_si(bernoulli->poly[6]->p[1], 0) == 0);
224 assert(value_cmp_si(bernoulli->poly[6]->p[2], -21) == 0);
225 assert(value_cmp_si(bernoulli->poly[6]->p[3], 0) == 0);
226 assert(value_cmp_si(bernoulli->poly[6]->p[4], 105) == 0);
227 assert(value_cmp_si(bernoulli->poly[6]->p[5], -126) == 0);
228 assert(value_cmp_si(bernoulli->poly[6]->p[6], 42) == 0);
229 assert(value_cmp_si(bernoulli->poly[6]->p[7], 42) == 0);
231 unsigned nvar, nparam;
232 char **all_vars;
233 evalue *base, *sum1, *sum2;
234 base = evalue_read_from_str("(1 * n + 1)", NULL, &all_vars, &nvar, &nparam,
235 options->MaxRays);
237 sum1 = evalue_polynomial(faulhaber->poly[3], base);
238 Free_ParamNames(all_vars, nvar+nparam);
240 sum2 = evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
241 NULL, &all_vars, &nvar, &nparam,
242 options->MaxRays);
243 Free_ParamNames(all_vars, nvar+nparam);
244 assert(eequal(sum1, sum2));
245 free_evalue_refs(base);
246 free_evalue_refs(sum1);
247 free_evalue_refs(sum2);
248 free(base);
249 free(sum1);
250 free(sum2);
253 int test_bernoulli_sum(struct barvinok_options *options)
255 unsigned nvar, nparam;
256 char **all_vars;
257 evalue *e, *sum1, *sum2;
258 e = evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
259 "i", &all_vars, &nvar, &nparam,
260 options->MaxRays);
261 Free_ParamNames(all_vars, nvar+nparam);
263 sum1 = Bernoulli_sum_evalue(e, 1, options);
264 sum2 = evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
265 NULL, &all_vars, &nvar, &nparam,
266 options->MaxRays);
267 Free_ParamNames(all_vars, nvar+nparam);
268 evalue_negate(sum1);
269 eadd(sum2, sum1);
270 reduce_evalue(sum1);
271 assert(EVALUE_IS_ZERO(*sum1));
272 free_evalue_refs(e);
273 free_evalue_refs(sum1);
274 free(e);
275 free(sum1);
277 e = evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
278 "i", &all_vars, &nvar, &nparam,
279 options->MaxRays);
280 Free_ParamNames(all_vars, nvar+nparam);
281 sum1 = Bernoulli_sum_evalue(e, 1, options);
282 evalue_negate(sum1);
283 eadd(sum2, sum1);
284 reduce_evalue(sum1);
285 assert(EVALUE_IS_ZERO(*sum1));
286 free_evalue_refs(e);
287 free_evalue_refs(sum1);
288 free(e);
289 free(sum1);
291 free_evalue_refs(sum2);
292 free(sum2);
294 e = evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
295 "i", &all_vars, &nvar, &nparam,
296 options->MaxRays);
297 Free_ParamNames(all_vars, nvar+nparam);
298 sum1 = Bernoulli_sum_evalue(e, 1, options);
299 sum2 = evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
300 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
301 NULL, &all_vars, &nvar, &nparam,
302 options->MaxRays);
303 Free_ParamNames(all_vars, nvar+nparam);
304 evalue_negate(sum1);
305 eadd(sum2, sum1);
306 reduce_evalue(sum1);
307 assert(EVALUE_IS_ZERO(*sum1));
308 free_evalue_refs(e);
309 free_evalue_refs(sum1);
310 free(e);
311 free(sum1);
312 free_evalue_refs(sum2);
313 free(sum2);
315 e = evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
316 "k -1 >= 0\n j -k >= 0\n\n1",
317 "i,j,k", &all_vars, &nvar, &nparam,
318 options->MaxRays);
319 Free_ParamNames(all_vars, nvar+nparam);
320 sum1 = Bernoulli_sum_evalue(e, 3, options);
321 sum2 = evalue_read_from_str("n -5 >= 0\n\n"
322 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
323 NULL, &all_vars, &nvar, &nparam,
324 options->MaxRays);
325 Free_ParamNames(all_vars, nvar+nparam);
326 evalue_negate(sum1);
327 eadd(sum2, sum1);
328 reduce_evalue(sum1);
329 assert(EVALUE_IS_ZERO(*sum1));
330 free_evalue_refs(e);
331 free_evalue_refs(sum1);
332 free(e);
333 free(sum1);
334 free_evalue_refs(sum2);
335 free(sum2);
338 int main(int argc, char **argv)
340 struct barvinok_options *options = barvinok_options_new_with_defaults();
341 test_evalue(options);
342 test_split_periods(options);
343 test_specialization(options);
344 test_lattice_points(options);
345 test_todd(options);
346 test_bernoulli(options);
347 test_bernoulli_sum(options);
348 barvinok_options_free(options);