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
, const 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
)
79 assert(value_cmp_si(n
.coeff
->p
[0], 1) == 0);
80 assert(value_cmp_si(n
.coeff
->p
[1], 5) == 0);
81 assert(value_cmp_si(n
.coeff
->p
[2], 10) == 0);
88 assert(value_cmp_si(d
.coeff
->p
[0], 2) == 0);
89 assert(value_cmp_si(d
.coeff
->p
[1], 1) == 0);
90 assert(value_cmp_si(d
.coeff
->p
[2], 0) == 0);
93 mpq_canonicalize(count
);
94 assert(value_cmp_si(mpq_numref(count
), 31) == 0);
95 assert(value_cmp_si(mpq_denref(count
), 8) == 0);
99 assert(value_cmp_si(n2
.coeff
->p
[0], 1) == 0);
100 assert(value_cmp_si(n2
.coeff
->p
[1], -2) == 0);
101 assert(value_cmp_si(n2
.coeff
->p
[2], 3) == 0);
104 mpq_canonicalize(count
);
105 assert(value_cmp_si(mpq_numref(count
), 6) == 0);
106 assert(value_cmp_si(mpq_denref(count
), 1) == 0);
112 int test_lattice_points(struct barvinok_options
*options
)
116 set_from_string(tmp
, "[[0 0 0 0 4][0 0 0 0 4][-1 0 1 0 4]]");
117 V
.Vertex
= zz2matrix(tmp
);
119 set_from_string(lambda
, "[3 5 7]");
121 set_from_string(rays
, "[[0 1 0][4 0 1][0 0 -1]]");
125 unsigned nvar
, nparam
;
127 point
[0] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
128 "( 7 * {( 1/4 * a + ( 3/4 * c + 3/4 ) ) } + -21/4 ) ) )",
129 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
130 Free_ParamNames(all_vars
, nvar
+nparam
);
131 point
[1] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
132 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/2 ) ) } + -1/2 ) ) )",
133 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
134 Free_ParamNames(all_vars
, nvar
+nparam
);
135 point
[2] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
136 "( 7 * {( 1/4 * a + ( 3/4 * c + 1/4 ) ) } + 17/4 ) ) )",
137 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
138 Free_ParamNames(all_vars
, nvar
+nparam
);
139 point
[3] = evalue_read_from_str("( -7/4 * a + ( 7/4 * c + "
140 "( 7 * {( 1/4 * a + ( 3/4 * c + 0 ) ) } + 9 ) ) )",
141 "a,b,c", &all_vars
, &nvar
, &nparam
, options
->MaxRays
);
142 Free_ParamNames(all_vars
, nvar
+nparam
);
144 lattice_point(&V
, rays
, lambda
, &num
, 4, NULL
, options
);
145 Matrix_Free(V
.Vertex
);
147 for (int i
= 0; i
< 4; ++i
) {
148 assert(eequal(num
.E
[i
], point
[i
]));
149 free_evalue_refs(point
[i
]);
151 free_evalue_refs(num
.E
[i
]);
157 int test_todd(struct barvinok_options
*options
)
159 tcounter
t(2, options
->max_index
);
160 assert(value_cmp_si(t
.todd
.coeff
->p
[0], 1) == 0);
161 assert(value_cmp_si(t
.todd
.coeff
->p
[1], -3) == 0);
162 assert(value_cmp_si(t
.todd
.coeff
->p
[2], 3) == 0);
163 assert(value_cmp_si(t
.todd_denom
->p
[0], 1) == 0);
164 assert(value_cmp_si(t
.todd_denom
->p
[1], 6) == 0);
165 assert(value_cmp_si(t
.todd_denom
->p
[2], 36) == 0);
168 set_from_string(lambda
, "[1 -1]");
169 zz2values(lambda
, t
.lambda
->p
);
172 set_from_string(rays
, "[[-1 0][-1 1]]");
177 set_from_string(v
, "[2 0 1]");
178 Vector
*vertex
= Vector_Alloc(3);
179 zz2values(v
, vertex
->p
);
181 t
.handle(rays
, vertex
->p
, one
, 1, NULL
, options
);
182 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
183 assert(value_cmp_si(mpq_denref(t
.count
), 24) == 0);
185 set_from_string(rays
, "[[0 -1][1 -1]]");
186 set_from_string(v
, "[0 2 1]");
187 zz2values(v
, vertex
->p
);
189 t
.handle(rays
, vertex
->p
, one
, 1, NULL
, options
);
190 assert(value_cmp_si(mpq_numref(t
.count
), 71) == 0);
191 assert(value_cmp_si(mpq_denref(t
.count
), 12) == 0);
193 set_from_string(rays
, "[[1 0][0 1]]");
194 set_from_string(v
, "[0 0 1]");
195 zz2values(v
, vertex
->p
);
197 t
.handle(rays
, vertex
->p
, one
, 1, NULL
, options
);
198 assert(value_cmp_si(mpq_numref(t
.count
), 6) == 0);
199 assert(value_cmp_si(mpq_denref(t
.count
), 1) == 0);
204 int test_bernoulli(struct barvinok_options
*options
)
206 struct bernoulli
*bernoulli
;
207 bernoulli
= bernoulli_compute(2);
208 bernoulli
= bernoulli_compute(4);
209 bernoulli
= bernoulli_compute(8);
210 assert(value_cmp_si(bernoulli
->b_num
->p
[6], 1) == 0);
211 assert(value_cmp_si(bernoulli
->b_den
->p
[6], 42) == 0);
212 assert(value_cmp_si(bernoulli
->sum
[3]->p
[0], 0) == 0);
213 assert(value_cmp_si(bernoulli
->sum
[3]->p
[1], 0) == 0);
214 assert(value_cmp_si(bernoulli
->sum
[3]->p
[2], 1) == 0);
215 assert(value_cmp_si(bernoulli
->sum
[3]->p
[3], -2) == 0);
216 assert(value_cmp_si(bernoulli
->sum
[3]->p
[4], 1) == 0);
217 assert(value_cmp_si(bernoulli
->sum
[3]->p
[5], 4) == 0);
219 unsigned nvar
, nparam
;
221 evalue
*base
, *sum1
, *sum2
;
222 base
= evalue_read_from_str("(1 * n + 1)", NULL
, &all_vars
, &nvar
, &nparam
,
225 sum1
= evalue_polynomial(bernoulli
->sum
[3], base
);
226 Free_ParamNames(all_vars
, nvar
+nparam
);
228 sum2
= evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
229 NULL
, &all_vars
, &nvar
, &nparam
,
231 Free_ParamNames(all_vars
, nvar
+nparam
);
232 assert(eequal(sum1
, sum2
));
233 free_evalue_refs(base
);
234 free_evalue_refs(sum1
);
235 free_evalue_refs(sum2
);
241 int test_bernoulli_sum(struct barvinok_options
*options
)
243 unsigned nvar
, nparam
;
245 evalue
*e
, *sum1
, *sum2
;
246 e
= evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
247 "i", &all_vars
, &nvar
, &nparam
,
249 Free_ParamNames(all_vars
, nvar
+nparam
);
251 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
252 sum2
= evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
253 NULL
, &all_vars
, &nvar
, &nparam
,
255 Free_ParamNames(all_vars
, nvar
+nparam
);
259 assert(EVALUE_IS_ZERO(*sum1
));
261 free_evalue_refs(sum1
);
265 e
= evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
266 "i", &all_vars
, &nvar
, &nparam
,
268 Free_ParamNames(all_vars
, nvar
+nparam
);
269 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
273 assert(EVALUE_IS_ZERO(*sum1
));
275 free_evalue_refs(sum1
);
279 free_evalue_refs(sum2
);
282 e
= evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
283 "i", &all_vars
, &nvar
, &nparam
,
285 Free_ParamNames(all_vars
, nvar
+nparam
);
286 sum1
= Bernoulli_sum_evalue(e
, 1, options
);
287 sum2
= evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
288 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
289 NULL
, &all_vars
, &nvar
, &nparam
,
291 Free_ParamNames(all_vars
, nvar
+nparam
);
295 assert(EVALUE_IS_ZERO(*sum1
));
297 free_evalue_refs(sum1
);
300 free_evalue_refs(sum2
);
303 e
= evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
304 "k -1 >= 0\n j -k >= 0\n\n1",
305 "i,j,k", &all_vars
, &nvar
, &nparam
,
307 Free_ParamNames(all_vars
, nvar
+nparam
);
308 sum1
= Bernoulli_sum_evalue(e
, 3, options
);
309 sum2
= evalue_read_from_str("n -5 >= 0\n\n"
310 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
311 NULL
, &all_vars
, &nvar
, &nparam
,
313 Free_ParamNames(all_vars
, nvar
+nparam
);
317 assert(EVALUE_IS_ZERO(*sum1
));
319 free_evalue_refs(sum1
);
322 free_evalue_refs(sum2
);
326 int main(int argc
, char **argv
)
328 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
329 test_evalue(options
);
330 test_split_periods(options
);
331 test_specialization(options
);
332 test_lattice_points(options
);
334 test_bernoulli(options
);
335 test_bernoulli_sum(options
);
336 barvinok_options_free(options
);