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"
16 #include "matrix_read.h"
23 void set_from_string(T
& v
, const char *s
)
25 std::istringstream
str(s
);
29 int test_evalue(struct barvinok_options
*options
)
31 unsigned nvar
, nparam
;
35 poly1
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
36 NULL
, &all_vars
, &nvar
, &nparam
,
38 Free_ParamNames(all_vars
, nvar
+nparam
);
41 evalue_copy(&poly2
, poly1
);
45 assert(EVALUE_IS_ZERO(*poly1
));
47 free_evalue_refs(&poly2
);
50 int test_split_periods(struct barvinok_options
*options
)
52 unsigned nvar
, nparam
;
56 e
= evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
57 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
58 NULL
, &all_vars
, &nvar
, &nparam
,
60 Free_ParamNames(all_vars
, nvar
+nparam
);
62 evalue_split_periods(e
, 2, options
->MaxRays
);
63 assert(value_zero_p(e
->d
));
64 assert(e
->x
.p
->type
== partition
);
65 assert(e
->x
.p
->size
== 4);
66 assert(value_zero_p(e
->x
.p
->arr
[1].d
));
67 assert(e
->x
.p
->arr
[1].x
.p
->type
== polynomial
);
68 assert(value_zero_p(e
->x
.p
->arr
[3].d
));
69 assert(e
->x
.p
->arr
[3].x
.p
->type
== polynomial
);
73 int test_specialization(struct barvinok_options
*options
)
82 assert(value_cmp_si(n
.coeff
->p
[0], 1) == 0);
83 assert(value_cmp_si(n
.coeff
->p
[1], 5) == 0);
84 assert(value_cmp_si(n
.coeff
->p
[2], 10) == 0);
91 assert(value_cmp_si(d
.coeff
->p
[0], 2) == 0);
92 assert(value_cmp_si(d
.coeff
->p
[1], 1) == 0);
93 assert(value_cmp_si(d
.coeff
->p
[2], 0) == 0);
96 mpq_canonicalize(count
);
97 assert(value_cmp_si(mpq_numref(count
), 31) == 0);
98 assert(value_cmp_si(mpq_denref(count
), 8) == 0);
102 assert(value_cmp_si(n2
.coeff
->p
[0], 1) == 0);
103 assert(value_cmp_si(n2
.coeff
->p
[1], -2) == 0);
104 assert(value_cmp_si(n2
.coeff
->p
[2], 3) == 0);
107 mpq_canonicalize(count
);
108 assert(value_cmp_si(mpq_numref(count
), 6) == 0);
109 assert(value_cmp_si(mpq_denref(count
), 1) == 0);
115 int test_lattice_points(struct barvinok_options
*options
)
119 set_from_string(tmp
, "[[0 0 0 0 4][0 0 0 0 4][-1 0 1 0 4]]");
120 V
.Vertex
= zz2matrix(tmp
);
122 set_from_string(lambda
, "[3 5 7]");
124 set_from_string(rays
, "[[0 1 0][4 0 1][0 0 -1]]");
128 unsigned nvar
, nparam
;
130 point
[0] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
131 "( 7 * {( 1/4 * a + ( 3/4 * c + 3/4 ) ) } + -21/4 ) ) )",
132 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
133 Free_ParamNames(all_vars
, nvar
+nparam
);
134 point
[1] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
135 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/2 ) ) } + -1/2 ) ) )",
136 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
137 Free_ParamNames(all_vars
, nvar
+nparam
);
138 point
[2] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
139 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/4 ) ) } + 17/4 ) ) )",
140 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
141 Free_ParamNames(all_vars
, nvar
+nparam
);
142 point
[3] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
143 "( 7 * {( 1/4 * a + ( 3/4 * c + 0 ) ) } + 9 ) ) )",
144 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
145 Free_ParamNames(all_vars
, nvar
+nparam
);
147 lattice_point(&V
, rays
, lambda
, &num
, 4, options
);
148 Matrix_Free(V
.Vertex
);
150 for (int i
= 0; i
< 4; ++i
) {
151 assert(eequal(num
.E
[i
], point
[i
]));
152 evalue_free(point
[i
]);
153 free_evalue_refs(num
.E
[i
]);
159 static int test_icounter(struct barvinok_options
*options
)
165 set_from_string(n_coeff
, "[-2/1 1/1]");
166 set_from_string(n_power
, "[[2 6][3 6]]");
167 d_power
.SetDims(0, 2);
168 cnt
.reduce(n_coeff
, n_power
, d_power
);
169 assert(value_cmp_si(mpq_numref(cnt
.count
), -1) == 0);
170 assert(value_cmp_si(mpq_denref(cnt
.count
), 1) == 0);
173 static Matrix
*matrix_read_from_str(const char *s
)
175 std::istringstream
str(s
);
176 return Matrix_Read(str
);
179 static int test_infinite_counter(struct barvinok_options
*options
)
181 Matrix
*M
= matrix_read_from_str("1 3\n 1 1 0\n");
182 Polyhedron
*ctx
= Constraints2Polyhedron(M
, options
->MaxRays
);
185 /* (1 -1/2 x^5 - 1/2 x^7)/(1-x) */
186 infinite_counter
*cnt
= new infinite_counter(1, 1);
191 set_from_string(n_coeff
, "[1/1 -1/2 -1/2]");
192 set_from_string(n_power
, "[[0][5][7]]");
193 set_from_string(d_power
, "[[1]]");
194 cnt
->reduce(n_coeff
, n_power
, d_power
);
195 assert(value_cmp_si(mpq_numref(cnt
->count
[0]), 6) == 0);
196 assert(value_cmp_si(mpq_denref(cnt
->count
[0]), 1) == 0);
197 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) == 0);
198 assert(value_cmp_si(mpq_denref(cnt
->count
[1]), 1) == 0);
200 Polyhedron_Free(ctx
);
202 M
= matrix_read_from_str("2 4\n 1 1 0 0\n 1 0 1 0\n");
203 ctx
= Constraints2Polyhedron(M
, options
->MaxRays
);
206 /* (1 - xy)/((1-x)(1-xy)) */
207 cnt
= new infinite_counter(2, 3);
209 set_from_string(n_coeff
, "[1/1 -1/1]");
210 set_from_string(n_power
, "[[0 0][1 1]]");
211 set_from_string(d_power
, "[[1 0][1 1]]");
212 cnt
->reduce(n_coeff
, n_power
, d_power
);
213 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) != 0);
214 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
215 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
216 assert(value_cmp_si(mpq_numref(cnt
->count
[3]), 0) == 0);
217 assert(value_cmp_si(mpq_denref(cnt
->count
[3]), 1) == 0);
220 cnt
= new infinite_counter(2, 2);
222 set_from_string(n_coeff
, "[-1/2 1/1 -1/3]");
223 set_from_string(n_power
, "[[2 6][3 6]]");
224 d_power
.SetDims(0, 2);
225 cnt
->reduce(n_coeff
, n_power
, d_power
);
226 assert(value_cmp_si(mpq_numref(cnt
->count
[0]), 1) == 0);
227 assert(value_cmp_si(mpq_denref(cnt
->count
[0]), 6) == 0);
228 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) == 0);
229 assert(value_cmp_si(mpq_denref(cnt
->count
[1]), 1) == 0);
230 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
231 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
234 cnt
= new infinite_counter(2, 2);
236 set_from_string(n_coeff
, "[1/1]");
237 set_from_string(n_power
, "[[0 11]]");
238 set_from_string(d_power
, "[[0 1]]");
239 cnt
->reduce(n_coeff
, n_power
, d_power
);
240 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) != 0);
241 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
242 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
245 Polyhedron_Free(ctx
);
250 static int test_series(struct barvinok_options
*options
)
252 Matrix
*M
= matrix_read_from_str(
254 " 0 1 0 0 0 0 0 1 0 0 3 \n"
255 " 0 0 1 0 0 0 0 -1 1 0 -5 \n"
256 " 0 0 0 1 0 0 0 0 -2 -1 6 \n"
257 " 0 0 0 0 1 0 0 1 1 0 5 \n"
258 " 0 0 0 0 0 1 0 0 -1 0 0 \n"
259 " 0 0 0 0 0 0 1 -2 0 -1 -3 \n"
260 " 1 0 0 0 0 0 0 2 0 1 3 \n"
261 " 1 0 0 0 0 0 0 1 -1 0 5 \n"
262 " 1 0 0 0 0 0 0 -1 -1 0 -5 \n"
263 " 1 0 0 0 0 0 0 -1 0 0 -3 \n"
264 " 1 0 0 0 0 0 0 0 2 1 -6 \n"
265 " 1 0 0 0 0 0 0 0 1 0 0 \n");
266 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
268 Polyhedron
*C
= Universe_Polyhedron(3);
269 gen_fun
*gf
= barvinok_series_with_options(P
, C
, options
);
274 M
= matrix_read_from_str(
276 " 0 1 1 0 0 1 0 2 \n"
277 " 0 0 0 1 0 -2 0 6 \n"
278 " 0 0 0 0 1 -1 0 -1 \n"
279 " 0 0 0 0 0 0 1 0 \n"
280 " 1 0 1 0 0 0 0 0 \n"
281 " 1 0 -1 0 0 -1 0 -2 \n"
282 " 1 0 0 0 0 1 0 -3 \n");
283 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
285 C
= Universe_Polyhedron(2);
286 gf
= barvinok_series_with_options(P
, C
, options
);
291 M
= matrix_read_from_str(
295 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
297 C
= Universe_Polyhedron(1);
298 gf
= barvinok_series_with_options(P
, C
, options
);
301 gen_fun
*sum
= gf
->summate(1, options
);
308 int test_todd(struct barvinok_options
*options
)
310 tcounter
t(2, options
->max_index
);
311 assert(value_cmp_si(t
.todd
.coeff
->p
[0], 1) == 0);
312 assert(value_cmp_si(t
.todd
.coeff
->p
[1], -3) == 0);
313 assert(value_cmp_si(t
.todd
.coeff
->p
[2], 3) == 0);
314 assert(value_cmp_si(t
.todd_denom
->p
[0], 1) == 0);
315 assert(value_cmp_si(t
.todd_denom
->p
[1], 6) == 0);
316 assert(value_cmp_si(t
.todd_denom
->p
[2], 36) == 0);
319 set_from_string(lambda
, "[1 -1]");
320 zz2values(lambda
, t
.lambda
->p
);
323 set_from_string(rays
, "[[-1 0][-1 1]]");
328 set_from_string(v
, "[2 0 1]");
329 Vector
*vertex
= Vector_Alloc(3);
330 zz2values(v
, vertex
->p
);
332 t
.handle(rays
, vertex
->p
, one
, 1, options
);
333 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
334 assert(value_cmp_si(mpq_denref(t
.count
), 24) == 0);
336 set_from_string(rays
, "[[0 -1][1 -1]]");
337 set_from_string(v
, "[0 2 1]");
338 zz2values(v
, vertex
->p
);
340 t
.handle(rays
, vertex
->p
, one
, 1, options
);
341 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
342 assert(value_cmp_si(mpq_denref(t
.count
), 12) == 0);
344 set_from_string(rays
, "[[1 0][0 1]]");
345 set_from_string(v
, "[0 0 1]");
346 zz2values(v
, vertex
->p
);
348 t
.handle(rays
, vertex
->p
, one
, 1, options
);
349 assert(value_cmp_si(mpq_numref(t
.count
), 6) == 0);
350 assert(value_cmp_si(mpq_denref(t
.count
), 1) == 0);
355 int test_bernoulli(struct barvinok_options
*options
)
357 struct bernoulli_coef
*bernoulli_coef
;
358 struct poly_list
*faulhaber
, *bernoulli
;
359 bernoulli_coef
= bernoulli_coef_compute(2);
360 faulhaber
= faulhaber_compute(4);
361 bernoulli_coef
= bernoulli_coef_compute(8);
362 assert(value_cmp_si(bernoulli_coef
->num
->p
[6], 1) == 0);
363 assert(value_cmp_si(bernoulli_coef
->den
->p
[6], 42) == 0);
364 assert(value_cmp_si(faulhaber
->poly
[3]->p
[0], 0) == 0);
365 assert(value_cmp_si(faulhaber
->poly
[3]->p
[1], 0) == 0);
366 assert(value_cmp_si(faulhaber
->poly
[3]->p
[2], 1) == 0);
367 assert(value_cmp_si(faulhaber
->poly
[3]->p
[3], -2) == 0);
368 assert(value_cmp_si(faulhaber
->poly
[3]->p
[4], 1) == 0);
369 assert(value_cmp_si(faulhaber
->poly
[3]->p
[5], 4) == 0);
371 bernoulli
= bernoulli_compute(6);
372 assert(value_cmp_si(bernoulli
->poly
[6]->p
[0], 1) == 0);
373 assert(value_cmp_si(bernoulli
->poly
[6]->p
[1], 0) == 0);
374 assert(value_cmp_si(bernoulli
->poly
[6]->p
[2], -21) == 0);
375 assert(value_cmp_si(bernoulli
->poly
[6]->p
[3], 0) == 0);
376 assert(value_cmp_si(bernoulli
->poly
[6]->p
[4], 105) == 0);
377 assert(value_cmp_si(bernoulli
->poly
[6]->p
[5], -126) == 0);
378 assert(value_cmp_si(bernoulli
->poly
[6]->p
[6], 42) == 0);
379 assert(value_cmp_si(bernoulli
->poly
[6]->p
[7], 42) == 0);
381 unsigned nvar
, nparam
;
383 evalue
*base
, *sum1
, *sum2
;
384 base
= evalue_read_from_str("(1 * n + 1)", NULL
, &all_vars
, &nvar
, &nparam
,
387 sum1
= evalue_polynomial(faulhaber
->poly
[3], base
);
388 Free_ParamNames(all_vars
, nvar
+nparam
);
390 sum2
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
391 NULL
, &all_vars
, &nvar
, &nparam
,
393 Free_ParamNames(all_vars
, nvar
+nparam
);
394 assert(eequal(sum1
, sum2
));
400 int test_bernoulli_sum(struct barvinok_options
*options
)
402 unsigned nvar
, nparam
;
404 evalue
*e
, *sum1
, *sum2
;
405 e
= evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
406 "i", &all_vars
, &nvar
, &nparam
,
408 Free_ParamNames(all_vars
, nvar
+nparam
);
410 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
411 sum2
= evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
412 NULL
, &all_vars
, &nvar
, &nparam
,
414 Free_ParamNames(all_vars
, nvar
+nparam
);
418 assert(EVALUE_IS_ZERO(*sum1
));
422 e
= evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
423 "i", &all_vars
, &nvar
, &nparam
,
425 Free_ParamNames(all_vars
, nvar
+nparam
);
426 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
430 assert(EVALUE_IS_ZERO(*sum1
));
436 e
= evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
437 "i", &all_vars
, &nvar
, &nparam
,
439 Free_ParamNames(all_vars
, nvar
+nparam
);
440 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
441 sum2
= evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
442 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
443 NULL
, &all_vars
, &nvar
, &nparam
,
445 Free_ParamNames(all_vars
, nvar
+nparam
);
449 assert(EVALUE_IS_ZERO(*sum1
));
454 e
= evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
455 "k -1 >= 0\n j -k >= 0\n\n1",
456 "i,j,k", &all_vars
, &nvar
, &nparam
,
458 Free_ParamNames(all_vars
, nvar
+nparam
);
459 sum1
= Bernoulli_sum_evalue(e
, 3, options
);
460 sum2
= evalue_read_from_str("n -5 >= 0\n\n"
461 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
462 NULL
, &all_vars
, &nvar
, &nparam
,
464 Free_ParamNames(all_vars
, nvar
+nparam
);
468 assert(EVALUE_IS_ZERO(*sum1
));
474 int test_hilbert(struct barvinok_options
*options
)
476 Matrix
*M
= matrix_read_from_str(
480 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
483 M
= Cone_Hilbert_Basis(P
, options
->MaxRays
);
484 assert(M
->NbRows
= 5);
485 assert(M
->NbColumns
= 3);
488 M
= Cone_Integer_Hull(P
, options
);
489 assert(M
->NbRows
= 4);
490 assert(M
->NbColumns
= 3);
496 int test_ilp(struct barvinok_options
*options
)
498 Matrix
*M
= matrix_read_from_str(
502 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
504 Vector
*obj
= Vector_Alloc(2);
505 value_set_si(obj
->p
[0], 7);
506 value_set_si(obj
->p
[1], -1);
511 value_set_si(min
, 1);
512 value_set_si(max
, 17);
513 Vector
*opt
= Polyhedron_Integer_Minimum(P
, obj
->p
, min
, max
,
516 assert(value_cmp_si(opt
->p
[0], 1) == 0);
517 assert(value_cmp_si(opt
->p
[1], 1) == 0);
518 assert(value_cmp_si(opt
->p
[2], 1) == 0);
527 int main(int argc
, char **argv
)
529 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
530 test_evalue(options
);
531 test_split_periods(options
);
532 test_specialization(options
);
533 test_lattice_points(options
);
534 test_icounter(options
);
535 test_infinite_counter(options
);
536 test_series(options
);
538 test_bernoulli(options
);
539 test_bernoulli_sum(options
);
540 test_hilbert(options
);
542 barvinok_options_free(options
);