Polyhedron_Sample: remove all equalities from polyhedron before scanning
[barvinok.git] / testlib.cc
blob90a45923b96bfe12360d16ac21cf306c92ec9f32
1 #include <sstream>
2 #include <barvinok/options.h>
3 #include <barvinok/evalue.h>
4 #include <barvinok/util.h>
5 #include "conversion.h"
6 #include "evalue_read.h"
7 #include "dpoly.h"
8 #include "lattice_point.h"
9 #include "tcounter.h"
10 #include "bernoulli.h"
12 using std::cout;
13 using std::cerr;
14 using std::endl;
16 template <typename T>
17 void set_from_string(T& v, char *s)
19 std::istringstream str(s);
20 str >> v;
23 int test_evalue(struct barvinok_options *options)
25 unsigned nvar, nparam;
26 char **all_vars;
27 evalue *poly1, poly2;
29 poly1 = evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
30 NULL, &all_vars, &nvar, &nparam,
31 options->MaxRays);
32 Free_ParamNames(all_vars, nvar+nparam);
34 value_init(poly2.d);
35 evalue_copy(&poly2, poly1);
36 evalue_negate(poly1);
37 eadd(&poly2, poly1);
38 reduce_evalue(poly1);
39 assert(EVALUE_IS_ZERO(*poly1));
40 free_evalue_refs(poly1);
41 free_evalue_refs(&poly2);
42 free(poly1);
45 int test_split_periods(struct barvinok_options *options)
47 unsigned nvar, nparam;
48 char **all_vars;
49 evalue *e;
51 e = evalue_read_from_str("U + 2V + 3 >= 0\n- U -2V >= 0\n- U 10 >= 0\n"
52 "U >= 0\n\n({( 1/3 * U + ( 2/3 * V + 0 ))})",
53 NULL, &all_vars, &nvar, &nparam,
54 options->MaxRays);
55 Free_ParamNames(all_vars, nvar+nparam);
57 evalue_split_periods(e, 2, options->MaxRays);
58 assert(value_zero_p(e->d));
59 assert(e->x.p->type == partition);
60 assert(e->x.p->size == 4);
61 assert(value_zero_p(e->x.p->arr[1].d));
62 assert(e->x.p->arr[1].x.p->type == polynomial);
63 assert(value_zero_p(e->x.p->arr[3].d));
64 assert(e->x.p->arr[3].x.p->type == polynomial);
65 free_evalue_refs(e);
66 free(e);
69 int test_specialization(struct barvinok_options *options)
71 Value v;
72 value_init(v);
73 mpq_t count;
74 mpq_init(count);
75 ZZ sign;
77 value_set_si(v, 5);
78 dpoly n(2, v);
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);
83 value_set_si(v, 1);
84 dpoly d(2, v, 1);
85 value_set_si(v, 2);
86 dpoly d2(2, v, 1);
87 d *= d2;
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);
92 sign = 1;
93 n.div(d, count, sign);
94 mpq_canonicalize(count);
95 assert(value_cmp_si(mpq_numref(count), 31) == 0);
96 assert(value_cmp_si(mpq_denref(count), 8) == 0);
98 value_set_si(v, -2);
99 dpoly n2(2, v);
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);
104 n2.div(d, count, sign);
105 mpq_canonicalize(count);
106 assert(value_cmp_si(mpq_numref(count), 6) == 0);
107 assert(value_cmp_si(mpq_denref(count), 1) == 0);
109 value_clear(v);
110 mpq_clear(count);
113 int test_lattice_points(struct barvinok_options *options)
115 Param_Vertices V;
116 mat_ZZ tmp;
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);
119 vec_ZZ lambda;
120 set_from_string(lambda, "[3 5 7]");
121 mat_ZZ rays;
122 set_from_string(rays, "[[0 1 0][4 0 1][0 0 -1]]");
123 term_info num;
124 evalue *point[4];
126 unsigned nvar, nparam;
127 char **all_vars;
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, NULL, options);
146 Matrix_Free(V.Vertex);
148 for (int i = 0; i < 4; ++i) {
149 assert(eequal(num.E[i], point[i]));
150 free_evalue_refs(point[i]);
151 free(point[i]);
152 free_evalue_refs(num.E[i]);
153 delete num.E[i];
155 delete [] num.E;
158 int test_todd(struct barvinok_options *options)
160 tcounter t(2, options->max_index);
161 assert(value_cmp_si(t.todd.coeff->p[0], 1) == 0);
162 assert(value_cmp_si(t.todd.coeff->p[1], -3) == 0);
163 assert(value_cmp_si(t.todd.coeff->p[2], 3) == 0);
164 assert(value_cmp_si(t.todd_denom->p[0], 1) == 0);
165 assert(value_cmp_si(t.todd_denom->p[1], 6) == 0);
166 assert(value_cmp_si(t.todd_denom->p[2], 36) == 0);
168 vec_ZZ lambda;
169 set_from_string(lambda, "[1 -1]");
170 zz2values(lambda, t.lambda->p);
172 mat_ZZ rays;
173 set_from_string(rays, "[[-1 0][-1 1]]");
175 QQ one(1, 1);
177 vec_ZZ v;
178 set_from_string(v, "[2 0 1]");
179 Vector *vertex = Vector_Alloc(3);
180 zz2values(v, vertex->p);
182 t.handle(rays, vertex->p, one, 1, NULL, options);
183 assert(value_cmp_si(mpq_numref(t.count), 71) == 0);
184 assert(value_cmp_si(mpq_denref(t.count), 24) == 0);
186 set_from_string(rays, "[[0 -1][1 -1]]");
187 set_from_string(v, "[0 2 1]");
188 zz2values(v, vertex->p);
190 t.handle(rays, vertex->p, one, 1, NULL, options);
191 assert(value_cmp_si(mpq_numref(t.count), 71) == 0);
192 assert(value_cmp_si(mpq_denref(t.count), 12) == 0);
194 set_from_string(rays, "[[1 0][0 1]]");
195 set_from_string(v, "[0 0 1]");
196 zz2values(v, vertex->p);
198 t.handle(rays, vertex->p, one, 1, NULL, options);
199 assert(value_cmp_si(mpq_numref(t.count), 6) == 0);
200 assert(value_cmp_si(mpq_denref(t.count), 1) == 0);
202 Vector_Free(vertex);
205 int test_bernoulli(struct barvinok_options *options)
207 struct bernoulli *bernoulli;
208 bernoulli = bernoulli_compute(2);
209 bernoulli = bernoulli_compute(4);
210 bernoulli = bernoulli_compute(8);
211 assert(value_cmp_si(bernoulli->b_num->p[6], 1) == 0);
212 assert(value_cmp_si(bernoulli->b_den->p[6], 42) == 0);
213 assert(value_cmp_si(bernoulli->sum[3]->p[0], 0) == 0);
214 assert(value_cmp_si(bernoulli->sum[3]->p[1], 0) == 0);
215 assert(value_cmp_si(bernoulli->sum[3]->p[2], 1) == 0);
216 assert(value_cmp_si(bernoulli->sum[3]->p[3], -2) == 0);
217 assert(value_cmp_si(bernoulli->sum[3]->p[4], 1) == 0);
218 assert(value_cmp_si(bernoulli->sum[3]->p[5], 4) == 0);
220 unsigned nvar, nparam;
221 char **all_vars;
222 evalue *base, *sum1, *sum2;
223 base = evalue_read_from_str("(1 * n + 1)", NULL, &all_vars, &nvar, &nparam,
224 options->MaxRays);
226 sum1 = evalue_polynomial(bernoulli->sum[3], base);
227 Free_ParamNames(all_vars, nvar+nparam);
229 sum2 = evalue_read_from_str("(1/4 * n^4 + 1/2 * n^3 + 1/4 * n^2)",
230 NULL, &all_vars, &nvar, &nparam,
231 options->MaxRays);
232 Free_ParamNames(all_vars, nvar+nparam);
233 assert(eequal(sum1, sum2));
234 free_evalue_refs(base);
235 free_evalue_refs(sum1);
236 free_evalue_refs(sum2);
237 free(base);
238 free(sum1);
239 free(sum2);
242 int test_bernoulli_sum(struct barvinok_options *options)
244 unsigned nvar, nparam;
245 char **all_vars;
246 evalue *e, *sum1, *sum2;
247 e = evalue_read_from_str("i + -1 >= 0\n -i + n >= 0\n\n 1 + (-1 *i) + i^2",
248 "i", &all_vars, &nvar, &nparam,
249 options->MaxRays);
250 Free_ParamNames(all_vars, nvar+nparam);
252 sum1 = Bernoulli_sum_evalue(e, 1, options);
253 sum2 = evalue_read_from_str("n -1 >= 0\n\n (1/3 * n^3 + 2/3 * n)",
254 NULL, &all_vars, &nvar, &nparam,
255 options->MaxRays);
256 Free_ParamNames(all_vars, nvar+nparam);
257 evalue_negate(sum1);
258 eadd(sum2, sum1);
259 reduce_evalue(sum1);
260 assert(EVALUE_IS_ZERO(*sum1));
261 free_evalue_refs(e);
262 free_evalue_refs(sum1);
263 free(e);
264 free(sum1);
266 e = evalue_read_from_str("-i + -1 >= 0\n i + n >= 0\n\n 1 + i + i^2",
267 "i", &all_vars, &nvar, &nparam,
268 options->MaxRays);
269 Free_ParamNames(all_vars, nvar+nparam);
270 sum1 = Bernoulli_sum_evalue(e, 1, options);
271 evalue_negate(sum1);
272 eadd(sum2, sum1);
273 reduce_evalue(sum1);
274 assert(EVALUE_IS_ZERO(*sum1));
275 free_evalue_refs(e);
276 free_evalue_refs(sum1);
277 free(e);
278 free(sum1);
280 free_evalue_refs(sum2);
281 free(sum2);
283 e = evalue_read_from_str("i + 4 >= 0\n -i + n >= 0\n\n i",
284 "i", &all_vars, &nvar, &nparam,
285 options->MaxRays);
286 Free_ParamNames(all_vars, nvar+nparam);
287 sum1 = Bernoulli_sum_evalue(e, 1, options);
288 sum2 = evalue_read_from_str("n + 0 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))\n"
289 "n + 4 >= 0\n -n -1 >= 0\n\n (1/2 * n^2 + 1/2 * n + (-10))",
290 NULL, &all_vars, &nvar, &nparam,
291 options->MaxRays);
292 Free_ParamNames(all_vars, nvar+nparam);
293 evalue_negate(sum1);
294 eadd(sum2, sum1);
295 reduce_evalue(sum1);
296 assert(EVALUE_IS_ZERO(*sum1));
297 free_evalue_refs(e);
298 free_evalue_refs(sum1);
299 free(e);
300 free(sum1);
301 free_evalue_refs(sum2);
302 free(sum2);
304 e = evalue_read_from_str("i -5 >= 0\n -i + n >= 0\n j -1 >= 0\n i -j >= 0\n"
305 "k -1 >= 0\n j -k >= 0\n\n1",
306 "i,j,k", &all_vars, &nvar, &nparam,
307 options->MaxRays);
308 Free_ParamNames(all_vars, nvar+nparam);
309 sum1 = Bernoulli_sum_evalue(e, 3, options);
310 sum2 = evalue_read_from_str("n -5 >= 0\n\n"
311 "1/6 * n^3 + 1/2 * n^2 + 1/3 * n + -20",
312 NULL, &all_vars, &nvar, &nparam,
313 options->MaxRays);
314 Free_ParamNames(all_vars, nvar+nparam);
315 evalue_negate(sum1);
316 eadd(sum2, sum1);
317 reduce_evalue(sum1);
318 assert(EVALUE_IS_ZERO(*sum1));
319 free_evalue_refs(e);
320 free_evalue_refs(sum1);
321 free(e);
322 free(sum1);
323 free_evalue_refs(sum2);
324 free(sum2);
327 int main(int argc, char **argv)
329 struct barvinok_options *options = barvinok_options_new_with_defaults();
330 test_evalue(options);
331 test_split_periods(options);
332 test_specialization(options);
333 test_lattice_points(options);
334 test_todd(options);
335 test_bernoulli(options);
336 test_bernoulli_sum(options);
337 barvinok_options_free(options);