2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
16 #include <isl/constraint.h>
17 #include <isl/polynomial.h>
18 #include <isl/union_map.h>
19 #include <isl_map_private.h>
23 void test_parse_map(isl_ctx *ctx, const char *str)
27 map = isl_map_read_from_str(ctx, str, -1);
32 void test_parse_pwqp(isl_ctx *ctx, const char *str)
34 isl_pw_qpolynomial *pwqp;
36 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
38 isl_pw_qpolynomial_free(pwqp);
41 void test_parse(struct isl_ctx *ctx)
46 str = "{ [i] -> [-i] }";
47 map = isl_map_read_from_str(ctx, str, -1);
51 str = "{ A[i] -> L[([i/3])] }";
52 map = isl_map_read_from_str(ctx, str, -1);
56 test_parse_map(ctx, "{[[s] -> A[i]] -> [[s+1] -> A[i]]}");
58 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
59 map = isl_map_read_from_str(ctx, str, -1);
60 str = "{ [new, old] -> [o0, o1] : "
61 "exists (e0 = [(-1 - new + o0)/2], e1 = [(-1 - old + o1)/2]: "
62 "2e0 = -1 - new + o0 and 2e1 = -1 - old + o1 and o0 >= 0 and "
63 "o0 <= 1 and o1 >= 0 and o1 <= 1) }";
64 map2 = isl_map_read_from_str(ctx, str, -1);
65 assert(isl_map_is_equal(map, map2));
69 str = "{[new,old] -> [new+1-2*[(new+1)/2],old+1-2*[(old+1)/2]]}";
70 map = isl_map_read_from_str(ctx, str, -1);
71 str = "{[new,old] -> [(new+1)%2,(old+1)%2]}";
72 map2 = isl_map_read_from_str(ctx, str, -1);
73 assert(isl_map_is_equal(map, map2));
77 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
80 void test_read(struct isl_ctx *ctx)
82 char filename[PATH_MAX];
85 struct isl_basic_set *bset1, *bset2;
86 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
88 n = snprintf(filename, sizeof(filename),
89 "%s/test_inputs/set.omega", srcdir);
90 assert(n < sizeof(filename));
91 input = fopen(filename, "r");
94 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
95 bset2 = isl_basic_set_read_from_str(ctx, str, 0);
97 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
99 isl_basic_set_free(bset1);
100 isl_basic_set_free(bset2);
105 void test_bounded(struct isl_ctx *ctx)
110 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }", -1);
111 bounded = isl_set_is_bounded(set);
115 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }", -1);
116 bounded = isl_set_is_bounded(set);
120 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }", -1);
121 bounded = isl_set_is_bounded(set);
126 /* Construct the basic set { [i] : 5 <= i <= N } */
127 void test_construction(struct isl_ctx *ctx)
131 struct isl_basic_set *bset;
132 struct isl_constraint *c;
136 dim = isl_dim_set_alloc(ctx, 1, 1);
137 bset = isl_basic_set_universe(dim);
139 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
140 isl_int_set_si(v, -1);
141 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
142 isl_int_set_si(v, 1);
143 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
144 bset = isl_basic_set_add_constraint(bset, c);
146 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
147 isl_int_set_si(v, 1);
148 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
149 isl_int_set_si(v, -5);
150 isl_constraint_set_constant(c, v);
151 bset = isl_basic_set_add_constraint(bset, c);
153 isl_basic_set_free(bset);
158 void test_dim(struct isl_ctx *ctx)
161 isl_map *map1, *map2;
163 map1 = isl_map_read_from_str(ctx,
164 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
165 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
166 map2 = isl_map_read_from_str(ctx,
167 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
168 assert(isl_map_is_equal(map1, map2));
171 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
172 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
173 assert(isl_map_is_equal(map1, map2));
178 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
179 map1 = isl_map_read_from_str(ctx, str, -1);
180 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
181 map2 = isl_map_read_from_str(ctx, str, -1);
182 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
183 assert(isl_map_is_equal(map1, map2));
189 void test_div(struct isl_ctx *ctx)
195 struct isl_basic_set *bset;
196 struct isl_constraint *c;
201 dim = isl_dim_set_alloc(ctx, 0, 1);
202 bset = isl_basic_set_universe(dim);
204 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
205 isl_int_set_si(v, -1);
206 isl_constraint_set_constant(c, v);
207 isl_int_set_si(v, 1);
208 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
209 div = isl_div_alloc(isl_basic_set_get_dim(bset));
210 c = isl_constraint_add_div(c, div, &pos);
211 isl_int_set_si(v, 3);
212 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
213 bset = isl_basic_set_add_constraint(bset, c);
215 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
216 isl_int_set_si(v, 1);
217 isl_constraint_set_constant(c, v);
218 isl_int_set_si(v, -1);
219 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
220 div = isl_div_alloc(isl_basic_set_get_dim(bset));
221 c = isl_constraint_add_div(c, div, &pos);
222 isl_int_set_si(v, 3);
223 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
224 bset = isl_basic_set_add_constraint(bset, c);
226 assert(bset && bset->n_div == 1);
227 isl_basic_set_free(bset);
230 dim = isl_dim_set_alloc(ctx, 0, 1);
231 bset = isl_basic_set_universe(dim);
233 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
234 isl_int_set_si(v, 1);
235 isl_constraint_set_constant(c, v);
236 isl_int_set_si(v, -1);
237 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
238 div = isl_div_alloc(isl_basic_set_get_dim(bset));
239 c = isl_constraint_add_div(c, div, &pos);
240 isl_int_set_si(v, 3);
241 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
242 bset = isl_basic_set_add_constraint(bset, c);
244 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
245 isl_int_set_si(v, -1);
246 isl_constraint_set_constant(c, v);
247 isl_int_set_si(v, 1);
248 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
249 div = isl_div_alloc(isl_basic_set_get_dim(bset));
250 c = isl_constraint_add_div(c, div, &pos);
251 isl_int_set_si(v, 3);
252 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
253 bset = isl_basic_set_add_constraint(bset, c);
255 assert(bset && bset->n_div == 1);
256 isl_basic_set_free(bset);
259 dim = isl_dim_set_alloc(ctx, 0, 1);
260 bset = isl_basic_set_universe(dim);
262 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
263 isl_int_set_si(v, 1);
264 isl_constraint_set_constant(c, v);
265 isl_int_set_si(v, -1);
266 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
267 div = isl_div_alloc(isl_basic_set_get_dim(bset));
268 c = isl_constraint_add_div(c, div, &pos);
269 isl_int_set_si(v, 3);
270 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
271 bset = isl_basic_set_add_constraint(bset, c);
273 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
274 isl_int_set_si(v, -3);
275 isl_constraint_set_constant(c, v);
276 isl_int_set_si(v, 1);
277 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
278 div = isl_div_alloc(isl_basic_set_get_dim(bset));
279 c = isl_constraint_add_div(c, div, &pos);
280 isl_int_set_si(v, 4);
281 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
282 bset = isl_basic_set_add_constraint(bset, c);
284 assert(bset && bset->n_div == 1);
285 isl_basic_set_free(bset);
288 dim = isl_dim_set_alloc(ctx, 0, 1);
289 bset = isl_basic_set_universe(dim);
291 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
292 isl_int_set_si(v, 2);
293 isl_constraint_set_constant(c, v);
294 isl_int_set_si(v, -1);
295 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
296 div = isl_div_alloc(isl_basic_set_get_dim(bset));
297 c = isl_constraint_add_div(c, div, &pos);
298 isl_int_set_si(v, 3);
299 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
300 bset = isl_basic_set_add_constraint(bset, c);
302 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
303 isl_int_set_si(v, -1);
304 isl_constraint_set_constant(c, v);
305 isl_int_set_si(v, 1);
306 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
307 div = isl_div_alloc(isl_basic_set_get_dim(bset));
308 c = isl_constraint_add_div(c, div, &pos);
309 isl_int_set_si(v, 6);
310 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
311 bset = isl_basic_set_add_constraint(bset, c);
313 assert(isl_basic_set_is_empty(bset));
314 isl_basic_set_free(bset);
317 dim = isl_dim_set_alloc(ctx, 0, 2);
318 bset = isl_basic_set_universe(dim);
320 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
321 isl_int_set_si(v, -1);
322 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
323 div = isl_div_alloc(isl_basic_set_get_dim(bset));
324 c = isl_constraint_add_div(c, div, &pos);
325 isl_int_set_si(v, 3);
326 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
327 bset = isl_basic_set_add_constraint(bset, c);
329 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
330 isl_int_set_si(v, 1);
331 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
332 isl_int_set_si(v, -3);
333 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
334 bset = isl_basic_set_add_constraint(bset, c);
336 assert(bset && bset->n_div == 0);
337 isl_basic_set_free(bset);
340 dim = isl_dim_set_alloc(ctx, 0, 2);
341 bset = isl_basic_set_universe(dim);
343 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
344 isl_int_set_si(v, -1);
345 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
346 div = isl_div_alloc(isl_basic_set_get_dim(bset));
347 c = isl_constraint_add_div(c, div, &pos);
348 isl_int_set_si(v, 6);
349 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
350 bset = isl_basic_set_add_constraint(bset, c);
352 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
353 isl_int_set_si(v, 1);
354 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
355 isl_int_set_si(v, -3);
356 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
357 bset = isl_basic_set_add_constraint(bset, c);
359 assert(bset && bset->n_div == 1);
360 isl_basic_set_free(bset);
363 /* This test is a bit tricky. We set up an equality
365 * Normalization of divs creates _two_ divs
368 * Afterwards e0 is removed again because it has coefficient -1
369 * and we end up with the original equality and div again.
370 * Perhaps we can avoid the introduction of this temporary div.
372 dim = isl_dim_set_alloc(ctx, 0, 3);
373 bset = isl_basic_set_universe(dim);
375 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
376 isl_int_set_si(v, -1);
377 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
378 isl_int_set_si(v, -3);
379 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
380 isl_int_set_si(v, -3);
381 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
382 div = isl_div_alloc(isl_basic_set_get_dim(bset));
383 c = isl_constraint_add_div(c, div, &pos);
384 isl_int_set_si(v, 6);
385 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
386 bset = isl_basic_set_add_constraint(bset, c);
388 /* Test disabled for now */
390 assert(bset && bset->n_div == 1);
392 isl_basic_set_free(bset);
395 dim = isl_dim_set_alloc(ctx, 0, 4);
396 bset = isl_basic_set_universe(dim);
398 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
399 isl_int_set_si(v, -1);
400 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
401 isl_int_set_si(v, -3);
402 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
403 isl_int_set_si(v, -3);
404 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
405 div = isl_div_alloc(isl_basic_set_get_dim(bset));
406 c = isl_constraint_add_div(c, div, &pos);
407 isl_int_set_si(v, 6);
408 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
409 bset = isl_basic_set_add_constraint(bset, c);
411 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
412 isl_int_set_si(v, -1);
413 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
414 isl_int_set_si(v, 1);
415 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
416 isl_int_set_si(v, 1);
417 isl_constraint_set_constant(c, v);
418 bset = isl_basic_set_add_constraint(bset, c);
420 /* Test disabled for now */
422 assert(bset && bset->n_div == 1);
424 isl_basic_set_free(bset);
427 dim = isl_dim_set_alloc(ctx, 0, 2);
428 bset = isl_basic_set_universe(dim);
430 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
431 isl_int_set_si(v, 1);
432 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
433 isl_int_set_si(v, -1);
434 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
435 div = isl_div_alloc(isl_basic_set_get_dim(bset));
436 c = isl_constraint_add_div(c, div, &pos);
437 isl_int_set_si(v, -2);
438 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
439 bset = isl_basic_set_add_constraint(bset, c);
441 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
442 isl_int_set_si(v, -1);
443 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
444 div = isl_div_alloc(isl_basic_set_get_dim(bset));
445 c = isl_constraint_add_div(c, div, &pos);
446 isl_int_set_si(v, 3);
447 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
448 isl_int_set_si(v, 2);
449 isl_constraint_set_constant(c, v);
450 bset = isl_basic_set_add_constraint(bset, c);
452 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
454 assert(!isl_basic_set_is_empty(bset));
456 isl_basic_set_free(bset);
459 dim = isl_dim_set_alloc(ctx, 0, 2);
460 bset = isl_basic_set_universe(dim);
462 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
463 isl_int_set_si(v, 1);
464 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
465 div = isl_div_alloc(isl_basic_set_get_dim(bset));
466 c = isl_constraint_add_div(c, div, &pos);
467 isl_int_set_si(v, -2);
468 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
469 bset = isl_basic_set_add_constraint(bset, c);
471 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
473 isl_basic_set_free(bset);
478 void test_application_case(struct isl_ctx *ctx, const char *name)
480 char filename[PATH_MAX];
483 struct isl_basic_set *bset1, *bset2;
484 struct isl_basic_map *bmap;
486 n = snprintf(filename, sizeof(filename),
487 "%s/test_inputs/%s.omega", srcdir, name);
488 assert(n < sizeof(filename));
489 input = fopen(filename, "r");
492 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
493 bmap = isl_basic_map_read_from_file(ctx, input, 0);
495 bset1 = isl_basic_set_apply(bset1, bmap);
497 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
499 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
501 isl_basic_set_free(bset1);
502 isl_basic_set_free(bset2);
507 void test_application(struct isl_ctx *ctx)
509 test_application_case(ctx, "application");
510 test_application_case(ctx, "application2");
513 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
515 char filename[PATH_MAX];
518 struct isl_basic_set *bset1, *bset2;
520 n = snprintf(filename, sizeof(filename),
521 "%s/test_inputs/%s.polylib", srcdir, name);
522 assert(n < sizeof(filename));
523 input = fopen(filename, "r");
526 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
527 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
529 bset1 = isl_basic_set_affine_hull(bset1);
531 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
533 isl_basic_set_free(bset1);
534 isl_basic_set_free(bset2);
539 void test_affine_hull(struct isl_ctx *ctx)
541 test_affine_hull_case(ctx, "affine2");
542 test_affine_hull_case(ctx, "affine");
543 test_affine_hull_case(ctx, "affine3");
546 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
548 char filename[PATH_MAX];
551 struct isl_basic_set *bset1, *bset2;
554 n = snprintf(filename, sizeof(filename),
555 "%s/test_inputs/%s.polylib", srcdir, name);
556 assert(n < sizeof(filename));
557 input = fopen(filename, "r");
560 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
561 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
563 set = isl_basic_set_union(bset1, bset2);
564 bset1 = isl_set_convex_hull(set);
566 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
568 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
570 isl_basic_set_free(bset1);
571 isl_basic_set_free(bset2);
576 void test_convex_hull(struct isl_ctx *ctx)
578 const char *str1, *str2;
579 isl_set *set1, *set2;
581 test_convex_hull_case(ctx, "convex0");
582 test_convex_hull_case(ctx, "convex1");
583 test_convex_hull_case(ctx, "convex2");
584 test_convex_hull_case(ctx, "convex3");
585 test_convex_hull_case(ctx, "convex4");
586 test_convex_hull_case(ctx, "convex5");
587 test_convex_hull_case(ctx, "convex6");
588 test_convex_hull_case(ctx, "convex7");
589 test_convex_hull_case(ctx, "convex8");
590 test_convex_hull_case(ctx, "convex9");
591 test_convex_hull_case(ctx, "convex10");
592 test_convex_hull_case(ctx, "convex11");
593 test_convex_hull_case(ctx, "convex12");
594 test_convex_hull_case(ctx, "convex13");
595 test_convex_hull_case(ctx, "convex14");
596 test_convex_hull_case(ctx, "convex15");
598 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
599 "(i0 = 1 and i1 = 0 and i2 = 1) or "
600 "(i0 = 0 and i1 = 0 and i2 = 0) }";
601 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
602 set1 = isl_set_read_from_str(ctx, str1, -1);
603 set2 = isl_set_read_from_str(ctx, str2, -1);
604 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
605 assert(isl_set_is_equal(set1, set2));
610 void test_gist_case(struct isl_ctx *ctx, const char *name)
612 char filename[PATH_MAX];
615 struct isl_basic_set *bset1, *bset2;
617 n = snprintf(filename, sizeof(filename),
618 "%s/test_inputs/%s.polylib", srcdir, name);
619 assert(n < sizeof(filename));
620 input = fopen(filename, "r");
623 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
624 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
626 bset1 = isl_basic_set_gist(bset1, bset2);
628 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
630 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
632 isl_basic_set_free(bset1);
633 isl_basic_set_free(bset2);
638 void test_gist(struct isl_ctx *ctx)
640 test_gist_case(ctx, "gist1");
643 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
647 set = isl_set_read_from_str(ctx, str, -1);
648 set = isl_set_coalesce(set);
649 set2 = isl_set_read_from_str(ctx, str, -1);
650 assert(isl_set_is_equal(set, set2));
652 assert(set && set->n == 1);
657 void test_coalesce(struct isl_ctx *ctx)
660 struct isl_set *set, *set2;
661 struct isl_map *map, *map2;
663 set = isl_set_read_from_str(ctx,
664 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
665 "y >= x & x >= 2 & 5 >= y }", -1);
666 set = isl_set_coalesce(set);
667 assert(set && set->n == 1);
670 set = isl_set_read_from_str(ctx,
671 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
672 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
673 set = isl_set_coalesce(set);
674 assert(set && set->n == 1);
677 set = isl_set_read_from_str(ctx,
678 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
679 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
680 set = isl_set_coalesce(set);
681 assert(set && set->n == 2);
684 set = isl_set_read_from_str(ctx,
685 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
686 "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
687 set = isl_set_coalesce(set);
688 assert(set && set->n == 1);
691 set = isl_set_read_from_str(ctx,
692 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
693 "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
694 set = isl_set_coalesce(set);
695 assert(set && set->n == 2);
698 set = isl_set_read_from_str(ctx,
699 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
700 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
701 set = isl_set_coalesce(set);
702 assert(set && set->n == 2);
705 set = isl_set_read_from_str(ctx,
706 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
707 "y >= 0 & x = 6 & y <= 6}", -1);
708 set = isl_set_coalesce(set);
709 assert(set && set->n == 1);
712 set = isl_set_read_from_str(ctx,
713 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
714 "y >= 0 & x = 7 & y <= 6}", -1);
715 set = isl_set_coalesce(set);
716 assert(set && set->n == 2);
719 set = isl_set_read_from_str(ctx,
720 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
721 "y >= 0 & x = 6 & y <= 5}", -1);
722 set = isl_set_coalesce(set);
723 assert(set && set->n == 1);
724 set2 = isl_set_read_from_str(ctx,
725 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
726 "y >= 0 & x = 6 & y <= 5}", -1);
727 assert(isl_set_is_equal(set, set2));
731 set = isl_set_read_from_str(ctx,
732 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
733 "y >= 0 & x = 6 & y <= 7}", -1);
734 set = isl_set_coalesce(set);
735 assert(set && set->n == 2);
738 set = isl_set_read_from_str(ctx,
739 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
740 set = isl_set_coalesce(set);
741 assert(set && set->n == 1);
742 set2 = isl_set_read_from_str(ctx,
743 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
744 assert(isl_set_is_equal(set, set2));
748 set = isl_set_read_from_str(ctx,
749 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
750 set = isl_set_coalesce(set);
751 set2 = isl_set_read_from_str(ctx,
752 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
753 assert(isl_set_is_equal(set, set2));
757 set = isl_set_read_from_str(ctx,
758 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
759 "2 <= i and i <= n }", -1);
760 set = isl_set_coalesce(set);
761 assert(set && set->n == 1);
762 set2 = isl_set_read_from_str(ctx,
763 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
764 "2 <= i and i <= n }", -1);
765 assert(isl_set_is_equal(set, set2));
769 map = isl_map_read_from_str(ctx,
770 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
771 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
772 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
773 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
774 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
775 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
776 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
777 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
778 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
779 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
780 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
781 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
782 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
783 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
784 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
785 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
786 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
787 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
788 map = isl_map_coalesce(map);
789 map2 = isl_map_read_from_str(ctx,
790 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
791 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
792 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
793 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
794 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
795 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
796 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
797 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
798 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
799 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
800 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
801 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
802 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
803 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
804 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
805 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
806 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
807 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
808 assert(isl_map_is_equal(map, map2));
812 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
813 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
814 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
815 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
816 map = isl_map_read_from_str(ctx, str, -1);
817 map = isl_map_coalesce(map);
818 map2 = isl_map_read_from_str(ctx, str, -1);
819 assert(isl_map_is_equal(map, map2));
823 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
824 "[o0, o1, o2, o3, o4, o5, o6] : "
825 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
826 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
827 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
828 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
829 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
830 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
831 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
832 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
833 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
834 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
835 "o6 >= i3 + i6 - o3 and M >= 0 and "
836 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
837 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
838 map = isl_map_read_from_str(ctx, str, -1);
839 map = isl_map_coalesce(map);
840 map2 = isl_map_read_from_str(ctx, str, -1);
841 assert(isl_map_is_equal(map, map2));
845 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
846 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
847 "(o0 = 0 and M >= 2 and N >= 3) or "
848 "(M = 0 and o0 = 0 and N >= 3) }";
849 map = isl_map_read_from_str(ctx, str, -1);
850 map = isl_map_coalesce(map);
851 map2 = isl_map_read_from_str(ctx, str, -1);
852 assert(isl_map_is_equal(map, map2));
856 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
857 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
858 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
859 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
860 map = isl_map_read_from_str(ctx, str, -1);
861 map = isl_map_coalesce(map);
862 map2 = isl_map_read_from_str(ctx, str, -1);
863 assert(isl_map_is_equal(map, map2));
867 test_coalesce_set(ctx,
868 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
869 "(i1 = M and M >= 1) }", 0);
870 test_coalesce_set(ctx,
871 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
872 test_coalesce_set(ctx,
873 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
874 "(y = 3 and x = 1) }", 1);
875 test_coalesce_set(ctx,
876 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
877 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
878 "i1 <= M and i3 <= M and i4 <= M) or "
879 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
880 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
881 "i4 <= -1 + M) }", 1);
882 test_coalesce_set(ctx,
883 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
884 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
885 test_coalesce_set(ctx,
886 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
887 "-x - y + 1 >= 0 and -3 <= z <= 3;"
888 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
889 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
890 test_coalesce_set(ctx,
891 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
892 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
893 test_coalesce_set(ctx,
894 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
895 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
898 void test_closure(struct isl_ctx *ctx)
906 /* COCOA example 1 */
907 map = isl_map_read_from_str(ctx,
908 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
909 "1 <= i and i < n and 1 <= j and j < n or "
910 "i2 = i + 1 and j2 = j - 1 and "
911 "1 <= i and i < n and 2 <= j and j <= n }", -1);
912 map = isl_map_power(map, 1, &exact);
916 /* COCOA example 1 */
917 map = isl_map_read_from_str(ctx,
918 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
919 "1 <= i and i < n and 1 <= j and j < n or "
920 "i2 = i + 1 and j2 = j - 1 and "
921 "1 <= i and i < n and 2 <= j and j <= n }", -1);
922 map = isl_map_transitive_closure(map, &exact);
924 map2 = isl_map_read_from_str(ctx,
925 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
926 "1 <= i and i < n and 1 <= j and j <= n and "
927 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
928 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
929 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
930 assert(isl_map_is_equal(map, map2));
934 map = isl_map_read_from_str(ctx,
935 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
936 " 0 <= y and y <= n }", -1);
937 map = isl_map_transitive_closure(map, &exact);
938 map2 = isl_map_read_from_str(ctx,
939 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
940 " 0 <= y and y <= n }", -1);
941 assert(isl_map_is_equal(map, map2));
945 /* COCOA example 2 */
946 map = isl_map_read_from_str(ctx,
947 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
948 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
949 "i2 = i + 2 and j2 = j - 2 and "
950 "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
951 map = isl_map_transitive_closure(map, &exact);
953 map2 = isl_map_read_from_str(ctx,
954 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
955 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
956 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
957 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
958 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
959 assert(isl_map_is_equal(map, map2));
963 /* COCOA Fig.2 left */
964 map = isl_map_read_from_str(ctx,
965 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
966 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
968 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
969 "j <= 2 i - 3 and j <= n - 2 or "
970 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
971 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
972 map = isl_map_transitive_closure(map, &exact);
976 /* COCOA Fig.2 right */
977 map = isl_map_read_from_str(ctx,
978 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
979 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
981 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
982 "j <= 2 i - 4 and j <= n - 3 or "
983 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
984 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
985 map = isl_map_power(map, 1, &exact);
989 /* COCOA Fig.2 right */
990 map = isl_map_read_from_str(ctx,
991 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
992 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
994 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
995 "j <= 2 i - 4 and j <= n - 3 or "
996 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
997 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
998 map = isl_map_transitive_closure(map, &exact);
1000 map2 = isl_map_read_from_str(ctx,
1001 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1002 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1003 "j <= n and 3 + i + 2 j <= 3 n and "
1004 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1005 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1006 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1007 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1008 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
1009 assert(isl_map_is_equal(map, map2));
1013 /* COCOA Fig.1 right */
1014 dom = isl_set_read_from_str(ctx,
1015 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1016 "2 x - 3 y + 3 >= 0 }", -1);
1017 right = isl_map_read_from_str(ctx,
1018 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
1019 up = isl_map_read_from_str(ctx,
1020 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1021 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1022 right = isl_map_intersect_range(right, isl_set_copy(dom));
1023 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1024 up = isl_map_intersect_range(up, dom);
1025 map = isl_map_union(up, right);
1026 map = isl_map_transitive_closure(map, &exact);
1028 map2 = isl_map_read_from_str(ctx,
1029 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1030 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1031 assert(isl_map_is_equal(map, map2));
1035 /* COCOA Theorem 1 counter example */
1036 map = isl_map_read_from_str(ctx,
1037 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1038 "i2 = 1 and j2 = j or "
1039 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1040 map = isl_map_transitive_closure(map, &exact);
1044 map = isl_map_read_from_str(ctx,
1045 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1046 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1047 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1048 "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1049 map = isl_map_transitive_closure(map, &exact);
1053 /* Kelly et al 1996, fig 12 */
1054 map = isl_map_read_from_str(ctx,
1055 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1056 "1 <= i,j,j+1 <= n or "
1057 "j = n and j2 = 1 and i2 = i + 1 and "
1058 "1 <= i,i+1 <= n }", -1);
1059 map = isl_map_transitive_closure(map, &exact);
1061 map2 = isl_map_read_from_str(ctx,
1062 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1063 "1 <= i <= n and i = i2 or "
1064 "1 <= i < i2 <= n and 1 <= j <= n and "
1065 "1 <= j2 <= n }", -1);
1066 assert(isl_map_is_equal(map, map2));
1070 /* Omega's closure4 */
1071 map = isl_map_read_from_str(ctx,
1072 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1073 "1 <= x,y <= 10 or "
1074 "x2 = x + 1 and y2 = y and "
1075 "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1076 map = isl_map_transitive_closure(map, &exact);
1080 map = isl_map_read_from_str(ctx,
1081 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1082 map = isl_map_transitive_closure(map, &exact);
1084 map2 = isl_map_read_from_str(ctx,
1085 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1086 assert(isl_map_is_equal(map, map2));
1090 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1091 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1092 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1093 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1094 map = isl_map_read_from_str(ctx, str, -1);
1095 map = isl_map_transitive_closure(map, &exact);
1097 map2 = isl_map_read_from_str(ctx, str, -1);
1098 assert(isl_map_is_equal(map, map2));
1102 str = "{[0] -> [1]; [2] -> [3]}";
1103 map = isl_map_read_from_str(ctx, str, -1);
1104 map = isl_map_transitive_closure(map, &exact);
1106 map2 = isl_map_read_from_str(ctx, str, -1);
1107 assert(isl_map_is_equal(map, map2));
1112 void test_lex(struct isl_ctx *ctx)
1117 dim = isl_dim_alloc(ctx, 0, 0, 0);
1118 map = isl_map_lex_le(dim);
1119 assert(!isl_map_is_empty(map));
1123 void test_lexmin(struct isl_ctx *ctx)
1126 isl_map *map, *map2;
1130 str = "[p0, p1] -> { [] -> [] : "
1131 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1132 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1133 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1134 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1135 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1136 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1137 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1138 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1139 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1140 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1141 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1142 map = isl_map_read_from_str(ctx, str, -1);
1143 map = isl_map_lexmin(map);
1146 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1147 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1148 set = isl_set_read_from_str(ctx, str, -1);
1149 set = isl_set_lexmax(set);
1150 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1151 set2 = isl_set_read_from_str(ctx, str, -1);
1152 set = isl_set_intersect(set, set2);
1153 assert(!isl_set_is_empty(set));
1156 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1157 map = isl_map_read_from_str(ctx, str, -1);
1158 map = isl_map_lexmin(map);
1159 str = "{ [x] -> [5] : 6 <= x <= 8; "
1160 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1161 map2 = isl_map_read_from_str(ctx, str, -1);
1162 assert(isl_map_is_equal(map, map2));
1166 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1167 map = isl_map_read_from_str(ctx, str, -1);
1168 map2 = isl_map_copy(map);
1169 map = isl_map_lexmin(map);
1170 assert(isl_map_is_equal(map, map2));
1174 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1175 map = isl_map_read_from_str(ctx, str, -1);
1176 map = isl_map_lexmin(map);
1177 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1178 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1179 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1180 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1181 map2 = isl_map_read_from_str(ctx, str, -1);
1182 assert(isl_map_is_equal(map, map2));
1192 static int collect_must_may(__isl_take isl_map *dep, int must,
1193 void *dep_user, void *user)
1195 struct must_may *mm = (struct must_may *)user;
1198 mm->must = isl_map_union(mm->must, dep);
1200 mm->may = isl_map_union(mm->may, dep);
1205 static int common_space(void *first, void *second)
1207 int depth = *(int *)first;
1211 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1219 map2 = isl_map_read_from_str(map->ctx, str, -1);
1220 equal = isl_map_is_equal(map, map2);
1226 void test_dep(struct isl_ctx *ctx)
1231 isl_access_info *ai;
1238 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1239 map = isl_map_read_from_str(ctx, str, -1);
1240 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1242 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1243 map = isl_map_read_from_str(ctx, str, -1);
1244 ai = isl_access_info_add_source(ai, map, 1, &depth);
1246 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1247 map = isl_map_read_from_str(ctx, str, -1);
1248 ai = isl_access_info_add_source(ai, map, 1, &depth);
1250 flow = isl_access_info_compute_flow(ai);
1251 dim = isl_dim_alloc(ctx, 0, 3, 3);
1252 mm.must = isl_map_empty(isl_dim_copy(dim));
1253 mm.may = isl_map_empty(dim);
1255 isl_flow_foreach(flow, collect_must_may, &mm);
1257 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1258 " [1,10,0] -> [2,5,0] }";
1259 assert(map_is_equal(mm.must, str));
1260 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1261 assert(map_is_equal(mm.may, str));
1263 isl_map_free(mm.must);
1264 isl_map_free(mm.may);
1265 isl_flow_free(flow);
1268 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1269 map = isl_map_read_from_str(ctx, str, -1);
1270 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1272 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1273 map = isl_map_read_from_str(ctx, str, -1);
1274 ai = isl_access_info_add_source(ai, map, 1, &depth);
1276 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1277 map = isl_map_read_from_str(ctx, str, -1);
1278 ai = isl_access_info_add_source(ai, map, 0, &depth);
1280 flow = isl_access_info_compute_flow(ai);
1281 dim = isl_dim_alloc(ctx, 0, 3, 3);
1282 mm.must = isl_map_empty(isl_dim_copy(dim));
1283 mm.may = isl_map_empty(dim);
1285 isl_flow_foreach(flow, collect_must_may, &mm);
1287 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1288 assert(map_is_equal(mm.must, str));
1289 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1290 assert(map_is_equal(mm.may, str));
1292 isl_map_free(mm.must);
1293 isl_map_free(mm.may);
1294 isl_flow_free(flow);
1297 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1298 map = isl_map_read_from_str(ctx, str, -1);
1299 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1301 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1302 map = isl_map_read_from_str(ctx, str, -1);
1303 ai = isl_access_info_add_source(ai, map, 0, &depth);
1305 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1306 map = isl_map_read_from_str(ctx, str, -1);
1307 ai = isl_access_info_add_source(ai, map, 0, &depth);
1309 flow = isl_access_info_compute_flow(ai);
1310 dim = isl_dim_alloc(ctx, 0, 3, 3);
1311 mm.must = isl_map_empty(isl_dim_copy(dim));
1312 mm.may = isl_map_empty(dim);
1314 isl_flow_foreach(flow, collect_must_may, &mm);
1316 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1317 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1318 assert(map_is_equal(mm.may, str));
1319 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1320 assert(map_is_equal(mm.must, str));
1322 isl_map_free(mm.must);
1323 isl_map_free(mm.may);
1324 isl_flow_free(flow);
1327 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1328 map = isl_map_read_from_str(ctx, str, -1);
1329 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1331 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1332 map = isl_map_read_from_str(ctx, str, -1);
1333 ai = isl_access_info_add_source(ai, map, 0, &depth);
1335 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1336 map = isl_map_read_from_str(ctx, str, -1);
1337 ai = isl_access_info_add_source(ai, map, 0, &depth);
1339 flow = isl_access_info_compute_flow(ai);
1340 dim = isl_dim_alloc(ctx, 0, 3, 3);
1341 mm.must = isl_map_empty(isl_dim_copy(dim));
1342 mm.may = isl_map_empty(dim);
1344 isl_flow_foreach(flow, collect_must_may, &mm);
1346 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1347 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1348 assert(map_is_equal(mm.may, str));
1349 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1350 assert(map_is_equal(mm.must, str));
1352 isl_map_free(mm.must);
1353 isl_map_free(mm.may);
1354 isl_flow_free(flow);
1357 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1358 map = isl_map_read_from_str(ctx, str, -1);
1359 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1361 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1362 map = isl_map_read_from_str(ctx, str, -1);
1363 ai = isl_access_info_add_source(ai, map, 0, &depth);
1365 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1366 map = isl_map_read_from_str(ctx, str, -1);
1367 ai = isl_access_info_add_source(ai, map, 0, &depth);
1369 flow = isl_access_info_compute_flow(ai);
1370 dim = isl_dim_alloc(ctx, 0, 3, 3);
1371 mm.must = isl_map_empty(isl_dim_copy(dim));
1372 mm.may = isl_map_empty(dim);
1374 isl_flow_foreach(flow, collect_must_may, &mm);
1376 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1377 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1378 assert(map_is_equal(mm.may, str));
1379 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1380 assert(map_is_equal(mm.must, str));
1382 isl_map_free(mm.must);
1383 isl_map_free(mm.may);
1384 isl_flow_free(flow);
1389 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1390 map = isl_map_read_from_str(ctx, str, -1);
1391 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1393 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1394 map = isl_map_read_from_str(ctx, str, -1);
1395 ai = isl_access_info_add_source(ai, map, 1, &depth);
1397 flow = isl_access_info_compute_flow(ai);
1398 dim = isl_dim_alloc(ctx, 0, 5, 5);
1399 mm.must = isl_map_empty(isl_dim_copy(dim));
1400 mm.may = isl_map_empty(dim);
1402 isl_flow_foreach(flow, collect_must_may, &mm);
1404 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1405 assert(map_is_equal(mm.must, str));
1406 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1407 assert(map_is_equal(mm.may, str));
1409 isl_map_free(mm.must);
1410 isl_map_free(mm.may);
1411 isl_flow_free(flow);
1414 void test_sv(struct isl_ctx *ctx)
1419 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1420 map = isl_map_read_from_str(ctx, str, -1);
1421 assert(isl_map_is_single_valued(map));
1424 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1425 map = isl_map_read_from_str(ctx, str, -1);
1426 assert(!isl_map_is_single_valued(map));
1430 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1434 map = isl_map_read_from_str(ctx, str, -1);
1436 assert(isl_map_is_bijective(map));
1438 assert(!isl_map_is_bijective(map));
1442 void test_bijective(struct isl_ctx *ctx)
1444 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1445 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1446 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1447 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1448 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1449 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1450 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1451 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1452 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1453 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1454 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1455 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1456 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1459 void test_pwqp(struct isl_ctx *ctx)
1463 isl_pw_qpolynomial *pwqp1, *pwqp2;
1465 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1466 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1468 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1471 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1472 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1474 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1476 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1478 isl_pw_qpolynomial_free(pwqp1);
1480 str = "{ [i] -> i }";
1481 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1482 str = "{ [k] : exists a : k = 2a }";
1483 set = isl_set_read_from_str(ctx, str, 0);
1484 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1485 str = "{ [i] -> i }";
1486 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1488 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1490 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1492 isl_pw_qpolynomial_free(pwqp1);
1494 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1495 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1497 set = isl_set_read_from_str(ctx, str, 0);
1498 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1499 str = "{ [i] -> 16 }";
1500 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1502 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1504 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1506 isl_pw_qpolynomial_free(pwqp1);
1508 str = "{ [i] -> ([(i)/2]) }";
1509 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1510 str = "{ [k] : exists a : k = 2a+1 }";
1511 set = isl_set_read_from_str(ctx, str, 0);
1512 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1513 str = "{ [i] -> -1/2 + 1/2 * i }";
1514 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1516 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1518 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1520 isl_pw_qpolynomial_free(pwqp1);
1522 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1523 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1524 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1525 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1527 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1529 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1531 isl_pw_qpolynomial_free(pwqp1);
1533 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1534 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1535 str = "{ [x] -> x }";
1536 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1538 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1540 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1542 isl_pw_qpolynomial_free(pwqp1);
1545 void test_split_periods(isl_ctx *ctx)
1548 isl_pw_qpolynomial *pwqp;
1550 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1551 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1552 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1553 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1555 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1558 isl_pw_qpolynomial_free(pwqp);
1561 void test_union(isl_ctx *ctx)
1564 isl_union_set *uset1, *uset2;
1565 isl_union_map *umap1, *umap2;
1567 str = "{ [i] : 0 <= i <= 1 }";
1568 uset1 = isl_union_set_read_from_str(ctx, str);
1569 str = "{ [1] -> [0] }";
1570 umap1 = isl_union_map_read_from_str(ctx, str);
1572 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1573 assert(isl_union_map_is_equal(umap1, umap2));
1575 isl_union_map_free(umap1);
1576 isl_union_map_free(umap2);
1578 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1579 umap1 = isl_union_map_read_from_str(ctx, str);
1580 str = "{ A[i]; B[i] }";
1581 uset1 = isl_union_set_read_from_str(ctx, str);
1583 uset2 = isl_union_map_domain(umap1);
1585 assert(isl_union_set_is_equal(uset1, uset2));
1587 isl_union_set_free(uset1);
1588 isl_union_set_free(uset2);
1591 void test_bound(isl_ctx *ctx)
1594 isl_pw_qpolynomial *pwqp;
1595 isl_pw_qpolynomial_fold *pwf;
1597 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1598 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1599 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1600 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1601 isl_pw_qpolynomial_fold_free(pwf);
1604 void test_lift(isl_ctx *ctx)
1607 isl_basic_map *bmap;
1608 isl_basic_set *bset;
1610 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1611 bset = isl_basic_set_read_from_str(ctx, str, 0);
1612 bset = isl_basic_set_lift(bset);
1613 bmap = isl_basic_map_from_range(bset);
1614 bset = isl_basic_map_domain(bmap);
1615 isl_basic_set_free(bset);
1618 void test_subset(isl_ctx *ctx)
1621 isl_set *set1, *set2;
1623 str = "{ [112, 0] }";
1624 set1 = isl_set_read_from_str(ctx, str, 0);
1625 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1626 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1627 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1628 set2 = isl_set_read_from_str(ctx, str, 0);
1629 assert(isl_set_is_subset(set1, set2));
1636 struct isl_ctx *ctx;
1638 srcdir = getenv("srcdir");
1641 ctx = isl_ctx_alloc();
1646 test_split_periods(ctx);
1651 test_bijective(ctx);
1655 test_construction(ctx);
1658 test_application(ctx);
1659 test_affine_hull(ctx);
1660 test_convex_hull(ctx);