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"
17 #include "matrix_read.h"
25 void set_from_string(T
& v
, const char *s
)
27 std::istringstream
str(s
);
31 int test_evalue_read(struct barvinok_options
*options
)
33 unsigned nvar
, nparam
;
37 e1
= evalue_read_from_str("(1 * aa + 2 * a)",
38 NULL
, &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
39 Free_ParamNames(all_vars
, nvar
+nparam
);
40 e2
= evalue_read_from_str("(3 * aa)",
41 NULL
, &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
42 Free_ParamNames(all_vars
, nvar
+nparam
);
43 assert(!eequal(e1
, e2
));
48 int test_evalue(struct barvinok_options
*options
)
50 unsigned nvar
, nparam
;
54 poly1
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
55 NULL
, &all_vars
, &nvar
, &nparam
,
57 Free_ParamNames(all_vars
, nvar
+nparam
);
60 evalue_copy(&poly2
, poly1
);
64 assert(EVALUE_IS_ZERO(*poly1
));
66 free_evalue_refs(&poly2
);
69 int test_split_periods(struct barvinok_options
*options
)
71 unsigned nvar
, nparam
;
75 e
= evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
76 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
77 NULL
, &all_vars
, &nvar
, &nparam
,
79 Free_ParamNames(all_vars
, nvar
+nparam
);
81 evalue_split_periods(e
, 2, options
->MaxRays
);
82 assert(value_zero_p(e
->d
));
83 assert(e
->x
.p
->type
== partition
);
84 assert(e
->x
.p
->size
== 4);
85 assert(value_zero_p(e
->x
.p
->arr
[1].d
));
86 assert(e
->x
.p
->arr
[1].x
.p
->type
== polynomial
);
87 assert(value_zero_p(e
->x
.p
->arr
[3].d
));
88 assert(e
->x
.p
->arr
[3].x
.p
->type
== polynomial
);
92 int test_specialization(struct barvinok_options
*options
)
101 assert(value_cmp_si(n
.coeff
->p
[0], 1) == 0);
102 assert(value_cmp_si(n
.coeff
->p
[1], 5) == 0);
103 assert(value_cmp_si(n
.coeff
->p
[2], 10) == 0);
110 assert(value_cmp_si(d
.coeff
->p
[0], 2) == 0);
111 assert(value_cmp_si(d
.coeff
->p
[1], 1) == 0);
112 assert(value_cmp_si(d
.coeff
->p
[2], 0) == 0);
115 mpq_canonicalize(count
);
116 assert(value_cmp_si(mpq_numref(count
), 31) == 0);
117 assert(value_cmp_si(mpq_denref(count
), 8) == 0);
121 assert(value_cmp_si(n2
.coeff
->p
[0], 1) == 0);
122 assert(value_cmp_si(n2
.coeff
->p
[1], -2) == 0);
123 assert(value_cmp_si(n2
.coeff
->p
[2], 3) == 0);
126 mpq_canonicalize(count
);
127 assert(value_cmp_si(mpq_numref(count
), 6) == 0);
128 assert(value_cmp_si(mpq_denref(count
), 1) == 0);
134 int test_lattice_points(struct barvinok_options
*options
)
138 set_from_string(tmp
, "[[0 0 0 0 4][0 0 0 0 4][-1 0 1 0 4]]");
139 V
.Vertex
= zz2matrix(tmp
);
141 set_from_string(lambda
, "[3 5 7]");
143 set_from_string(rays
, "[[0 1 0][4 0 1][0 0 -1]]");
147 unsigned nvar
, nparam
;
149 point
[0] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
150 "( 7 * {( 1/4 * a + ( 3/4 * c + 3/4 ) ) } + -21/4 ) ) )",
151 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
152 Free_ParamNames(all_vars
, nvar
+nparam
);
153 point
[1] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
154 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/2 ) ) } + -1/2 ) ) )",
155 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
156 Free_ParamNames(all_vars
, nvar
+nparam
);
157 point
[2] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
158 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/4 ) ) } + 17/4 ) ) )",
159 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
160 Free_ParamNames(all_vars
, nvar
+nparam
);
161 point
[3] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
162 "( 7 * {( 1/4 * a + ( 3/4 * c + 0 ) ) } + 9 ) ) )",
163 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
164 Free_ParamNames(all_vars
, nvar
+nparam
);
166 lattice_point(&V
, rays
, lambda
, &num
, 4, options
);
167 Matrix_Free(V
.Vertex
);
169 for (int i
= 0; i
< 4; ++i
) {
170 assert(eequal(num
.E
[i
], point
[i
]));
171 evalue_free(point
[i
]);
172 free_evalue_refs(num
.E
[i
]);
178 static int test_icounter(struct barvinok_options
*options
)
184 set_from_string(n_coeff
, "[-2/1 1/1]");
185 set_from_string(n_power
, "[[2 6][3 6]]");
186 d_power
.SetDims(0, 2);
187 cnt
.reduce(n_coeff
, n_power
, d_power
);
188 assert(value_cmp_si(mpq_numref(cnt
.count
), -1) == 0);
189 assert(value_cmp_si(mpq_denref(cnt
.count
), 1) == 0);
192 static Matrix
*matrix_read_from_str(const char *s
)
194 std::istringstream
str(s
);
195 return Matrix_Read(str
);
198 static int test_infinite_counter(struct barvinok_options
*options
)
200 Matrix
*M
= matrix_read_from_str("1 3\n 1 1 0\n");
201 Polyhedron
*ctx
= Constraints2Polyhedron(M
, options
->MaxRays
);
204 /* (1 -1/2 x^5 - 1/2 x^7)/(1-x) */
205 infinite_counter
*cnt
= new infinite_counter(1, 1);
210 set_from_string(n_coeff
, "[1/1 -1/2 -1/2]");
211 set_from_string(n_power
, "[[0][5][7]]");
212 set_from_string(d_power
, "[[1]]");
213 cnt
->reduce(n_coeff
, n_power
, d_power
);
214 assert(value_cmp_si(mpq_numref(cnt
->count
[0]), 6) == 0);
215 assert(value_cmp_si(mpq_denref(cnt
->count
[0]), 1) == 0);
216 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) == 0);
217 assert(value_cmp_si(mpq_denref(cnt
->count
[1]), 1) == 0);
219 Polyhedron_Free(ctx
);
221 M
= matrix_read_from_str("2 4\n 1 1 0 0\n 1 0 1 0\n");
222 ctx
= Constraints2Polyhedron(M
, options
->MaxRays
);
225 /* (1 - xy)/((1-x)(1-xy)) */
226 cnt
= new infinite_counter(2, 3);
228 set_from_string(n_coeff
, "[1/1 -1/1]");
229 set_from_string(n_power
, "[[0 0][1 1]]");
230 set_from_string(d_power
, "[[1 0][1 1]]");
231 cnt
->reduce(n_coeff
, n_power
, d_power
);
232 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) != 0);
233 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
234 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
235 assert(value_cmp_si(mpq_numref(cnt
->count
[3]), 0) == 0);
236 assert(value_cmp_si(mpq_denref(cnt
->count
[3]), 1) == 0);
239 cnt
= new infinite_counter(2, 2);
241 set_from_string(n_coeff
, "[-1/2 1/1 -1/3]");
242 set_from_string(n_power
, "[[2 6][3 6]]");
243 d_power
.SetDims(0, 2);
244 cnt
->reduce(n_coeff
, n_power
, d_power
);
245 assert(value_cmp_si(mpq_numref(cnt
->count
[0]), 1) == 0);
246 assert(value_cmp_si(mpq_denref(cnt
->count
[0]), 6) == 0);
247 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) == 0);
248 assert(value_cmp_si(mpq_denref(cnt
->count
[1]), 1) == 0);
249 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
250 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
253 cnt
= new infinite_counter(2, 2);
255 set_from_string(n_coeff
, "[1/1]");
256 set_from_string(n_power
, "[[0 11]]");
257 set_from_string(d_power
, "[[0 1]]");
258 cnt
->reduce(n_coeff
, n_power
, d_power
);
259 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) != 0);
260 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
261 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
264 Polyhedron_Free(ctx
);
269 static int test_series(struct barvinok_options
*options
)
271 Matrix
*M
= matrix_read_from_str(
273 " 0 1 0 0 0 0 0 1 0 0 3 \n"
274 " 0 0 1 0 0 0 0 -1 1 0 -5 \n"
275 " 0 0 0 1 0 0 0 0 -2 -1 6 \n"
276 " 0 0 0 0 1 0 0 1 1 0 5 \n"
277 " 0 0 0 0 0 1 0 0 -1 0 0 \n"
278 " 0 0 0 0 0 0 1 -2 0 -1 -3 \n"
279 " 1 0 0 0 0 0 0 2 0 1 3 \n"
280 " 1 0 0 0 0 0 0 1 -1 0 5 \n"
281 " 1 0 0 0 0 0 0 -1 -1 0 -5 \n"
282 " 1 0 0 0 0 0 0 -1 0 0 -3 \n"
283 " 1 0 0 0 0 0 0 0 2 1 -6 \n"
284 " 1 0 0 0 0 0 0 0 1 0 0 \n");
285 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
287 Polyhedron
*C
= Universe_Polyhedron(3);
288 gen_fun
*gf
= barvinok_series_with_options(P
, C
, options
);
293 M
= matrix_read_from_str(
295 " 0 1 1 0 0 1 0 2 \n"
296 " 0 0 0 1 0 -2 0 6 \n"
297 " 0 0 0 0 1 -1 0 -1 \n"
298 " 0 0 0 0 0 0 1 0 \n"
299 " 1 0 1 0 0 0 0 0 \n"
300 " 1 0 -1 0 0 -1 0 -2 \n"
301 " 1 0 0 0 0 1 0 -3 \n");
302 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
304 C
= Universe_Polyhedron(2);
305 gf
= barvinok_series_with_options(P
, C
, options
);
310 M
= matrix_read_from_str(
314 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
316 C
= Universe_Polyhedron(1);
317 gf
= barvinok_series_with_options(P
, C
, options
);
320 gen_fun
*sum
= gf
->summate(1, options
);
327 int test_todd(struct barvinok_options
*options
)
329 tcounter
t(2, options
->max_index
);
330 assert(value_cmp_si(t
.todd
.coeff
->p
[0], 1) == 0);
331 assert(value_cmp_si(t
.todd
.coeff
->p
[1], -3) == 0);
332 assert(value_cmp_si(t
.todd
.coeff
->p
[2], 3) == 0);
333 assert(value_cmp_si(t
.todd_denom
->p
[0], 1) == 0);
334 assert(value_cmp_si(t
.todd_denom
->p
[1], 6) == 0);
335 assert(value_cmp_si(t
.todd_denom
->p
[2], 36) == 0);
338 set_from_string(lambda
, "[1 -1]");
339 zz2values(lambda
, t
.lambda
->p
);
342 set_from_string(rays
, "[[-1 0][-1 1]]");
347 set_from_string(v
, "[2 0 1]");
348 Vector
*vertex
= Vector_Alloc(3);
349 zz2values(v
, vertex
->p
);
351 t
.handle(rays
, vertex
->p
, one
, 1, options
);
352 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
353 assert(value_cmp_si(mpq_denref(t
.count
), 24) == 0);
355 set_from_string(rays
, "[[0 -1][1 -1]]");
356 set_from_string(v
, "[0 2 1]");
357 zz2values(v
, vertex
->p
);
359 t
.handle(rays
, vertex
->p
, one
, 1, options
);
360 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
361 assert(value_cmp_si(mpq_denref(t
.count
), 12) == 0);
363 set_from_string(rays
, "[[1 0][0 1]]");
364 set_from_string(v
, "[0 0 1]");
365 zz2values(v
, vertex
->p
);
367 t
.handle(rays
, vertex
->p
, one
, 1, options
);
368 assert(value_cmp_si(mpq_numref(t
.count
), 6) == 0);
369 assert(value_cmp_si(mpq_denref(t
.count
), 1) == 0);
374 int test_bernoulli(struct barvinok_options
*options
)
376 struct bernoulli_coef
*bernoulli_coef
;
377 struct poly_list
*faulhaber
, *bernoulli
;
378 bernoulli_coef
= bernoulli_coef_compute(2);
379 faulhaber
= faulhaber_compute(4);
380 bernoulli_coef
= bernoulli_coef_compute(8);
381 assert(value_cmp_si(bernoulli_coef
->num
->p
[6], 1) == 0);
382 assert(value_cmp_si(bernoulli_coef
->den
->p
[6], 42) == 0);
383 assert(value_cmp_si(faulhaber
->poly
[3]->p
[0], 0) == 0);
384 assert(value_cmp_si(faulhaber
->poly
[3]->p
[1], 0) == 0);
385 assert(value_cmp_si(faulhaber
->poly
[3]->p
[2], 1) == 0);
386 assert(value_cmp_si(faulhaber
->poly
[3]->p
[3], -2) == 0);
387 assert(value_cmp_si(faulhaber
->poly
[3]->p
[4], 1) == 0);
388 assert(value_cmp_si(faulhaber
->poly
[3]->p
[5], 4) == 0);
390 bernoulli
= bernoulli_compute(6);
391 assert(value_cmp_si(bernoulli
->poly
[6]->p
[0], 1) == 0);
392 assert(value_cmp_si(bernoulli
->poly
[6]->p
[1], 0) == 0);
393 assert(value_cmp_si(bernoulli
->poly
[6]->p
[2], -21) == 0);
394 assert(value_cmp_si(bernoulli
->poly
[6]->p
[3], 0) == 0);
395 assert(value_cmp_si(bernoulli
->poly
[6]->p
[4], 105) == 0);
396 assert(value_cmp_si(bernoulli
->poly
[6]->p
[5], -126) == 0);
397 assert(value_cmp_si(bernoulli
->poly
[6]->p
[6], 42) == 0);
398 assert(value_cmp_si(bernoulli
->poly
[6]->p
[7], 42) == 0);
400 unsigned nvar
, nparam
;
402 evalue
*base
, *sum1
, *sum2
;
403 base
= evalue_read_from_str("(1 * n + 1)", NULL
, &all_vars
, &nvar
, &nparam
,
406 sum1
= evalue_polynomial(faulhaber
->poly
[3], base
);
407 Free_ParamNames(all_vars
, nvar
+nparam
);
409 sum2
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
410 NULL
, &all_vars
, &nvar
, &nparam
,
412 Free_ParamNames(all_vars
, nvar
+nparam
);
413 assert(eequal(sum1
, sum2
));
419 int test_bernoulli_sum(struct barvinok_options
*options
)
421 unsigned nvar
, nparam
;
423 evalue
*e
, *sum1
, *sum2
;
424 e
= evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
425 "i", &all_vars
, &nvar
, &nparam
,
427 Free_ParamNames(all_vars
, nvar
+nparam
);
429 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
430 sum2
= evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
431 NULL
, &all_vars
, &nvar
, &nparam
,
433 Free_ParamNames(all_vars
, nvar
+nparam
);
437 assert(EVALUE_IS_ZERO(*sum1
));
441 e
= evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
442 "i", &all_vars
, &nvar
, &nparam
,
444 Free_ParamNames(all_vars
, nvar
+nparam
);
445 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
449 assert(EVALUE_IS_ZERO(*sum1
));
455 e
= evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
456 "i", &all_vars
, &nvar
, &nparam
,
458 Free_ParamNames(all_vars
, nvar
+nparam
);
459 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
460 sum2
= evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
461 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
462 NULL
, &all_vars
, &nvar
, &nparam
,
464 Free_ParamNames(all_vars
, nvar
+nparam
);
468 assert(EVALUE_IS_ZERO(*sum1
));
473 e
= evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
474 "k -1 >= 0\n j -k >= 0\n\n1",
475 "i,j,k", &all_vars
, &nvar
, &nparam
,
477 Free_ParamNames(all_vars
, nvar
+nparam
);
478 sum1
= Bernoulli_sum_evalue(e
, 3, options
);
479 sum2
= evalue_read_from_str("n -5 >= 0\n\n"
480 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
481 NULL
, &all_vars
, &nvar
, &nparam
,
483 Free_ParamNames(all_vars
, nvar
+nparam
);
487 assert(EVALUE_IS_ZERO(*sum1
));
493 int test_hilbert(struct barvinok_options
*options
)
496 Matrix
*M
= matrix_read_from_str(
500 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
503 M
= Cone_Hilbert_Basis(P
, options
->MaxRays
);
504 assert(M
->NbRows
= 5);
505 assert(M
->NbColumns
= 3);
508 M
= Cone_Integer_Hull(P
, NULL
, 0, options
);
509 assert(M
->NbRows
= 4);
510 assert(M
->NbColumns
= 3);
517 int test_ilp(struct barvinok_options
*options
)
519 Matrix
*M
= matrix_read_from_str(
523 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
525 Vector
*obj
= Vector_Alloc(2);
526 value_set_si(obj
->p
[0], 7);
527 value_set_si(obj
->p
[1], -1);
532 value_set_si(min
, 1);
533 value_set_si(max
, 17);
534 Vector
*opt
= Polyhedron_Integer_Minimum(P
, obj
->p
, min
, max
,
537 assert(value_cmp_si(opt
->p
[0], 1) == 0);
538 assert(value_cmp_si(opt
->p
[1], 1) == 0);
539 assert(value_cmp_si(opt
->p
[2], 1) == 0);
548 int test_hull(struct barvinok_options
*options
)
550 Matrix
*M
= matrix_read_from_str(
556 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
559 Matrix
*hull
= Polyhedron_Integer_Hull(P
, options
);
561 assert(hull
->NbRows
== 4);
562 M
= Matrix_Alloc(hull
->NbRows
, 1+hull
->NbColumns
);
563 for (int i
= 0; i
< hull
->NbRows
; ++i
) {
564 value_set_si(M
->p
[i
][0], 1);
565 Vector_Copy(hull
->p
[i
], M
->p
[i
]+1, hull
->NbColumns
);
568 Polyhedron
*H
= Constraints2Polyhedron(M
, options
->MaxRays
);
571 M
= matrix_read_from_str(
577 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
579 assert(PolyhedronIncludes(P
, H
) && PolyhedronIncludes(H
, P
));
583 M
= matrix_read_from_str(
588 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
591 hull
= Polyhedron_Integer_Hull(P
, options
);
593 assert(hull
->NbRows
== 0);
597 int main(int argc
, char **argv
)
599 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
600 test_evalue_read(options
);
601 test_evalue(options
);
602 test_split_periods(options
);
603 test_specialization(options
);
604 test_lattice_points(options
);
605 test_icounter(options
);
606 test_infinite_counter(options
);
607 test_series(options
);
609 test_bernoulli(options
);
610 test_bernoulli_sum(options
);
611 test_hilbert(options
);
614 barvinok_options_free(options
);