isl_output.c: extract out common print_dim
[isl.git] / isl_output.c
blob1880b290b109b2c0d960252c46e1fc46b1a2c367
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 <stdlib.h>
14 #include <string.h>
15 #include <isl_set.h>
16 #include <isl_seq.h>
17 #include <isl_polynomial_private.h>
18 #include <isl_printer_private.h>
19 #include <isl_dim_private.h>
20 #include <isl_map_private.h>
21 #include <isl_union_map.h>
23 static const char *s_to[2] = { " -> ", " \\to " };
24 static const char *s_and[2] = { " and ", " \\wedge " };
25 static const char *s_or[2] = { " or ", " \\vee " };
26 static const char *s_le[2] = { "<=", "\\le" };
27 static const char *s_ge[2] = { ">=", "\\ge" };
28 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
29 static const char *s_close_set[2] = { " }", " \\,\\}" };
30 static const char *s_open_list[2] = { "[", "(" };
31 static const char *s_close_list[2] = { "]", ")" };
32 static const char *s_such_that[2] = { " : ", " \\mid " };
33 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
34 static const char *s_close_exists[2] = { ")", "" };
35 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
36 static const char *s_param_prefix[2] = { "p", "p_" };
37 static const char *s_input_prefix[2] = { "i", "i_" };
38 static const char *s_output_prefix[2] = { "o", "o_" };
40 static __isl_give isl_printer *print_constraint_polylib(
41 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
43 int i;
44 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
45 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
46 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
47 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
49 p = isl_printer_start_line(p);
50 p = isl_printer_print_int(p, ineq);
51 for (i = 0; i < n_out; ++i) {
52 p = isl_printer_print_str(p, " ");
53 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
55 for (i = 0; i < n_in; ++i) {
56 p = isl_printer_print_str(p, " ");
57 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
59 for (i = 0; i < bmap->n_div; ++i) {
60 p = isl_printer_print_str(p, " ");
61 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
63 for (i = 0; i < nparam; ++i) {
64 p = isl_printer_print_str(p, " ");
65 p = isl_printer_print_isl_int(p, c[1+i]);
67 p = isl_printer_print_str(p, " ");
68 p = isl_printer_print_isl_int(p, c[0]);
69 p = isl_printer_end_line(p);
70 return p;
73 static __isl_give isl_printer *print_constraints_polylib(
74 struct isl_basic_map *bmap, __isl_take isl_printer *p)
76 int i;
78 p = isl_printer_set_isl_int_width(p, 5);
80 for (i = 0; i < bmap->n_eq; ++i)
81 p = print_constraint_polylib(bmap, 0, i, p);
82 for (i = 0; i < bmap->n_ineq; ++i)
83 p = print_constraint_polylib(bmap, 1, i, p);
85 return p;
88 static __isl_give isl_printer *bset_print_constraints_polylib(
89 struct isl_basic_set *bset, __isl_take isl_printer *p)
91 return print_constraints_polylib((struct isl_basic_map *)bset, p);
94 static __isl_give isl_printer *isl_basic_map_print_polylib(
95 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
97 unsigned total = isl_basic_map_total_dim(bmap);
98 p = isl_printer_start_line(p);
99 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
100 p = isl_printer_print_str(p, " ");
101 p = isl_printer_print_int(p, 1 + total + 1);
102 p = isl_printer_end_line(p);
103 return print_constraints_polylib(bmap, p);
106 static __isl_give isl_printer *isl_basic_set_print_polylib(
107 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
109 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p);
112 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
113 __isl_take isl_printer *p)
115 int i;
117 p = isl_printer_start_line(p);
118 p = isl_printer_print_int(p, map->n);
119 p = isl_printer_end_line(p);
120 for (i = 0; i < map->n; ++i) {
121 p = isl_printer_start_line(p);
122 p = isl_printer_end_line(p);
123 p = isl_basic_map_print_polylib(map->p[i], p);
125 return p;
128 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
129 __isl_take isl_printer *p)
131 return isl_map_print_polylib((struct isl_map *)set, p);
134 static int count_same_name(__isl_keep isl_dim *dim,
135 enum isl_dim_type type, unsigned pos, const char *name)
137 enum isl_dim_type t;
138 unsigned p, s;
139 int count = 0;
141 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
142 s = t == type ? pos : isl_dim_size(dim, t);
143 for (p = 0; p < s; ++p) {
144 const char *n = isl_dim_get_name(dim, t, p);
145 if (n && !strcmp(n, name))
146 count++;
149 return count;
152 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
153 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
154 int set, int latex)
156 const char *name;
157 char buffer[20];
158 int primes;
160 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
162 if (!name) {
163 const char *prefix;
164 if (type == isl_dim_param)
165 prefix = s_param_prefix[latex];
166 else if (type == isl_dim_div)
167 prefix = s_div_prefix[latex];
168 else if (set || type == isl_dim_in)
169 prefix = s_input_prefix[latex];
170 else
171 prefix = s_output_prefix[latex];
172 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
173 name = buffer;
175 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
176 pos, name);
177 p = isl_printer_print_str(p, name);
178 while (primes-- > 0)
179 p = isl_printer_print_str(p, "'");
180 return p;
183 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
185 enum isl_dim_type type;
186 unsigned n_in = isl_dim_size(dim, isl_dim_in);
187 unsigned n_out = isl_dim_size(dim, isl_dim_out);
188 unsigned nparam = isl_dim_size(dim, isl_dim_param);
190 if (*pos < 1 + nparam) {
191 type = isl_dim_param;
192 *pos -= 1;
193 } else if (*pos < 1 + nparam + n_in) {
194 type = isl_dim_in;
195 *pos -= 1 + nparam;
196 } else if (*pos < 1 + nparam + n_in + n_out) {
197 type = isl_dim_out;
198 *pos -= 1 + nparam + n_in;
199 } else {
200 type = isl_dim_div;
201 *pos -= 1 + nparam + n_in + n_out;
204 return type;
207 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
208 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
210 enum isl_dim_type type;
212 if (pos == 0)
213 return isl_printer_print_isl_int(p, c);
215 if (isl_int_is_one(c))
217 else if (isl_int_is_negone(c))
218 p = isl_printer_print_str(p, "-");
219 else
220 p = isl_printer_print_isl_int(p, c);
221 type = pos2type(dim, &pos);
222 p = print_name(dim, p, type, pos, set, latex);
223 return p;
226 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
227 __isl_take isl_printer *p, isl_int *c, int len, int set)
229 int i;
230 int first;
232 for (i = 0, first = 1; i < len; ++i) {
233 int flip = 0;
234 if (isl_int_is_zero(c[i]))
235 continue;
236 if (!first) {
237 if (isl_int_is_neg(c[i])) {
238 flip = 1;
239 isl_int_neg(c[i], c[i]);
240 p = isl_printer_print_str(p, " - ");
241 } else
242 p = isl_printer_print_str(p, " + ");
244 first = 0;
245 p = print_term(dim, c[i], i, p, set, 0);
246 if (flip)
247 isl_int_neg(c[i], c[i]);
249 if (first)
250 p = isl_printer_print_str(p, "0");
251 return p;
254 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
255 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
257 unsigned len = 1 + isl_basic_map_total_dim(bmap);
258 return print_affine_of_len(dim, p, c, len, set);
261 static int defining_equality(__isl_keep isl_basic_map *eq,
262 enum isl_dim_type type, int pos)
264 int i;
265 unsigned total;
267 if (!eq)
268 return -1;
270 pos += isl_dim_offset(eq->dim, type);
271 total = isl_basic_map_total_dim(eq);
273 for (i = 0; i < eq->n_eq; ++i) {
274 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
275 continue;
276 if (isl_int_is_one(eq->eq[i][1 + pos]))
277 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
278 return i;
281 return -1;
284 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
285 __isl_take isl_printer *p, enum isl_dim_type type, int set, int latex,
286 __isl_keep isl_basic_map *eq)
288 int i, j;
290 for (i = 0; i < isl_dim_size(dim, type); ++i) {
291 if (i)
292 p = isl_printer_print_str(p, ", ");
293 j = defining_equality(eq, type, i);
294 if (j >= 0) {
295 int pos = 1 + isl_dim_offset(dim, type) + i;
296 p = print_affine_of_len(dim, p, eq->eq[j], pos, set);
297 } else
298 p = print_name(dim, p, type, i, set, latex);
300 return p;
303 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
304 __isl_take isl_printer *p, enum isl_dim_type type, int set, int latex,
305 __isl_keep isl_basic_map *eq)
307 const char *name = NULL;
308 unsigned n = isl_dim_size(dim, type);
309 if ((type == isl_dim_in || type == isl_dim_out)) {
310 name = isl_dim_get_tuple_name(dim, type);
311 if (name) {
312 if (latex)
313 p = isl_printer_print_str(p, "\\mathrm{");
314 p = isl_printer_print_str(p, name);
315 if (latex)
316 p = isl_printer_print_str(p, "}");
319 if (!latex || n != 1 || name)
320 p = isl_printer_print_str(p, s_open_list[latex]);
321 p = print_var_list(dim, p, type, set, latex, eq);
322 if (!latex || n != 1 || name)
323 p = isl_printer_print_str(p, s_close_list[latex]);
324 return p;
327 static __isl_give isl_printer *print_dim(__isl_keep isl_dim *dim,
328 __isl_take isl_printer *p, int set, int latex,
329 __isl_keep isl_basic_map *eq)
331 if (set) {
332 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
333 } else {
334 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
335 p = isl_printer_print_str(p, s_to[0]);
336 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
339 return p;
342 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
343 __isl_take isl_printer *p)
345 if (isl_dim_size(dim, isl_dim_param) == 0)
346 return p;
348 p = isl_printer_start_line(p);
349 p = isl_printer_print_str(p, "symbolic ");
350 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
351 p = isl_printer_print_str(p, ";");
352 p = isl_printer_end_line(p);
353 return p;
356 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
357 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
358 isl_int *c, int last, const char *op, int first_constraint, int set,
359 int latex)
361 if (!first_constraint)
362 p = isl_printer_print_str(p, s_and[latex]);
364 isl_int_abs(c[last], c[last]);
366 p = print_term(dim, c[last], last, p, set, latex);
368 p = isl_printer_print_str(p, " ");
369 p = isl_printer_print_str(p, op);
370 p = isl_printer_print_str(p, " ");
372 isl_int_set_si(c[last], 0);
373 p = print_affine(bmap, dim, p, c, set);
375 return p;
378 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
379 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
381 int i;
382 struct isl_vec *c;
383 unsigned total = isl_basic_map_total_dim(bmap);
385 c = isl_vec_alloc(bmap->ctx, 1 + total);
386 if (!c)
387 goto error;
389 for (i = bmap->n_eq - 1; i >= 0; --i) {
390 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
391 isl_assert(bmap->ctx, l >= 0, goto error);
392 if (isl_int_is_neg(bmap->eq[i][l]))
393 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
394 else
395 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
396 p = print_constraint(bmap, dim, p, c->el, l,
397 "=", i == bmap->n_eq - 1, set, latex);
399 for (i = 0; i < bmap->n_ineq; ++i) {
400 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
401 int s;
402 const char *op;
403 if (l < 0)
404 continue;
405 s = isl_int_sgn(bmap->ineq[i][l]);
406 if (s < 0)
407 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
408 else
409 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
410 op = s < 0 ? s_le[latex] : s_ge[latex];
411 p = print_constraint(bmap, dim, p, c->el, l,
412 op, !bmap->n_eq && !i, set, latex);
415 isl_vec_free(c);
417 return p;
418 error:
419 isl_printer_free(p);
420 return NULL;
423 static __isl_give isl_printer *print_omega_constraints(
424 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
426 if (bmap->n_eq + bmap->n_ineq == 0)
427 return p;
429 p = isl_printer_print_str(p, ": ");
430 if (bmap->n_div > 0) {
431 int i;
432 p = isl_printer_print_str(p, "exists (");
433 for (i = 0; i < bmap->n_div; ++i) {
434 if (i)
435 p = isl_printer_print_str(p, ", ");
436 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
438 p = isl_printer_print_str(p, ": ");
440 p = print_constraints(bmap, bmap->dim, p, set, 0);
441 if (bmap->n_div > 0)
442 p = isl_printer_print_str(p, ")");
443 return p;
446 static __isl_give isl_printer *basic_map_print_omega(
447 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
449 p = isl_printer_print_str(p, "{ [");
450 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
451 p = isl_printer_print_str(p, "] -> [");
452 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
453 p = isl_printer_print_str(p, "] ");
454 p = print_omega_constraints(bmap, p, 0);
455 p = isl_printer_print_str(p, " }");
456 return p;
459 static __isl_give isl_printer *isl_basic_map_print_omega(
460 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
462 p = print_omega_parameters(bmap->dim, p);
464 p = isl_printer_start_line(p);
465 p = basic_map_print_omega(bmap, p);
466 p = isl_printer_end_line(p);
467 return p;
470 static __isl_give isl_printer *basic_set_print_omega(
471 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
473 p = isl_printer_print_str(p, "{ [");
474 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
475 p = isl_printer_print_str(p, "] ");
476 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
477 p = isl_printer_print_str(p, " }");
478 return p;
481 static __isl_give isl_printer *isl_basic_set_print_omega(
482 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
484 p = print_omega_parameters(bset->dim, p);
486 p = isl_printer_start_line(p);
487 p = basic_set_print_omega(bset, p);
488 p = isl_printer_end_line(p);
489 return p;
492 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
493 __isl_take isl_printer *p)
495 int i;
497 p = print_omega_parameters(map->dim, p);
499 p = isl_printer_start_line(p);
500 for (i = 0; i < map->n; ++i) {
501 if (i)
502 p = isl_printer_print_str(p, " union ");
503 p = basic_map_print_omega(map->p[i], p);
505 p = isl_printer_end_line(p);
506 return p;
509 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
510 __isl_take isl_printer *p)
512 int i;
514 p = print_omega_parameters(set->dim, p);
516 p = isl_printer_start_line(p);
517 for (i = 0; i < set->n; ++i) {
518 if (i)
519 p = isl_printer_print_str(p, " union ");
520 p = basic_set_print_omega(set->p[i], p);
522 p = isl_printer_end_line(p);
523 return p;
526 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
527 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
529 if (bmap->n_div > 0) {
530 int i;
531 p = isl_printer_print_str(p, s_open_exists[latex]);
532 for (i = 0; i < bmap->n_div; ++i) {
533 if (i)
534 p = isl_printer_print_str(p, ", ");
535 p = print_name(dim, p, isl_dim_div, i, 0, latex);
536 if (latex || isl_int_is_zero(bmap->div[i][0]))
537 continue;
538 p = isl_printer_print_str(p, " = [(");
539 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
540 p = isl_printer_print_str(p, ")/");
541 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
542 p = isl_printer_print_str(p, "]");
544 p = isl_printer_print_str(p, ": ");
547 p = print_constraints(bmap, dim, p, set, latex);
549 if (bmap->n_div > 0)
550 p = isl_printer_print_str(p, s_close_exists[latex]);
551 return p;
554 static __isl_give isl_printer *isl_basic_map_print_isl(
555 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int latex)
557 int i;
559 p = isl_printer_start_line(p);
560 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
561 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
562 p = isl_printer_print_str(p, " -> ");
564 p = isl_printer_print_str(p, "{ ");
565 p = print_dim(bmap->dim, p, 0, latex, NULL);
566 p = isl_printer_print_str(p, " : ");
567 p = print_disjunct(bmap, bmap->dim, p, 0, latex);
568 p = isl_printer_print_str(p, " }");
569 p = isl_printer_end_line(p);
570 return p;
573 static __isl_give isl_printer *isl_basic_set_print_isl(
574 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int latex)
576 int i;
578 p = isl_printer_start_line(p);
579 if (isl_basic_set_dim(bset, isl_dim_param) > 0) {
580 p = print_tuple(bset->dim, p, isl_dim_param, 0, latex, NULL);
581 p = isl_printer_print_str(p, " -> ");
583 p = isl_printer_print_str(p, "{ ");
584 p = print_dim(bset->dim, p, 1, latex, NULL);
585 p = isl_printer_print_str(p, " : ");
586 p = print_disjunct((isl_basic_map *)bset, bset->dim, p, 1, latex);
587 p = isl_printer_print_str(p, " }");
588 p = isl_printer_end_line(p);
589 return p;
592 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
593 __isl_take isl_printer *p, int set, int latex)
595 int i;
597 if (isl_map_fast_is_universe(map))
598 return p;
600 p = isl_printer_print_str(p, s_such_that[latex]);
601 if (map->n == 0)
602 p = isl_printer_print_str(p, "1 = 0");
603 for (i = 0; i < map->n; ++i) {
604 if (i)
605 p = isl_printer_print_str(p, s_or[latex]);
606 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
607 p = isl_printer_print_str(p, "(");
608 p = print_disjunct(map->p[i], map->dim, p, set, latex);
609 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
610 p = isl_printer_print_str(p, ")");
612 return p;
615 struct isl_aff_split {
616 isl_basic_map *aff;
617 isl_map *map;
620 static void free_split(__isl_take struct isl_aff_split *split, int n)
622 int i;
624 if (!split)
625 return;
627 for (i = 0; i < n; ++i) {
628 isl_basic_map_free(split[i].aff);
629 isl_map_free(split[i].map);
632 free(split);
635 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
637 int i, j;
638 unsigned nparam, n_in, n_out, total;
640 bmap = isl_basic_map_cow(bmap);
641 if (!bmap)
642 return NULL;
643 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
644 goto error;
646 nparam = isl_basic_map_dim(bmap, isl_dim_param);
647 n_in = isl_basic_map_dim(bmap, isl_dim_in);
648 n_out = isl_basic_map_dim(bmap, isl_dim_out);
649 total = isl_basic_map_dim(bmap, isl_dim_all);
650 for (i = bmap->n_eq - 1; i >= 0; --i) {
651 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
652 if (j >= nparam && j < nparam + n_in + n_out &&
653 (isl_int_is_one(bmap->eq[i][1 + j]) ||
654 isl_int_is_negone(bmap->eq[i][1 + j])))
655 continue;
656 if (isl_basic_map_drop_equality(bmap, i) < 0)
657 goto error;
660 bmap = isl_basic_map_finalize(bmap);
662 return bmap;
663 error:
664 isl_basic_map_free(bmap);
665 return NULL;
668 static int aff_split_cmp(const void *p1, const void *p2)
670 const struct isl_aff_split *s1, *s2;
671 s1 = (const struct isl_aff_split *) p1;
672 s2 = (const struct isl_aff_split *) p2;
674 return isl_basic_map_fast_cmp(s1->aff, s2->aff);
677 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
678 __isl_keep isl_basic_map *aff)
680 int i, j;
681 unsigned total;
683 if (!bmap || !aff)
684 goto error;
686 total = isl_dim_total(bmap->dim);
688 for (i = bmap->n_eq - 1; i >= 0; --i) {
689 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
690 bmap->n_div) != -1)
691 continue;
692 for (j = 0; j < aff->n_eq; ++j) {
693 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
694 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
695 continue;
696 if (isl_basic_map_drop_equality(bmap, i) < 0)
697 goto error;
698 break;
702 return bmap;
703 error:
704 isl_basic_map_free(bmap);
705 return NULL;
708 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
710 int i, n;
711 struct isl_aff_split *split;
713 split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
714 if (!split)
715 return NULL;
717 for (i = 0; i < map->n; ++i) {
718 isl_basic_map *bmap;
719 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
720 bmap = isl_basic_map_copy(map->p[i]);
721 bmap = isl_basic_map_cow(bmap);
722 bmap = drop_aff(bmap, split[i].aff);
723 split[i].map = isl_map_from_basic_map(bmap);
724 if (!split[i].aff || !split[i].map)
725 goto error;
728 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
730 n = map->n;
731 for (i = n - 1; i >= 1; --i) {
732 if (!isl_basic_map_fast_is_equal(split[i - 1].aff,
733 split[i].aff))
734 continue;
735 isl_basic_map_free(split[i].aff);
736 split[i - 1].map = isl_map_union(split[i - 1].map,
737 split[i].map);
738 if (i != n - 1)
739 split[i] = split[n - 1];
740 split[n - 1].aff = NULL;
741 split[n - 1].map = NULL;
742 --n;
745 return split;
746 error:
747 free_split(split, map->n);
748 return NULL;
751 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
752 struct isl_aff_split *split, int n, int set)
754 int i;
756 for (i = 0; i < n; ++i) {
757 isl_dim *dim;
759 if (!split[i].map)
760 break;
761 dim = split[i].map->dim;
762 if (i)
763 p = isl_printer_print_str(p, "; ");
764 p = print_dim(dim, p, set, 0, split[i].aff);
765 p = print_disjuncts(split[i].map, p, set, 0);
768 return p;
771 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
772 __isl_take isl_printer *p, int set)
774 struct isl_aff_split *split = NULL;
776 if (map->n > 0)
777 split = split_aff(map);
778 if (split) {
779 p = print_split_map(p, split, map->n, set);
780 } else {
781 p = print_dim(map->dim, p, set, 0, NULL);
782 p = print_disjuncts(map, p, set, 0);
784 free_split(split, map->n);
785 return p;
788 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
789 __isl_take isl_printer *p, int set)
791 if (isl_map_dim(map, isl_dim_param) > 0) {
792 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
793 p = isl_printer_print_str(p, s_to[0]);
795 p = isl_printer_print_str(p, s_open_set[0]);
796 p = isl_map_print_isl_body(map, p, set);
797 p = isl_printer_print_str(p, s_close_set[0]);
798 return p;
801 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
802 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
804 if (isl_map_dim(map, isl_dim_param) > 0) {
805 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
806 p = isl_printer_print_str(p, s_to[1]);
808 p = isl_printer_print_str(p, s_open_set[1]);
809 p = print_dim(map->dim, p, set, 1, aff);
810 p = print_disjuncts(map, p, set, 1);
811 p = isl_printer_print_str(p, s_close_set[1]);
813 return p;
816 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
817 __isl_take isl_printer *p, int set)
819 int i;
820 struct isl_aff_split *split = NULL;
822 if (map->n > 0)
823 split = split_aff(map);
825 if (!split)
826 return print_latex_map(map, p, NULL, set);
828 for (i = 0; i < map->n; ++i) {
829 if (!split[i].map)
830 break;
831 if (i)
832 p = isl_printer_print_str(p, " \\cup ");
833 p = print_latex_map(split[i].map, p, split[i].aff, set);
836 free_split(split, map->n);
837 return p;
840 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
841 __isl_keep isl_basic_map *bmap)
843 if (!p || !bmap)
844 goto error;
845 if (p->output_format == ISL_FORMAT_ISL)
846 return isl_basic_map_print_isl(bmap, p, 0);
847 else if (p->output_format == ISL_FORMAT_OMEGA)
848 return isl_basic_map_print_omega(bmap, p);
849 isl_assert(bmap->ctx, 0, goto error);
850 error:
851 isl_printer_free(p);
852 return NULL;
855 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
856 const char *prefix, const char *suffix, unsigned output_format)
858 isl_printer *printer;
860 if (!bmap)
861 return;
863 printer = isl_printer_to_file(bmap->ctx, out);
864 printer = isl_printer_set_indent(printer, indent);
865 printer = isl_printer_set_prefix(printer, prefix);
866 printer = isl_printer_set_suffix(printer, suffix);
867 printer = isl_printer_set_output_format(printer, output_format);
868 isl_printer_print_basic_map(printer, bmap);
870 isl_printer_free(printer);
873 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
874 __isl_keep isl_basic_set *bset)
876 if (!p || !bset)
877 goto error;
879 if (p->output_format == ISL_FORMAT_ISL)
880 return isl_basic_set_print_isl(bset, p, 0);
881 else if (p->output_format == ISL_FORMAT_POLYLIB)
882 return isl_basic_set_print_polylib(bset, p);
883 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
884 return bset_print_constraints_polylib(bset, p);
885 else if (p->output_format == ISL_FORMAT_OMEGA)
886 return isl_basic_set_print_omega(bset, p);
887 isl_assert(p->ctx, 0, goto error);
888 error:
889 isl_printer_free(p);
890 return NULL;
893 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
894 const char *prefix, const char *suffix, unsigned output_format)
896 isl_printer *printer;
898 if (!bset)
899 return;
901 printer = isl_printer_to_file(bset->ctx, out);
902 printer = isl_printer_set_indent(printer, indent);
903 printer = isl_printer_set_prefix(printer, prefix);
904 printer = isl_printer_set_suffix(printer, suffix);
905 printer = isl_printer_set_output_format(printer, output_format);
906 isl_printer_print_basic_set(printer, bset);
908 isl_printer_free(printer);
911 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
912 __isl_keep isl_set *set)
914 if (!p || !set)
915 goto error;
916 if (p->output_format == ISL_FORMAT_ISL)
917 return isl_map_print_isl((isl_map *)set, p, 1);
918 else if (p->output_format == ISL_FORMAT_POLYLIB)
919 return isl_set_print_polylib(set, p);
920 else if (p->output_format == ISL_FORMAT_OMEGA)
921 return isl_set_print_omega(set, p);
922 else if (p->output_format == ISL_FORMAT_LATEX)
923 return isl_map_print_latex((isl_map *)set, p, 1);
924 isl_assert(set->ctx, 0, goto error);
925 error:
926 isl_printer_free(p);
927 return NULL;
930 void isl_set_print(struct isl_set *set, FILE *out, int indent,
931 unsigned output_format)
933 isl_printer *printer;
935 if (!set)
936 return;
938 printer = isl_printer_to_file(set->ctx, out);
939 printer = isl_printer_set_indent(printer, indent);
940 printer = isl_printer_set_output_format(printer, output_format);
941 printer = isl_printer_print_set(printer, set);
943 isl_printer_free(printer);
946 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
947 __isl_keep isl_map *map)
949 if (!p || !map)
950 goto error;
952 if (p->output_format == ISL_FORMAT_ISL)
953 return isl_map_print_isl(map, p, 0);
954 else if (p->output_format == ISL_FORMAT_POLYLIB)
955 return isl_map_print_polylib(map, p);
956 else if (p->output_format == ISL_FORMAT_OMEGA)
957 return isl_map_print_omega(map, p);
958 else if (p->output_format == ISL_FORMAT_LATEX)
959 return isl_map_print_latex(map, p, 0);
960 isl_assert(map->ctx, 0, goto error);
961 error:
962 isl_printer_free(p);
963 return NULL;
966 struct isl_union_print_data {
967 isl_printer *p;
968 int set;
969 int first;
972 static int print_map_body(__isl_take isl_map *map, void *user)
974 struct isl_union_print_data *data;
975 data = (struct isl_union_print_data *)user;
977 if (!data->first)
978 data->p = isl_printer_print_str(data->p, "; ");
979 data->first = 0;
981 data->p = isl_map_print_isl_body(map, data->p, data->set);
982 isl_map_free(map);
984 return 0;
987 static __isl_give isl_printer *isl_union_map_print_isl(
988 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
990 struct isl_union_print_data data = { p, set, 1 };
991 isl_dim *dim;
992 dim = isl_union_map_get_dim(umap);
993 if (isl_dim_size(dim, isl_dim_param) > 0) {
994 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
995 p = isl_printer_print_str(p, s_to[0]);
997 isl_dim_free(dim);
998 p = isl_printer_print_str(p, s_open_set[0]);
999 isl_union_map_foreach_map(umap, &print_map_body, &data);
1000 p = data.p;
1001 p = isl_printer_print_str(p, s_close_set[0]);
1002 return p;
1005 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1007 struct isl_union_print_data *data;
1008 data = (struct isl_union_print_data *)user;
1010 if (!data->first)
1011 data->p = isl_printer_print_str(data->p, " \\cup ");
1012 data->first = 0;
1014 data->p = isl_map_print_latex(map, data->p, data->set);
1015 isl_map_free(map);
1017 return 0;
1020 static __isl_give isl_printer *isl_union_map_print_latex(
1021 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1023 struct isl_union_print_data data = { p, set, 1 };
1024 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1025 p = data.p;
1026 return p;
1029 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1030 __isl_keep isl_union_map *umap)
1032 if (!p || !umap)
1033 goto error;
1035 if (p->output_format == ISL_FORMAT_ISL)
1036 return isl_union_map_print_isl(umap, p, 0);
1037 if (p->output_format == ISL_FORMAT_LATEX)
1038 return isl_union_map_print_latex(umap, p, 0);
1040 isl_die(p->ctx, isl_error_invalid,
1041 "invalid output format for isl_union_map", goto error);
1042 error:
1043 isl_printer_free(p);
1044 return NULL;
1047 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1048 __isl_keep isl_union_set *uset)
1050 if (!p || !uset)
1051 goto error;
1053 if (p->output_format == ISL_FORMAT_ISL)
1054 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1055 if (p->output_format == ISL_FORMAT_LATEX)
1056 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1058 isl_die(p->ctx, isl_error_invalid,
1059 "invalid output format for isl_union_set", goto error);
1060 error:
1061 isl_printer_free(p);
1062 return NULL;
1065 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1066 unsigned output_format)
1068 isl_printer *printer;
1070 if (!map)
1071 return;
1073 printer = isl_printer_to_file(map->ctx, out);
1074 printer = isl_printer_set_indent(printer, indent);
1075 printer = isl_printer_set_output_format(printer, output_format);
1076 printer = isl_printer_print_map(printer, map);
1078 isl_printer_free(printer);
1081 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1083 int i;
1084 int n;
1086 for (i = 0, n = 0; i < rec->n; ++i)
1087 if (!isl_upoly_is_zero(rec->p[i]))
1088 ++n;
1090 return n;
1093 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
1094 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1096 int c = p->output_format == ISL_FORMAT_C;
1097 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1098 p = print_affine_of_len(dim, p, div->row[pos] + 1, div->n_col - 1, 1);
1099 p = isl_printer_print_str(p, c ? ", " : ")/");
1100 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1101 p = isl_printer_print_str(p, c ? ")" : "]");
1102 return p;
1105 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1106 __isl_take isl_printer *p, int first)
1108 struct isl_upoly_cst *cst;
1109 int neg;
1111 cst = isl_upoly_as_cst(up);
1112 if (!cst)
1113 goto error;
1114 neg = !first && isl_int_is_neg(cst->n);
1115 if (!first)
1116 p = isl_printer_print_str(p, neg ? " - " : " + ");
1117 if (neg)
1118 isl_int_neg(cst->n, cst->n);
1119 if (isl_int_is_zero(cst->d)) {
1120 int sgn = isl_int_sgn(cst->n);
1121 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1122 sgn == 0 ? "NaN" : "infty");
1123 } else
1124 p = isl_printer_print_isl_int(p, cst->n);
1125 if (neg)
1126 isl_int_neg(cst->n, cst->n);
1127 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1128 p = isl_printer_print_str(p, "/");
1129 p = isl_printer_print_isl_int(p, cst->d);
1131 return p;
1132 error:
1133 isl_printer_free(p);
1134 return NULL;
1137 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1138 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
1140 unsigned total;
1142 total = isl_dim_total(dim);
1143 if (var < total)
1144 p = print_term(dim, dim->ctx->one, 1 + var, p, 1, 0);
1145 else
1146 p = print_div(dim, div, var - total, p);
1147 return p;
1150 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1151 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
1153 p = print_base(p, dim, div, var);
1154 if (exp == 1)
1155 return p;
1156 if (p->output_format == ISL_FORMAT_C) {
1157 int i;
1158 for (i = 1; i < exp; ++i) {
1159 p = isl_printer_print_str(p, "*");
1160 p = print_base(p, dim, div, var);
1162 } else {
1163 p = isl_printer_print_str(p, "^");
1164 p = isl_printer_print_int(p, exp);
1166 return p;
1169 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1170 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
1171 __isl_take isl_printer *p)
1173 int i, n, first;
1174 struct isl_upoly_rec *rec;
1176 if (!p || !up || !dim || !div)
1177 goto error;
1179 if (isl_upoly_is_cst(up))
1180 return upoly_print_cst(up, p, 1);
1182 rec = isl_upoly_as_rec(up);
1183 if (!rec)
1184 goto error;
1185 n = upoly_rec_n_non_zero(rec);
1186 if (n > 1)
1187 p = isl_printer_print_str(p, "(");
1188 for (i = 0, first = 1; i < rec->n; ++i) {
1189 if (isl_upoly_is_zero(rec->p[i]))
1190 continue;
1191 if (isl_upoly_is_negone(rec->p[i])) {
1192 if (!i)
1193 p = isl_printer_print_str(p, "-1");
1194 else if (first)
1195 p = isl_printer_print_str(p, "-");
1196 else
1197 p = isl_printer_print_str(p, " - ");
1198 } else if (isl_upoly_is_cst(rec->p[i]) &&
1199 !isl_upoly_is_one(rec->p[i]))
1200 p = upoly_print_cst(rec->p[i], p, first);
1201 else {
1202 if (!first)
1203 p = isl_printer_print_str(p, " + ");
1204 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1205 p = upoly_print(rec->p[i], dim, div, p);
1207 first = 0;
1208 if (i == 0)
1209 continue;
1210 if (!isl_upoly_is_one(rec->p[i]) &&
1211 !isl_upoly_is_negone(rec->p[i]))
1212 p = isl_printer_print_str(p, " * ");
1213 p = print_pow(p, dim, div, rec->up.var, i);
1215 if (n > 1)
1216 p = isl_printer_print_str(p, ")");
1217 return p;
1218 error:
1219 isl_printer_free(p);
1220 return NULL;
1223 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
1224 __isl_keep isl_qpolynomial *qp)
1226 if (!p || !qp)
1227 goto error;
1228 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1229 return p;
1230 error:
1231 isl_printer_free(p);
1232 return NULL;
1235 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1236 unsigned output_format)
1238 isl_printer *p;
1240 if (!qp)
1241 return;
1243 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1244 p = isl_printer_to_file(qp->dim->ctx, out);
1245 p = isl_printer_print_qpolynomial(p, qp);
1246 isl_printer_free(p);
1249 static __isl_give isl_printer *qpolynomial_fold_print(
1250 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1252 int i;
1254 if (fold->type == isl_fold_min)
1255 p = isl_printer_print_str(p, "min");
1256 else if (fold->type == isl_fold_max)
1257 p = isl_printer_print_str(p, "max");
1258 p = isl_printer_print_str(p, "(");
1259 for (i = 0; i < fold->n; ++i) {
1260 if (i)
1261 p = isl_printer_print_str(p, ", ");
1262 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
1264 p = isl_printer_print_str(p, ")");
1265 return p;
1268 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1269 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1271 if (!p || !fold)
1272 goto error;
1273 p = qpolynomial_fold_print(fold, p);
1274 return p;
1275 error:
1276 isl_printer_free(p);
1277 return NULL;
1280 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1281 FILE *out, unsigned output_format)
1283 isl_printer *p;
1285 if (!fold)
1286 return;
1288 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1290 p = isl_printer_to_file(fold->dim->ctx, out);
1291 p = isl_printer_print_qpolynomial_fold(p, fold);
1293 isl_printer_free(p);
1296 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1297 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1299 int i = 0;
1301 for (i = 0; i < pwqp->n; ++i) {
1302 if (i)
1303 p = isl_printer_print_str(p, "; ");
1304 if (isl_dim_size(pwqp->p[i].set->dim, isl_dim_set) > 0 ||
1305 isl_dim_get_tuple_name(pwqp->p[i].set->dim, isl_dim_set)) {
1306 p = print_dim(pwqp->p[i].set->dim, p, 1, 0, NULL);
1307 p = isl_printer_print_str(p, " -> ");
1309 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
1310 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1313 return p;
1316 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1317 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1319 if (!p || !pwqp)
1320 goto error;
1322 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
1323 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1324 p = isl_printer_print_str(p, " -> ");
1326 p = isl_printer_print_str(p, "{ ");
1327 if (pwqp->n == 0) {
1328 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0) {
1329 p = print_dim(pwqp->dim, p, 1, 0, NULL);
1330 p = isl_printer_print_str(p, " -> ");
1332 p = isl_printer_print_str(p, "0");
1334 p = isl_pwqp_print_isl_body(p, pwqp);
1335 p = isl_printer_print_str(p, " }");
1336 return p;
1337 error:
1338 isl_printer_free(p);
1339 return NULL;
1342 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1343 unsigned output_format)
1345 isl_printer *p;
1347 if (!pwqp)
1348 return;
1350 p = isl_printer_to_file(pwqp->dim->ctx, out);
1351 p = isl_printer_set_output_format(p, output_format);
1352 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1354 isl_printer_free(p);
1357 static __isl_give isl_printer *isl_pwf_print_isl_body(
1358 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1360 int i = 0;
1362 for (i = 0; i < pwf->n; ++i) {
1363 if (i)
1364 p = isl_printer_print_str(p, "; ");
1365 if (isl_dim_size(pwf->p[i].set->dim, isl_dim_set) > 0) {
1366 p = print_dim(pwf->p[i].set->dim, p, 1, 0, NULL);
1367 p = isl_printer_print_str(p, " -> ");
1369 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1370 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1373 return p;
1376 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1377 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1379 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1380 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1381 p = isl_printer_print_str(p, " -> ");
1383 p = isl_printer_print_str(p, "{ ");
1384 if (pwf->n == 0) {
1385 if (isl_dim_size(pwf->dim, isl_dim_set) > 0) {
1386 p = print_dim(pwf->dim, p, 1, 0, NULL);
1387 p = isl_printer_print_str(p, " -> ");
1389 p = isl_printer_print_str(p, "0");
1391 p = isl_pwf_print_isl_body(p, pwf);
1392 p = isl_printer_print_str(p, " }");
1393 return p;
1396 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1397 __isl_keep isl_basic_set *bset, isl_int *c);
1399 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1400 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1402 if (type == isl_dim_div) {
1403 p = isl_printer_print_str(p, "floord(");
1404 p = print_affine_c(p, bset, bset->div[pos] + 1);
1405 p = isl_printer_print_str(p, ", ");
1406 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1407 p = isl_printer_print_str(p, ")");
1408 } else {
1409 const char *name;
1411 name = isl_dim_get_name(bset->dim, type, pos);
1412 if (!name)
1413 name = "UNNAMED";
1414 p = isl_printer_print_str(p, name);
1416 return p;
1419 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1420 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1422 enum isl_dim_type type;
1423 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1425 if (pos == 0)
1426 return isl_printer_print_isl_int(p, c);
1428 if (isl_int_is_one(c))
1430 else if (isl_int_is_negone(c))
1431 p = isl_printer_print_str(p, "-");
1432 else {
1433 p = isl_printer_print_isl_int(p, c);
1434 p = isl_printer_print_str(p, "*");
1436 type = pos2type(bset->dim, &pos);
1437 p = print_name_c(p, bset, type, pos);
1438 return p;
1441 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1442 __isl_keep isl_basic_set *bset, isl_int *c)
1444 int i;
1445 int first;
1446 unsigned len = 1 + isl_basic_set_total_dim(bset);
1448 for (i = 0, first = 1; i < len; ++i) {
1449 int flip = 0;
1450 if (isl_int_is_zero(c[i]))
1451 continue;
1452 if (!first) {
1453 if (isl_int_is_neg(c[i])) {
1454 flip = 1;
1455 isl_int_neg(c[i], c[i]);
1456 p = isl_printer_print_str(p, " - ");
1457 } else
1458 p = isl_printer_print_str(p, " + ");
1460 first = 0;
1461 p = print_term_c(p, bset, c[i], i);
1462 if (flip)
1463 isl_int_neg(c[i], c[i]);
1465 if (first)
1466 p = isl_printer_print_str(p, "0");
1467 return p;
1470 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1471 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1473 if (!first)
1474 p = isl_printer_print_str(p, " && ");
1476 p = print_affine_c(p, bset, c);
1477 p = isl_printer_print_str(p, " ");
1478 p = isl_printer_print_str(p, op);
1479 p = isl_printer_print_str(p, " 0");
1480 return p;
1483 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1484 __isl_keep isl_basic_set *bset)
1486 int i;
1488 for (i = 0; i < bset->n_eq; ++i)
1489 p = print_constraint_c(p, bset, bset->eq[i], "==", !i);
1490 for (i = 0; i < bset->n_ineq; ++i)
1491 p = print_constraint_c(p, bset, bset->ineq[i], ">=",
1492 !bset->n_eq && !i);
1493 return p;
1496 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1497 __isl_keep isl_set *set)
1499 int i;
1501 if (set->n == 0)
1502 p = isl_printer_print_str(p, "0");
1504 for (i = 0; i < set->n; ++i) {
1505 if (i)
1506 p = isl_printer_print_str(p, " || ");
1507 if (set->n > 1)
1508 p = isl_printer_print_str(p, "(");
1509 p = print_basic_set_c(p, set->p[i]);
1510 if (set->n > 1)
1511 p = isl_printer_print_str(p, ")");
1513 return p;
1516 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1517 __isl_keep isl_qpolynomial *qp)
1519 isl_int den;
1521 isl_int_init(den);
1522 isl_qpolynomial_get_den(qp, &den);
1523 if (!isl_int_is_one(den)) {
1524 isl_qpolynomial *f;
1525 p = isl_printer_print_str(p, "(");
1526 qp = isl_qpolynomial_copy(qp);
1527 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1528 den, qp->dim->ctx->one);
1529 qp = isl_qpolynomial_mul(qp, f);
1531 if (qp)
1532 p = upoly_print(qp->upoly, qp->dim, qp->div, p);
1533 if (!isl_int_is_one(den)) {
1534 p = isl_printer_print_str(p, ")/");
1535 p = isl_printer_print_isl_int(p, den);
1536 isl_qpolynomial_free(qp);
1538 isl_int_clear(den);
1539 return p;
1542 static __isl_give isl_printer *print_pw_qpolynomial_c(
1543 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwpq)
1545 int i;
1547 if (pwpq->n == 1 && isl_set_fast_is_universe(pwpq->p[0].set))
1548 return print_qpolynomial_c(p, pwpq->p[0].qp);
1550 for (i = 0; i < pwpq->n; ++i) {
1551 p = isl_printer_print_str(p, "(");
1552 p = print_set_c(p, pwpq->p[i].set);
1553 p = isl_printer_print_str(p, ") ? (");
1554 p = print_qpolynomial_c(p, pwpq->p[i].qp);
1555 p = isl_printer_print_str(p, ") : ");
1558 p = isl_printer_print_str(p, "0");
1559 return p;
1562 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1563 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1565 if (!p || !pwqp)
1566 goto error;
1568 if (p->output_format == ISL_FORMAT_ISL)
1569 return print_pw_qpolynomial_isl(p, pwqp);
1570 else if (p->output_format == ISL_FORMAT_C)
1571 return print_pw_qpolynomial_c(p, pwqp);
1572 isl_assert(p->ctx, 0, goto error);
1573 error:
1574 isl_printer_free(p);
1575 return NULL;
1578 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1580 struct isl_union_print_data *data;
1581 data = (struct isl_union_print_data *)user;
1583 if (!data->first)
1584 data->p = isl_printer_print_str(data->p, "; ");
1585 data->first = 0;
1587 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1588 isl_pw_qpolynomial_free(pwqp);
1590 return 0;
1593 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1594 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1596 struct isl_union_print_data data = { p, 1, 1 };
1597 isl_dim *dim;
1598 dim = isl_union_pw_qpolynomial_get_dim(upwqp);
1599 if (isl_dim_size(dim, isl_dim_param) > 0) {
1600 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1601 p = isl_printer_print_str(p, " -> ");
1603 isl_dim_free(dim);
1604 p = isl_printer_print_str(p, "{ ");
1605 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1606 &data);
1607 p = data.p;
1608 p = isl_printer_print_str(p, " }");
1609 return p;
1612 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1613 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1615 if (!p || !upwqp)
1616 goto error;
1618 if (p->output_format == ISL_FORMAT_ISL)
1619 return print_union_pw_qpolynomial_isl(p, upwqp);
1620 isl_die(p->ctx, isl_error_invalid,
1621 "invalid output format for isl_union_pw_qpolynomial",
1622 goto error);
1623 error:
1624 isl_printer_free(p);
1625 return NULL;
1628 static __isl_give isl_printer *print_qpolynomial_fold_c(
1629 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1631 int i;
1633 for (i = 0; i < fold->n - 1; ++i)
1634 if (fold->type == isl_fold_min)
1635 p = isl_printer_print_str(p, "min(");
1636 else if (fold->type == isl_fold_max)
1637 p = isl_printer_print_str(p, "max(");
1639 for (i = 0; i < fold->n; ++i) {
1640 if (i)
1641 p = isl_printer_print_str(p, ", ");
1642 p = print_qpolynomial_c(p, fold->qp[i]);
1643 if (i)
1644 p = isl_printer_print_str(p, ")");
1646 return p;
1649 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1650 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1652 int i;
1654 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1655 return print_qpolynomial_fold_c(p, pwf->p[0].fold);
1657 for (i = 0; i < pwf->n; ++i) {
1658 p = isl_printer_print_str(p, "(");
1659 p = print_set_c(p, pwf->p[i].set);
1660 p = isl_printer_print_str(p, ") ? (");
1661 p = print_qpolynomial_fold_c(p, pwf->p[i].fold);
1662 p = isl_printer_print_str(p, ") : ");
1665 p = isl_printer_print_str(p, "0");
1666 return p;
1669 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1670 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1672 if (!p || !pwf)
1673 goto error;
1675 if (p->output_format == ISL_FORMAT_ISL)
1676 return print_pw_qpolynomial_fold_isl(p, pwf);
1677 else if (p->output_format == ISL_FORMAT_C)
1678 return print_pw_qpolynomial_fold_c(p, pwf);
1679 isl_assert(p->ctx, 0, goto error);
1680 error:
1681 isl_printer_free(p);
1682 return NULL;
1685 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1686 FILE *out, unsigned output_format)
1688 isl_printer *p;
1690 if (!pwf)
1691 return;
1693 p = isl_printer_to_file(pwf->dim->ctx, out);
1694 p = isl_printer_set_output_format(p, output_format);
1695 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1697 isl_printer_free(p);
1700 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1702 struct isl_union_print_data *data;
1703 data = (struct isl_union_print_data *)user;
1705 if (!data->first)
1706 data->p = isl_printer_print_str(data->p, "; ");
1707 data->first = 0;
1709 data->p = isl_pwf_print_isl_body(data->p, pwf);
1710 isl_pw_qpolynomial_fold_free(pwf);
1712 return 0;
1715 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1716 __isl_take isl_printer *p,
1717 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1719 struct isl_union_print_data data = { p, 1, 1 };
1720 isl_dim *dim;
1721 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
1722 if (isl_dim_size(dim, isl_dim_param) > 0) {
1723 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1724 p = isl_printer_print_str(p, " -> ");
1726 isl_dim_free(dim);
1727 p = isl_printer_print_str(p, "{ ");
1728 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1729 &print_pwf_body, &data);
1730 p = data.p;
1731 p = isl_printer_print_str(p, " }");
1732 return p;
1735 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1736 __isl_take isl_printer *p,
1737 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1739 if (!p || !upwf)
1740 goto error;
1742 if (p->output_format == ISL_FORMAT_ISL)
1743 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1744 isl_die(p->ctx, isl_error_invalid,
1745 "invalid output format for isl_union_pw_qpolynomial_fold",
1746 goto error);
1747 error:
1748 isl_printer_free(p);
1749 return NULL;