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