+
+ map = isl_map_read_from_str(ctx,
+ "[m,n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 2 and "
+ "1 <= i,i2 <= n and 1 <= j,j2 <= m or "
+ "i2 = i + 1 and 3 <= j2 - j <= 4 and "
+ "1 <= i,i2 <= n and 1 <= j,j2 <= m }", -1);
+ map = isl_map_transitive_closure(map, &exact);
+ assert(exact);
+ isl_map_free(map);
+
+ /* Kelly et al 1996, fig 12 */
+ map = isl_map_read_from_str(ctx,
+ "[n] -> { [i,j] -> [i2,j2] : i2 = i and j2 = j + 1 and "
+ "1 <= i,j,j+1 <= n or "
+ "j = n and j2 = 1 and i2 = i + 1 and "
+ "1 <= i,i+1 <= n }", -1);
+ map = isl_map_transitive_closure(map, &exact);
+ assert(exact);
+ map2 = isl_map_read_from_str(ctx,
+ "[n] -> { [i,j] -> [i2,j2] : 1 <= j < j2 <= n and "
+ "1 <= i <= n and i = i2 or "
+ "1 <= i < i2 <= n and 1 <= j <= n and "
+ "1 <= j2 <= n }", -1);
+ assert(isl_map_is_equal(map, map2));
+ isl_map_free(map2);
+ isl_map_free(map);
+
+ /* Omega's closure4 */
+ map = isl_map_read_from_str(ctx,
+ "[m,n] -> { [x,y] -> [x2,y2] : x2 = x and y2 = y + 1 and "
+ "1 <= x,y <= 10 or "
+ "x2 = x + 1 and y2 = y and "
+ "1 <= x <= 20 && 5 <= y <= 15 }", -1);
+ map = isl_map_transitive_closure(map, &exact);
+ assert(exact);
+ isl_map_free(map);
+
+ map = isl_map_read_from_str(ctx,
+ "[n] -> { [x] -> [y]: 1 <= n <= y - x <= 10 }", -1);
+ map = isl_map_transitive_closure(map, &exact);
+ assert(!exact);
+ map2 = isl_map_read_from_str(ctx,
+ "[n] -> { [x] -> [y] : 1 <= n <= 10 and y >= n + x }", -1);
+ assert(isl_map_is_equal(map, map2));
+ isl_map_free(map);
+ isl_map_free(map2);
+
+ str = "[n, m] -> { [i0, i1, i2, i3] -> [o0, o1, o2, o3] : "
+ "i3 = 1 and o0 = i0 and o1 = -1 + i1 and o2 = -1 + i2 and "
+ "o3 = -2 + i2 and i1 <= -1 + i0 and i1 >= 1 - m + i0 and "
+ "i1 >= 2 and i1 <= n and i2 >= 3 and i2 <= 1 + n and i2 <= m }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ map = isl_map_transitive_closure(map, &exact);
+ assert(exact);
+ map2 = isl_map_read_from_str(ctx, str, -1);
+ assert(isl_map_is_equal(map, map2));
+ isl_map_free(map);
+ isl_map_free(map2);
+
+ str = "{[0] -> [1]; [2] -> [3]}";
+ map = isl_map_read_from_str(ctx, str, -1);
+ map = isl_map_transitive_closure(map, &exact);
+ assert(exact);
+ map2 = isl_map_read_from_str(ctx, str, -1);
+ assert(isl_map_is_equal(map, map2));
+ isl_map_free(map);
+ isl_map_free(map2);
+}
+
+void test_lex(struct isl_ctx *ctx)
+{
+ isl_dim *dim;
+ isl_map *map;
+
+ dim = isl_dim_alloc(ctx, 0, 0, 0);
+ map = isl_map_lex_le(dim);
+ assert(!isl_map_is_empty(map));
+ isl_map_free(map);
+}
+
+void test_lexmin(struct isl_ctx *ctx)
+{
+ const char *str;
+ isl_map *map;
+ isl_set *set;
+ isl_set *set2;
+
+ str = "[p0, p1] -> { [] -> [] : "
+ "exists (e0 = [(2p1)/3], e1, e2, e3 = [(3 - p1 + 3e0)/3], "
+ "e4 = [(p1)/3], e5 = [(p1 + 3e4)/3]: "
+ "3e0 >= -2 + 2p1 and 3e0 >= p1 and 3e3 >= 1 - p1 + 3e0 and "
+ "3e0 <= 2p1 and 3e3 >= -2 + p1 and 3e3 <= -1 + p1 and p1 >= 3 and "
+ "3e5 >= -2 + 2p1 and 3e5 >= p1 and 3e5 <= -1 + p1 + 3e4 and "
+ "3e4 <= p1 and 3e4 >= -2 + p1 and e3 <= -1 + e0 and "
+ "3e4 >= 6 - p1 + 3e1 and 3e1 >= p1 and 3e5 >= -2 + p1 + 3e4 and "
+ "2e4 >= 3 - p1 + 2e1 and e4 <= e1 and 3e3 <= 2 - p1 + 3e0 and "
+ "e5 >= 1 + e1 and 3e4 >= 6 - 2p1 + 3e1 and "
+ "p0 >= 2 and p1 >= p0 and 3e2 >= p1 and 3e4 >= 6 - p1 + 3e2 and "
+ "e2 <= e1 and e3 >= 1 and e4 <= e2) }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ map = isl_map_lexmin(map);
+ isl_map_free(map);
+
+ str = "[C] -> { [obj,a,b,c] : obj <= 38 a + 7 b + 10 c and "
+ "a + b <= 1 and c <= 10 b and c <= C and a,b,c,C >= 0 }";
+ set = isl_set_read_from_str(ctx, str, -1);
+ set = isl_set_lexmax(set);
+ str = "[C] -> { [obj,a,b,c] : C = 8 }";
+ set2 = isl_set_read_from_str(ctx, str, -1);
+ set = isl_set_intersect(set, set2);
+ assert(!isl_set_is_empty(set));
+ isl_set_free(set);
+}
+
+struct must_may {
+ isl_map *must;
+ isl_map *may;
+};
+
+static int collect_must_may(__isl_take isl_map *dep, int must,
+ void *dep_user, void *user)
+{
+ struct must_may *mm = (struct must_may *)user;
+
+ if (must)
+ mm->must = isl_map_union(mm->must, dep);
+ else
+ mm->may = isl_map_union(mm->may, dep);
+
+ return 0;
+}
+
+static int common_space(void *first, void *second)
+{
+ int depth = *(int *)first;
+ return 2 * depth;
+}
+
+static int map_is_equal(__isl_keep isl_map *map, const char *str)
+{
+ isl_map *map2;
+ int equal;
+
+ if (!map)
+ return -1;
+
+ map2 = isl_map_read_from_str(map->ctx, str, -1);
+ equal = isl_map_is_equal(map, map2);
+ isl_map_free(map2);
+
+ return equal;
+}
+
+void test_dep(struct isl_ctx *ctx)
+{
+ const char *str;
+ isl_dim *dim;
+ isl_map *map;
+ isl_access_info *ai;
+ isl_flow *flow;
+ int depth;
+ struct must_may mm;
+
+ depth = 3;
+
+ str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_alloc(map, &depth, &common_space, 2);
+
+ str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 1, &depth);
+
+ str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 1, &depth);
+
+ flow = isl_access_info_compute_flow(ai);
+ dim = isl_dim_alloc(ctx, 0, 3, 3);
+ mm.must = isl_map_empty(isl_dim_copy(dim));
+ mm.may = isl_map_empty(dim);
+
+ isl_flow_foreach(flow, collect_must_may, &mm);
+
+ str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10); "
+ " [1,10,0] -> [2,5,0] }";
+ assert(map_is_equal(mm.must, str));
+ str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
+ assert(map_is_equal(mm.may, str));
+
+ isl_map_free(mm.must);
+ isl_map_free(mm.may);
+ isl_flow_free(flow);
+
+
+ str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_alloc(map, &depth, &common_space, 2);
+
+ str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 1, &depth);
+
+ str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 0, &depth);
+
+ flow = isl_access_info_compute_flow(ai);
+ dim = isl_dim_alloc(ctx, 0, 3, 3);
+ mm.must = isl_map_empty(isl_dim_copy(dim));
+ mm.may = isl_map_empty(dim);
+
+ isl_flow_foreach(flow, collect_must_may, &mm);
+
+ str = "{ [0,i,0] -> [2,i,0] : (0 <= i <= 4) or (6 <= i <= 10) }";
+ assert(map_is_equal(mm.must, str));
+ str = "{ [0,5,0] -> [2,5,0]; [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
+ assert(map_is_equal(mm.may, str));
+
+ isl_map_free(mm.must);
+ isl_map_free(mm.may);
+ isl_flow_free(flow);
+
+
+ str = "{ [2,i,0] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_alloc(map, &depth, &common_space, 2);
+
+ str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 0, &depth);
+
+ str = "{ [1,i,0] -> [5] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 0, &depth);
+
+ flow = isl_access_info_compute_flow(ai);
+ dim = isl_dim_alloc(ctx, 0, 3, 3);
+ mm.must = isl_map_empty(isl_dim_copy(dim));
+ mm.may = isl_map_empty(dim);
+
+ isl_flow_foreach(flow, collect_must_may, &mm);
+
+ str = "{ [0,i,0] -> [2,i,0] : 0 <= i <= 10; "
+ " [1,i,0] -> [2,5,0] : 0 <= i <= 10 }";
+ assert(map_is_equal(mm.may, str));
+ str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
+ assert(map_is_equal(mm.must, str));
+
+ isl_map_free(mm.must);
+ isl_map_free(mm.may);
+ isl_flow_free(flow);
+
+
+ str = "{ [0,i,2] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_alloc(map, &depth, &common_space, 2);
+
+ str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 0, &depth);
+
+ str = "{ [0,i,1] -> [5] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 0, &depth);
+
+ flow = isl_access_info_compute_flow(ai);
+ dim = isl_dim_alloc(ctx, 0, 3, 3);
+ mm.must = isl_map_empty(isl_dim_copy(dim));
+ mm.may = isl_map_empty(dim);
+
+ isl_flow_foreach(flow, collect_must_may, &mm);
+
+ str = "{ [0,i,0] -> [0,i,2] : 0 <= i <= 10; "
+ " [0,i,1] -> [0,5,2] : 0 <= i <= 5 }";
+ assert(map_is_equal(mm.may, str));
+ str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
+ assert(map_is_equal(mm.must, str));
+
+ isl_map_free(mm.must);
+ isl_map_free(mm.may);
+ isl_flow_free(flow);
+
+
+ str = "{ [0,i,1] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_alloc(map, &depth, &common_space, 2);
+
+ str = "{ [0,i,0] -> [i] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 0, &depth);
+
+ str = "{ [0,i,2] -> [5] : 0 <= i <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 0, &depth);
+
+ flow = isl_access_info_compute_flow(ai);
+ dim = isl_dim_alloc(ctx, 0, 3, 3);
+ mm.must = isl_map_empty(isl_dim_copy(dim));
+ mm.may = isl_map_empty(dim);
+
+ isl_flow_foreach(flow, collect_must_may, &mm);
+
+ str = "{ [0,i,0] -> [0,i,1] : 0 <= i <= 10; "
+ " [0,i,2] -> [0,5,1] : 0 <= i <= 4 }";
+ assert(map_is_equal(mm.may, str));
+ str = "{ [i,j,k] -> [l,m,n] : 1 = 0 }";
+ assert(map_is_equal(mm.must, str));
+
+ isl_map_free(mm.must);
+ isl_map_free(mm.may);
+ isl_flow_free(flow);
+
+
+ depth = 5;
+
+ str = "{ [1,i,0,0,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_alloc(map, &depth, &common_space, 1);
+
+ str = "{ [0,i,0,j,0] -> [i,j] : 0 <= i <= 10 and 0 <= j <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ ai = isl_access_info_add_source(ai, map, 1, &depth);
+
+ flow = isl_access_info_compute_flow(ai);
+ dim = isl_dim_alloc(ctx, 0, 5, 5);
+ mm.must = isl_map_empty(isl_dim_copy(dim));
+ mm.may = isl_map_empty(dim);
+
+ isl_flow_foreach(flow, collect_must_may, &mm);
+
+ str = "{ [0,i,0,j,0] -> [1,i,0,0,0] : 0 <= i,j <= 10 }";
+ assert(map_is_equal(mm.must, str));
+ str = "{ [0,0,0,0,0] -> [0,0,0,0,0] : 1 = 0 }";
+ assert(map_is_equal(mm.may, str));
+
+ isl_map_free(mm.must);
+ isl_map_free(mm.may);
+ isl_flow_free(flow);
+}
+
+void test_sv(struct isl_ctx *ctx)
+{
+ const char *str;
+ isl_map *map;
+
+ str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 9 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ assert(isl_map_is_single_valued(map));
+ isl_map_free(map);
+
+ str = "[N] -> { [i] -> [f] : 0 <= i <= N and 0 <= i - 10 f <= 10 }";
+ map = isl_map_read_from_str(ctx, str, -1);
+ assert(!isl_map_is_single_valued(map));
+ isl_map_free(map);
+}
+
+void test_bijective_case(struct isl_ctx *ctx, const char *str, int bijective)
+{
+ isl_map *map;
+
+ map = isl_map_read_from_str(ctx, str, -1);
+ if (bijective)
+ assert(isl_map_is_bijective(map));
+ else
+ assert(!isl_map_is_bijective(map));
+ isl_map_free(map);
+}
+
+void test_bijective(struct isl_ctx *ctx)
+{
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [i]}", 0);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=i}", 1);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=0}", 1);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [i] : j=N}", 1);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [j,i]}", 1);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [i+j]}", 0);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> []}", 0);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,j,N]}", 1);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i]}", 0);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [i,i]}", 0);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,i]}", 0);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [2i,j]}", 1);
+ test_bijective_case(ctx, "[N,M]->{[i,j] -> [x,y] : 2x=i & y =j}", 1);
+}
+
+void test_pwqp(struct isl_ctx *ctx)
+{
+ const char *str;
+ isl_set *set;
+ isl_pw_qpolynomial *pwqp1, *pwqp2;
+
+ str = "{ [i,j,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
+ pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+
+ pwqp1 = isl_pw_qpolynomial_move_dims(pwqp1, isl_dim_param, 0,
+ isl_dim_set, 1, 1);
+
+ str = "[j] -> { [i,k] -> 1 + 9 * [i/5] + 7 * [j/11] + 4 * [k/13] }";
+ pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
+
+ pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
+
+ assert(isl_pw_qpolynomial_is_zero(pwqp1));
+
+ isl_pw_qpolynomial_free(pwqp1);
+
+ str = "{ [i] -> i }";
+ pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+ str = "{ [k] : exists a : k = 2a }";
+ set = isl_set_read_from_str(ctx, str, 0);
+ pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
+ str = "{ [i] -> i }";
+ pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
+
+ pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
+
+ assert(isl_pw_qpolynomial_is_zero(pwqp1));
+
+ isl_pw_qpolynomial_free(pwqp1);
+
+ str = "{ [i] -> i + [ (i + [i/3])/2 ] }";
+ pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+ str = "{ [10] }";
+ set = isl_set_read_from_str(ctx, str, 0);
+ pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
+ str = "{ [i] -> 16 }";
+ pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
+
+ pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
+
+ assert(isl_pw_qpolynomial_is_zero(pwqp1));
+
+ isl_pw_qpolynomial_free(pwqp1);
+
+ str = "{ [i] -> ([(i)/2]) }";
+ pwqp1 = isl_pw_qpolynomial_read_from_str(ctx, str);
+ str = "{ [k] : exists a : k = 2a+1 }";
+ set = isl_set_read_from_str(ctx, str, 0);
+ pwqp1 = isl_pw_qpolynomial_gist(pwqp1, set);
+ str = "{ [i] -> -1/2 + 1/2 * i }";
+ pwqp2 = isl_pw_qpolynomial_read_from_str(ctx, str);
+
+ pwqp1 = isl_pw_qpolynomial_sub(pwqp1, pwqp2);
+
+ assert(isl_pw_qpolynomial_is_zero(pwqp1));
+
+ isl_pw_qpolynomial_free(pwqp1);
+}
+
+void test_split_periods(isl_ctx *ctx)
+{
+ const char *str;
+ isl_pw_qpolynomial *pwqp;
+
+ str = "{ [U,V] -> 1/3 * U + 2/3 * V - [(U + 2V)/3] + [U/2] : "
+ "U + 2V + 3 >= 0 and - U -2V >= 0 and - U + 10 >= 0 and "
+ "U >= 0; [U,V] -> U^2 : U >= 100 }";
+ pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
+
+ pwqp = isl_pw_qpolynomial_split_periods(pwqp, 2);
+ assert(pwqp);
+
+ isl_pw_qpolynomial_free(pwqp);
+}
+
+void test_union(isl_ctx *ctx)
+{
+ const char *str;
+ isl_union_set *uset1, *uset2;
+ isl_union_map *umap1, *umap2;
+
+ str = "{ [i] : 0 <= i <= 1 }";
+ uset1 = isl_union_set_read_from_str(ctx, str);
+ str = "{ [1] -> [0] }";
+ umap1 = isl_union_map_read_from_str(ctx, str);
+
+ umap2 = isl_union_set_lex_gt_union_set(isl_union_set_copy(uset1), uset1);
+ assert(isl_union_map_is_equal(umap1, umap2));
+
+ isl_union_map_free(umap1);
+ isl_union_map_free(umap2);
+
+ str = "{ A[i] -> B[i]; B[i] -> C[i]; A[0] -> C[1] }";
+ umap1 = isl_union_map_read_from_str(ctx, str);
+ str = "{ A[i]; B[i] }";
+ uset1 = isl_union_set_read_from_str(ctx, str);
+
+ uset2 = isl_union_map_domain(umap1);
+
+ assert(isl_union_set_is_equal(uset1, uset2));
+
+ isl_union_set_free(uset1);
+ isl_union_set_free(uset2);
+}
+
+void test_bound(isl_ctx *ctx)
+{
+ const char *str;
+ isl_pw_qpolynomial *pwqp;
+ isl_pw_qpolynomial_fold *pwf;
+
+ str = "{ [[a, b, c, d] -> [e]] -> 0 }";
+ pwqp = isl_pw_qpolynomial_read_from_str(ctx, str);
+ pwf = isl_pw_qpolynomial_bound(pwqp, isl_fold_max, NULL);
+ assert(isl_pw_qpolynomial_fold_dim(pwf, isl_dim_set) == 4);
+ isl_pw_qpolynomial_fold_free(pwf);