add isl_basic_map_get_tuple_name
[isl.git] / isl_output.c
blob64b41196d930d86df52d8e22db32218f62a45cf9
1 /*
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
13 #include <stdlib.h>
14 #include <string.h>
15 #include <isl_set.h>
16 #include <isl_seq.h>
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)
44 int i;
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);
71 return p;
74 static __isl_give isl_printer *print_constraints_polylib(
75 struct isl_basic_map *bmap, __isl_take isl_printer *p)
77 int i;
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);
86 return 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)
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);
103 p = isl_printer_end_line(p);
104 return print_constraints_polylib(bmap, p);
107 static __isl_give isl_printer *isl_basic_set_print_polylib(
108 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
110 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p);
113 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
114 __isl_take isl_printer *p)
116 int i;
118 p = isl_printer_start_line(p);
119 p = isl_printer_print_int(p, map->n);
120 p = isl_printer_end_line(p);
121 for (i = 0; i < map->n; ++i) {
122 p = isl_printer_start_line(p);
123 p = isl_printer_end_line(p);
124 p = isl_basic_map_print_polylib(map->p[i], p);
126 return p;
129 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
130 __isl_take isl_printer *p)
132 return isl_map_print_polylib((struct isl_map *)set, p);
135 static int count_same_name(__isl_keep isl_dim *dim,
136 enum isl_dim_type type, unsigned pos, const char *name)
138 enum isl_dim_type t;
139 unsigned p, s;
140 int count = 0;
142 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
143 s = t == type ? pos : isl_dim_size(dim, t);
144 for (p = 0; p < s; ++p) {
145 const char *n = isl_dim_get_name(dim, t, p);
146 if (n && !strcmp(n, name))
147 count++;
150 return count;
153 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
154 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
155 int set, int latex)
157 const char *name;
158 char buffer[20];
159 int primes;
161 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
163 if (!name) {
164 const char *prefix;
165 if (type == isl_dim_param)
166 prefix = s_param_prefix[latex];
167 else if (type == isl_dim_div)
168 prefix = s_div_prefix[latex];
169 else if (set || type == isl_dim_in)
170 prefix = s_input_prefix[latex];
171 else
172 prefix = s_output_prefix[latex];
173 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
174 name = buffer;
176 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
177 pos, name);
178 p = isl_printer_print_str(p, name);
179 while (primes-- > 0)
180 p = isl_printer_print_str(p, "'");
181 return p;
184 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
186 enum isl_dim_type type;
187 unsigned n_in = isl_dim_size(dim, isl_dim_in);
188 unsigned n_out = isl_dim_size(dim, isl_dim_out);
189 unsigned nparam = isl_dim_size(dim, isl_dim_param);
191 if (*pos < 1 + nparam) {
192 type = isl_dim_param;
193 *pos -= 1;
194 } else if (*pos < 1 + nparam + n_in) {
195 type = isl_dim_in;
196 *pos -= 1 + nparam;
197 } else if (*pos < 1 + nparam + n_in + n_out) {
198 type = isl_dim_out;
199 *pos -= 1 + nparam + n_in;
200 } else {
201 type = isl_dim_div;
202 *pos -= 1 + nparam + n_in + n_out;
205 return type;
208 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
209 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
211 enum isl_dim_type type;
213 if (pos == 0)
214 return isl_printer_print_isl_int(p, c);
216 if (isl_int_is_one(c))
218 else if (isl_int_is_negone(c))
219 p = isl_printer_print_str(p, "-");
220 else
221 p = isl_printer_print_isl_int(p, c);
222 type = pos2type(dim, &pos);
223 p = print_name(dim, p, type, pos, set, latex);
224 return p;
227 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
228 __isl_take isl_printer *p, isl_int *c, int len, int set)
230 int i;
231 int first;
233 for (i = 0, first = 1; i < len; ++i) {
234 int flip = 0;
235 if (isl_int_is_zero(c[i]))
236 continue;
237 if (!first) {
238 if (isl_int_is_neg(c[i])) {
239 flip = 1;
240 isl_int_neg(c[i], c[i]);
241 p = isl_printer_print_str(p, " - ");
242 } else
243 p = isl_printer_print_str(p, " + ");
245 first = 0;
246 p = print_term(dim, c[i], i, p, set, 0);
247 if (flip)
248 isl_int_neg(c[i], c[i]);
250 if (first)
251 p = isl_printer_print_str(p, "0");
252 return p;
255 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
256 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
258 unsigned len = 1 + isl_basic_map_total_dim(bmap);
259 return print_affine_of_len(dim, p, c, len, set);
262 static int defining_equality(__isl_keep isl_basic_map *eq,
263 __isl_keep isl_dim *dim, enum isl_dim_type type, int pos)
265 int i;
266 unsigned total;
268 if (!eq)
269 return -1;
271 pos += isl_dim_offset(dim, type);
272 total = isl_basic_map_total_dim(eq);
274 for (i = 0; i < eq->n_eq; ++i) {
275 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
276 continue;
277 if (isl_int_is_one(eq->eq[i][1 + pos]))
278 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
279 return i;
282 return -1;
285 /* offset is the offset of local_dim inside global_type of global_dim.
287 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
288 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
289 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
290 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
292 int i, j;
294 if (global_dim != local_dim && local_type == isl_dim_out)
295 offset += local_dim->n_in;
297 for (i = 0; i < isl_dim_size(local_dim, local_type); ++i) {
298 if (i)
299 p = isl_printer_print_str(p, ", ");
300 j = defining_equality(eq, global_dim, global_type, offset + i);
301 if (j >= 0) {
302 int pos = 1 + isl_dim_offset(global_dim, global_type)
303 + offset + i;
304 p = print_affine_of_len(eq->dim, p, eq->eq[j], pos, set);
305 } else {
306 p = print_name(global_dim, p, global_type, offset + i,
307 set, latex);
310 return p;
313 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
314 __isl_take isl_printer *p, enum isl_dim_type type,
315 int set, int latex, __isl_keep isl_basic_map *eq)
317 return print_nested_var_list(p, dim, type, dim, type, set, latex, eq, 0);
320 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
321 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
322 __isl_keep isl_dim *local_dim,
323 int set, int latex, __isl_keep isl_basic_map *eq, int offset);
325 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
326 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
327 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
328 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
330 const char *name = NULL;
331 unsigned n = isl_dim_size(local_dim, local_type);
332 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
333 name = isl_dim_get_tuple_name(local_dim, local_type);
334 if (name) {
335 if (latex)
336 p = isl_printer_print_str(p, "\\mathrm{");
337 p = isl_printer_print_str(p, name);
338 if (latex)
339 p = isl_printer_print_str(p, "}");
342 if (!latex || n != 1 || name)
343 p = isl_printer_print_str(p, s_open_list[latex]);
344 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
345 local_dim->nested[local_type - isl_dim_in]) {
346 if (global_dim != local_dim && local_type == isl_dim_out)
347 offset += local_dim->n_in;
348 p = print_nested_map_dim(p, global_dim, global_type,
349 local_dim->nested[local_type - isl_dim_in],
350 set, latex, eq, offset);
351 } else
352 p = print_nested_var_list(p, global_dim, global_type,
353 local_dim, local_type, set, latex, eq, offset);
354 if (!latex || n != 1 || name)
355 p = isl_printer_print_str(p, s_close_list[latex]);
356 return p;
359 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
360 __isl_take isl_printer *p, enum isl_dim_type type,
361 int set, int latex, __isl_keep isl_basic_map *eq)
363 return print_nested_tuple(p, dim, type, dim, type, set, latex, eq, 0);
366 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
367 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
368 __isl_keep isl_dim *local_dim,
369 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
371 p = print_nested_tuple(p, global_dim, global_type,
372 local_dim, isl_dim_in, set, latex, eq, offset);
373 p = isl_printer_print_str(p, s_to[latex]);
374 p = print_nested_tuple(p, global_dim, global_type,
375 local_dim, isl_dim_out, set, latex, eq, offset);
377 return p;
380 static __isl_give isl_printer *print_dim(__isl_keep isl_dim *dim,
381 __isl_take isl_printer *p, int set, int latex,
382 __isl_keep isl_basic_map *eq)
384 if (set)
385 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
386 else {
387 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
388 p = isl_printer_print_str(p, s_to[latex]);
389 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
392 return p;
395 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
396 __isl_take isl_printer *p)
398 if (isl_dim_size(dim, isl_dim_param) == 0)
399 return p;
401 p = isl_printer_start_line(p);
402 p = isl_printer_print_str(p, "symbolic ");
403 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
404 p = isl_printer_print_str(p, ";");
405 p = isl_printer_end_line(p);
406 return p;
409 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
410 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
411 isl_int *c, int last, const char *op, int first_constraint, int set,
412 int latex)
414 if (!first_constraint)
415 p = isl_printer_print_str(p, s_and[latex]);
417 isl_int_abs(c[last], c[last]);
419 p = print_term(dim, c[last], last, p, set, latex);
421 p = isl_printer_print_str(p, " ");
422 p = isl_printer_print_str(p, op);
423 p = isl_printer_print_str(p, " ");
425 isl_int_set_si(c[last], 0);
426 p = print_affine(bmap, dim, p, c, set);
428 return p;
431 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
432 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
434 int i;
435 struct isl_vec *c;
436 unsigned total = isl_basic_map_total_dim(bmap);
438 c = isl_vec_alloc(bmap->ctx, 1 + total);
439 if (!c)
440 goto error;
442 for (i = bmap->n_eq - 1; i >= 0; --i) {
443 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
444 isl_assert(bmap->ctx, l >= 0, goto error);
445 if (isl_int_is_neg(bmap->eq[i][l]))
446 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
447 else
448 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
449 p = print_constraint(bmap, dim, p, c->el, l,
450 "=", i == bmap->n_eq - 1, set, latex);
452 for (i = 0; i < bmap->n_ineq; ++i) {
453 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
454 int s;
455 const char *op;
456 if (l < 0)
457 continue;
458 s = isl_int_sgn(bmap->ineq[i][l]);
459 if (s < 0)
460 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
461 else
462 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
463 op = s < 0 ? s_le[latex] : s_ge[latex];
464 p = print_constraint(bmap, dim, p, c->el, l,
465 op, !bmap->n_eq && !i, set, latex);
468 isl_vec_free(c);
470 return p;
471 error:
472 isl_printer_free(p);
473 return NULL;
476 static __isl_give isl_printer *print_omega_constraints(
477 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
479 if (bmap->n_eq + bmap->n_ineq == 0)
480 return p;
482 p = isl_printer_print_str(p, ": ");
483 if (bmap->n_div > 0) {
484 int i;
485 p = isl_printer_print_str(p, "exists (");
486 for (i = 0; i < bmap->n_div; ++i) {
487 if (i)
488 p = isl_printer_print_str(p, ", ");
489 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
491 p = isl_printer_print_str(p, ": ");
493 p = print_constraints(bmap, bmap->dim, p, set, 0);
494 if (bmap->n_div > 0)
495 p = isl_printer_print_str(p, ")");
496 return p;
499 static __isl_give isl_printer *basic_map_print_omega(
500 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
502 p = isl_printer_print_str(p, "{ [");
503 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
504 p = isl_printer_print_str(p, "] -> [");
505 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
506 p = isl_printer_print_str(p, "] ");
507 p = print_omega_constraints(bmap, p, 0);
508 p = isl_printer_print_str(p, " }");
509 return p;
512 static __isl_give isl_printer *isl_basic_map_print_omega(
513 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
515 p = print_omega_parameters(bmap->dim, p);
517 p = isl_printer_start_line(p);
518 p = basic_map_print_omega(bmap, p);
519 p = isl_printer_end_line(p);
520 return p;
523 static __isl_give isl_printer *basic_set_print_omega(
524 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
526 p = isl_printer_print_str(p, "{ [");
527 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
528 p = isl_printer_print_str(p, "] ");
529 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
530 p = isl_printer_print_str(p, " }");
531 return p;
534 static __isl_give isl_printer *isl_basic_set_print_omega(
535 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
537 p = print_omega_parameters(bset->dim, p);
539 p = isl_printer_start_line(p);
540 p = basic_set_print_omega(bset, p);
541 p = isl_printer_end_line(p);
542 return p;
545 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
546 __isl_take isl_printer *p)
548 int i;
550 p = print_omega_parameters(map->dim, p);
552 p = isl_printer_start_line(p);
553 for (i = 0; i < map->n; ++i) {
554 if (i)
555 p = isl_printer_print_str(p, " union ");
556 p = basic_map_print_omega(map->p[i], p);
558 p = isl_printer_end_line(p);
559 return p;
562 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
563 __isl_take isl_printer *p)
565 int i;
567 p = print_omega_parameters(set->dim, p);
569 p = isl_printer_start_line(p);
570 for (i = 0; i < set->n; ++i) {
571 if (i)
572 p = isl_printer_print_str(p, " union ");
573 p = basic_set_print_omega(set->p[i], p);
575 p = isl_printer_end_line(p);
576 return p;
579 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
580 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
582 if (bmap->n_div > 0) {
583 int i;
584 p = isl_printer_print_str(p, s_open_exists[latex]);
585 for (i = 0; i < bmap->n_div; ++i) {
586 if (i)
587 p = isl_printer_print_str(p, ", ");
588 p = print_name(dim, p, isl_dim_div, i, 0, latex);
589 if (latex || isl_int_is_zero(bmap->div[i][0]))
590 continue;
591 p = isl_printer_print_str(p, " = [(");
592 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
593 p = isl_printer_print_str(p, ")/");
594 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
595 p = isl_printer_print_str(p, "]");
597 p = isl_printer_print_str(p, ": ");
600 p = print_constraints(bmap, dim, p, set, latex);
602 if (bmap->n_div > 0)
603 p = isl_printer_print_str(p, s_close_exists[latex]);
604 return p;
607 static __isl_give isl_printer *isl_basic_map_print_isl(
608 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int latex)
610 int i;
612 p = isl_printer_start_line(p);
613 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
614 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
615 p = isl_printer_print_str(p, " -> ");
617 p = isl_printer_print_str(p, "{ ");
618 p = print_dim(bmap->dim, p, 0, latex, NULL);
619 p = isl_printer_print_str(p, " : ");
620 p = print_disjunct(bmap, bmap->dim, p, 0, latex);
621 p = isl_printer_print_str(p, " }");
622 p = isl_printer_end_line(p);
623 return p;
626 static __isl_give isl_printer *isl_basic_set_print_isl(
627 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int latex)
629 int i;
631 p = isl_printer_start_line(p);
632 if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
633 p = print_tuple(bset->dim, p, isl_dim_param, 0, latex, NULL);
634 p = isl_printer_print_str(p, " -> ");
636 p = isl_printer_print_str(p, "{ ");
637 p = print_dim(bset->dim, p, 1, latex, NULL);
638 p = isl_printer_print_str(p, " : ");
639 p = print_disjunct((isl_basic_map *)bset, bset->dim, p, 1, latex);
640 p = isl_printer_print_str(p, " }");
641 p = isl_printer_end_line(p);
642 return p;
645 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
646 __isl_take isl_printer *p, int set, int latex)
648 int i;
650 if (isl_map_fast_is_universe(map))
651 return p;
653 p = isl_printer_print_str(p, s_such_that[latex]);
654 if (map->n == 0)
655 p = isl_printer_print_str(p, "1 = 0");
656 for (i = 0; i < map->n; ++i) {
657 if (i)
658 p = isl_printer_print_str(p, s_or[latex]);
659 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
660 p = isl_printer_print_str(p, "(");
661 p = print_disjunct(map->p[i], map->dim, p, set, latex);
662 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
663 p = isl_printer_print_str(p, ")");
665 return p;
668 struct isl_aff_split {
669 isl_basic_map *aff;
670 isl_map *map;
673 static void free_split(__isl_take struct isl_aff_split *split, int n)
675 int i;
677 if (!split)
678 return;
680 for (i = 0; i < n; ++i) {
681 isl_basic_map_free(split[i].aff);
682 isl_map_free(split[i].map);
685 free(split);
688 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
690 int i, j;
691 unsigned nparam, n_in, n_out, total;
693 bmap = isl_basic_map_cow(bmap);
694 if (!bmap)
695 return NULL;
696 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
697 goto error;
699 nparam = isl_basic_map_dim(bmap, isl_dim_param);
700 n_in = isl_basic_map_dim(bmap, isl_dim_in);
701 n_out = isl_basic_map_dim(bmap, isl_dim_out);
702 total = isl_basic_map_dim(bmap, isl_dim_all);
703 for (i = bmap->n_eq - 1; i >= 0; --i) {
704 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
705 if (j >= nparam && j < nparam + n_in + n_out &&
706 (isl_int_is_one(bmap->eq[i][1 + j]) ||
707 isl_int_is_negone(bmap->eq[i][1 + j])))
708 continue;
709 if (isl_basic_map_drop_equality(bmap, i) < 0)
710 goto error;
713 bmap = isl_basic_map_finalize(bmap);
715 return bmap;
716 error:
717 isl_basic_map_free(bmap);
718 return NULL;
721 static int aff_split_cmp(const void *p1, const void *p2)
723 const struct isl_aff_split *s1, *s2;
724 s1 = (const struct isl_aff_split *) p1;
725 s2 = (const struct isl_aff_split *) p2;
727 return isl_basic_map_fast_cmp(s1->aff, s2->aff);
730 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
731 __isl_keep isl_basic_map *aff)
733 int i, j;
734 unsigned total;
736 if (!bmap || !aff)
737 goto error;
739 total = isl_dim_total(bmap->dim);
741 for (i = bmap->n_eq - 1; i >= 0; --i) {
742 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
743 bmap->n_div) != -1)
744 continue;
745 for (j = 0; j < aff->n_eq; ++j) {
746 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
747 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
748 continue;
749 if (isl_basic_map_drop_equality(bmap, i) < 0)
750 goto error;
751 break;
755 return bmap;
756 error:
757 isl_basic_map_free(bmap);
758 return NULL;
761 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
763 int i, n;
764 struct isl_aff_split *split;
766 split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
767 if (!split)
768 return NULL;
770 for (i = 0; i < map->n; ++i) {
771 isl_basic_map *bmap;
772 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
773 bmap = isl_basic_map_copy(map->p[i]);
774 bmap = isl_basic_map_cow(bmap);
775 bmap = drop_aff(bmap, split[i].aff);
776 split[i].map = isl_map_from_basic_map(bmap);
777 if (!split[i].aff || !split[i].map)
778 goto error;
781 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
783 n = map->n;
784 for (i = n - 1; i >= 1; --i) {
785 if (!isl_basic_map_fast_is_equal(split[i - 1].aff,
786 split[i].aff))
787 continue;
788 isl_basic_map_free(split[i].aff);
789 split[i - 1].map = isl_map_union(split[i - 1].map,
790 split[i].map);
791 if (i != n - 1)
792 split[i] = split[n - 1];
793 split[n - 1].aff = NULL;
794 split[n - 1].map = NULL;
795 --n;
798 return split;
799 error:
800 free_split(split, map->n);
801 return NULL;
804 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
805 struct isl_aff_split *split, int n, int set)
807 int i;
809 for (i = 0; i < n; ++i) {
810 isl_dim *dim;
812 if (!split[i].map)
813 break;
814 dim = split[i].map->dim;
815 if (i)
816 p = isl_printer_print_str(p, "; ");
817 p = print_dim(dim, p, set, 0, split[i].aff);
818 p = print_disjuncts(split[i].map, p, set, 0);
821 return p;
824 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
825 __isl_take isl_printer *p, int set)
827 struct isl_aff_split *split = NULL;
829 if (map->n > 0)
830 split = split_aff(map);
831 if (split) {
832 p = print_split_map(p, split, map->n, set);
833 } else {
834 p = print_dim(map->dim, p, set, 0, NULL);
835 p = print_disjuncts(map, p, set, 0);
837 free_split(split, map->n);
838 return p;
841 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
842 __isl_take isl_printer *p, int set)
844 if (isl_map_dim(map, isl_dim_param) > 0) {
845 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
846 p = isl_printer_print_str(p, s_to[0]);
848 p = isl_printer_print_str(p, s_open_set[0]);
849 p = isl_map_print_isl_body(map, p, set);
850 p = isl_printer_print_str(p, s_close_set[0]);
851 return p;
854 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
855 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
857 if (isl_map_dim(map, isl_dim_param) > 0) {
858 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
859 p = isl_printer_print_str(p, s_to[1]);
861 p = isl_printer_print_str(p, s_open_set[1]);
862 p = print_dim(map->dim, p, set, 1, aff);
863 p = print_disjuncts(map, p, set, 1);
864 p = isl_printer_print_str(p, s_close_set[1]);
866 return p;
869 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
870 __isl_take isl_printer *p, int set)
872 int i;
873 struct isl_aff_split *split = NULL;
875 if (map->n > 0)
876 split = split_aff(map);
878 if (!split)
879 return print_latex_map(map, p, NULL, set);
881 for (i = 0; i < map->n; ++i) {
882 if (!split[i].map)
883 break;
884 if (i)
885 p = isl_printer_print_str(p, " \\cup ");
886 p = print_latex_map(split[i].map, p, split[i].aff, set);
889 free_split(split, map->n);
890 return p;
893 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
894 __isl_keep isl_basic_map *bmap)
896 if (!p || !bmap)
897 goto error;
898 if (p->output_format == ISL_FORMAT_ISL)
899 return isl_basic_map_print_isl(bmap, p, 0);
900 else if (p->output_format == ISL_FORMAT_OMEGA)
901 return isl_basic_map_print_omega(bmap, p);
902 isl_assert(bmap->ctx, 0, goto error);
903 error:
904 isl_printer_free(p);
905 return NULL;
908 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
909 const char *prefix, const char *suffix, unsigned output_format)
911 isl_printer *printer;
913 if (!bmap)
914 return;
916 printer = isl_printer_to_file(bmap->ctx, out);
917 printer = isl_printer_set_indent(printer, indent);
918 printer = isl_printer_set_prefix(printer, prefix);
919 printer = isl_printer_set_suffix(printer, suffix);
920 printer = isl_printer_set_output_format(printer, output_format);
921 isl_printer_print_basic_map(printer, bmap);
923 isl_printer_free(printer);
926 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
927 __isl_keep isl_basic_set *bset)
929 if (!p || !bset)
930 goto error;
932 if (p->output_format == ISL_FORMAT_ISL)
933 return isl_basic_set_print_isl(bset, p, 0);
934 else if (p->output_format == ISL_FORMAT_POLYLIB)
935 return isl_basic_set_print_polylib(bset, p);
936 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
937 return bset_print_constraints_polylib(bset, p);
938 else if (p->output_format == ISL_FORMAT_OMEGA)
939 return isl_basic_set_print_omega(bset, p);
940 isl_assert(p->ctx, 0, goto error);
941 error:
942 isl_printer_free(p);
943 return NULL;
946 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
947 const char *prefix, const char *suffix, unsigned output_format)
949 isl_printer *printer;
951 if (!bset)
952 return;
954 printer = isl_printer_to_file(bset->ctx, out);
955 printer = isl_printer_set_indent(printer, indent);
956 printer = isl_printer_set_prefix(printer, prefix);
957 printer = isl_printer_set_suffix(printer, suffix);
958 printer = isl_printer_set_output_format(printer, output_format);
959 isl_printer_print_basic_set(printer, bset);
961 isl_printer_free(printer);
964 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
965 __isl_keep isl_set *set)
967 if (!p || !set)
968 goto error;
969 if (p->output_format == ISL_FORMAT_ISL)
970 return isl_map_print_isl((isl_map *)set, p, 1);
971 else if (p->output_format == ISL_FORMAT_POLYLIB)
972 return isl_set_print_polylib(set, p);
973 else if (p->output_format == ISL_FORMAT_OMEGA)
974 return isl_set_print_omega(set, p);
975 else if (p->output_format == ISL_FORMAT_LATEX)
976 return isl_map_print_latex((isl_map *)set, p, 1);
977 isl_assert(set->ctx, 0, goto error);
978 error:
979 isl_printer_free(p);
980 return NULL;
983 void isl_set_print(struct isl_set *set, FILE *out, int indent,
984 unsigned output_format)
986 isl_printer *printer;
988 if (!set)
989 return;
991 printer = isl_printer_to_file(set->ctx, out);
992 printer = isl_printer_set_indent(printer, indent);
993 printer = isl_printer_set_output_format(printer, output_format);
994 printer = isl_printer_print_set(printer, set);
996 isl_printer_free(printer);
999 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1000 __isl_keep isl_map *map)
1002 if (!p || !map)
1003 goto error;
1005 if (p->output_format == ISL_FORMAT_ISL)
1006 return isl_map_print_isl(map, p, 0);
1007 else if (p->output_format == ISL_FORMAT_POLYLIB)
1008 return isl_map_print_polylib(map, p);
1009 else if (p->output_format == ISL_FORMAT_OMEGA)
1010 return isl_map_print_omega(map, p);
1011 else if (p->output_format == ISL_FORMAT_LATEX)
1012 return isl_map_print_latex(map, p, 0);
1013 isl_assert(map->ctx, 0, goto error);
1014 error:
1015 isl_printer_free(p);
1016 return NULL;
1019 struct isl_union_print_data {
1020 isl_printer *p;
1021 int set;
1022 int first;
1025 static int print_map_body(__isl_take isl_map *map, void *user)
1027 struct isl_union_print_data *data;
1028 data = (struct isl_union_print_data *)user;
1030 if (!data->first)
1031 data->p = isl_printer_print_str(data->p, "; ");
1032 data->first = 0;
1034 data->p = isl_map_print_isl_body(map, data->p, data->set);
1035 isl_map_free(map);
1037 return 0;
1040 static __isl_give isl_printer *isl_union_map_print_isl(
1041 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1043 struct isl_union_print_data data = { p, set, 1 };
1044 isl_dim *dim;
1045 dim = isl_union_map_get_dim(umap);
1046 if (isl_dim_size(dim, isl_dim_param) > 0) {
1047 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1048 p = isl_printer_print_str(p, s_to[0]);
1050 isl_dim_free(dim);
1051 p = isl_printer_print_str(p, s_open_set[0]);
1052 isl_union_map_foreach_map(umap, &print_map_body, &data);
1053 p = data.p;
1054 p = isl_printer_print_str(p, s_close_set[0]);
1055 return p;
1058 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1060 struct isl_union_print_data *data;
1061 data = (struct isl_union_print_data *)user;
1063 if (!data->first)
1064 data->p = isl_printer_print_str(data->p, " \\cup ");
1065 data->first = 0;
1067 data->p = isl_map_print_latex(map, data->p, data->set);
1068 isl_map_free(map);
1070 return 0;
1073 static __isl_give isl_printer *isl_union_map_print_latex(
1074 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1076 struct isl_union_print_data data = { p, set, 1 };
1077 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1078 p = data.p;
1079 return p;
1082 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1083 __isl_keep isl_union_map *umap)
1085 if (!p || !umap)
1086 goto error;
1088 if (p->output_format == ISL_FORMAT_ISL)
1089 return isl_union_map_print_isl(umap, p, 0);
1090 if (p->output_format == ISL_FORMAT_LATEX)
1091 return isl_union_map_print_latex(umap, p, 0);
1093 isl_die(p->ctx, isl_error_invalid,
1094 "invalid output format for isl_union_map", goto error);
1095 error:
1096 isl_printer_free(p);
1097 return NULL;
1100 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1101 __isl_keep isl_union_set *uset)
1103 if (!p || !uset)
1104 goto error;
1106 if (p->output_format == ISL_FORMAT_ISL)
1107 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1108 if (p->output_format == ISL_FORMAT_LATEX)
1109 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1111 isl_die(p->ctx, isl_error_invalid,
1112 "invalid output format for isl_union_set", goto error);
1113 error:
1114 isl_printer_free(p);
1115 return NULL;
1118 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1119 unsigned output_format)
1121 isl_printer *printer;
1123 if (!map)
1124 return;
1126 printer = isl_printer_to_file(map->ctx, out);
1127 printer = isl_printer_set_indent(printer, indent);
1128 printer = isl_printer_set_output_format(printer, output_format);
1129 printer = isl_printer_print_map(printer, map);
1131 isl_printer_free(printer);
1134 void isl_union_map_dump(__isl_keep isl_union_map *umap)
1136 isl_printer *printer;
1138 if (!umap)
1139 return;
1141 printer = isl_printer_to_file(isl_union_map_get_ctx(umap), stderr);
1142 printer = isl_printer_print_union_map(printer, umap);
1143 printer = isl_printer_end_line(printer);
1145 isl_printer_free(printer);
1148 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial *upwqp)
1150 isl_printer *printer;
1152 if (!upwqp)
1153 return;
1155 printer = isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp),
1156 stderr);
1157 printer = isl_printer_print_union_pw_qpolynomial(printer, upwqp);
1158 printer = isl_printer_end_line(printer);
1160 isl_printer_free(printer);
1163 void isl_union_pw_qpolynomial_fold_dump(
1164 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1166 isl_printer *printer;
1168 if (!upwf)
1169 return;
1171 printer = isl_printer_to_file(
1172 isl_union_pw_qpolynomial_fold_get_ctx(upwf), stderr);
1173 printer = isl_printer_print_union_pw_qpolynomial_fold(printer, upwf);
1174 printer = isl_printer_end_line(printer);
1176 isl_printer_free(printer);
1179 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1181 int i;
1182 int n;
1184 for (i = 0, n = 0; i < rec->n; ++i)
1185 if (!isl_upoly_is_zero(rec->p[i]))
1186 ++n;
1188 return n;
1191 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
1192 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1194 int c = p->output_format == ISL_FORMAT_C;
1195 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1196 p = print_affine_of_len(dim, p, div->row[pos] + 1, div->n_col - 1, 1);
1197 p = isl_printer_print_str(p, c ? ", " : ")/");
1198 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1199 p = isl_printer_print_str(p, c ? ")" : "]");
1200 return p;
1203 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1204 __isl_take isl_printer *p, int first)
1206 struct isl_upoly_cst *cst;
1207 int neg;
1209 cst = isl_upoly_as_cst(up);
1210 if (!cst)
1211 goto error;
1212 neg = !first && isl_int_is_neg(cst->n);
1213 if (!first)
1214 p = isl_printer_print_str(p, neg ? " - " : " + ");
1215 if (neg)
1216 isl_int_neg(cst->n, cst->n);
1217 if (isl_int_is_zero(cst->d)) {
1218 int sgn = isl_int_sgn(cst->n);
1219 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1220 sgn == 0 ? "NaN" : "infty");
1221 } else
1222 p = isl_printer_print_isl_int(p, cst->n);
1223 if (neg)
1224 isl_int_neg(cst->n, cst->n);
1225 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1226 p = isl_printer_print_str(p, "/");
1227 p = isl_printer_print_isl_int(p, cst->d);
1229 return p;
1230 error:
1231 isl_printer_free(p);
1232 return NULL;
1235 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1236 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
1238 unsigned total;
1240 total = isl_dim_total(dim);
1241 if (var < total)
1242 p = print_term(dim, dim->ctx->one, 1 + var, p, 1, 0);
1243 else
1244 p = print_div(dim, div, var - total, p);
1245 return p;
1248 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1249 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
1251 p = print_base(p, dim, div, var);
1252 if (exp == 1)
1253 return p;
1254 if (p->output_format == ISL_FORMAT_C) {
1255 int i;
1256 for (i = 1; i < exp; ++i) {
1257 p = isl_printer_print_str(p, "*");
1258 p = print_base(p, dim, div, var);
1260 } else {
1261 p = isl_printer_print_str(p, "^");
1262 p = isl_printer_print_int(p, exp);
1264 return p;
1267 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1268 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
1269 __isl_take isl_printer *p, int outer)
1271 int i, n, first, print_parens;
1272 struct isl_upoly_rec *rec;
1274 if (!p || !up || !dim || !div)
1275 goto error;
1277 if (isl_upoly_is_cst(up))
1278 return upoly_print_cst(up, p, 1);
1280 rec = isl_upoly_as_rec(up);
1281 if (!rec)
1282 goto error;
1283 n = upoly_rec_n_non_zero(rec);
1284 print_parens = n > 1 || (outer && rec->up.var >= isl_dim_total(dim));
1285 if (print_parens)
1286 p = isl_printer_print_str(p, "(");
1287 for (i = 0, first = 1; i < rec->n; ++i) {
1288 if (isl_upoly_is_zero(rec->p[i]))
1289 continue;
1290 if (isl_upoly_is_negone(rec->p[i])) {
1291 if (!i)
1292 p = isl_printer_print_str(p, "-1");
1293 else if (first)
1294 p = isl_printer_print_str(p, "-");
1295 else
1296 p = isl_printer_print_str(p, " - ");
1297 } else if (isl_upoly_is_cst(rec->p[i]) &&
1298 !isl_upoly_is_one(rec->p[i]))
1299 p = upoly_print_cst(rec->p[i], p, first);
1300 else {
1301 if (!first)
1302 p = isl_printer_print_str(p, " + ");
1303 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1304 p = upoly_print(rec->p[i], dim, div, p, 0);
1306 first = 0;
1307 if (i == 0)
1308 continue;
1309 if (!isl_upoly_is_one(rec->p[i]) &&
1310 !isl_upoly_is_negone(rec->p[i]))
1311 p = isl_printer_print_str(p, " * ");
1312 p = print_pow(p, dim, div, rec->up.var, i);
1314 if (print_parens)
1315 p = isl_printer_print_str(p, ")");
1316 return p;
1317 error:
1318 isl_printer_free(p);
1319 return NULL;
1322 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
1323 __isl_keep isl_qpolynomial *qp)
1325 if (!p || !qp)
1326 goto error;
1327 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1328 return p;
1329 error:
1330 isl_printer_free(p);
1331 return NULL;
1334 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1335 unsigned output_format)
1337 isl_printer *p;
1339 if (!qp)
1340 return;
1342 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1343 p = isl_printer_to_file(qp->dim->ctx, out);
1344 p = isl_printer_print_qpolynomial(p, qp);
1345 isl_printer_free(p);
1348 static __isl_give isl_printer *qpolynomial_fold_print(
1349 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1351 int i;
1353 if (fold->type == isl_fold_min)
1354 p = isl_printer_print_str(p, "min");
1355 else if (fold->type == isl_fold_max)
1356 p = isl_printer_print_str(p, "max");
1357 p = isl_printer_print_str(p, "(");
1358 for (i = 0; i < fold->n; ++i) {
1359 if (i)
1360 p = isl_printer_print_str(p, ", ");
1361 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
1363 p = isl_printer_print_str(p, ")");
1364 return p;
1367 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1368 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1370 if (!p || !fold)
1371 goto error;
1372 p = qpolynomial_fold_print(fold, p);
1373 return p;
1374 error:
1375 isl_printer_free(p);
1376 return NULL;
1379 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1380 FILE *out, unsigned output_format)
1382 isl_printer *p;
1384 if (!fold)
1385 return;
1387 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1389 p = isl_printer_to_file(fold->dim->ctx, out);
1390 p = isl_printer_print_qpolynomial_fold(p, fold);
1392 isl_printer_free(p);
1395 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1396 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1398 int i = 0;
1400 for (i = 0; i < pwqp->n; ++i) {
1401 if (i)
1402 p = isl_printer_print_str(p, "; ");
1403 if (isl_dim_size(pwqp->p[i].set->dim, isl_dim_set) > 0 ||
1404 isl_dim_get_tuple_name(pwqp->p[i].set->dim, isl_dim_set)) {
1405 p = print_dim(pwqp->p[i].set->dim, p, 1, 0, NULL);
1406 p = isl_printer_print_str(p, " -> ");
1408 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
1409 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1412 return p;
1415 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1416 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1418 if (!p || !pwqp)
1419 goto error;
1421 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
1422 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1423 p = isl_printer_print_str(p, " -> ");
1425 p = isl_printer_print_str(p, "{ ");
1426 if (pwqp->n == 0) {
1427 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0) {
1428 p = print_dim(pwqp->dim, p, 1, 0, NULL);
1429 p = isl_printer_print_str(p, " -> ");
1431 p = isl_printer_print_str(p, "0");
1433 p = isl_pwqp_print_isl_body(p, pwqp);
1434 p = isl_printer_print_str(p, " }");
1435 return p;
1436 error:
1437 isl_printer_free(p);
1438 return NULL;
1441 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1442 unsigned output_format)
1444 isl_printer *p;
1446 if (!pwqp)
1447 return;
1449 p = isl_printer_to_file(pwqp->dim->ctx, out);
1450 p = isl_printer_set_output_format(p, output_format);
1451 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1453 isl_printer_free(p);
1456 static __isl_give isl_printer *isl_pwf_print_isl_body(
1457 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1459 int i = 0;
1461 for (i = 0; i < pwf->n; ++i) {
1462 if (i)
1463 p = isl_printer_print_str(p, "; ");
1464 if (isl_dim_size(pwf->p[i].set->dim, isl_dim_set) > 0) {
1465 p = print_dim(pwf->p[i].set->dim, p, 1, 0, NULL);
1466 p = isl_printer_print_str(p, " -> ");
1468 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1469 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1472 return p;
1475 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1476 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1478 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1479 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1480 p = isl_printer_print_str(p, " -> ");
1482 p = isl_printer_print_str(p, "{ ");
1483 if (pwf->n == 0) {
1484 if (isl_dim_size(pwf->dim, isl_dim_set) > 0) {
1485 p = print_dim(pwf->dim, p, 1, 0, NULL);
1486 p = isl_printer_print_str(p, " -> ");
1488 p = isl_printer_print_str(p, "0");
1490 p = isl_pwf_print_isl_body(p, pwf);
1491 p = isl_printer_print_str(p, " }");
1492 return p;
1495 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1496 __isl_keep isl_basic_set *bset, isl_int *c);
1498 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1499 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1501 if (type == isl_dim_div) {
1502 p = isl_printer_print_str(p, "floord(");
1503 p = print_affine_c(p, bset, bset->div[pos] + 1);
1504 p = isl_printer_print_str(p, ", ");
1505 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1506 p = isl_printer_print_str(p, ")");
1507 } else {
1508 const char *name;
1510 name = isl_dim_get_name(bset->dim, type, pos);
1511 if (!name)
1512 name = "UNNAMED";
1513 p = isl_printer_print_str(p, name);
1515 return p;
1518 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1519 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1521 enum isl_dim_type type;
1522 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1524 if (pos == 0)
1525 return isl_printer_print_isl_int(p, c);
1527 if (isl_int_is_one(c))
1529 else if (isl_int_is_negone(c))
1530 p = isl_printer_print_str(p, "-");
1531 else {
1532 p = isl_printer_print_isl_int(p, c);
1533 p = isl_printer_print_str(p, "*");
1535 type = pos2type(bset->dim, &pos);
1536 p = print_name_c(p, bset, type, pos);
1537 return p;
1540 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1541 __isl_keep isl_basic_set *bset, isl_int *c)
1543 int i;
1544 int first;
1545 unsigned len = 1 + isl_basic_set_total_dim(bset);
1547 for (i = 0, first = 1; i < len; ++i) {
1548 int flip = 0;
1549 if (isl_int_is_zero(c[i]))
1550 continue;
1551 if (!first) {
1552 if (isl_int_is_neg(c[i])) {
1553 flip = 1;
1554 isl_int_neg(c[i], c[i]);
1555 p = isl_printer_print_str(p, " - ");
1556 } else
1557 p = isl_printer_print_str(p, " + ");
1559 first = 0;
1560 p = print_term_c(p, bset, c[i], i);
1561 if (flip)
1562 isl_int_neg(c[i], c[i]);
1564 if (first)
1565 p = isl_printer_print_str(p, "0");
1566 return p;
1569 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1570 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1572 if (!first)
1573 p = isl_printer_print_str(p, " && ");
1575 p = print_affine_c(p, bset, c);
1576 p = isl_printer_print_str(p, " ");
1577 p = isl_printer_print_str(p, op);
1578 p = isl_printer_print_str(p, " 0");
1579 return p;
1582 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1583 __isl_keep isl_basic_set *bset)
1585 int i;
1587 for (i = 0; i < bset->n_eq; ++i)
1588 p = print_constraint_c(p, bset, bset->eq[i], "==", !i);
1589 for (i = 0; i < bset->n_ineq; ++i)
1590 p = print_constraint_c(p, bset, bset->ineq[i], ">=",
1591 !bset->n_eq && !i);
1592 return p;
1595 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1596 __isl_keep isl_set *set)
1598 int i;
1600 if (set->n == 0)
1601 p = isl_printer_print_str(p, "0");
1603 for (i = 0; i < set->n; ++i) {
1604 if (i)
1605 p = isl_printer_print_str(p, " || ");
1606 if (set->n > 1)
1607 p = isl_printer_print_str(p, "(");
1608 p = print_basic_set_c(p, set->p[i]);
1609 if (set->n > 1)
1610 p = isl_printer_print_str(p, ")");
1612 return p;
1615 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1616 __isl_keep isl_qpolynomial *qp)
1618 isl_int den;
1620 isl_int_init(den);
1621 isl_qpolynomial_get_den(qp, &den);
1622 if (!isl_int_is_one(den)) {
1623 isl_qpolynomial *f;
1624 p = isl_printer_print_str(p, "(");
1625 qp = isl_qpolynomial_copy(qp);
1626 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1627 den, qp->dim->ctx->one);
1628 qp = isl_qpolynomial_mul(qp, f);
1630 if (qp)
1631 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 0);
1632 if (!isl_int_is_one(den)) {
1633 p = isl_printer_print_str(p, ")/");
1634 p = isl_printer_print_isl_int(p, den);
1635 isl_qpolynomial_free(qp);
1637 isl_int_clear(den);
1638 return p;
1641 static __isl_give isl_printer *print_pw_qpolynomial_c(
1642 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwpq)
1644 int i;
1646 if (pwpq->n == 1 && isl_set_fast_is_universe(pwpq->p[0].set))
1647 return print_qpolynomial_c(p, pwpq->p[0].qp);
1649 for (i = 0; i < pwpq->n; ++i) {
1650 p = isl_printer_print_str(p, "(");
1651 p = print_set_c(p, pwpq->p[i].set);
1652 p = isl_printer_print_str(p, ") ? (");
1653 p = print_qpolynomial_c(p, pwpq->p[i].qp);
1654 p = isl_printer_print_str(p, ") : ");
1657 p = isl_printer_print_str(p, "0");
1658 return p;
1661 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1662 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1664 if (!p || !pwqp)
1665 goto error;
1667 if (p->output_format == ISL_FORMAT_ISL)
1668 return print_pw_qpolynomial_isl(p, pwqp);
1669 else if (p->output_format == ISL_FORMAT_C)
1670 return print_pw_qpolynomial_c(p, pwqp);
1671 isl_assert(p->ctx, 0, goto error);
1672 error:
1673 isl_printer_free(p);
1674 return NULL;
1677 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1679 struct isl_union_print_data *data;
1680 data = (struct isl_union_print_data *)user;
1682 if (!data->first)
1683 data->p = isl_printer_print_str(data->p, "; ");
1684 data->first = 0;
1686 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1687 isl_pw_qpolynomial_free(pwqp);
1689 return 0;
1692 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1693 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1695 struct isl_union_print_data data = { p, 1, 1 };
1696 isl_dim *dim;
1697 dim = isl_union_pw_qpolynomial_get_dim(upwqp);
1698 if (isl_dim_size(dim, isl_dim_param) > 0) {
1699 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1700 p = isl_printer_print_str(p, " -> ");
1702 isl_dim_free(dim);
1703 p = isl_printer_print_str(p, "{ ");
1704 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1705 &data);
1706 p = data.p;
1707 p = isl_printer_print_str(p, " }");
1708 return p;
1711 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1712 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1714 if (!p || !upwqp)
1715 goto error;
1717 if (p->output_format == ISL_FORMAT_ISL)
1718 return print_union_pw_qpolynomial_isl(p, upwqp);
1719 isl_die(p->ctx, isl_error_invalid,
1720 "invalid output format for isl_union_pw_qpolynomial",
1721 goto error);
1722 error:
1723 isl_printer_free(p);
1724 return NULL;
1727 static __isl_give isl_printer *print_qpolynomial_fold_c(
1728 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1730 int i;
1732 for (i = 0; i < fold->n - 1; ++i)
1733 if (fold->type == isl_fold_min)
1734 p = isl_printer_print_str(p, "min(");
1735 else if (fold->type == isl_fold_max)
1736 p = isl_printer_print_str(p, "max(");
1738 for (i = 0; i < fold->n; ++i) {
1739 if (i)
1740 p = isl_printer_print_str(p, ", ");
1741 p = print_qpolynomial_c(p, fold->qp[i]);
1742 if (i)
1743 p = isl_printer_print_str(p, ")");
1745 return p;
1748 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1749 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1751 int i;
1753 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1754 return print_qpolynomial_fold_c(p, pwf->p[0].fold);
1756 for (i = 0; i < pwf->n; ++i) {
1757 p = isl_printer_print_str(p, "(");
1758 p = print_set_c(p, pwf->p[i].set);
1759 p = isl_printer_print_str(p, ") ? (");
1760 p = print_qpolynomial_fold_c(p, pwf->p[i].fold);
1761 p = isl_printer_print_str(p, ") : ");
1764 p = isl_printer_print_str(p, "0");
1765 return p;
1768 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1769 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1771 if (!p || !pwf)
1772 goto error;
1774 if (p->output_format == ISL_FORMAT_ISL)
1775 return print_pw_qpolynomial_fold_isl(p, pwf);
1776 else if (p->output_format == ISL_FORMAT_C)
1777 return print_pw_qpolynomial_fold_c(p, pwf);
1778 isl_assert(p->ctx, 0, goto error);
1779 error:
1780 isl_printer_free(p);
1781 return NULL;
1784 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1785 FILE *out, unsigned output_format)
1787 isl_printer *p;
1789 if (!pwf)
1790 return;
1792 p = isl_printer_to_file(pwf->dim->ctx, out);
1793 p = isl_printer_set_output_format(p, output_format);
1794 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1796 isl_printer_free(p);
1799 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1801 struct isl_union_print_data *data;
1802 data = (struct isl_union_print_data *)user;
1804 if (!data->first)
1805 data->p = isl_printer_print_str(data->p, "; ");
1806 data->first = 0;
1808 data->p = isl_pwf_print_isl_body(data->p, pwf);
1809 isl_pw_qpolynomial_fold_free(pwf);
1811 return 0;
1814 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1815 __isl_take isl_printer *p,
1816 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1818 struct isl_union_print_data data = { p, 1, 1 };
1819 isl_dim *dim;
1820 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
1821 if (isl_dim_size(dim, isl_dim_param) > 0) {
1822 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1823 p = isl_printer_print_str(p, " -> ");
1825 isl_dim_free(dim);
1826 p = isl_printer_print_str(p, "{ ");
1827 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1828 &print_pwf_body, &data);
1829 p = data.p;
1830 p = isl_printer_print_str(p, " }");
1831 return p;
1834 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1835 __isl_take isl_printer *p,
1836 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1838 if (!p || !upwf)
1839 goto error;
1841 if (p->output_format == ISL_FORMAT_ISL)
1842 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1843 isl_die(p->ctx, isl_error_invalid,
1844 "invalid output format for isl_union_pw_qpolynomial_fold",
1845 goto error);
1846 error:
1847 isl_printer_free(p);
1848 return NULL;