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 test_parse_pwqp(ctx, "{ [i] -> i + [ (i + [i/3])/2 ] }");
72 void test_read(struct isl_ctx *ctx)
74 char filename[PATH_MAX];
77 struct isl_basic_set *bset1, *bset2;
78 const char *str = "{[y]: Exists ( alpha : 2alpha = y)}";
80 n = snprintf(filename, sizeof(filename),
81 "%s/test_inputs/set.omega", srcdir);
82 assert(n < sizeof(filename));
83 input = fopen(filename, "r");
86 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
87 bset2 = isl_basic_set_read_from_str(ctx, str, 0);
89 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
91 isl_basic_set_free(bset1);
92 isl_basic_set_free(bset2);
97 void test_bounded(struct isl_ctx *ctx)
102 set = isl_set_read_from_str(ctx, "[n] -> {[i] : 0 <= i <= n }", -1);
103 bounded = isl_set_is_bounded(set);
107 set = isl_set_read_from_str(ctx, "{[n, i] : 0 <= i <= n }", -1);
108 bounded = isl_set_is_bounded(set);
112 set = isl_set_read_from_str(ctx, "[n] -> {[i] : i <= n }", -1);
113 bounded = isl_set_is_bounded(set);
118 /* Construct the basic set { [i] : 5 <= i <= N } */
119 void test_construction(struct isl_ctx *ctx)
123 struct isl_basic_set *bset;
124 struct isl_constraint *c;
128 dim = isl_dim_set_alloc(ctx, 1, 1);
129 bset = isl_basic_set_universe(dim);
131 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
132 isl_int_set_si(v, -1);
133 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
134 isl_int_set_si(v, 1);
135 isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
136 bset = isl_basic_set_add_constraint(bset, c);
138 c = isl_inequality_alloc(isl_basic_set_get_dim(bset));
139 isl_int_set_si(v, 1);
140 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
141 isl_int_set_si(v, -5);
142 isl_constraint_set_constant(c, v);
143 bset = isl_basic_set_add_constraint(bset, c);
145 isl_basic_set_free(bset);
150 void test_dim(struct isl_ctx *ctx)
153 isl_map *map1, *map2;
155 map1 = isl_map_read_from_str(ctx,
156 "[n] -> { [i] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
157 map1 = isl_map_add_dims(map1, isl_dim_in, 1);
158 map2 = isl_map_read_from_str(ctx,
159 "[n] -> { [i,k] -> [j] : exists (a = [i/10] : i - 10a <= n ) }", -1);
160 assert(isl_map_is_equal(map1, map2));
163 map1 = isl_map_project_out(map1, isl_dim_in, 0, 1);
164 map2 = isl_map_read_from_str(ctx, "[n] -> { [i] -> [j] : n >= 0 }", -1);
165 assert(isl_map_is_equal(map1, map2));
170 str = "[n] -> { [i] -> [] : exists a : 0 <= i <= n and i = 2 a }";
171 map1 = isl_map_read_from_str(ctx, str, -1);
172 str = "{ [i] -> [j] : exists a : 0 <= i <= j and i = 2 a }";
173 map2 = isl_map_read_from_str(ctx, str, -1);
174 map1 = isl_map_move_dims(map1, isl_dim_out, 0, isl_dim_param, 0, 1);
175 assert(isl_map_is_equal(map1, map2));
181 void test_div(struct isl_ctx *ctx)
187 struct isl_basic_set *bset;
188 struct isl_constraint *c;
193 dim = isl_dim_set_alloc(ctx, 0, 1);
194 bset = isl_basic_set_universe(dim);
196 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
197 isl_int_set_si(v, -1);
198 isl_constraint_set_constant(c, v);
199 isl_int_set_si(v, 1);
200 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
201 div = isl_div_alloc(isl_basic_set_get_dim(bset));
202 c = isl_constraint_add_div(c, div, &pos);
203 isl_int_set_si(v, 3);
204 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
205 bset = isl_basic_set_add_constraint(bset, c);
207 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
208 isl_int_set_si(v, 1);
209 isl_constraint_set_constant(c, v);
210 isl_int_set_si(v, -1);
211 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
212 div = isl_div_alloc(isl_basic_set_get_dim(bset));
213 c = isl_constraint_add_div(c, div, &pos);
214 isl_int_set_si(v, 3);
215 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
216 bset = isl_basic_set_add_constraint(bset, c);
218 assert(bset && bset->n_div == 1);
219 isl_basic_set_free(bset);
222 dim = isl_dim_set_alloc(ctx, 0, 1);
223 bset = isl_basic_set_universe(dim);
225 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
226 isl_int_set_si(v, 1);
227 isl_constraint_set_constant(c, v);
228 isl_int_set_si(v, -1);
229 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
230 div = isl_div_alloc(isl_basic_set_get_dim(bset));
231 c = isl_constraint_add_div(c, div, &pos);
232 isl_int_set_si(v, 3);
233 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
234 bset = isl_basic_set_add_constraint(bset, c);
236 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
237 isl_int_set_si(v, -1);
238 isl_constraint_set_constant(c, v);
239 isl_int_set_si(v, 1);
240 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
241 div = isl_div_alloc(isl_basic_set_get_dim(bset));
242 c = isl_constraint_add_div(c, div, &pos);
243 isl_int_set_si(v, 3);
244 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
245 bset = isl_basic_set_add_constraint(bset, c);
247 assert(bset && bset->n_div == 1);
248 isl_basic_set_free(bset);
251 dim = isl_dim_set_alloc(ctx, 0, 1);
252 bset = isl_basic_set_universe(dim);
254 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
255 isl_int_set_si(v, 1);
256 isl_constraint_set_constant(c, v);
257 isl_int_set_si(v, -1);
258 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
259 div = isl_div_alloc(isl_basic_set_get_dim(bset));
260 c = isl_constraint_add_div(c, div, &pos);
261 isl_int_set_si(v, 3);
262 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
263 bset = isl_basic_set_add_constraint(bset, c);
265 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
266 isl_int_set_si(v, -3);
267 isl_constraint_set_constant(c, v);
268 isl_int_set_si(v, 1);
269 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
270 div = isl_div_alloc(isl_basic_set_get_dim(bset));
271 c = isl_constraint_add_div(c, div, &pos);
272 isl_int_set_si(v, 4);
273 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
274 bset = isl_basic_set_add_constraint(bset, c);
276 assert(bset && bset->n_div == 1);
277 isl_basic_set_free(bset);
280 dim = isl_dim_set_alloc(ctx, 0, 1);
281 bset = isl_basic_set_universe(dim);
283 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
284 isl_int_set_si(v, 2);
285 isl_constraint_set_constant(c, v);
286 isl_int_set_si(v, -1);
287 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
288 div = isl_div_alloc(isl_basic_set_get_dim(bset));
289 c = isl_constraint_add_div(c, div, &pos);
290 isl_int_set_si(v, 3);
291 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
292 bset = isl_basic_set_add_constraint(bset, c);
294 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
295 isl_int_set_si(v, -1);
296 isl_constraint_set_constant(c, v);
297 isl_int_set_si(v, 1);
298 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
299 div = isl_div_alloc(isl_basic_set_get_dim(bset));
300 c = isl_constraint_add_div(c, div, &pos);
301 isl_int_set_si(v, 6);
302 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
303 bset = isl_basic_set_add_constraint(bset, c);
305 assert(isl_basic_set_is_empty(bset));
306 isl_basic_set_free(bset);
309 dim = isl_dim_set_alloc(ctx, 0, 2);
310 bset = isl_basic_set_universe(dim);
312 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
313 isl_int_set_si(v, -1);
314 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
315 div = isl_div_alloc(isl_basic_set_get_dim(bset));
316 c = isl_constraint_add_div(c, div, &pos);
317 isl_int_set_si(v, 3);
318 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
319 bset = isl_basic_set_add_constraint(bset, c);
321 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
322 isl_int_set_si(v, 1);
323 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
324 isl_int_set_si(v, -3);
325 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
326 bset = isl_basic_set_add_constraint(bset, c);
328 assert(bset && bset->n_div == 0);
329 isl_basic_set_free(bset);
332 dim = isl_dim_set_alloc(ctx, 0, 2);
333 bset = isl_basic_set_universe(dim);
335 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
336 isl_int_set_si(v, -1);
337 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
338 div = isl_div_alloc(isl_basic_set_get_dim(bset));
339 c = isl_constraint_add_div(c, div, &pos);
340 isl_int_set_si(v, 6);
341 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
342 bset = isl_basic_set_add_constraint(bset, c);
344 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
345 isl_int_set_si(v, 1);
346 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
347 isl_int_set_si(v, -3);
348 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
349 bset = isl_basic_set_add_constraint(bset, c);
351 assert(bset && bset->n_div == 1);
352 isl_basic_set_free(bset);
355 /* This test is a bit tricky. We set up an equality
357 * Normalization of divs creates _two_ divs
360 * Afterwards e0 is removed again because it has coefficient -1
361 * and we end up with the original equality and div again.
362 * Perhaps we can avoid the introduction of this temporary div.
364 dim = isl_dim_set_alloc(ctx, 0, 3);
365 bset = isl_basic_set_universe(dim);
367 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
368 isl_int_set_si(v, -1);
369 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
370 isl_int_set_si(v, -3);
371 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
372 isl_int_set_si(v, -3);
373 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
374 div = isl_div_alloc(isl_basic_set_get_dim(bset));
375 c = isl_constraint_add_div(c, div, &pos);
376 isl_int_set_si(v, 6);
377 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
378 bset = isl_basic_set_add_constraint(bset, c);
380 /* Test disabled for now */
382 assert(bset && bset->n_div == 1);
384 isl_basic_set_free(bset);
387 dim = isl_dim_set_alloc(ctx, 0, 4);
388 bset = isl_basic_set_universe(dim);
390 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
391 isl_int_set_si(v, -1);
392 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
393 isl_int_set_si(v, -3);
394 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
395 isl_int_set_si(v, -3);
396 isl_constraint_set_coefficient(c, isl_dim_set, 3, v);
397 div = isl_div_alloc(isl_basic_set_get_dim(bset));
398 c = isl_constraint_add_div(c, div, &pos);
399 isl_int_set_si(v, 6);
400 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
401 bset = isl_basic_set_add_constraint(bset, c);
403 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
404 isl_int_set_si(v, -1);
405 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
406 isl_int_set_si(v, 1);
407 isl_constraint_set_coefficient(c, isl_dim_set, 2, v);
408 isl_int_set_si(v, 1);
409 isl_constraint_set_constant(c, v);
410 bset = isl_basic_set_add_constraint(bset, c);
412 /* Test disabled for now */
414 assert(bset && bset->n_div == 1);
416 isl_basic_set_free(bset);
419 dim = isl_dim_set_alloc(ctx, 0, 2);
420 bset = isl_basic_set_universe(dim);
422 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
423 isl_int_set_si(v, 1);
424 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
425 isl_int_set_si(v, -1);
426 isl_constraint_set_coefficient(c, isl_dim_set, 1, v);
427 div = isl_div_alloc(isl_basic_set_get_dim(bset));
428 c = isl_constraint_add_div(c, div, &pos);
429 isl_int_set_si(v, -2);
430 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
431 bset = isl_basic_set_add_constraint(bset, c);
433 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
434 isl_int_set_si(v, -1);
435 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
436 div = isl_div_alloc(isl_basic_set_get_dim(bset));
437 c = isl_constraint_add_div(c, div, &pos);
438 isl_int_set_si(v, 3);
439 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
440 isl_int_set_si(v, 2);
441 isl_constraint_set_constant(c, v);
442 bset = isl_basic_set_add_constraint(bset, c);
444 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
446 assert(!isl_basic_set_is_empty(bset));
448 isl_basic_set_free(bset);
451 dim = isl_dim_set_alloc(ctx, 0, 2);
452 bset = isl_basic_set_universe(dim);
454 c = isl_equality_alloc(isl_basic_set_get_dim(bset));
455 isl_int_set_si(v, 1);
456 isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
457 div = isl_div_alloc(isl_basic_set_get_dim(bset));
458 c = isl_constraint_add_div(c, div, &pos);
459 isl_int_set_si(v, -2);
460 isl_constraint_set_coefficient(c, isl_dim_div, pos, v);
461 bset = isl_basic_set_add_constraint(bset, c);
463 bset = isl_basic_set_fix_si(bset, isl_dim_set, 0, 2);
465 isl_basic_set_free(bset);
470 void test_application_case(struct isl_ctx *ctx, const char *name)
472 char filename[PATH_MAX];
475 struct isl_basic_set *bset1, *bset2;
476 struct isl_basic_map *bmap;
478 n = snprintf(filename, sizeof(filename),
479 "%s/test_inputs/%s.omega", srcdir, name);
480 assert(n < sizeof(filename));
481 input = fopen(filename, "r");
484 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
485 bmap = isl_basic_map_read_from_file(ctx, input, 0);
487 bset1 = isl_basic_set_apply(bset1, bmap);
489 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
491 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
493 isl_basic_set_free(bset1);
494 isl_basic_set_free(bset2);
499 void test_application(struct isl_ctx *ctx)
501 test_application_case(ctx, "application");
502 test_application_case(ctx, "application2");
505 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
507 char filename[PATH_MAX];
510 struct isl_basic_set *bset1, *bset2;
512 n = snprintf(filename, sizeof(filename),
513 "%s/test_inputs/%s.polylib", srcdir, name);
514 assert(n < sizeof(filename));
515 input = fopen(filename, "r");
518 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
519 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
521 bset1 = isl_basic_set_affine_hull(bset1);
523 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
525 isl_basic_set_free(bset1);
526 isl_basic_set_free(bset2);
531 void test_affine_hull(struct isl_ctx *ctx)
533 test_affine_hull_case(ctx, "affine2");
534 test_affine_hull_case(ctx, "affine");
535 test_affine_hull_case(ctx, "affine3");
538 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
540 char filename[PATH_MAX];
543 struct isl_basic_set *bset1, *bset2;
546 n = snprintf(filename, sizeof(filename),
547 "%s/test_inputs/%s.polylib", srcdir, name);
548 assert(n < sizeof(filename));
549 input = fopen(filename, "r");
552 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
553 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
555 set = isl_basic_set_union(bset1, bset2);
556 bset1 = isl_set_convex_hull(set);
558 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
560 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
562 isl_basic_set_free(bset1);
563 isl_basic_set_free(bset2);
568 void test_convex_hull(struct isl_ctx *ctx)
570 const char *str1, *str2;
571 isl_set *set1, *set2;
573 test_convex_hull_case(ctx, "convex0");
574 test_convex_hull_case(ctx, "convex1");
575 test_convex_hull_case(ctx, "convex2");
576 test_convex_hull_case(ctx, "convex3");
577 test_convex_hull_case(ctx, "convex4");
578 test_convex_hull_case(ctx, "convex5");
579 test_convex_hull_case(ctx, "convex6");
580 test_convex_hull_case(ctx, "convex7");
581 test_convex_hull_case(ctx, "convex8");
582 test_convex_hull_case(ctx, "convex9");
583 test_convex_hull_case(ctx, "convex10");
584 test_convex_hull_case(ctx, "convex11");
585 test_convex_hull_case(ctx, "convex12");
586 test_convex_hull_case(ctx, "convex13");
587 test_convex_hull_case(ctx, "convex14");
588 test_convex_hull_case(ctx, "convex15");
590 str1 = "{ [i0, i1, i2] : (i2 = 1 and i0 = 0 and i1 >= 0) or "
591 "(i0 = 1 and i1 = 0 and i2 = 1) or "
592 "(i0 = 0 and i1 = 0 and i2 = 0) }";
593 str2 = "{ [i0, i1, i2] : i0 >= 0 and i2 >= i0 and i2 <= 1 and i1 >= 0 }";
594 set1 = isl_set_read_from_str(ctx, str1, -1);
595 set2 = isl_set_read_from_str(ctx, str2, -1);
596 set1 = isl_set_from_basic_set(isl_set_convex_hull(set1));
597 assert(isl_set_is_equal(set1, set2));
602 void test_gist_case(struct isl_ctx *ctx, const char *name)
604 char filename[PATH_MAX];
607 struct isl_basic_set *bset1, *bset2;
609 n = snprintf(filename, sizeof(filename),
610 "%s/test_inputs/%s.polylib", srcdir, name);
611 assert(n < sizeof(filename));
612 input = fopen(filename, "r");
615 bset1 = isl_basic_set_read_from_file(ctx, input, 0);
616 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
618 bset1 = isl_basic_set_gist(bset1, bset2);
620 bset2 = isl_basic_set_read_from_file(ctx, input, 0);
622 assert(isl_basic_set_is_equal(bset1, bset2) == 1);
624 isl_basic_set_free(bset1);
625 isl_basic_set_free(bset2);
630 void test_gist(struct isl_ctx *ctx)
632 test_gist_case(ctx, "gist1");
635 void test_coalesce_set(isl_ctx *ctx, const char *str, int check_one)
639 set = isl_set_read_from_str(ctx, str, -1);
640 set = isl_set_coalesce(set);
641 set2 = isl_set_read_from_str(ctx, str, -1);
642 assert(isl_set_is_equal(set, set2));
644 assert(set && set->n == 1);
649 void test_coalesce(struct isl_ctx *ctx)
652 struct isl_set *set, *set2;
653 struct isl_map *map, *map2;
655 set = isl_set_read_from_str(ctx,
656 "{[x,y]: x >= 0 & x <= 10 & y >= 0 & y <= 10 or "
657 "y >= x & x >= 2 & 5 >= y }", -1);
658 set = isl_set_coalesce(set);
659 assert(set && set->n == 1);
662 set = isl_set_read_from_str(ctx,
663 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
664 "x + y >= 10 & y <= x & x + y <= 20 & y >= 0}", -1);
665 set = isl_set_coalesce(set);
666 assert(set && set->n == 1);
669 set = isl_set_read_from_str(ctx,
670 "{[x,y]: y >= 0 & 2x + y <= 30 & y <= 10 & x >= 0 or "
671 "x + y >= 10 & y <= x & x + y <= 19 & y >= 0}", -1);
672 set = isl_set_coalesce(set);
673 assert(set && set->n == 2);
676 set = isl_set_read_from_str(ctx,
677 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
678 "y >= 0 & x >= 6 & x <= 10 & y <= x}", -1);
679 set = isl_set_coalesce(set);
680 assert(set && set->n == 1);
683 set = isl_set_read_from_str(ctx,
684 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
685 "y >= 0 & x >= 7 & x <= 10 & y <= x}", -1);
686 set = isl_set_coalesce(set);
687 assert(set && set->n == 2);
690 set = isl_set_read_from_str(ctx,
691 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
692 "y >= 0 & x >= 6 & x <= 10 & y + 1 <= x}", -1);
693 set = isl_set_coalesce(set);
694 assert(set && set->n == 2);
697 set = isl_set_read_from_str(ctx,
698 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
699 "y >= 0 & x = 6 & y <= 6}", -1);
700 set = isl_set_coalesce(set);
701 assert(set && set->n == 1);
704 set = isl_set_read_from_str(ctx,
705 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
706 "y >= 0 & x = 7 & y <= 6}", -1);
707 set = isl_set_coalesce(set);
708 assert(set && set->n == 2);
711 set = isl_set_read_from_str(ctx,
712 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
713 "y >= 0 & x = 6 & y <= 5}", -1);
714 set = isl_set_coalesce(set);
715 assert(set && set->n == 1);
716 set2 = isl_set_read_from_str(ctx,
717 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
718 "y >= 0 & x = 6 & y <= 5}", -1);
719 assert(isl_set_is_equal(set, set2));
723 set = isl_set_read_from_str(ctx,
724 "{[x,y]: y >= 0 & x <= 5 & y <= x or "
725 "y >= 0 & x = 6 & y <= 7}", -1);
726 set = isl_set_coalesce(set);
727 assert(set && set->n == 2);
730 set = isl_set_read_from_str(ctx,
731 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
732 set = isl_set_coalesce(set);
733 assert(set && set->n == 1);
734 set2 = isl_set_read_from_str(ctx,
735 "[n] -> { [i] : i = 1 and n >= 2 or 2 <= i and i <= n }", -1);
736 assert(isl_set_is_equal(set, set2));
740 set = isl_set_read_from_str(ctx,
741 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
742 set = isl_set_coalesce(set);
743 set2 = isl_set_read_from_str(ctx,
744 "{[x,y] : x >= 0 and y >= 0 or 0 <= y and y <= 5 and x = -1}", -1);
745 assert(isl_set_is_equal(set, set2));
749 set = isl_set_read_from_str(ctx,
750 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
751 "2 <= i and i <= n }", -1);
752 set = isl_set_coalesce(set);
753 assert(set && set->n == 1);
754 set2 = isl_set_read_from_str(ctx,
755 "[n] -> { [i] : 1 <= i and i <= n - 1 or "
756 "2 <= i and i <= n }", -1);
757 assert(isl_set_is_equal(set, set2));
761 map = isl_map_read_from_str(ctx,
762 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
763 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
764 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
765 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
766 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
767 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
768 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
769 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
770 "[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 2e0 >= 3 + n and e0 <= -4 + n and "
774 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
775 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
776 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
777 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
778 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
779 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
780 map = isl_map_coalesce(map);
781 map2 = isl_map_read_from_str(ctx,
782 "[n] -> { [i0] -> [o0] : exists (e0 = [(i0)/4], e1 = [(o0)/4], "
783 "e2 = [(n)/2], e3 = [(-2 + i0)/4], e4 = [(-2 + o0)/4], "
784 "e5 = [(-2n + i0)/4]: 2e2 = n and 4e3 = -2 + i0 and "
785 "4e4 = -2 + o0 and i0 >= 8 + 2n and o0 >= 2 + i0 and "
786 "o0 <= 56 + 2n and o0 <= -12 + 4n and i0 <= 57 + 2n and "
787 "i0 <= -11 + 4n and o0 >= 6 + 2n and 4e0 <= i0 and "
788 "4e0 >= -3 + i0 and 4e1 <= o0 and 4e1 >= -3 + o0 and "
789 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0);"
790 "[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 2e0 >= 3 + n and e0 <= -4 + n and "
794 "2e0 <= 27 + n and e1 <= -4 + n and 2e1 <= 27 + n and "
795 "2e1 >= 2 + n and e1 >= 1 + e0 and i0 >= 7 + 2n and "
796 "i0 <= -11 + 4n and i0 <= 57 + 2n and 4e0 <= -2 + i0 and "
797 "4e0 >= -3 + i0 and o0 >= 6 + 2n and o0 <= -11 + 4n and "
798 "o0 <= 57 + 2n and 4e1 <= -2 + o0 and 4e1 >= -3 + o0 and "
799 "4e5 <= -2n + i0 and 4e5 >= -3 - 2n + i0 ) }", -1);
800 assert(isl_map_is_equal(map, map2));
804 str = "[n, m] -> { [] -> [o0, o2, o3] : (o3 = 1 and o0 >= 1 + m and "
805 "o0 <= n + m and o2 <= m and o0 >= 2 + n and o2 >= 3) or "
806 "(o0 >= 2 + n and o0 >= 1 + m and o0 <= n + m and n >= 1 and "
807 "o3 <= -1 + o2 and o3 >= 1 - m + o2 and o3 >= 2 and o3 <= n) }";
808 map = isl_map_read_from_str(ctx, str, -1);
809 map = isl_map_coalesce(map);
810 map2 = isl_map_read_from_str(ctx, str, -1);
811 assert(isl_map_is_equal(map, map2));
815 str = "[M, N] -> { [i0, i1, i2, i3, i4, i5, i6] -> "
816 "[o0, o1, o2, o3, o4, o5, o6] : "
817 "(o6 <= -4 + 2M - 2N + i0 + i1 - i2 + i6 - o0 - o1 + o2 and "
818 "o3 <= -2 + i3 and o6 >= 2 + i0 + i3 + i6 - o0 - o3 and "
819 "o6 >= 2 - M + N + i3 + i4 + i6 - o3 - o4 and o0 <= -1 + i0 and "
820 "o4 >= 4 - 3M + 3N - i0 - i1 + i2 + 2i3 + i4 + o0 + o1 - o2 - 2o3 "
821 "and o6 <= -3 + 2M - 2N + i3 + i4 - i5 + i6 - o3 - o4 + o5 and "
822 "2o6 <= -5 + 5M - 5N + 2i0 + i1 - i2 - i5 + 2i6 - 2o0 - o1 + o2 + o5 "
823 "and o6 >= 2i0 + i1 + i6 - 2o0 - o1 and "
824 "3o6 <= -5 + 4M - 4N + 2i0 + i1 - i2 + 2i3 + i4 - i5 + 3i6 "
825 "- 2o0 - o1 + o2 - 2o3 - o4 + o5) or "
826 "(N >= 2 and o3 <= -1 + i3 and o0 <= -1 + i0 and "
827 "o6 >= i3 + i6 - o3 and M >= 0 and "
828 "2o6 >= 1 + i0 + i3 + 2i6 - o0 - o3 and "
829 "o6 >= 1 - M + i0 + i6 - o0 and N >= 2M and o6 >= i0 + i6 - o0) }";
830 map = isl_map_read_from_str(ctx, str, -1);
831 map = isl_map_coalesce(map);
832 map2 = isl_map_read_from_str(ctx, str, -1);
833 assert(isl_map_is_equal(map, map2));
837 str = "[M, N] -> { [] -> [o0] : (o0 = 0 and M >= 1 and N >= 2) or "
838 "(o0 = 0 and M >= 1 and N >= 2M and N >= 2 + M) or "
839 "(o0 = 0 and M >= 2 and N >= 3) or "
840 "(M = 0 and o0 = 0 and N >= 3) }";
841 map = isl_map_read_from_str(ctx, str, -1);
842 map = isl_map_coalesce(map);
843 map2 = isl_map_read_from_str(ctx, str, -1);
844 assert(isl_map_is_equal(map, map2));
848 str = "{ [i0, i1, i2, i3] : (i1 = 10i0 and i0 >= 1 and 10i0 <= 100 and "
849 "i3 <= 9 + 10 i2 and i3 >= 1 + 10i2 and i3 >= 0) or "
850 "(i1 <= 9 + 10i0 and i1 >= 1 + 10i0 and i2 >= 0 and "
851 "i0 >= 0 and i1 <= 100 and i3 <= 9 + 10i2 and i3 >= 1 + 10i2) }";
852 map = isl_map_read_from_str(ctx, str, -1);
853 map = isl_map_coalesce(map);
854 map2 = isl_map_read_from_str(ctx, str, -1);
855 assert(isl_map_is_equal(map, map2));
859 test_coalesce_set(ctx,
860 "[M] -> { [i1] : (i1 >= 2 and i1 <= M) or "
861 "(i1 = M and M >= 1) }", 0);
862 test_coalesce_set(ctx,
863 "{[x,y] : x,y >= 0; [x,y] : 10 <= x <= 20 and y >= -1 }", 0);
864 test_coalesce_set(ctx,
865 "{ [x, y] : (x >= 1 and y >= 1 and x <= 2 and y <= 2) or "
866 "(y = 3 and x = 1) }", 1);
867 test_coalesce_set(ctx,
868 "[M] -> { [i0, i1, i2, i3, i4] : (i1 >= 3 and i4 >= 2 + i2 and "
869 "i2 >= 2 and i0 >= 2 and i3 >= 1 + i2 and i0 <= M and "
870 "i1 <= M and i3 <= M and i4 <= M) or "
871 "(i1 >= 2 and i4 >= 1 + i2 and i2 >= 2 and i0 >= 2 and "
872 "i3 >= 1 + i2 and i0 <= M and i1 <= -1 + M and i3 <= M and "
873 "i4 <= -1 + M) }", 1);
874 test_coalesce_set(ctx,
875 "{ [x, y] : (x >= 0 and y >= 0 and x <= 10 and y <= 10) or "
876 "(x >= 1 and y >= 1 and x <= 11 and y <= 11) }", 1);
877 test_coalesce_set(ctx,
878 "{[x,y,z] : y + 2 >= 0 and x - y + 1 >= 0 and "
879 "-x - y + 1 >= 0 and -3 <= z <= 3;"
880 "[x,y,z] : -x+z + 20 >= 0 and -x-z + 20 >= 0 and "
881 "x-z + 20 >= 0 and x+z + 20 >= 0 and -10 <= y <= 0}", 1);
882 test_coalesce_set(ctx,
883 "{[x,y] : 0 <= x,y <= 10; [5,y]: 4 <=y <= 11}", 1);
884 test_coalesce_set(ctx, "{[x,0] : x >= 0; [x,1] : x <= 20}", 0);
885 test_coalesce_set(ctx,
886 "{[x,0,0] : -5 <= x <= 5; [0,y,1] : -5 <= y <= 5 }", 1);
887 test_coalesce_set(ctx, "{ [x, 1 - x] : 0 <= x <= 1; [0,0] }", 1);
890 void test_closure(struct isl_ctx *ctx)
898 /* COCOA example 1 */
899 map = isl_map_read_from_str(ctx,
900 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
901 "1 <= i and i < n and 1 <= j and j < n or "
902 "i2 = i + 1 and j2 = j - 1 and "
903 "1 <= i and i < n and 2 <= j and j <= n }", -1);
904 map = isl_map_power(map, 1, &exact);
908 /* COCOA example 1 */
909 map = isl_map_read_from_str(ctx,
910 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 1 and j2 = j + 1 and "
911 "1 <= i and i < n and 1 <= j and j < n or "
912 "i2 = i + 1 and j2 = j - 1 and "
913 "1 <= i and i < n and 2 <= j and j <= n }", -1);
914 map = isl_map_transitive_closure(map, &exact);
916 map2 = isl_map_read_from_str(ctx,
917 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
918 "1 <= i and i < n and 1 <= j and j <= n and "
919 "2 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
920 "i2 = i + k1 + k2 and j2 = j + k1 - k2 and "
921 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1 )}", -1);
922 assert(isl_map_is_equal(map, map2));
926 map = isl_map_read_from_str(ctx,
927 "[n] -> { [x] -> [y] : y = x + 1 and 0 <= x and x <= n and "
928 " 0 <= y and y <= n }", -1);
929 map = isl_map_transitive_closure(map, &exact);
930 map2 = isl_map_read_from_str(ctx,
931 "[n] -> { [x] -> [y] : y > x and 0 <= x and x <= n and "
932 " 0 <= y and y <= n }", -1);
933 assert(isl_map_is_equal(map, map2));
937 /* COCOA example 2 */
938 map = isl_map_read_from_str(ctx,
939 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j + 2 and "
940 "1 <= i and i < n - 1 and 1 <= j and j < n - 1 or "
941 "i2 = i + 2 and j2 = j - 2 and "
942 "1 <= i and i < n - 1 and 3 <= j and j <= n }", -1);
943 map = isl_map_transitive_closure(map, &exact);
945 map2 = isl_map_read_from_str(ctx,
946 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k : "
947 "1 <= i and i < n - 1 and 1 <= j and j <= n and "
948 "3 <= i2 and i2 <= n and 1 <= j2 and j2 <= n and "
949 "i2 = i + 2 k1 + 2 k2 and j2 = j + 2 k1 - 2 k2 and "
950 "k1 >= 0 and k2 >= 0 and k1 + k2 = k and k >= 1) }", -1);
951 assert(isl_map_is_equal(map, map2));
955 /* COCOA Fig.2 left */
956 map = isl_map_read_from_str(ctx,
957 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 2 and j2 = j and "
958 "i <= 2 j - 3 and i <= n - 2 and j <= 2 i - 1 and "
960 "i2 = i and j2 = j + 2 and i <= 2 j - 1 and i <= n and "
961 "j <= 2 i - 3 and j <= n - 2 or "
962 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
963 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
964 map = isl_map_transitive_closure(map, &exact);
968 /* COCOA Fig.2 right */
969 map = isl_map_read_from_str(ctx,
970 "[n,k] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
971 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
973 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
974 "j <= 2 i - 4 and j <= n - 3 or "
975 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
976 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
977 map = isl_map_power(map, 1, &exact);
981 /* COCOA Fig.2 right */
982 map = isl_map_read_from_str(ctx,
983 "[n] -> { [i,j] -> [i2,j2] : i2 = i + 3 and j2 = j and "
984 "i <= 2 j - 4 and i <= n - 3 and j <= 2 i - 1 and "
986 "i2 = i and j2 = j + 3 and i <= 2 j - 1 and i <= n and "
987 "j <= 2 i - 4 and j <= n - 3 or "
988 "i2 = i + 1 and j2 = j + 1 and i <= 2 j - 1 and "
989 "i <= n - 1 and j <= 2 i - 1 and j <= n - 1 }", -1);
990 map = isl_map_transitive_closure(map, &exact);
992 map2 = isl_map_read_from_str(ctx,
993 "[n] -> { [i,j] -> [i2,j2] : exists (k1,k2,k3,k : "
994 "i <= 2 j - 1 and i <= n and j <= 2 i - 1 and "
995 "j <= n and 3 + i + 2 j <= 3 n and "
996 "3 + 2 i + j <= 3n and i2 <= 2 j2 -1 and i2 <= n and "
997 "i2 <= 3 j2 - 4 and j2 <= 2 i2 -1 and j2 <= n and "
998 "13 + 4 j2 <= 11 i2 and i2 = i + 3 k1 + k3 and "
999 "j2 = j + 3 k2 + k3 and k1 >= 0 and k2 >= 0 and "
1000 "k3 >= 0 and k1 + k2 + k3 = k and k > 0) }", -1);
1001 assert(isl_map_is_equal(map, map2));
1005 /* COCOA Fig.1 right */
1006 dom = isl_set_read_from_str(ctx,
1007 "{ [x,y] : x >= 0 and -2 x + 3 y >= 0 and x <= 3 and "
1008 "2 x - 3 y + 3 >= 0 }", -1);
1009 right = isl_map_read_from_str(ctx,
1010 "{ [x,y] -> [x2,y2] : x2 = x + 1 and y2 = y }", -1);
1011 up = isl_map_read_from_str(ctx,
1012 "{ [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 }", -1);
1013 right = isl_map_intersect_domain(right, isl_set_copy(dom));
1014 right = isl_map_intersect_range(right, isl_set_copy(dom));
1015 up = isl_map_intersect_domain(up, isl_set_copy(dom));
1016 up = isl_map_intersect_range(up, dom);
1017 map = isl_map_union(up, right);
1018 map = isl_map_transitive_closure(map, &exact);
1020 map2 = isl_map_read_from_str(ctx,
1021 "{ [0,0] -> [0,1]; [0,0] -> [1,1]; [0,1] -> [1,1]; "
1022 " [2,2] -> [3,2]; [2,2] -> [3,3]; [3,2] -> [3,3] }", -1);
1023 assert(isl_map_is_equal(map, map2));
1027 /* COCOA Theorem 1 counter example */
1028 map = isl_map_read_from_str(ctx,
1029 "{ [i,j] -> [i2,j2] : i = 0 and 0 <= j and j <= 1 and "
1030 "i2 = 1 and j2 = j or "
1031 "i = 0 and j = 0 and i2 = 0 and j2 = 1 }", -1);
1032 map = isl_map_transitive_closure(map, &exact);
1036 map = isl_map_read_from_str(ctx,
1037 "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
1038 "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
1039 "i2 = i + 1 and 3 <= j2 - j <= 4 and "
1040 "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
1041 map = isl_map_transitive_closure(map, &exact);
1045 /* Kelly et al 1996, fig 12 */
1046 map = isl_map_read_from_str(ctx,
1047 "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
1048 "1 <= i,j,j+1 <= n or "
1049 "j = n and j2 = 1 and i2 = i + 1 and "
1050 "1 <= i,i+1 <= n }", -1);
1051 map = isl_map_transitive_closure(map, &exact);
1053 map2 = isl_map_read_from_str(ctx,
1054 "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
1055 "1 <= i <= n and i = i2 or "
1056 "1 <= i < i2 <= n and 1 <= j <= n and "
1057 "1 <= j2 <= n }", -1);
1058 assert(isl_map_is_equal(map, map2));
1062 /* Omega's closure4 */
1063 map = isl_map_read_from_str(ctx,
1064 "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
1065 "1 <= x,y <= 10 or "
1066 "x2 = x + 1 and y2 = y and "
1067 "1 <= x <= 20 && 5 <= y <= 15 }", -1);
1068 map = isl_map_transitive_closure(map, &exact);
1072 map = isl_map_read_from_str(ctx,
1073 "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
1074 map = isl_map_transitive_closure(map, &exact);
1076 map2 = isl_map_read_from_str(ctx,
1077 "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
1078 assert(isl_map_is_equal(map, map2));
1082 str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
1083 "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
1084 "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
1085 "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
1086 map = isl_map_read_from_str(ctx, str, -1);
1087 map = isl_map_transitive_closure(map, &exact);
1089 map2 = isl_map_read_from_str(ctx, str, -1);
1090 assert(isl_map_is_equal(map, map2));
1094 str = "{[0] -> [1]; [2] -> [3]}";
1095 map = isl_map_read_from_str(ctx, str, -1);
1096 map = isl_map_transitive_closure(map, &exact);
1098 map2 = isl_map_read_from_str(ctx, str, -1);
1099 assert(isl_map_is_equal(map, map2));
1104 void test_lex(struct isl_ctx *ctx)
1109 dim = isl_dim_alloc(ctx, 0, 0, 0);
1110 map = isl_map_lex_le(dim);
1111 assert(!isl_map_is_empty(map));
1115 void test_lexmin(struct isl_ctx *ctx)
1118 isl_map *map, *map2;
1122 str = "[p0, p1] -> { [] -> [] : "
1123 "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
1124 "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
1125 "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
1126 "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
1127 "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
1128 "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
1129 "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
1130 "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
1131 "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
1132 "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
1133 "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
1134 map = isl_map_read_from_str(ctx, str, -1);
1135 map = isl_map_lexmin(map);
1138 str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
1139 "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
1140 set = isl_set_read_from_str(ctx, str, -1);
1141 set = isl_set_lexmax(set);
1142 str = "[C] -> { [obj,a,b,c] : C = 8 }";
1143 set2 = isl_set_read_from_str(ctx, str, -1);
1144 set = isl_set_intersect(set, set2);
1145 assert(!isl_set_is_empty(set));
1148 str = "{ [x] -> [y] : x <= y <= 10; [x] -> [5] : -8 <= x <= 8 }";
1149 map = isl_map_read_from_str(ctx, str, -1);
1150 map = isl_map_lexmin(map);
1151 str = "{ [x] -> [5] : 6 <= x <= 8; "
1152 "[x] -> [x] : x <= 5 or (9 <= x <= 10) }";
1153 map2 = isl_map_read_from_str(ctx, str, -1);
1154 assert(isl_map_is_equal(map, map2));
1158 str = "{ [x] -> [y] : 4y = x or 4y = -1 + x or 4y = -2 + x }";
1159 map = isl_map_read_from_str(ctx, str, -1);
1160 map2 = isl_map_copy(map);
1161 map = isl_map_lexmin(map);
1162 assert(isl_map_is_equal(map, map2));
1166 str = "{ [x] -> [y] : x = 4y; [x] -> [y] : x = 2y }";
1167 map = isl_map_read_from_str(ctx, str, -1);
1168 map = isl_map_lexmin(map);
1169 str = "{ [x] -> [y] : (4y = x and x >= 0) or "
1170 "(exists (e0 = [(x)/4], e1 = [(-2 + x)/4]: 2y = x and "
1171 "4e1 = -2 + x and 4e0 <= -1 + x and 4e0 >= -3 + x)) or "
1172 "(exists (e0 = [(x)/4]: 2y = x and 4e0 = x and x <= -4)) }";
1173 map2 = isl_map_read_from_str(ctx, str, -1);
1174 assert(isl_map_is_equal(map, map2));
1184 static int collect_must_may(__isl_take isl_map *dep, int must,
1185 void *dep_user, void *user)
1187 struct must_may *mm = (struct must_may *)user;
1190 mm->must = isl_map_union(mm->must, dep);
1192 mm->may = isl_map_union(mm->may, dep);
1197 static int common_space(void *first, void *second)
1199 int depth = *(int *)first;
1203 static int map_is_equal(__isl_keep isl_map *map, const char *str)
1211 map2 = isl_map_read_from_str(map->ctx, str, -1);
1212 equal = isl_map_is_equal(map, map2);
1218 void test_dep(struct isl_ctx *ctx)
1223 isl_access_info *ai;
1230 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1231 map = isl_map_read_from_str(ctx, str, -1);
1232 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1234 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1235 map = isl_map_read_from_str(ctx, str, -1);
1236 ai = isl_access_info_add_source(ai, map, 1, &depth);
1238 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1239 map = isl_map_read_from_str(ctx, str, -1);
1240 ai = isl_access_info_add_source(ai, map, 1, &depth);
1242 flow = isl_access_info_compute_flow(ai);
1243 dim = isl_dim_alloc(ctx, 0, 3, 3);
1244 mm.must = isl_map_empty(isl_dim_copy(dim));
1245 mm.may = isl_map_empty(dim);
1247 isl_flow_foreach(flow, collect_must_may, &mm);
1249 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
1250 " [1,10,0] -> [2,5,0] }";
1251 assert(map_is_equal(mm.must, str));
1252 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1253 assert(map_is_equal(mm.may, str));
1255 isl_map_free(mm.must);
1256 isl_map_free(mm.may);
1257 isl_flow_free(flow);
1260 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1261 map = isl_map_read_from_str(ctx, str, -1);
1262 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1264 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1265 map = isl_map_read_from_str(ctx, str, -1);
1266 ai = isl_access_info_add_source(ai, map, 1, &depth);
1268 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1269 map = isl_map_read_from_str(ctx, str, -1);
1270 ai = isl_access_info_add_source(ai, map, 0, &depth);
1272 flow = isl_access_info_compute_flow(ai);
1273 dim = isl_dim_alloc(ctx, 0, 3, 3);
1274 mm.must = isl_map_empty(isl_dim_copy(dim));
1275 mm.may = isl_map_empty(dim);
1277 isl_flow_foreach(flow, collect_must_may, &mm);
1279 str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
1280 assert(map_is_equal(mm.must, str));
1281 str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1282 assert(map_is_equal(mm.may, str));
1284 isl_map_free(mm.must);
1285 isl_map_free(mm.may);
1286 isl_flow_free(flow);
1289 str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
1290 map = isl_map_read_from_str(ctx, str, -1);
1291 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1293 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1294 map = isl_map_read_from_str(ctx, str, -1);
1295 ai = isl_access_info_add_source(ai, map, 0, &depth);
1297 str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
1298 map = isl_map_read_from_str(ctx, str, -1);
1299 ai = isl_access_info_add_source(ai, map, 0, &depth);
1301 flow = isl_access_info_compute_flow(ai);
1302 dim = isl_dim_alloc(ctx, 0, 3, 3);
1303 mm.must = isl_map_empty(isl_dim_copy(dim));
1304 mm.may = isl_map_empty(dim);
1306 isl_flow_foreach(flow, collect_must_may, &mm);
1308 str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
1309 " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
1310 assert(map_is_equal(mm.may, str));
1311 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1312 assert(map_is_equal(mm.must, str));
1314 isl_map_free(mm.must);
1315 isl_map_free(mm.may);
1316 isl_flow_free(flow);
1319 str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
1320 map = isl_map_read_from_str(ctx, str, -1);
1321 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1323 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1324 map = isl_map_read_from_str(ctx, str, -1);
1325 ai = isl_access_info_add_source(ai, map, 0, &depth);
1327 str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
1328 map = isl_map_read_from_str(ctx, str, -1);
1329 ai = isl_access_info_add_source(ai, map, 0, &depth);
1331 flow = isl_access_info_compute_flow(ai);
1332 dim = isl_dim_alloc(ctx, 0, 3, 3);
1333 mm.must = isl_map_empty(isl_dim_copy(dim));
1334 mm.may = isl_map_empty(dim);
1336 isl_flow_foreach(flow, collect_must_may, &mm);
1338 str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
1339 " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
1340 assert(map_is_equal(mm.may, str));
1341 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1342 assert(map_is_equal(mm.must, str));
1344 isl_map_free(mm.must);
1345 isl_map_free(mm.may);
1346 isl_flow_free(flow);
1349 str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
1350 map = isl_map_read_from_str(ctx, str, -1);
1351 ai = isl_access_info_alloc(map, &depth, &common_space, 2);
1353 str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
1354 map = isl_map_read_from_str(ctx, str, -1);
1355 ai = isl_access_info_add_source(ai, map, 0, &depth);
1357 str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
1358 map = isl_map_read_from_str(ctx, str, -1);
1359 ai = isl_access_info_add_source(ai, map, 0, &depth);
1361 flow = isl_access_info_compute_flow(ai);
1362 dim = isl_dim_alloc(ctx, 0, 3, 3);
1363 mm.must = isl_map_empty(isl_dim_copy(dim));
1364 mm.may = isl_map_empty(dim);
1366 isl_flow_foreach(flow, collect_must_may, &mm);
1368 str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
1369 " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
1370 assert(map_is_equal(mm.may, str));
1371 str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
1372 assert(map_is_equal(mm.must, str));
1374 isl_map_free(mm.must);
1375 isl_map_free(mm.may);
1376 isl_flow_free(flow);
1381 str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1382 map = isl_map_read_from_str(ctx, str, -1);
1383 ai = isl_access_info_alloc(map, &depth, &common_space, 1);
1385 str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
1386 map = isl_map_read_from_str(ctx, str, -1);
1387 ai = isl_access_info_add_source(ai, map, 1, &depth);
1389 flow = isl_access_info_compute_flow(ai);
1390 dim = isl_dim_alloc(ctx, 0, 5, 5);
1391 mm.must = isl_map_empty(isl_dim_copy(dim));
1392 mm.may = isl_map_empty(dim);
1394 isl_flow_foreach(flow, collect_must_may, &mm);
1396 str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
1397 assert(map_is_equal(mm.must, str));
1398 str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
1399 assert(map_is_equal(mm.may, str));
1401 isl_map_free(mm.must);
1402 isl_map_free(mm.may);
1403 isl_flow_free(flow);
1406 void test_sv(struct isl_ctx *ctx)
1411 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
1412 map = isl_map_read_from_str(ctx, str, -1);
1413 assert(isl_map_is_single_valued(map));
1416 str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
1417 map = isl_map_read_from_str(ctx, str, -1);
1418 assert(!isl_map_is_single_valued(map));
1422 void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
1426 map = isl_map_read_from_str(ctx, str, -1);
1428 assert(isl_map_is_bijective(map));
1430 assert(!isl_map_is_bijective(map));
1434 void test_bijective(struct isl_ctx *ctx)
1436 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
1437 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
1438 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
1439 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
1440 test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
1441 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
1442 test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
1443 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
1444 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
1445 test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
1446 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
1447 test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
1448 test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
1451 void test_pwqp(struct isl_ctx *ctx)
1455 isl_pw_qpolynomial *pwqp1, *pwqp2;
1457 str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1458 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1460 pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
1463 str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
1464 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1466 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1468 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1470 isl_pw_qpolynomial_free(pwqp1);
1472 str = "{ [i] -> i }";
1473 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1474 str = "{ [k] : exists a : k = 2a }";
1475 set = isl_set_read_from_str(ctx, str, 0);
1476 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1477 str = "{ [i] -> i }";
1478 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1480 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1482 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1484 isl_pw_qpolynomial_free(pwqp1);
1486 str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
1487 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1489 set = isl_set_read_from_str(ctx, str, 0);
1490 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1491 str = "{ [i] -> 16 }";
1492 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1494 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1496 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1498 isl_pw_qpolynomial_free(pwqp1);
1500 str = "{ [i] -> ([(i)/2]) }";
1501 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1502 str = "{ [k] : exists a : k = 2a+1 }";
1503 set = isl_set_read_from_str(ctx, str, 0);
1504 pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
1505 str = "{ [i] -> -1/2 + 1/2 * i }";
1506 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1508 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1510 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1512 isl_pw_qpolynomial_free(pwqp1);
1514 str = "{ [i] -> ([([i/2] + [i/2])/5]) }";
1515 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1516 str = "{ [i] -> ([(2 * [i/2])/5]) }";
1517 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1519 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1521 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1523 isl_pw_qpolynomial_free(pwqp1);
1525 str = "{ [x] -> ([x/2] + [(x+1)/2]) }";
1526 pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
1527 str = "{ [x] -> x }";
1528 pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
1530 pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
1532 assert(isl_pw_qpolynomial_is_zero(pwqp1));
1534 isl_pw_qpolynomial_free(pwqp1);
1537 void test_split_periods(isl_ctx *ctx)
1540 isl_pw_qpolynomial *pwqp;
1542 str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
1543 "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
1544 "U >= 0; [U,V] -> U^2 : U >= 100 }";
1545 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1547 pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
1550 isl_pw_qpolynomial_free(pwqp);
1553 void test_union(isl_ctx *ctx)
1556 isl_union_set *uset1, *uset2;
1557 isl_union_map *umap1, *umap2;
1559 str = "{ [i] : 0 <= i <= 1 }";
1560 uset1 = isl_union_set_read_from_str(ctx, str);
1561 str = "{ [1] -> [0] }";
1562 umap1 = isl_union_map_read_from_str(ctx, str);
1564 umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
1565 assert(isl_union_map_is_equal(umap1, umap2));
1567 isl_union_map_free(umap1);
1568 isl_union_map_free(umap2);
1570 str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
1571 umap1 = isl_union_map_read_from_str(ctx, str);
1572 str = "{ A[i]; B[i] }";
1573 uset1 = isl_union_set_read_from_str(ctx, str);
1575 uset2 = isl_union_map_domain(umap1);
1577 assert(isl_union_set_is_equal(uset1, uset2));
1579 isl_union_set_free(uset1);
1580 isl_union_set_free(uset2);
1583 void test_bound(isl_ctx *ctx)
1586 isl_pw_qpolynomial *pwqp;
1587 isl_pw_qpolynomial_fold *pwf;
1589 str = "{ [[a, b, c, d] -> [e]] -> 0 }";
1590 pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
1591 pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
1592 assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
1593 isl_pw_qpolynomial_fold_free(pwf);
1596 void test_lift(isl_ctx *ctx)
1599 isl_basic_map *bmap;
1600 isl_basic_set *bset;
1602 str = "{ [i0] : exists e0 : i0 = 4e0 }";
1603 bset = isl_basic_set_read_from_str(ctx, str, 0);
1604 bset = isl_basic_set_lift(bset);
1605 bmap = isl_basic_map_from_range(bset);
1606 bset = isl_basic_map_domain(bmap);
1607 isl_basic_set_free(bset);
1610 void test_subset(isl_ctx *ctx)
1613 isl_set *set1, *set2;
1615 str = "{ [112, 0] }";
1616 set1 = isl_set_read_from_str(ctx, str, 0);
1617 str = "{ [i0, i1] : exists (e0 = [(i0 - i1)/16], e1: "
1618 "16e0 <= i0 - i1 and 16e0 >= -15 + i0 - i1 and "
1619 "16e1 <= i1 and 16e0 >= -i1 and 16e1 >= -i0 + i1) }";
1620 set2 = isl_set_read_from_str(ctx, str, 0);
1621 assert(isl_set_is_subset(set1, set2));
1628 struct isl_ctx *ctx;
1630 srcdir = getenv("srcdir");
1633 ctx = isl_ctx_alloc();
1638 test_split_periods(ctx);
1643 test_bijective(ctx);
1647 test_construction(ctx);
1650 test_application(ctx);
1651 test_affine_hull(ctx);
1652 test_convex_hull(ctx);