add isl_union_map_deltas_map
[platform/upstream/isl.git] / isl_output.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
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 
11  */
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <isl_map_private.h>
16 #include <isl/set.h>
17 #include <isl/seq.h>
18 #include <isl_polynomial_private.h>
19 #include <isl_printer_private.h>
20 #include <isl_dim_private.h>
21 #include <isl_mat_private.h>
22 #include <isl/union_map.h>
23
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_" };
40
41 static __isl_give isl_printer *print_constraint_polylib(
42         struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
43 {
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];
49
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]);
55         }
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]);
59         }
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]);
63         }
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]);
67         }
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;
72 }
73
74 static __isl_give isl_printer *print_constraints_polylib(
75         struct isl_basic_map *bmap, __isl_take isl_printer *p)
76 {
77         int i;
78
79         p = isl_printer_set_isl_int_width(p, 5);
80
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);
85
86         return p;
87 }
88
89 static __isl_give isl_printer *bset_print_constraints_polylib(
90         struct isl_basic_set *bset, __isl_take isl_printer *p)
91 {
92         return print_constraints_polylib((struct isl_basic_map *)bset, p);
93 }
94
95 static __isl_give isl_printer *isl_basic_map_print_polylib(
96         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
97 {
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         if (ext) {
104                 p = isl_printer_print_str(p, " ");
105                 p = isl_printer_print_int(p,
106                                     isl_basic_map_dim(bmap, isl_dim_out));
107                 p = isl_printer_print_str(p, " ");
108                 p = isl_printer_print_int(p,
109                                     isl_basic_map_dim(bmap, isl_dim_in));
110                 p = isl_printer_print_str(p, " ");
111                 p = isl_printer_print_int(p,
112                                     isl_basic_map_dim(bmap, isl_dim_div));
113                 p = isl_printer_print_str(p, " ");
114                 p = isl_printer_print_int(p,
115                                     isl_basic_map_dim(bmap, isl_dim_param));
116         }
117         p = isl_printer_end_line(p);
118         return print_constraints_polylib(bmap, p);
119 }
120
121 static __isl_give isl_printer *isl_basic_set_print_polylib(
122         __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
123 {
124         return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
125 }
126
127 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
128         __isl_take isl_printer *p, int ext)
129 {
130         int i;
131
132         p = isl_printer_start_line(p);
133         p = isl_printer_print_int(p, map->n);
134         p = isl_printer_end_line(p);
135         for (i = 0; i < map->n; ++i) {
136                 p = isl_printer_start_line(p);
137                 p = isl_printer_end_line(p);
138                 p = isl_basic_map_print_polylib(map->p[i], p, ext);
139         }
140         return p;
141 }
142
143 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
144         __isl_take isl_printer *p, int ext)
145 {
146         return isl_map_print_polylib((struct isl_map *)set, p, ext);
147 }
148
149 static int count_same_name(__isl_keep isl_dim *dim,
150         enum isl_dim_type type, unsigned pos, const char *name)
151 {
152         enum isl_dim_type t;
153         unsigned p, s;
154         int count = 0;
155
156         for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
157                 s = t == type ? pos : isl_dim_size(dim, t);
158                 for (p = 0; p < s; ++p) {
159                         const char *n = isl_dim_get_name(dim, t, p);
160                         if (n && !strcmp(n, name))
161                                 count++;
162                 }
163         }
164         return count;
165 }
166
167 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
168         __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
169         int set, int latex)
170 {
171         const char *name;
172         char buffer[20];
173         int primes;
174
175         name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
176
177         if (!name) {
178                 const char *prefix;
179                 if (type == isl_dim_param)
180                         prefix = s_param_prefix[latex];
181                 else if (type == isl_dim_div)
182                         prefix = s_div_prefix[latex];
183                 else if (set || type == isl_dim_in)
184                         prefix = s_input_prefix[latex];
185                 else
186                         prefix = s_output_prefix[latex];
187                 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
188                 name = buffer;
189         }
190         primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
191                                  pos, name);
192         p = isl_printer_print_str(p, name);
193         while (primes-- > 0)
194                 p = isl_printer_print_str(p, "'");
195         return p;
196 }
197
198 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
199 {
200         enum isl_dim_type type;
201         unsigned n_in = isl_dim_size(dim, isl_dim_in);
202         unsigned n_out = isl_dim_size(dim, isl_dim_out);
203         unsigned nparam = isl_dim_size(dim, isl_dim_param);
204
205         if (*pos < 1 + nparam) {
206                 type = isl_dim_param;
207                 *pos -= 1;
208         } else if (*pos < 1 + nparam + n_in) {
209                 type = isl_dim_in;
210                 *pos -= 1 + nparam;
211         } else if (*pos < 1 + nparam + n_in + n_out) {
212                 type = isl_dim_out;
213                 *pos -= 1 + nparam + n_in;
214         } else {
215                 type = isl_dim_div;
216                 *pos -= 1 + nparam + n_in + n_out;
217         }
218
219         return type;
220 }
221
222 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
223         __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
224
225 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
226         __isl_keep isl_mat *div,
227         isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
228 {
229         enum isl_dim_type type;
230         int print_div_def;
231
232         if (pos == 0)
233                 return isl_printer_print_isl_int(p, c);
234
235         type = pos2type(dim, &pos);
236         print_div_def = type == isl_dim_div && div &&
237                         !isl_int_is_zero(div->row[pos][0]);
238
239         if (isl_int_is_one(c))
240                 ;
241         else if (isl_int_is_negone(c))
242                 p = isl_printer_print_str(p, "-");
243         else {
244                 p = isl_printer_print_isl_int(p, c);
245                 if (p->output_format == ISL_FORMAT_C || print_div_def)
246                         p = isl_printer_print_str(p, "*");
247         }
248         if (print_div_def)
249                 p = print_div(dim, div, pos, p);
250         else
251                 p = print_name(dim, p, type, pos, set, latex);
252         return p;
253 }
254
255 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
256         __isl_keep isl_mat *div,
257         __isl_take isl_printer *p, isl_int *c, int len, int set)
258 {
259         int i;
260         int first;
261
262         for (i = 0, first = 1; i < len; ++i) {
263                 int flip = 0;
264                 if (isl_int_is_zero(c[i]))
265                         continue;
266                 if (!first) {
267                         if (isl_int_is_neg(c[i])) {
268                                 flip = 1;
269                                 isl_int_neg(c[i], c[i]);
270                                 p = isl_printer_print_str(p, " - ");
271                         } else 
272                                 p = isl_printer_print_str(p, " + ");
273                 }
274                 first = 0;
275                 p = print_term(dim, div, c[i], i, p, set, 0);
276                 if (flip)
277                         isl_int_neg(c[i], c[i]);
278         }
279         if (first)
280                 p = isl_printer_print_str(p, "0");
281         return p;
282 }
283
284 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
285         __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
286 {
287         unsigned len = 1 + isl_basic_map_total_dim(bmap);
288         return print_affine_of_len(dim, NULL, p, c, len, set);
289 }
290
291 static int defining_equality(__isl_keep isl_basic_map *eq,
292         __isl_keep isl_dim *dim, enum isl_dim_type type, int pos)
293 {
294         int i;
295         unsigned total;
296
297         if (!eq)
298                 return -1;
299
300         pos += isl_dim_offset(dim, type);
301         total = isl_basic_map_total_dim(eq);
302
303         for (i = 0; i < eq->n_eq; ++i) {
304                 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
305                         continue;
306                 if (isl_int_is_one(eq->eq[i][1 + pos]))
307                         isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
308                 return i;
309         }
310
311         return -1;
312 }
313
314 /* offset is the offset of local_dim inside global_type of global_dim.
315  */
316 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
317         __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
318         __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
319         int set, int latex, __isl_keep isl_basic_map *eq, int offset)
320 {
321         int i, j;
322
323         if (global_dim != local_dim && local_type == isl_dim_out)
324                 offset += local_dim->n_in;
325
326         for (i = 0; i < isl_dim_size(local_dim, local_type); ++i) {
327                 if (i)
328                         p = isl_printer_print_str(p, ", ");
329                 j = defining_equality(eq, global_dim, global_type, offset + i);
330                 if (j >= 0) {
331                         int pos = 1 + isl_dim_offset(global_dim, global_type)
332                                     + offset + i;
333                         p = print_affine_of_len(eq->dim, NULL,
334                                                 p, eq->eq[j], pos, set);
335                 } else {
336                         p = print_name(global_dim, p, global_type, offset + i,
337                                         set, latex);
338                 }
339         }
340         return p;
341 }
342
343 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
344         __isl_take isl_printer *p, enum isl_dim_type type,
345         int set, int latex, __isl_keep isl_basic_map *eq)
346 {
347         return print_nested_var_list(p, dim, type, dim, type, set, latex, eq, 0);
348 }
349
350 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
351         __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
352         __isl_keep isl_dim *local_dim,
353          int set, int latex, __isl_keep isl_basic_map *eq, int offset);
354
355 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
356         __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
357         __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
358         int set, int latex, __isl_keep isl_basic_map *eq, int offset)
359 {
360         const char *name = NULL;
361         unsigned n = isl_dim_size(local_dim, local_type);
362         if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
363                 name = isl_dim_get_tuple_name(local_dim, local_type);
364                 if (name) {
365                         if (latex)
366                                 p = isl_printer_print_str(p, "\\mathrm{");
367                         p = isl_printer_print_str(p, name);
368                         if (latex)
369                                 p = isl_printer_print_str(p, "}");
370                 }
371         }
372         if (!latex || n != 1 || name)
373                 p = isl_printer_print_str(p, s_open_list[latex]);
374         if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
375             local_dim->nested[local_type - isl_dim_in]) {
376                 if (global_dim != local_dim && local_type == isl_dim_out)
377                         offset += local_dim->n_in;
378                 p = print_nested_map_dim(p, global_dim, global_type,
379                                 local_dim->nested[local_type - isl_dim_in],
380                                 set, latex, eq, offset);
381         } else
382                 p = print_nested_var_list(p, global_dim, global_type,
383                                  local_dim, local_type, set, latex, eq, offset);
384         if (!latex || n != 1 || name)
385                 p = isl_printer_print_str(p, s_close_list[latex]);
386         return p;
387 }
388
389 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
390         __isl_take isl_printer *p, enum isl_dim_type type,
391         int set, int latex, __isl_keep isl_basic_map *eq)
392 {
393         return print_nested_tuple(p, dim, type, dim, type, set, latex, eq, 0);
394 }
395
396 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
397         __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
398         __isl_keep isl_dim *local_dim,
399          int set, int latex, __isl_keep isl_basic_map *eq, int offset)
400 {
401         p = print_nested_tuple(p, global_dim, global_type,
402                         local_dim, isl_dim_in, set, latex, eq, offset);
403         p = isl_printer_print_str(p, s_to[latex]);
404         p = print_nested_tuple(p, global_dim, global_type,
405                         local_dim, isl_dim_out, set, latex, eq, offset);
406
407         return p;
408 }
409
410 static __isl_give isl_printer *print_dim(__isl_keep isl_dim *dim,
411         __isl_take isl_printer *p, int set, int latex, int rational,
412         __isl_keep isl_basic_map *eq)
413 {
414         if (rational && !latex)
415                 p = isl_printer_print_str(p, "rat: ");
416         if (set)
417                 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
418         else {
419                 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
420                 p = isl_printer_print_str(p, s_to[latex]);
421                 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
422         }
423
424         return p;
425 }
426
427 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
428         __isl_take isl_printer *p)
429 {
430         if (isl_dim_size(dim, isl_dim_param) == 0)
431                 return p;
432
433         p = isl_printer_start_line(p);
434         p = isl_printer_print_str(p, "symbolic ");
435         p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
436         p = isl_printer_print_str(p, ";");
437         p = isl_printer_end_line(p);
438         return p;
439 }
440
441 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
442         __isl_keep isl_dim *dim, __isl_take isl_printer *p,
443         isl_int *c, int last, const char *op, int first_constraint, int set,
444         int latex)
445 {
446         if (!first_constraint)
447                 p = isl_printer_print_str(p, s_and[latex]);
448
449         isl_int_abs(c[last], c[last]);
450
451         p = print_term(dim, NULL, c[last], last, p, set, latex);
452
453         p = isl_printer_print_str(p, " ");
454         p = isl_printer_print_str(p, op);
455         p = isl_printer_print_str(p, " ");
456
457         isl_int_set_si(c[last], 0);
458         p = print_affine(bmap, dim, p, c, set);
459
460         return p;
461 }
462
463 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
464         __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
465 {
466         int i;
467         struct isl_vec *c;
468         unsigned total = isl_basic_map_total_dim(bmap);
469
470         c = isl_vec_alloc(bmap->ctx, 1 + total);
471         if (!c)
472                 goto error;
473
474         for (i = bmap->n_eq - 1; i >= 0; --i) {
475                 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
476                 isl_assert(bmap->ctx, l >= 0, goto error);
477                 if (isl_int_is_neg(bmap->eq[i][l]))
478                         isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
479                 else
480                         isl_seq_neg(c->el, bmap->eq[i], 1 + total);
481                 p = print_constraint(bmap, dim, p, c->el, l,
482                                     "=", i == bmap->n_eq - 1, set, latex);
483         }
484         for (i = 0; i < bmap->n_ineq; ++i) {
485                 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
486                 int s;
487                 const char *op;
488                 if (l < 0)
489                         continue;
490                 s = isl_int_sgn(bmap->ineq[i][l]);
491                 if (s < 0)
492                         isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
493                 else
494                         isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
495                 op = s < 0 ? s_le[latex] : s_ge[latex];
496                 p = print_constraint(bmap, dim, p, c->el, l,
497                                         op, !bmap->n_eq && !i, set, latex);
498         }
499
500         isl_vec_free(c);
501
502         return p;
503 error:
504         isl_vec_free(c);
505         isl_printer_free(p);
506         return NULL;
507 }
508
509 static __isl_give isl_printer *print_omega_constraints(
510         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
511 {
512         if (bmap->n_eq + bmap->n_ineq == 0)
513                 return p;
514
515         p = isl_printer_print_str(p, ": ");
516         if (bmap->n_div > 0) {
517                 int i;
518                 p = isl_printer_print_str(p, "exists (");
519                 for (i = 0; i < bmap->n_div; ++i) {
520                         if (i)
521                                 p = isl_printer_print_str(p, ", ");
522                         p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
523                 }
524                 p = isl_printer_print_str(p, ": ");
525         }
526         p = print_constraints(bmap, bmap->dim, p, set, 0);
527         if (bmap->n_div > 0)
528                 p = isl_printer_print_str(p, ")");
529         return p;
530 }
531
532 static __isl_give isl_printer *basic_map_print_omega(
533         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
534 {
535         p = isl_printer_print_str(p, "{ [");
536         p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
537         p = isl_printer_print_str(p, "] -> [");
538         p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
539         p = isl_printer_print_str(p, "] ");
540         p = print_omega_constraints(bmap, p, 0);
541         p = isl_printer_print_str(p, " }");
542         return p;
543 }
544
545 static __isl_give isl_printer *isl_basic_map_print_omega(
546         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
547 {
548         p = print_omega_parameters(bmap->dim, p);
549
550         p = isl_printer_start_line(p);
551         p = basic_map_print_omega(bmap, p);
552         p = isl_printer_end_line(p);
553         return p;
554 }
555
556 static __isl_give isl_printer *basic_set_print_omega(
557         __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
558 {
559         p = isl_printer_print_str(p, "{ [");
560         p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
561         p = isl_printer_print_str(p, "] ");
562         p = print_omega_constraints((isl_basic_map *)bset, p, 1);
563         p = isl_printer_print_str(p, " }");
564         return p;
565 }
566
567 static __isl_give isl_printer *isl_basic_set_print_omega(
568         __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
569 {
570         p = print_omega_parameters(bset->dim, p);
571
572         p = isl_printer_start_line(p);
573         p = basic_set_print_omega(bset, p);
574         p = isl_printer_end_line(p);
575         return p;
576 }
577
578 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
579         __isl_take isl_printer *p)
580 {
581         int i;
582
583         p = print_omega_parameters(map->dim, p);
584
585         p = isl_printer_start_line(p);
586         for (i = 0; i < map->n; ++i) {
587                 if (i)
588                         p = isl_printer_print_str(p, " union ");
589                 p = basic_map_print_omega(map->p[i], p);
590         }
591         p = isl_printer_end_line(p);
592         return p;
593 }
594
595 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
596         __isl_take isl_printer *p)
597 {
598         int i;
599
600         p = print_omega_parameters(set->dim, p);
601
602         p = isl_printer_start_line(p);
603         for (i = 0; i < set->n; ++i) {
604                 if (i)
605                         p = isl_printer_print_str(p, " union ");
606                 p = basic_set_print_omega(set->p[i], p);
607         }
608         p = isl_printer_end_line(p);
609         return p;
610 }
611
612 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
613         __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
614 {
615         if (bmap->n_div > 0) {
616                 int i;
617                 p = isl_printer_print_str(p, s_open_exists[latex]);
618                 for (i = 0; i < bmap->n_div; ++i) {
619                         if (i)
620                                 p = isl_printer_print_str(p, ", ");
621                         p = print_name(dim, p, isl_dim_div, i, 0, latex);
622                         if (latex || isl_int_is_zero(bmap->div[i][0]))
623                                 continue;
624                         p = isl_printer_print_str(p, " = [(");
625                         p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
626                         p = isl_printer_print_str(p, ")/");
627                         p = isl_printer_print_isl_int(p, bmap->div[i][0]);
628                         p = isl_printer_print_str(p, "]");
629                 }
630                 p = isl_printer_print_str(p, ": ");
631         }
632
633         p = print_constraints(bmap, dim, p, set, latex);
634
635         if (bmap->n_div > 0)
636                 p = isl_printer_print_str(p, s_close_exists[latex]);
637         return p;
638 }
639
640 static __isl_give isl_printer *isl_basic_map_print_isl(
641         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
642         int set, int latex)
643 {
644         int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
645         if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
646                 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
647                 p = isl_printer_print_str(p, " -> ");
648         }
649         p = isl_printer_print_str(p, "{ ");
650         p = print_dim(bmap->dim, p, set, latex, rational, NULL);
651         p = isl_printer_print_str(p, " : ");
652         p = print_disjunct(bmap, bmap->dim, p, set, latex);
653         p = isl_printer_print_str(p, " }");
654         return p;
655 }
656
657 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
658         __isl_take isl_printer *p, int set, int latex)
659 {
660         int i;
661
662         if (isl_map_fast_is_universe(map))
663                 return p;
664
665         p = isl_printer_print_str(p, s_such_that[latex]);
666         if (map->n == 0)
667                 p = isl_printer_print_str(p, "1 = 0");
668         for (i = 0; i < map->n; ++i) {
669                 if (i)
670                         p = isl_printer_print_str(p, s_or[latex]);
671                 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
672                         p = isl_printer_print_str(p, "(");
673                 p = print_disjunct(map->p[i], map->dim, p, set, latex);
674                 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
675                         p = isl_printer_print_str(p, ")");
676         }
677         return p;
678 }
679
680 struct isl_aff_split {
681         isl_basic_map *aff;
682         isl_map *map;
683 };
684
685 static void free_split(__isl_take struct isl_aff_split *split, int n)
686 {
687         int i;
688
689         if (!split)
690                 return;
691
692         for (i = 0; i < n; ++i) {
693                 isl_basic_map_free(split[i].aff);
694                 isl_map_free(split[i].map);
695         }
696
697         free(split);
698 }
699
700 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
701 {
702         int i, j;
703         unsigned nparam, n_in, n_out, total;
704
705         bmap = isl_basic_map_cow(bmap);
706         if (!bmap)
707                 return NULL;
708         if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
709                 goto error;
710
711         nparam = isl_basic_map_dim(bmap, isl_dim_param);
712         n_in = isl_basic_map_dim(bmap, isl_dim_in);
713         n_out = isl_basic_map_dim(bmap, isl_dim_out);
714         total = isl_basic_map_dim(bmap, isl_dim_all);
715         for (i = bmap->n_eq - 1; i >= 0; --i) {
716                 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
717                 if (j >= nparam && j < nparam + n_in + n_out &&
718                     (isl_int_is_one(bmap->eq[i][1 + j]) ||
719                      isl_int_is_negone(bmap->eq[i][1 + j])))
720                         continue;
721                 if (isl_basic_map_drop_equality(bmap, i) < 0)
722                         goto error;
723         }
724
725         bmap = isl_basic_map_finalize(bmap);
726
727         return bmap;
728 error:
729         isl_basic_map_free(bmap);
730         return NULL;
731 }
732
733 static int aff_split_cmp(const void *p1, const void *p2)
734 {
735         const struct isl_aff_split *s1, *s2;
736         s1 = (const struct isl_aff_split *) p1;
737         s2 = (const struct isl_aff_split *) p2;
738
739         return isl_basic_map_fast_cmp(s1->aff, s2->aff);
740 }
741
742 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
743         __isl_keep isl_basic_map *aff)
744 {
745         int i, j;
746         unsigned total;
747
748         if (!bmap || !aff)
749                 goto error;
750
751         total = isl_dim_total(bmap->dim);
752
753         for (i = bmap->n_eq - 1; i >= 0; --i) {
754                 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
755                                             bmap->n_div) != -1)
756                         continue;
757                 for (j = 0; j < aff->n_eq; ++j) {
758                         if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
759                             !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
760                                 continue;
761                         if (isl_basic_map_drop_equality(bmap, i) < 0)
762                                 goto error;
763                         break;
764                 }
765         }
766
767         return bmap;
768 error:
769         isl_basic_map_free(bmap);
770         return NULL;
771 }
772
773 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
774 {
775         int i, n;
776         struct isl_aff_split *split;
777
778         split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
779         if (!split)
780                 return NULL;
781         
782         for (i = 0; i < map->n; ++i) {
783                 isl_basic_map *bmap;
784                 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
785                 bmap = isl_basic_map_copy(map->p[i]);
786                 bmap = isl_basic_map_cow(bmap);
787                 bmap = drop_aff(bmap, split[i].aff);
788                 split[i].map = isl_map_from_basic_map(bmap);
789                 if (!split[i].aff || !split[i].map)
790                         goto error;
791         }
792
793         qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
794
795         n = map->n;
796         for (i = n - 1; i >= 1; --i) {
797                 if (!isl_basic_map_fast_is_equal(split[i - 1].aff,
798                                                  split[i].aff))
799                         continue;
800                 isl_basic_map_free(split[i].aff);
801                 split[i - 1].map = isl_map_union(split[i - 1].map,
802                                                  split[i].map);
803                 if (i != n - 1)
804                         split[i] = split[n - 1];
805                 split[n - 1].aff = NULL;
806                 split[n - 1].map = NULL;
807                 --n;
808         }
809
810         return split;
811 error:
812         free_split(split, map->n);
813         return NULL;
814 }
815
816 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
817         struct isl_aff_split *split, int n, int set)
818 {
819         int i;
820         int rational;
821
822         for (i = 0; i < n; ++i) {
823                 isl_dim *dim;
824
825                 if (!split[i].map)
826                         break;
827                 dim = split[i].map->dim;
828                 rational = split[i].map->n > 0 &&
829                     ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
830                 if (i)
831                         p = isl_printer_print_str(p, "; ");
832                 p = print_dim(dim, p, set, 0, rational, split[i].aff);
833                 p = print_disjuncts(split[i].map, p, set, 0);
834         }
835
836         return p;
837 }
838
839 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
840         __isl_take isl_printer *p, int set)
841 {
842         struct isl_aff_split *split = NULL;
843         int rational;
844
845         if (map->n > 0)
846                 split = split_aff(map);
847         if (split) {
848                 p = print_split_map(p, split, map->n, set);
849         } else {
850                 rational = map->n > 0 &&
851                     ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
852                 p = print_dim(map->dim, p, set, 0, rational, NULL);
853                 p = print_disjuncts(map, p, set, 0);
854         }
855         free_split(split, map->n);
856         return p;
857 }
858
859 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
860         __isl_take isl_printer *p, int set)
861 {
862         if (isl_map_dim(map, isl_dim_param) > 0) {
863                 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
864                 p = isl_printer_print_str(p, s_to[0]);
865         }
866         p = isl_printer_print_str(p, s_open_set[0]);
867         p = isl_map_print_isl_body(map, p, set);
868         p = isl_printer_print_str(p, s_close_set[0]);
869         return p;
870 }
871
872 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
873         __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
874 {
875         if (isl_map_dim(map, isl_dim_param) > 0) {
876                 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
877                 p = isl_printer_print_str(p, s_to[1]);
878         }
879         p = isl_printer_print_str(p, s_open_set[1]);
880         p = print_dim(map->dim, p, set, 1, 0, aff);
881         p = print_disjuncts(map, p, set, 1);
882         p = isl_printer_print_str(p, s_close_set[1]);
883
884         return p;
885 }
886
887 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
888         __isl_take isl_printer *p, int set)
889 {
890         int i;
891         struct isl_aff_split *split = NULL;
892
893         if (map->n > 0)
894                 split = split_aff(map);
895
896         if (!split)
897                 return print_latex_map(map, p, NULL, set);
898
899         for (i = 0; i < map->n; ++i) {
900                 if (!split[i].map)
901                         break;
902                 if (i)
903                         p = isl_printer_print_str(p, " \\cup ");
904                 p = print_latex_map(split[i].map, p, split[i].aff, set);
905         }
906
907         free_split(split, map->n);
908         return p;
909 }
910
911 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
912         __isl_keep isl_basic_map *bmap)
913 {
914         if (!p || !bmap)
915                 goto error;
916         if (p->output_format == ISL_FORMAT_ISL)
917                 return isl_basic_map_print_isl(bmap, p, 0, 0);
918         else if (p->output_format == ISL_FORMAT_OMEGA)
919                 return isl_basic_map_print_omega(bmap, p);
920         isl_assert(bmap->ctx, 0, goto error);
921 error:
922         isl_printer_free(p);
923         return NULL;
924 }
925
926 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
927         const char *prefix, const char *suffix, unsigned output_format)
928 {
929         isl_printer *printer;
930
931         if (!bmap)
932                 return;
933
934         printer = isl_printer_to_file(bmap->ctx, out);
935         printer = isl_printer_set_indent(printer, indent);
936         printer = isl_printer_set_prefix(printer, prefix);
937         printer = isl_printer_set_suffix(printer, suffix);
938         printer = isl_printer_set_output_format(printer, output_format);
939         isl_printer_print_basic_map(printer, bmap);
940
941         isl_printer_free(printer);
942 }
943
944 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
945         __isl_keep isl_basic_set *bset)
946 {
947         if (!p || !bset)
948                 goto error;
949
950         if (p->output_format == ISL_FORMAT_ISL)
951                 return isl_basic_map_print_isl(bset, p, 1, 0);
952         else if (p->output_format == ISL_FORMAT_POLYLIB)
953                 return isl_basic_set_print_polylib(bset, p, 0);
954         else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
955                 return isl_basic_set_print_polylib(bset, p, 1);
956         else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
957                 return bset_print_constraints_polylib(bset, p);
958         else if (p->output_format == ISL_FORMAT_OMEGA)
959                 return isl_basic_set_print_omega(bset, p);
960         isl_assert(p->ctx, 0, goto error);
961 error:
962         isl_printer_free(p);
963         return NULL;
964 }
965
966 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
967         const char *prefix, const char *suffix, unsigned output_format)
968 {
969         isl_printer *printer;
970
971         if (!bset)
972                 return;
973
974         printer = isl_printer_to_file(bset->ctx, out);
975         printer = isl_printer_set_indent(printer, indent);
976         printer = isl_printer_set_prefix(printer, prefix);
977         printer = isl_printer_set_suffix(printer, suffix);
978         printer = isl_printer_set_output_format(printer, output_format);
979         isl_printer_print_basic_set(printer, bset);
980
981         isl_printer_free(printer);
982 }
983
984 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
985         __isl_keep isl_set *set)
986 {
987         if (!p || !set)
988                 goto error;
989         if (p->output_format == ISL_FORMAT_ISL)
990                 return isl_map_print_isl((isl_map *)set, p, 1);
991         else if (p->output_format == ISL_FORMAT_POLYLIB)
992                 return isl_set_print_polylib(set, p, 0);
993         else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
994                 return isl_set_print_polylib(set, p, 1);
995         else if (p->output_format == ISL_FORMAT_OMEGA)
996                 return isl_set_print_omega(set, p);
997         else if (p->output_format == ISL_FORMAT_LATEX)
998                 return isl_map_print_latex((isl_map *)set, p, 1);
999         isl_assert(set->ctx, 0, goto error);
1000 error:
1001         isl_printer_free(p);
1002         return NULL;
1003 }
1004
1005 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1006         unsigned output_format)
1007 {
1008         isl_printer *printer;
1009
1010         if (!set)
1011                 return;
1012
1013         printer = isl_printer_to_file(set->ctx, out);
1014         printer = isl_printer_set_indent(printer, indent);
1015         printer = isl_printer_set_output_format(printer, output_format);
1016         printer = isl_printer_print_set(printer, set);
1017
1018         isl_printer_free(printer);
1019 }
1020
1021 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1022         __isl_keep isl_map *map)
1023 {
1024         if (!p || !map)
1025                 goto error;
1026
1027         if (p->output_format == ISL_FORMAT_ISL)
1028                 return isl_map_print_isl(map, p, 0);
1029         else if (p->output_format == ISL_FORMAT_POLYLIB)
1030                 return isl_map_print_polylib(map, p, 0);
1031         else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1032                 return isl_map_print_polylib(map, p, 1);
1033         else if (p->output_format == ISL_FORMAT_OMEGA)
1034                 return isl_map_print_omega(map, p);
1035         else if (p->output_format == ISL_FORMAT_LATEX)
1036                 return isl_map_print_latex(map, p, 0);
1037         isl_assert(map->ctx, 0, goto error);
1038 error:
1039         isl_printer_free(p);
1040         return NULL;
1041 }
1042
1043 struct isl_union_print_data {
1044         isl_printer *p;
1045         int set;
1046         int first;
1047 };
1048
1049 static int print_map_body(__isl_take isl_map *map, void *user)
1050 {
1051         struct isl_union_print_data *data;
1052         data = (struct isl_union_print_data *)user;
1053
1054         if (!data->first)
1055                 data->p = isl_printer_print_str(data->p, "; ");
1056         data->first = 0;
1057
1058         data->p = isl_map_print_isl_body(map, data->p, data->set);
1059         isl_map_free(map);
1060
1061         return 0;
1062 }
1063
1064 static __isl_give isl_printer *isl_union_map_print_isl(
1065         __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1066 {
1067         struct isl_union_print_data data = { p, set, 1 };
1068         isl_dim *dim;
1069         dim = isl_union_map_get_dim(umap);
1070         if (isl_dim_size(dim, isl_dim_param) > 0) {
1071                 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1072                 p = isl_printer_print_str(p, s_to[0]);
1073         }
1074         isl_dim_free(dim);
1075         p = isl_printer_print_str(p, s_open_set[0]);
1076         isl_union_map_foreach_map(umap, &print_map_body, &data);
1077         p = data.p;
1078         p = isl_printer_print_str(p, s_close_set[0]);
1079         return p;
1080 }
1081
1082 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1083 {
1084         struct isl_union_print_data *data;
1085         data = (struct isl_union_print_data *)user;
1086
1087         if (!data->first)
1088                 data->p = isl_printer_print_str(data->p, " \\cup ");
1089         data->first = 0;
1090
1091         data->p = isl_map_print_latex(map, data->p, data->set);
1092         isl_map_free(map);
1093
1094         return 0;
1095 }
1096
1097 static __isl_give isl_printer *isl_union_map_print_latex(
1098         __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1099 {
1100         struct isl_union_print_data data = { p, set, 1 };
1101         isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1102         p = data.p;
1103         return p;
1104 }
1105
1106 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1107         __isl_keep isl_union_map *umap)
1108 {
1109         if (!p || !umap)
1110                 goto error;
1111
1112         if (p->output_format == ISL_FORMAT_ISL)
1113                 return isl_union_map_print_isl(umap, p, 0);
1114         if (p->output_format == ISL_FORMAT_LATEX)
1115                 return isl_union_map_print_latex(umap, p, 0);
1116
1117         isl_die(p->ctx, isl_error_invalid,
1118                 "invalid output format for isl_union_map", goto error);
1119 error:
1120         isl_printer_free(p);
1121         return NULL;
1122 }
1123
1124 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1125         __isl_keep isl_union_set *uset)
1126 {
1127         if (!p || !uset)
1128                 goto error;
1129
1130         if (p->output_format == ISL_FORMAT_ISL)
1131                 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1132         if (p->output_format == ISL_FORMAT_LATEX)
1133                 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1134
1135         isl_die(p->ctx, isl_error_invalid,
1136                 "invalid output format for isl_union_set", goto error);
1137 error:
1138         isl_printer_free(p);
1139         return NULL;
1140 }
1141
1142 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1143         unsigned output_format)
1144 {
1145         isl_printer *printer;
1146
1147         if (!map)
1148                 return;
1149
1150         printer = isl_printer_to_file(map->ctx, out);
1151         printer = isl_printer_set_indent(printer, indent);
1152         printer = isl_printer_set_output_format(printer, output_format);
1153         printer = isl_printer_print_map(printer, map);
1154
1155         isl_printer_free(printer);
1156 }
1157
1158 void isl_basic_map_dump(__isl_keep isl_basic_map *bmap)
1159 {
1160         isl_printer *printer;
1161
1162         if (!bmap)
1163                 return;
1164
1165         printer = isl_printer_to_file(isl_basic_map_get_ctx(bmap), stderr);
1166         printer = isl_printer_print_basic_map(printer, bmap);
1167         printer = isl_printer_end_line(printer);
1168
1169         isl_printer_free(printer);
1170 }
1171
1172 void isl_basic_set_dump(__isl_keep isl_basic_set *bset)
1173 {
1174         isl_printer *printer;
1175
1176         if (!bset)
1177                 return;
1178
1179         printer = isl_printer_to_file(isl_basic_set_get_ctx(bset), stderr);
1180         printer = isl_printer_print_basic_set(printer, bset);
1181         printer = isl_printer_end_line(printer);
1182
1183         isl_printer_free(printer);
1184 }
1185
1186 void isl_map_dump(__isl_keep isl_map *map)
1187 {
1188         isl_printer *printer;
1189
1190         if (!map)
1191                 return;
1192
1193         printer = isl_printer_to_file(isl_map_get_ctx(map), stderr);
1194         printer = isl_printer_print_map(printer, map);
1195         printer = isl_printer_end_line(printer);
1196
1197         isl_printer_free(printer);
1198 }
1199
1200 void isl_set_dump(__isl_keep isl_set *set)
1201 {
1202         isl_printer *printer;
1203
1204         if (!set)
1205                 return;
1206
1207         printer = isl_printer_to_file(isl_set_get_ctx(set), stderr);
1208         printer = isl_printer_print_set(printer, set);
1209         printer = isl_printer_end_line(printer);
1210
1211         isl_printer_free(printer);
1212 }
1213
1214 void isl_union_map_dump(__isl_keep isl_union_map *umap)
1215 {
1216         isl_printer *printer;
1217
1218         if (!umap)
1219                 return;
1220
1221         printer = isl_printer_to_file(isl_union_map_get_ctx(umap), stderr);
1222         printer = isl_printer_print_union_map(printer, umap);
1223         printer = isl_printer_end_line(printer);
1224
1225         isl_printer_free(printer);
1226 }
1227
1228 void isl_union_set_dump(__isl_keep isl_union_set *uset)
1229 {
1230         isl_printer *printer;
1231
1232         if (!uset)
1233                 return;
1234
1235         printer = isl_printer_to_file(isl_union_set_get_ctx(uset), stderr);
1236         printer = isl_printer_print_union_set(printer, uset);
1237         printer = isl_printer_end_line(printer);
1238
1239         isl_printer_free(printer);
1240 }
1241
1242 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial *upwqp)
1243 {
1244         isl_printer *printer;
1245
1246         if (!upwqp)
1247                 return;
1248
1249         printer = isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp),
1250                                         stderr);
1251         printer = isl_printer_print_union_pw_qpolynomial(printer, upwqp);
1252         printer = isl_printer_end_line(printer);
1253
1254         isl_printer_free(printer);
1255 }
1256
1257 void isl_qpolynomial_dump(__isl_keep isl_qpolynomial *qp)
1258 {
1259         isl_printer *printer;
1260
1261         if (!qp)
1262                 return;
1263
1264         printer = isl_printer_to_file(isl_qpolynomial_get_ctx(qp), stderr);
1265         printer = isl_printer_print_qpolynomial(printer, qp);
1266         printer = isl_printer_end_line(printer);
1267
1268         isl_printer_free(printer);
1269 }
1270
1271 void isl_qpolynomial_fold_dump(__isl_keep isl_qpolynomial_fold *fold)
1272 {
1273         isl_printer *printer;
1274
1275         if (!fold)
1276                 return;
1277
1278         printer = isl_printer_to_file(isl_qpolynomial_fold_get_ctx(fold),
1279                                         stderr);
1280         printer = isl_printer_print_qpolynomial_fold(printer, fold);
1281         printer = isl_printer_end_line(printer);
1282
1283         isl_printer_free(printer);
1284 }
1285
1286 void isl_pw_qpolynomial_dump(__isl_keep isl_pw_qpolynomial *pwqp)
1287 {
1288         isl_printer *printer;
1289
1290         if (!pwqp)
1291                 return;
1292
1293         printer = isl_printer_to_file(isl_pw_qpolynomial_get_ctx(pwqp), stderr);
1294         printer = isl_printer_print_pw_qpolynomial(printer, pwqp);
1295         printer = isl_printer_end_line(printer);
1296
1297         isl_printer_free(printer);
1298 }
1299
1300 void isl_pw_qpolynomial_fold_dump(__isl_keep isl_pw_qpolynomial_fold *pwf)
1301 {
1302         isl_printer *printer;
1303
1304         if (!pwf)
1305                 return;
1306
1307         printer = isl_printer_to_file(
1308                         isl_pw_qpolynomial_fold_get_ctx(pwf), stderr);
1309         printer = isl_printer_print_pw_qpolynomial_fold(printer, pwf);
1310         printer = isl_printer_end_line(printer);
1311
1312         isl_printer_free(printer);
1313 }
1314
1315 void isl_union_pw_qpolynomial_fold_dump(
1316         __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1317 {
1318         isl_printer *printer;
1319
1320         if (!upwf)
1321                 return;
1322
1323         printer = isl_printer_to_file(
1324                         isl_union_pw_qpolynomial_fold_get_ctx(upwf), stderr);
1325         printer = isl_printer_print_union_pw_qpolynomial_fold(printer, upwf);
1326         printer = isl_printer_end_line(printer);
1327
1328         isl_printer_free(printer);
1329 }
1330
1331 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1332 {
1333         int i;
1334         int n;
1335
1336         for (i = 0, n = 0; i < rec->n; ++i)
1337                 if (!isl_upoly_is_zero(rec->p[i]))
1338                         ++n;
1339
1340         return n;
1341 }
1342
1343 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
1344         __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1345 {
1346         int c = p->output_format == ISL_FORMAT_C;
1347         p = isl_printer_print_str(p, c ? "floord(" : "[(");
1348         p = print_affine_of_len(dim, div, p,
1349                                 div->row[pos] + 1, div->n_col - 1, 1);
1350         p = isl_printer_print_str(p, c ? ", " : ")/");
1351         p = isl_printer_print_isl_int(p, div->row[pos][0]);
1352         p = isl_printer_print_str(p, c ? ")" : "]");
1353         return p;
1354 }
1355
1356 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1357         __isl_take isl_printer *p, int first)
1358 {
1359         struct isl_upoly_cst *cst;
1360         int neg;
1361
1362         cst = isl_upoly_as_cst(up);
1363         if (!cst)
1364                 goto error;
1365         neg = !first && isl_int_is_neg(cst->n);
1366         if (!first)
1367                 p = isl_printer_print_str(p, neg ? " - " :  " + ");
1368         if (neg)
1369                 isl_int_neg(cst->n, cst->n);
1370         if (isl_int_is_zero(cst->d)) {
1371                 int sgn = isl_int_sgn(cst->n);
1372                 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1373                                             sgn == 0 ? "NaN" : "infty");
1374         } else
1375                 p = isl_printer_print_isl_int(p, cst->n);
1376         if (neg)
1377                 isl_int_neg(cst->n, cst->n);
1378         if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1379                 p = isl_printer_print_str(p, "/");
1380                 p = isl_printer_print_isl_int(p, cst->d);
1381         }
1382         return p;
1383 error:
1384         isl_printer_free(p);
1385         return NULL;
1386 }
1387
1388 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1389         __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
1390 {
1391         unsigned total;
1392
1393         total = isl_dim_total(dim);
1394         if (var < total)
1395                 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 1, 0);
1396         else
1397                 p = print_div(dim, div, var - total, p);
1398         return p;
1399 }
1400
1401 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1402         __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
1403 {
1404         p = print_base(p, dim, div, var);
1405         if (exp == 1)
1406                 return p;
1407         if (p->output_format == ISL_FORMAT_C) {
1408                 int i;
1409                 for (i = 1; i < exp; ++i) {
1410                         p = isl_printer_print_str(p, "*");
1411                         p = print_base(p, dim, div, var);
1412                 }
1413         } else {
1414                 p = isl_printer_print_str(p, "^");
1415                 p = isl_printer_print_int(p, exp);
1416         }
1417         return p;
1418 }
1419
1420 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1421         __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
1422         __isl_take isl_printer *p, int outer)
1423 {
1424         int i, n, first, print_parens;
1425         struct isl_upoly_rec *rec;
1426
1427         if (!p || !up || !dim || !div)
1428                 goto error;
1429
1430         if (isl_upoly_is_cst(up))
1431                 return upoly_print_cst(up, p, 1);
1432
1433         rec = isl_upoly_as_rec(up);
1434         if (!rec)
1435                 goto error;
1436         n = upoly_rec_n_non_zero(rec);
1437         print_parens = n > 1 || (outer && rec->up.var >= isl_dim_total(dim));
1438         if (print_parens)
1439                 p = isl_printer_print_str(p, "(");
1440         for (i = 0, first = 1; i < rec->n; ++i) {
1441                 if (isl_upoly_is_zero(rec->p[i]))
1442                         continue;
1443                 if (isl_upoly_is_negone(rec->p[i])) {
1444                         if (!i)
1445                                 p = isl_printer_print_str(p, "-1");
1446                         else if (first)
1447                                 p = isl_printer_print_str(p, "-");
1448                         else
1449                                 p = isl_printer_print_str(p, " - ");
1450                 } else if (isl_upoly_is_cst(rec->p[i]) &&
1451                                 !isl_upoly_is_one(rec->p[i]))
1452                         p = upoly_print_cst(rec->p[i], p, first);
1453                 else {
1454                         if (!first)
1455                                 p = isl_printer_print_str(p, " + ");
1456                         if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1457                                 p = upoly_print(rec->p[i], dim, div, p, 0);
1458                 }
1459                 first = 0;
1460                 if (i == 0)
1461                         continue;
1462                 if (!isl_upoly_is_one(rec->p[i]) &&
1463                     !isl_upoly_is_negone(rec->p[i]))
1464                         p = isl_printer_print_str(p, " * ");
1465                 p = print_pow(p, dim, div, rec->up.var, i);
1466         }
1467         if (print_parens)
1468                 p = isl_printer_print_str(p, ")");
1469         return p;
1470 error:
1471         isl_printer_free(p);
1472         return NULL;
1473 }
1474
1475 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
1476         __isl_keep isl_qpolynomial *qp)
1477 {
1478         if (!p || !qp)
1479                 goto error;
1480         p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1481         return p;
1482 error:
1483         isl_printer_free(p);
1484         return NULL;
1485 }
1486
1487 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1488         unsigned output_format)
1489 {
1490         isl_printer *p;
1491
1492         if  (!qp)
1493                 return;
1494
1495         isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1496         p = isl_printer_to_file(qp->dim->ctx, out);
1497         p = isl_printer_print_qpolynomial(p, qp);
1498         isl_printer_free(p);
1499 }
1500
1501 static __isl_give isl_printer *qpolynomial_fold_print(
1502         __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1503 {
1504         int i;
1505
1506         if (fold->type == isl_fold_min)
1507                 p = isl_printer_print_str(p, "min");
1508         else if (fold->type == isl_fold_max)
1509                 p = isl_printer_print_str(p, "max");
1510         p = isl_printer_print_str(p, "(");
1511         for (i = 0; i < fold->n; ++i) {
1512                 if (i)
1513                         p = isl_printer_print_str(p, ", ");
1514                 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
1515         }
1516         p = isl_printer_print_str(p, ")");
1517         return p;
1518 }
1519
1520 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1521         FILE *out, unsigned output_format)
1522 {
1523         isl_printer *p;
1524
1525         if (!fold)
1526                 return;
1527
1528         isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1529
1530         p = isl_printer_to_file(fold->dim->ctx, out);
1531         p = isl_printer_print_qpolynomial_fold(p, fold);
1532
1533         isl_printer_free(p);
1534 }
1535
1536 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1537         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1538 {
1539         int i = 0;
1540
1541         for (i = 0; i < pwqp->n; ++i) {
1542                 if (i)
1543                         p = isl_printer_print_str(p, "; ");
1544                 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1545                     isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1546                         p = print_dim(pwqp->p[i].set->dim, p, 1, 0, 0, NULL);
1547                         p = isl_printer_print_str(p, " -> ");
1548                 }
1549                 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
1550                 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1551         }
1552
1553         return p;
1554 }
1555
1556 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1557         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1558 {
1559         if (!p || !pwqp)
1560                 goto error;
1561
1562         if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
1563                 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1564                 p = isl_printer_print_str(p, " -> ");
1565         }
1566         p = isl_printer_print_str(p, "{ ");
1567         if (pwqp->n == 0) {
1568                 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1569                     isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1570                         p = print_dim(pwqp->dim, p, 1, 0, 0, NULL);
1571                         p = isl_printer_print_str(p, " -> ");
1572                 }
1573                 p = isl_printer_print_str(p, "0");
1574         }
1575         p = isl_pwqp_print_isl_body(p, pwqp);
1576         p = isl_printer_print_str(p, " }");
1577         return p;
1578 error:
1579         isl_printer_free(p);
1580         return NULL;
1581 }
1582
1583 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1584         unsigned output_format)
1585 {
1586         isl_printer *p;
1587
1588         if (!pwqp)
1589                 return;
1590
1591         p = isl_printer_to_file(pwqp->dim->ctx, out);
1592         p = isl_printer_set_output_format(p, output_format);
1593         p = isl_printer_print_pw_qpolynomial(p, pwqp);
1594
1595         isl_printer_free(p);
1596 }
1597
1598 static __isl_give isl_printer *isl_pwf_print_isl_body(
1599         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1600 {
1601         int i = 0;
1602
1603         for (i = 0; i < pwf->n; ++i) {
1604                 if (i)
1605                         p = isl_printer_print_str(p, "; ");
1606                 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1607                     isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1608                         p = print_dim(pwf->p[i].set->dim, p, 1, 0, 0, NULL);
1609                         p = isl_printer_print_str(p, " -> ");
1610                 }
1611                 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1612                 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1613         }
1614
1615         return p;
1616 }
1617
1618 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1619         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1620 {
1621         if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1622                 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1623                 p = isl_printer_print_str(p, " -> ");
1624         }
1625         p = isl_printer_print_str(p, "{ ");
1626         if (pwf->n == 0) {
1627                 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1628                     isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1629                         p = print_dim(pwf->dim, p, 1, 0, 0, NULL);
1630                         p = isl_printer_print_str(p, " -> ");
1631                 }
1632                 p = isl_printer_print_str(p, "0");
1633         }
1634         p = isl_pwf_print_isl_body(p, pwf);
1635         p = isl_printer_print_str(p, " }");
1636         return p;
1637 }
1638
1639 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1640         __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1641
1642 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1643         __isl_keep isl_dim *dim,
1644         __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1645 {
1646         if (type == isl_dim_div) {
1647                 p = isl_printer_print_str(p, "floord(");
1648                 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1649                 p = isl_printer_print_str(p, ", ");
1650                 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1651                 p = isl_printer_print_str(p, ")");
1652         } else {
1653                 const char *name;
1654
1655                 name = isl_dim_get_name(dim, type, pos);
1656                 if (!name)
1657                         name = "UNNAMED";
1658                 p = isl_printer_print_str(p, name);
1659         }
1660         return p;
1661 }
1662
1663 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1664         __isl_keep isl_dim *dim,
1665         __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1666 {
1667         enum isl_dim_type type;
1668         unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1669
1670         if (pos == 0)
1671                 return isl_printer_print_isl_int(p, c);
1672
1673         if (isl_int_is_one(c))
1674                 ;
1675         else if (isl_int_is_negone(c))
1676                 p = isl_printer_print_str(p, "-");
1677         else {
1678                 p = isl_printer_print_isl_int(p, c);
1679                 p = isl_printer_print_str(p, "*");
1680         }
1681         type = pos2type(dim, &pos);
1682         p = print_name_c(p, dim, bset, type, pos);
1683         return p;
1684 }
1685
1686 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1687         __isl_keep isl_dim *dim,
1688         __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1689 {
1690         int i;
1691         int first;
1692
1693         for (i = 0, first = 1; i < len; ++i) {
1694                 int flip = 0;
1695                 if (isl_int_is_zero(c[i]))
1696                         continue;
1697                 if (!first) {
1698                         if (isl_int_is_neg(c[i])) {
1699                                 flip = 1;
1700                                 isl_int_neg(c[i], c[i]);
1701                                 p = isl_printer_print_str(p, " - ");
1702                         } else 
1703                                 p = isl_printer_print_str(p, " + ");
1704                 }
1705                 first = 0;
1706                 p = print_term_c(p, dim, bset, c[i], i);
1707                 if (flip)
1708                         isl_int_neg(c[i], c[i]);
1709         }
1710         if (first)
1711                 p = isl_printer_print_str(p, "0");
1712         return p;
1713 }
1714
1715 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1716         __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1717 {
1718         unsigned len = 1 + isl_basic_set_total_dim(bset);
1719         return print_partial_affine_c(p, dim, bset, c, len);
1720 }
1721
1722 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1723         __isl_keep isl_dim *dim,
1724         __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1725 {
1726         if (!first)
1727                 p = isl_printer_print_str(p, " && ");
1728
1729         p = print_affine_c(p, dim, bset, c);
1730         p = isl_printer_print_str(p, " ");
1731         p = isl_printer_print_str(p, op);
1732         p = isl_printer_print_str(p, " 0");
1733         return p;
1734 }
1735
1736 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1737         __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset)
1738 {
1739         int i, j;
1740         unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1741         unsigned total = isl_basic_set_total_dim(bset) - n_div;
1742
1743         for (i = 0; i < bset->n_eq; ++i) {
1744                 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1745                 if (j < 0)
1746                         p = print_constraint_c(p, dim, bset,
1747                                                 bset->eq[i], "==", !i);
1748                 else {
1749                         if (i)
1750                                 p = isl_printer_print_str(p, " && ");
1751                         p = isl_printer_print_str(p, "(");
1752                         p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1753                                                    1 + total + j);
1754                         p = isl_printer_print_str(p, ") % ");
1755                         p = isl_printer_print_isl_int(p,
1756                                                 bset->eq[i][1 + total + j]);
1757                         p = isl_printer_print_str(p, " == 0");
1758                 }
1759         }
1760         for (i = 0; i < bset->n_ineq; ++i)
1761                 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1762                                         !bset->n_eq && !i);
1763         return p;
1764 }
1765
1766 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1767         __isl_keep isl_dim *dim, __isl_keep isl_set *set)
1768 {
1769         int i;
1770
1771         if (set->n == 0)
1772                 p = isl_printer_print_str(p, "0");
1773
1774         for (i = 0; i < set->n; ++i) {
1775                 if (i)
1776                         p = isl_printer_print_str(p, " || ");
1777                 if (set->n > 1)
1778                         p = isl_printer_print_str(p, "(");
1779                 p = print_basic_set_c(p, dim, set->p[i]);
1780                 if (set->n > 1)
1781                         p = isl_printer_print_str(p, ")");
1782         }
1783         return p;
1784 }
1785
1786 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1787         __isl_keep isl_dim *dim, __isl_keep isl_qpolynomial *qp)
1788 {
1789         isl_int den;
1790
1791         isl_int_init(den);
1792         isl_qpolynomial_get_den(qp, &den);
1793         if (!isl_int_is_one(den)) {
1794                 isl_qpolynomial *f;
1795                 p = isl_printer_print_str(p, "(");
1796                 qp = isl_qpolynomial_copy(qp);
1797                 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1798                                                 den, qp->dim->ctx->one);
1799                 qp = isl_qpolynomial_mul(qp, f);
1800         }
1801         if (qp)
1802                 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1803         if (!isl_int_is_one(den)) {
1804                 p = isl_printer_print_str(p, ")/");
1805                 p = isl_printer_print_isl_int(p, den);
1806                 isl_qpolynomial_free(qp);
1807         }
1808         isl_int_clear(den);
1809         return p;
1810 }
1811
1812 static __isl_give isl_printer *print_pw_qpolynomial_c(
1813         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1814 {
1815         int i;
1816
1817         if (pwqp->n == 1 && isl_set_fast_is_universe(pwqp->p[0].set))
1818                 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1819
1820         for (i = 0; i < pwqp->n; ++i) {
1821                 p = isl_printer_print_str(p, "(");
1822                 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1823                 p = isl_printer_print_str(p, ") ? (");
1824                 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1825                 p = isl_printer_print_str(p, ") : ");
1826         }
1827
1828         p = isl_printer_print_str(p, "0");
1829         return p;
1830 }
1831
1832 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1833         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1834 {
1835         if (!p || !pwqp)
1836                 goto error;
1837
1838         if (p->output_format == ISL_FORMAT_ISL)
1839                 return print_pw_qpolynomial_isl(p, pwqp);
1840         else if (p->output_format == ISL_FORMAT_C)
1841                 return print_pw_qpolynomial_c(p, pwqp);
1842         isl_assert(p->ctx, 0, goto error);
1843 error:
1844         isl_printer_free(p);
1845         return NULL;
1846 }
1847
1848 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1849 {
1850         struct isl_union_print_data *data;
1851         data = (struct isl_union_print_data *)user;
1852
1853         if (!data->first)
1854                 data->p = isl_printer_print_str(data->p, "; ");
1855         data->first = 0;
1856
1857         data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1858         isl_pw_qpolynomial_free(pwqp);
1859
1860         return 0;
1861 }
1862
1863 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1864         __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1865 {
1866         struct isl_union_print_data data = { p, 1, 1 };
1867         isl_dim *dim;
1868         dim = isl_union_pw_qpolynomial_get_dim(upwqp);
1869         if (isl_dim_size(dim, isl_dim_param) > 0) {
1870                 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1871                 p = isl_printer_print_str(p, " -> ");
1872         }
1873         isl_dim_free(dim);
1874         p = isl_printer_print_str(p, "{ ");
1875         isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1876                                                         &data);
1877         p = data.p;
1878         p = isl_printer_print_str(p, " }");
1879         return p;
1880 }
1881
1882 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1883         __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1884 {
1885         if (!p || !upwqp)
1886                 goto error;
1887
1888         if (p->output_format == ISL_FORMAT_ISL)
1889                 return print_union_pw_qpolynomial_isl(p, upwqp);
1890         isl_die(p->ctx, isl_error_invalid,
1891                 "invalid output format for isl_union_pw_qpolynomial",
1892                 goto error);
1893 error:
1894         isl_printer_free(p);
1895         return NULL;
1896 }
1897
1898 static __isl_give isl_printer *print_qpolynomial_fold_c(
1899         __isl_take isl_printer *p, __isl_keep isl_dim *dim,
1900         __isl_keep isl_qpolynomial_fold *fold)
1901 {
1902         int i;
1903
1904         for (i = 0; i < fold->n - 1; ++i)
1905                 if (fold->type == isl_fold_min)
1906                         p = isl_printer_print_str(p, "min(");
1907                 else if (fold->type == isl_fold_max)
1908                         p = isl_printer_print_str(p, "max(");
1909
1910         for (i = 0; i < fold->n; ++i) {
1911                 if (i)
1912                         p = isl_printer_print_str(p, ", ");
1913                 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1914                 if (i)
1915                         p = isl_printer_print_str(p, ")");
1916         }
1917         return p;
1918 }
1919
1920 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1921         __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1922 {
1923         if  (!p || !fold)
1924                 goto error;
1925         if (p->output_format == ISL_FORMAT_ISL)
1926                 return qpolynomial_fold_print(fold, p);
1927         else if (p->output_format == ISL_FORMAT_C)
1928                 return print_qpolynomial_fold_c(p, fold->dim, fold);
1929         isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1930                 goto error);
1931 error:
1932         isl_printer_free(p);
1933         return NULL;
1934 }
1935
1936 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1937         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1938 {
1939         int i;
1940
1941         if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1942                 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1943
1944         for (i = 0; i < pwf->n; ++i) {
1945                 p = isl_printer_print_str(p, "(");
1946                 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1947                 p = isl_printer_print_str(p, ") ? (");
1948                 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1949                 p = isl_printer_print_str(p, ") : ");
1950         }
1951
1952         p = isl_printer_print_str(p, "0");
1953         return p;
1954 }
1955
1956 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1957         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1958 {
1959         if (!p || !pwf)
1960                 goto error;
1961
1962         if (p->output_format == ISL_FORMAT_ISL)
1963                 return print_pw_qpolynomial_fold_isl(p, pwf);
1964         else if (p->output_format == ISL_FORMAT_C)
1965                 return print_pw_qpolynomial_fold_c(p, pwf);
1966         isl_assert(p->ctx, 0, goto error);
1967 error:
1968         isl_printer_free(p);
1969         return NULL;
1970 }
1971
1972 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1973         FILE *out, unsigned output_format)
1974 {
1975         isl_printer *p;
1976
1977         if (!pwf)
1978                 return;
1979
1980         p = isl_printer_to_file(pwf->dim->ctx, out);
1981         p = isl_printer_set_output_format(p, output_format);
1982         p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1983
1984         isl_printer_free(p);
1985 }
1986
1987 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1988 {
1989         struct isl_union_print_data *data;
1990         data = (struct isl_union_print_data *)user;
1991
1992         if (!data->first)
1993                 data->p = isl_printer_print_str(data->p, "; ");
1994         data->first = 0;
1995
1996         data->p = isl_pwf_print_isl_body(data->p, pwf);
1997         isl_pw_qpolynomial_fold_free(pwf);
1998
1999         return 0;
2000 }
2001
2002 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2003         __isl_take isl_printer *p,
2004         __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2005 {
2006         struct isl_union_print_data data = { p, 1, 1 };
2007         isl_dim *dim;
2008         dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
2009         if (isl_dim_size(dim, isl_dim_param) > 0) {
2010                 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
2011                 p = isl_printer_print_str(p, " -> ");
2012         }
2013         isl_dim_free(dim);
2014         p = isl_printer_print_str(p, "{ ");
2015         isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2016                                                         &print_pwf_body, &data);
2017         p = data.p;
2018         p = isl_printer_print_str(p, " }");
2019         return p;
2020 }
2021
2022 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2023         __isl_take isl_printer *p,
2024         __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2025 {
2026         if (!p || !upwf)
2027                 goto error;
2028
2029         if (p->output_format == ISL_FORMAT_ISL)
2030                 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2031         isl_die(p->ctx, isl_error_invalid,
2032                 "invalid output format for isl_union_pw_qpolynomial_fold",
2033                 goto error);
2034 error:
2035         isl_printer_free(p);
2036         return NULL;
2037 }