isl_pw_qpolynomial_{fold_,}print: set output format
[platform/upstream/isl.git] / isl_output.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include <string.h>
14 #include <isl_set.h>
15 #include <isl_seq.h>
16 #include <isl_polynomial_private.h>
17 #include <isl_printer_private.h>
18
19 static __isl_give isl_printer *print_constraint_polylib(
20         struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
21 {
22         int i;
23         unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
24         unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
25         unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
26         isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
27
28         p = isl_printer_start_line(p);
29         p = isl_printer_print_int(p, ineq);
30         for (i = 0; i < n_out; ++i) {
31                 p = isl_printer_print_str(p, " ");
32                 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
33         }
34         for (i = 0; i < n_in; ++i) {
35                 p = isl_printer_print_str(p, " ");
36                 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
37         }
38         for (i = 0; i < bmap->n_div; ++i) {
39                 p = isl_printer_print_str(p, " ");
40                 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
41         }
42         for (i = 0; i < nparam; ++i) {
43                 p = isl_printer_print_str(p, " ");
44                 p = isl_printer_print_isl_int(p, c[1+i]);
45         }
46         p = isl_printer_print_str(p, " ");
47         p = isl_printer_print_isl_int(p, c[0]);
48         p = isl_printer_end_line(p);
49         return p;
50 }
51
52 static __isl_give isl_printer *print_constraints_polylib(
53         struct isl_basic_map *bmap, __isl_take isl_printer *p)
54 {
55         int i;
56
57         p = isl_printer_set_isl_int_width(p, 5);
58
59         for (i = 0; i < bmap->n_eq; ++i)
60                 p = print_constraint_polylib(bmap, 0, i, p);
61         for (i = 0; i < bmap->n_ineq; ++i)
62                 p = print_constraint_polylib(bmap, 1, i, p);
63
64         return p;
65 }
66
67 static __isl_give isl_printer *bset_print_constraints_polylib(
68         struct isl_basic_set *bset, __isl_take isl_printer *p)
69 {
70         return print_constraints_polylib((struct isl_basic_map *)bset, p);
71 }
72
73 static __isl_give isl_printer *isl_basic_map_print_polylib(
74         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
75 {
76         unsigned total = isl_basic_map_total_dim(bmap);
77         p = isl_printer_start_line(p);
78         p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
79         p = isl_printer_print_str(p, " ");
80         p = isl_printer_print_int(p, 1 + total + 1);
81         p = isl_printer_end_line(p);
82         return print_constraints_polylib(bmap, p);
83 }
84
85 static __isl_give isl_printer *isl_basic_set_print_polylib(
86         __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
87 {
88         return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p);
89 }
90
91 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
92         __isl_take isl_printer *p)
93 {
94         int i;
95
96         p = isl_printer_start_line(p);
97         p = isl_printer_print_int(p, map->n);
98         p = isl_printer_end_line(p);
99         for (i = 0; i < map->n; ++i) {
100                 p = isl_printer_start_line(p);
101                 p = isl_printer_end_line(p);
102                 p = isl_basic_map_print_polylib(map->p[i], p);
103         }
104         return p;
105 }
106
107 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
108         __isl_take isl_printer *p)
109 {
110         return isl_map_print_polylib((struct isl_map *)set, p);
111 }
112
113 static int count_same_name(__isl_keep isl_dim *dim,
114         enum isl_dim_type type, unsigned pos, const char *name)
115 {
116         enum isl_dim_type t;
117         unsigned p, s;
118         int count = 0;
119
120         for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
121                 s = t == type ? pos : isl_dim_size(dim, t);
122                 for (p = 0; p < s; ++p) {
123                         const char *n = isl_dim_get_name(dim, t, p);
124                         if (n && !strcmp(n, name))
125                                 count++;
126                 }
127         }
128         return count;
129 }
130
131 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
132         __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos, int set)
133 {
134         const char *name;
135         char buffer[20];
136         int primes;
137
138         name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
139
140         if (!name) {
141                 const char *prefix;
142                 if (type == isl_dim_param)
143                         prefix = "p";
144                 else if (type == isl_dim_div)
145                         prefix = "e";
146                 else if (set || type == isl_dim_in)
147                         prefix = "i";
148                 else
149                         prefix = "o";
150                 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
151                 name = buffer;
152         }
153         primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
154                                  pos, name);
155         p = isl_printer_print_str(p, name);
156         while (primes-- > 0)
157                 p = isl_printer_print_str(p, "'");
158         return p;
159 }
160
161 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
162         __isl_take isl_printer *p, enum isl_dim_type type, int set)
163 {
164         int i;
165
166         for (i = 0; i < isl_dim_size(dim, type); ++i) {
167                 if (i)
168                         p = isl_printer_print_str(p, ", ");
169                 p = print_name(dim, p, type, i, set);
170         }
171         return p;
172 }
173
174 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
175         __isl_take isl_printer *p, enum isl_dim_type type, int set)
176 {
177         p = isl_printer_print_str(p, "[");
178         p = print_var_list(dim, p, type, set);
179         p = isl_printer_print_str(p, "]");
180         return p;
181 }
182
183 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
184         __isl_take isl_printer *p)
185 {
186         if (isl_dim_size(dim, isl_dim_param) == 0)
187                 return p;
188
189         p = isl_printer_start_line(p);
190         p = isl_printer_print_str(p, "symbolic ");
191         p = print_var_list(dim, p, isl_dim_param, 0);
192         p = isl_printer_print_str(p, ";");
193         p = isl_printer_end_line(p);
194         return p;
195 }
196
197 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
198 {
199         enum isl_dim_type type;
200         unsigned n_in = isl_dim_size(dim, isl_dim_in);
201         unsigned n_out = isl_dim_size(dim, isl_dim_out);
202         unsigned nparam = isl_dim_size(dim, isl_dim_param);
203
204         if (*pos < 1 + nparam) {
205                 type = isl_dim_param;
206                 *pos -= 1;
207         } else if (*pos < 1 + nparam + n_in) {
208                 type = isl_dim_in;
209                 *pos -= 1 + nparam;
210         } else if (*pos < 1 + nparam + n_in + n_out) {
211                 type = isl_dim_out;
212                 *pos -= 1 + nparam + n_in;
213         } else {
214                 type = isl_dim_div;
215                 *pos -= 1 + nparam + n_in + n_out;
216         }
217
218         return type;
219 }
220
221 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
222                         isl_int c, unsigned pos, __isl_take isl_printer *p, int set)
223 {
224         enum isl_dim_type type;
225
226         if (pos == 0)
227                 return isl_printer_print_isl_int(p, c);
228
229         if (isl_int_is_one(c))
230                 ;
231         else if (isl_int_is_negone(c))
232                 p = isl_printer_print_str(p, "-");
233         else
234                 p = isl_printer_print_isl_int(p, c);
235         type = pos2type(dim, &pos);
236         p = print_name(dim, p, type, pos, set);
237         return p;
238 }
239
240 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
241         __isl_take isl_printer *p, isl_int *c, int len, int set)
242 {
243         int i;
244         int first;
245
246         for (i = 0, first = 1; i < len; ++i) {
247                 int flip = 0;
248                 if (isl_int_is_zero(c[i]))
249                         continue;
250                 if (!first) {
251                         if (isl_int_is_neg(c[i])) {
252                                 flip = 1;
253                                 isl_int_neg(c[i], c[i]);
254                                 p = isl_printer_print_str(p, " - ");
255                         } else 
256                                 p = isl_printer_print_str(p, " + ");
257                 }
258                 first = 0;
259                 p = print_term(dim, c[i], i, p, set);
260                 if (flip)
261                         isl_int_neg(c[i], c[i]);
262         }
263         if (first)
264                 p = isl_printer_print_str(p, "0");
265         return p;
266 }
267
268 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
269         __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
270 {
271         unsigned len = 1 + isl_basic_map_total_dim(bmap);
272         return print_affine_of_len(dim, p, c, len, set);
273 }
274
275 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
276         __isl_keep isl_dim *dim, __isl_take isl_printer *p,
277         isl_int *c, int last, const char *op, int first_constraint, int set)
278 {
279         if (!first_constraint)
280                 p = isl_printer_print_str(p, " and ");
281
282         isl_int_abs(c[last], c[last]);
283
284         p = print_term(dim, c[last], last, p, set);
285
286         p = isl_printer_print_str(p, " ");
287         p = isl_printer_print_str(p, op);
288         p = isl_printer_print_str(p, " ");
289
290         isl_int_set_si(c[last], 0);
291         p = print_affine(bmap, dim, p, c, set);
292
293         return p;
294 }
295
296 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
297         __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set)
298 {
299         int i;
300         struct isl_vec *c;
301         unsigned total = isl_basic_map_total_dim(bmap);
302
303         c = isl_vec_alloc(bmap->ctx, 1 + total);
304         if (!c)
305                 goto error;
306
307         for (i = bmap->n_eq - 1; i >= 0; --i) {
308                 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
309                 isl_assert(bmap->ctx, l >= 0, goto error);
310                 if (isl_int_is_neg(bmap->eq[i][l]))
311                         isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
312                 else
313                         isl_seq_neg(c->el, bmap->eq[i], 1 + total);
314                 p = print_constraint(bmap, dim, p, c->el, l,
315                                     "=", i == bmap->n_eq - 1, set);
316         }
317         for (i = 0; i < bmap->n_ineq; ++i) {
318                 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
319                 int s;
320                 isl_assert(bmap->ctx, l >= 0, goto error);
321                 s = isl_int_sgn(bmap->ineq[i][l]);
322                 if (s < 0)
323                         isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
324                 else
325                         isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
326                 p = print_constraint(bmap, dim, p, c->el, l,
327                                     s < 0 ? "<=" : ">=", !bmap->n_eq && !i, set);
328         }
329
330         isl_vec_free(c);
331
332         return p;
333 error:
334         isl_printer_free(p);
335         return NULL;
336 }
337
338 static __isl_give isl_printer *print_omega_constraints(
339         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
340 {
341         if (bmap->n_eq + bmap->n_ineq == 0)
342                 return p;
343
344         p = isl_printer_print_str(p, ": ");
345         if (bmap->n_div > 0) {
346                 int i;
347                 p = isl_printer_print_str(p, "exists (");
348                 for (i = 0; i < bmap->n_div; ++i) {
349                         if (i)
350                                 p = isl_printer_print_str(p, ", ");
351                         p = print_name(bmap->dim, p, isl_dim_div, i, 0);
352                 }
353                 p = isl_printer_print_str(p, ": ");
354         }
355         p = print_constraints(bmap, bmap->dim, p, set);
356         if (bmap->n_div > 0)
357                 p = isl_printer_print_str(p, ")");
358         return p;
359 }
360
361 static __isl_give isl_printer *basic_map_print_omega(
362         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
363 {
364         p = isl_printer_print_str(p, "{ [");
365         p = print_var_list(bmap->dim, p, isl_dim_in, 0);
366         p = isl_printer_print_str(p, "] -> [");
367         p = print_var_list(bmap->dim, p, isl_dim_out, 0);
368         p = isl_printer_print_str(p, "] ");
369         p = print_omega_constraints(bmap, p, 0);
370         p = isl_printer_print_str(p, " }");
371         return p;
372 }
373
374 static __isl_give isl_printer *isl_basic_map_print_omega(
375         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
376 {
377         p = print_omega_parameters(bmap->dim, p);
378
379         p = isl_printer_start_line(p);
380         p = basic_map_print_omega(bmap, p);
381         p = isl_printer_end_line(p);
382         return p;
383 }
384
385 static __isl_give isl_printer *basic_set_print_omega(
386         __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
387 {
388         p = isl_printer_print_str(p, "{ [");
389         p = print_var_list(bset->dim, p, isl_dim_set, 1);
390         p = isl_printer_print_str(p, "] ");
391         p = print_omega_constraints((isl_basic_map *)bset, p, 1);
392         p = isl_printer_print_str(p, " }");
393         return p;
394 }
395
396 static __isl_give isl_printer *isl_basic_set_print_omega(
397         __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
398 {
399         p = print_omega_parameters(bset->dim, p);
400
401         p = isl_printer_start_line(p);
402         p = basic_set_print_omega(bset, p);
403         p = isl_printer_end_line(p);
404         return p;
405 }
406
407 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
408         __isl_take isl_printer *p)
409 {
410         int i;
411
412         p = print_omega_parameters(map->dim, p);
413
414         p = isl_printer_start_line(p);
415         for (i = 0; i < map->n; ++i) {
416                 if (i)
417                         p = isl_printer_print_str(p, " union ");
418                 p = basic_map_print_omega(map->p[i], p);
419         }
420         p = isl_printer_end_line(p);
421         return p;
422 }
423
424 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
425         __isl_take isl_printer *p)
426 {
427         int i;
428
429         p = print_omega_parameters(set->dim, p);
430
431         p = isl_printer_start_line(p);
432         for (i = 0; i < set->n; ++i) {
433                 if (i)
434                         p = isl_printer_print_str(p, " union ");
435                 p = basic_set_print_omega(set->p[i], p);
436         }
437         p = isl_printer_end_line(p);
438         return p;
439 }
440
441 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
442         __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set)
443 {
444         if (bmap->n_div > 0) {
445                 int i;
446                 p = isl_printer_print_str(p, "exists (");
447                 for (i = 0; i < bmap->n_div; ++i) {
448                         if (i)
449                                 p = isl_printer_print_str(p, ", ");
450                         p = print_name(dim, p, isl_dim_div, i, 0);
451                         if (isl_int_is_zero(bmap->div[i][0]))
452                                 continue;
453                         p = isl_printer_print_str(p, " = [(");
454                         p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
455                         p = isl_printer_print_str(p, ")/");
456                         p = isl_printer_print_isl_int(p, bmap->div[i][0]);
457                         p = isl_printer_print_str(p, "]");
458                 }
459                 p = isl_printer_print_str(p, ": ");
460         }
461
462         p = print_constraints(bmap, dim, p, set);
463
464         if (bmap->n_div > 0)
465                 p = isl_printer_print_str(p, ")");
466         return p;
467 }
468
469 static __isl_give isl_printer *isl_basic_map_print_isl(
470         __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
471 {
472         int i;
473
474         p = isl_printer_start_line(p);
475         if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
476                 p = print_tuple(bmap->dim, p, isl_dim_param, 0);
477                 p = isl_printer_print_str(p, " -> ");
478         }
479         p = isl_printer_print_str(p, "{ ");
480         p = print_tuple(bmap->dim, p, isl_dim_in, 0);
481         p = isl_printer_print_str(p, " -> ");
482         p = print_tuple(bmap->dim, p, isl_dim_out, 0);
483         p = isl_printer_print_str(p, " : ");
484         p = print_disjunct(bmap, bmap->dim, p, 0);
485         p = isl_printer_end_line(p);
486         return p;
487 }
488
489 static __isl_give isl_printer *isl_basic_set_print_isl(
490         __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
491 {
492         int i;
493
494         p = isl_printer_start_line(p);
495         if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
496                 p = print_tuple(bset->dim, p, isl_dim_param, 0);
497                 p = isl_printer_print_str(p, " -> ");
498         }
499         p = isl_printer_print_str(p, "{ ");
500         p = print_tuple(bset->dim, p, isl_dim_set, 1);
501         p = isl_printer_print_str(p, " : ");
502         p = print_disjunct((isl_basic_map *)bset, bset->dim, p, 1);
503         p = isl_printer_end_line(p);
504         return p;
505 }
506
507 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
508         __isl_take isl_printer *p, int set)
509 {
510         int i;
511
512         if (isl_map_fast_is_universe(map))
513                 return p;
514
515         p = isl_printer_print_str(p, " : ");
516         if (map->n == 0)
517                 p = isl_printer_print_str(p, "1 = 0");
518         for (i = 0; i < map->n; ++i) {
519                 if (i)
520                         p = isl_printer_print_str(p, " or ");
521                 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
522                         p = isl_printer_print_str(p, "(");
523                 p = print_disjunct(map->p[i], map->dim, p, set);
524                 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
525                         p = isl_printer_print_str(p, ")");
526         }
527         return p;
528 }
529
530 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
531         __isl_take isl_printer *p)
532 {
533         if (isl_map_dim(map, isl_dim_param) > 0) {
534                 p = print_tuple(map->dim, p, isl_dim_param, 0);
535                 p = isl_printer_print_str(p, " -> ");
536         }
537         p = isl_printer_print_str(p, "{ ");
538         p = print_tuple(map->dim, p, isl_dim_in, 0);
539         p = isl_printer_print_str(p, " -> ");
540         p = print_tuple(map->dim, p, isl_dim_out, 0);
541         p = print_disjuncts(map, p, 0);
542         p = isl_printer_print_str(p, " }");
543         return p;
544 }
545
546 static __isl_give isl_printer *isl_set_print_isl(__isl_keep isl_set *set,
547         __isl_take isl_printer *p)
548 {
549         int i;
550
551         if (isl_set_dim(set, isl_dim_param) > 0) {
552                 p = print_tuple(set->dim, p, isl_dim_param, 0);
553                 p = isl_printer_print_str(p, " -> ");
554         }
555         p = isl_printer_print_str(p, "{ ");
556         p = print_tuple(set->dim, p, isl_dim_set, 1);
557         p = print_disjuncts((isl_map *)set, p, 1);
558         p = isl_printer_print_str(p, " }");
559         return p;
560 }
561
562 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
563         __isl_keep isl_basic_map *bmap)
564 {
565         if (!p || !bmap)
566                 goto error;
567         if (p->output_format == ISL_FORMAT_ISL)
568                 return isl_basic_map_print_isl(bmap, p);
569         else if (p->output_format == ISL_FORMAT_OMEGA)
570                 return isl_basic_map_print_omega(bmap, p);
571         isl_assert(bmap->ctx, 0, goto error);
572 error:
573         isl_printer_free(p);
574         return NULL;
575 }
576
577 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
578         const char *prefix, const char *suffix, unsigned output_format)
579 {
580         isl_printer *printer;
581
582         if (!bmap)
583                 return;
584
585         printer = isl_printer_to_file(bmap->ctx, out);
586         printer = isl_printer_set_indent(printer, indent);
587         printer = isl_printer_set_prefix(printer, prefix);
588         printer = isl_printer_set_suffix(printer, suffix);
589         printer = isl_printer_set_output_format(printer, output_format);
590         isl_printer_print_basic_map(printer, bmap);
591
592         isl_printer_free(printer);
593 }
594
595 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
596         __isl_keep isl_basic_set *bset)
597 {
598         if (!p || !bset)
599                 goto error;
600
601         if (p->output_format == ISL_FORMAT_ISL)
602                 return isl_basic_set_print_isl(bset, p);
603         else if (p->output_format == ISL_FORMAT_POLYLIB)
604                 return isl_basic_set_print_polylib(bset, p);
605         else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
606                 return bset_print_constraints_polylib(bset, p);
607         else if (p->output_format == ISL_FORMAT_OMEGA)
608                 return isl_basic_set_print_omega(bset, p);
609         isl_assert(p->ctx, 0, goto error);
610 error:
611         isl_printer_free(p);
612         return NULL;
613 }
614
615 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
616         const char *prefix, const char *suffix, unsigned output_format)
617 {
618         isl_printer *printer;
619
620         if (!bset)
621                 return;
622
623         printer = isl_printer_to_file(bset->ctx, out);
624         printer = isl_printer_set_indent(printer, indent);
625         printer = isl_printer_set_prefix(printer, prefix);
626         printer = isl_printer_set_suffix(printer, suffix);
627         printer = isl_printer_set_output_format(printer, output_format);
628         isl_printer_print_basic_set(printer, bset);
629
630         isl_printer_free(printer);
631 }
632
633 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
634         __isl_keep isl_set *set)
635 {
636         if (!p || !set)
637                 goto error;
638         if (p->output_format == ISL_FORMAT_ISL)
639                 return isl_set_print_isl(set, p);
640         else if (p->output_format == ISL_FORMAT_POLYLIB)
641                 return isl_set_print_polylib(set, p);
642         else if (p->output_format == ISL_FORMAT_OMEGA)
643                 return isl_set_print_omega(set, p);
644         isl_assert(set->ctx, 0, goto error);
645 error:
646         isl_printer_free(p);
647         return NULL;
648 }
649
650 void isl_set_print(struct isl_set *set, FILE *out, int indent,
651         unsigned output_format)
652 {
653         isl_printer *printer;
654
655         if (!set)
656                 return;
657
658         printer = isl_printer_to_file(set->ctx, out);
659         printer = isl_printer_set_indent(printer, indent);
660         printer = isl_printer_set_output_format(printer, output_format);
661         printer = isl_printer_print_set(printer, set);
662
663         isl_printer_free(printer);
664 }
665
666 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
667         __isl_keep isl_map *map)
668 {
669         if (!p || !map)
670                 goto error;
671
672         if (p->output_format == ISL_FORMAT_ISL)
673                 return isl_map_print_isl(map, p);
674         else if (p->output_format == ISL_FORMAT_POLYLIB)
675                 return isl_map_print_polylib(map, p);
676         else if (p->output_format == ISL_FORMAT_OMEGA)
677                 return isl_map_print_omega(map, p);
678         isl_assert(map->ctx, 0, goto error);
679 error:
680         isl_printer_free(p);
681         return NULL;
682 }
683
684 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
685         unsigned output_format)
686 {
687         isl_printer *printer;
688
689         if (!map)
690                 return;
691
692         printer = isl_printer_to_file(map->ctx, out);
693         printer = isl_printer_set_indent(printer, indent);
694         printer = isl_printer_set_output_format(printer, output_format);
695         printer = isl_printer_print_map(printer, map);
696
697         isl_printer_free(printer);
698 }
699
700 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
701 {
702         int i;
703         int n;
704
705         for (i = 0, n = 0; i < rec->n; ++i)
706                 if (!isl_upoly_is_zero(rec->p[i]))
707                         ++n;
708
709         return n;
710 }
711
712 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
713         __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
714 {
715         int c = p->output_format == ISL_FORMAT_C;
716         p = isl_printer_print_str(p, c ? "floord(" : "[(");
717         p = print_affine_of_len(dim, p, div->row[pos] + 1, div->n_col - 1, 1);
718         p = isl_printer_print_str(p, c ? ", " : ")/");
719         p = isl_printer_print_isl_int(p, div->row[pos][0]);
720         p = isl_printer_print_str(p, c ? ")" : "]");
721         return p;
722 }
723
724 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
725         __isl_take isl_printer *p, int first)
726 {
727         struct isl_upoly_cst *cst;
728         int neg;
729
730         cst = isl_upoly_as_cst(up);
731         if (!cst)
732                 goto error;
733         neg = !first && isl_int_is_neg(cst->n);
734         if (!first)
735                 p = isl_printer_print_str(p, neg ? " - " :  " + ");
736         if (neg)
737                 isl_int_neg(cst->n, cst->n);
738         if (isl_int_is_zero(cst->d)) {
739                 int sgn = isl_int_sgn(cst->n);
740                 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
741                                             sgn == 0 ? "NaN" : "infty");
742         } else
743                 p = isl_printer_print_isl_int(p, cst->n);
744         if (neg)
745                 isl_int_neg(cst->n, cst->n);
746         if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
747                 p = isl_printer_print_str(p, "/");
748                 p = isl_printer_print_isl_int(p, cst->d);
749         }
750         return p;
751 error:
752         isl_printer_free(p);
753         return NULL;
754 }
755
756 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
757         __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
758 {
759         unsigned total;
760
761         total = isl_dim_total(dim);
762         if (var < total)
763                 p = print_term(dim, dim->ctx->one, 1 + var, p, 1);
764         else
765                 p = print_div(dim, div, var - total, p);
766         return p;
767 }
768
769 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
770         __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
771 {
772         p = print_base(p, dim, div, var);
773         if (exp == 1)
774                 return p;
775         if (p->output_format == ISL_FORMAT_C) {
776                 int i;
777                 for (i = 1; i < exp; ++i) {
778                         p = isl_printer_print_str(p, "*");
779                         p = print_base(p, dim, div, var);
780                 }
781         } else {
782                 p = isl_printer_print_str(p, "^");
783                 p = isl_printer_print_int(p, exp);
784         }
785         return p;
786 }
787
788 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
789         __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
790         __isl_take isl_printer *p)
791 {
792         int i, n, first;
793         struct isl_upoly_rec *rec;
794
795         if (!p || !up || !dim || !div)
796                 goto error;
797
798         if (isl_upoly_is_cst(up))
799                 return upoly_print_cst(up, p, 1);
800
801         rec = isl_upoly_as_rec(up);
802         if (!rec)
803                 goto error;
804         n = upoly_rec_n_non_zero(rec);
805         if (n > 1)
806                 p = isl_printer_print_str(p, "(");
807         for (i = 0, first = 1; i < rec->n; ++i) {
808                 if (isl_upoly_is_zero(rec->p[i]))
809                         continue;
810                 if (isl_upoly_is_negone(rec->p[i])) {
811                         if (!i)
812                                 p = isl_printer_print_str(p, "-1");
813                         else if (first)
814                                 p = isl_printer_print_str(p, "-");
815                         else
816                                 p = isl_printer_print_str(p, " - ");
817                 } else if (isl_upoly_is_cst(rec->p[i]) &&
818                                 !isl_upoly_is_one(rec->p[i]))
819                         p = upoly_print_cst(rec->p[i], p, first);
820                 else {
821                         if (!first)
822                                 p = isl_printer_print_str(p, " + ");
823                         if (i == 0 || !isl_upoly_is_one(rec->p[i]))
824                                 p = upoly_print(rec->p[i], dim, div, p);
825                 }
826                 first = 0;
827                 if (i == 0)
828                         continue;
829                 if (!isl_upoly_is_one(rec->p[i]) &&
830                     !isl_upoly_is_negone(rec->p[i]))
831                         p = isl_printer_print_str(p, " * ");
832                 p = print_pow(p, dim, div, rec->up.var, i);
833         }
834         if (n > 1)
835                 p = isl_printer_print_str(p, ")");
836         return p;
837 error:
838         isl_printer_free(p);
839         return NULL;
840 }
841
842 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
843         __isl_keep isl_qpolynomial *qp)
844 {
845         if (!p || !qp)
846                 goto error;
847         p = upoly_print(qp->upoly, qp->dim, qp->div, p);
848         return p;
849 error:
850         isl_printer_free(p);
851         return NULL;
852 }
853
854 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
855         unsigned output_format)
856 {
857         isl_printer *p;
858
859         if  (!qp)
860                 return;
861
862         isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
863         p = isl_printer_to_file(qp->dim->ctx, out);
864         p = isl_printer_print_qpolynomial(p, qp);
865         isl_printer_free(p);
866 }
867
868 static __isl_give isl_printer *qpolynomial_fold_print(
869         __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
870 {
871         int i;
872
873         if (fold->type == isl_fold_min)
874                 p = isl_printer_print_str(p, "min");
875         else if (fold->type == isl_fold_max)
876                 p = isl_printer_print_str(p, "max");
877         p = isl_printer_print_str(p, "(");
878         for (i = 0; i < fold->n; ++i) {
879                 if (i)
880                         p = isl_printer_print_str(p, ", ");
881                 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
882         }
883         p = isl_printer_print_str(p, ")");
884         return p;
885 }
886
887 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
888         __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
889 {
890         if  (!p || !fold)
891                 goto error;
892         p = qpolynomial_fold_print(fold, p);
893         return p;
894 error:
895         isl_printer_free(p);
896         return NULL;
897 }
898
899 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
900         FILE *out, unsigned output_format)
901 {
902         isl_printer *p;
903
904         if (!fold)
905                 return;
906
907         isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
908
909         p = isl_printer_to_file(fold->dim->ctx, out);
910         p = isl_printer_print_qpolynomial_fold(p, fold);
911
912         isl_printer_free(p);
913 }
914
915 static __isl_give isl_printer *print_pw_qpolynomial_isl(
916         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
917 {
918         int i = 0;
919
920         if (!p || !pwqp)
921                 goto error;
922
923         if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
924                 p = print_tuple(pwqp->dim, p, isl_dim_param, 0);
925                 p = isl_printer_print_str(p, " -> ");
926         }
927         p = isl_printer_print_str(p, "{ ");
928         if (pwqp->n == 0) {
929                 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0) {
930                         p = print_tuple(pwqp->dim, p, isl_dim_set, 1);
931                         p = isl_printer_print_str(p, " -> ");
932                 }
933                 p = isl_printer_print_str(p, "0");
934         }
935         for (i = 0; i < pwqp->n; ++i) {
936                 if (i)
937                         p = isl_printer_print_str(p, "; ");
938                 if (isl_dim_size(pwqp->p[i].set->dim, isl_dim_set) > 0) {
939                         p = print_tuple(pwqp->p[i].set->dim, p, isl_dim_set, 1);
940                         p = isl_printer_print_str(p, " -> ");
941                 }
942                 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
943                 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1);
944         }
945         p = isl_printer_print_str(p, " }");
946         return p;
947 error:
948         isl_printer_free(p);
949         return NULL;
950 }
951
952 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
953         unsigned output_format)
954 {
955         isl_printer *p;
956
957         if (!pwqp)
958                 return;
959
960         p = isl_printer_to_file(pwqp->dim->ctx, out);
961         p = isl_printer_set_output_format(p, output_format);
962         p = isl_printer_print_pw_qpolynomial(p, pwqp);
963
964         isl_printer_free(p);
965 }
966
967 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
968         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
969 {
970         int i = 0;
971
972         if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
973                 p = print_tuple(pwf->dim, p, isl_dim_param, 0);
974                 p = isl_printer_print_str(p, " -> ");
975         }
976         p = isl_printer_print_str(p, "{ ");
977         if (pwf->n == 0) {
978                 if (isl_dim_size(pwf->dim, isl_dim_set) > 0) {
979                         p = print_tuple(pwf->dim, p, isl_dim_set, 0);
980                         p = isl_printer_print_str(p, " -> ");
981                 }
982                 p = isl_printer_print_str(p, "0");
983         }
984         for (i = 0; i < pwf->n; ++i) {
985                 if (i)
986                         p = isl_printer_print_str(p, "; ");
987                 if (isl_dim_size(pwf->p[i].set->dim, isl_dim_set) > 0) {
988                         p = print_tuple(pwf->p[i].set->dim, p, isl_dim_set, 0);
989                         p = isl_printer_print_str(p, " -> ");
990                 }
991                 p = qpolynomial_fold_print(pwf->p[i].fold, p);
992                 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1);
993         }
994         p = isl_printer_print_str(p, " }");
995         return p;
996 }
997
998 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
999         __isl_keep isl_basic_set *bset, isl_int *c);
1000
1001 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1002         __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1003 {
1004         if (type == isl_dim_div) {
1005                 p = isl_printer_print_str(p, "floord(");
1006                 p = print_affine_c(p, bset, bset->div[pos] + 1);
1007                 p = isl_printer_print_str(p, ", ");
1008                 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1009                 p = isl_printer_print_str(p, ")");
1010         } else {
1011                 const char *name;
1012
1013                 name = isl_dim_get_name(bset->dim, type, pos);
1014                 if (!name)
1015                         name = "UNNAMED";
1016                 p = isl_printer_print_str(p, name);
1017         }
1018         return p;
1019 }
1020
1021 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1022         __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1023 {
1024         enum isl_dim_type type;
1025         unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1026
1027         if (pos == 0)
1028                 return isl_printer_print_isl_int(p, c);
1029
1030         if (isl_int_is_one(c))
1031                 ;
1032         else if (isl_int_is_negone(c))
1033                 p = isl_printer_print_str(p, "-");
1034         else {
1035                 p = isl_printer_print_isl_int(p, c);
1036                 p = isl_printer_print_str(p, "*");
1037         }
1038         type = pos2type(bset->dim, &pos);
1039         p = print_name_c(p, bset, type, pos);
1040         return p;
1041 }
1042
1043 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1044         __isl_keep isl_basic_set *bset, isl_int *c)
1045 {
1046         int i;
1047         int first;
1048         unsigned len = 1 + isl_basic_set_total_dim(bset);
1049
1050         for (i = 0, first = 1; i < len; ++i) {
1051                 int flip = 0;
1052                 if (isl_int_is_zero(c[i]))
1053                         continue;
1054                 if (!first) {
1055                         if (isl_int_is_neg(c[i])) {
1056                                 flip = 1;
1057                                 isl_int_neg(c[i], c[i]);
1058                                 p = isl_printer_print_str(p, " - ");
1059                         } else 
1060                                 p = isl_printer_print_str(p, " + ");
1061                 }
1062                 first = 0;
1063                 p = print_term(bset->dim, c[i], i, p, 1);
1064                 if (flip)
1065                         isl_int_neg(c[i], c[i]);
1066         }
1067         if (first)
1068                 p = isl_printer_print_str(p, "0");
1069         return p;
1070 }
1071
1072 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1073         __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1074 {
1075         if (!first)
1076                 p = isl_printer_print_str(p, " && ");
1077
1078         p = print_affine_c(p, bset, c);
1079         p = isl_printer_print_str(p, " ");
1080         p = isl_printer_print_str(p, op);
1081         p = isl_printer_print_str(p, " 0");
1082         return p;
1083 }
1084
1085 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1086         __isl_keep isl_basic_set *bset)
1087 {
1088         int i;
1089
1090         for (i = 0; i < bset->n_eq; ++i)
1091                 p = print_constraint_c(p, bset, bset->eq[i], "==", !i);
1092         for (i = 0; i < bset->n_ineq; ++i)
1093                 p = print_constraint_c(p, bset, bset->ineq[i], ">=",
1094                                         !bset->n_eq && !i);
1095         return p;
1096 }
1097
1098 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1099         __isl_keep isl_set *set)
1100 {
1101         int i;
1102
1103         if (set->n == 0)
1104                 p = isl_printer_print_str(p, "0");
1105
1106         for (i = 0; i < set->n; ++i) {
1107                 if (i)
1108                         p = isl_printer_print_str(p, " || ");
1109                 if (set->n > 1)
1110                         p = isl_printer_print_str(p, "(");
1111                 p = print_basic_set_c(p, set->p[i]);
1112                 if (set->n > 1)
1113                         p = isl_printer_print_str(p, ")");
1114         }
1115         return p;
1116 }
1117
1118 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1119         __isl_keep isl_qpolynomial *qp)
1120 {
1121         isl_int den;
1122
1123         isl_int_init(den);
1124         isl_qpolynomial_get_den(qp, &den);
1125         if (!isl_int_is_one(den)) {
1126                 isl_qpolynomial *f;
1127                 p = isl_printer_print_str(p, "(");
1128                 qp = isl_qpolynomial_copy(qp);
1129                 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1130                                                 den, qp->dim->ctx->one);
1131                 qp = isl_qpolynomial_mul(qp, f);
1132         }
1133         if (qp)
1134                 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1135         if (!isl_int_is_one(den)) {
1136                 p = isl_printer_print_str(p, ")/");
1137                 p = isl_printer_print_isl_int(p, den);
1138                 isl_qpolynomial_free(qp);
1139         }
1140         isl_int_clear(den);
1141         return p;
1142 }
1143
1144 static __isl_give isl_printer *print_pw_qpolynomial_c(
1145         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwpq)
1146 {
1147         int i;
1148
1149         if (pwpq->n == 1 && isl_set_fast_is_universe(pwpq->p[0].set))
1150                 return print_qpolynomial_c(p, pwpq->p[0].qp);
1151
1152         for (i = 0; i < pwpq->n; ++i) {
1153                 p = isl_printer_print_str(p, "(");
1154                 p = print_set_c(p, pwpq->p[i].set);
1155                 p = isl_printer_print_str(p, ") ? (");
1156                 p = print_qpolynomial_c(p, pwpq->p[i].qp);
1157                 p = isl_printer_print_str(p, ") : ");
1158         }
1159
1160         p = isl_printer_print_str(p, "0");
1161         return p;
1162 }
1163
1164 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1165         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1166 {
1167         if (!p || !pwqp)
1168                 goto error;
1169
1170         if (p->output_format == ISL_FORMAT_ISL)
1171                 return print_pw_qpolynomial_isl(p, pwqp);
1172         else if (p->output_format == ISL_FORMAT_C)
1173                 return print_pw_qpolynomial_c(p, pwqp);
1174         isl_assert(p->ctx, 0, goto error);
1175 error:
1176         isl_printer_free(p);
1177         return NULL;
1178 }
1179
1180 static __isl_give isl_printer *print_qpolynomial_fold_c(
1181         __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1182 {
1183         int i;
1184
1185         for (i = 0; i < fold->n - 1; ++i)
1186                 if (fold->type == isl_fold_min)
1187                         p = isl_printer_print_str(p, "min(");
1188                 else if (fold->type == isl_fold_max)
1189                         p = isl_printer_print_str(p, "max(");
1190
1191         for (i = 0; i < fold->n; ++i) {
1192                 if (i)
1193                         p = isl_printer_print_str(p, ", ");
1194                 p = print_qpolynomial_c(p, fold->qp[i]);
1195                 if (i)
1196                         p = isl_printer_print_str(p, ")");
1197         }
1198         return p;
1199 }
1200
1201 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1202         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1203 {
1204         int i;
1205
1206         if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1207                 return print_qpolynomial_fold_c(p, pwf->p[0].fold);
1208
1209         for (i = 0; i < pwf->n; ++i) {
1210                 p = isl_printer_print_str(p, "(");
1211                 p = print_set_c(p, pwf->p[i].set);
1212                 p = isl_printer_print_str(p, ") ? (");
1213                 p = print_qpolynomial_fold_c(p, pwf->p[i].fold);
1214                 p = isl_printer_print_str(p, ") : ");
1215         }
1216
1217         p = isl_printer_print_str(p, "0");
1218         return p;
1219 }
1220
1221 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1222         __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1223 {
1224         if (!p || !pwf)
1225                 goto error;
1226
1227         if (p->output_format == ISL_FORMAT_ISL)
1228                 return print_pw_qpolynomial_fold_isl(p, pwf);
1229         else if (p->output_format == ISL_FORMAT_C)
1230                 return print_pw_qpolynomial_fold_c(p, pwf);
1231         isl_assert(p->ctx, 0, goto error);
1232 error:
1233         isl_printer_free(p);
1234         return NULL;
1235 }
1236
1237 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1238         FILE *out, unsigned output_format)
1239 {
1240         isl_printer *p;
1241
1242         if (!pwf)
1243                 return;
1244
1245         p = isl_printer_to_file(pwf->dim->ctx, out);
1246         p = isl_printer_set_output_format(p, output_format);
1247         p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1248
1249         isl_printer_free(p);
1250 }