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
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_printer_private.h>
21 #include <isl_dim_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/union_map.h>
24 #include <isl/constraint.h>
26 static const char *s_to[2] = { " -> ", " \\to " };
27 static const char *s_and[2] = { " and ", " \\wedge " };
28 static const char *s_or[2] = { " or ", " \\vee " };
29 static const char *s_le[2] = { "<=", "\\le" };
30 static const char *s_ge[2] = { ">=", "\\ge" };
31 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
32 static const char *s_close_set[2] = { " }", " \\,\\}" };
33 static const char *s_open_list[2] = { "[", "(" };
34 static const char *s_close_list[2] = { "]", ")" };
35 static const char *s_such_that[2] = { " : ", " \\mid " };
36 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
37 static const char *s_close_exists[2] = { ")", "" };
38 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
39 static const char *s_param_prefix[2] = { "p", "p_" };
40 static const char *s_input_prefix[2] = { "i", "i_" };
41 static const char *s_output_prefix[2] = { "o", "o_" };
43 static __isl_give isl_printer *print_constraint_polylib(
44 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
47 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
48 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
49 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
50 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
52 p = isl_printer_start_line(p);
53 p = isl_printer_print_int(p, ineq);
54 for (i = 0; i < n_out; ++i) {
55 p = isl_printer_print_str(p, " ");
56 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
58 for (i = 0; i < n_in; ++i) {
59 p = isl_printer_print_str(p, " ");
60 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
62 for (i = 0; i < bmap->n_div; ++i) {
63 p = isl_printer_print_str(p, " ");
64 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
66 for (i = 0; i < nparam; ++i) {
67 p = isl_printer_print_str(p, " ");
68 p = isl_printer_print_isl_int(p, c[1+i]);
70 p = isl_printer_print_str(p, " ");
71 p = isl_printer_print_isl_int(p, c[0]);
72 p = isl_printer_end_line(p);
76 static __isl_give isl_printer *print_constraints_polylib(
77 struct isl_basic_map *bmap, __isl_take isl_printer *p)
81 p = isl_printer_set_isl_int_width(p, 5);
83 for (i = 0; i < bmap->n_eq; ++i)
84 p = print_constraint_polylib(bmap, 0, i, p);
85 for (i = 0; i < bmap->n_ineq; ++i)
86 p = print_constraint_polylib(bmap, 1, i, p);
91 static __isl_give isl_printer *bset_print_constraints_polylib(
92 struct isl_basic_set *bset, __isl_take isl_printer *p)
94 return print_constraints_polylib((struct isl_basic_map *)bset, p);
97 static __isl_give isl_printer *isl_basic_map_print_polylib(
98 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
100 unsigned total = isl_basic_map_total_dim(bmap);
101 p = isl_printer_start_line(p);
102 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
103 p = isl_printer_print_str(p, " ");
104 p = isl_printer_print_int(p, 1 + total + 1);
106 p = isl_printer_print_str(p, " ");
107 p = isl_printer_print_int(p,
108 isl_basic_map_dim(bmap, isl_dim_out));
109 p = isl_printer_print_str(p, " ");
110 p = isl_printer_print_int(p,
111 isl_basic_map_dim(bmap, isl_dim_in));
112 p = isl_printer_print_str(p, " ");
113 p = isl_printer_print_int(p,
114 isl_basic_map_dim(bmap, isl_dim_div));
115 p = isl_printer_print_str(p, " ");
116 p = isl_printer_print_int(p,
117 isl_basic_map_dim(bmap, isl_dim_param));
119 p = isl_printer_end_line(p);
120 return print_constraints_polylib(bmap, p);
123 static __isl_give isl_printer *isl_basic_set_print_polylib(
124 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
126 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
129 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
130 __isl_take isl_printer *p, int ext)
134 p = isl_printer_start_line(p);
135 p = isl_printer_print_int(p, map->n);
136 p = isl_printer_end_line(p);
137 for (i = 0; i < map->n; ++i) {
138 p = isl_printer_start_line(p);
139 p = isl_printer_end_line(p);
140 p = isl_basic_map_print_polylib(map->p[i], p, ext);
145 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
146 __isl_take isl_printer *p, int ext)
148 return isl_map_print_polylib((struct isl_map *)set, p, ext);
151 static int count_same_name(__isl_keep isl_dim *dim,
152 enum isl_dim_type type, unsigned pos, const char *name)
158 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
159 s = t == type ? pos : isl_dim_size(dim, t);
160 for (p = 0; p < s; ++p) {
161 const char *n = isl_dim_get_name(dim, t, p);
162 if (n && !strcmp(n, name))
169 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
170 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
177 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
181 if (type == isl_dim_param)
182 prefix = s_param_prefix[latex];
183 else if (type == isl_dim_div)
184 prefix = s_div_prefix[latex];
185 else if (set || type == isl_dim_in)
186 prefix = s_input_prefix[latex];
188 prefix = s_output_prefix[latex];
189 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
192 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
194 p = isl_printer_print_str(p, name);
196 p = isl_printer_print_str(p, "'");
200 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
202 enum isl_dim_type type;
203 unsigned n_in = isl_dim_size(dim, isl_dim_in);
204 unsigned n_out = isl_dim_size(dim, isl_dim_out);
205 unsigned nparam = isl_dim_size(dim, isl_dim_param);
207 if (*pos < 1 + nparam) {
208 type = isl_dim_param;
210 } else if (*pos < 1 + nparam + n_in) {
213 } else if (*pos < 1 + nparam + n_in + n_out) {
215 *pos -= 1 + nparam + n_in;
218 *pos -= 1 + nparam + n_in + n_out;
224 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
225 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
227 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
228 __isl_keep isl_mat *div,
229 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
231 enum isl_dim_type type;
235 return isl_printer_print_isl_int(p, c);
237 type = pos2type(dim, &pos);
238 print_div_def = type == isl_dim_div && div &&
239 !isl_int_is_zero(div->row[pos][0]);
241 if (isl_int_is_one(c))
243 else if (isl_int_is_negone(c))
244 p = isl_printer_print_str(p, "-");
246 p = isl_printer_print_isl_int(p, c);
247 if (p->output_format == ISL_FORMAT_C || print_div_def)
248 p = isl_printer_print_str(p, "*");
251 p = print_div(dim, div, pos, p);
253 p = print_name(dim, p, type, pos, set, latex);
257 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
258 __isl_keep isl_mat *div,
259 __isl_take isl_printer *p, isl_int *c, int len, int set)
264 for (i = 0, first = 1; i < len; ++i) {
266 if (isl_int_is_zero(c[i]))
269 if (isl_int_is_neg(c[i])) {
271 isl_int_neg(c[i], c[i]);
272 p = isl_printer_print_str(p, " - ");
274 p = isl_printer_print_str(p, " + ");
277 p = print_term(dim, div, c[i], i, p, set, 0);
279 isl_int_neg(c[i], c[i]);
282 p = isl_printer_print_str(p, "0");
286 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
287 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
289 unsigned len = 1 + isl_basic_map_total_dim(bmap);
290 return print_affine_of_len(dim, NULL, p, c, len, set);
293 static int defining_equality(__isl_keep isl_basic_map *eq,
294 __isl_keep isl_dim *dim, enum isl_dim_type type, int pos)
302 pos += isl_dim_offset(dim, type);
303 total = isl_basic_map_total_dim(eq);
305 for (i = 0; i < eq->n_eq; ++i) {
306 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
308 if (isl_int_is_one(eq->eq[i][1 + pos]))
309 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
316 /* offset is the offset of local_dim inside global_type of global_dim.
318 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
319 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
320 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
321 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
325 if (global_dim != local_dim && local_type == isl_dim_out)
326 offset += local_dim->n_in;
328 for (i = 0; i < isl_dim_size(local_dim, local_type); ++i) {
330 p = isl_printer_print_str(p, ", ");
331 j = defining_equality(eq, global_dim, global_type, offset + i);
333 int pos = 1 + isl_dim_offset(global_dim, global_type)
335 p = print_affine_of_len(eq->dim, NULL,
336 p, eq->eq[j], pos, set);
338 p = print_name(global_dim, p, global_type, offset + i,
345 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
346 __isl_take isl_printer *p, enum isl_dim_type type,
347 int set, int latex, __isl_keep isl_basic_map *eq)
349 return print_nested_var_list(p, dim, type, dim, type, set, latex, eq, 0);
352 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
353 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
354 __isl_keep isl_dim *local_dim,
355 int set, int latex, __isl_keep isl_basic_map *eq, int offset);
357 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
358 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
359 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
360 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
362 const char *name = NULL;
363 unsigned n = isl_dim_size(local_dim, local_type);
364 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
365 name = isl_dim_get_tuple_name(local_dim, local_type);
368 p = isl_printer_print_str(p, "\\mathrm{");
369 p = isl_printer_print_str(p, name);
371 p = isl_printer_print_str(p, "}");
374 if (!latex || n != 1 || name)
375 p = isl_printer_print_str(p, s_open_list[latex]);
376 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
377 local_dim->nested[local_type - isl_dim_in]) {
378 if (global_dim != local_dim && local_type == isl_dim_out)
379 offset += local_dim->n_in;
380 p = print_nested_map_dim(p, global_dim, global_type,
381 local_dim->nested[local_type - isl_dim_in],
382 set, latex, eq, offset);
384 p = print_nested_var_list(p, global_dim, global_type,
385 local_dim, local_type, set, latex, eq, offset);
386 if (!latex || n != 1 || name)
387 p = isl_printer_print_str(p, s_close_list[latex]);
391 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
392 __isl_take isl_printer *p, enum isl_dim_type type,
393 int set, int latex, __isl_keep isl_basic_map *eq)
395 return print_nested_tuple(p, dim, type, dim, type, set, latex, eq, 0);
398 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
399 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
400 __isl_keep isl_dim *local_dim,
401 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
403 p = print_nested_tuple(p, global_dim, global_type,
404 local_dim, isl_dim_in, set, latex, eq, offset);
405 p = isl_printer_print_str(p, s_to[latex]);
406 p = print_nested_tuple(p, global_dim, global_type,
407 local_dim, isl_dim_out, set, latex, eq, offset);
412 static __isl_give isl_printer *print_dim(__isl_keep isl_dim *dim,
413 __isl_take isl_printer *p, int set, int latex, int rational,
414 __isl_keep isl_basic_map *eq)
416 if (rational && !latex)
417 p = isl_printer_print_str(p, "rat: ");
419 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
421 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
422 p = isl_printer_print_str(p, s_to[latex]);
423 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
429 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
430 __isl_take isl_printer *p)
432 if (isl_dim_size(dim, isl_dim_param) == 0)
435 p = isl_printer_start_line(p);
436 p = isl_printer_print_str(p, "symbolic ");
437 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
438 p = isl_printer_print_str(p, ";");
439 p = isl_printer_end_line(p);
443 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
444 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
445 isl_int *c, int last, const char *op, int first_constraint, int set,
448 if (!first_constraint)
449 p = isl_printer_print_str(p, s_and[latex]);
451 isl_int_abs(c[last], c[last]);
453 p = print_term(dim, NULL, c[last], last, p, set, latex);
455 p = isl_printer_print_str(p, " ");
456 p = isl_printer_print_str(p, op);
457 p = isl_printer_print_str(p, " ");
459 isl_int_set_si(c[last], 0);
460 p = print_affine(bmap, dim, p, c, set);
465 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
466 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
470 unsigned total = isl_basic_map_total_dim(bmap);
472 c = isl_vec_alloc(bmap->ctx, 1 + total);
476 for (i = bmap->n_eq - 1; i >= 0; --i) {
477 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
479 if (i != bmap->n_eq - 1)
480 p = isl_printer_print_str(p, s_and[latex]);
481 p = isl_printer_print_str(p, "0 = 0");
484 if (isl_int_is_neg(bmap->eq[i][l]))
485 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
487 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
488 p = print_constraint(bmap, dim, p, c->el, l,
489 "=", i == bmap->n_eq - 1, set, latex);
491 for (i = 0; i < bmap->n_ineq; ++i) {
492 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
497 s = isl_int_sgn(bmap->ineq[i][l]);
499 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
501 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
502 op = s < 0 ? s_le[latex] : s_ge[latex];
503 p = print_constraint(bmap, dim, p, c->el, l,
504 op, !bmap->n_eq && !i, set, latex);
516 static __isl_give isl_printer *print_omega_constraints(
517 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
519 if (bmap->n_eq + bmap->n_ineq == 0)
522 p = isl_printer_print_str(p, ": ");
523 if (bmap->n_div > 0) {
525 p = isl_printer_print_str(p, "exists (");
526 for (i = 0; i < bmap->n_div; ++i) {
528 p = isl_printer_print_str(p, ", ");
529 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
531 p = isl_printer_print_str(p, ": ");
533 p = print_constraints(bmap, bmap->dim, p, set, 0);
535 p = isl_printer_print_str(p, ")");
539 static __isl_give isl_printer *basic_map_print_omega(
540 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
542 p = isl_printer_print_str(p, "{ [");
543 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
544 p = isl_printer_print_str(p, "] -> [");
545 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
546 p = isl_printer_print_str(p, "] ");
547 p = print_omega_constraints(bmap, p, 0);
548 p = isl_printer_print_str(p, " }");
552 static __isl_give isl_printer *isl_basic_map_print_omega(
553 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
555 p = print_omega_parameters(bmap->dim, p);
557 p = isl_printer_start_line(p);
558 p = basic_map_print_omega(bmap, p);
559 p = isl_printer_end_line(p);
563 static __isl_give isl_printer *basic_set_print_omega(
564 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
566 p = isl_printer_print_str(p, "{ [");
567 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
568 p = isl_printer_print_str(p, "] ");
569 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
570 p = isl_printer_print_str(p, " }");
574 static __isl_give isl_printer *isl_basic_set_print_omega(
575 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
577 p = print_omega_parameters(bset->dim, p);
579 p = isl_printer_start_line(p);
580 p = basic_set_print_omega(bset, p);
581 p = isl_printer_end_line(p);
585 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
586 __isl_take isl_printer *p)
590 p = print_omega_parameters(map->dim, p);
592 p = isl_printer_start_line(p);
593 for (i = 0; i < map->n; ++i) {
595 p = isl_printer_print_str(p, " union ");
596 p = basic_map_print_omega(map->p[i], p);
598 p = isl_printer_end_line(p);
602 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
603 __isl_take isl_printer *p)
607 p = print_omega_parameters(set->dim, p);
609 p = isl_printer_start_line(p);
610 for (i = 0; i < set->n; ++i) {
612 p = isl_printer_print_str(p, " union ");
613 p = basic_set_print_omega(set->p[i], p);
615 p = isl_printer_end_line(p);
619 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
620 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
622 if (bmap->n_div > 0) {
624 p = isl_printer_print_str(p, s_open_exists[latex]);
625 for (i = 0; i < bmap->n_div; ++i) {
627 p = isl_printer_print_str(p, ", ");
628 p = print_name(dim, p, isl_dim_div, i, 0, latex);
629 if (latex || isl_int_is_zero(bmap->div[i][0]))
631 p = isl_printer_print_str(p, " = [(");
632 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
633 p = isl_printer_print_str(p, ")/");
634 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
635 p = isl_printer_print_str(p, "]");
637 p = isl_printer_print_str(p, ": ");
640 p = print_constraints(bmap, dim, p, set, latex);
643 p = isl_printer_print_str(p, s_close_exists[latex]);
647 static __isl_give isl_printer *isl_basic_map_print_isl(
648 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
651 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
652 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
653 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
654 p = isl_printer_print_str(p, " -> ");
656 p = isl_printer_print_str(p, "{ ");
657 p = print_dim(bmap->dim, p, set, latex, rational, NULL);
658 p = isl_printer_print_str(p, " : ");
659 p = print_disjunct(bmap, bmap->dim, p, set, latex);
660 p = isl_printer_print_str(p, " }");
664 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
665 __isl_take isl_printer *p, int set, int latex)
669 if (isl_map_fast_is_universe(map))
672 p = isl_printer_print_str(p, s_such_that[latex]);
674 p = isl_printer_print_str(p, "1 = 0");
675 for (i = 0; i < map->n; ++i) {
677 p = isl_printer_print_str(p, s_or[latex]);
678 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
679 p = isl_printer_print_str(p, "(");
680 p = print_disjunct(map->p[i], map->dim, p, set, latex);
681 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
682 p = isl_printer_print_str(p, ")");
687 struct isl_aff_split {
692 static void free_split(__isl_take struct isl_aff_split *split, int n)
699 for (i = 0; i < n; ++i) {
700 isl_basic_map_free(split[i].aff);
701 isl_map_free(split[i].map);
707 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
710 unsigned nparam, n_in, n_out, total;
712 bmap = isl_basic_map_cow(bmap);
715 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
718 nparam = isl_basic_map_dim(bmap, isl_dim_param);
719 n_in = isl_basic_map_dim(bmap, isl_dim_in);
720 n_out = isl_basic_map_dim(bmap, isl_dim_out);
721 total = isl_basic_map_dim(bmap, isl_dim_all);
722 for (i = bmap->n_eq - 1; i >= 0; --i) {
723 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
724 if (j >= nparam && j < nparam + n_in + n_out &&
725 (isl_int_is_one(bmap->eq[i][1 + j]) ||
726 isl_int_is_negone(bmap->eq[i][1 + j])))
728 if (isl_basic_map_drop_equality(bmap, i) < 0)
732 bmap = isl_basic_map_finalize(bmap);
736 isl_basic_map_free(bmap);
740 static int aff_split_cmp(const void *p1, const void *p2)
742 const struct isl_aff_split *s1, *s2;
743 s1 = (const struct isl_aff_split *) p1;
744 s2 = (const struct isl_aff_split *) p2;
746 return isl_basic_map_fast_cmp(s1->aff, s2->aff);
749 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
750 __isl_keep isl_basic_map *aff)
758 total = isl_dim_total(bmap->dim);
760 for (i = bmap->n_eq - 1; i >= 0; --i) {
761 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
764 for (j = 0; j < aff->n_eq; ++j) {
765 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
766 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
768 if (isl_basic_map_drop_equality(bmap, i) < 0)
776 isl_basic_map_free(bmap);
780 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
783 struct isl_aff_split *split;
785 split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
789 for (i = 0; i < map->n; ++i) {
791 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
792 bmap = isl_basic_map_copy(map->p[i]);
793 bmap = isl_basic_map_cow(bmap);
794 bmap = drop_aff(bmap, split[i].aff);
795 split[i].map = isl_map_from_basic_map(bmap);
796 if (!split[i].aff || !split[i].map)
800 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
803 for (i = n - 1; i >= 1; --i) {
804 if (!isl_basic_map_fast_is_equal(split[i - 1].aff,
807 isl_basic_map_free(split[i].aff);
808 split[i - 1].map = isl_map_union(split[i - 1].map,
811 split[i] = split[n - 1];
812 split[n - 1].aff = NULL;
813 split[n - 1].map = NULL;
819 free_split(split, map->n);
823 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
824 struct isl_aff_split *split, int n, int set)
829 for (i = 0; i < n; ++i) {
834 dim = split[i].map->dim;
835 rational = split[i].map->n > 0 &&
836 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
838 p = isl_printer_print_str(p, "; ");
839 p = print_dim(dim, p, set, 0, rational, split[i].aff);
840 p = print_disjuncts(split[i].map, p, set, 0);
846 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
847 __isl_take isl_printer *p, int set)
849 struct isl_aff_split *split = NULL;
853 split = split_aff(map);
855 p = print_split_map(p, split, map->n, set);
857 rational = map->n > 0 &&
858 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
859 p = print_dim(map->dim, p, set, 0, rational, NULL);
860 p = print_disjuncts(map, p, set, 0);
862 free_split(split, map->n);
866 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
867 __isl_take isl_printer *p, int set)
869 if (isl_map_dim(map, isl_dim_param) > 0) {
870 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
871 p = isl_printer_print_str(p, s_to[0]);
873 p = isl_printer_print_str(p, s_open_set[0]);
874 p = isl_map_print_isl_body(map, p, set);
875 p = isl_printer_print_str(p, s_close_set[0]);
879 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
880 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
882 if (isl_map_dim(map, isl_dim_param) > 0) {
883 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
884 p = isl_printer_print_str(p, s_to[1]);
886 p = isl_printer_print_str(p, s_open_set[1]);
887 p = print_dim(map->dim, p, set, 1, 0, aff);
888 p = print_disjuncts(map, p, set, 1);
889 p = isl_printer_print_str(p, s_close_set[1]);
894 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
895 __isl_take isl_printer *p, int set)
898 struct isl_aff_split *split = NULL;
901 split = split_aff(map);
904 return print_latex_map(map, p, NULL, set);
906 for (i = 0; i < map->n; ++i) {
910 p = isl_printer_print_str(p, " \\cup ");
911 p = print_latex_map(split[i].map, p, split[i].aff, set);
914 free_split(split, map->n);
918 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
919 __isl_keep isl_basic_map *bmap)
923 if (p->output_format == ISL_FORMAT_ISL)
924 return isl_basic_map_print_isl(bmap, p, 0, 0);
925 else if (p->output_format == ISL_FORMAT_OMEGA)
926 return isl_basic_map_print_omega(bmap, p);
927 isl_assert(bmap->ctx, 0, goto error);
933 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
934 const char *prefix, const char *suffix, unsigned output_format)
936 isl_printer *printer;
941 printer = isl_printer_to_file(bmap->ctx, out);
942 printer = isl_printer_set_indent(printer, indent);
943 printer = isl_printer_set_prefix(printer, prefix);
944 printer = isl_printer_set_suffix(printer, suffix);
945 printer = isl_printer_set_output_format(printer, output_format);
946 isl_printer_print_basic_map(printer, bmap);
948 isl_printer_free(printer);
951 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
952 __isl_keep isl_basic_set *bset)
957 if (p->output_format == ISL_FORMAT_ISL)
958 return isl_basic_map_print_isl(bset, p, 1, 0);
959 else if (p->output_format == ISL_FORMAT_POLYLIB)
960 return isl_basic_set_print_polylib(bset, p, 0);
961 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
962 return isl_basic_set_print_polylib(bset, p, 1);
963 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
964 return bset_print_constraints_polylib(bset, p);
965 else if (p->output_format == ISL_FORMAT_OMEGA)
966 return isl_basic_set_print_omega(bset, p);
967 isl_assert(p->ctx, 0, goto error);
973 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
974 const char *prefix, const char *suffix, unsigned output_format)
976 isl_printer *printer;
981 printer = isl_printer_to_file(bset->ctx, out);
982 printer = isl_printer_set_indent(printer, indent);
983 printer = isl_printer_set_prefix(printer, prefix);
984 printer = isl_printer_set_suffix(printer, suffix);
985 printer = isl_printer_set_output_format(printer, output_format);
986 isl_printer_print_basic_set(printer, bset);
988 isl_printer_free(printer);
991 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
992 __isl_keep isl_set *set)
996 if (p->output_format == ISL_FORMAT_ISL)
997 return isl_map_print_isl((isl_map *)set, p, 1);
998 else if (p->output_format == ISL_FORMAT_POLYLIB)
999 return isl_set_print_polylib(set, p, 0);
1000 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1001 return isl_set_print_polylib(set, p, 1);
1002 else if (p->output_format == ISL_FORMAT_OMEGA)
1003 return isl_set_print_omega(set, p);
1004 else if (p->output_format == ISL_FORMAT_LATEX)
1005 return isl_map_print_latex((isl_map *)set, p, 1);
1006 isl_assert(set->ctx, 0, goto error);
1008 isl_printer_free(p);
1012 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1013 unsigned output_format)
1015 isl_printer *printer;
1020 printer = isl_printer_to_file(set->ctx, out);
1021 printer = isl_printer_set_indent(printer, indent);
1022 printer = isl_printer_set_output_format(printer, output_format);
1023 printer = isl_printer_print_set(printer, set);
1025 isl_printer_free(printer);
1028 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1029 __isl_keep isl_map *map)
1034 if (p->output_format == ISL_FORMAT_ISL)
1035 return isl_map_print_isl(map, p, 0);
1036 else if (p->output_format == ISL_FORMAT_POLYLIB)
1037 return isl_map_print_polylib(map, p, 0);
1038 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1039 return isl_map_print_polylib(map, p, 1);
1040 else if (p->output_format == ISL_FORMAT_OMEGA)
1041 return isl_map_print_omega(map, p);
1042 else if (p->output_format == ISL_FORMAT_LATEX)
1043 return isl_map_print_latex(map, p, 0);
1044 isl_assert(map->ctx, 0, goto error);
1046 isl_printer_free(p);
1050 struct isl_union_print_data {
1056 static int print_map_body(__isl_take isl_map *map, void *user)
1058 struct isl_union_print_data *data;
1059 data = (struct isl_union_print_data *)user;
1062 data->p = isl_printer_print_str(data->p, "; ");
1065 data->p = isl_map_print_isl_body(map, data->p, data->set);
1071 static __isl_give isl_printer *isl_union_map_print_isl(
1072 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1074 struct isl_union_print_data data = { p, set, 1 };
1076 dim = isl_union_map_get_dim(umap);
1077 if (isl_dim_size(dim, isl_dim_param) > 0) {
1078 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1079 p = isl_printer_print_str(p, s_to[0]);
1082 p = isl_printer_print_str(p, s_open_set[0]);
1083 isl_union_map_foreach_map(umap, &print_map_body, &data);
1085 p = isl_printer_print_str(p, s_close_set[0]);
1089 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1091 struct isl_union_print_data *data;
1092 data = (struct isl_union_print_data *)user;
1095 data->p = isl_printer_print_str(data->p, " \\cup ");
1098 data->p = isl_map_print_latex(map, data->p, data->set);
1104 static __isl_give isl_printer *isl_union_map_print_latex(
1105 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1107 struct isl_union_print_data data = { p, set, 1 };
1108 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1113 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1114 __isl_keep isl_union_map *umap)
1119 if (p->output_format == ISL_FORMAT_ISL)
1120 return isl_union_map_print_isl(umap, p, 0);
1121 if (p->output_format == ISL_FORMAT_LATEX)
1122 return isl_union_map_print_latex(umap, p, 0);
1124 isl_die(p->ctx, isl_error_invalid,
1125 "invalid output format for isl_union_map", goto error);
1127 isl_printer_free(p);
1131 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1132 __isl_keep isl_union_set *uset)
1137 if (p->output_format == ISL_FORMAT_ISL)
1138 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1139 if (p->output_format == ISL_FORMAT_LATEX)
1140 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1142 isl_die(p->ctx, isl_error_invalid,
1143 "invalid output format for isl_union_set", goto error);
1145 isl_printer_free(p);
1149 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1150 unsigned output_format)
1152 isl_printer *printer;
1157 printer = isl_printer_to_file(map->ctx, out);
1158 printer = isl_printer_set_indent(printer, indent);
1159 printer = isl_printer_set_output_format(printer, output_format);
1160 printer = isl_printer_print_map(printer, map);
1162 isl_printer_free(printer);
1165 void isl_basic_map_dump(__isl_keep isl_basic_map *bmap)
1167 isl_printer *printer;
1172 printer = isl_printer_to_file(isl_basic_map_get_ctx(bmap), stderr);
1173 printer = isl_printer_print_basic_map(printer, bmap);
1174 printer = isl_printer_end_line(printer);
1176 isl_printer_free(printer);
1179 void isl_basic_set_dump(__isl_keep isl_basic_set *bset)
1181 isl_printer *printer;
1186 printer = isl_printer_to_file(isl_basic_set_get_ctx(bset), stderr);
1187 printer = isl_printer_print_basic_set(printer, bset);
1188 printer = isl_printer_end_line(printer);
1190 isl_printer_free(printer);
1193 void isl_map_dump(__isl_keep isl_map *map)
1195 isl_printer *printer;
1200 printer = isl_printer_to_file(isl_map_get_ctx(map), stderr);
1201 printer = isl_printer_print_map(printer, map);
1202 printer = isl_printer_end_line(printer);
1204 isl_printer_free(printer);
1207 void isl_set_dump(__isl_keep isl_set *set)
1209 isl_printer *printer;
1214 printer = isl_printer_to_file(isl_set_get_ctx(set), stderr);
1215 printer = isl_printer_print_set(printer, set);
1216 printer = isl_printer_end_line(printer);
1218 isl_printer_free(printer);
1221 void isl_union_map_dump(__isl_keep isl_union_map *umap)
1223 isl_printer *printer;
1228 printer = isl_printer_to_file(isl_union_map_get_ctx(umap), stderr);
1229 printer = isl_printer_print_union_map(printer, umap);
1230 printer = isl_printer_end_line(printer);
1232 isl_printer_free(printer);
1235 void isl_union_set_dump(__isl_keep isl_union_set *uset)
1237 isl_printer *printer;
1242 printer = isl_printer_to_file(isl_union_set_get_ctx(uset), stderr);
1243 printer = isl_printer_print_union_set(printer, uset);
1244 printer = isl_printer_end_line(printer);
1246 isl_printer_free(printer);
1249 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial *upwqp)
1251 isl_printer *printer;
1256 printer = isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp),
1258 printer = isl_printer_print_union_pw_qpolynomial(printer, upwqp);
1259 printer = isl_printer_end_line(printer);
1261 isl_printer_free(printer);
1264 void isl_qpolynomial_dump(__isl_keep isl_qpolynomial *qp)
1266 isl_printer *printer;
1271 printer = isl_printer_to_file(isl_qpolynomial_get_ctx(qp), stderr);
1272 printer = isl_printer_print_qpolynomial(printer, qp);
1273 printer = isl_printer_end_line(printer);
1275 isl_printer_free(printer);
1278 void isl_qpolynomial_fold_dump(__isl_keep isl_qpolynomial_fold *fold)
1280 isl_printer *printer;
1285 printer = isl_printer_to_file(isl_qpolynomial_fold_get_ctx(fold),
1287 printer = isl_printer_print_qpolynomial_fold(printer, fold);
1288 printer = isl_printer_end_line(printer);
1290 isl_printer_free(printer);
1293 void isl_pw_qpolynomial_dump(__isl_keep isl_pw_qpolynomial *pwqp)
1295 isl_printer *printer;
1300 printer = isl_printer_to_file(isl_pw_qpolynomial_get_ctx(pwqp), stderr);
1301 printer = isl_printer_print_pw_qpolynomial(printer, pwqp);
1302 printer = isl_printer_end_line(printer);
1304 isl_printer_free(printer);
1307 void isl_pw_qpolynomial_fold_dump(__isl_keep isl_pw_qpolynomial_fold *pwf)
1309 isl_printer *printer;
1314 printer = isl_printer_to_file(
1315 isl_pw_qpolynomial_fold_get_ctx(pwf), stderr);
1316 printer = isl_printer_print_pw_qpolynomial_fold(printer, pwf);
1317 printer = isl_printer_end_line(printer);
1319 isl_printer_free(printer);
1322 void isl_union_pw_qpolynomial_fold_dump(
1323 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1325 isl_printer *printer;
1330 printer = isl_printer_to_file(
1331 isl_union_pw_qpolynomial_fold_get_ctx(upwf), stderr);
1332 printer = isl_printer_print_union_pw_qpolynomial_fold(printer, upwf);
1333 printer = isl_printer_end_line(printer);
1335 isl_printer_free(printer);
1338 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1343 for (i = 0, n = 0; i < rec->n; ++i)
1344 if (!isl_upoly_is_zero(rec->p[i]))
1350 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
1351 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1353 int c = p->output_format == ISL_FORMAT_C;
1354 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1355 p = print_affine_of_len(dim, div, p,
1356 div->row[pos] + 1, div->n_col - 1, 1);
1357 p = isl_printer_print_str(p, c ? ", " : ")/");
1358 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1359 p = isl_printer_print_str(p, c ? ")" : "]");
1363 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1364 __isl_take isl_printer *p, int first)
1366 struct isl_upoly_cst *cst;
1369 cst = isl_upoly_as_cst(up);
1372 neg = !first && isl_int_is_neg(cst->n);
1374 p = isl_printer_print_str(p, neg ? " - " : " + ");
1376 isl_int_neg(cst->n, cst->n);
1377 if (isl_int_is_zero(cst->d)) {
1378 int sgn = isl_int_sgn(cst->n);
1379 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1380 sgn == 0 ? "NaN" : "infty");
1382 p = isl_printer_print_isl_int(p, cst->n);
1384 isl_int_neg(cst->n, cst->n);
1385 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1386 p = isl_printer_print_str(p, "/");
1387 p = isl_printer_print_isl_int(p, cst->d);
1391 isl_printer_free(p);
1395 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1396 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
1400 total = isl_dim_total(dim);
1402 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 1, 0);
1404 p = print_div(dim, div, var - total, p);
1408 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1409 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
1411 p = print_base(p, dim, div, var);
1414 if (p->output_format == ISL_FORMAT_C) {
1416 for (i = 1; i < exp; ++i) {
1417 p = isl_printer_print_str(p, "*");
1418 p = print_base(p, dim, div, var);
1421 p = isl_printer_print_str(p, "^");
1422 p = isl_printer_print_int(p, exp);
1427 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1428 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
1429 __isl_take isl_printer *p, int outer)
1431 int i, n, first, print_parens;
1432 struct isl_upoly_rec *rec;
1434 if (!p || !up || !dim || !div)
1437 if (isl_upoly_is_cst(up))
1438 return upoly_print_cst(up, p, 1);
1440 rec = isl_upoly_as_rec(up);
1443 n = upoly_rec_n_non_zero(rec);
1444 print_parens = n > 1 || (outer && rec->up.var >= isl_dim_total(dim));
1446 p = isl_printer_print_str(p, "(");
1447 for (i = 0, first = 1; i < rec->n; ++i) {
1448 if (isl_upoly_is_zero(rec->p[i]))
1450 if (isl_upoly_is_negone(rec->p[i])) {
1452 p = isl_printer_print_str(p, "-1");
1454 p = isl_printer_print_str(p, "-");
1456 p = isl_printer_print_str(p, " - ");
1457 } else if (isl_upoly_is_cst(rec->p[i]) &&
1458 !isl_upoly_is_one(rec->p[i]))
1459 p = upoly_print_cst(rec->p[i], p, first);
1462 p = isl_printer_print_str(p, " + ");
1463 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1464 p = upoly_print(rec->p[i], dim, div, p, 0);
1469 if (!isl_upoly_is_one(rec->p[i]) &&
1470 !isl_upoly_is_negone(rec->p[i]))
1471 p = isl_printer_print_str(p, " * ");
1472 p = print_pow(p, dim, div, rec->up.var, i);
1475 p = isl_printer_print_str(p, ")");
1478 isl_printer_free(p);
1482 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
1483 __isl_keep isl_qpolynomial *qp)
1487 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1490 isl_printer_free(p);
1494 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1495 unsigned output_format)
1502 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1503 p = isl_printer_to_file(qp->dim->ctx, out);
1504 p = isl_printer_print_qpolynomial(p, qp);
1505 isl_printer_free(p);
1508 static __isl_give isl_printer *qpolynomial_fold_print(
1509 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1513 if (fold->type == isl_fold_min)
1514 p = isl_printer_print_str(p, "min");
1515 else if (fold->type == isl_fold_max)
1516 p = isl_printer_print_str(p, "max");
1517 p = isl_printer_print_str(p, "(");
1518 for (i = 0; i < fold->n; ++i) {
1520 p = isl_printer_print_str(p, ", ");
1521 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
1523 p = isl_printer_print_str(p, ")");
1527 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1528 FILE *out, unsigned output_format)
1535 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1537 p = isl_printer_to_file(fold->dim->ctx, out);
1538 p = isl_printer_print_qpolynomial_fold(p, fold);
1540 isl_printer_free(p);
1543 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1544 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1548 for (i = 0; i < pwqp->n; ++i) {
1550 p = isl_printer_print_str(p, "; ");
1551 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1552 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1553 p = print_dim(pwqp->p[i].set->dim, p, 1, 0, 0, NULL);
1554 p = isl_printer_print_str(p, " -> ");
1556 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
1557 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1563 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1564 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1569 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
1570 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1571 p = isl_printer_print_str(p, " -> ");
1573 p = isl_printer_print_str(p, "{ ");
1575 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1576 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1577 p = print_dim(pwqp->dim, p, 1, 0, 0, NULL);
1578 p = isl_printer_print_str(p, " -> ");
1580 p = isl_printer_print_str(p, "0");
1582 p = isl_pwqp_print_isl_body(p, pwqp);
1583 p = isl_printer_print_str(p, " }");
1586 isl_printer_free(p);
1590 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1591 unsigned output_format)
1598 p = isl_printer_to_file(pwqp->dim->ctx, out);
1599 p = isl_printer_set_output_format(p, output_format);
1600 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1602 isl_printer_free(p);
1605 static __isl_give isl_printer *isl_pwf_print_isl_body(
1606 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1610 for (i = 0; i < pwf->n; ++i) {
1612 p = isl_printer_print_str(p, "; ");
1613 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1614 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1615 p = print_dim(pwf->p[i].set->dim, p, 1, 0, 0, NULL);
1616 p = isl_printer_print_str(p, " -> ");
1618 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1619 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1625 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1626 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1628 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1629 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1630 p = isl_printer_print_str(p, " -> ");
1632 p = isl_printer_print_str(p, "{ ");
1634 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1635 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1636 p = print_dim(pwf->dim, p, 1, 0, 0, NULL);
1637 p = isl_printer_print_str(p, " -> ");
1639 p = isl_printer_print_str(p, "0");
1641 p = isl_pwf_print_isl_body(p, pwf);
1642 p = isl_printer_print_str(p, " }");
1646 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1647 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1649 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1650 __isl_keep isl_dim *dim,
1651 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1653 if (type == isl_dim_div) {
1654 p = isl_printer_print_str(p, "floord(");
1655 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1656 p = isl_printer_print_str(p, ", ");
1657 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1658 p = isl_printer_print_str(p, ")");
1662 name = isl_dim_get_name(dim, type, pos);
1665 p = isl_printer_print_str(p, name);
1670 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1671 __isl_keep isl_dim *dim,
1672 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1674 enum isl_dim_type type;
1675 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1678 return isl_printer_print_isl_int(p, c);
1680 if (isl_int_is_one(c))
1682 else if (isl_int_is_negone(c))
1683 p = isl_printer_print_str(p, "-");
1685 p = isl_printer_print_isl_int(p, c);
1686 p = isl_printer_print_str(p, "*");
1688 type = pos2type(dim, &pos);
1689 p = print_name_c(p, dim, bset, type, pos);
1693 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1694 __isl_keep isl_dim *dim,
1695 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1700 for (i = 0, first = 1; i < len; ++i) {
1702 if (isl_int_is_zero(c[i]))
1705 if (isl_int_is_neg(c[i])) {
1707 isl_int_neg(c[i], c[i]);
1708 p = isl_printer_print_str(p, " - ");
1710 p = isl_printer_print_str(p, " + ");
1713 p = print_term_c(p, dim, bset, c[i], i);
1715 isl_int_neg(c[i], c[i]);
1718 p = isl_printer_print_str(p, "0");
1722 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1723 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1725 unsigned len = 1 + isl_basic_set_total_dim(bset);
1726 return print_partial_affine_c(p, dim, bset, c, len);
1729 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1730 __isl_keep isl_dim *dim,
1731 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1734 p = isl_printer_print_str(p, " && ");
1736 p = print_affine_c(p, dim, bset, c);
1737 p = isl_printer_print_str(p, " ");
1738 p = isl_printer_print_str(p, op);
1739 p = isl_printer_print_str(p, " 0");
1743 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1744 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset)
1747 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1748 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1750 for (i = 0; i < bset->n_eq; ++i) {
1751 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1753 p = print_constraint_c(p, dim, bset,
1754 bset->eq[i], "==", !i);
1757 p = isl_printer_print_str(p, " && ");
1758 p = isl_printer_print_str(p, "(");
1759 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1761 p = isl_printer_print_str(p, ") % ");
1762 p = isl_printer_print_isl_int(p,
1763 bset->eq[i][1 + total + j]);
1764 p = isl_printer_print_str(p, " == 0");
1767 for (i = 0; i < bset->n_ineq; ++i)
1768 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1773 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1774 __isl_keep isl_dim *dim, __isl_keep isl_set *set)
1779 p = isl_printer_print_str(p, "0");
1781 for (i = 0; i < set->n; ++i) {
1783 p = isl_printer_print_str(p, " || ");
1785 p = isl_printer_print_str(p, "(");
1786 p = print_basic_set_c(p, dim, set->p[i]);
1788 p = isl_printer_print_str(p, ")");
1793 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1794 __isl_keep isl_dim *dim, __isl_keep isl_qpolynomial *qp)
1799 isl_qpolynomial_get_den(qp, &den);
1800 if (!isl_int_is_one(den)) {
1802 p = isl_printer_print_str(p, "(");
1803 qp = isl_qpolynomial_copy(qp);
1804 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1805 den, qp->dim->ctx->one);
1806 qp = isl_qpolynomial_mul(qp, f);
1809 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1810 if (!isl_int_is_one(den)) {
1811 p = isl_printer_print_str(p, ")/");
1812 p = isl_printer_print_isl_int(p, den);
1813 isl_qpolynomial_free(qp);
1819 static __isl_give isl_printer *print_pw_qpolynomial_c(
1820 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1824 if (pwqp->n == 1 && isl_set_fast_is_universe(pwqp->p[0].set))
1825 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1827 for (i = 0; i < pwqp->n; ++i) {
1828 p = isl_printer_print_str(p, "(");
1829 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1830 p = isl_printer_print_str(p, ") ? (");
1831 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1832 p = isl_printer_print_str(p, ") : ");
1835 p = isl_printer_print_str(p, "0");
1839 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1840 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1845 if (p->output_format == ISL_FORMAT_ISL)
1846 return print_pw_qpolynomial_isl(p, pwqp);
1847 else if (p->output_format == ISL_FORMAT_C)
1848 return print_pw_qpolynomial_c(p, pwqp);
1849 isl_assert(p->ctx, 0, goto error);
1851 isl_printer_free(p);
1855 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1857 struct isl_union_print_data *data;
1858 data = (struct isl_union_print_data *)user;
1861 data->p = isl_printer_print_str(data->p, "; ");
1864 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1865 isl_pw_qpolynomial_free(pwqp);
1870 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1871 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1873 struct isl_union_print_data data = { p, 1, 1 };
1875 dim = isl_union_pw_qpolynomial_get_dim(upwqp);
1876 if (isl_dim_size(dim, isl_dim_param) > 0) {
1877 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1878 p = isl_printer_print_str(p, " -> ");
1881 p = isl_printer_print_str(p, "{ ");
1882 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1885 p = isl_printer_print_str(p, " }");
1889 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1890 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1895 if (p->output_format == ISL_FORMAT_ISL)
1896 return print_union_pw_qpolynomial_isl(p, upwqp);
1897 isl_die(p->ctx, isl_error_invalid,
1898 "invalid output format for isl_union_pw_qpolynomial",
1901 isl_printer_free(p);
1905 static __isl_give isl_printer *print_qpolynomial_fold_c(
1906 __isl_take isl_printer *p, __isl_keep isl_dim *dim,
1907 __isl_keep isl_qpolynomial_fold *fold)
1911 for (i = 0; i < fold->n - 1; ++i)
1912 if (fold->type == isl_fold_min)
1913 p = isl_printer_print_str(p, "min(");
1914 else if (fold->type == isl_fold_max)
1915 p = isl_printer_print_str(p, "max(");
1917 for (i = 0; i < fold->n; ++i) {
1919 p = isl_printer_print_str(p, ", ");
1920 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1922 p = isl_printer_print_str(p, ")");
1927 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1928 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1932 if (p->output_format == ISL_FORMAT_ISL)
1933 return qpolynomial_fold_print(fold, p);
1934 else if (p->output_format == ISL_FORMAT_C)
1935 return print_qpolynomial_fold_c(p, fold->dim, fold);
1936 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1939 isl_printer_free(p);
1943 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1944 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1948 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1949 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1951 for (i = 0; i < pwf->n; ++i) {
1952 p = isl_printer_print_str(p, "(");
1953 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1954 p = isl_printer_print_str(p, ") ? (");
1955 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1956 p = isl_printer_print_str(p, ") : ");
1959 p = isl_printer_print_str(p, "0");
1963 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1964 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1969 if (p->output_format == ISL_FORMAT_ISL)
1970 return print_pw_qpolynomial_fold_isl(p, pwf);
1971 else if (p->output_format == ISL_FORMAT_C)
1972 return print_pw_qpolynomial_fold_c(p, pwf);
1973 isl_assert(p->ctx, 0, goto error);
1975 isl_printer_free(p);
1979 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1980 FILE *out, unsigned output_format)
1987 p = isl_printer_to_file(pwf->dim->ctx, out);
1988 p = isl_printer_set_output_format(p, output_format);
1989 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1991 isl_printer_free(p);
1994 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1996 struct isl_union_print_data *data;
1997 data = (struct isl_union_print_data *)user;
2000 data->p = isl_printer_print_str(data->p, "; ");
2003 data->p = isl_pwf_print_isl_body(data->p, pwf);
2004 isl_pw_qpolynomial_fold_free(pwf);
2009 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2010 __isl_take isl_printer *p,
2011 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2013 struct isl_union_print_data data = { p, 1, 1 };
2015 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
2016 if (isl_dim_size(dim, isl_dim_param) > 0) {
2017 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
2018 p = isl_printer_print_str(p, " -> ");
2021 p = isl_printer_print_str(p, "{ ");
2022 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2023 &print_pwf_body, &data);
2025 p = isl_printer_print_str(p, " }");
2029 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2030 __isl_take isl_printer *p,
2031 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2036 if (p->output_format == ISL_FORMAT_ISL)
2037 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2038 isl_die(p->ctx, isl_error_invalid,
2039 "invalid output format for isl_union_pw_qpolynomial_fold",
2042 isl_printer_free(p);
2046 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
2047 __isl_keep isl_constraint *c)
2049 isl_basic_map *bmap;
2054 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
2055 p = isl_printer_print_basic_map(p, bmap);
2056 isl_basic_map_free(bmap);
2059 isl_printer_free(p);
2063 void isl_constraint_dump(__isl_keep isl_constraint *c)
2065 isl_printer *printer;
2070 printer = isl_printer_to_file(isl_constraint_get_ctx(c), stderr);
2071 printer = isl_printer_print_constraint(printer, c);
2072 printer = isl_printer_end_line(printer);
2074 isl_printer_free(printer);