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