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