2 #include <barvinok/set.h>
3 #include <barvinok/options.h>
4 #include <barvinok/evalue.h>
5 #include <barvinok/util.h>
6 #include "conversion.h"
7 #include "evalue_read.h"
9 #include "lattice_point.h"
11 #include "bernoulli.h"
18 void set_from_string(T
& v
, char *s
)
20 std::istringstream
str(s
);
24 int test_evalue(struct barvinok_options
*options
)
26 unsigned nvar
, nparam
;
30 poly1
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
31 NULL
, &all_vars
, &nvar
, &nparam
,
33 Free_ParamNames(all_vars
, nvar
+nparam
);
36 evalue_copy(&poly2
, poly1
);
40 assert(EVALUE_IS_ZERO(*poly1
));
41 free_evalue_refs(poly1
);
42 free_evalue_refs(&poly2
);
46 int test_split_periods(struct barvinok_options
*options
)
48 unsigned nvar
, nparam
;
52 e
= evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
53 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
54 NULL
, &all_vars
, &nvar
, &nparam
,
56 Free_ParamNames(all_vars
, nvar
+nparam
);
58 evalue_split_periods(e
, 2, options
->MaxRays
);
59 assert(value_zero_p(e
->d
));
60 assert(e
->x
.p
->type
== partition
);
61 assert(e
->x
.p
->size
== 4);
62 assert(value_zero_p(e
->x
.p
->arr
[1].d
));
63 assert(e
->x
.p
->arr
[1].x
.p
->type
== polynomial
);
64 assert(value_zero_p(e
->x
.p
->arr
[3].d
));
65 assert(e
->x
.p
->arr
[3].x
.p
->type
== polynomial
);
70 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 n
.div(d
, count
, sign
);
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);
105 n2
.div(d
, count
, sign
);
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, NULL
, options
);
147 Matrix_Free(V
.Vertex
);
149 for (int i
= 0; i
< 4; ++i
) {
150 assert(eequal(num
.E
[i
], point
[i
]));
151 free_evalue_refs(point
[i
]);
153 free_evalue_refs(num
.E
[i
]);
159 int test_todd(struct barvinok_options
*options
)
161 tcounter
t(2, options
->max_index
);
162 assert(value_cmp_si(t
.todd
.coeff
->p
[0], 1) == 0);
163 assert(value_cmp_si(t
.todd
.coeff
->p
[1], -3) == 0);
164 assert(value_cmp_si(t
.todd
.coeff
->p
[2], 3) == 0);
165 assert(value_cmp_si(t
.todd_denom
->p
[0], 1) == 0);
166 assert(value_cmp_si(t
.todd_denom
->p
[1], 6) == 0);
167 assert(value_cmp_si(t
.todd_denom
->p
[2], 36) == 0);
170 set_from_string(lambda
, "[1 -1]");
171 zz2values(lambda
, t
.lambda
->p
);
174 set_from_string(rays
, "[[-1 0][-1 1]]");
179 set_from_string(v
, "[2 0 1]");
180 Vector
*vertex
= Vector_Alloc(3);
181 zz2values(v
, vertex
->p
);
183 t
.handle(rays
, vertex
->p
, one
, 1, NULL
, options
);
184 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
185 assert(value_cmp_si(mpq_denref(t
.count
), 24) == 0);
187 set_from_string(rays
, "[[0 -1][1 -1]]");
188 set_from_string(v
, "[0 2 1]");
189 zz2values(v
, vertex
->p
);
191 t
.handle(rays
, vertex
->p
, one
, 1, NULL
, options
);
192 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
193 assert(value_cmp_si(mpq_denref(t
.count
), 12) == 0);
195 set_from_string(rays
, "[[1 0][0 1]]");
196 set_from_string(v
, "[0 0 1]");
197 zz2values(v
, vertex
->p
);
199 t
.handle(rays
, vertex
->p
, one
, 1, NULL
, options
);
200 assert(value_cmp_si(mpq_numref(t
.count
), 6) == 0);
201 assert(value_cmp_si(mpq_denref(t
.count
), 1) == 0);
206 int test_bernoulli(struct barvinok_options
*options
)
208 struct bernoulli
*bernoulli
;
209 bernoulli
= bernoulli_compute(2);
210 bernoulli
= bernoulli_compute(4);
211 bernoulli
= bernoulli_compute(8);
212 assert(value_cmp_si(bernoulli
->b_num
->p
[6], 1) == 0);
213 assert(value_cmp_si(bernoulli
->b_den
->p
[6], 42) == 0);
214 assert(value_cmp_si(bernoulli
->sum
[3]->p
[0], 0) == 0);
215 assert(value_cmp_si(bernoulli
->sum
[3]->p
[1], 0) == 0);
216 assert(value_cmp_si(bernoulli
->sum
[3]->p
[2], 1) == 0);
217 assert(value_cmp_si(bernoulli
->sum
[3]->p
[3], -2) == 0);
218 assert(value_cmp_si(bernoulli
->sum
[3]->p
[4], 1) == 0);
219 assert(value_cmp_si(bernoulli
->sum
[3]->p
[5], 4) == 0);
221 unsigned nvar
, nparam
;
223 evalue
*base
, *sum1
, *sum2
;
224 base
= evalue_read_from_str("(1 * n + 1)", NULL
, &all_vars
, &nvar
, &nparam
,
227 sum1
= evalue_polynomial(bernoulli
->sum
[3], base
);
228 Free_ParamNames(all_vars
, nvar
+nparam
);
230 sum2
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
231 NULL
, &all_vars
, &nvar
, &nparam
,
233 Free_ParamNames(all_vars
, nvar
+nparam
);
234 assert(eequal(sum1
, sum2
));
235 free_evalue_refs(base
);
236 free_evalue_refs(sum1
);
237 free_evalue_refs(sum2
);
243 int test_bernoulli_sum(struct barvinok_options
*options
)
245 unsigned nvar
, nparam
;
247 evalue
*e
, *sum1
, *sum2
;
248 e
= evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
249 "i", &all_vars
, &nvar
, &nparam
,
251 Free_ParamNames(all_vars
, nvar
+nparam
);
253 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
254 sum2
= evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
255 NULL
, &all_vars
, &nvar
, &nparam
,
257 Free_ParamNames(all_vars
, nvar
+nparam
);
261 assert(EVALUE_IS_ZERO(*sum1
));
263 free_evalue_refs(sum1
);
267 e
= evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
268 "i", &all_vars
, &nvar
, &nparam
,
270 Free_ParamNames(all_vars
, nvar
+nparam
);
271 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
275 assert(EVALUE_IS_ZERO(*sum1
));
277 free_evalue_refs(sum1
);
281 free_evalue_refs(sum2
);
284 e
= evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
285 "i", &all_vars
, &nvar
, &nparam
,
287 Free_ParamNames(all_vars
, nvar
+nparam
);
288 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
289 sum2
= evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
290 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
291 NULL
, &all_vars
, &nvar
, &nparam
,
293 Free_ParamNames(all_vars
, nvar
+nparam
);
297 assert(EVALUE_IS_ZERO(*sum1
));
299 free_evalue_refs(sum1
);
302 free_evalue_refs(sum2
);
305 e
= evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
306 "k -1 >= 0\n j -k >= 0\n\n1",
307 "i,j,k", &all_vars
, &nvar
, &nparam
,
309 Free_ParamNames(all_vars
, nvar
+nparam
);
310 sum1
= Bernoulli_sum_evalue(e
, 3, options
);
311 sum2
= evalue_read_from_str("n -5 >= 0\n\n"
312 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
313 NULL
, &all_vars
, &nvar
, &nparam
,
315 Free_ParamNames(all_vars
, nvar
+nparam
);
319 assert(EVALUE_IS_ZERO(*sum1
));
321 free_evalue_refs(sum1
);
324 free_evalue_refs(sum2
);
328 int main(int argc
, char **argv
)
330 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
331 test_evalue(options
);
332 test_split_periods(options
);
333 test_specialization(options
);
334 test_lattice_points(options
);
336 test_bernoulli(options
);
337 test_bernoulli_sum(options
);
338 barvinok_options_free(options
);