3 #include <barvinok/barvinok.h>
4 #include <barvinok/set.h>
5 #include <barvinok/options.h>
6 #include <barvinok/evalue.h>
7 #include <barvinok/util.h>
8 #include "conversion.h"
9 #include "evalue_read.h"
11 #include "lattice_point.h"
13 #include "bernoulli.h"
15 #include "matrix_read.h"
22 void set_from_string(T
& v
, const char *s
)
24 std::istringstream
str(s
);
28 int test_evalue(struct barvinok_options
*options
)
30 unsigned nvar
, nparam
;
34 poly1
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
35 NULL
, &all_vars
, &nvar
, &nparam
,
37 Free_ParamNames(all_vars
, nvar
+nparam
);
40 evalue_copy(&poly2
, poly1
);
44 assert(EVALUE_IS_ZERO(*poly1
));
46 free_evalue_refs(&poly2
);
49 int test_split_periods(struct barvinok_options
*options
)
51 unsigned nvar
, nparam
;
55 e
= evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
56 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
57 NULL
, &all_vars
, &nvar
, &nparam
,
59 Free_ParamNames(all_vars
, nvar
+nparam
);
61 evalue_split_periods(e
, 2, options
->MaxRays
);
62 assert(value_zero_p(e
->d
));
63 assert(e
->x
.p
->type
== partition
);
64 assert(e
->x
.p
->size
== 4);
65 assert(value_zero_p(e
->x
.p
->arr
[1].d
));
66 assert(e
->x
.p
->arr
[1].x
.p
->type
== polynomial
);
67 assert(value_zero_p(e
->x
.p
->arr
[3].d
));
68 assert(e
->x
.p
->arr
[3].x
.p
->type
== polynomial
);
72 int test_specialization(struct barvinok_options
*options
)
81 assert(value_cmp_si(n
.coeff
->p
[0], 1) == 0);
82 assert(value_cmp_si(n
.coeff
->p
[1], 5) == 0);
83 assert(value_cmp_si(n
.coeff
->p
[2], 10) == 0);
90 assert(value_cmp_si(d
.coeff
->p
[0], 2) == 0);
91 assert(value_cmp_si(d
.coeff
->p
[1], 1) == 0);
92 assert(value_cmp_si(d
.coeff
->p
[2], 0) == 0);
95 mpq_canonicalize(count
);
96 assert(value_cmp_si(mpq_numref(count
), 31) == 0);
97 assert(value_cmp_si(mpq_denref(count
), 8) == 0);
101 assert(value_cmp_si(n2
.coeff
->p
[0], 1) == 0);
102 assert(value_cmp_si(n2
.coeff
->p
[1], -2) == 0);
103 assert(value_cmp_si(n2
.coeff
->p
[2], 3) == 0);
106 mpq_canonicalize(count
);
107 assert(value_cmp_si(mpq_numref(count
), 6) == 0);
108 assert(value_cmp_si(mpq_denref(count
), 1) == 0);
114 int test_lattice_points(struct barvinok_options
*options
)
118 set_from_string(tmp
, "[[0 0 0 0 4][0 0 0 0 4][-1 0 1 0 4]]");
119 V
.Vertex
= zz2matrix(tmp
);
121 set_from_string(lambda
, "[3 5 7]");
123 set_from_string(rays
, "[[0 1 0][4 0 1][0 0 -1]]");
127 unsigned nvar
, nparam
;
129 point
[0] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
130 "( 7 * {( 1/4 * a + ( 3/4 * c + 3/4 ) ) } + -21/4 ) ) )",
131 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
132 Free_ParamNames(all_vars
, nvar
+nparam
);
133 point
[1] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
134 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/2 ) ) } + -1/2 ) ) )",
135 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
136 Free_ParamNames(all_vars
, nvar
+nparam
);
137 point
[2] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
138 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/4 ) ) } + 17/4 ) ) )",
139 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
140 Free_ParamNames(all_vars
, nvar
+nparam
);
141 point
[3] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
142 "( 7 * {( 1/4 * a + ( 3/4 * c + 0 ) ) } + 9 ) ) )",
143 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
144 Free_ParamNames(all_vars
, nvar
+nparam
);
146 lattice_point(&V
, rays
, lambda
, &num
, 4, options
);
147 Matrix_Free(V
.Vertex
);
149 for (int i
= 0; i
< 4; ++i
) {
150 assert(eequal(num
.E
[i
], point
[i
]));
151 evalue_free(point
[i
]);
152 free_evalue_refs(num
.E
[i
]);
158 static int test_icounter(struct barvinok_options
*options
)
164 set_from_string(n_coeff
, "[-2/1 1/1]");
165 set_from_string(n_power
, "[[2 6][3 6]]");
166 d_power
.SetDims(0, 2);
167 cnt
.reduce(n_coeff
, n_power
, d_power
);
168 assert(value_cmp_si(mpq_numref(cnt
.count
), -1) == 0);
169 assert(value_cmp_si(mpq_denref(cnt
.count
), 1) == 0);
172 static Matrix
*matrix_read_from_str(const char *s
)
174 std::istringstream
str(s
);
175 return Matrix_Read(str
);
178 static int test_infinite_counter(struct barvinok_options
*options
)
180 Matrix
*M
= matrix_read_from_str("1 3\n 1 1 0\n");
181 Polyhedron
*ctx
= Constraints2Polyhedron(M
, options
->MaxRays
);
184 /* (1 -1/2 x^5 - 1/2 x^7)/(1-x) */
185 infinite_counter
*cnt
= new infinite_counter(1, 1);
190 set_from_string(n_coeff
, "[1/1 -1/2 -1/2]");
191 set_from_string(n_power
, "[[0][5][7]]");
192 set_from_string(d_power
, "[[1]]");
193 cnt
->reduce(n_coeff
, n_power
, d_power
);
194 assert(value_cmp_si(mpq_numref(cnt
->count
[0]), 6) == 0);
195 assert(value_cmp_si(mpq_denref(cnt
->count
[0]), 1) == 0);
196 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) == 0);
197 assert(value_cmp_si(mpq_denref(cnt
->count
[1]), 1) == 0);
199 Polyhedron_Free(ctx
);
201 M
= matrix_read_from_str("2 4\n 1 1 0 0\n 1 0 1 0\n");
202 ctx
= Constraints2Polyhedron(M
, options
->MaxRays
);
205 /* (1 - xy)/((1-x)(1-xy)) */
206 cnt
= new infinite_counter(2, 3);
208 set_from_string(n_coeff
, "[1/1 -1/1]");
209 set_from_string(n_power
, "[[0 0][1 1]]");
210 set_from_string(d_power
, "[[1 0][1 1]]");
211 cnt
->reduce(n_coeff
, n_power
, d_power
);
212 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) != 0);
213 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
214 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
215 assert(value_cmp_si(mpq_numref(cnt
->count
[3]), 0) == 0);
216 assert(value_cmp_si(mpq_denref(cnt
->count
[3]), 1) == 0);
219 cnt
= new infinite_counter(2, 2);
221 set_from_string(n_coeff
, "[-1/2 1/1 -1/3]");
222 set_from_string(n_power
, "[[2 6][3 6]]");
223 d_power
.SetDims(0, 2);
224 cnt
->reduce(n_coeff
, n_power
, d_power
);
225 assert(value_cmp_si(mpq_numref(cnt
->count
[0]), 1) == 0);
226 assert(value_cmp_si(mpq_denref(cnt
->count
[0]), 6) == 0);
227 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) == 0);
228 assert(value_cmp_si(mpq_denref(cnt
->count
[1]), 1) == 0);
229 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
230 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
233 cnt
= new infinite_counter(2, 2);
235 set_from_string(n_coeff
, "[1/1]");
236 set_from_string(n_power
, "[[0 11]]");
237 set_from_string(d_power
, "[[0 1]]");
238 cnt
->reduce(n_coeff
, n_power
, d_power
);
239 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) != 0);
240 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
241 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
244 Polyhedron_Free(ctx
);
249 static int test_series(struct barvinok_options
*options
)
251 Matrix
*M
= matrix_read_from_str(
253 " 0 1 0 0 0 0 0 1 0 0 3 \n"
254 " 0 0 1 0 0 0 0 -1 1 0 -5 \n"
255 " 0 0 0 1 0 0 0 0 -2 -1 6 \n"
256 " 0 0 0 0 1 0 0 1 1 0 5 \n"
257 " 0 0 0 0 0 1 0 0 -1 0 0 \n"
258 " 0 0 0 0 0 0 1 -2 0 -1 -3 \n"
259 " 1 0 0 0 0 0 0 2 0 1 3 \n"
260 " 1 0 0 0 0 0 0 1 -1 0 5 \n"
261 " 1 0 0 0 0 0 0 -1 -1 0 -5 \n"
262 " 1 0 0 0 0 0 0 -1 0 0 -3 \n"
263 " 1 0 0 0 0 0 0 0 2 1 -6 \n"
264 " 1 0 0 0 0 0 0 0 1 0 0 \n");
265 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
267 Polyhedron
*C
= Universe_Polyhedron(3);
268 gen_fun
*gf
= barvinok_series_with_options(P
, C
, options
);
273 M
= matrix_read_from_str(
275 " 0 1 1 0 0 1 0 2 \n"
276 " 0 0 0 1 0 -2 0 6 \n"
277 " 0 0 0 0 1 -1 0 -1 \n"
278 " 0 0 0 0 0 0 1 0 \n"
279 " 1 0 1 0 0 0 0 0 \n"
280 " 1 0 -1 0 0 -1 0 -2 \n"
281 " 1 0 0 0 0 1 0 -3 \n");
282 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
284 C
= Universe_Polyhedron(2);
285 gf
= barvinok_series_with_options(P
, C
, options
);
290 M
= matrix_read_from_str(
294 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
296 C
= Universe_Polyhedron(1);
297 gf
= barvinok_series_with_options(P
, C
, options
);
300 gen_fun
*sum
= gf
->summate(1, options
);
307 int test_todd(struct barvinok_options
*options
)
309 tcounter
t(2, options
->max_index
);
310 assert(value_cmp_si(t
.todd
.coeff
->p
[0], 1) == 0);
311 assert(value_cmp_si(t
.todd
.coeff
->p
[1], -3) == 0);
312 assert(value_cmp_si(t
.todd
.coeff
->p
[2], 3) == 0);
313 assert(value_cmp_si(t
.todd_denom
->p
[0], 1) == 0);
314 assert(value_cmp_si(t
.todd_denom
->p
[1], 6) == 0);
315 assert(value_cmp_si(t
.todd_denom
->p
[2], 36) == 0);
318 set_from_string(lambda
, "[1 -1]");
319 zz2values(lambda
, t
.lambda
->p
);
322 set_from_string(rays
, "[[-1 0][-1 1]]");
327 set_from_string(v
, "[2 0 1]");
328 Vector
*vertex
= Vector_Alloc(3);
329 zz2values(v
, vertex
->p
);
331 t
.handle(rays
, vertex
->p
, one
, 1, options
);
332 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
333 assert(value_cmp_si(mpq_denref(t
.count
), 24) == 0);
335 set_from_string(rays
, "[[0 -1][1 -1]]");
336 set_from_string(v
, "[0 2 1]");
337 zz2values(v
, vertex
->p
);
339 t
.handle(rays
, vertex
->p
, one
, 1, options
);
340 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
341 assert(value_cmp_si(mpq_denref(t
.count
), 12) == 0);
343 set_from_string(rays
, "[[1 0][0 1]]");
344 set_from_string(v
, "[0 0 1]");
345 zz2values(v
, vertex
->p
);
347 t
.handle(rays
, vertex
->p
, one
, 1, options
);
348 assert(value_cmp_si(mpq_numref(t
.count
), 6) == 0);
349 assert(value_cmp_si(mpq_denref(t
.count
), 1) == 0);
354 int test_bernoulli(struct barvinok_options
*options
)
356 struct bernoulli_coef
*bernoulli_coef
;
357 struct poly_list
*faulhaber
, *bernoulli
;
358 bernoulli_coef
= bernoulli_coef_compute(2);
359 faulhaber
= faulhaber_compute(4);
360 bernoulli_coef
= bernoulli_coef_compute(8);
361 assert(value_cmp_si(bernoulli_coef
->num
->p
[6], 1) == 0);
362 assert(value_cmp_si(bernoulli_coef
->den
->p
[6], 42) == 0);
363 assert(value_cmp_si(faulhaber
->poly
[3]->p
[0], 0) == 0);
364 assert(value_cmp_si(faulhaber
->poly
[3]->p
[1], 0) == 0);
365 assert(value_cmp_si(faulhaber
->poly
[3]->p
[2], 1) == 0);
366 assert(value_cmp_si(faulhaber
->poly
[3]->p
[3], -2) == 0);
367 assert(value_cmp_si(faulhaber
->poly
[3]->p
[4], 1) == 0);
368 assert(value_cmp_si(faulhaber
->poly
[3]->p
[5], 4) == 0);
370 bernoulli
= bernoulli_compute(6);
371 assert(value_cmp_si(bernoulli
->poly
[6]->p
[0], 1) == 0);
372 assert(value_cmp_si(bernoulli
->poly
[6]->p
[1], 0) == 0);
373 assert(value_cmp_si(bernoulli
->poly
[6]->p
[2], -21) == 0);
374 assert(value_cmp_si(bernoulli
->poly
[6]->p
[3], 0) == 0);
375 assert(value_cmp_si(bernoulli
->poly
[6]->p
[4], 105) == 0);
376 assert(value_cmp_si(bernoulli
->poly
[6]->p
[5], -126) == 0);
377 assert(value_cmp_si(bernoulli
->poly
[6]->p
[6], 42) == 0);
378 assert(value_cmp_si(bernoulli
->poly
[6]->p
[7], 42) == 0);
380 unsigned nvar
, nparam
;
382 evalue
*base
, *sum1
, *sum2
;
383 base
= evalue_read_from_str("(1 * n + 1)", NULL
, &all_vars
, &nvar
, &nparam
,
386 sum1
= evalue_polynomial(faulhaber
->poly
[3], base
);
387 Free_ParamNames(all_vars
, nvar
+nparam
);
389 sum2
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
390 NULL
, &all_vars
, &nvar
, &nparam
,
392 Free_ParamNames(all_vars
, nvar
+nparam
);
393 assert(eequal(sum1
, sum2
));
399 int test_bernoulli_sum(struct barvinok_options
*options
)
401 unsigned nvar
, nparam
;
403 evalue
*e
, *sum1
, *sum2
;
404 e
= evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
405 "i", &all_vars
, &nvar
, &nparam
,
407 Free_ParamNames(all_vars
, nvar
+nparam
);
409 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
410 sum2
= evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
411 NULL
, &all_vars
, &nvar
, &nparam
,
413 Free_ParamNames(all_vars
, nvar
+nparam
);
417 assert(EVALUE_IS_ZERO(*sum1
));
421 e
= evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
422 "i", &all_vars
, &nvar
, &nparam
,
424 Free_ParamNames(all_vars
, nvar
+nparam
);
425 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
429 assert(EVALUE_IS_ZERO(*sum1
));
435 e
= evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
436 "i", &all_vars
, &nvar
, &nparam
,
438 Free_ParamNames(all_vars
, nvar
+nparam
);
439 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
440 sum2
= evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
441 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
442 NULL
, &all_vars
, &nvar
, &nparam
,
444 Free_ParamNames(all_vars
, nvar
+nparam
);
448 assert(EVALUE_IS_ZERO(*sum1
));
453 e
= evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
454 "k -1 >= 0\n j -k >= 0\n\n1",
455 "i,j,k", &all_vars
, &nvar
, &nparam
,
457 Free_ParamNames(all_vars
, nvar
+nparam
);
458 sum1
= Bernoulli_sum_evalue(e
, 3, options
);
459 sum2
= evalue_read_from_str("n -5 >= 0\n\n"
460 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
461 NULL
, &all_vars
, &nvar
, &nparam
,
463 Free_ParamNames(all_vars
, nvar
+nparam
);
467 assert(EVALUE_IS_ZERO(*sum1
));
473 int test_hilbert(struct barvinok_options
*options
)
475 Matrix
*M
= matrix_read_from_str(
479 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
482 M
= Cone_Hilbert_Basis(P
, options
->MaxRays
);
484 assert(M
->NbRows
= 5);
485 assert(M
->NbColumns
= 3);
489 int main(int argc
, char **argv
)
491 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
492 test_evalue(options
);
493 test_split_periods(options
);
494 test_specialization(options
);
495 test_lattice_points(options
);
496 test_icounter(options
);
497 test_infinite_counter(options
);
498 test_series(options
);
500 test_bernoulli(options
);
501 test_bernoulli_sum(options
);
502 test_hilbert(options
);
503 barvinok_options_free(options
);