2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 #include <isl_polynomial_private.h>
18 #include <isl_printer_private.h>
19 #include <isl_dim_private.h>
20 #include <isl_map_private.h>
21 #include <isl_mat_private.h>
22 #include <isl/union_map.h>
24 static const char *s_to[2] = { " -> ", " \\to " };
25 static const char *s_and[2] = { " and ", " \\wedge " };
26 static const char *s_or[2] = { " or ", " \\vee " };
27 static const char *s_le[2] = { "<=", "\\le" };
28 static const char *s_ge[2] = { ">=", "\\ge" };
29 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
30 static const char *s_close_set[2] = { " }", " \\,\\}" };
31 static const char *s_open_list[2] = { "[", "(" };
32 static const char *s_close_list[2] = { "]", ")" };
33 static const char *s_such_that[2] = { " : ", " \\mid " };
34 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
35 static const char *s_close_exists[2] = { ")", "" };
36 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
37 static const char *s_param_prefix[2] = { "p", "p_" };
38 static const char *s_input_prefix[2] = { "i", "i_" };
39 static const char *s_output_prefix[2] = { "o", "o_" };
41 static __isl_give isl_printer *print_constraint_polylib(
42 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
45 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
46 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
47 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
48 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
50 p = isl_printer_start_line(p);
51 p = isl_printer_print_int(p, ineq);
52 for (i = 0; i < n_out; ++i) {
53 p = isl_printer_print_str(p, " ");
54 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
56 for (i = 0; i < n_in; ++i) {
57 p = isl_printer_print_str(p, " ");
58 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
60 for (i = 0; i < bmap->n_div; ++i) {
61 p = isl_printer_print_str(p, " ");
62 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
64 for (i = 0; i < nparam; ++i) {
65 p = isl_printer_print_str(p, " ");
66 p = isl_printer_print_isl_int(p, c[1+i]);
68 p = isl_printer_print_str(p, " ");
69 p = isl_printer_print_isl_int(p, c[0]);
70 p = isl_printer_end_line(p);
74 static __isl_give isl_printer *print_constraints_polylib(
75 struct isl_basic_map *bmap, __isl_take isl_printer *p)
79 p = isl_printer_set_isl_int_width(p, 5);
81 for (i = 0; i < bmap->n_eq; ++i)
82 p = print_constraint_polylib(bmap, 0, i, p);
83 for (i = 0; i < bmap->n_ineq; ++i)
84 p = print_constraint_polylib(bmap, 1, i, p);
89 static __isl_give isl_printer *bset_print_constraints_polylib(
90 struct isl_basic_set *bset, __isl_take isl_printer *p)
92 return print_constraints_polylib((struct isl_basic_map *)bset, p);
95 static __isl_give isl_printer *isl_basic_map_print_polylib(
96 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
98 unsigned total = isl_basic_map_total_dim(bmap);
99 p = isl_printer_start_line(p);
100 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
101 p = isl_printer_print_str(p, " ");
102 p = isl_printer_print_int(p, 1 + total + 1);
104 p = isl_printer_print_str(p, " ");
105 p = isl_printer_print_int(p,
106 isl_basic_map_dim(bmap, isl_dim_out));
107 p = isl_printer_print_str(p, " ");
108 p = isl_printer_print_int(p,
109 isl_basic_map_dim(bmap, isl_dim_in));
110 p = isl_printer_print_str(p, " ");
111 p = isl_printer_print_int(p,
112 isl_basic_map_dim(bmap, isl_dim_div));
113 p = isl_printer_print_str(p, " ");
114 p = isl_printer_print_int(p,
115 isl_basic_map_dim(bmap, isl_dim_param));
117 p = isl_printer_end_line(p);
118 return print_constraints_polylib(bmap, p);
121 static __isl_give isl_printer *isl_basic_set_print_polylib(
122 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
124 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
127 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
128 __isl_take isl_printer *p, int ext)
132 p = isl_printer_start_line(p);
133 p = isl_printer_print_int(p, map->n);
134 p = isl_printer_end_line(p);
135 for (i = 0; i < map->n; ++i) {
136 p = isl_printer_start_line(p);
137 p = isl_printer_end_line(p);
138 p = isl_basic_map_print_polylib(map->p[i], p, ext);
143 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
144 __isl_take isl_printer *p, int ext)
146 return isl_map_print_polylib((struct isl_map *)set, p, ext);
149 static int count_same_name(__isl_keep isl_dim *dim,
150 enum isl_dim_type type, unsigned pos, const char *name)
156 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
157 s = t == type ? pos : isl_dim_size(dim, t);
158 for (p = 0; p < s; ++p) {
159 const char *n = isl_dim_get_name(dim, t, p);
160 if (n && !strcmp(n, name))
167 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
168 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
175 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
179 if (type == isl_dim_param)
180 prefix = s_param_prefix[latex];
181 else if (type == isl_dim_div)
182 prefix = s_div_prefix[latex];
183 else if (set || type == isl_dim_in)
184 prefix = s_input_prefix[latex];
186 prefix = s_output_prefix[latex];
187 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
190 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
192 p = isl_printer_print_str(p, name);
194 p = isl_printer_print_str(p, "'");
198 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
200 enum isl_dim_type type;
201 unsigned n_in = isl_dim_size(dim, isl_dim_in);
202 unsigned n_out = isl_dim_size(dim, isl_dim_out);
203 unsigned nparam = isl_dim_size(dim, isl_dim_param);
205 if (*pos < 1 + nparam) {
206 type = isl_dim_param;
208 } else if (*pos < 1 + nparam + n_in) {
211 } else if (*pos < 1 + nparam + n_in + n_out) {
213 *pos -= 1 + nparam + n_in;
216 *pos -= 1 + nparam + n_in + n_out;
222 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
223 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
225 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
226 __isl_keep isl_mat *div,
227 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
229 enum isl_dim_type type;
233 return isl_printer_print_isl_int(p, c);
235 type = pos2type(dim, &pos);
236 print_div_def = type == isl_dim_div && div &&
237 !isl_int_is_zero(div->row[pos][0]);
239 if (isl_int_is_one(c))
241 else if (isl_int_is_negone(c))
242 p = isl_printer_print_str(p, "-");
244 p = isl_printer_print_isl_int(p, c);
245 if (p->output_format == ISL_FORMAT_C || print_div_def)
246 p = isl_printer_print_str(p, "*");
249 p = print_div(dim, div, pos, p);
251 p = print_name(dim, p, type, pos, set, latex);
255 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
256 __isl_keep isl_mat *div,
257 __isl_take isl_printer *p, isl_int *c, int len, int set)
262 for (i = 0, first = 1; i < len; ++i) {
264 if (isl_int_is_zero(c[i]))
267 if (isl_int_is_neg(c[i])) {
269 isl_int_neg(c[i], c[i]);
270 p = isl_printer_print_str(p, " - ");
272 p = isl_printer_print_str(p, " + ");
275 p = print_term(dim, div, c[i], i, p, set, 0);
277 isl_int_neg(c[i], c[i]);
280 p = isl_printer_print_str(p, "0");
284 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
285 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
287 unsigned len = 1 + isl_basic_map_total_dim(bmap);
288 return print_affine_of_len(dim, NULL, p, c, len, set);
291 static int defining_equality(__isl_keep isl_basic_map *eq,
292 __isl_keep isl_dim *dim, enum isl_dim_type type, int pos)
300 pos += isl_dim_offset(dim, type);
301 total = isl_basic_map_total_dim(eq);
303 for (i = 0; i < eq->n_eq; ++i) {
304 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
306 if (isl_int_is_one(eq->eq[i][1 + pos]))
307 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
314 /* offset is the offset of local_dim inside global_type of global_dim.
316 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
317 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
318 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
319 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
323 if (global_dim != local_dim && local_type == isl_dim_out)
324 offset += local_dim->n_in;
326 for (i = 0; i < isl_dim_size(local_dim, local_type); ++i) {
328 p = isl_printer_print_str(p, ", ");
329 j = defining_equality(eq, global_dim, global_type, offset + i);
331 int pos = 1 + isl_dim_offset(global_dim, global_type)
333 p = print_affine_of_len(eq->dim, NULL,
334 p, eq->eq[j], pos, set);
336 p = print_name(global_dim, p, global_type, offset + i,
343 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
344 __isl_take isl_printer *p, enum isl_dim_type type,
345 int set, int latex, __isl_keep isl_basic_map *eq)
347 return print_nested_var_list(p, dim, type, dim, type, set, latex, eq, 0);
350 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
351 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
352 __isl_keep isl_dim *local_dim,
353 int set, int latex, __isl_keep isl_basic_map *eq, int offset);
355 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
356 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
357 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
358 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
360 const char *name = NULL;
361 unsigned n = isl_dim_size(local_dim, local_type);
362 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
363 name = isl_dim_get_tuple_name(local_dim, local_type);
366 p = isl_printer_print_str(p, "\\mathrm{");
367 p = isl_printer_print_str(p, name);
369 p = isl_printer_print_str(p, "}");
372 if (!latex || n != 1 || name)
373 p = isl_printer_print_str(p, s_open_list[latex]);
374 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
375 local_dim->nested[local_type - isl_dim_in]) {
376 if (global_dim != local_dim && local_type == isl_dim_out)
377 offset += local_dim->n_in;
378 p = print_nested_map_dim(p, global_dim, global_type,
379 local_dim->nested[local_type - isl_dim_in],
380 set, latex, eq, offset);
382 p = print_nested_var_list(p, global_dim, global_type,
383 local_dim, local_type, set, latex, eq, offset);
384 if (!latex || n != 1 || name)
385 p = isl_printer_print_str(p, s_close_list[latex]);
389 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
390 __isl_take isl_printer *p, enum isl_dim_type type,
391 int set, int latex, __isl_keep isl_basic_map *eq)
393 return print_nested_tuple(p, dim, type, dim, type, set, latex, eq, 0);
396 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
397 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
398 __isl_keep isl_dim *local_dim,
399 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
401 p = print_nested_tuple(p, global_dim, global_type,
402 local_dim, isl_dim_in, set, latex, eq, offset);
403 p = isl_printer_print_str(p, s_to[latex]);
404 p = print_nested_tuple(p, global_dim, global_type,
405 local_dim, isl_dim_out, set, latex, eq, offset);
410 static __isl_give isl_printer *print_dim(__isl_keep isl_dim *dim,
411 __isl_take isl_printer *p, int set, int latex,
412 __isl_keep isl_basic_map *eq)
415 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
417 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
418 p = isl_printer_print_str(p, s_to[latex]);
419 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
425 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
426 __isl_take isl_printer *p)
428 if (isl_dim_size(dim, isl_dim_param) == 0)
431 p = isl_printer_start_line(p);
432 p = isl_printer_print_str(p, "symbolic ");
433 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
434 p = isl_printer_print_str(p, ";");
435 p = isl_printer_end_line(p);
439 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
440 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
441 isl_int *c, int last, const char *op, int first_constraint, int set,
444 if (!first_constraint)
445 p = isl_printer_print_str(p, s_and[latex]);
447 isl_int_abs(c[last], c[last]);
449 p = print_term(dim, NULL, c[last], last, p, set, latex);
451 p = isl_printer_print_str(p, " ");
452 p = isl_printer_print_str(p, op);
453 p = isl_printer_print_str(p, " ");
455 isl_int_set_si(c[last], 0);
456 p = print_affine(bmap, dim, p, c, set);
461 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
462 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
466 unsigned total = isl_basic_map_total_dim(bmap);
468 c = isl_vec_alloc(bmap->ctx, 1 + total);
472 for (i = bmap->n_eq - 1; i >= 0; --i) {
473 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
474 isl_assert(bmap->ctx, l >= 0, goto error);
475 if (isl_int_is_neg(bmap->eq[i][l]))
476 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
478 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
479 p = print_constraint(bmap, dim, p, c->el, l,
480 "=", i == bmap->n_eq - 1, set, latex);
482 for (i = 0; i < bmap->n_ineq; ++i) {
483 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
488 s = isl_int_sgn(bmap->ineq[i][l]);
490 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
492 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
493 op = s < 0 ? s_le[latex] : s_ge[latex];
494 p = print_constraint(bmap, dim, p, c->el, l,
495 op, !bmap->n_eq && !i, set, latex);
507 static __isl_give isl_printer *print_omega_constraints(
508 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
510 if (bmap->n_eq + bmap->n_ineq == 0)
513 p = isl_printer_print_str(p, ": ");
514 if (bmap->n_div > 0) {
516 p = isl_printer_print_str(p, "exists (");
517 for (i = 0; i < bmap->n_div; ++i) {
519 p = isl_printer_print_str(p, ", ");
520 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
522 p = isl_printer_print_str(p, ": ");
524 p = print_constraints(bmap, bmap->dim, p, set, 0);
526 p = isl_printer_print_str(p, ")");
530 static __isl_give isl_printer *basic_map_print_omega(
531 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
533 p = isl_printer_print_str(p, "{ [");
534 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
535 p = isl_printer_print_str(p, "] -> [");
536 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
537 p = isl_printer_print_str(p, "] ");
538 p = print_omega_constraints(bmap, p, 0);
539 p = isl_printer_print_str(p, " }");
543 static __isl_give isl_printer *isl_basic_map_print_omega(
544 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
546 p = print_omega_parameters(bmap->dim, p);
548 p = isl_printer_start_line(p);
549 p = basic_map_print_omega(bmap, p);
550 p = isl_printer_end_line(p);
554 static __isl_give isl_printer *basic_set_print_omega(
555 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
557 p = isl_printer_print_str(p, "{ [");
558 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
559 p = isl_printer_print_str(p, "] ");
560 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
561 p = isl_printer_print_str(p, " }");
565 static __isl_give isl_printer *isl_basic_set_print_omega(
566 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
568 p = print_omega_parameters(bset->dim, p);
570 p = isl_printer_start_line(p);
571 p = basic_set_print_omega(bset, p);
572 p = isl_printer_end_line(p);
576 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
577 __isl_take isl_printer *p)
581 p = print_omega_parameters(map->dim, p);
583 p = isl_printer_start_line(p);
584 for (i = 0; i < map->n; ++i) {
586 p = isl_printer_print_str(p, " union ");
587 p = basic_map_print_omega(map->p[i], p);
589 p = isl_printer_end_line(p);
593 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
594 __isl_take isl_printer *p)
598 p = print_omega_parameters(set->dim, p);
600 p = isl_printer_start_line(p);
601 for (i = 0; i < set->n; ++i) {
603 p = isl_printer_print_str(p, " union ");
604 p = basic_set_print_omega(set->p[i], p);
606 p = isl_printer_end_line(p);
610 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
611 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
613 if (bmap->n_div > 0) {
615 p = isl_printer_print_str(p, s_open_exists[latex]);
616 for (i = 0; i < bmap->n_div; ++i) {
618 p = isl_printer_print_str(p, ", ");
619 p = print_name(dim, p, isl_dim_div, i, 0, latex);
620 if (latex || isl_int_is_zero(bmap->div[i][0]))
622 p = isl_printer_print_str(p, " = [(");
623 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
624 p = isl_printer_print_str(p, ")/");
625 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
626 p = isl_printer_print_str(p, "]");
628 p = isl_printer_print_str(p, ": ");
631 p = print_constraints(bmap, dim, p, set, latex);
634 p = isl_printer_print_str(p, s_close_exists[latex]);
638 static __isl_give isl_printer *isl_basic_map_print_isl(
639 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int latex)
643 p = isl_printer_start_line(p);
644 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
645 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
646 p = isl_printer_print_str(p, " -> ");
648 p = isl_printer_print_str(p, "{ ");
649 p = print_dim(bmap->dim, p, 0, latex, NULL);
650 p = isl_printer_print_str(p, " : ");
651 p = print_disjunct(bmap, bmap->dim, p, 0, latex);
652 p = isl_printer_print_str(p, " }");
656 static __isl_give isl_printer *isl_basic_set_print_isl(
657 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int latex)
661 p = isl_printer_start_line(p);
662 if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
663 p = print_tuple(bset->dim, p, isl_dim_param, 0, latex, NULL);
664 p = isl_printer_print_str(p, " -> ");
666 p = isl_printer_print_str(p, "{ ");
667 p = print_dim(bset->dim, p, 1, latex, NULL);
668 p = isl_printer_print_str(p, " : ");
669 p = print_disjunct((isl_basic_map *)bset, bset->dim, p, 1, latex);
670 p = isl_printer_print_str(p, " }");
674 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
675 __isl_take isl_printer *p, int set, int latex)
679 if (isl_map_fast_is_universe(map))
682 p = isl_printer_print_str(p, s_such_that[latex]);
684 p = isl_printer_print_str(p, "1 = 0");
685 for (i = 0; i < map->n; ++i) {
687 p = isl_printer_print_str(p, s_or[latex]);
688 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
689 p = isl_printer_print_str(p, "(");
690 p = print_disjunct(map->p[i], map->dim, p, set, latex);
691 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
692 p = isl_printer_print_str(p, ")");
697 struct isl_aff_split {
702 static void free_split(__isl_take struct isl_aff_split *split, int n)
709 for (i = 0; i < n; ++i) {
710 isl_basic_map_free(split[i].aff);
711 isl_map_free(split[i].map);
717 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
720 unsigned nparam, n_in, n_out, total;
722 bmap = isl_basic_map_cow(bmap);
725 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
728 nparam = isl_basic_map_dim(bmap, isl_dim_param);
729 n_in = isl_basic_map_dim(bmap, isl_dim_in);
730 n_out = isl_basic_map_dim(bmap, isl_dim_out);
731 total = isl_basic_map_dim(bmap, isl_dim_all);
732 for (i = bmap->n_eq - 1; i >= 0; --i) {
733 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
734 if (j >= nparam && j < nparam + n_in + n_out &&
735 (isl_int_is_one(bmap->eq[i][1 + j]) ||
736 isl_int_is_negone(bmap->eq[i][1 + j])))
738 if (isl_basic_map_drop_equality(bmap, i) < 0)
742 bmap = isl_basic_map_finalize(bmap);
746 isl_basic_map_free(bmap);
750 static int aff_split_cmp(const void *p1, const void *p2)
752 const struct isl_aff_split *s1, *s2;
753 s1 = (const struct isl_aff_split *) p1;
754 s2 = (const struct isl_aff_split *) p2;
756 return isl_basic_map_fast_cmp(s1->aff, s2->aff);
759 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
760 __isl_keep isl_basic_map *aff)
768 total = isl_dim_total(bmap->dim);
770 for (i = bmap->n_eq - 1; i >= 0; --i) {
771 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
774 for (j = 0; j < aff->n_eq; ++j) {
775 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
776 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
778 if (isl_basic_map_drop_equality(bmap, i) < 0)
786 isl_basic_map_free(bmap);
790 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
793 struct isl_aff_split *split;
795 split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
799 for (i = 0; i < map->n; ++i) {
801 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
802 bmap = isl_basic_map_copy(map->p[i]);
803 bmap = isl_basic_map_cow(bmap);
804 bmap = drop_aff(bmap, split[i].aff);
805 split[i].map = isl_map_from_basic_map(bmap);
806 if (!split[i].aff || !split[i].map)
810 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
813 for (i = n - 1; i >= 1; --i) {
814 if (!isl_basic_map_fast_is_equal(split[i - 1].aff,
817 isl_basic_map_free(split[i].aff);
818 split[i - 1].map = isl_map_union(split[i - 1].map,
821 split[i] = split[n - 1];
822 split[n - 1].aff = NULL;
823 split[n - 1].map = NULL;
829 free_split(split, map->n);
833 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
834 struct isl_aff_split *split, int n, int set)
838 for (i = 0; i < n; ++i) {
843 dim = split[i].map->dim;
845 p = isl_printer_print_str(p, "; ");
846 p = print_dim(dim, p, set, 0, split[i].aff);
847 p = print_disjuncts(split[i].map, p, set, 0);
853 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
854 __isl_take isl_printer *p, int set)
856 struct isl_aff_split *split = NULL;
859 split = split_aff(map);
861 p = print_split_map(p, split, map->n, set);
863 p = print_dim(map->dim, p, set, 0, NULL);
864 p = print_disjuncts(map, p, set, 0);
866 free_split(split, map->n);
870 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
871 __isl_take isl_printer *p, int set)
873 if (isl_map_dim(map, isl_dim_param) > 0) {
874 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
875 p = isl_printer_print_str(p, s_to[0]);
877 p = isl_printer_print_str(p, s_open_set[0]);
878 p = isl_map_print_isl_body(map, p, set);
879 p = isl_printer_print_str(p, s_close_set[0]);
883 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
884 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
886 if (isl_map_dim(map, isl_dim_param) > 0) {
887 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
888 p = isl_printer_print_str(p, s_to[1]);
890 p = isl_printer_print_str(p, s_open_set[1]);
891 p = print_dim(map->dim, p, set, 1, aff);
892 p = print_disjuncts(map, p, set, 1);
893 p = isl_printer_print_str(p, s_close_set[1]);
898 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
899 __isl_take isl_printer *p, int set)
902 struct isl_aff_split *split = NULL;
905 split = split_aff(map);
908 return print_latex_map(map, p, NULL, set);
910 for (i = 0; i < map->n; ++i) {
914 p = isl_printer_print_str(p, " \\cup ");
915 p = print_latex_map(split[i].map, p, split[i].aff, set);
918 free_split(split, map->n);
922 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
923 __isl_keep isl_basic_map *bmap)
927 if (p->output_format == ISL_FORMAT_ISL)
928 return isl_basic_map_print_isl(bmap, p, 0);
929 else if (p->output_format == ISL_FORMAT_OMEGA)
930 return isl_basic_map_print_omega(bmap, p);
931 isl_assert(bmap->ctx, 0, goto error);
937 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
938 const char *prefix, const char *suffix, unsigned output_format)
940 isl_printer *printer;
945 printer = isl_printer_to_file(bmap->ctx, out);
946 printer = isl_printer_set_indent(printer, indent);
947 printer = isl_printer_set_prefix(printer, prefix);
948 printer = isl_printer_set_suffix(printer, suffix);
949 printer = isl_printer_set_output_format(printer, output_format);
950 isl_printer_print_basic_map(printer, bmap);
952 isl_printer_free(printer);
955 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
956 __isl_keep isl_basic_set *bset)
961 if (p->output_format == ISL_FORMAT_ISL)
962 return isl_basic_set_print_isl(bset, p, 0);
963 else if (p->output_format == ISL_FORMAT_POLYLIB)
964 return isl_basic_set_print_polylib(bset, p, 0);
965 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
966 return isl_basic_set_print_polylib(bset, p, 1);
967 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
968 return bset_print_constraints_polylib(bset, p);
969 else if (p->output_format == ISL_FORMAT_OMEGA)
970 return isl_basic_set_print_omega(bset, p);
971 isl_assert(p->ctx, 0, goto error);
977 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
978 const char *prefix, const char *suffix, unsigned output_format)
980 isl_printer *printer;
985 printer = isl_printer_to_file(bset->ctx, out);
986 printer = isl_printer_set_indent(printer, indent);
987 printer = isl_printer_set_prefix(printer, prefix);
988 printer = isl_printer_set_suffix(printer, suffix);
989 printer = isl_printer_set_output_format(printer, output_format);
990 isl_printer_print_basic_set(printer, bset);
992 isl_printer_free(printer);
995 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
996 __isl_keep isl_set *set)
1000 if (p->output_format == ISL_FORMAT_ISL)
1001 return isl_map_print_isl((isl_map *)set, p, 1);
1002 else if (p->output_format == ISL_FORMAT_POLYLIB)
1003 return isl_set_print_polylib(set, p, 0);
1004 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1005 return isl_set_print_polylib(set, p, 1);
1006 else if (p->output_format == ISL_FORMAT_OMEGA)
1007 return isl_set_print_omega(set, p);
1008 else if (p->output_format == ISL_FORMAT_LATEX)
1009 return isl_map_print_latex((isl_map *)set, p, 1);
1010 isl_assert(set->ctx, 0, goto error);
1012 isl_printer_free(p);
1016 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1017 unsigned output_format)
1019 isl_printer *printer;
1024 printer = isl_printer_to_file(set->ctx, out);
1025 printer = isl_printer_set_indent(printer, indent);
1026 printer = isl_printer_set_output_format(printer, output_format);
1027 printer = isl_printer_print_set(printer, set);
1029 isl_printer_free(printer);
1032 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1033 __isl_keep isl_map *map)
1038 if (p->output_format == ISL_FORMAT_ISL)
1039 return isl_map_print_isl(map, p, 0);
1040 else if (p->output_format == ISL_FORMAT_POLYLIB)
1041 return isl_map_print_polylib(map, p, 0);
1042 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1043 return isl_map_print_polylib(map, p, 1);
1044 else if (p->output_format == ISL_FORMAT_OMEGA)
1045 return isl_map_print_omega(map, p);
1046 else if (p->output_format == ISL_FORMAT_LATEX)
1047 return isl_map_print_latex(map, p, 0);
1048 isl_assert(map->ctx, 0, goto error);
1050 isl_printer_free(p);
1054 struct isl_union_print_data {
1060 static int print_map_body(__isl_take isl_map *map, void *user)
1062 struct isl_union_print_data *data;
1063 data = (struct isl_union_print_data *)user;
1066 data->p = isl_printer_print_str(data->p, "; ");
1069 data->p = isl_map_print_isl_body(map, data->p, data->set);
1075 static __isl_give isl_printer *isl_union_map_print_isl(
1076 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1078 struct isl_union_print_data data = { p, set, 1 };
1080 dim = isl_union_map_get_dim(umap);
1081 if (isl_dim_size(dim, isl_dim_param) > 0) {
1082 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1083 p = isl_printer_print_str(p, s_to[0]);
1086 p = isl_printer_print_str(p, s_open_set[0]);
1087 isl_union_map_foreach_map(umap, &print_map_body, &data);
1089 p = isl_printer_print_str(p, s_close_set[0]);
1093 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1095 struct isl_union_print_data *data;
1096 data = (struct isl_union_print_data *)user;
1099 data->p = isl_printer_print_str(data->p, " \\cup ");
1102 data->p = isl_map_print_latex(map, data->p, data->set);
1108 static __isl_give isl_printer *isl_union_map_print_latex(
1109 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1111 struct isl_union_print_data data = { p, set, 1 };
1112 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1117 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1118 __isl_keep isl_union_map *umap)
1123 if (p->output_format == ISL_FORMAT_ISL)
1124 return isl_union_map_print_isl(umap, p, 0);
1125 if (p->output_format == ISL_FORMAT_LATEX)
1126 return isl_union_map_print_latex(umap, p, 0);
1128 isl_die(p->ctx, isl_error_invalid,
1129 "invalid output format for isl_union_map", goto error);
1131 isl_printer_free(p);
1135 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1136 __isl_keep isl_union_set *uset)
1141 if (p->output_format == ISL_FORMAT_ISL)
1142 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1143 if (p->output_format == ISL_FORMAT_LATEX)
1144 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1146 isl_die(p->ctx, isl_error_invalid,
1147 "invalid output format for isl_union_set", goto error);
1149 isl_printer_free(p);
1153 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1154 unsigned output_format)
1156 isl_printer *printer;
1161 printer = isl_printer_to_file(map->ctx, out);
1162 printer = isl_printer_set_indent(printer, indent);
1163 printer = isl_printer_set_output_format(printer, output_format);
1164 printer = isl_printer_print_map(printer, map);
1166 isl_printer_free(printer);
1169 void isl_basic_map_dump(__isl_keep isl_basic_map *bmap)
1171 isl_printer *printer;
1176 printer = isl_printer_to_file(isl_basic_map_get_ctx(bmap), stderr);
1177 printer = isl_printer_print_basic_map(printer, bmap);
1178 printer = isl_printer_end_line(printer);
1180 isl_printer_free(printer);
1183 void isl_basic_set_dump(__isl_keep isl_basic_set *bset)
1185 isl_printer *printer;
1190 printer = isl_printer_to_file(isl_basic_set_get_ctx(bset), stderr);
1191 printer = isl_printer_print_basic_set(printer, bset);
1192 printer = isl_printer_end_line(printer);
1194 isl_printer_free(printer);
1197 void isl_map_dump(__isl_keep isl_map *map)
1199 isl_printer *printer;
1204 printer = isl_printer_to_file(isl_map_get_ctx(map), stderr);
1205 printer = isl_printer_print_map(printer, map);
1206 printer = isl_printer_end_line(printer);
1208 isl_printer_free(printer);
1211 void isl_set_dump(__isl_keep isl_set *set)
1213 isl_printer *printer;
1218 printer = isl_printer_to_file(isl_set_get_ctx(set), stderr);
1219 printer = isl_printer_print_set(printer, set);
1220 printer = isl_printer_end_line(printer);
1222 isl_printer_free(printer);
1225 void isl_union_map_dump(__isl_keep isl_union_map *umap)
1227 isl_printer *printer;
1232 printer = isl_printer_to_file(isl_union_map_get_ctx(umap), stderr);
1233 printer = isl_printer_print_union_map(printer, umap);
1234 printer = isl_printer_end_line(printer);
1236 isl_printer_free(printer);
1239 void isl_union_set_dump(__isl_keep isl_union_set *uset)
1241 isl_printer *printer;
1246 printer = isl_printer_to_file(isl_union_set_get_ctx(uset), stderr);
1247 printer = isl_printer_print_union_set(printer, uset);
1248 printer = isl_printer_end_line(printer);
1250 isl_printer_free(printer);
1253 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial *upwqp)
1255 isl_printer *printer;
1260 printer = isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp),
1262 printer = isl_printer_print_union_pw_qpolynomial(printer, upwqp);
1263 printer = isl_printer_end_line(printer);
1265 isl_printer_free(printer);
1268 void isl_union_pw_qpolynomial_fold_dump(
1269 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1271 isl_printer *printer;
1276 printer = isl_printer_to_file(
1277 isl_union_pw_qpolynomial_fold_get_ctx(upwf), stderr);
1278 printer = isl_printer_print_union_pw_qpolynomial_fold(printer, upwf);
1279 printer = isl_printer_end_line(printer);
1281 isl_printer_free(printer);
1284 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1289 for (i = 0, n = 0; i < rec->n; ++i)
1290 if (!isl_upoly_is_zero(rec->p[i]))
1296 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
1297 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1299 int c = p->output_format == ISL_FORMAT_C;
1300 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1301 p = print_affine_of_len(dim, div, p,
1302 div->row[pos] + 1, div->n_col - 1, 1);
1303 p = isl_printer_print_str(p, c ? ", " : ")/");
1304 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1305 p = isl_printer_print_str(p, c ? ")" : "]");
1309 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1310 __isl_take isl_printer *p, int first)
1312 struct isl_upoly_cst *cst;
1315 cst = isl_upoly_as_cst(up);
1318 neg = !first && isl_int_is_neg(cst->n);
1320 p = isl_printer_print_str(p, neg ? " - " : " + ");
1322 isl_int_neg(cst->n, cst->n);
1323 if (isl_int_is_zero(cst->d)) {
1324 int sgn = isl_int_sgn(cst->n);
1325 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1326 sgn == 0 ? "NaN" : "infty");
1328 p = isl_printer_print_isl_int(p, cst->n);
1330 isl_int_neg(cst->n, cst->n);
1331 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1332 p = isl_printer_print_str(p, "/");
1333 p = isl_printer_print_isl_int(p, cst->d);
1337 isl_printer_free(p);
1341 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1342 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
1346 total = isl_dim_total(dim);
1348 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 1, 0);
1350 p = print_div(dim, div, var - total, p);
1354 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1355 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
1357 p = print_base(p, dim, div, var);
1360 if (p->output_format == ISL_FORMAT_C) {
1362 for (i = 1; i < exp; ++i) {
1363 p = isl_printer_print_str(p, "*");
1364 p = print_base(p, dim, div, var);
1367 p = isl_printer_print_str(p, "^");
1368 p = isl_printer_print_int(p, exp);
1373 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1374 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
1375 __isl_take isl_printer *p, int outer)
1377 int i, n, first, print_parens;
1378 struct isl_upoly_rec *rec;
1380 if (!p || !up || !dim || !div)
1383 if (isl_upoly_is_cst(up))
1384 return upoly_print_cst(up, p, 1);
1386 rec = isl_upoly_as_rec(up);
1389 n = upoly_rec_n_non_zero(rec);
1390 print_parens = n > 1 || (outer && rec->up.var >= isl_dim_total(dim));
1392 p = isl_printer_print_str(p, "(");
1393 for (i = 0, first = 1; i < rec->n; ++i) {
1394 if (isl_upoly_is_zero(rec->p[i]))
1396 if (isl_upoly_is_negone(rec->p[i])) {
1398 p = isl_printer_print_str(p, "-1");
1400 p = isl_printer_print_str(p, "-");
1402 p = isl_printer_print_str(p, " - ");
1403 } else if (isl_upoly_is_cst(rec->p[i]) &&
1404 !isl_upoly_is_one(rec->p[i]))
1405 p = upoly_print_cst(rec->p[i], p, first);
1408 p = isl_printer_print_str(p, " + ");
1409 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1410 p = upoly_print(rec->p[i], dim, div, p, 0);
1415 if (!isl_upoly_is_one(rec->p[i]) &&
1416 !isl_upoly_is_negone(rec->p[i]))
1417 p = isl_printer_print_str(p, " * ");
1418 p = print_pow(p, dim, div, rec->up.var, i);
1421 p = isl_printer_print_str(p, ")");
1424 isl_printer_free(p);
1428 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
1429 __isl_keep isl_qpolynomial *qp)
1433 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1436 isl_printer_free(p);
1440 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1441 unsigned output_format)
1448 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1449 p = isl_printer_to_file(qp->dim->ctx, out);
1450 p = isl_printer_print_qpolynomial(p, qp);
1451 isl_printer_free(p);
1454 static __isl_give isl_printer *qpolynomial_fold_print(
1455 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1459 if (fold->type == isl_fold_min)
1460 p = isl_printer_print_str(p, "min");
1461 else if (fold->type == isl_fold_max)
1462 p = isl_printer_print_str(p, "max");
1463 p = isl_printer_print_str(p, "(");
1464 for (i = 0; i < fold->n; ++i) {
1466 p = isl_printer_print_str(p, ", ");
1467 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
1469 p = isl_printer_print_str(p, ")");
1473 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1474 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1478 p = qpolynomial_fold_print(fold, p);
1481 isl_printer_free(p);
1485 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1486 FILE *out, unsigned output_format)
1493 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1495 p = isl_printer_to_file(fold->dim->ctx, out);
1496 p = isl_printer_print_qpolynomial_fold(p, fold);
1498 isl_printer_free(p);
1501 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1502 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1506 for (i = 0; i < pwqp->n; ++i) {
1508 p = isl_printer_print_str(p, "; ");
1509 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1510 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1511 p = print_dim(pwqp->p[i].set->dim, p, 1, 0, NULL);
1512 p = isl_printer_print_str(p, " -> ");
1514 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
1515 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1521 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1522 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1527 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
1528 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1529 p = isl_printer_print_str(p, " -> ");
1531 p = isl_printer_print_str(p, "{ ");
1533 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1534 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1535 p = print_dim(pwqp->dim, p, 1, 0, NULL);
1536 p = isl_printer_print_str(p, " -> ");
1538 p = isl_printer_print_str(p, "0");
1540 p = isl_pwqp_print_isl_body(p, pwqp);
1541 p = isl_printer_print_str(p, " }");
1544 isl_printer_free(p);
1548 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1549 unsigned output_format)
1556 p = isl_printer_to_file(pwqp->dim->ctx, out);
1557 p = isl_printer_set_output_format(p, output_format);
1558 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1560 isl_printer_free(p);
1563 static __isl_give isl_printer *isl_pwf_print_isl_body(
1564 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1568 for (i = 0; i < pwf->n; ++i) {
1570 p = isl_printer_print_str(p, "; ");
1571 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1572 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1573 p = print_dim(pwf->p[i].set->dim, p, 1, 0, NULL);
1574 p = isl_printer_print_str(p, " -> ");
1576 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1577 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1583 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1584 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1586 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1587 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1588 p = isl_printer_print_str(p, " -> ");
1590 p = isl_printer_print_str(p, "{ ");
1592 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1593 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1594 p = print_dim(pwf->dim, p, 1, 0, NULL);
1595 p = isl_printer_print_str(p, " -> ");
1597 p = isl_printer_print_str(p, "0");
1599 p = isl_pwf_print_isl_body(p, pwf);
1600 p = isl_printer_print_str(p, " }");
1604 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1605 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1607 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1608 __isl_keep isl_dim *dim,
1609 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1611 if (type == isl_dim_div) {
1612 p = isl_printer_print_str(p, "floord(");
1613 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1614 p = isl_printer_print_str(p, ", ");
1615 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1616 p = isl_printer_print_str(p, ")");
1620 name = isl_dim_get_name(dim, type, pos);
1623 p = isl_printer_print_str(p, name);
1628 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1629 __isl_keep isl_dim *dim,
1630 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1632 enum isl_dim_type type;
1633 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1636 return isl_printer_print_isl_int(p, c);
1638 if (isl_int_is_one(c))
1640 else if (isl_int_is_negone(c))
1641 p = isl_printer_print_str(p, "-");
1643 p = isl_printer_print_isl_int(p, c);
1644 p = isl_printer_print_str(p, "*");
1646 type = pos2type(dim, &pos);
1647 p = print_name_c(p, dim, bset, type, pos);
1651 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1652 __isl_keep isl_dim *dim,
1653 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1658 for (i = 0, first = 1; i < len; ++i) {
1660 if (isl_int_is_zero(c[i]))
1663 if (isl_int_is_neg(c[i])) {
1665 isl_int_neg(c[i], c[i]);
1666 p = isl_printer_print_str(p, " - ");
1668 p = isl_printer_print_str(p, " + ");
1671 p = print_term_c(p, dim, bset, c[i], i);
1673 isl_int_neg(c[i], c[i]);
1676 p = isl_printer_print_str(p, "0");
1680 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1681 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1683 unsigned len = 1 + isl_basic_set_total_dim(bset);
1684 return print_partial_affine_c(p, dim, bset, c, len);
1687 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1688 __isl_keep isl_dim *dim,
1689 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1692 p = isl_printer_print_str(p, " && ");
1694 p = print_affine_c(p, dim, bset, c);
1695 p = isl_printer_print_str(p, " ");
1696 p = isl_printer_print_str(p, op);
1697 p = isl_printer_print_str(p, " 0");
1701 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1702 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset)
1705 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1706 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1708 for (i = 0; i < bset->n_eq; ++i) {
1709 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1711 p = print_constraint_c(p, dim, bset,
1712 bset->eq[i], "==", !i);
1715 p = isl_printer_print_str(p, " && ");
1716 p = isl_printer_print_str(p, "(");
1717 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1719 p = isl_printer_print_str(p, ") % ");
1720 p = isl_printer_print_isl_int(p,
1721 bset->eq[i][1 + total + j]);
1722 p = isl_printer_print_str(p, " == 0");
1725 for (i = 0; i < bset->n_ineq; ++i)
1726 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1731 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1732 __isl_keep isl_dim *dim, __isl_keep isl_set *set)
1737 p = isl_printer_print_str(p, "0");
1739 for (i = 0; i < set->n; ++i) {
1741 p = isl_printer_print_str(p, " || ");
1743 p = isl_printer_print_str(p, "(");
1744 p = print_basic_set_c(p, dim, set->p[i]);
1746 p = isl_printer_print_str(p, ")");
1751 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1752 __isl_keep isl_dim *dim, __isl_keep isl_qpolynomial *qp)
1757 isl_qpolynomial_get_den(qp, &den);
1758 if (!isl_int_is_one(den)) {
1760 p = isl_printer_print_str(p, "(");
1761 qp = isl_qpolynomial_copy(qp);
1762 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1763 den, qp->dim->ctx->one);
1764 qp = isl_qpolynomial_mul(qp, f);
1767 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1768 if (!isl_int_is_one(den)) {
1769 p = isl_printer_print_str(p, ")/");
1770 p = isl_printer_print_isl_int(p, den);
1771 isl_qpolynomial_free(qp);
1777 static __isl_give isl_printer *print_pw_qpolynomial_c(
1778 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1782 if (pwqp->n == 1 && isl_set_fast_is_universe(pwqp->p[0].set))
1783 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1785 for (i = 0; i < pwqp->n; ++i) {
1786 p = isl_printer_print_str(p, "(");
1787 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1788 p = isl_printer_print_str(p, ") ? (");
1789 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1790 p = isl_printer_print_str(p, ") : ");
1793 p = isl_printer_print_str(p, "0");
1797 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1798 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1803 if (p->output_format == ISL_FORMAT_ISL)
1804 return print_pw_qpolynomial_isl(p, pwqp);
1805 else if (p->output_format == ISL_FORMAT_C)
1806 return print_pw_qpolynomial_c(p, pwqp);
1807 isl_assert(p->ctx, 0, goto error);
1809 isl_printer_free(p);
1813 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1815 struct isl_union_print_data *data;
1816 data = (struct isl_union_print_data *)user;
1819 data->p = isl_printer_print_str(data->p, "; ");
1822 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1823 isl_pw_qpolynomial_free(pwqp);
1828 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1829 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1831 struct isl_union_print_data data = { p, 1, 1 };
1833 dim = isl_union_pw_qpolynomial_get_dim(upwqp);
1834 if (isl_dim_size(dim, isl_dim_param) > 0) {
1835 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1836 p = isl_printer_print_str(p, " -> ");
1839 p = isl_printer_print_str(p, "{ ");
1840 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1843 p = isl_printer_print_str(p, " }");
1847 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1848 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1853 if (p->output_format == ISL_FORMAT_ISL)
1854 return print_union_pw_qpolynomial_isl(p, upwqp);
1855 isl_die(p->ctx, isl_error_invalid,
1856 "invalid output format for isl_union_pw_qpolynomial",
1859 isl_printer_free(p);
1863 static __isl_give isl_printer *print_qpolynomial_fold_c(
1864 __isl_take isl_printer *p, __isl_keep isl_dim *dim,
1865 __isl_keep isl_qpolynomial_fold *fold)
1869 for (i = 0; i < fold->n - 1; ++i)
1870 if (fold->type == isl_fold_min)
1871 p = isl_printer_print_str(p, "min(");
1872 else if (fold->type == isl_fold_max)
1873 p = isl_printer_print_str(p, "max(");
1875 for (i = 0; i < fold->n; ++i) {
1877 p = isl_printer_print_str(p, ", ");
1878 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1880 p = isl_printer_print_str(p, ")");
1885 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1886 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1890 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1891 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1893 for (i = 0; i < pwf->n; ++i) {
1894 p = isl_printer_print_str(p, "(");
1895 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1896 p = isl_printer_print_str(p, ") ? (");
1897 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1898 p = isl_printer_print_str(p, ") : ");
1901 p = isl_printer_print_str(p, "0");
1905 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1906 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1911 if (p->output_format == ISL_FORMAT_ISL)
1912 return print_pw_qpolynomial_fold_isl(p, pwf);
1913 else if (p->output_format == ISL_FORMAT_C)
1914 return print_pw_qpolynomial_fold_c(p, pwf);
1915 isl_assert(p->ctx, 0, goto error);
1917 isl_printer_free(p);
1921 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1922 FILE *out, unsigned output_format)
1929 p = isl_printer_to_file(pwf->dim->ctx, out);
1930 p = isl_printer_set_output_format(p, output_format);
1931 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1933 isl_printer_free(p);
1936 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1938 struct isl_union_print_data *data;
1939 data = (struct isl_union_print_data *)user;
1942 data->p = isl_printer_print_str(data->p, "; ");
1945 data->p = isl_pwf_print_isl_body(data->p, pwf);
1946 isl_pw_qpolynomial_fold_free(pwf);
1951 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1952 __isl_take isl_printer *p,
1953 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1955 struct isl_union_print_data data = { p, 1, 1 };
1957 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
1958 if (isl_dim_size(dim, isl_dim_param) > 0) {
1959 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1960 p = isl_printer_print_str(p, " -> ");
1963 p = isl_printer_print_str(p, "{ ");
1964 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1965 &print_pwf_body, &data);
1967 p = isl_printer_print_str(p, " }");
1971 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1972 __isl_take isl_printer *p,
1973 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1978 if (p->output_format == ISL_FORMAT_ISL)
1979 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1980 isl_die(p->ctx, isl_error_invalid,
1981 "invalid output format for isl_union_pw_qpolynomial_fold",
1984 isl_printer_free(p);