add isl_pw_qpolynomial_fold
[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
18 static void print_constraint_polylib(struct isl_basic_map *bmap,
19         int ineq, int n,
20         FILE *out, int indent, const char *prefix, const char *suffix)
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         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
29         fprintf(out, "%d", ineq);
30         for (i = 0; i < n_out; ++i) {
31                 fprintf(out, " ");
32                 isl_int_print(out, c[1+nparam+n_in+i], 5);
33         }
34         for (i = 0; i < n_in; ++i) {
35                 fprintf(out, " ");
36                 isl_int_print(out, c[1+nparam+i], 5);
37         }
38         for (i = 0; i < bmap->n_div; ++i) {
39                 fprintf(out, " ");
40                 isl_int_print(out, c[1+nparam+n_in+n_out+i], 5);
41         }
42         for (i = 0; i < nparam; ++i) {
43                 fprintf(out, " ");
44                 isl_int_print(out, c[1+i], 5);
45         }
46         fprintf(out, " ");
47         isl_int_print(out, c[0], 5);
48         fprintf(out, "%s\n", suffix ? suffix : "");
49 }
50
51 static void print_constraints_polylib(struct isl_basic_map *bmap,
52         FILE *out, int indent, const char *prefix, const char *suffix)
53 {
54         int i;
55
56         for (i = 0; i < bmap->n_eq; ++i)
57                 print_constraint_polylib(bmap, 0, i, out,
58                                         indent, prefix, suffix);
59         for (i = 0; i < bmap->n_ineq; ++i)
60                 print_constraint_polylib(bmap, 1, i, out,
61                                         indent, prefix, suffix);
62 }
63
64 static void bset_print_constraints_polylib(struct isl_basic_set *bset,
65         FILE *out, int indent, const char *prefix, const char *suffix)
66 {
67         print_constraints_polylib((struct isl_basic_map *)bset,
68                                  out, indent, prefix, suffix);
69 }
70
71 static void isl_basic_map_print_polylib(struct isl_basic_map *bmap, FILE *out,
72         int indent, const char *prefix, const char *suffix)
73 {
74         unsigned total = isl_basic_map_total_dim(bmap);
75         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
76         fprintf(out, "%d %d", bmap->n_eq + bmap->n_ineq, 1 + total + 1);
77         fprintf(out, "%s\n", suffix ? suffix : "");
78         print_constraints_polylib(bmap, out, indent, prefix, suffix);
79 }
80
81 static void isl_basic_set_print_polylib(struct isl_basic_set *bset, FILE *out,
82         int indent, const char *prefix, const char *suffix)
83 {
84         isl_basic_map_print_polylib((struct isl_basic_map *)bset, out,
85                                         indent, prefix, suffix);
86 }
87
88 static void isl_map_print_polylib(struct isl_map *map, FILE *out, int indent)
89 {
90         int i;
91
92         fprintf(out, "%*s", indent, "");
93         fprintf(out, "%d\n", map->n);
94         for (i = 0; i < map->n; ++i) {
95                 fprintf(out, "\n");
96                 isl_basic_map_print_polylib(map->p[i], out, indent, NULL, NULL);
97         }
98 }
99
100 static void isl_set_print_polylib(struct isl_set *set, FILE *out, int indent)
101 {
102         isl_map_print_polylib((struct isl_map *)set, out, indent);
103 }
104
105 static int count_same_name(__isl_keep isl_dim *dim,
106         enum isl_dim_type type, unsigned pos, const char *name)
107 {
108         enum isl_dim_type t;
109         unsigned p, s;
110         int count = 0;
111
112         for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
113                 s = t == type ? pos : isl_dim_size(dim, t);
114                 for (p = 0; p < s; ++p) {
115                         const char *n = isl_dim_get_name(dim, t, p);
116                         if (n && !strcmp(n, name))
117                                 count++;
118                 }
119         }
120         return count;
121 }
122
123 static void print_name(struct isl_dim *dim, FILE *out,
124         enum isl_dim_type type, unsigned pos, int set)
125 {
126         const char *name;
127         char buffer[20];
128         int primes;
129
130         name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
131
132         if (!name) {
133                 const char *prefix;
134                 if (type == isl_dim_param)
135                         prefix = "p";
136                 else if (type == isl_dim_div)
137                         prefix = "e";
138                 else if (set || type == isl_dim_in)
139                         prefix = "i";
140                 else
141                         prefix = "o";
142                 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
143                 name = buffer;
144         }
145         primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
146                                  pos, name);
147         fprintf(out, "%s", name);
148         while (primes-- > 0)
149                 fputc('\'', out);
150 }
151
152 static void print_var_list(struct isl_dim *dim, FILE *out,
153         enum isl_dim_type type, int set)
154 {
155         int i;
156
157         for (i = 0; i < isl_dim_size(dim, type); ++i) {
158                 if (i)
159                         fprintf(out, ", ");
160                 print_name(dim, out, type, i, set);
161         }
162 }
163
164 static void print_tuple(__isl_keep isl_dim *dim, FILE *out,
165         enum isl_dim_type type, int set)
166 {
167         fprintf(out, "[");
168         print_var_list(dim, out, type, set);
169         fprintf(out, "]");
170 }
171
172 static void print_omega_parameters(struct isl_dim *dim, FILE *out,
173         int indent, const char *prefix, const char *suffix)
174 {
175         if (isl_dim_size(dim, isl_dim_param) == 0)
176                 return;
177
178         fprintf(out, "%*s%ssymbolic ", indent, "", prefix ? prefix : "");
179         print_var_list(dim, out, isl_dim_param, 0);
180         fprintf(out, ";%s\n", suffix ? suffix : "");
181 }
182
183 static void print_term(__isl_keep isl_dim *dim,
184                         isl_int c, int pos, FILE *out, int set)
185 {
186         enum isl_dim_type type;
187         unsigned n_in = isl_dim_size(dim, isl_dim_in);
188         unsigned n_out = isl_dim_size(dim, isl_dim_out);
189         unsigned nparam = isl_dim_size(dim, isl_dim_param);
190
191         if (pos == 0) {
192                 isl_int_print(out, c, 0);
193                 return;
194         }
195
196         if (isl_int_is_one(c))
197                 ;
198         else if (isl_int_is_negone(c))
199                 fprintf(out, "-");
200         else
201                 isl_int_print(out, c, 0);
202         if (pos < 1 + nparam) {
203                 type = isl_dim_param;
204                 pos -= 1;
205         } else if (pos < 1 + nparam + n_in) {
206                 type = isl_dim_in;
207                 pos -= 1 + nparam;
208         } else if (pos < 1 + nparam + n_in + n_out) {
209                 type = isl_dim_out;
210                 pos -= 1 + nparam + n_in;
211         } else {
212                 type = isl_dim_div;
213                 pos -= 1 + nparam + n_in + n_out;
214         }
215         print_name(dim, out, type, pos, set);
216 }
217
218 static void print_affine_of_len(__isl_keep isl_dim *dim, FILE *out,
219         isl_int *c, int len, int set)
220 {
221         int i;
222         int first;
223
224         for (i = 0, first = 1; i < len; ++i) {
225                 int flip = 0;
226                 if (isl_int_is_zero(c[i]))
227                         continue;
228                 if (!first) {
229                         if (isl_int_is_neg(c[i])) {
230                                 flip = 1;
231                                 isl_int_neg(c[i], c[i]);
232                                 fprintf(out, " - ");
233                         } else 
234                                 fprintf(out, " + ");
235                 }
236                 first = 0;
237                 print_term(dim, c[i], i, out, set);
238                 if (flip)
239                         isl_int_neg(c[i], c[i]);
240         }
241         if (first)
242                 fprintf(out, "0");
243 }
244
245 static void print_affine(__isl_keep isl_basic_map *bmap,
246         __isl_keep isl_dim *dim, FILE *out, isl_int *c, int set)
247 {
248         unsigned len = 1 + isl_basic_map_total_dim(bmap);
249         print_affine_of_len(dim, out, c, len, set);
250 }
251
252 static void print_constraint(struct isl_basic_map *bmap,
253         __isl_keep isl_dim *dim, FILE *out,
254         isl_int *c, int last, const char *op, int first_constraint, int set)
255 {
256         if (!first_constraint)
257                 fprintf(out, " and ");
258
259         isl_int_abs(c[last], c[last]);
260
261         print_term(dim, c[last], last, out, set);
262
263         fprintf(out, " %s ", op);
264
265         isl_int_set_si(c[last], 0);
266         print_affine(bmap, dim, out, c, set);
267 }
268
269 static void print_constraints(__isl_keep isl_basic_map *bmap,
270         __isl_keep isl_dim *dim, FILE *out, int set)
271 {
272         int i;
273         struct isl_vec *c;
274         unsigned total = isl_basic_map_total_dim(bmap);
275
276         c = isl_vec_alloc(bmap->ctx, 1 + total);
277         if (!c)
278                 return;
279
280         for (i = bmap->n_eq - 1; i >= 0; --i) {
281                 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
282                 isl_assert(bmap->ctx, l >= 0, return);
283                 if (isl_int_is_neg(bmap->eq[i][l]))
284                         isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
285                 else
286                         isl_seq_neg(c->el, bmap->eq[i], 1 + total);
287                 print_constraint(bmap, dim, out, c->el, l,
288                                     "=", i == bmap->n_eq - 1, set);
289         }
290         for (i = 0; i < bmap->n_ineq; ++i) {
291                 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
292                 int s;
293                 isl_assert(bmap->ctx, l >= 0, return);
294                 s = isl_int_sgn(bmap->ineq[i][l]);
295                 if (s < 0)
296                         isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
297                 else
298                         isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
299                 print_constraint(bmap, dim, out, c->el, l,
300                                     s < 0 ? "<=" : ">=", !bmap->n_eq && !i, set);
301         }
302
303         isl_vec_free(c);
304 }
305
306 static void print_omega_constraints(__isl_keep isl_basic_map *bmap, FILE *out,
307         int set)
308 {
309         if (bmap->n_eq + bmap->n_ineq == 0)
310                 return;
311
312         fprintf(out, ": ");
313         if (bmap->n_div > 0) {
314                 int i;
315                 fprintf(out, "exists (");
316                 for (i = 0; i < bmap->n_div; ++i) {
317                         if (i)
318                                 fprintf(out, ", ");
319                         print_name(bmap->dim, out, isl_dim_div, i, 0);
320                 }
321                 fprintf(out, ": ");
322         }
323         print_constraints(bmap, bmap->dim, out, set);
324         if (bmap->n_div > 0)
325                 fprintf(out, ")");
326 }
327
328 static void basic_map_print_omega(struct isl_basic_map *bmap, FILE *out)
329 {
330         fprintf(out, "{ [");
331         print_var_list(bmap->dim, out, isl_dim_in, 0);
332         fprintf(out, "] -> [");
333         print_var_list(bmap->dim, out, isl_dim_out, 0);
334         fprintf(out, "] ");
335         print_omega_constraints(bmap, out, 0);
336         fprintf(out, " }");
337 }
338
339 static void isl_basic_map_print_omega(struct isl_basic_map *bmap, FILE *out,
340         int indent, const char *prefix, const char *suffix)
341 {
342         print_omega_parameters(bmap->dim, out, indent, prefix, suffix);
343
344         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
345         basic_map_print_omega(bmap, out);
346         fprintf(out, "%s\n", suffix ? suffix : "");
347 }
348
349 static void basic_set_print_omega(struct isl_basic_set *bset, FILE *out)
350 {
351         fprintf(out, "{ [");
352         print_var_list(bset->dim, out, isl_dim_set, 1);
353         fprintf(out, "] ");
354         print_omega_constraints((isl_basic_map *)bset, out, 1);
355         fprintf(out, " }");
356 }
357
358 static void isl_basic_set_print_omega(struct isl_basic_set *bset, FILE *out,
359         int indent, const char *prefix, const char *suffix)
360 {
361         print_omega_parameters(bset->dim, out, indent, prefix, suffix);
362
363         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
364         basic_set_print_omega(bset, out);
365         fprintf(out, "%s\n", suffix ? suffix : "");
366 }
367
368 static void isl_map_print_omega(struct isl_map *map, FILE *out, int indent)
369 {
370         int i;
371
372         print_omega_parameters(map->dim, out, indent, "", "");
373
374         fprintf(out, "%*s", indent, "");
375         for (i = 0; i < map->n; ++i) {
376                 if (i)
377                         fprintf(out, " union ");
378                 basic_map_print_omega(map->p[i], out);
379         }
380         fprintf(out, "\n");
381 }
382
383 static void isl_set_print_omega(struct isl_set *set, FILE *out, int indent)
384 {
385         int i;
386
387         print_omega_parameters(set->dim, out, indent, "", "");
388
389         fprintf(out, "%*s", indent, "");
390         for (i = 0; i < set->n; ++i) {
391                 if (i)
392                         fprintf(out, " union ");
393                 basic_set_print_omega(set->p[i], out);
394         }
395         fprintf(out, "\n");
396 }
397
398 static void print_disjunct(__isl_keep isl_basic_map *bmap,
399         __isl_keep isl_dim *dim, FILE *out, int set)
400 {
401         if (bmap->n_div > 0) {
402                 int i;
403                 fprintf(out, "exists (");
404                 for (i = 0; i < bmap->n_div; ++i) {
405                         if (i)
406                                 fprintf(out, ", ");
407                         print_name(dim, out, isl_dim_div, i, 0);
408                         if (isl_int_is_zero(bmap->div[i][0]))
409                                 continue;
410                         fprintf(out, " = [(");
411                         print_affine(bmap, dim, out, bmap->div[i] + 1, set);
412                         fprintf(out, ")/");
413                         isl_int_print(out, bmap->div[i][0], 0);
414                         fprintf(out, "]");
415                 }
416                 fprintf(out, ": ");
417         }
418
419         print_constraints(bmap, dim, out, set);
420
421         if (bmap->n_div > 0)
422                 fprintf(out, ")");
423 }
424
425 static void isl_basic_map_print_isl(__isl_keep isl_basic_map *bmap, FILE *out,
426         int indent, const char *prefix, const char *suffix)
427 {
428         int i;
429
430         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
431         if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
432                 print_tuple(bmap->dim, out, isl_dim_param, 0);
433                 fprintf(out, " -> ");
434         }
435         fprintf(out, "{ ");
436         print_tuple(bmap->dim, out, isl_dim_in, 0);
437         fprintf(out, " -> ");
438         print_tuple(bmap->dim, out, isl_dim_out, 0);
439         fprintf(out, " : ");
440         print_disjunct(bmap, bmap->dim, out, 0);
441         fprintf(out, " }%s\n", suffix ? suffix : "");
442 }
443
444 static void isl_basic_set_print_isl(__isl_keep isl_basic_set *bset, FILE *out,
445         int indent, const char *prefix, const char *suffix)
446 {
447         int i;
448
449         fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
450         if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
451                 print_tuple(bset->dim, out, isl_dim_param, 0);
452                 fprintf(out, " -> ");
453         }
454         fprintf(out, "{ ");
455         print_tuple(bset->dim, out, isl_dim_set, 1);
456         fprintf(out, " : ");
457         print_disjunct((isl_basic_map *)bset, bset->dim, out, 1);
458         fprintf(out, " }%s\n", suffix ? suffix : "");
459 }
460
461 static void print_disjuncts(__isl_keep isl_map *map, FILE *out, int set)
462 {
463         int i;
464
465         if (isl_map_fast_is_universe(map))
466                 return;
467
468         fprintf(out, " : ");
469         if (map->n == 0)
470                 fprintf(out, "1 = 0");
471         for (i = 0; i < map->n; ++i) {
472                 if (i)
473                         fprintf(out, " or ");
474                 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
475                         fprintf(out, "(");
476                 print_disjunct(map->p[i], map->dim, out, set);
477                 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
478                         fprintf(out, ")");
479         }
480 }
481
482 static void isl_map_print_isl(__isl_keep isl_map *map, FILE *out, int indent)
483 {
484         fprintf(out, "%*s", indent, "");
485         if (isl_map_dim(map, isl_dim_param) > 0) {
486                 print_tuple(map->dim, out, isl_dim_param, 0);
487                 fprintf(out, " -> ");
488         }
489         fprintf(out, "{ ");
490         print_tuple(map->dim, out, isl_dim_in, 0);
491         fprintf(out, " -> ");
492         print_tuple(map->dim, out, isl_dim_out, 0);
493         print_disjuncts(map, out, 0);
494         fprintf(out, " }\n");
495 }
496
497 static void isl_set_print_isl(__isl_keep isl_set *set, FILE *out, int indent)
498 {
499         int i;
500
501         fprintf(out, "%*s", indent, "");
502         if (isl_set_dim(set, isl_dim_param) > 0) {
503                 print_tuple(set->dim, out, isl_dim_param, 0);
504                 fprintf(out, " -> ");
505         }
506         fprintf(out, "{ ");
507         print_tuple(set->dim, out, isl_dim_set, 1);
508         print_disjuncts((isl_map *)set, out, 1);
509         fprintf(out, " }\n");
510 }
511
512 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
513         const char *prefix, const char *suffix, unsigned output_format)
514 {
515         if (!bmap)
516                 return;
517         if (output_format == ISL_FORMAT_ISL)
518                 isl_basic_map_print_isl(bmap, out, indent, prefix, suffix);
519         else if (output_format == ISL_FORMAT_OMEGA)
520                 isl_basic_map_print_omega(bmap, out, indent, prefix, suffix);
521         else
522                 isl_assert(bmap->ctx, 0, return);
523 }
524
525 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
526         const char *prefix, const char *suffix, unsigned output_format)
527 {
528         if (!bset)
529                 return;
530         if (output_format == ISL_FORMAT_ISL)
531                 isl_basic_set_print_isl(bset, out, indent, prefix, suffix);
532         else if (output_format == ISL_FORMAT_POLYLIB)
533                 isl_basic_set_print_polylib(bset, out, indent, prefix, suffix);
534         else if (output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
535                 bset_print_constraints_polylib(bset, out, indent, prefix, suffix);
536         else if (output_format == ISL_FORMAT_OMEGA)
537                 isl_basic_set_print_omega(bset, out, indent, prefix, suffix);
538         else
539                 isl_assert(bset->ctx, 0, return);
540 }
541
542 void isl_set_print(struct isl_set *set, FILE *out, int indent,
543         unsigned output_format)
544 {
545         if (!set)
546                 return;
547         if (output_format == ISL_FORMAT_ISL)
548                 isl_set_print_isl(set, out, indent);
549         else if (output_format == ISL_FORMAT_POLYLIB)
550                 isl_set_print_polylib(set, out, indent);
551         else if (output_format == ISL_FORMAT_OMEGA)
552                 isl_set_print_omega(set, out, indent);
553         else
554                 isl_assert(set->ctx, 0, return);
555 }
556
557 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
558         unsigned output_format)
559 {
560         if (!map)
561                 return;
562         if (output_format == ISL_FORMAT_ISL)
563                 isl_map_print_isl(map, out, indent);
564         else if (output_format == ISL_FORMAT_POLYLIB)
565                 isl_map_print_polylib(map, out, indent);
566         else if (output_format == ISL_FORMAT_OMEGA)
567                 isl_map_print_omega(map, out, indent);
568         else
569                 isl_assert(map->ctx, 0, return);
570 }
571
572 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
573 {
574         int i;
575         int n;
576
577         for (i = 0, n = 0; i < rec->n; ++i)
578                 if (!isl_upoly_is_zero(rec->p[i]))
579                         ++n;
580
581         return n;
582 }
583
584 static void print_div(__isl_keep isl_dim *dim, __isl_keep isl_mat *div,
585         int pos, FILE *out)
586 {
587         fprintf(out, "[(");
588         print_affine_of_len(dim, out, div->row[pos] + 1, div->n_col - 1, 1);
589         fprintf(out, ")/");
590         isl_int_print(out, div->row[pos][0], 0);
591         fprintf(out, "]");
592 }
593
594 static void upoly_print_cst(__isl_keep struct isl_upoly *up, FILE *out, int first)
595 {
596         struct isl_upoly_cst *cst;
597         int neg;
598
599         cst = isl_upoly_as_cst(up);
600         if (!cst)
601                 return;
602         neg = !first && isl_int_is_neg(cst->n);
603         if (!first)
604                 fprintf(out, neg ? " - " :  " + ");
605         if (neg)
606                 isl_int_neg(cst->n, cst->n);
607         if (isl_int_is_zero(cst->d)) {
608                 int sgn = isl_int_sgn(cst->n);
609                 fprintf(out, sgn < 0 ? "-infty" : sgn == 0 ? "NaN" : "infty");
610         } else
611                 isl_int_print(out, cst->n, 0);
612         if (neg)
613                 isl_int_neg(cst->n, cst->n);
614         if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
615                 fprintf(out, "/");
616                 isl_int_print(out, cst->d, 0);
617         }
618 }
619
620 static void upoly_print(__isl_keep struct isl_upoly *up,
621         __isl_keep isl_dim *dim, __isl_keep isl_mat *div, FILE *out)
622 {
623         unsigned total;
624         int i, n, first;
625         struct isl_upoly_rec *rec;
626
627         if (!up || !dim || !div)
628                 return;
629
630         if (isl_upoly_is_cst(up)) {
631                 upoly_print_cst(up, out, 1);
632                 return;
633         }
634
635         total = isl_dim_total(dim);
636         rec = isl_upoly_as_rec(up);
637         if (!rec)
638                 return;
639         n = upoly_rec_n_non_zero(rec);
640         if (n > 1)
641                 fprintf(out, "(");
642         for (i = 0, first = 1; i < rec->n; ++i) {
643                 if (isl_upoly_is_zero(rec->p[i]))
644                         continue;
645                 if (isl_upoly_is_negone(rec->p[i])) {
646                         if (!i)
647                                 fprintf(out, "-1");
648                         else if (first)
649                                 fprintf(out, "-");
650                         else
651                                 fprintf(out, " - ");
652                 } else if (isl_upoly_is_cst(rec->p[i]) &&
653                                 !isl_upoly_is_one(rec->p[i]))
654                         upoly_print_cst(rec->p[i], out, first);
655                 else {
656                         if (!first)
657                                 fprintf(out, " + ");
658                         if (i == 0 || !isl_upoly_is_one(rec->p[i]))
659                                 upoly_print(rec->p[i], dim, div, out);
660                 }
661                 first = 0;
662                 if (i == 0)
663                         continue;
664                 if (!isl_upoly_is_one(rec->p[i]) &&
665                     !isl_upoly_is_negone(rec->p[i]))
666                         fprintf(out, " * ");
667                 if (rec->up.var < total)
668                         print_term(dim, up->ctx->one, 1 + rec->up.var, out, 1);
669                 else
670                         print_div(dim, div, rec->up.var - total, out);
671                 if (i == 1)
672                         continue;
673                 fprintf(out, "^%d", i);
674         }
675         if (n > 1)
676                 fprintf(out, ")");
677 }
678
679 static void qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out)
680 {
681         if (!qp)
682                 return;
683         upoly_print(qp->upoly, qp->dim, qp->div, out);
684 }
685
686 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
687         unsigned output_format)
688 {
689         if  (!qp)
690                 return;
691         isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
692         qpolynomial_print(qp, out);
693         fprintf(out, "\n");
694 }
695
696 static void qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
697         FILE *out)
698 {
699         int i;
700
701         if (fold->type == isl_fold_min)
702                 fprintf(out, "min");
703         else if (fold->type == isl_fold_max)
704                 fprintf(out, "max");
705         fprintf(out, "(");
706         for (i = 0; i < fold->n; ++i) {
707                 if (i)
708                         fprintf(out, ", ");
709                 qpolynomial_print(fold->qp[i], out);
710         }
711         fprintf(out, ")");
712 }
713
714 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold, FILE *out,
715         unsigned output_format)
716 {
717         if  (!fold)
718                 return;
719         isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
720         qpolynomial_fold_print(fold, out);
721         fprintf(out, "\n");
722 }
723
724 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
725         unsigned output_format)
726 {
727         int i = 0;
728
729         if (!pwqp)
730                 return;
731         isl_assert(pwqp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
732         if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
733                 print_tuple(pwqp->dim, out, isl_dim_param, 0);
734                 fprintf(out, " -> ");
735         }
736         fprintf(out, "{ ");
737         if (pwqp->n == 0) {
738                 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0) {
739                         print_tuple(pwqp->dim, out, isl_dim_set, 0);
740                         fprintf(out, " -> ");
741                 }
742                 fprintf(out, "0");
743         }
744         for (i = 0; i < pwqp->n; ++i) {
745                 if (i)
746                         fprintf(out, "; ");
747                 if (isl_dim_size(pwqp->p[i].set->dim, isl_dim_set) > 0) {
748                         print_tuple(pwqp->p[i].set->dim, out, isl_dim_set, 0);
749                         fprintf(out, " -> ");
750                 }
751                 qpolynomial_print(pwqp->p[i].qp, out);
752                 print_disjuncts((isl_map *)pwqp->p[i].set, out, 1);
753         }
754         fprintf(out, " }\n");
755 }
756
757 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
758         FILE *out, unsigned output_format)
759 {
760         int i = 0;
761
762         if (!pwf)
763                 return;
764         isl_assert(pwf->dim->ctx, output_format == ISL_FORMAT_ISL, return);
765         if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
766                 print_tuple(pwf->dim, out, isl_dim_param, 0);
767                 fprintf(out, " -> ");
768         }
769         fprintf(out, "{ ");
770         if (pwf->n == 0) {
771                 if (isl_dim_size(pwf->dim, isl_dim_set) > 0) {
772                         print_tuple(pwf->dim, out, isl_dim_set, 0);
773                         fprintf(out, " -> ");
774                 }
775                 fprintf(out, "0");
776         }
777         for (i = 0; i < pwf->n; ++i) {
778                 if (i)
779                         fprintf(out, "; ");
780                 if (isl_dim_size(pwf->p[i].set->dim, isl_dim_set) > 0) {
781                         print_tuple(pwf->p[i].set->dim, out, isl_dim_set, 0);
782                         fprintf(out, " -> ");
783                 }
784                 qpolynomial_fold_print(pwf->p[i].fold, out);
785                 print_disjuncts((isl_map *)pwf->p[i].set, out, 1);
786         }
787         fprintf(out, " }\n");
788 }