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(struct barvinok_options
*options
)
33 unsigned nvar
, nparam
;
37 poly1
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
38 NULL
, &all_vars
, &nvar
, &nparam
,
40 Free_ParamNames(all_vars
, nvar
+nparam
);
43 evalue_copy(&poly2
, poly1
);
47 assert(EVALUE_IS_ZERO(*poly1
));
49 free_evalue_refs(&poly2
);
52 int test_split_periods(struct barvinok_options
*options
)
54 unsigned nvar
, nparam
;
58 e
= evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
59 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
60 NULL
, &all_vars
, &nvar
, &nparam
,
62 Free_ParamNames(all_vars
, nvar
+nparam
);
64 evalue_split_periods(e
, 2, options
->MaxRays
);
65 assert(value_zero_p(e
->d
));
66 assert(e
->x
.p
->type
== partition
);
67 assert(e
->x
.p
->size
== 4);
68 assert(value_zero_p(e
->x
.p
->arr
[1].d
));
69 assert(e
->x
.p
->arr
[1].x
.p
->type
== polynomial
);
70 assert(value_zero_p(e
->x
.p
->arr
[3].d
));
71 assert(e
->x
.p
->arr
[3].x
.p
->type
== polynomial
);
75 int test_specialization(struct barvinok_options
*options
)
84 assert(value_cmp_si(n
.coeff
->p
[0], 1) == 0);
85 assert(value_cmp_si(n
.coeff
->p
[1], 5) == 0);
86 assert(value_cmp_si(n
.coeff
->p
[2], 10) == 0);
93 assert(value_cmp_si(d
.coeff
->p
[0], 2) == 0);
94 assert(value_cmp_si(d
.coeff
->p
[1], 1) == 0);
95 assert(value_cmp_si(d
.coeff
->p
[2], 0) == 0);
98 mpq_canonicalize(count
);
99 assert(value_cmp_si(mpq_numref(count
), 31) == 0);
100 assert(value_cmp_si(mpq_denref(count
), 8) == 0);
104 assert(value_cmp_si(n2
.coeff
->p
[0], 1) == 0);
105 assert(value_cmp_si(n2
.coeff
->p
[1], -2) == 0);
106 assert(value_cmp_si(n2
.coeff
->p
[2], 3) == 0);
109 mpq_canonicalize(count
);
110 assert(value_cmp_si(mpq_numref(count
), 6) == 0);
111 assert(value_cmp_si(mpq_denref(count
), 1) == 0);
117 int test_lattice_points(struct barvinok_options
*options
)
121 set_from_string(tmp
, "[[0 0 0 0 4][0 0 0 0 4][-1 0 1 0 4]]");
122 V
.Vertex
= zz2matrix(tmp
);
124 set_from_string(lambda
, "[3 5 7]");
126 set_from_string(rays
, "[[0 1 0][4 0 1][0 0 -1]]");
130 unsigned nvar
, nparam
;
132 point
[0] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
133 "( 7 * {( 1/4 * a + ( 3/4 * c + 3/4 ) ) } + -21/4 ) ) )",
134 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
135 Free_ParamNames(all_vars
, nvar
+nparam
);
136 point
[1] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
137 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/2 ) ) } + -1/2 ) ) )",
138 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
139 Free_ParamNames(all_vars
, nvar
+nparam
);
140 point
[2] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
141 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/4 ) ) } + 17/4 ) ) )",
142 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
143 Free_ParamNames(all_vars
, nvar
+nparam
);
144 point
[3] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
145 "( 7 * {( 1/4 * a + ( 3/4 * c + 0 ) ) } + 9 ) ) )",
146 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
147 Free_ParamNames(all_vars
, nvar
+nparam
);
149 lattice_point(&V
, rays
, lambda
, &num
, 4, options
);
150 Matrix_Free(V
.Vertex
);
152 for (int i
= 0; i
< 4; ++i
) {
153 assert(eequal(num
.E
[i
], point
[i
]));
154 evalue_free(point
[i
]);
155 free_evalue_refs(num
.E
[i
]);
161 static int test_icounter(struct barvinok_options
*options
)
167 set_from_string(n_coeff
, "[-2/1 1/1]");
168 set_from_string(n_power
, "[[2 6][3 6]]");
169 d_power
.SetDims(0, 2);
170 cnt
.reduce(n_coeff
, n_power
, d_power
);
171 assert(value_cmp_si(mpq_numref(cnt
.count
), -1) == 0);
172 assert(value_cmp_si(mpq_denref(cnt
.count
), 1) == 0);
175 static Matrix
*matrix_read_from_str(const char *s
)
177 std::istringstream
str(s
);
178 return Matrix_Read(str
);
181 static int test_infinite_counter(struct barvinok_options
*options
)
183 Matrix
*M
= matrix_read_from_str("1 3\n 1 1 0\n");
184 Polyhedron
*ctx
= Constraints2Polyhedron(M
, options
->MaxRays
);
187 /* (1 -1/2 x^5 - 1/2 x^7)/(1-x) */
188 infinite_counter
*cnt
= new infinite_counter(1, 1);
193 set_from_string(n_coeff
, "[1/1 -1/2 -1/2]");
194 set_from_string(n_power
, "[[0][5][7]]");
195 set_from_string(d_power
, "[[1]]");
196 cnt
->reduce(n_coeff
, n_power
, d_power
);
197 assert(value_cmp_si(mpq_numref(cnt
->count
[0]), 6) == 0);
198 assert(value_cmp_si(mpq_denref(cnt
->count
[0]), 1) == 0);
199 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) == 0);
200 assert(value_cmp_si(mpq_denref(cnt
->count
[1]), 1) == 0);
202 Polyhedron_Free(ctx
);
204 M
= matrix_read_from_str("2 4\n 1 1 0 0\n 1 0 1 0\n");
205 ctx
= Constraints2Polyhedron(M
, options
->MaxRays
);
208 /* (1 - xy)/((1-x)(1-xy)) */
209 cnt
= new infinite_counter(2, 3);
211 set_from_string(n_coeff
, "[1/1 -1/1]");
212 set_from_string(n_power
, "[[0 0][1 1]]");
213 set_from_string(d_power
, "[[1 0][1 1]]");
214 cnt
->reduce(n_coeff
, n_power
, d_power
);
215 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) != 0);
216 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
217 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
218 assert(value_cmp_si(mpq_numref(cnt
->count
[3]), 0) == 0);
219 assert(value_cmp_si(mpq_denref(cnt
->count
[3]), 1) == 0);
222 cnt
= new infinite_counter(2, 2);
224 set_from_string(n_coeff
, "[-1/2 1/1 -1/3]");
225 set_from_string(n_power
, "[[2 6][3 6]]");
226 d_power
.SetDims(0, 2);
227 cnt
->reduce(n_coeff
, n_power
, d_power
);
228 assert(value_cmp_si(mpq_numref(cnt
->count
[0]), 1) == 0);
229 assert(value_cmp_si(mpq_denref(cnt
->count
[0]), 6) == 0);
230 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) == 0);
231 assert(value_cmp_si(mpq_denref(cnt
->count
[1]), 1) == 0);
232 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
233 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
236 cnt
= new infinite_counter(2, 2);
238 set_from_string(n_coeff
, "[1/1]");
239 set_from_string(n_power
, "[[0 11]]");
240 set_from_string(d_power
, "[[0 1]]");
241 cnt
->reduce(n_coeff
, n_power
, d_power
);
242 assert(value_cmp_si(mpq_numref(cnt
->count
[1]), 0) != 0);
243 assert(value_cmp_si(mpq_numref(cnt
->count
[2]), 0) == 0);
244 assert(value_cmp_si(mpq_denref(cnt
->count
[2]), 1) == 0);
247 Polyhedron_Free(ctx
);
252 static int test_series(struct barvinok_options
*options
)
254 Matrix
*M
= matrix_read_from_str(
256 " 0 1 0 0 0 0 0 1 0 0 3 \n"
257 " 0 0 1 0 0 0 0 -1 1 0 -5 \n"
258 " 0 0 0 1 0 0 0 0 -2 -1 6 \n"
259 " 0 0 0 0 1 0 0 1 1 0 5 \n"
260 " 0 0 0 0 0 1 0 0 -1 0 0 \n"
261 " 0 0 0 0 0 0 1 -2 0 -1 -3 \n"
262 " 1 0 0 0 0 0 0 2 0 1 3 \n"
263 " 1 0 0 0 0 0 0 1 -1 0 5 \n"
264 " 1 0 0 0 0 0 0 -1 -1 0 -5 \n"
265 " 1 0 0 0 0 0 0 -1 0 0 -3 \n"
266 " 1 0 0 0 0 0 0 0 2 1 -6 \n"
267 " 1 0 0 0 0 0 0 0 1 0 0 \n");
268 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
270 Polyhedron
*C
= Universe_Polyhedron(3);
271 gen_fun
*gf
= barvinok_series_with_options(P
, C
, options
);
276 M
= matrix_read_from_str(
278 " 0 1 1 0 0 1 0 2 \n"
279 " 0 0 0 1 0 -2 0 6 \n"
280 " 0 0 0 0 1 -1 0 -1 \n"
281 " 0 0 0 0 0 0 1 0 \n"
282 " 1 0 1 0 0 0 0 0 \n"
283 " 1 0 -1 0 0 -1 0 -2 \n"
284 " 1 0 0 0 0 1 0 -3 \n");
285 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
287 C
= Universe_Polyhedron(2);
288 gf
= barvinok_series_with_options(P
, C
, options
);
293 M
= matrix_read_from_str(
297 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
299 C
= Universe_Polyhedron(1);
300 gf
= barvinok_series_with_options(P
, C
, options
);
303 gen_fun
*sum
= gf
->summate(1, options
);
310 int test_todd(struct barvinok_options
*options
)
312 tcounter
t(2, options
->max_index
);
313 assert(value_cmp_si(t
.todd
.coeff
->p
[0], 1) == 0);
314 assert(value_cmp_si(t
.todd
.coeff
->p
[1], -3) == 0);
315 assert(value_cmp_si(t
.todd
.coeff
->p
[2], 3) == 0);
316 assert(value_cmp_si(t
.todd_denom
->p
[0], 1) == 0);
317 assert(value_cmp_si(t
.todd_denom
->p
[1], 6) == 0);
318 assert(value_cmp_si(t
.todd_denom
->p
[2], 36) == 0);
321 set_from_string(lambda
, "[1 -1]");
322 zz2values(lambda
, t
.lambda
->p
);
325 set_from_string(rays
, "[[-1 0][-1 1]]");
330 set_from_string(v
, "[2 0 1]");
331 Vector
*vertex
= Vector_Alloc(3);
332 zz2values(v
, vertex
->p
);
334 t
.handle(rays
, vertex
->p
, one
, 1, options
);
335 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
336 assert(value_cmp_si(mpq_denref(t
.count
), 24) == 0);
338 set_from_string(rays
, "[[0 -1][1 -1]]");
339 set_from_string(v
, "[0 2 1]");
340 zz2values(v
, vertex
->p
);
342 t
.handle(rays
, vertex
->p
, one
, 1, options
);
343 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
344 assert(value_cmp_si(mpq_denref(t
.count
), 12) == 0);
346 set_from_string(rays
, "[[1 0][0 1]]");
347 set_from_string(v
, "[0 0 1]");
348 zz2values(v
, vertex
->p
);
350 t
.handle(rays
, vertex
->p
, one
, 1, options
);
351 assert(value_cmp_si(mpq_numref(t
.count
), 6) == 0);
352 assert(value_cmp_si(mpq_denref(t
.count
), 1) == 0);
357 int test_bernoulli(struct barvinok_options
*options
)
359 struct bernoulli_coef
*bernoulli_coef
;
360 struct poly_list
*faulhaber
, *bernoulli
;
361 bernoulli_coef
= bernoulli_coef_compute(2);
362 faulhaber
= faulhaber_compute(4);
363 bernoulli_coef
= bernoulli_coef_compute(8);
364 assert(value_cmp_si(bernoulli_coef
->num
->p
[6], 1) == 0);
365 assert(value_cmp_si(bernoulli_coef
->den
->p
[6], 42) == 0);
366 assert(value_cmp_si(faulhaber
->poly
[3]->p
[0], 0) == 0);
367 assert(value_cmp_si(faulhaber
->poly
[3]->p
[1], 0) == 0);
368 assert(value_cmp_si(faulhaber
->poly
[3]->p
[2], 1) == 0);
369 assert(value_cmp_si(faulhaber
->poly
[3]->p
[3], -2) == 0);
370 assert(value_cmp_si(faulhaber
->poly
[3]->p
[4], 1) == 0);
371 assert(value_cmp_si(faulhaber
->poly
[3]->p
[5], 4) == 0);
373 bernoulli
= bernoulli_compute(6);
374 assert(value_cmp_si(bernoulli
->poly
[6]->p
[0], 1) == 0);
375 assert(value_cmp_si(bernoulli
->poly
[6]->p
[1], 0) == 0);
376 assert(value_cmp_si(bernoulli
->poly
[6]->p
[2], -21) == 0);
377 assert(value_cmp_si(bernoulli
->poly
[6]->p
[3], 0) == 0);
378 assert(value_cmp_si(bernoulli
->poly
[6]->p
[4], 105) == 0);
379 assert(value_cmp_si(bernoulli
->poly
[6]->p
[5], -126) == 0);
380 assert(value_cmp_si(bernoulli
->poly
[6]->p
[6], 42) == 0);
381 assert(value_cmp_si(bernoulli
->poly
[6]->p
[7], 42) == 0);
383 unsigned nvar
, nparam
;
385 evalue
*base
, *sum1
, *sum2
;
386 base
= evalue_read_from_str("(1 * n + 1)", NULL
, &all_vars
, &nvar
, &nparam
,
389 sum1
= evalue_polynomial(faulhaber
->poly
[3], base
);
390 Free_ParamNames(all_vars
, nvar
+nparam
);
392 sum2
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
393 NULL
, &all_vars
, &nvar
, &nparam
,
395 Free_ParamNames(all_vars
, nvar
+nparam
);
396 assert(eequal(sum1
, sum2
));
402 int test_bernoulli_sum(struct barvinok_options
*options
)
404 unsigned nvar
, nparam
;
406 evalue
*e
, *sum1
, *sum2
;
407 e
= evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
408 "i", &all_vars
, &nvar
, &nparam
,
410 Free_ParamNames(all_vars
, nvar
+nparam
);
412 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
413 sum2
= evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
414 NULL
, &all_vars
, &nvar
, &nparam
,
416 Free_ParamNames(all_vars
, nvar
+nparam
);
420 assert(EVALUE_IS_ZERO(*sum1
));
424 e
= evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
425 "i", &all_vars
, &nvar
, &nparam
,
427 Free_ParamNames(all_vars
, nvar
+nparam
);
428 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
432 assert(EVALUE_IS_ZERO(*sum1
));
438 e
= evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
439 "i", &all_vars
, &nvar
, &nparam
,
441 Free_ParamNames(all_vars
, nvar
+nparam
);
442 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
443 sum2
= evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
444 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
445 NULL
, &all_vars
, &nvar
, &nparam
,
447 Free_ParamNames(all_vars
, nvar
+nparam
);
451 assert(EVALUE_IS_ZERO(*sum1
));
456 e
= evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
457 "k -1 >= 0\n j -k >= 0\n\n1",
458 "i,j,k", &all_vars
, &nvar
, &nparam
,
460 Free_ParamNames(all_vars
, nvar
+nparam
);
461 sum1
= Bernoulli_sum_evalue(e
, 3, options
);
462 sum2
= evalue_read_from_str("n -5 >= 0\n\n"
463 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
464 NULL
, &all_vars
, &nvar
, &nparam
,
466 Free_ParamNames(all_vars
, nvar
+nparam
);
470 assert(EVALUE_IS_ZERO(*sum1
));
476 int test_hilbert(struct barvinok_options
*options
)
479 Matrix
*M
= matrix_read_from_str(
483 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
486 M
= Cone_Hilbert_Basis(P
, options
->MaxRays
);
487 assert(M
->NbRows
= 5);
488 assert(M
->NbColumns
= 3);
491 M
= Cone_Integer_Hull(P
, NULL
, 0, options
);
492 assert(M
->NbRows
= 4);
493 assert(M
->NbColumns
= 3);
500 int test_ilp(struct barvinok_options
*options
)
502 Matrix
*M
= matrix_read_from_str(
506 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
508 Vector
*obj
= Vector_Alloc(2);
509 value_set_si(obj
->p
[0], 7);
510 value_set_si(obj
->p
[1], -1);
515 value_set_si(min
, 1);
516 value_set_si(max
, 17);
517 Vector
*opt
= Polyhedron_Integer_Minimum(P
, obj
->p
, min
, max
,
520 assert(value_cmp_si(opt
->p
[0], 1) == 0);
521 assert(value_cmp_si(opt
->p
[1], 1) == 0);
522 assert(value_cmp_si(opt
->p
[2], 1) == 0);
531 int test_hull(struct barvinok_options
*options
)
533 Matrix
*M
= matrix_read_from_str(
539 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
542 Matrix
*hull
= Polyhedron_Integer_Hull(P
, options
);
544 assert(hull
->NbRows
== 4);
545 M
= Matrix_Alloc(hull
->NbRows
, 1+hull
->NbColumns
);
546 for (int i
= 0; i
< hull
->NbRows
; ++i
) {
547 value_set_si(M
->p
[i
][0], 1);
548 Vector_Copy(hull
->p
[i
], M
->p
[i
]+1, hull
->NbColumns
);
551 Polyhedron
*H
= Constraints2Polyhedron(M
, options
->MaxRays
);
554 M
= matrix_read_from_str(
560 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
562 assert(PolyhedronIncludes(P
, H
) && PolyhedronIncludes(H
, P
));
566 M
= matrix_read_from_str(
571 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
574 hull
= Polyhedron_Integer_Hull(P
, options
);
576 assert(hull
->NbRows
== 0);
580 int main(int argc
, char **argv
)
582 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
583 test_evalue(options
);
584 test_split_periods(options
);
585 test_specialization(options
);
586 test_lattice_points(options
);
587 test_icounter(options
);
588 test_infinite_counter(options
);
589 test_series(options
);
591 test_bernoulli(options
);
592 test_bernoulli_sum(options
);
593 test_hilbert(options
);
596 barvinok_options_free(options
);