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
16 #include <isl_polynomial_private.h>
18 static void print_constraint_polylib(struct isl_basic_map *bmap,
20 FILE *out, int indent, const char *prefix, const char *suffix)
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 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
29 fprintf(out, "%d", ineq);
30 for (i = 0; i < n_out; ++i) {
32 isl_int_print(out, c[1+nparam+n_in+i], 5);
34 for (i = 0; i < n_in; ++i) {
36 isl_int_print(out, c[1+nparam+i], 5);
38 for (i = 0; i < bmap->n_div; ++i) {
40 isl_int_print(out, c[1+nparam+n_in+n_out+i], 5);
42 for (i = 0; i < nparam; ++i) {
44 isl_int_print(out, c[1+i], 5);
47 isl_int_print(out, c[0], 5);
48 fprintf(out, "%s\n", suffix ? suffix : "");
51 static void print_constraints_polylib(struct isl_basic_map *bmap,
52 FILE *out, int indent, const char *prefix, const char *suffix)
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);
64 static void bset_print_constraints_polylib(struct isl_basic_set *bset,
65 FILE *out, int indent, const char *prefix, const char *suffix)
67 print_constraints_polylib((struct isl_basic_map *)bset,
68 out, indent, prefix, suffix);
71 static void isl_basic_map_print_polylib(struct isl_basic_map *bmap, FILE *out,
72 int indent, const char *prefix, const char *suffix)
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);
81 static void isl_basic_set_print_polylib(struct isl_basic_set *bset, FILE *out,
82 int indent, const char *prefix, const char *suffix)
84 isl_basic_map_print_polylib((struct isl_basic_map *)bset, out,
85 indent, prefix, suffix);
88 static void isl_map_print_polylib(struct isl_map *map, FILE *out, int indent)
92 fprintf(out, "%*s", indent, "");
93 fprintf(out, "%d\n", map->n);
94 for (i = 0; i < map->n; ++i) {
96 isl_basic_map_print_polylib(map->p[i], out, indent, NULL, NULL);
100 static void isl_set_print_polylib(struct isl_set *set, FILE *out, int indent)
102 isl_map_print_polylib((struct isl_map *)set, out, indent);
105 static int count_same_name(__isl_keep isl_dim *dim,
106 enum isl_dim_type type, unsigned pos, const char *name)
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))
123 static void print_name(struct isl_dim *dim, FILE *out,
124 enum isl_dim_type type, unsigned pos, int set)
130 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
134 if (type == isl_dim_param)
136 else if (type == isl_dim_div)
138 else if (set || type == isl_dim_in)
142 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
145 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
147 fprintf(out, "%s", name);
152 static void print_var_list(struct isl_dim *dim, FILE *out,
153 enum isl_dim_type type, int set)
157 for (i = 0; i < isl_dim_size(dim, type); ++i) {
160 print_name(dim, out, type, i, set);
164 static void print_tuple(__isl_keep isl_dim *dim, FILE *out,
165 enum isl_dim_type type, int set)
168 print_var_list(dim, out, type, set);
172 static void print_omega_parameters(struct isl_dim *dim, FILE *out,
173 int indent, const char *prefix, const char *suffix)
175 if (isl_dim_size(dim, isl_dim_param) == 0)
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 : "");
183 static void print_term(__isl_keep isl_dim *dim,
184 isl_int c, int pos, FILE *out, int set)
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);
192 isl_int_print(out, c, 0);
196 if (isl_int_is_one(c))
198 else if (isl_int_is_negone(c))
201 isl_int_print(out, c, 0);
202 if (pos < 1 + nparam) {
203 type = isl_dim_param;
205 } else if (pos < 1 + nparam + n_in) {
208 } else if (pos < 1 + nparam + n_in + n_out) {
210 pos -= 1 + nparam + n_in;
213 pos -= 1 + nparam + n_in + n_out;
215 print_name(dim, out, type, pos, set);
218 static void print_affine_of_len(__isl_keep isl_dim *dim, FILE *out,
219 isl_int *c, int len, int set)
224 for (i = 0, first = 1; i < len; ++i) {
226 if (isl_int_is_zero(c[i]))
229 if (isl_int_is_neg(c[i])) {
231 isl_int_neg(c[i], c[i]);
237 print_term(dim, c[i], i, out, set);
239 isl_int_neg(c[i], c[i]);
245 static void print_affine(__isl_keep isl_basic_map *bmap,
246 __isl_keep isl_dim *dim, FILE *out, isl_int *c, int set)
248 unsigned len = 1 + isl_basic_map_total_dim(bmap);
249 print_affine_of_len(dim, out, c, len, set);
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)
256 if (!first_constraint)
257 fprintf(out, " and ");
259 isl_int_abs(c[last], c[last]);
261 print_term(dim, c[last], last, out, set);
263 fprintf(out, " %s ", op);
265 isl_int_set_si(c[last], 0);
266 print_affine(bmap, dim, out, c, set);
269 static void print_constraints(__isl_keep isl_basic_map *bmap,
270 __isl_keep isl_dim *dim, FILE *out, int set)
274 unsigned total = isl_basic_map_total_dim(bmap);
276 c = isl_vec_alloc(bmap->ctx, 1 + total);
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);
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);
290 for (i = 0; i < bmap->n_ineq; ++i) {
291 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
293 isl_assert(bmap->ctx, l >= 0, return);
294 s = isl_int_sgn(bmap->ineq[i][l]);
296 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
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);
306 static void print_omega_constraints(__isl_keep isl_basic_map *bmap, FILE *out,
309 if (bmap->n_eq + bmap->n_ineq == 0)
313 if (bmap->n_div > 0) {
315 fprintf(out, "exists (");
316 for (i = 0; i < bmap->n_div; ++i) {
319 print_name(bmap->dim, out, isl_dim_div, i, 0);
323 print_constraints(bmap, bmap->dim, out, set);
328 static void basic_map_print_omega(struct isl_basic_map *bmap, FILE *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);
335 print_omega_constraints(bmap, out, 0);
339 static void isl_basic_map_print_omega(struct isl_basic_map *bmap, FILE *out,
340 int indent, const char *prefix, const char *suffix)
342 print_omega_parameters(bmap->dim, out, indent, prefix, suffix);
344 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
345 basic_map_print_omega(bmap, out);
346 fprintf(out, "%s\n", suffix ? suffix : "");
349 static void basic_set_print_omega(struct isl_basic_set *bset, FILE *out)
352 print_var_list(bset->dim, out, isl_dim_set, 1);
354 print_omega_constraints((isl_basic_map *)bset, out, 1);
358 static void isl_basic_set_print_omega(struct isl_basic_set *bset, FILE *out,
359 int indent, const char *prefix, const char *suffix)
361 print_omega_parameters(bset->dim, out, indent, prefix, suffix);
363 fprintf(out, "%*s%s", indent, "", prefix ? prefix : "");
364 basic_set_print_omega(bset, out);
365 fprintf(out, "%s\n", suffix ? suffix : "");
368 static void isl_map_print_omega(struct isl_map *map, FILE *out, int indent)
372 print_omega_parameters(map->dim, out, indent, "", "");
374 fprintf(out, "%*s", indent, "");
375 for (i = 0; i < map->n; ++i) {
377 fprintf(out, " union ");
378 basic_map_print_omega(map->p[i], out);
383 static void isl_set_print_omega(struct isl_set *set, FILE *out, int indent)
387 print_omega_parameters(set->dim, out, indent, "", "");
389 fprintf(out, "%*s", indent, "");
390 for (i = 0; i < set->n; ++i) {
392 fprintf(out, " union ");
393 basic_set_print_omega(set->p[i], out);
398 static void print_disjunct(__isl_keep isl_basic_map *bmap,
399 __isl_keep isl_dim *dim, FILE *out, int set)
401 if (bmap->n_div > 0) {
403 fprintf(out, "exists (");
404 for (i = 0; i < bmap->n_div; ++i) {
407 print_name(dim, out, isl_dim_div, i, 0);
408 if (isl_int_is_zero(bmap->div[i][0]))
410 fprintf(out, " = [(");
411 print_affine(bmap, dim, out, bmap->div[i] + 1, set);
413 isl_int_print(out, bmap->div[i][0], 0);
419 print_constraints(bmap, dim, out, set);
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)
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, " -> ");
436 print_tuple(bmap->dim, out, isl_dim_in, 0);
437 fprintf(out, " -> ");
438 print_tuple(bmap->dim, out, isl_dim_out, 0);
440 print_disjunct(bmap, bmap->dim, out, 0);
441 fprintf(out, " }%s\n", suffix ? suffix : "");
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)
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, " -> ");
455 print_tuple(bset->dim, out, isl_dim_set, 1);
457 print_disjunct((isl_basic_map *)bset, bset->dim, out, 1);
458 fprintf(out, " }%s\n", suffix ? suffix : "");
461 static void print_disjuncts(__isl_keep isl_map *map, FILE *out, int set)
465 if (isl_map_fast_is_universe(map))
470 fprintf(out, "1 = 0");
471 for (i = 0; i < map->n; ++i) {
473 fprintf(out, " or ");
474 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
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)
482 static void isl_map_print_isl(__isl_keep isl_map *map, FILE *out, int indent)
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, " -> ");
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");
497 static void isl_set_print_isl(__isl_keep isl_set *set, FILE *out, int indent)
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, " -> ");
507 print_tuple(set->dim, out, isl_dim_set, 1);
508 print_disjuncts((isl_map *)set, out, 1);
509 fprintf(out, " }\n");
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)
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);
522 isl_assert(bmap->ctx, 0, return);
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)
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);
539 isl_assert(bset->ctx, 0, return);
542 void isl_set_print(struct isl_set *set, FILE *out, int indent,
543 unsigned output_format)
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);
554 isl_assert(set->ctx, 0, return);
557 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
558 unsigned output_format)
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);
569 isl_assert(map->ctx, 0, return);
572 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
577 for (i = 0, n = 0; i < rec->n; ++i)
578 if (!isl_upoly_is_zero(rec->p[i]))
584 static void print_div(__isl_keep isl_dim *dim, __isl_keep isl_mat *div,
588 print_affine_of_len(dim, out, div->row[pos] + 1, div->n_col - 1, 1);
590 isl_int_print(out, div->row[pos][0], 0);
594 static void upoly_print_cst(__isl_keep struct isl_upoly *up, FILE *out, int first)
596 struct isl_upoly_cst *cst;
599 cst = isl_upoly_as_cst(up);
602 neg = !first && isl_int_is_neg(cst->n);
604 fprintf(out, 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");
611 isl_int_print(out, cst->n, 0);
613 isl_int_neg(cst->n, cst->n);
614 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
616 isl_int_print(out, cst->d, 0);
620 static void upoly_print(__isl_keep struct isl_upoly *up,
621 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, FILE *out)
625 struct isl_upoly_rec *rec;
627 if (!up || !dim || !div)
630 if (isl_upoly_is_cst(up)) {
631 upoly_print_cst(up, out, 1);
635 total = isl_dim_total(dim);
636 rec = isl_upoly_as_rec(up);
639 n = upoly_rec_n_non_zero(rec);
642 for (i = 0, first = 1; i < rec->n; ++i) {
643 if (isl_upoly_is_zero(rec->p[i]))
645 if (isl_upoly_is_negone(rec->p[i])) {
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);
658 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
659 upoly_print(rec->p[i], dim, div, out);
664 if (!isl_upoly_is_one(rec->p[i]) &&
665 !isl_upoly_is_negone(rec->p[i]))
667 if (rec->up.var < total)
668 print_term(dim, up->ctx->one, 1 + rec->up.var, out, 1);
670 print_div(dim, div, rec->up.var - total, out);
673 fprintf(out, "^%d", i);
679 static void qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out)
683 upoly_print(qp->upoly, qp->dim, qp->div, out);
686 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
687 unsigned output_format)
691 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
692 qpolynomial_print(qp, out);
696 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
697 unsigned output_format)
703 isl_assert(pwqp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
704 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
705 print_tuple(pwqp->dim, out, isl_dim_param, 0);
706 fprintf(out, " -> ");
710 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0) {
711 print_tuple(pwqp->dim, out, isl_dim_set, 0);
712 fprintf(out, " -> ");
716 for (i = 0; i < pwqp->n; ++i) {
719 if (isl_dim_size(pwqp->p[i].set->dim, isl_dim_set) > 0) {
720 print_tuple(pwqp->p[i].set->dim, out, isl_dim_set, 0);
721 fprintf(out, " -> ");
723 qpolynomial_print(pwqp->p[i].qp, out);
724 print_disjuncts((isl_map *)pwqp->p[i].set, out, 1);
726 fprintf(out, " }\n");