isl_mat_scale_down: avoid trampling over aliased matrices
[isl.git] / isl_output.c
blobffdb3e2457423f388730c37ca28f721e492cd6cb
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_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl/set.h>
18 #include <isl/seq.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_printer_private.h>
21 #include <isl_dim_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/union_map.h>
25 static const char *s_to[2] = { " -> ", " \\to " };
26 static const char *s_and[2] = { " and ", " \\wedge " };
27 static const char *s_or[2] = { " or ", " \\vee " };
28 static const char *s_le[2] = { "<=", "\\le" };
29 static const char *s_ge[2] = { ">=", "\\ge" };
30 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
31 static const char *s_close_set[2] = { " }", " \\,\\}" };
32 static const char *s_open_list[2] = { "[", "(" };
33 static const char *s_close_list[2] = { "]", ")" };
34 static const char *s_such_that[2] = { " : ", " \\mid " };
35 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
36 static const char *s_close_exists[2] = { ")", "" };
37 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
38 static const char *s_param_prefix[2] = { "p", "p_" };
39 static const char *s_input_prefix[2] = { "i", "i_" };
40 static const char *s_output_prefix[2] = { "o", "o_" };
42 static __isl_give isl_printer *print_constraint_polylib(
43 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
45 int i;
46 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
47 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
48 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
49 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
51 p = isl_printer_start_line(p);
52 p = isl_printer_print_int(p, ineq);
53 for (i = 0; i < n_out; ++i) {
54 p = isl_printer_print_str(p, " ");
55 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
57 for (i = 0; i < n_in; ++i) {
58 p = isl_printer_print_str(p, " ");
59 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
61 for (i = 0; i < bmap->n_div; ++i) {
62 p = isl_printer_print_str(p, " ");
63 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
65 for (i = 0; i < nparam; ++i) {
66 p = isl_printer_print_str(p, " ");
67 p = isl_printer_print_isl_int(p, c[1+i]);
69 p = isl_printer_print_str(p, " ");
70 p = isl_printer_print_isl_int(p, c[0]);
71 p = isl_printer_end_line(p);
72 return p;
75 static __isl_give isl_printer *print_constraints_polylib(
76 struct isl_basic_map *bmap, __isl_take isl_printer *p)
78 int i;
80 p = isl_printer_set_isl_int_width(p, 5);
82 for (i = 0; i < bmap->n_eq; ++i)
83 p = print_constraint_polylib(bmap, 0, i, p);
84 for (i = 0; i < bmap->n_ineq; ++i)
85 p = print_constraint_polylib(bmap, 1, i, p);
87 return p;
90 static __isl_give isl_printer *bset_print_constraints_polylib(
91 struct isl_basic_set *bset, __isl_take isl_printer *p)
93 return print_constraints_polylib((struct isl_basic_map *)bset, p);
96 static __isl_give isl_printer *isl_basic_map_print_polylib(
97 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
99 unsigned total = isl_basic_map_total_dim(bmap);
100 p = isl_printer_start_line(p);
101 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
102 p = isl_printer_print_str(p, " ");
103 p = isl_printer_print_int(p, 1 + total + 1);
104 if (ext) {
105 p = isl_printer_print_str(p, " ");
106 p = isl_printer_print_int(p,
107 isl_basic_map_dim(bmap, isl_dim_out));
108 p = isl_printer_print_str(p, " ");
109 p = isl_printer_print_int(p,
110 isl_basic_map_dim(bmap, isl_dim_in));
111 p = isl_printer_print_str(p, " ");
112 p = isl_printer_print_int(p,
113 isl_basic_map_dim(bmap, isl_dim_div));
114 p = isl_printer_print_str(p, " ");
115 p = isl_printer_print_int(p,
116 isl_basic_map_dim(bmap, isl_dim_param));
118 p = isl_printer_end_line(p);
119 return print_constraints_polylib(bmap, p);
122 static __isl_give isl_printer *isl_basic_set_print_polylib(
123 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
125 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
128 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
129 __isl_take isl_printer *p, int ext)
131 int i;
133 p = isl_printer_start_line(p);
134 p = isl_printer_print_int(p, map->n);
135 p = isl_printer_end_line(p);
136 for (i = 0; i < map->n; ++i) {
137 p = isl_printer_start_line(p);
138 p = isl_printer_end_line(p);
139 p = isl_basic_map_print_polylib(map->p[i], p, ext);
141 return p;
144 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
145 __isl_take isl_printer *p, int ext)
147 return isl_map_print_polylib((struct isl_map *)set, p, ext);
150 static int count_same_name(__isl_keep isl_dim *dim,
151 enum isl_dim_type type, unsigned pos, const char *name)
153 enum isl_dim_type t;
154 unsigned p, s;
155 int count = 0;
157 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
158 s = t == type ? pos : isl_dim_size(dim, t);
159 for (p = 0; p < s; ++p) {
160 const char *n = isl_dim_get_name(dim, t, p);
161 if (n && !strcmp(n, name))
162 count++;
165 return count;
168 static __isl_give isl_printer *print_name(__isl_keep isl_dim *dim,
169 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
170 int set, int latex)
172 const char *name;
173 char buffer[20];
174 int primes;
176 name = type == isl_dim_div ? NULL : isl_dim_get_name(dim, type, pos);
178 if (!name) {
179 const char *prefix;
180 if (type == isl_dim_param)
181 prefix = s_param_prefix[latex];
182 else if (type == isl_dim_div)
183 prefix = s_div_prefix[latex];
184 else if (set || type == isl_dim_in)
185 prefix = s_input_prefix[latex];
186 else
187 prefix = s_output_prefix[latex];
188 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
189 name = buffer;
191 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
192 pos, name);
193 p = isl_printer_print_str(p, name);
194 while (primes-- > 0)
195 p = isl_printer_print_str(p, "'");
196 return p;
199 static enum isl_dim_type pos2type(__isl_keep isl_dim *dim, unsigned *pos)
201 enum isl_dim_type type;
202 unsigned n_in = isl_dim_size(dim, isl_dim_in);
203 unsigned n_out = isl_dim_size(dim, isl_dim_out);
204 unsigned nparam = isl_dim_size(dim, isl_dim_param);
206 if (*pos < 1 + nparam) {
207 type = isl_dim_param;
208 *pos -= 1;
209 } else if (*pos < 1 + nparam + n_in) {
210 type = isl_dim_in;
211 *pos -= 1 + nparam;
212 } else if (*pos < 1 + nparam + n_in + n_out) {
213 type = isl_dim_out;
214 *pos -= 1 + nparam + n_in;
215 } else {
216 type = isl_dim_div;
217 *pos -= 1 + nparam + n_in + n_out;
220 return type;
223 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
224 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
226 static __isl_give isl_printer *print_term(__isl_keep isl_dim *dim,
227 __isl_keep isl_mat *div,
228 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
230 enum isl_dim_type type;
231 int print_div_def;
233 if (pos == 0)
234 return isl_printer_print_isl_int(p, c);
236 type = pos2type(dim, &pos);
237 print_div_def = type == isl_dim_div && div &&
238 !isl_int_is_zero(div->row[pos][0]);
240 if (isl_int_is_one(c))
242 else if (isl_int_is_negone(c))
243 p = isl_printer_print_str(p, "-");
244 else {
245 p = isl_printer_print_isl_int(p, c);
246 if (p->output_format == ISL_FORMAT_C || print_div_def)
247 p = isl_printer_print_str(p, "*");
249 if (print_div_def)
250 p = print_div(dim, div, pos, p);
251 else
252 p = print_name(dim, p, type, pos, set, latex);
253 return p;
256 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_dim *dim,
257 __isl_keep isl_mat *div,
258 __isl_take isl_printer *p, isl_int *c, int len, int set)
260 int i;
261 int first;
263 for (i = 0, first = 1; i < len; ++i) {
264 int flip = 0;
265 if (isl_int_is_zero(c[i]))
266 continue;
267 if (!first) {
268 if (isl_int_is_neg(c[i])) {
269 flip = 1;
270 isl_int_neg(c[i], c[i]);
271 p = isl_printer_print_str(p, " - ");
272 } else
273 p = isl_printer_print_str(p, " + ");
275 first = 0;
276 p = print_term(dim, div, c[i], i, p, set, 0);
277 if (flip)
278 isl_int_neg(c[i], c[i]);
280 if (first)
281 p = isl_printer_print_str(p, "0");
282 return p;
285 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
286 __isl_keep isl_dim *dim, __isl_take isl_printer *p, isl_int *c, int set)
288 unsigned len = 1 + isl_basic_map_total_dim(bmap);
289 return print_affine_of_len(dim, NULL, p, c, len, set);
292 static int defining_equality(__isl_keep isl_basic_map *eq,
293 __isl_keep isl_dim *dim, enum isl_dim_type type, int pos)
295 int i;
296 unsigned total;
298 if (!eq)
299 return -1;
301 pos += isl_dim_offset(dim, type);
302 total = isl_basic_map_total_dim(eq);
304 for (i = 0; i < eq->n_eq; ++i) {
305 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
306 continue;
307 if (isl_int_is_one(eq->eq[i][1 + pos]))
308 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
309 return i;
312 return -1;
315 /* offset is the offset of local_dim inside global_type of global_dim.
317 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
318 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
319 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
320 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
322 int i, j;
324 if (global_dim != local_dim && local_type == isl_dim_out)
325 offset += local_dim->n_in;
327 for (i = 0; i < isl_dim_size(local_dim, local_type); ++i) {
328 if (i)
329 p = isl_printer_print_str(p, ", ");
330 j = defining_equality(eq, global_dim, global_type, offset + i);
331 if (j >= 0) {
332 int pos = 1 + isl_dim_offset(global_dim, global_type)
333 + offset + i;
334 p = print_affine_of_len(eq->dim, NULL,
335 p, eq->eq[j], pos, set);
336 } else {
337 p = print_name(global_dim, p, global_type, offset + i,
338 set, latex);
341 return p;
344 static __isl_give isl_printer *print_var_list(__isl_keep isl_dim *dim,
345 __isl_take isl_printer *p, enum isl_dim_type type,
346 int set, int latex, __isl_keep isl_basic_map *eq)
348 return print_nested_var_list(p, dim, type, dim, type, set, latex, eq, 0);
351 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
352 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
353 __isl_keep isl_dim *local_dim,
354 int set, int latex, __isl_keep isl_basic_map *eq, int offset);
356 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
357 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
358 __isl_keep isl_dim *local_dim, enum isl_dim_type local_type,
359 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
361 const char *name = NULL;
362 unsigned n = isl_dim_size(local_dim, local_type);
363 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
364 name = isl_dim_get_tuple_name(local_dim, local_type);
365 if (name) {
366 if (latex)
367 p = isl_printer_print_str(p, "\\mathrm{");
368 p = isl_printer_print_str(p, name);
369 if (latex)
370 p = isl_printer_print_str(p, "}");
373 if (!latex || n != 1 || name)
374 p = isl_printer_print_str(p, s_open_list[latex]);
375 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
376 local_dim->nested[local_type - isl_dim_in]) {
377 if (global_dim != local_dim && local_type == isl_dim_out)
378 offset += local_dim->n_in;
379 p = print_nested_map_dim(p, global_dim, global_type,
380 local_dim->nested[local_type - isl_dim_in],
381 set, latex, eq, offset);
382 } else
383 p = print_nested_var_list(p, global_dim, global_type,
384 local_dim, local_type, set, latex, eq, offset);
385 if (!latex || n != 1 || name)
386 p = isl_printer_print_str(p, s_close_list[latex]);
387 return p;
390 static __isl_give isl_printer *print_tuple(__isl_keep isl_dim *dim,
391 __isl_take isl_printer *p, enum isl_dim_type type,
392 int set, int latex, __isl_keep isl_basic_map *eq)
394 return print_nested_tuple(p, dim, type, dim, type, set, latex, eq, 0);
397 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
398 __isl_keep isl_dim *global_dim, enum isl_dim_type global_type,
399 __isl_keep isl_dim *local_dim,
400 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
402 p = print_nested_tuple(p, global_dim, global_type,
403 local_dim, isl_dim_in, set, latex, eq, offset);
404 p = isl_printer_print_str(p, s_to[latex]);
405 p = print_nested_tuple(p, global_dim, global_type,
406 local_dim, isl_dim_out, set, latex, eq, offset);
408 return p;
411 static __isl_give isl_printer *print_dim(__isl_keep isl_dim *dim,
412 __isl_take isl_printer *p, int set, int latex, int rational,
413 __isl_keep isl_basic_map *eq)
415 if (rational && !latex)
416 p = isl_printer_print_str(p, "rat: ");
417 if (set)
418 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
419 else {
420 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
421 p = isl_printer_print_str(p, s_to[latex]);
422 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
425 return p;
428 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_dim *dim,
429 __isl_take isl_printer *p)
431 if (isl_dim_size(dim, isl_dim_param) == 0)
432 return p;
434 p = isl_printer_start_line(p);
435 p = isl_printer_print_str(p, "symbolic ");
436 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
437 p = isl_printer_print_str(p, ";");
438 p = isl_printer_end_line(p);
439 return p;
442 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
443 __isl_keep isl_dim *dim, __isl_take isl_printer *p,
444 isl_int *c, int last, const char *op, int first_constraint, int set,
445 int latex)
447 if (!first_constraint)
448 p = isl_printer_print_str(p, s_and[latex]);
450 isl_int_abs(c[last], c[last]);
452 p = print_term(dim, NULL, c[last], last, p, set, latex);
454 p = isl_printer_print_str(p, " ");
455 p = isl_printer_print_str(p, op);
456 p = isl_printer_print_str(p, " ");
458 isl_int_set_si(c[last], 0);
459 p = print_affine(bmap, dim, p, c, set);
461 return p;
464 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
465 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
467 int i;
468 struct isl_vec *c;
469 unsigned total = isl_basic_map_total_dim(bmap);
471 c = isl_vec_alloc(bmap->ctx, 1 + total);
472 if (!c)
473 goto error;
475 for (i = bmap->n_eq - 1; i >= 0; --i) {
476 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
477 isl_assert(bmap->ctx, l >= 0, goto error);
478 if (isl_int_is_neg(bmap->eq[i][l]))
479 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
480 else
481 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
482 p = print_constraint(bmap, dim, p, c->el, l,
483 "=", i == bmap->n_eq - 1, set, latex);
485 for (i = 0; i < bmap->n_ineq; ++i) {
486 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
487 int s;
488 const char *op;
489 if (l < 0)
490 continue;
491 s = isl_int_sgn(bmap->ineq[i][l]);
492 if (s < 0)
493 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
494 else
495 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
496 op = s < 0 ? s_le[latex] : s_ge[latex];
497 p = print_constraint(bmap, dim, p, c->el, l,
498 op, !bmap->n_eq && !i, set, latex);
501 isl_vec_free(c);
503 return p;
504 error:
505 isl_vec_free(c);
506 isl_printer_free(p);
507 return NULL;
510 static __isl_give isl_printer *print_omega_constraints(
511 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
513 if (bmap->n_eq + bmap->n_ineq == 0)
514 return p;
516 p = isl_printer_print_str(p, ": ");
517 if (bmap->n_div > 0) {
518 int i;
519 p = isl_printer_print_str(p, "exists (");
520 for (i = 0; i < bmap->n_div; ++i) {
521 if (i)
522 p = isl_printer_print_str(p, ", ");
523 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
525 p = isl_printer_print_str(p, ": ");
527 p = print_constraints(bmap, bmap->dim, p, set, 0);
528 if (bmap->n_div > 0)
529 p = isl_printer_print_str(p, ")");
530 return p;
533 static __isl_give isl_printer *basic_map_print_omega(
534 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
536 p = isl_printer_print_str(p, "{ [");
537 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
538 p = isl_printer_print_str(p, "] -> [");
539 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
540 p = isl_printer_print_str(p, "] ");
541 p = print_omega_constraints(bmap, p, 0);
542 p = isl_printer_print_str(p, " }");
543 return p;
546 static __isl_give isl_printer *isl_basic_map_print_omega(
547 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
549 p = print_omega_parameters(bmap->dim, p);
551 p = isl_printer_start_line(p);
552 p = basic_map_print_omega(bmap, p);
553 p = isl_printer_end_line(p);
554 return p;
557 static __isl_give isl_printer *basic_set_print_omega(
558 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
560 p = isl_printer_print_str(p, "{ [");
561 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
562 p = isl_printer_print_str(p, "] ");
563 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
564 p = isl_printer_print_str(p, " }");
565 return p;
568 static __isl_give isl_printer *isl_basic_set_print_omega(
569 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
571 p = print_omega_parameters(bset->dim, p);
573 p = isl_printer_start_line(p);
574 p = basic_set_print_omega(bset, p);
575 p = isl_printer_end_line(p);
576 return p;
579 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
580 __isl_take isl_printer *p)
582 int i;
584 p = print_omega_parameters(map->dim, p);
586 p = isl_printer_start_line(p);
587 for (i = 0; i < map->n; ++i) {
588 if (i)
589 p = isl_printer_print_str(p, " union ");
590 p = basic_map_print_omega(map->p[i], p);
592 p = isl_printer_end_line(p);
593 return p;
596 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
597 __isl_take isl_printer *p)
599 int i;
601 p = print_omega_parameters(set->dim, p);
603 p = isl_printer_start_line(p);
604 for (i = 0; i < set->n; ++i) {
605 if (i)
606 p = isl_printer_print_str(p, " union ");
607 p = basic_set_print_omega(set->p[i], p);
609 p = isl_printer_end_line(p);
610 return p;
613 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
614 __isl_keep isl_dim *dim, __isl_take isl_printer *p, int set, int latex)
616 if (bmap->n_div > 0) {
617 int i;
618 p = isl_printer_print_str(p, s_open_exists[latex]);
619 for (i = 0; i < bmap->n_div; ++i) {
620 if (i)
621 p = isl_printer_print_str(p, ", ");
622 p = print_name(dim, p, isl_dim_div, i, 0, latex);
623 if (latex || isl_int_is_zero(bmap->div[i][0]))
624 continue;
625 p = isl_printer_print_str(p, " = [(");
626 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
627 p = isl_printer_print_str(p, ")/");
628 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
629 p = isl_printer_print_str(p, "]");
631 p = isl_printer_print_str(p, ": ");
634 p = print_constraints(bmap, dim, p, set, latex);
636 if (bmap->n_div > 0)
637 p = isl_printer_print_str(p, s_close_exists[latex]);
638 return p;
641 static __isl_give isl_printer *isl_basic_map_print_isl(
642 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
643 int set, int latex)
645 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
646 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
647 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
648 p = isl_printer_print_str(p, " -> ");
650 p = isl_printer_print_str(p, "{ ");
651 p = print_dim(bmap->dim, p, set, latex, rational, NULL);
652 p = isl_printer_print_str(p, " : ");
653 p = print_disjunct(bmap, bmap->dim, p, set, latex);
654 p = isl_printer_print_str(p, " }");
655 return p;
658 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
659 __isl_take isl_printer *p, int set, int latex)
661 int i;
663 if (isl_map_fast_is_universe(map))
664 return p;
666 p = isl_printer_print_str(p, s_such_that[latex]);
667 if (map->n == 0)
668 p = isl_printer_print_str(p, "1 = 0");
669 for (i = 0; i < map->n; ++i) {
670 if (i)
671 p = isl_printer_print_str(p, s_or[latex]);
672 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
673 p = isl_printer_print_str(p, "(");
674 p = print_disjunct(map->p[i], map->dim, p, set, latex);
675 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
676 p = isl_printer_print_str(p, ")");
678 return p;
681 struct isl_aff_split {
682 isl_basic_map *aff;
683 isl_map *map;
686 static void free_split(__isl_take struct isl_aff_split *split, int n)
688 int i;
690 if (!split)
691 return;
693 for (i = 0; i < n; ++i) {
694 isl_basic_map_free(split[i].aff);
695 isl_map_free(split[i].map);
698 free(split);
701 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
703 int i, j;
704 unsigned nparam, n_in, n_out, total;
706 bmap = isl_basic_map_cow(bmap);
707 if (!bmap)
708 return NULL;
709 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
710 goto error;
712 nparam = isl_basic_map_dim(bmap, isl_dim_param);
713 n_in = isl_basic_map_dim(bmap, isl_dim_in);
714 n_out = isl_basic_map_dim(bmap, isl_dim_out);
715 total = isl_basic_map_dim(bmap, isl_dim_all);
716 for (i = bmap->n_eq - 1; i >= 0; --i) {
717 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
718 if (j >= nparam && j < nparam + n_in + n_out &&
719 (isl_int_is_one(bmap->eq[i][1 + j]) ||
720 isl_int_is_negone(bmap->eq[i][1 + j])))
721 continue;
722 if (isl_basic_map_drop_equality(bmap, i) < 0)
723 goto error;
726 bmap = isl_basic_map_finalize(bmap);
728 return bmap;
729 error:
730 isl_basic_map_free(bmap);
731 return NULL;
734 static int aff_split_cmp(const void *p1, const void *p2)
736 const struct isl_aff_split *s1, *s2;
737 s1 = (const struct isl_aff_split *) p1;
738 s2 = (const struct isl_aff_split *) p2;
740 return isl_basic_map_fast_cmp(s1->aff, s2->aff);
743 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
744 __isl_keep isl_basic_map *aff)
746 int i, j;
747 unsigned total;
749 if (!bmap || !aff)
750 goto error;
752 total = isl_dim_total(bmap->dim);
754 for (i = bmap->n_eq - 1; i >= 0; --i) {
755 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
756 bmap->n_div) != -1)
757 continue;
758 for (j = 0; j < aff->n_eq; ++j) {
759 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
760 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
761 continue;
762 if (isl_basic_map_drop_equality(bmap, i) < 0)
763 goto error;
764 break;
768 return bmap;
769 error:
770 isl_basic_map_free(bmap);
771 return NULL;
774 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
776 int i, n;
777 struct isl_aff_split *split;
779 split = isl_calloc_array(map->isl, struct isl_aff_split, map->n);
780 if (!split)
781 return NULL;
783 for (i = 0; i < map->n; ++i) {
784 isl_basic_map *bmap;
785 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
786 bmap = isl_basic_map_copy(map->p[i]);
787 bmap = isl_basic_map_cow(bmap);
788 bmap = drop_aff(bmap, split[i].aff);
789 split[i].map = isl_map_from_basic_map(bmap);
790 if (!split[i].aff || !split[i].map)
791 goto error;
794 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
796 n = map->n;
797 for (i = n - 1; i >= 1; --i) {
798 if (!isl_basic_map_fast_is_equal(split[i - 1].aff,
799 split[i].aff))
800 continue;
801 isl_basic_map_free(split[i].aff);
802 split[i - 1].map = isl_map_union(split[i - 1].map,
803 split[i].map);
804 if (i != n - 1)
805 split[i] = split[n - 1];
806 split[n - 1].aff = NULL;
807 split[n - 1].map = NULL;
808 --n;
811 return split;
812 error:
813 free_split(split, map->n);
814 return NULL;
817 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
818 struct isl_aff_split *split, int n, int set)
820 int i;
821 int rational;
823 for (i = 0; i < n; ++i) {
824 isl_dim *dim;
826 if (!split[i].map)
827 break;
828 dim = split[i].map->dim;
829 rational = split[i].map->n > 0 &&
830 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
831 if (i)
832 p = isl_printer_print_str(p, "; ");
833 p = print_dim(dim, p, set, 0, rational, split[i].aff);
834 p = print_disjuncts(split[i].map, p, set, 0);
837 return p;
840 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
841 __isl_take isl_printer *p, int set)
843 struct isl_aff_split *split = NULL;
844 int rational;
846 if (map->n > 0)
847 split = split_aff(map);
848 if (split) {
849 p = print_split_map(p, split, map->n, set);
850 } else {
851 rational = map->n > 0 &&
852 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
853 p = print_dim(map->dim, p, set, 0, rational, NULL);
854 p = print_disjuncts(map, p, set, 0);
856 free_split(split, map->n);
857 return p;
860 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
861 __isl_take isl_printer *p, int set)
863 if (isl_map_dim(map, isl_dim_param) > 0) {
864 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
865 p = isl_printer_print_str(p, s_to[0]);
867 p = isl_printer_print_str(p, s_open_set[0]);
868 p = isl_map_print_isl_body(map, p, set);
869 p = isl_printer_print_str(p, s_close_set[0]);
870 return p;
873 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
874 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
876 if (isl_map_dim(map, isl_dim_param) > 0) {
877 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
878 p = isl_printer_print_str(p, s_to[1]);
880 p = isl_printer_print_str(p, s_open_set[1]);
881 p = print_dim(map->dim, p, set, 1, 0, aff);
882 p = print_disjuncts(map, p, set, 1);
883 p = isl_printer_print_str(p, s_close_set[1]);
885 return p;
888 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
889 __isl_take isl_printer *p, int set)
891 int i;
892 struct isl_aff_split *split = NULL;
894 if (map->n > 0)
895 split = split_aff(map);
897 if (!split)
898 return print_latex_map(map, p, NULL, set);
900 for (i = 0; i < map->n; ++i) {
901 if (!split[i].map)
902 break;
903 if (i)
904 p = isl_printer_print_str(p, " \\cup ");
905 p = print_latex_map(split[i].map, p, split[i].aff, set);
908 free_split(split, map->n);
909 return p;
912 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
913 __isl_keep isl_basic_map *bmap)
915 if (!p || !bmap)
916 goto error;
917 if (p->output_format == ISL_FORMAT_ISL)
918 return isl_basic_map_print_isl(bmap, p, 0, 0);
919 else if (p->output_format == ISL_FORMAT_OMEGA)
920 return isl_basic_map_print_omega(bmap, p);
921 isl_assert(bmap->ctx, 0, goto error);
922 error:
923 isl_printer_free(p);
924 return NULL;
927 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
928 const char *prefix, const char *suffix, unsigned output_format)
930 isl_printer *printer;
932 if (!bmap)
933 return;
935 printer = isl_printer_to_file(bmap->ctx, out);
936 printer = isl_printer_set_indent(printer, indent);
937 printer = isl_printer_set_prefix(printer, prefix);
938 printer = isl_printer_set_suffix(printer, suffix);
939 printer = isl_printer_set_output_format(printer, output_format);
940 isl_printer_print_basic_map(printer, bmap);
942 isl_printer_free(printer);
945 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
946 __isl_keep isl_basic_set *bset)
948 if (!p || !bset)
949 goto error;
951 if (p->output_format == ISL_FORMAT_ISL)
952 return isl_basic_map_print_isl(bset, p, 1, 0);
953 else if (p->output_format == ISL_FORMAT_POLYLIB)
954 return isl_basic_set_print_polylib(bset, p, 0);
955 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
956 return isl_basic_set_print_polylib(bset, p, 1);
957 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
958 return bset_print_constraints_polylib(bset, p);
959 else if (p->output_format == ISL_FORMAT_OMEGA)
960 return isl_basic_set_print_omega(bset, p);
961 isl_assert(p->ctx, 0, goto error);
962 error:
963 isl_printer_free(p);
964 return NULL;
967 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
968 const char *prefix, const char *suffix, unsigned output_format)
970 isl_printer *printer;
972 if (!bset)
973 return;
975 printer = isl_printer_to_file(bset->ctx, out);
976 printer = isl_printer_set_indent(printer, indent);
977 printer = isl_printer_set_prefix(printer, prefix);
978 printer = isl_printer_set_suffix(printer, suffix);
979 printer = isl_printer_set_output_format(printer, output_format);
980 isl_printer_print_basic_set(printer, bset);
982 isl_printer_free(printer);
985 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
986 __isl_keep isl_set *set)
988 if (!p || !set)
989 goto error;
990 if (p->output_format == ISL_FORMAT_ISL)
991 return isl_map_print_isl((isl_map *)set, p, 1);
992 else if (p->output_format == ISL_FORMAT_POLYLIB)
993 return isl_set_print_polylib(set, p, 0);
994 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
995 return isl_set_print_polylib(set, p, 1);
996 else if (p->output_format == ISL_FORMAT_OMEGA)
997 return isl_set_print_omega(set, p);
998 else if (p->output_format == ISL_FORMAT_LATEX)
999 return isl_map_print_latex((isl_map *)set, p, 1);
1000 isl_assert(set->ctx, 0, goto error);
1001 error:
1002 isl_printer_free(p);
1003 return NULL;
1006 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1007 unsigned output_format)
1009 isl_printer *printer;
1011 if (!set)
1012 return;
1014 printer = isl_printer_to_file(set->ctx, out);
1015 printer = isl_printer_set_indent(printer, indent);
1016 printer = isl_printer_set_output_format(printer, output_format);
1017 printer = isl_printer_print_set(printer, set);
1019 isl_printer_free(printer);
1022 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1023 __isl_keep isl_map *map)
1025 if (!p || !map)
1026 goto error;
1028 if (p->output_format == ISL_FORMAT_ISL)
1029 return isl_map_print_isl(map, p, 0);
1030 else if (p->output_format == ISL_FORMAT_POLYLIB)
1031 return isl_map_print_polylib(map, p, 0);
1032 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1033 return isl_map_print_polylib(map, p, 1);
1034 else if (p->output_format == ISL_FORMAT_OMEGA)
1035 return isl_map_print_omega(map, p);
1036 else if (p->output_format == ISL_FORMAT_LATEX)
1037 return isl_map_print_latex(map, p, 0);
1038 isl_assert(map->ctx, 0, goto error);
1039 error:
1040 isl_printer_free(p);
1041 return NULL;
1044 struct isl_union_print_data {
1045 isl_printer *p;
1046 int set;
1047 int first;
1050 static int print_map_body(__isl_take isl_map *map, void *user)
1052 struct isl_union_print_data *data;
1053 data = (struct isl_union_print_data *)user;
1055 if (!data->first)
1056 data->p = isl_printer_print_str(data->p, "; ");
1057 data->first = 0;
1059 data->p = isl_map_print_isl_body(map, data->p, data->set);
1060 isl_map_free(map);
1062 return 0;
1065 static __isl_give isl_printer *isl_union_map_print_isl(
1066 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1068 struct isl_union_print_data data = { p, set, 1 };
1069 isl_dim *dim;
1070 dim = isl_union_map_get_dim(umap);
1071 if (isl_dim_size(dim, isl_dim_param) > 0) {
1072 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1073 p = isl_printer_print_str(p, s_to[0]);
1075 isl_dim_free(dim);
1076 p = isl_printer_print_str(p, s_open_set[0]);
1077 isl_union_map_foreach_map(umap, &print_map_body, &data);
1078 p = data.p;
1079 p = isl_printer_print_str(p, s_close_set[0]);
1080 return p;
1083 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1085 struct isl_union_print_data *data;
1086 data = (struct isl_union_print_data *)user;
1088 if (!data->first)
1089 data->p = isl_printer_print_str(data->p, " \\cup ");
1090 data->first = 0;
1092 data->p = isl_map_print_latex(map, data->p, data->set);
1093 isl_map_free(map);
1095 return 0;
1098 static __isl_give isl_printer *isl_union_map_print_latex(
1099 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1101 struct isl_union_print_data data = { p, set, 1 };
1102 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1103 p = data.p;
1104 return p;
1107 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1108 __isl_keep isl_union_map *umap)
1110 if (!p || !umap)
1111 goto error;
1113 if (p->output_format == ISL_FORMAT_ISL)
1114 return isl_union_map_print_isl(umap, p, 0);
1115 if (p->output_format == ISL_FORMAT_LATEX)
1116 return isl_union_map_print_latex(umap, p, 0);
1118 isl_die(p->ctx, isl_error_invalid,
1119 "invalid output format for isl_union_map", goto error);
1120 error:
1121 isl_printer_free(p);
1122 return NULL;
1125 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1126 __isl_keep isl_union_set *uset)
1128 if (!p || !uset)
1129 goto error;
1131 if (p->output_format == ISL_FORMAT_ISL)
1132 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1133 if (p->output_format == ISL_FORMAT_LATEX)
1134 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1136 isl_die(p->ctx, isl_error_invalid,
1137 "invalid output format for isl_union_set", goto error);
1138 error:
1139 isl_printer_free(p);
1140 return NULL;
1143 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1144 unsigned output_format)
1146 isl_printer *printer;
1148 if (!map)
1149 return;
1151 printer = isl_printer_to_file(map->ctx, out);
1152 printer = isl_printer_set_indent(printer, indent);
1153 printer = isl_printer_set_output_format(printer, output_format);
1154 printer = isl_printer_print_map(printer, map);
1156 isl_printer_free(printer);
1159 void isl_basic_map_dump(__isl_keep isl_basic_map *bmap)
1161 isl_printer *printer;
1163 if (!bmap)
1164 return;
1166 printer = isl_printer_to_file(isl_basic_map_get_ctx(bmap), stderr);
1167 printer = isl_printer_print_basic_map(printer, bmap);
1168 printer = isl_printer_end_line(printer);
1170 isl_printer_free(printer);
1173 void isl_basic_set_dump(__isl_keep isl_basic_set *bset)
1175 isl_printer *printer;
1177 if (!bset)
1178 return;
1180 printer = isl_printer_to_file(isl_basic_set_get_ctx(bset), stderr);
1181 printer = isl_printer_print_basic_set(printer, bset);
1182 printer = isl_printer_end_line(printer);
1184 isl_printer_free(printer);
1187 void isl_map_dump(__isl_keep isl_map *map)
1189 isl_printer *printer;
1191 if (!map)
1192 return;
1194 printer = isl_printer_to_file(isl_map_get_ctx(map), stderr);
1195 printer = isl_printer_print_map(printer, map);
1196 printer = isl_printer_end_line(printer);
1198 isl_printer_free(printer);
1201 void isl_set_dump(__isl_keep isl_set *set)
1203 isl_printer *printer;
1205 if (!set)
1206 return;
1208 printer = isl_printer_to_file(isl_set_get_ctx(set), stderr);
1209 printer = isl_printer_print_set(printer, set);
1210 printer = isl_printer_end_line(printer);
1212 isl_printer_free(printer);
1215 void isl_union_map_dump(__isl_keep isl_union_map *umap)
1217 isl_printer *printer;
1219 if (!umap)
1220 return;
1222 printer = isl_printer_to_file(isl_union_map_get_ctx(umap), stderr);
1223 printer = isl_printer_print_union_map(printer, umap);
1224 printer = isl_printer_end_line(printer);
1226 isl_printer_free(printer);
1229 void isl_union_set_dump(__isl_keep isl_union_set *uset)
1231 isl_printer *printer;
1233 if (!uset)
1234 return;
1236 printer = isl_printer_to_file(isl_union_set_get_ctx(uset), stderr);
1237 printer = isl_printer_print_union_set(printer, uset);
1238 printer = isl_printer_end_line(printer);
1240 isl_printer_free(printer);
1243 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial *upwqp)
1245 isl_printer *printer;
1247 if (!upwqp)
1248 return;
1250 printer = isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp),
1251 stderr);
1252 printer = isl_printer_print_union_pw_qpolynomial(printer, upwqp);
1253 printer = isl_printer_end_line(printer);
1255 isl_printer_free(printer);
1258 void isl_qpolynomial_dump(__isl_keep isl_qpolynomial *qp)
1260 isl_printer *printer;
1262 if (!qp)
1263 return;
1265 printer = isl_printer_to_file(isl_qpolynomial_get_ctx(qp), stderr);
1266 printer = isl_printer_print_qpolynomial(printer, qp);
1267 printer = isl_printer_end_line(printer);
1269 isl_printer_free(printer);
1272 void isl_qpolynomial_fold_dump(__isl_keep isl_qpolynomial_fold *fold)
1274 isl_printer *printer;
1276 if (!fold)
1277 return;
1279 printer = isl_printer_to_file(isl_qpolynomial_fold_get_ctx(fold),
1280 stderr);
1281 printer = isl_printer_print_qpolynomial_fold(printer, fold);
1282 printer = isl_printer_end_line(printer);
1284 isl_printer_free(printer);
1287 void isl_pw_qpolynomial_dump(__isl_keep isl_pw_qpolynomial *pwqp)
1289 isl_printer *printer;
1291 if (!pwqp)
1292 return;
1294 printer = isl_printer_to_file(isl_pw_qpolynomial_get_ctx(pwqp), stderr);
1295 printer = isl_printer_print_pw_qpolynomial(printer, pwqp);
1296 printer = isl_printer_end_line(printer);
1298 isl_printer_free(printer);
1301 void isl_pw_qpolynomial_fold_dump(__isl_keep isl_pw_qpolynomial_fold *pwf)
1303 isl_printer *printer;
1305 if (!pwf)
1306 return;
1308 printer = isl_printer_to_file(
1309 isl_pw_qpolynomial_fold_get_ctx(pwf), stderr);
1310 printer = isl_printer_print_pw_qpolynomial_fold(printer, pwf);
1311 printer = isl_printer_end_line(printer);
1313 isl_printer_free(printer);
1316 void isl_union_pw_qpolynomial_fold_dump(
1317 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1319 isl_printer *printer;
1321 if (!upwf)
1322 return;
1324 printer = isl_printer_to_file(
1325 isl_union_pw_qpolynomial_fold_get_ctx(upwf), stderr);
1326 printer = isl_printer_print_union_pw_qpolynomial_fold(printer, upwf);
1327 printer = isl_printer_end_line(printer);
1329 isl_printer_free(printer);
1332 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1334 int i;
1335 int n;
1337 for (i = 0, n = 0; i < rec->n; ++i)
1338 if (!isl_upoly_is_zero(rec->p[i]))
1339 ++n;
1341 return n;
1344 static __isl_give isl_printer *print_div(__isl_keep isl_dim *dim,
1345 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1347 int c = p->output_format == ISL_FORMAT_C;
1348 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1349 p = print_affine_of_len(dim, div, p,
1350 div->row[pos] + 1, div->n_col - 1, 1);
1351 p = isl_printer_print_str(p, c ? ", " : ")/");
1352 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1353 p = isl_printer_print_str(p, c ? ")" : "]");
1354 return p;
1357 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1358 __isl_take isl_printer *p, int first)
1360 struct isl_upoly_cst *cst;
1361 int neg;
1363 cst = isl_upoly_as_cst(up);
1364 if (!cst)
1365 goto error;
1366 neg = !first && isl_int_is_neg(cst->n);
1367 if (!first)
1368 p = isl_printer_print_str(p, neg ? " - " : " + ");
1369 if (neg)
1370 isl_int_neg(cst->n, cst->n);
1371 if (isl_int_is_zero(cst->d)) {
1372 int sgn = isl_int_sgn(cst->n);
1373 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1374 sgn == 0 ? "NaN" : "infty");
1375 } else
1376 p = isl_printer_print_isl_int(p, cst->n);
1377 if (neg)
1378 isl_int_neg(cst->n, cst->n);
1379 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1380 p = isl_printer_print_str(p, "/");
1381 p = isl_printer_print_isl_int(p, cst->d);
1383 return p;
1384 error:
1385 isl_printer_free(p);
1386 return NULL;
1389 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1390 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var)
1392 unsigned total;
1394 total = isl_dim_total(dim);
1395 if (var < total)
1396 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 1, 0);
1397 else
1398 p = print_div(dim, div, var - total, p);
1399 return p;
1402 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1403 __isl_keep isl_dim *dim, __isl_keep isl_mat *div, int var, int exp)
1405 p = print_base(p, dim, div, var);
1406 if (exp == 1)
1407 return p;
1408 if (p->output_format == ISL_FORMAT_C) {
1409 int i;
1410 for (i = 1; i < exp; ++i) {
1411 p = isl_printer_print_str(p, "*");
1412 p = print_base(p, dim, div, var);
1414 } else {
1415 p = isl_printer_print_str(p, "^");
1416 p = isl_printer_print_int(p, exp);
1418 return p;
1421 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1422 __isl_keep isl_dim *dim, __isl_keep isl_mat *div,
1423 __isl_take isl_printer *p, int outer)
1425 int i, n, first, print_parens;
1426 struct isl_upoly_rec *rec;
1428 if (!p || !up || !dim || !div)
1429 goto error;
1431 if (isl_upoly_is_cst(up))
1432 return upoly_print_cst(up, p, 1);
1434 rec = isl_upoly_as_rec(up);
1435 if (!rec)
1436 goto error;
1437 n = upoly_rec_n_non_zero(rec);
1438 print_parens = n > 1 || (outer && rec->up.var >= isl_dim_total(dim));
1439 if (print_parens)
1440 p = isl_printer_print_str(p, "(");
1441 for (i = 0, first = 1; i < rec->n; ++i) {
1442 if (isl_upoly_is_zero(rec->p[i]))
1443 continue;
1444 if (isl_upoly_is_negone(rec->p[i])) {
1445 if (!i)
1446 p = isl_printer_print_str(p, "-1");
1447 else if (first)
1448 p = isl_printer_print_str(p, "-");
1449 else
1450 p = isl_printer_print_str(p, " - ");
1451 } else if (isl_upoly_is_cst(rec->p[i]) &&
1452 !isl_upoly_is_one(rec->p[i]))
1453 p = upoly_print_cst(rec->p[i], p, first);
1454 else {
1455 if (!first)
1456 p = isl_printer_print_str(p, " + ");
1457 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1458 p = upoly_print(rec->p[i], dim, div, p, 0);
1460 first = 0;
1461 if (i == 0)
1462 continue;
1463 if (!isl_upoly_is_one(rec->p[i]) &&
1464 !isl_upoly_is_negone(rec->p[i]))
1465 p = isl_printer_print_str(p, " * ");
1466 p = print_pow(p, dim, div, rec->up.var, i);
1468 if (print_parens)
1469 p = isl_printer_print_str(p, ")");
1470 return p;
1471 error:
1472 isl_printer_free(p);
1473 return NULL;
1476 __isl_give isl_printer *isl_printer_print_qpolynomial(__isl_take isl_printer *p,
1477 __isl_keep isl_qpolynomial *qp)
1479 if (!p || !qp)
1480 goto error;
1481 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1482 return p;
1483 error:
1484 isl_printer_free(p);
1485 return NULL;
1488 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1489 unsigned output_format)
1491 isl_printer *p;
1493 if (!qp)
1494 return;
1496 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1497 p = isl_printer_to_file(qp->dim->ctx, out);
1498 p = isl_printer_print_qpolynomial(p, qp);
1499 isl_printer_free(p);
1502 static __isl_give isl_printer *qpolynomial_fold_print(
1503 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1505 int i;
1507 if (fold->type == isl_fold_min)
1508 p = isl_printer_print_str(p, "min");
1509 else if (fold->type == isl_fold_max)
1510 p = isl_printer_print_str(p, "max");
1511 p = isl_printer_print_str(p, "(");
1512 for (i = 0; i < fold->n; ++i) {
1513 if (i)
1514 p = isl_printer_print_str(p, ", ");
1515 p = isl_printer_print_qpolynomial(p, fold->qp[i]);
1517 p = isl_printer_print_str(p, ")");
1518 return p;
1521 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1522 FILE *out, unsigned output_format)
1524 isl_printer *p;
1526 if (!fold)
1527 return;
1529 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1531 p = isl_printer_to_file(fold->dim->ctx, out);
1532 p = isl_printer_print_qpolynomial_fold(p, fold);
1534 isl_printer_free(p);
1537 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1538 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1540 int i = 0;
1542 for (i = 0; i < pwqp->n; ++i) {
1543 if (i)
1544 p = isl_printer_print_str(p, "; ");
1545 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1546 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1547 p = print_dim(pwqp->p[i].set->dim, p, 1, 0, 0, NULL);
1548 p = isl_printer_print_str(p, " -> ");
1550 p = isl_printer_print_qpolynomial(p, pwqp->p[i].qp);
1551 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1554 return p;
1557 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1558 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1560 if (!p || !pwqp)
1561 goto error;
1563 if (isl_dim_size(pwqp->dim, isl_dim_param) > 0) {
1564 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1565 p = isl_printer_print_str(p, " -> ");
1567 p = isl_printer_print_str(p, "{ ");
1568 if (pwqp->n == 0) {
1569 if (isl_dim_size(pwqp->dim, isl_dim_set) > 0 ||
1570 isl_dim_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1571 p = print_dim(pwqp->dim, p, 1, 0, 0, NULL);
1572 p = isl_printer_print_str(p, " -> ");
1574 p = isl_printer_print_str(p, "0");
1576 p = isl_pwqp_print_isl_body(p, pwqp);
1577 p = isl_printer_print_str(p, " }");
1578 return p;
1579 error:
1580 isl_printer_free(p);
1581 return NULL;
1584 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1585 unsigned output_format)
1587 isl_printer *p;
1589 if (!pwqp)
1590 return;
1592 p = isl_printer_to_file(pwqp->dim->ctx, out);
1593 p = isl_printer_set_output_format(p, output_format);
1594 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1596 isl_printer_free(p);
1599 static __isl_give isl_printer *isl_pwf_print_isl_body(
1600 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1602 int i = 0;
1604 for (i = 0; i < pwf->n; ++i) {
1605 if (i)
1606 p = isl_printer_print_str(p, "; ");
1607 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1608 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1609 p = print_dim(pwf->p[i].set->dim, p, 1, 0, 0, NULL);
1610 p = isl_printer_print_str(p, " -> ");
1612 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1613 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1616 return p;
1619 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1620 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1622 if (isl_dim_size(pwf->dim, isl_dim_param) > 0) {
1623 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1624 p = isl_printer_print_str(p, " -> ");
1626 p = isl_printer_print_str(p, "{ ");
1627 if (pwf->n == 0) {
1628 if (isl_dim_size(pwf->dim, isl_dim_set) > 0 ||
1629 isl_dim_is_named_or_nested(pwf->dim, isl_dim_set)) {
1630 p = print_dim(pwf->dim, p, 1, 0, 0, NULL);
1631 p = isl_printer_print_str(p, " -> ");
1633 p = isl_printer_print_str(p, "0");
1635 p = isl_pwf_print_isl_body(p, pwf);
1636 p = isl_printer_print_str(p, " }");
1637 return p;
1640 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1641 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1643 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1644 __isl_keep isl_dim *dim,
1645 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1647 if (type == isl_dim_div) {
1648 p = isl_printer_print_str(p, "floord(");
1649 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1650 p = isl_printer_print_str(p, ", ");
1651 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1652 p = isl_printer_print_str(p, ")");
1653 } else {
1654 const char *name;
1656 name = isl_dim_get_name(dim, type, pos);
1657 if (!name)
1658 name = "UNNAMED";
1659 p = isl_printer_print_str(p, name);
1661 return p;
1664 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1665 __isl_keep isl_dim *dim,
1666 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1668 enum isl_dim_type type;
1669 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
1671 if (pos == 0)
1672 return isl_printer_print_isl_int(p, c);
1674 if (isl_int_is_one(c))
1676 else if (isl_int_is_negone(c))
1677 p = isl_printer_print_str(p, "-");
1678 else {
1679 p = isl_printer_print_isl_int(p, c);
1680 p = isl_printer_print_str(p, "*");
1682 type = pos2type(dim, &pos);
1683 p = print_name_c(p, dim, bset, type, pos);
1684 return p;
1687 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1688 __isl_keep isl_dim *dim,
1689 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1691 int i;
1692 int first;
1694 for (i = 0, first = 1; i < len; ++i) {
1695 int flip = 0;
1696 if (isl_int_is_zero(c[i]))
1697 continue;
1698 if (!first) {
1699 if (isl_int_is_neg(c[i])) {
1700 flip = 1;
1701 isl_int_neg(c[i], c[i]);
1702 p = isl_printer_print_str(p, " - ");
1703 } else
1704 p = isl_printer_print_str(p, " + ");
1706 first = 0;
1707 p = print_term_c(p, dim, bset, c[i], i);
1708 if (flip)
1709 isl_int_neg(c[i], c[i]);
1711 if (first)
1712 p = isl_printer_print_str(p, "0");
1713 return p;
1716 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1717 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1719 unsigned len = 1 + isl_basic_set_total_dim(bset);
1720 return print_partial_affine_c(p, dim, bset, c, len);
1723 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1724 __isl_keep isl_dim *dim,
1725 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1727 if (!first)
1728 p = isl_printer_print_str(p, " && ");
1730 p = print_affine_c(p, dim, bset, c);
1731 p = isl_printer_print_str(p, " ");
1732 p = isl_printer_print_str(p, op);
1733 p = isl_printer_print_str(p, " 0");
1734 return p;
1737 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1738 __isl_keep isl_dim *dim, __isl_keep isl_basic_set *bset)
1740 int i, j;
1741 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1742 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1744 for (i = 0; i < bset->n_eq; ++i) {
1745 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1746 if (j < 0)
1747 p = print_constraint_c(p, dim, bset,
1748 bset->eq[i], "==", !i);
1749 else {
1750 if (i)
1751 p = isl_printer_print_str(p, " && ");
1752 p = isl_printer_print_str(p, "(");
1753 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1754 1 + total + j);
1755 p = isl_printer_print_str(p, ") % ");
1756 p = isl_printer_print_isl_int(p,
1757 bset->eq[i][1 + total + j]);
1758 p = isl_printer_print_str(p, " == 0");
1761 for (i = 0; i < bset->n_ineq; ++i)
1762 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1763 !bset->n_eq && !i);
1764 return p;
1767 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1768 __isl_keep isl_dim *dim, __isl_keep isl_set *set)
1770 int i;
1772 if (set->n == 0)
1773 p = isl_printer_print_str(p, "0");
1775 for (i = 0; i < set->n; ++i) {
1776 if (i)
1777 p = isl_printer_print_str(p, " || ");
1778 if (set->n > 1)
1779 p = isl_printer_print_str(p, "(");
1780 p = print_basic_set_c(p, dim, set->p[i]);
1781 if (set->n > 1)
1782 p = isl_printer_print_str(p, ")");
1784 return p;
1787 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1788 __isl_keep isl_dim *dim, __isl_keep isl_qpolynomial *qp)
1790 isl_int den;
1792 isl_int_init(den);
1793 isl_qpolynomial_get_den(qp, &den);
1794 if (!isl_int_is_one(den)) {
1795 isl_qpolynomial *f;
1796 p = isl_printer_print_str(p, "(");
1797 qp = isl_qpolynomial_copy(qp);
1798 f = isl_qpolynomial_rat_cst(isl_dim_copy(qp->dim),
1799 den, qp->dim->ctx->one);
1800 qp = isl_qpolynomial_mul(qp, f);
1802 if (qp)
1803 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1804 if (!isl_int_is_one(den)) {
1805 p = isl_printer_print_str(p, ")/");
1806 p = isl_printer_print_isl_int(p, den);
1807 isl_qpolynomial_free(qp);
1809 isl_int_clear(den);
1810 return p;
1813 static __isl_give isl_printer *print_pw_qpolynomial_c(
1814 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1816 int i;
1818 if (pwqp->n == 1 && isl_set_fast_is_universe(pwqp->p[0].set))
1819 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1821 for (i = 0; i < pwqp->n; ++i) {
1822 p = isl_printer_print_str(p, "(");
1823 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1824 p = isl_printer_print_str(p, ") ? (");
1825 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1826 p = isl_printer_print_str(p, ") : ");
1829 p = isl_printer_print_str(p, "0");
1830 return p;
1833 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1834 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1836 if (!p || !pwqp)
1837 goto error;
1839 if (p->output_format == ISL_FORMAT_ISL)
1840 return print_pw_qpolynomial_isl(p, pwqp);
1841 else if (p->output_format == ISL_FORMAT_C)
1842 return print_pw_qpolynomial_c(p, pwqp);
1843 isl_assert(p->ctx, 0, goto error);
1844 error:
1845 isl_printer_free(p);
1846 return NULL;
1849 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1851 struct isl_union_print_data *data;
1852 data = (struct isl_union_print_data *)user;
1854 if (!data->first)
1855 data->p = isl_printer_print_str(data->p, "; ");
1856 data->first = 0;
1858 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1859 isl_pw_qpolynomial_free(pwqp);
1861 return 0;
1864 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1865 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1867 struct isl_union_print_data data = { p, 1, 1 };
1868 isl_dim *dim;
1869 dim = isl_union_pw_qpolynomial_get_dim(upwqp);
1870 if (isl_dim_size(dim, isl_dim_param) > 0) {
1871 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1872 p = isl_printer_print_str(p, " -> ");
1874 isl_dim_free(dim);
1875 p = isl_printer_print_str(p, "{ ");
1876 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1877 &data);
1878 p = data.p;
1879 p = isl_printer_print_str(p, " }");
1880 return p;
1883 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1884 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1886 if (!p || !upwqp)
1887 goto error;
1889 if (p->output_format == ISL_FORMAT_ISL)
1890 return print_union_pw_qpolynomial_isl(p, upwqp);
1891 isl_die(p->ctx, isl_error_invalid,
1892 "invalid output format for isl_union_pw_qpolynomial",
1893 goto error);
1894 error:
1895 isl_printer_free(p);
1896 return NULL;
1899 static __isl_give isl_printer *print_qpolynomial_fold_c(
1900 __isl_take isl_printer *p, __isl_keep isl_dim *dim,
1901 __isl_keep isl_qpolynomial_fold *fold)
1903 int i;
1905 for (i = 0; i < fold->n - 1; ++i)
1906 if (fold->type == isl_fold_min)
1907 p = isl_printer_print_str(p, "min(");
1908 else if (fold->type == isl_fold_max)
1909 p = isl_printer_print_str(p, "max(");
1911 for (i = 0; i < fold->n; ++i) {
1912 if (i)
1913 p = isl_printer_print_str(p, ", ");
1914 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1915 if (i)
1916 p = isl_printer_print_str(p, ")");
1918 return p;
1921 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1922 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1924 if (!p || !fold)
1925 goto error;
1926 if (p->output_format == ISL_FORMAT_ISL)
1927 return qpolynomial_fold_print(fold, p);
1928 else if (p->output_format == ISL_FORMAT_C)
1929 return print_qpolynomial_fold_c(p, fold->dim, fold);
1930 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1931 goto error);
1932 error:
1933 isl_printer_free(p);
1934 return NULL;
1937 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1938 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1940 int i;
1942 if (pwf->n == 1 && isl_set_fast_is_universe(pwf->p[0].set))
1943 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1945 for (i = 0; i < pwf->n; ++i) {
1946 p = isl_printer_print_str(p, "(");
1947 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1948 p = isl_printer_print_str(p, ") ? (");
1949 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1950 p = isl_printer_print_str(p, ") : ");
1953 p = isl_printer_print_str(p, "0");
1954 return p;
1957 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1958 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1960 if (!p || !pwf)
1961 goto error;
1963 if (p->output_format == ISL_FORMAT_ISL)
1964 return print_pw_qpolynomial_fold_isl(p, pwf);
1965 else if (p->output_format == ISL_FORMAT_C)
1966 return print_pw_qpolynomial_fold_c(p, pwf);
1967 isl_assert(p->ctx, 0, goto error);
1968 error:
1969 isl_printer_free(p);
1970 return NULL;
1973 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1974 FILE *out, unsigned output_format)
1976 isl_printer *p;
1978 if (!pwf)
1979 return;
1981 p = isl_printer_to_file(pwf->dim->ctx, out);
1982 p = isl_printer_set_output_format(p, output_format);
1983 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1985 isl_printer_free(p);
1988 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1990 struct isl_union_print_data *data;
1991 data = (struct isl_union_print_data *)user;
1993 if (!data->first)
1994 data->p = isl_printer_print_str(data->p, "; ");
1995 data->first = 0;
1997 data->p = isl_pwf_print_isl_body(data->p, pwf);
1998 isl_pw_qpolynomial_fold_free(pwf);
2000 return 0;
2003 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
2004 __isl_take isl_printer *p,
2005 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2007 struct isl_union_print_data data = { p, 1, 1 };
2008 isl_dim *dim;
2009 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
2010 if (isl_dim_size(dim, isl_dim_param) > 0) {
2011 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
2012 p = isl_printer_print_str(p, " -> ");
2014 isl_dim_free(dim);
2015 p = isl_printer_print_str(p, "{ ");
2016 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
2017 &print_pwf_body, &data);
2018 p = data.p;
2019 p = isl_printer_print_str(p, " }");
2020 return p;
2023 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
2024 __isl_take isl_printer *p,
2025 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
2027 if (!p || !upwf)
2028 goto error;
2030 if (p->output_format == ISL_FORMAT_ISL)
2031 return print_union_pw_qpolynomial_fold_isl(p, upwf);
2032 isl_die(p->ctx, isl_error_invalid,
2033 "invalid output format for isl_union_pw_qpolynomial_fold",
2034 goto error);
2035 error:
2036 isl_printer_free(p);
2037 return NULL;