add isl_printer
[isl.git] / isl_output.c
blobbce0d72e43234bc9eccfe72dab3602bc5496ed1e
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
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
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>
19 static __isl_give isl_printer *print_constraint_polylib(
20 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
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];
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]);
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]);
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]);
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]);
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;
52 static __isl_give isl_printer *print_constraints_polylib(
53 struct isl_basic_map *bmap, __isl_take isl_printer *p)
55 int i;
57 p = isl_printer_set_isl_int_width(p, 5);
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);
64 return p;
67 static __isl_give isl_printer *bset_print_constraints_polylib(
68 struct isl_basic_set *bset, __isl_take isl_printer *p)
70 return print_constraints_polylib((struct isl_basic_map *)bset, p);
73 static __isl_give isl_printer *isl_basic_map_print_polylib(
74 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
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);
85 static __isl_give isl_printer *isl_basic_set_print_polylib(
86 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
88 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p);
91 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
92 __isl_take isl_printer *p)
94 int i;
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);
104 return p;
107 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
108 __isl_take isl_printer *p)
110 return isl_map_print_polylib((struct isl_map *)set, p);
113 static int count_same_name(__isl_keep isl_dim *dim,
114 enum isl_dim_type type, unsigned pos, const char *name)
116 enum isl_dim_type t;
117 unsigned p, s;
118 int count = 0;
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++;
128 return count;
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)
134 const char *name;
135 char buffer[20];
136 int primes;
138 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
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;
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;
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)
164 int i;
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);
171 return p;
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)
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;
183 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
184 __isl_take isl_printer *p)
186 if (isl_dim_size(dim, isl_dim_param) == 0)
187 return p;
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;
197 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
198 isl_int c, int pos, __isl_take isl_printer *p, int set)
200 enum isl_dim_type type;
201 unsigned n_in = isl_dim_size(dim, isl_dim_in);
202 unsigned n_out = isl_dim_size(dim, isl_dim_out);
203 unsigned nparam = isl_dim_size(dim, isl_dim_param);
205 if (pos == 0)
206 return isl_printer_print_isl_int(p, c);
208 if (isl_int_is_one(c))
210 else if (isl_int_is_negone(c))
211 p = isl_printer_print_str(p, "-");
212 else
213 p = isl_printer_print_isl_int(p, c);
214 if (pos < 1 + nparam) {
215 type = isl_dim_param;
216 pos -= 1;
217 } else if (pos < 1 + nparam + n_in) {
218 type = isl_dim_in;
219 pos -= 1 + nparam;
220 } else if (pos < 1 + nparam + n_in + n_out) {
221 type = isl_dim_out;
222 pos -= 1 + nparam + n_in;
223 } else {
224 type = isl_dim_div;
225 pos -= 1 + nparam + n_in + n_out;
227 p = print_name(dim, p, type, pos, set);
228 return p;
231 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
232 __isl_take isl_printer *p, isl_int *c, int len, int set)
234 int i;
235 int first;
237 for (i = 0, first = 1; i < len; ++i) {
238 int flip = 0;
239 if (isl_int_is_zero(c[i]))
240 continue;
241 if (!first) {
242 if (isl_int_is_neg(c[i])) {
243 flip = 1;
244 isl_int_neg(c[i], c[i]);
245 p = isl_printer_print_str(p, " - ");
246 } else
247 p = isl_printer_print_str(p, " + ");
249 first = 0;
250 p = print_term(dim, c[i], i, p, set);
251 if (flip)
252 isl_int_neg(c[i], c[i]);
254 if (first)
255 p = isl_printer_print_str(p, "0");
256 return p;
259 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
260 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
262 unsigned len = 1 + isl_basic_map_total_dim(bmap);
263 return print_affine_of_len(dim, p, c, len, set);
266 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
267 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
268 isl_int *c, int last, const char *op, int first_constraint, int set)
270 if (!first_constraint)
271 p = isl_printer_print_str(p, " and ");
273 isl_int_abs(c[last], c[last]);
275 p = print_term(dim, c[last], last, p, set);
277 p = isl_printer_print_str(p, " ");
278 p = isl_printer_print_str(p, op);
279 p = isl_printer_print_str(p, " ");
281 isl_int_set_si(c[last], 0);
282 p = print_affine(bmap, dim, p, c, set);
284 return p;
287 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
288 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set)
290 int i;
291 struct isl_vec *c;
292 unsigned total = isl_basic_map_total_dim(bmap);
294 c = isl_vec_alloc(bmap->ctx, 1 + total);
295 if (!c)
296 goto error;
298 for (i = bmap->n_eq - 1; i >= 0; --i) {
299 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
300 isl_assert(bmap->ctx, l >= 0, goto error);
301 if (isl_int_is_neg(bmap->eq[i][l]))
302 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
303 else
304 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
305 p = print_constraint(bmap, dim, p, c->el, l,
306 "=", i == bmap->n_eq - 1, set);
308 for (i = 0; i < bmap->n_ineq; ++i) {
309 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
310 int s;
311 isl_assert(bmap->ctx, l >= 0, goto error);
312 s = isl_int_sgn(bmap->ineq[i][l]);
313 if (s < 0)
314 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
315 else
316 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
317 p = print_constraint(bmap, dim, p, c->el, l,
318 s < 0 ? "<=" : ">=", !bmap->n_eq && !i, set);
321 isl_vec_free(c);
323 return p;
324 error:
325 isl_printer_free(p);
326 return NULL;
329 static __isl_give isl_printer *print_omega_constraints(
330 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
332 if (bmap->n_eq + bmap->n_ineq == 0)
333 return p;
335 p = isl_printer_print_str(p, ": ");
336 if (bmap->n_div > 0) {
337 int i;
338 p = isl_printer_print_str(p, "exists (");
339 for (i = 0; i < bmap->n_div; ++i) {
340 if (i)
341 p = isl_printer_print_str(p, ", ");
342 p = print_name(bmap->dim, p, isl_dim_div, i, 0);
344 p = isl_printer_print_str(p, ": ");
346 p = print_constraints(bmap, bmap->dim, p, set);
347 if (bmap->n_div > 0)
348 p = isl_printer_print_str(p, ")");
349 return p;
352 static __isl_give isl_printer *basic_map_print_omega(
353 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
355 p = isl_printer_print_str(p, "{ [");
356 p = print_var_list(bmap->dim, p, isl_dim_in, 0);
357 p = isl_printer_print_str(p, "] -> [");
358 p = print_var_list(bmap->dim, p, isl_dim_out, 0);
359 p = isl_printer_print_str(p, "] ");
360 p = print_omega_constraints(bmap, p, 0);
361 p = isl_printer_print_str(p, " }");
362 return p;
365 static __isl_give isl_printer *isl_basic_map_print_omega(
366 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
368 p = print_omega_parameters(bmap->dim, p);
370 p = isl_printer_start_line(p);
371 p = basic_map_print_omega(bmap, p);
372 p = isl_printer_end_line(p);
373 return p;
376 static __isl_give isl_printer *basic_set_print_omega(
377 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
379 p = isl_printer_print_str(p, "{ [");
380 p = print_var_list(bset->dim, p, isl_dim_set, 1);
381 p = isl_printer_print_str(p, "] ");
382 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
383 p = isl_printer_print_str(p, " }");
384 return p;
387 static __isl_give isl_printer *isl_basic_set_print_omega(
388 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
390 p = print_omega_parameters(bset->dim, p);
392 p = isl_printer_start_line(p);
393 p = basic_set_print_omega(bset, p);
394 p = isl_printer_end_line(p);
395 return p;
398 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
399 __isl_take isl_printer *p)
401 int i;
403 p = print_omega_parameters(map->dim, p);
405 p = isl_printer_start_line(p);
406 for (i = 0; i < map->n; ++i) {
407 if (i)
408 p = isl_printer_print_str(p, " union ");
409 p = basic_map_print_omega(map->p[i], p);
411 p = isl_printer_end_line(p);
412 return p;
415 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
416 __isl_take isl_printer *p)
418 int i;
420 p = print_omega_parameters(set->dim, p);
422 p = isl_printer_start_line(p);
423 for (i = 0; i < set->n; ++i) {
424 if (i)
425 p = isl_printer_print_str(p, " union ");
426 p = basic_set_print_omega(set->p[i], p);
428 p = isl_printer_end_line(p);
429 return p;
432 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
433 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set)
435 if (bmap->n_div > 0) {
436 int i;
437 p = isl_printer_print_str(p, "exists (");
438 for (i = 0; i < bmap->n_div; ++i) {
439 if (i)
440 p = isl_printer_print_str(p, ", ");
441 p = print_name(dim, p, isl_dim_div, i, 0);
442 if (isl_int_is_zero(bmap->div[i][0]))
443 continue;
444 p = isl_printer_print_str(p, " = [(");
445 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
446 p = isl_printer_print_str(p, ")/");
447 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
448 p = isl_printer_print_str(p, "]");
450 p = isl_printer_print_str(p, ": ");
453 p = print_constraints(bmap, dim, p, set);
455 if (bmap->n_div > 0)
456 p = isl_printer_print_str(p, ")");
457 return p;
460 static __isl_give isl_printer *isl_basic_map_print_isl(
461 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
463 int i;
465 p = isl_printer_start_line(p);
466 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
467 p = print_tuple(bmap->dim, p, isl_dim_param, 0);
468 p = isl_printer_print_str(p, " -> ");
470 p = isl_printer_print_str(p, "{ ");
471 p = print_tuple(bmap->dim, p, isl_dim_in, 0);
472 p = isl_printer_print_str(p, " -> ");
473 p = print_tuple(bmap->dim, p, isl_dim_out, 0);
474 p = isl_printer_print_str(p, " : ");
475 p = print_disjunct(bmap, bmap->dim, p, 0);
476 p = isl_printer_end_line(p);
477 return p;
480 static __isl_give isl_printer *isl_basic_set_print_isl(
481 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
483 int i;
485 p = isl_printer_start_line(p);
486 if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
487 p = print_tuple(bset->dim, p, isl_dim_param, 0);
488 p = isl_printer_print_str(p, " -> ");
490 p = isl_printer_print_str(p, "{ ");
491 p = print_tuple(bset->dim, p, isl_dim_set, 1);
492 p = isl_printer_print_str(p, " : ");
493 p = print_disjunct((isl_basic_map *)bset, bset->dim, p, 1);
494 p = isl_printer_end_line(p);
495 return p;
498 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
499 __isl_take isl_printer *p, int set)
501 int i;
503 if (isl_map_fast_is_universe(map))
504 return p;
506 p = isl_printer_print_str(p, " : ");
507 if (map->n == 0)
508 p = isl_printer_print_str(p, "1 = 0");
509 for (i = 0; i < map->n; ++i) {
510 if (i)
511 p = isl_printer_print_str(p, " or ");
512 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
513 p = isl_printer_print_str(p, "(");
514 p = print_disjunct(map->p[i], map->dim, p, set);
515 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
516 p = isl_printer_print_str(p, ")");
518 return p;
521 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
522 __isl_take isl_printer *p)
524 if (isl_map_dim(map, isl_dim_param) > 0) {
525 p = print_tuple(map->dim, p, isl_dim_param, 0);
526 p = isl_printer_print_str(p, " -> ");
528 p = isl_printer_print_str(p, "{ ");
529 p = print_tuple(map->dim, p, isl_dim_in, 0);
530 p = isl_printer_print_str(p, " -> ");
531 p = print_tuple(map->dim, p, isl_dim_out, 0);
532 p = print_disjuncts(map, p, 0);
533 p = isl_printer_print_str(p, " }");
534 return p;
537 static __isl_give isl_printer *isl_set_print_isl(__isl_keep isl_set *set,
538 __isl_take isl_printer *p)
540 int i;
542 if (isl_set_dim(set, isl_dim_param) > 0) {
543 p = print_tuple(set->dim, p, isl_dim_param, 0);
544 p = isl_printer_print_str(p, " -> ");
546 p = isl_printer_print_str(p, "{ ");
547 p = print_tuple(set->dim, p, isl_dim_set, 1);
548 p = print_disjuncts((isl_map *)set, p, 1);
549 p = isl_printer_print_str(p, " }");
550 return p;
553 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
554 __isl_keep isl_basic_map *bmap)
556 if (!p || !bmap)
557 goto error;
558 if (p->output_format == ISL_FORMAT_ISL)
559 return isl_basic_map_print_isl(bmap, p);
560 else if (p->output_format == ISL_FORMAT_OMEGA)
561 return isl_basic_map_print_omega(bmap, p);
562 isl_assert(bmap->ctx, 0, goto error);
563 error:
564 isl_printer_free(p);
565 return NULL;
568 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
569 const char *prefix, const char *suffix, unsigned output_format)
571 isl_printer *printer;
573 if (!bmap)
574 return;
576 printer = isl_printer_to_file(bmap->ctx, out);
577 printer = isl_printer_set_indent(printer, indent);
578 printer = isl_printer_set_prefix(printer, prefix);
579 printer = isl_printer_set_suffix(printer, suffix);
580 printer = isl_printer_set_output_format(printer, output_format);
581 isl_printer_print_basic_map(printer, bmap);
583 isl_printer_free(printer);
586 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
587 __isl_keep isl_basic_set *bset)
589 if (!p || !bset)
590 goto error;
592 if (p->output_format == ISL_FORMAT_ISL)
593 return isl_basic_set_print_isl(bset, p);
594 else if (p->output_format == ISL_FORMAT_POLYLIB)
595 return isl_basic_set_print_polylib(bset, p);
596 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
597 return bset_print_constraints_polylib(bset, p);
598 else if (p->output_format == ISL_FORMAT_OMEGA)
599 return isl_basic_set_print_omega(bset, p);
600 isl_assert(p->ctx, 0, goto error);
601 error:
602 isl_printer_free(p);
603 return NULL;
606 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
607 const char *prefix, const char *suffix, unsigned output_format)
609 isl_printer *printer;
611 if (!bset)
612 return;
614 printer = isl_printer_to_file(bset->ctx, out);
615 printer = isl_printer_set_indent(printer, indent);
616 printer = isl_printer_set_prefix(printer, prefix);
617 printer = isl_printer_set_suffix(printer, suffix);
618 printer = isl_printer_set_output_format(printer, output_format);
619 isl_printer_print_basic_set(printer, bset);
621 isl_printer_free(printer);
624 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
625 __isl_keep isl_set *set)
627 if (!p || !set)
628 goto error;
629 if (p->output_format == ISL_FORMAT_ISL)
630 return isl_set_print_isl(set, p);
631 else if (p->output_format == ISL_FORMAT_POLYLIB)
632 return isl_set_print_polylib(set, p);
633 else if (p->output_format == ISL_FORMAT_OMEGA)
634 return isl_set_print_omega(set, p);
635 isl_assert(set->ctx, 0, goto error);
636 error:
637 isl_printer_free(p);
638 return NULL;
641 void isl_set_print(struct isl_set *set, FILE *out, int indent,
642 unsigned output_format)
644 isl_printer *printer;
646 if (!set)
647 return;
649 printer = isl_printer_to_file(set->ctx, out);
650 printer = isl_printer_set_indent(printer, indent);
651 printer = isl_printer_set_output_format(printer, output_format);
652 printer = isl_printer_print_set(printer, set);
654 isl_printer_free(printer);
657 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
658 __isl_keep isl_map *map)
660 if (!p || !map)
661 goto error;
663 if (p->output_format == ISL_FORMAT_ISL)
664 return isl_map_print_isl(map, p);
665 else if (p->output_format == ISL_FORMAT_POLYLIB)
666 return isl_map_print_polylib(map, p);
667 else if (p->output_format == ISL_FORMAT_OMEGA)
668 return isl_map_print_omega(map, p);
669 isl_assert(map->ctx, 0, goto error);
670 error:
671 isl_printer_free(p);
672 return NULL;
675 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
676 unsigned output_format)
678 isl_printer *printer;
680 if (!map)
681 return;
683 printer = isl_printer_to_file(map->ctx, out);
684 printer = isl_printer_set_indent(printer, indent);
685 printer = isl_printer_set_output_format(printer, output_format);
686 printer = isl_printer_print_map(printer, map);
688 isl_printer_free(printer);
691 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
693 int i;
694 int n;
696 for (i = 0, n = 0; i < rec->n; ++i)
697 if (!isl_upoly_is_zero(rec->p[i]))
698 ++n;
700 return n;
703 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
704 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
706 p = isl_printer_print_str(p, "[(");
707 p = print_affine_of_len(dim, p, div->row[pos] + 1, div->n_col - 1, 1);
708 p = isl_printer_print_str(p, ")/");
709 p = isl_printer_print_isl_int(p, div->row[pos][0]);
710 p = isl_printer_print_str(p, "]");
711 return p;
714 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
715 __isl_take isl_printer *p, int first)
717 struct isl_upoly_cst *cst;
718 int neg;
720 cst = isl_upoly_as_cst(up);
721 if (!cst)
722 goto error;
723 neg = !first && isl_int_is_neg(cst->n);
724 if (!first)
725 p = isl_printer_print_str(p, neg ? " - " : " + ");
726 if (neg)
727 isl_int_neg(cst->n, cst->n);
728 if (isl_int_is_zero(cst->d)) {
729 int sgn = isl_int_sgn(cst->n);
730 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
731 sgn == 0 ? "NaN" : "infty");
732 } else
733 p = isl_printer_print_isl_int(p, cst->n);
734 if (neg)
735 isl_int_neg(cst->n, cst->n);
736 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
737 p = isl_printer_print_str(p, "/");
738 p = isl_printer_print_isl_int(p, cst->d);
740 return p;
741 error:
742 isl_printer_free(p);
743 return NULL;
746 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
747 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
748 __isl_take isl_printer *p)
750 unsigned total;
751 int i, n, first;
752 struct isl_upoly_rec *rec;
754 if (!p || !up || !dim || !div)
755 goto error;
757 if (isl_upoly_is_cst(up))
758 return upoly_print_cst(up, p, 1);
760 total = isl_dim_total(dim);
761 rec = isl_upoly_as_rec(up);
762 if (!rec)
763 goto error;
764 n = upoly_rec_n_non_zero(rec);
765 if (n > 1)
766 p = isl_printer_print_str(p, "(");
767 for (i = 0, first = 1; i < rec->n; ++i) {
768 if (isl_upoly_is_zero(rec->p[i]))
769 continue;
770 if (isl_upoly_is_negone(rec->p[i])) {
771 if (!i)
772 p = isl_printer_print_str(p, "-1");
773 else if (first)
774 p = isl_printer_print_str(p, "-");
775 else
776 p = isl_printer_print_str(p, " - ");
777 } else if (isl_upoly_is_cst(rec->p[i]) &&
778 !isl_upoly_is_one(rec->p[i]))
779 p = upoly_print_cst(rec->p[i], p, first);
780 else {
781 if (!first)
782 p = isl_printer_print_str(p, " + ");
783 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
784 p = upoly_print(rec->p[i], dim, div, p);
786 first = 0;
787 if (i == 0)
788 continue;
789 if (!isl_upoly_is_one(rec->p[i]) &&
790 !isl_upoly_is_negone(rec->p[i]))
791 p = isl_printer_print_str(p, " * ");
792 if (rec->up.var < total)
793 p = print_term(dim, up->ctx->one, 1 + rec->up.var, p, 1);
794 else
795 p = print_div(dim, div, rec->up.var - total, p);
796 if (i == 1)
797 continue;
798 p = isl_printer_print_str(p, "^");
799 p = isl_printer_print_int(p, i);
801 if (n > 1)
802 p = isl_printer_print_str(p, ")");
803 return p;
804 error:
805 isl_printer_free(p);
806 return NULL;
809 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
810 __isl_keep isl_qpolynomial *qp)
812 if (!p || !qp)
813 goto error;
814 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
815 return p;
816 error:
817 isl_printer_free(p);
818 return NULL;
821 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
822 unsigned output_format)
824 isl_printer *p;
826 if (!qp)
827 return;
829 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
830 p = isl_printer_to_file(qp->dim->ctx, out);
831 p = isl_printer_print_qpolynomial(p, qp);
832 isl_printer_free(p);
835 static __isl_give isl_printer *qpolynomial_fold_print(
836 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
838 int i;
840 if (fold->type == isl_fold_min)
841 p = isl_printer_print_str(p, "min");
842 else if (fold->type == isl_fold_max)
843 p = isl_printer_print_str(p, "max");
844 p = isl_printer_print_str(p, "(");
845 for (i = 0; i < fold->n; ++i) {
846 if (i)
847 p = isl_printer_print_str(p, ", ");
848 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
850 p = isl_printer_print_str(p, ")");
851 return p;
854 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
855 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
857 if (!p || !fold)
858 goto error;
859 p = qpolynomial_fold_print(fold, p);
860 return p;
861 error:
862 isl_printer_free(p);
863 return NULL;
866 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
867 FILE *out, unsigned output_format)
869 isl_printer *p;
871 if (!fold)
872 return;
874 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
876 p = isl_printer_to_file(fold->dim->ctx, out);
877 p = isl_printer_print_qpolynomial_fold(p, fold);
879 isl_printer_free(p);
882 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
883 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
885 int i = 0;
887 if (!p || !pwqp)
888 goto error;
890 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
891 p = print_tuple(pwqp->dim, p, isl_dim_param, 0);
892 p = isl_printer_print_str(p, " -> ");
894 p = isl_printer_print_str(p, "{ ");
895 if (pwqp->n == 0) {
896 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0) {
897 p = print_tuple(pwqp->dim, p, isl_dim_set, 1);
898 p = isl_printer_print_str(p, " -> ");
900 p = isl_printer_print_str(p, "0");
902 for (i = 0; i < pwqp->n; ++i) {
903 if (i)
904 p = isl_printer_print_str(p, "; ");
905 if (isl_dim_size(pwqp->p[i].set->dim, isl_dim_set) > 0) {
906 p = print_tuple(pwqp->p[i].set->dim, p, isl_dim_set, 1);
907 p = isl_printer_print_str(p, " -> ");
909 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
910 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1);
912 p = isl_printer_print_str(p, " }");
913 return p;
914 error:
915 isl_printer_free(p);
916 return NULL;
919 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
920 unsigned output_format)
922 isl_printer *p;
924 if (!pwqp)
925 return;
927 isl_assert(pwqp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
929 p = isl_printer_to_file(pwqp->dim->ctx, out);
930 p = isl_printer_print_pw_qpolynomial(p, pwqp);
932 isl_printer_free(p);
935 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
936 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
938 int i = 0;
940 if (!p || !pwf)
941 return;
943 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
944 p = print_tuple(pwf->dim, p, isl_dim_param, 0);
945 p = isl_printer_print_str(p, " -> ");
947 p = isl_printer_print_str(p, "{ ");
948 if (pwf->n == 0) {
949 if (isl_dim_size(pwf->dim, isl_dim_set) > 0) {
950 p = print_tuple(pwf->dim, p, isl_dim_set, 0);
951 p = isl_printer_print_str(p, " -> ");
953 p = isl_printer_print_str(p, "0");
955 for (i = 0; i < pwf->n; ++i) {
956 if (i)
957 p = isl_printer_print_str(p, "; ");
958 if (isl_dim_size(pwf->p[i].set->dim, isl_dim_set) > 0) {
959 p = print_tuple(pwf->p[i].set->dim, p, isl_dim_set, 0);
960 p = isl_printer_print_str(p, " -> ");
962 p = qpolynomial_fold_print(pwf->p[i].fold, p);
963 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1);
965 p = isl_printer_print_str(p, " }");
966 return p;
969 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
970 FILE *out, unsigned output_format)
972 isl_printer *p;
974 if (!pwf)
975 return;
977 isl_assert(pwf->dim->ctx, output_format == ISL_FORMAT_ISL, return);
979 p = isl_printer_to_file(pwf->dim->ctx, out);
980 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
982 isl_printer_free(p);