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_algo(struct isl_ctx *ctx, int convex)
578 const char *str1, *str2;
579 isl_set *set1, *set2;
580 int orig_convex = ctx->opt->convex;
581 ctx->opt->convex = convex;
583 test_convex_hull_case(ctx, "convex0");
584 test_convex_hull_case(ctx, "convex1");
585 test_convex_hull_case(ctx, "convex2");
586 test_convex_hull_case(ctx, "convex3");
587 test_convex_hull_case(ctx, "convex4");
588 test_convex_hull_case(ctx, "convex5");
589 test_convex_hull_case(ctx, "convex6");
590 test_convex_hull_case(ctx, "convex7");
591 test_convex_hull_case(ctx, "convex8");
592 test_convex_hull_case(ctx, "convex9");
593 test_convex_hull_case(ctx, "convex10");
594 test_convex_hull_case(ctx, "convex11");
595 test_convex_hull_case(ctx, "convex12");
596 test_convex_hull_case(ctx, "convex13");
597 test_convex_hull_case(ctx, "convex14");
598 test_convex_hull_case(ctx, "convex15");
600 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
601 "(i0 = 1 and i1 = 0 and i2 = 1) or "
602 "(i0 = 0 and i1 = 0 and i2 = 0) }";
603 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
604 set1 = isl_set_read_from_str(ctx, str1, -1);
605 set2 = isl_set_read_from_str(ctx, str2, -1);
606 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
607 assert(isl_set_is_equal(set1, set2));
611 ctx->opt->convex = orig_convex;
614 void test_convex_hull(struct isl_ctx *ctx)
616 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_FM);
617 test_convex_hull_algo(ctx, ISL_CONVEX_HULL_WRAP);
620 void test_gist_case(struct isl_ctx *ctx, const char *name)
622 char filename[PATH_MAX];
625 struct isl_basic_set *bset1, *bset2;
627 n = snprintf(filename, sizeof(filename),
628 "%s/test_inputs/%s.polylib", srcdir, name);
629 assert(n < sizeof(filename));
630 input = fopen(filename, "r");
633 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
634 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
636 bset1 = isl_basic_set_gist(bset1, bset2);
638 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
640 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
642 isl_basic_set_free(bset1);
643 isl_basic_set_free(bset2);
648 void test_gist(struct isl_ctx *ctx)
650 test_gist_case(ctx, "gist1");
653 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
657 set = isl_set_read_from_str(ctx, str, -1);
658 set = isl_set_coalesce(set);
659 set2 = isl_set_read_from_str(ctx, str, -1);
660 assert(isl_set_is_equal(set, set2));
662 assert(set && set->n == 1);
667 void test_coalesce(struct isl_ctx *ctx)
670 struct isl_set *set, *set2;
671 struct isl_map *map, *map2;
673 set = isl_set_read_from_str(ctx,
674 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
675 "y >= x & x >= 2 & 5 >= y }", -1);
676 set = isl_set_coalesce(set);
677 assert(set && set->n == 1);
680 set = isl_set_read_from_str(ctx,
681 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
682 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
683 set = isl_set_coalesce(set);
684 assert(set && set->n == 1);
687 set = isl_set_read_from_str(ctx,
688 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
689 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
690 set = isl_set_coalesce(set);
691 assert(set && set->n == 2);
694 set = isl_set_read_from_str(ctx,
695 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
696 "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
697 set = isl_set_coalesce(set);
698 assert(set && set->n == 1);
701 set = isl_set_read_from_str(ctx,
702 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
703 "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
704 set = isl_set_coalesce(set);
705 assert(set && set->n == 2);
708 set = isl_set_read_from_str(ctx,
709 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
710 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
711 set = isl_set_coalesce(set);
712 assert(set && set->n == 2);
715 set = isl_set_read_from_str(ctx,
716 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
717 "y >= 0 & x = 6 & y <= 6}", -1);
718 set = isl_set_coalesce(set);
719 assert(set && set->n == 1);
722 set = isl_set_read_from_str(ctx,
723 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
724 "y >= 0 & x = 7 & y <= 6}", -1);
725 set = isl_set_coalesce(set);
726 assert(set && set->n == 2);
729 set = isl_set_read_from_str(ctx,
730 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
731 "y >= 0 & x = 6 & y <= 5}", -1);
732 set = isl_set_coalesce(set);
733 assert(set && set->n == 1);
734 set2 = isl_set_read_from_str(ctx,
735 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
736 "y >= 0 & x = 6 & y <= 5}", -1);
737 assert(isl_set_is_equal(set, set2));
741 set = isl_set_read_from_str(ctx,
742 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
743 "y >= 0 & x = 6 & y <= 7}", -1);
744 set = isl_set_coalesce(set);
745 assert(set && set->n == 2);
748 set = isl_set_read_from_str(ctx,
749 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
750 set = isl_set_coalesce(set);
751 assert(set && set->n == 1);
752 set2 = isl_set_read_from_str(ctx,
753 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
754 assert(isl_set_is_equal(set, set2));
758 set = isl_set_read_from_str(ctx,
759 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
760 set = isl_set_coalesce(set);
761 set2 = isl_set_read_from_str(ctx,
762 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
763 assert(isl_set_is_equal(set, set2));
767 set = isl_set_read_from_str(ctx,
768 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
769 "2 <= i and i <= n }", -1);
770 set = isl_set_coalesce(set);
771 assert(set && set->n == 1);
772 set2 = isl_set_read_from_str(ctx,
773 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
774 "2 <= i and i <= n }", -1);
775 assert(isl_set_is_equal(set, set2));
779 map = isl_map_read_from_str(ctx,
780 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
781 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
782 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
783 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
784 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
785 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
786 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
787 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
788 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
789 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
790 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
791 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
792 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
793 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
794 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
795 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
796 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
797 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
798 map = isl_map_coalesce(map);
799 map2 = isl_map_read_from_str(ctx,
800 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
801 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
802 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
803 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
804 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
805 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
806 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
807 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
808 "[i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
809 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
810 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
811 "4e4 = -2 + o0 and 2e0 >= 3 + n and e0 <= -4 + n and "
812 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
813 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
814 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
815 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
816 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
817 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
818 assert(isl_map_is_equal(map, map2));
822 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
823 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
824 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
825 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
826 map = isl_map_read_from_str(ctx, str, -1);
827 map = isl_map_coalesce(map);
828 map2 = isl_map_read_from_str(ctx, str, -1);
829 assert(isl_map_is_equal(map, map2));
833 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
834 "[o0, o1, o2, o3, o4, o5, o6] : "
835 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
836 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
837 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
838 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
839 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
840 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
841 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
842 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
843 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
844 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
845 "o6 >= i3 + i6 - o3 and M >= 0 and "
846 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
847 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
848 map = isl_map_read_from_str(ctx, str, -1);
849 map = isl_map_coalesce(map);
850 map2 = isl_map_read_from_str(ctx, str, -1);
851 assert(isl_map_is_equal(map, map2));
855 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
856 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
857 "(o0 = 0 and M >= 2 and N >= 3) or "
858 "(M = 0 and o0 = 0 and N >= 3) }";
859 map = isl_map_read_from_str(ctx, str, -1);
860 map = isl_map_coalesce(map);
861 map2 = isl_map_read_from_str(ctx, str, -1);
862 assert(isl_map_is_equal(map, map2));
866 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
867 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
868 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
869 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
870 map = isl_map_read_from_str(ctx, str, -1);
871 map = isl_map_coalesce(map);
872 map2 = isl_map_read_from_str(ctx, str, -1);
873 assert(isl_map_is_equal(map, map2));
877 test_coalesce_set(ctx,
878 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
879 "(i1 = M and M >= 1) }", 0);
880 test_coalesce_set(ctx,
881 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
882 test_coalesce_set(ctx,
883 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
884 "(y = 3 and x = 1) }", 1);
885 test_coalesce_set(ctx,
886 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
887 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
888 "i1 <= M and i3 <= M and i4 <= M) or "
889 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
890 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
891 "i4 <= -1 + M) }", 1);
892 test_coalesce_set(ctx,
893 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
894 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
895 test_coalesce_set(ctx,
896 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
897 "-x - y + 1 >= 0 and -3 <= z <= 3;"
898 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
899 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
900 test_coalesce_set(ctx,
901 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
902 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
903 test_coalesce_set(ctx,
904 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
905 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
908 void test_closure(struct isl_ctx *ctx)
916 /* COCOA example 1 */
917 map = isl_map_read_from_str(ctx,
918 "[n,k] -> { [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_power(map, 1, &exact);
926 /* COCOA example 1 */
927 map = isl_map_read_from_str(ctx,
928 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
929 "1 <= i and i < n and 1 <= j and j < n or "
930 "i2 = i + 1 and j2 = j - 1 and "
931 "1 <= i and i < n and 2 <= j and j <= n }", -1);
932 map = isl_map_transitive_closure(map, &exact);
934 map2 = isl_map_read_from_str(ctx,
935 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
936 "1 <= i and i < n and 1 <= j and j <= n and "
937 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
938 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
939 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
940 assert(isl_map_is_equal(map, map2));
944 map = isl_map_read_from_str(ctx,
945 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
946 " 0 <= y and y <= n }", -1);
947 map = isl_map_transitive_closure(map, &exact);
948 map2 = isl_map_read_from_str(ctx,
949 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
950 " 0 <= y and y <= n }", -1);
951 assert(isl_map_is_equal(map, map2));
955 /* COCOA example 2 */
956 map = isl_map_read_from_str(ctx,
957 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
958 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
959 "i2 = i + 2 and j2 = j - 2 and "
960 "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
961 map = isl_map_transitive_closure(map, &exact);
963 map2 = isl_map_read_from_str(ctx,
964 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
965 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
966 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
967 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
968 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
969 assert(isl_map_is_equal(map, map2));
973 /* COCOA Fig.2 left */
974 map = isl_map_read_from_str(ctx,
975 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
976 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
978 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
979 "j <= 2 i - 3 and j <= n - 2 or "
980 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
981 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
982 map = isl_map_transitive_closure(map, &exact);
986 /* COCOA Fig.2 right */
987 map = isl_map_read_from_str(ctx,
988 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
989 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
991 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
992 "j <= 2 i - 4 and j <= n - 3 or "
993 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
994 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
995 map = isl_map_power(map, 1, &exact);
999 /* COCOA Fig.2 right */
1000 map = isl_map_read_from_str(ctx,
1001 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
1002 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
1004 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
1005 "j <= 2 i - 4 and j <= n - 3 or "
1006 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
1007 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
1008 map = isl_map_transitive_closure(map, &exact);
1010 map2 = isl_map_read_from_str(ctx,
1011 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
1012 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
1013 "j <= n and 3 + i + 2 j <= 3 n and "
1014 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
1015 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
1016 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
1017 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1018 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
1019 assert(isl_map_is_equal(map, map2));
1023 /* COCOA Fig.1 right */
1024 dom = isl_set_read_from_str(ctx,
1025 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1026 "2 x - 3 y + 3 >= 0 }", -1);
1027 right = isl_map_read_from_str(ctx,
1028 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
1029 up = isl_map_read_from_str(ctx,
1030 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1031 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1032 right = isl_map_intersect_range(right, isl_set_copy(dom));
1033 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1034 up = isl_map_intersect_range(up, dom);
1035 map = isl_map_union(up, right);
1036 map = isl_map_transitive_closure(map, &exact);
1038 map2 = isl_map_read_from_str(ctx,
1039 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1040 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1041 assert(isl_map_is_equal(map, map2));
1045 /* COCOA Theorem 1 counter example */
1046 map = isl_map_read_from_str(ctx,
1047 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1048 "i2 = 1 and j2 = j or "
1049 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1050 map = isl_map_transitive_closure(map, &exact);
1054 map = isl_map_read_from_str(ctx,
1055 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1056 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1057 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1058 "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1059 map = isl_map_transitive_closure(map, &exact);
1063 /* Kelly et al 1996, fig 12 */
1064 map = isl_map_read_from_str(ctx,
1065 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1066 "1 <= i,j,j+1 <= n or "
1067 "j = n and j2 = 1 and i2 = i + 1 and "
1068 "1 <= i,i+1 <= n }", -1);
1069 map = isl_map_transitive_closure(map, &exact);
1071 map2 = isl_map_read_from_str(ctx,
1072 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1073 "1 <= i <= n and i = i2 or "
1074 "1 <= i < i2 <= n and 1 <= j <= n and "
1075 "1 <= j2 <= n }", -1);
1076 assert(isl_map_is_equal(map, map2));
1080 /* Omega's closure4 */
1081 map = isl_map_read_from_str(ctx,
1082 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1083 "1 <= x,y <= 10 or "
1084 "x2 = x + 1 and y2 = y and "
1085 "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1086 map = isl_map_transitive_closure(map, &exact);
1090 map = isl_map_read_from_str(ctx,
1091 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1092 map = isl_map_transitive_closure(map, &exact);
1094 map2 = isl_map_read_from_str(ctx,
1095 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1096 assert(isl_map_is_equal(map, map2));
1100 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1101 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1102 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1103 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1104 map = isl_map_read_from_str(ctx, str, -1);
1105 map = isl_map_transitive_closure(map, &exact);
1107 map2 = isl_map_read_from_str(ctx, str, -1);
1108 assert(isl_map_is_equal(map, map2));
1112 str = "{[0] -> [1]; [2] -> [3]}";
1113 map = isl_map_read_from_str(ctx, str, -1);
1114 map = isl_map_transitive_closure(map, &exact);
1116 map2 = isl_map_read_from_str(ctx, str, -1);
1117 assert(isl_map_is_equal(map, map2));
1122 void test_lex(struct isl_ctx *ctx)
1127 dim = isl_dim_alloc(ctx, 0, 0, 0);
1128 map = isl_map_lex_le(dim);
1129 assert(!isl_map_is_empty(map));
1133 void test_lexmin(struct isl_ctx *ctx)
1136 isl_map *map, *map2;
1140 str = "[p0, p1] -> { [] -> [] : "
1141 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1142 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1143 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1144 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1145 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1146 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1147 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1148 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1149 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1150 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1151 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1152 map = isl_map_read_from_str(ctx, str, -1);
1153 map = isl_map_lexmin(map);
1156 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1157 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1158 set = isl_set_read_from_str(ctx, str, -1);
1159 set = isl_set_lexmax(set);
1160 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1161 set2 = isl_set_read_from_str(ctx, str, -1);
1162 set = isl_set_intersect(set, set2);
1163 assert(!isl_set_is_empty(set));
1166 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1167 map = isl_map_read_from_str(ctx, str, -1);
1168 map = isl_map_lexmin(map);
1169 str = "{ [x] -> [5] : 6 <= x <= 8; "
1170 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1171 map2 = isl_map_read_from_str(ctx, str, -1);
1172 assert(isl_map_is_equal(map, map2));
1176 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1177 map = isl_map_read_from_str(ctx, str, -1);
1178 map2 = isl_map_copy(map);
1179 map = isl_map_lexmin(map);
1180 assert(isl_map_is_equal(map, map2));
1184 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1185 map = isl_map_read_from_str(ctx, str, -1);
1186 map = isl_map_lexmin(map);
1187 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1188 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1189 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1190 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1191 map2 = isl_map_read_from_str(ctx, str, -1);
1192 assert(isl_map_is_equal(map, map2));
1202 static int collect_must_may(__isl_take isl_map *dep, int must,
1203 void *dep_user, void *user)
1205 struct must_may *mm = (struct must_may *)user;
1208 mm->must = isl_map_union(mm->must, dep);
1210 mm->may = isl_map_union(mm->may, dep);
1215 static int common_space(void *first, void *second)
1217 int depth = *(int *)first;
1221 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1229 map2 = isl_map_read_from_str(map->ctx, str, -1);
1230 equal = isl_map_is_equal(map, map2);
1236 void test_dep(struct isl_ctx *ctx)
1241 isl_access_info *ai;
1248 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1249 map = isl_map_read_from_str(ctx, str, -1);
1250 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1252 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1253 map = isl_map_read_from_str(ctx, str, -1);
1254 ai = isl_access_info_add_source(ai, map, 1, &depth);
1256 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1257 map = isl_map_read_from_str(ctx, str, -1);
1258 ai = isl_access_info_add_source(ai, map, 1, &depth);
1260 flow = isl_access_info_compute_flow(ai);
1261 dim = isl_dim_alloc(ctx, 0, 3, 3);
1262 mm.must = isl_map_empty(isl_dim_copy(dim));
1263 mm.may = isl_map_empty(dim);
1265 isl_flow_foreach(flow, collect_must_may, &mm);
1267 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1268 " [1,10,0] -> [2,5,0] }";
1269 assert(map_is_equal(mm.must, str));
1270 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1271 assert(map_is_equal(mm.may, str));
1273 isl_map_free(mm.must);
1274 isl_map_free(mm.may);
1275 isl_flow_free(flow);
1278 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1279 map = isl_map_read_from_str(ctx, str, -1);
1280 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1282 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1283 map = isl_map_read_from_str(ctx, str, -1);
1284 ai = isl_access_info_add_source(ai, map, 1, &depth);
1286 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1287 map = isl_map_read_from_str(ctx, str, -1);
1288 ai = isl_access_info_add_source(ai, map, 0, &depth);
1290 flow = isl_access_info_compute_flow(ai);
1291 dim = isl_dim_alloc(ctx, 0, 3, 3);
1292 mm.must = isl_map_empty(isl_dim_copy(dim));
1293 mm.may = isl_map_empty(dim);
1295 isl_flow_foreach(flow, collect_must_may, &mm);
1297 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1298 assert(map_is_equal(mm.must, str));
1299 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1300 assert(map_is_equal(mm.may, str));
1302 isl_map_free(mm.must);
1303 isl_map_free(mm.may);
1304 isl_flow_free(flow);
1307 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1308 map = isl_map_read_from_str(ctx, str, -1);
1309 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1311 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1312 map = isl_map_read_from_str(ctx, str, -1);
1313 ai = isl_access_info_add_source(ai, map, 0, &depth);
1315 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1316 map = isl_map_read_from_str(ctx, str, -1);
1317 ai = isl_access_info_add_source(ai, map, 0, &depth);
1319 flow = isl_access_info_compute_flow(ai);
1320 dim = isl_dim_alloc(ctx, 0, 3, 3);
1321 mm.must = isl_map_empty(isl_dim_copy(dim));
1322 mm.may = isl_map_empty(dim);
1324 isl_flow_foreach(flow, collect_must_may, &mm);
1326 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1327 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1328 assert(map_is_equal(mm.may, str));
1329 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1330 assert(map_is_equal(mm.must, str));
1332 isl_map_free(mm.must);
1333 isl_map_free(mm.may);
1334 isl_flow_free(flow);
1337 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1338 map = isl_map_read_from_str(ctx, str, -1);
1339 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1341 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1342 map = isl_map_read_from_str(ctx, str, -1);
1343 ai = isl_access_info_add_source(ai, map, 0, &depth);
1345 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1346 map = isl_map_read_from_str(ctx, str, -1);
1347 ai = isl_access_info_add_source(ai, map, 0, &depth);
1349 flow = isl_access_info_compute_flow(ai);
1350 dim = isl_dim_alloc(ctx, 0, 3, 3);
1351 mm.must = isl_map_empty(isl_dim_copy(dim));
1352 mm.may = isl_map_empty(dim);
1354 isl_flow_foreach(flow, collect_must_may, &mm);
1356 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1357 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1358 assert(map_is_equal(mm.may, str));
1359 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1360 assert(map_is_equal(mm.must, str));
1362 isl_map_free(mm.must);
1363 isl_map_free(mm.may);
1364 isl_flow_free(flow);
1367 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1368 map = isl_map_read_from_str(ctx, str, -1);
1369 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1371 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1372 map = isl_map_read_from_str(ctx, str, -1);
1373 ai = isl_access_info_add_source(ai, map, 0, &depth);
1375 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1376 map = isl_map_read_from_str(ctx, str, -1);
1377 ai = isl_access_info_add_source(ai, map, 0, &depth);
1379 flow = isl_access_info_compute_flow(ai);
1380 dim = isl_dim_alloc(ctx, 0, 3, 3);
1381 mm.must = isl_map_empty(isl_dim_copy(dim));
1382 mm.may = isl_map_empty(dim);
1384 isl_flow_foreach(flow, collect_must_may, &mm);
1386 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1387 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1388 assert(map_is_equal(mm.may, str));
1389 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1390 assert(map_is_equal(mm.must, str));
1392 isl_map_free(mm.must);
1393 isl_map_free(mm.may);
1394 isl_flow_free(flow);
1399 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1400 map = isl_map_read_from_str(ctx, str, -1);
1401 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1403 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1404 map = isl_map_read_from_str(ctx, str, -1);
1405 ai = isl_access_info_add_source(ai, map, 1, &depth);
1407 flow = isl_access_info_compute_flow(ai);
1408 dim = isl_dim_alloc(ctx, 0, 5, 5);
1409 mm.must = isl_map_empty(isl_dim_copy(dim));
1410 mm.may = isl_map_empty(dim);
1412 isl_flow_foreach(flow, collect_must_may, &mm);
1414 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1415 assert(map_is_equal(mm.must, str));
1416 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1417 assert(map_is_equal(mm.may, str));
1419 isl_map_free(mm.must);
1420 isl_map_free(mm.may);
1421 isl_flow_free(flow);
1424 void test_sv(struct isl_ctx *ctx)
1429 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1430 map = isl_map_read_from_str(ctx, str, -1);
1431 assert(isl_map_is_single_valued(map));
1434 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1435 map = isl_map_read_from_str(ctx, str, -1);
1436 assert(!isl_map_is_single_valued(map));
1440 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1444 map = isl_map_read_from_str(ctx, str, -1);
1446 assert(isl_map_is_bijective(map));
1448 assert(!isl_map_is_bijective(map));
1452 void test_bijective(struct isl_ctx *ctx)
1454 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1455 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1456 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1457 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1458 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1459 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1460 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1461 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1462 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1463 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1464 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1465 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1466 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1469 void test_pwqp(struct isl_ctx *ctx)
1473 isl_pw_qpolynomial *pwqp1, *pwqp2;
1475 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1476 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1478 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1481 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1482 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1484 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1486 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1488 isl_pw_qpolynomial_free(pwqp1);
1490 str = "{ [i] -> i }";
1491 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1492 str = "{ [k] : exists a : k = 2a }";
1493 set = isl_set_read_from_str(ctx, str, 0);
1494 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1495 str = "{ [i] -> i }";
1496 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1498 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1500 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1502 isl_pw_qpolynomial_free(pwqp1);
1504 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1505 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1507 set = isl_set_read_from_str(ctx, str, 0);
1508 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1509 str = "{ [i] -> 16 }";
1510 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1512 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1514 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1516 isl_pw_qpolynomial_free(pwqp1);
1518 str = "{ [i] -> ([(i)/2]) }";
1519 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1520 str = "{ [k] : exists a : k = 2a+1 }";
1521 set = isl_set_read_from_str(ctx, str, 0);
1522 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1523 str = "{ [i] -> -1/2 + 1/2 * i }";
1524 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1526 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1528 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1530 isl_pw_qpolynomial_free(pwqp1);
1532 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1533 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1534 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1535 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1537 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1539 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1541 isl_pw_qpolynomial_free(pwqp1);
1543 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1544 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1545 str = "{ [x] -> x }";
1546 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1548 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1550 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1552 isl_pw_qpolynomial_free(pwqp1);
1555 void test_split_periods(isl_ctx *ctx)
1558 isl_pw_qpolynomial *pwqp;
1560 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1561 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1562 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1563 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1565 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1568 isl_pw_qpolynomial_free(pwqp);
1571 void test_union(isl_ctx *ctx)
1574 isl_union_set *uset1, *uset2;
1575 isl_union_map *umap1, *umap2;
1577 str = "{ [i] : 0 <= i <= 1 }";
1578 uset1 = isl_union_set_read_from_str(ctx, str);
1579 str = "{ [1] -> [0] }";
1580 umap1 = isl_union_map_read_from_str(ctx, str);
1582 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1583 assert(isl_union_map_is_equal(umap1, umap2));
1585 isl_union_map_free(umap1);
1586 isl_union_map_free(umap2);
1588 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1589 umap1 = isl_union_map_read_from_str(ctx, str);
1590 str = "{ A[i]; B[i] }";
1591 uset1 = isl_union_set_read_from_str(ctx, str);
1593 uset2 = isl_union_map_domain(umap1);
1595 assert(isl_union_set_is_equal(uset1, uset2));
1597 isl_union_set_free(uset1);
1598 isl_union_set_free(uset2);
1601 void test_bound(isl_ctx *ctx)
1604 isl_pw_qpolynomial *pwqp;
1605 isl_pw_qpolynomial_fold *pwf;
1607 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1608 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1609 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1610 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1611 isl_pw_qpolynomial_fold_free(pwf);
1614 void test_lift(isl_ctx *ctx)
1617 isl_basic_map *bmap;
1618 isl_basic_set *bset;
1620 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1621 bset = isl_basic_set_read_from_str(ctx, str, 0);
1622 bset = isl_basic_set_lift(bset);
1623 bmap = isl_basic_map_from_range(bset);
1624 bset = isl_basic_map_domain(bmap);
1625 isl_basic_set_free(bset);
1628 void test_subset(isl_ctx *ctx)
1631 isl_set *set1, *set2;
1633 str = "{ [112, 0] }";
1634 set1 = isl_set_read_from_str(ctx, str, 0);
1635 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1636 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1637 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1638 set2 = isl_set_read_from_str(ctx, str, 0);
1639 assert(isl_set_is_subset(set1, set2));
1646 struct isl_ctx *ctx;
1648 srcdir = getenv("srcdir");
1651 ctx = isl_ctx_alloc();
1656 test_split_periods(ctx);
1661 test_bijective(ctx);
1665 test_construction(ctx);
1668 test_application(ctx);
1669 test_affine_hull(ctx);
1670 test_convex_hull(ctx);