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"
14 #include "matrix_read.h"
21 void set_from_string(T
& v
, const char *s
)
23 std::istringstream
str(s
);
27 int test_evalue(struct barvinok_options
*options
)
29 unsigned nvar
, nparam
;
33 poly1
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
34 NULL
, &all_vars
, &nvar
, &nparam
,
36 Free_ParamNames(all_vars
, nvar
+nparam
);
39 evalue_copy(&poly2
, poly1
);
43 assert(EVALUE_IS_ZERO(*poly1
));
45 free_evalue_refs(&poly2
);
48 int test_split_periods(struct barvinok_options
*options
)
50 unsigned nvar
, nparam
;
54 e
= evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
55 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
56 NULL
, &all_vars
, &nvar
, &nparam
,
58 Free_ParamNames(all_vars
, nvar
+nparam
);
60 evalue_split_periods(e
, 2, options
->MaxRays
);
61 assert(value_zero_p(e
->d
));
62 assert(e
->x
.p
->type
== partition
);
63 assert(e
->x
.p
->size
== 4);
64 assert(value_zero_p(e
->x
.p
->arr
[1].d
));
65 assert(e
->x
.p
->arr
[1].x
.p
->type
== polynomial
);
66 assert(value_zero_p(e
->x
.p
->arr
[3].d
));
67 assert(e
->x
.p
->arr
[3].x
.p
->type
== polynomial
);
71 int test_specialization(struct barvinok_options
*options
)
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);
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);
94 mpq_canonicalize(count
);
95 assert(value_cmp_si(mpq_numref(count
), 31) == 0);
96 assert(value_cmp_si(mpq_denref(count
), 8) == 0);
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);
105 mpq_canonicalize(count
);
106 assert(value_cmp_si(mpq_numref(count
), 6) == 0);
107 assert(value_cmp_si(mpq_denref(count
), 1) == 0);
113 int test_lattice_points(struct barvinok_options
*options
)
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
);
120 set_from_string(lambda
, "[3 5 7]");
122 set_from_string(rays
, "[[0 1 0][4 0 1][0 0 -1]]");
126 unsigned nvar
, nparam
;
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 evalue_free(point
[i
]);
151 free_evalue_refs(num
.E
[i
]);
157 static Matrix
*matrix_read_from_str(const char *s
)
159 std::istringstream
str(s
);
160 return Matrix_Read(str
);
163 static int test_series(struct barvinok_options
*options
)
165 Matrix
*M
= matrix_read_from_str(
167 " 0 1 0 0 0 0 0 1 0 0 3 \n"
168 " 0 0 1 0 0 0 0 -1 1 0 -5 \n"
169 " 0 0 0 1 0 0 0 0 -2 -1 6 \n"
170 " 0 0 0 0 1 0 0 1 1 0 5 \n"
171 " 0 0 0 0 0 1 0 0 -1 0 0 \n"
172 " 0 0 0 0 0 0 1 -2 0 -1 -3 \n"
173 " 1 0 0 0 0 0 0 2 0 1 3 \n"
174 " 1 0 0 0 0 0 0 1 -1 0 5 \n"
175 " 1 0 0 0 0 0 0 -1 -1 0 -5 \n"
176 " 1 0 0 0 0 0 0 -1 0 0 -3 \n"
177 " 1 0 0 0 0 0 0 0 2 1 -6 \n"
178 " 1 0 0 0 0 0 0 0 1 0 0 \n");
179 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
181 Polyhedron
*C
= Universe_Polyhedron(3);
182 gen_fun
*gf
= barvinok_series_with_options(P
, C
, options
);
187 M
= matrix_read_from_str(
189 " 0 1 1 0 0 1 0 2 \n"
190 " 0 0 0 1 0 -2 0 6 \n"
191 " 0 0 0 0 1 -1 0 -1 \n"
192 " 0 0 0 0 0 0 1 0 \n"
193 " 1 0 1 0 0 0 0 0 \n"
194 " 1 0 -1 0 0 -1 0 -2 \n"
195 " 1 0 0 0 0 1 0 -3 \n");
196 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
198 C
= Universe_Polyhedron(2);
199 gf
= barvinok_series_with_options(P
, C
, options
);
207 int test_todd(struct barvinok_options
*options
)
209 tcounter
t(2, options
->max_index
);
210 assert(value_cmp_si(t
.todd
.coeff
->p
[0], 1) == 0);
211 assert(value_cmp_si(t
.todd
.coeff
->p
[1], -3) == 0);
212 assert(value_cmp_si(t
.todd
.coeff
->p
[2], 3) == 0);
213 assert(value_cmp_si(t
.todd_denom
->p
[0], 1) == 0);
214 assert(value_cmp_si(t
.todd_denom
->p
[1], 6) == 0);
215 assert(value_cmp_si(t
.todd_denom
->p
[2], 36) == 0);
218 set_from_string(lambda
, "[1 -1]");
219 zz2values(lambda
, t
.lambda
->p
);
222 set_from_string(rays
, "[[-1 0][-1 1]]");
227 set_from_string(v
, "[2 0 1]");
228 Vector
*vertex
= Vector_Alloc(3);
229 zz2values(v
, vertex
->p
);
231 t
.handle(rays
, vertex
->p
, one
, 1, options
);
232 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
233 assert(value_cmp_si(mpq_denref(t
.count
), 24) == 0);
235 set_from_string(rays
, "[[0 -1][1 -1]]");
236 set_from_string(v
, "[0 2 1]");
237 zz2values(v
, vertex
->p
);
239 t
.handle(rays
, vertex
->p
, one
, 1, options
);
240 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
241 assert(value_cmp_si(mpq_denref(t
.count
), 12) == 0);
243 set_from_string(rays
, "[[1 0][0 1]]");
244 set_from_string(v
, "[0 0 1]");
245 zz2values(v
, vertex
->p
);
247 t
.handle(rays
, vertex
->p
, one
, 1, options
);
248 assert(value_cmp_si(mpq_numref(t
.count
), 6) == 0);
249 assert(value_cmp_si(mpq_denref(t
.count
), 1) == 0);
254 int test_bernoulli(struct barvinok_options
*options
)
256 struct bernoulli_coef
*bernoulli_coef
;
257 struct poly_list
*faulhaber
, *bernoulli
;
258 bernoulli_coef
= bernoulli_coef_compute(2);
259 faulhaber
= faulhaber_compute(4);
260 bernoulli_coef
= bernoulli_coef_compute(8);
261 assert(value_cmp_si(bernoulli_coef
->num
->p
[6], 1) == 0);
262 assert(value_cmp_si(bernoulli_coef
->den
->p
[6], 42) == 0);
263 assert(value_cmp_si(faulhaber
->poly
[3]->p
[0], 0) == 0);
264 assert(value_cmp_si(faulhaber
->poly
[3]->p
[1], 0) == 0);
265 assert(value_cmp_si(faulhaber
->poly
[3]->p
[2], 1) == 0);
266 assert(value_cmp_si(faulhaber
->poly
[3]->p
[3], -2) == 0);
267 assert(value_cmp_si(faulhaber
->poly
[3]->p
[4], 1) == 0);
268 assert(value_cmp_si(faulhaber
->poly
[3]->p
[5], 4) == 0);
270 bernoulli
= bernoulli_compute(6);
271 assert(value_cmp_si(bernoulli
->poly
[6]->p
[0], 1) == 0);
272 assert(value_cmp_si(bernoulli
->poly
[6]->p
[1], 0) == 0);
273 assert(value_cmp_si(bernoulli
->poly
[6]->p
[2], -21) == 0);
274 assert(value_cmp_si(bernoulli
->poly
[6]->p
[3], 0) == 0);
275 assert(value_cmp_si(bernoulli
->poly
[6]->p
[4], 105) == 0);
276 assert(value_cmp_si(bernoulli
->poly
[6]->p
[5], -126) == 0);
277 assert(value_cmp_si(bernoulli
->poly
[6]->p
[6], 42) == 0);
278 assert(value_cmp_si(bernoulli
->poly
[6]->p
[7], 42) == 0);
280 unsigned nvar
, nparam
;
282 evalue
*base
, *sum1
, *sum2
;
283 base
= evalue_read_from_str("(1 * n + 1)", NULL
, &all_vars
, &nvar
, &nparam
,
286 sum1
= evalue_polynomial(faulhaber
->poly
[3], base
);
287 Free_ParamNames(all_vars
, nvar
+nparam
);
289 sum2
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
290 NULL
, &all_vars
, &nvar
, &nparam
,
292 Free_ParamNames(all_vars
, nvar
+nparam
);
293 assert(eequal(sum1
, sum2
));
299 int test_bernoulli_sum(struct barvinok_options
*options
)
301 unsigned nvar
, nparam
;
303 evalue
*e
, *sum1
, *sum2
;
304 e
= evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
305 "i", &all_vars
, &nvar
, &nparam
,
307 Free_ParamNames(all_vars
, nvar
+nparam
);
309 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
310 sum2
= evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
311 NULL
, &all_vars
, &nvar
, &nparam
,
313 Free_ParamNames(all_vars
, nvar
+nparam
);
317 assert(EVALUE_IS_ZERO(*sum1
));
321 e
= evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
322 "i", &all_vars
, &nvar
, &nparam
,
324 Free_ParamNames(all_vars
, nvar
+nparam
);
325 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
329 assert(EVALUE_IS_ZERO(*sum1
));
335 e
= evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
336 "i", &all_vars
, &nvar
, &nparam
,
338 Free_ParamNames(all_vars
, nvar
+nparam
);
339 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
340 sum2
= evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
341 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
342 NULL
, &all_vars
, &nvar
, &nparam
,
344 Free_ParamNames(all_vars
, nvar
+nparam
);
348 assert(EVALUE_IS_ZERO(*sum1
));
353 e
= evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
354 "k -1 >= 0\n j -k >= 0\n\n1",
355 "i,j,k", &all_vars
, &nvar
, &nparam
,
357 Free_ParamNames(all_vars
, nvar
+nparam
);
358 sum1
= Bernoulli_sum_evalue(e
, 3, options
);
359 sum2
= evalue_read_from_str("n -5 >= 0\n\n"
360 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
361 NULL
, &all_vars
, &nvar
, &nparam
,
363 Free_ParamNames(all_vars
, nvar
+nparam
);
367 assert(EVALUE_IS_ZERO(*sum1
));
373 int main(int argc
, char **argv
)
375 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
376 test_evalue(options
);
377 test_split_periods(options
);
378 test_specialization(options
);
379 test_lattice_points(options
);
380 test_series(options
);
382 test_bernoulli(options
);
383 test_bernoulli_sum(options
);
384 barvinok_options_free(options
);