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