add isl_map_flat_domain_product
[isl.git] / isl_output.c
blob57a157051d7ebb650cd283d556f176ce0d8f7682
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_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/union_map.h>
24 #include <isl/constraint.h>
25 #include <isl_local_space_private.h>
26 #include <isl_aff_private.h>
28 static const char *s_to[2] = { " -> ", " \\to " };
29 static const char *s_and[2] = { " and ", " \\wedge " };
30 static const char *s_or[2] = { " or ", " \\vee " };
31 static const char *s_le[2] = { "<=", "\\le" };
32 static const char *s_ge[2] = { ">=", "\\ge" };
33 static const char *s_open_set[2] = { "{ ", "\\{\\, " };
34 static const char *s_close_set[2] = { " }", " \\,\\}" };
35 static const char *s_open_list[2] = { "[", "(" };
36 static const char *s_close_list[2] = { "]", ")" };
37 static const char *s_such_that[2] = { " : ", " \\mid " };
38 static const char *s_open_exists[2] = { "exists (", "\\exists \\, " };
39 static const char *s_close_exists[2] = { ")", "" };
40 static const char *s_div_prefix[2] = { "e", "\\alpha_" };
41 static const char *s_param_prefix[2] = { "p", "p_" };
42 static const char *s_input_prefix[2] = { "i", "i_" };
43 static const char *s_output_prefix[2] = { "o", "o_" };
45 static __isl_give isl_printer *print_constraint_polylib(
46 struct isl_basic_map *bmap, int ineq, int n, __isl_take isl_printer *p)
48 int i;
49 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
50 unsigned n_out = isl_basic_map_dim(bmap, isl_dim_out);
51 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
52 isl_int *c = ineq ? bmap->ineq[n] : bmap->eq[n];
54 p = isl_printer_start_line(p);
55 p = isl_printer_print_int(p, ineq);
56 for (i = 0; i < n_out; ++i) {
57 p = isl_printer_print_str(p, " ");
58 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+i]);
60 for (i = 0; i < n_in; ++i) {
61 p = isl_printer_print_str(p, " ");
62 p = isl_printer_print_isl_int(p, c[1+nparam+i]);
64 for (i = 0; i < bmap->n_div; ++i) {
65 p = isl_printer_print_str(p, " ");
66 p = isl_printer_print_isl_int(p, c[1+nparam+n_in+n_out+i]);
68 for (i = 0; i < nparam; ++i) {
69 p = isl_printer_print_str(p, " ");
70 p = isl_printer_print_isl_int(p, c[1+i]);
72 p = isl_printer_print_str(p, " ");
73 p = isl_printer_print_isl_int(p, c[0]);
74 p = isl_printer_end_line(p);
75 return p;
78 static __isl_give isl_printer *print_constraints_polylib(
79 struct isl_basic_map *bmap, __isl_take isl_printer *p)
81 int i;
83 p = isl_printer_set_isl_int_width(p, 5);
85 for (i = 0; i < bmap->n_eq; ++i)
86 p = print_constraint_polylib(bmap, 0, i, p);
87 for (i = 0; i < bmap->n_ineq; ++i)
88 p = print_constraint_polylib(bmap, 1, i, p);
90 return p;
93 static __isl_give isl_printer *bset_print_constraints_polylib(
94 struct isl_basic_set *bset, __isl_take isl_printer *p)
96 return print_constraints_polylib((struct isl_basic_map *)bset, p);
99 static __isl_give isl_printer *isl_basic_map_print_polylib(
100 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int ext)
102 unsigned total = isl_basic_map_total_dim(bmap);
103 p = isl_printer_start_line(p);
104 p = isl_printer_print_int(p, bmap->n_eq + bmap->n_ineq);
105 p = isl_printer_print_str(p, " ");
106 p = isl_printer_print_int(p, 1 + total + 1);
107 if (ext) {
108 p = isl_printer_print_str(p, " ");
109 p = isl_printer_print_int(p,
110 isl_basic_map_dim(bmap, isl_dim_out));
111 p = isl_printer_print_str(p, " ");
112 p = isl_printer_print_int(p,
113 isl_basic_map_dim(bmap, isl_dim_in));
114 p = isl_printer_print_str(p, " ");
115 p = isl_printer_print_int(p,
116 isl_basic_map_dim(bmap, isl_dim_div));
117 p = isl_printer_print_str(p, " ");
118 p = isl_printer_print_int(p,
119 isl_basic_map_dim(bmap, isl_dim_param));
121 p = isl_printer_end_line(p);
122 return print_constraints_polylib(bmap, p);
125 static __isl_give isl_printer *isl_basic_set_print_polylib(
126 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p, int ext)
128 return isl_basic_map_print_polylib((struct isl_basic_map *)bset, p, ext);
131 static __isl_give isl_printer *isl_map_print_polylib(__isl_keep isl_map *map,
132 __isl_take isl_printer *p, int ext)
134 int i;
136 p = isl_printer_start_line(p);
137 p = isl_printer_print_int(p, map->n);
138 p = isl_printer_end_line(p);
139 for (i = 0; i < map->n; ++i) {
140 p = isl_printer_start_line(p);
141 p = isl_printer_end_line(p);
142 p = isl_basic_map_print_polylib(map->p[i], p, ext);
144 return p;
147 static __isl_give isl_printer *isl_set_print_polylib(__isl_keep isl_set *set,
148 __isl_take isl_printer *p, int ext)
150 return isl_map_print_polylib((struct isl_map *)set, p, ext);
153 static int count_same_name(__isl_keep isl_space *dim,
154 enum isl_dim_type type, unsigned pos, const char *name)
156 enum isl_dim_type t;
157 unsigned p, s;
158 int count = 0;
160 for (t = isl_dim_param; t <= type && t <= isl_dim_out; ++t) {
161 s = t == type ? pos : isl_space_dim(dim, t);
162 for (p = 0; p < s; ++p) {
163 const char *n = isl_space_get_dim_name(dim, t, p);
164 if (n && !strcmp(n, name))
165 count++;
168 return count;
171 static __isl_give isl_printer *print_name(__isl_keep isl_space *dim,
172 __isl_take isl_printer *p, enum isl_dim_type type, unsigned pos,
173 int set, int latex)
175 const char *name;
176 char buffer[20];
177 int primes;
179 name = type == isl_dim_div ? NULL : isl_space_get_dim_name(dim, type, pos);
181 if (!name) {
182 const char *prefix;
183 if (type == isl_dim_param)
184 prefix = s_param_prefix[latex];
185 else if (type == isl_dim_div)
186 prefix = s_div_prefix[latex];
187 else if (set || type == isl_dim_in)
188 prefix = s_input_prefix[latex];
189 else
190 prefix = s_output_prefix[latex];
191 snprintf(buffer, sizeof(buffer), "%s%d", prefix, pos);
192 name = buffer;
194 primes = count_same_name(dim, name == buffer ? isl_dim_div : type,
195 pos, name);
196 p = isl_printer_print_str(p, name);
197 while (primes-- > 0)
198 p = isl_printer_print_str(p, "'");
199 return p;
202 static enum isl_dim_type pos2type(__isl_keep isl_space *dim, unsigned *pos)
204 enum isl_dim_type type;
205 unsigned n_in = isl_space_dim(dim, isl_dim_in);
206 unsigned n_out = isl_space_dim(dim, isl_dim_out);
207 unsigned nparam = isl_space_dim(dim, isl_dim_param);
209 if (*pos < 1 + nparam) {
210 type = isl_dim_param;
211 *pos -= 1;
212 } else if (*pos < 1 + nparam + n_in) {
213 type = isl_dim_in;
214 *pos -= 1 + nparam;
215 } else if (*pos < 1 + nparam + n_in + n_out) {
216 type = isl_dim_out;
217 *pos -= 1 + nparam + n_in;
218 } else {
219 type = isl_dim_div;
220 *pos -= 1 + nparam + n_in + n_out;
223 return type;
226 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
227 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p);
229 static __isl_give isl_printer *print_term(__isl_keep isl_space *dim,
230 __isl_keep isl_mat *div,
231 isl_int c, unsigned pos, __isl_take isl_printer *p, int set, int latex)
233 enum isl_dim_type type;
234 int print_div_def;
236 if (pos == 0)
237 return isl_printer_print_isl_int(p, c);
239 type = pos2type(dim, &pos);
240 print_div_def = type == isl_dim_div && div &&
241 !isl_int_is_zero(div->row[pos][0]);
243 if (isl_int_is_one(c))
245 else if (isl_int_is_negone(c))
246 p = isl_printer_print_str(p, "-");
247 else {
248 p = isl_printer_print_isl_int(p, c);
249 if (p->output_format == ISL_FORMAT_C || print_div_def)
250 p = isl_printer_print_str(p, "*");
252 if (print_div_def)
253 p = print_div(dim, div, pos, p);
254 else
255 p = print_name(dim, p, type, pos, set, latex);
256 return p;
259 static __isl_give isl_printer *print_affine_of_len(__isl_keep isl_space *dim,
260 __isl_keep isl_mat *div,
261 __isl_take isl_printer *p, isl_int *c, int len, int set)
263 int i;
264 int first;
266 for (i = 0, first = 1; i < len; ++i) {
267 int flip = 0;
268 if (isl_int_is_zero(c[i]))
269 continue;
270 if (!first) {
271 if (isl_int_is_neg(c[i])) {
272 flip = 1;
273 isl_int_neg(c[i], c[i]);
274 p = isl_printer_print_str(p, " - ");
275 } else
276 p = isl_printer_print_str(p, " + ");
278 first = 0;
279 p = print_term(dim, div, c[i], i, p, set, 0);
280 if (flip)
281 isl_int_neg(c[i], c[i]);
283 if (first)
284 p = isl_printer_print_str(p, "0");
285 return p;
288 static __isl_give isl_printer *print_affine(__isl_keep isl_basic_map *bmap,
289 __isl_keep isl_space *dim, __isl_take isl_printer *p, isl_int *c, int set)
291 unsigned len = 1 + isl_basic_map_total_dim(bmap);
292 return print_affine_of_len(dim, NULL, p, c, len, set);
295 static int defining_equality(__isl_keep isl_basic_map *eq,
296 __isl_keep isl_space *dim, enum isl_dim_type type, int pos)
298 int i;
299 unsigned total;
301 if (!eq)
302 return -1;
304 pos += isl_space_offset(dim, type);
305 total = isl_basic_map_total_dim(eq);
307 for (i = 0; i < eq->n_eq; ++i) {
308 if (isl_seq_last_non_zero(eq->eq[i] + 1, total) != pos)
309 continue;
310 if (isl_int_is_one(eq->eq[i][1 + pos]))
311 isl_seq_neg(eq->eq[i], eq->eq[i], 1 + total);
312 return i;
315 return -1;
318 /* offset is the offset of local_dim inside global_type of global_dim.
320 static __isl_give isl_printer *print_nested_var_list(__isl_take isl_printer *p,
321 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
322 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
323 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
325 int i, j;
327 if (global_dim != local_dim && local_type == isl_dim_out)
328 offset += local_dim->n_in;
330 for (i = 0; i < isl_space_dim(local_dim, local_type); ++i) {
331 if (i)
332 p = isl_printer_print_str(p, ", ");
333 j = defining_equality(eq, global_dim, global_type, offset + i);
334 if (j >= 0) {
335 int pos = 1 + isl_space_offset(global_dim, global_type)
336 + offset + i;
337 p = print_affine_of_len(eq->dim, NULL,
338 p, eq->eq[j], pos, set);
339 } else {
340 p = print_name(global_dim, p, global_type, offset + i,
341 set, latex);
344 return p;
347 static __isl_give isl_printer *print_var_list(__isl_keep isl_space *dim,
348 __isl_take isl_printer *p, enum isl_dim_type type,
349 int set, int latex, __isl_keep isl_basic_map *eq)
351 return print_nested_var_list(p, dim, type, dim, type, set, latex, eq, 0);
354 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
355 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
356 __isl_keep isl_space *local_dim,
357 int set, int latex, __isl_keep isl_basic_map *eq, int offset);
359 static __isl_give isl_printer *print_nested_tuple(__isl_take isl_printer *p,
360 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
361 __isl_keep isl_space *local_dim, enum isl_dim_type local_type,
362 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
364 const char *name = NULL;
365 unsigned n = isl_space_dim(local_dim, local_type);
366 if ((local_type == isl_dim_in || local_type == isl_dim_out)) {
367 name = isl_space_get_tuple_name(local_dim, local_type);
368 if (name) {
369 if (latex)
370 p = isl_printer_print_str(p, "\\mathrm{");
371 p = isl_printer_print_str(p, name);
372 if (latex)
373 p = isl_printer_print_str(p, "}");
376 if (!latex || n != 1 || name)
377 p = isl_printer_print_str(p, s_open_list[latex]);
378 if ((local_type == isl_dim_in || local_type == isl_dim_out) &&
379 local_dim->nested[local_type - isl_dim_in]) {
380 if (global_dim != local_dim && local_type == isl_dim_out)
381 offset += local_dim->n_in;
382 p = print_nested_map_dim(p, global_dim, global_type,
383 local_dim->nested[local_type - isl_dim_in],
384 set, latex, eq, offset);
385 } else
386 p = print_nested_var_list(p, global_dim, global_type,
387 local_dim, local_type, set, latex, eq, offset);
388 if (!latex || n != 1 || name)
389 p = isl_printer_print_str(p, s_close_list[latex]);
390 return p;
393 static __isl_give isl_printer *print_tuple(__isl_keep isl_space *dim,
394 __isl_take isl_printer *p, enum isl_dim_type type,
395 int set, int latex, __isl_keep isl_basic_map *eq)
397 return print_nested_tuple(p, dim, type, dim, type, set, latex, eq, 0);
400 static __isl_give isl_printer *print_nested_map_dim(__isl_take isl_printer *p,
401 __isl_keep isl_space *global_dim, enum isl_dim_type global_type,
402 __isl_keep isl_space *local_dim,
403 int set, int latex, __isl_keep isl_basic_map *eq, int offset)
405 p = print_nested_tuple(p, global_dim, global_type,
406 local_dim, isl_dim_in, set, latex, eq, offset);
407 p = isl_printer_print_str(p, s_to[latex]);
408 p = print_nested_tuple(p, global_dim, global_type,
409 local_dim, isl_dim_out, set, latex, eq, offset);
411 return p;
414 static __isl_give isl_printer *print_space(__isl_keep isl_space *dim,
415 __isl_take isl_printer *p, int set, int latex, int rational,
416 __isl_keep isl_basic_map *eq)
418 if (rational && !latex)
419 p = isl_printer_print_str(p, "rat: ");
420 if (set)
421 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
422 else {
423 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
424 p = isl_printer_print_str(p, s_to[latex]);
425 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
428 return p;
431 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
432 __isl_take isl_printer *p)
434 if (isl_space_dim(dim, isl_dim_param) == 0)
435 return p;
437 p = isl_printer_start_line(p);
438 p = isl_printer_print_str(p, "symbolic ");
439 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
440 p = isl_printer_print_str(p, ";");
441 p = isl_printer_end_line(p);
442 return p;
445 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
446 __isl_keep isl_space *dim, __isl_take isl_printer *p,
447 isl_int *c, int last, const char *op, int first_constraint, int set,
448 int latex)
450 if (!first_constraint)
451 p = isl_printer_print_str(p, s_and[latex]);
453 isl_int_abs(c[last], c[last]);
455 p = print_term(dim, NULL, c[last], last, p, set, latex);
457 p = isl_printer_print_str(p, " ");
458 p = isl_printer_print_str(p, op);
459 p = isl_printer_print_str(p, " ");
461 isl_int_set_si(c[last], 0);
462 p = print_affine(bmap, dim, p, c, set);
464 return p;
467 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
468 __isl_keep isl_space *dim, __isl_take isl_printer *p, int set, int latex)
470 int i;
471 struct isl_vec *c;
472 unsigned total = isl_basic_map_total_dim(bmap);
474 c = isl_vec_alloc(bmap->ctx, 1 + total);
475 if (!c)
476 goto error;
478 for (i = bmap->n_eq - 1; i >= 0; --i) {
479 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
480 if (l < 0) {
481 if (i != bmap->n_eq - 1)
482 p = isl_printer_print_str(p, s_and[latex]);
483 p = isl_printer_print_str(p, "0 = 0");
484 continue;
486 if (isl_int_is_neg(bmap->eq[i][l]))
487 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
488 else
489 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
490 p = print_constraint(bmap, dim, p, c->el, l,
491 "=", i == bmap->n_eq - 1, set, latex);
493 for (i = 0; i < bmap->n_ineq; ++i) {
494 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
495 int s;
496 const char *op;
497 if (l < 0)
498 continue;
499 s = isl_int_sgn(bmap->ineq[i][l]);
500 if (s < 0)
501 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
502 else
503 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
504 op = s < 0 ? s_le[latex] : s_ge[latex];
505 p = print_constraint(bmap, dim, p, c->el, l,
506 op, !bmap->n_eq && !i, set, latex);
509 isl_vec_free(c);
511 return p;
512 error:
513 isl_vec_free(c);
514 isl_printer_free(p);
515 return NULL;
518 static __isl_give isl_printer *print_omega_constraints(
519 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
521 if (bmap->n_eq + bmap->n_ineq == 0)
522 return p;
524 p = isl_printer_print_str(p, ": ");
525 if (bmap->n_div > 0) {
526 int i;
527 p = isl_printer_print_str(p, "exists (");
528 for (i = 0; i < bmap->n_div; ++i) {
529 if (i)
530 p = isl_printer_print_str(p, ", ");
531 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
533 p = isl_printer_print_str(p, ": ");
535 p = print_constraints(bmap, bmap->dim, p, set, 0);
536 if (bmap->n_div > 0)
537 p = isl_printer_print_str(p, ")");
538 return p;
541 static __isl_give isl_printer *basic_map_print_omega(
542 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
544 p = isl_printer_print_str(p, "{ [");
545 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
546 p = isl_printer_print_str(p, "] -> [");
547 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
548 p = isl_printer_print_str(p, "] ");
549 p = print_omega_constraints(bmap, p, 0);
550 p = isl_printer_print_str(p, " }");
551 return p;
554 static __isl_give isl_printer *isl_basic_map_print_omega(
555 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
557 p = print_omega_parameters(bmap->dim, p);
559 p = isl_printer_start_line(p);
560 p = basic_map_print_omega(bmap, p);
561 p = isl_printer_end_line(p);
562 return p;
565 static __isl_give isl_printer *basic_set_print_omega(
566 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
568 p = isl_printer_print_str(p, "{ [");
569 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
570 p = isl_printer_print_str(p, "] ");
571 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
572 p = isl_printer_print_str(p, " }");
573 return p;
576 static __isl_give isl_printer *isl_basic_set_print_omega(
577 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
579 p = print_omega_parameters(bset->dim, p);
581 p = isl_printer_start_line(p);
582 p = basic_set_print_omega(bset, p);
583 p = isl_printer_end_line(p);
584 return p;
587 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
588 __isl_take isl_printer *p)
590 int i;
592 p = print_omega_parameters(map->dim, p);
594 p = isl_printer_start_line(p);
595 for (i = 0; i < map->n; ++i) {
596 if (i)
597 p = isl_printer_print_str(p, " union ");
598 p = basic_map_print_omega(map->p[i], p);
600 p = isl_printer_end_line(p);
601 return p;
604 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
605 __isl_take isl_printer *p)
607 int i;
609 p = print_omega_parameters(set->dim, p);
611 p = isl_printer_start_line(p);
612 for (i = 0; i < set->n; ++i) {
613 if (i)
614 p = isl_printer_print_str(p, " union ");
615 p = basic_set_print_omega(set->p[i], p);
617 p = isl_printer_end_line(p);
618 return p;
621 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
622 __isl_keep isl_space *dim, __isl_take isl_printer *p, int set, int latex)
624 if (bmap->n_div > 0) {
625 int i;
626 p = isl_printer_print_str(p, s_open_exists[latex]);
627 for (i = 0; i < bmap->n_div; ++i) {
628 if (i)
629 p = isl_printer_print_str(p, ", ");
630 p = print_name(dim, p, isl_dim_div, i, 0, latex);
631 if (latex || isl_int_is_zero(bmap->div[i][0]))
632 continue;
633 p = isl_printer_print_str(p, " = [(");
634 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
635 p = isl_printer_print_str(p, ")/");
636 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
637 p = isl_printer_print_str(p, "]");
639 p = isl_printer_print_str(p, ": ");
642 p = print_constraints(bmap, dim, p, set, latex);
644 if (bmap->n_div > 0)
645 p = isl_printer_print_str(p, s_close_exists[latex]);
646 return p;
649 static __isl_give isl_printer *isl_basic_map_print_isl(
650 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
651 int set, int latex)
653 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
654 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
655 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
656 p = isl_printer_print_str(p, " -> ");
658 p = isl_printer_print_str(p, "{ ");
659 p = print_space(bmap->dim, p, set, latex, rational, NULL);
660 p = isl_printer_print_str(p, " : ");
661 p = print_disjunct(bmap, bmap->dim, p, set, latex);
662 p = isl_printer_print_str(p, " }");
663 return p;
666 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
667 __isl_take isl_printer *p, int set, int latex)
669 int i;
671 if (isl_map_plain_is_universe(map))
672 return p;
674 p = isl_printer_print_str(p, s_such_that[latex]);
675 if (map->n == 0)
676 p = isl_printer_print_str(p, "1 = 0");
677 for (i = 0; i < map->n; ++i) {
678 if (i)
679 p = isl_printer_print_str(p, s_or[latex]);
680 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
681 p = isl_printer_print_str(p, "(");
682 p = print_disjunct(map->p[i], map->dim, p, set, latex);
683 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
684 p = isl_printer_print_str(p, ")");
686 return p;
689 struct isl_aff_split {
690 isl_basic_map *aff;
691 isl_map *map;
694 static void free_split(__isl_take struct isl_aff_split *split, int n)
696 int i;
698 if (!split)
699 return;
701 for (i = 0; i < n; ++i) {
702 isl_basic_map_free(split[i].aff);
703 isl_map_free(split[i].map);
706 free(split);
709 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
711 int i, j;
712 unsigned nparam, n_in, n_out, total;
714 bmap = isl_basic_map_cow(bmap);
715 if (!bmap)
716 return NULL;
717 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
718 goto error;
720 nparam = isl_basic_map_dim(bmap, isl_dim_param);
721 n_in = isl_basic_map_dim(bmap, isl_dim_in);
722 n_out = isl_basic_map_dim(bmap, isl_dim_out);
723 total = isl_basic_map_dim(bmap, isl_dim_all);
724 for (i = bmap->n_eq - 1; i >= 0; --i) {
725 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
726 if (j >= nparam && j < nparam + n_in + n_out &&
727 (isl_int_is_one(bmap->eq[i][1 + j]) ||
728 isl_int_is_negone(bmap->eq[i][1 + j])))
729 continue;
730 if (isl_basic_map_drop_equality(bmap, i) < 0)
731 goto error;
734 bmap = isl_basic_map_finalize(bmap);
736 return bmap;
737 error:
738 isl_basic_map_free(bmap);
739 return NULL;
742 static int aff_split_cmp(const void *p1, const void *p2)
744 const struct isl_aff_split *s1, *s2;
745 s1 = (const struct isl_aff_split *) p1;
746 s2 = (const struct isl_aff_split *) p2;
748 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
751 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
752 __isl_keep isl_basic_map *aff)
754 int i, j;
755 unsigned total;
757 if (!bmap || !aff)
758 goto error;
760 total = isl_space_dim(bmap->dim, isl_dim_all);
762 for (i = bmap->n_eq - 1; i >= 0; --i) {
763 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
764 bmap->n_div) != -1)
765 continue;
766 for (j = 0; j < aff->n_eq; ++j) {
767 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
768 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
769 continue;
770 if (isl_basic_map_drop_equality(bmap, i) < 0)
771 goto error;
772 break;
776 return bmap;
777 error:
778 isl_basic_map_free(bmap);
779 return NULL;
782 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
784 int i, n;
785 struct isl_aff_split *split;
786 isl_ctx *ctx;
788 ctx = isl_map_get_ctx(map);
789 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
790 if (!split)
791 return NULL;
793 for (i = 0; i < map->n; ++i) {
794 isl_basic_map *bmap;
795 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
796 bmap = isl_basic_map_copy(map->p[i]);
797 bmap = isl_basic_map_cow(bmap);
798 bmap = drop_aff(bmap, split[i].aff);
799 split[i].map = isl_map_from_basic_map(bmap);
800 if (!split[i].aff || !split[i].map)
801 goto error;
804 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
806 n = map->n;
807 for (i = n - 1; i >= 1; --i) {
808 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
809 split[i].aff))
810 continue;
811 isl_basic_map_free(split[i].aff);
812 split[i - 1].map = isl_map_union(split[i - 1].map,
813 split[i].map);
814 if (i != n - 1)
815 split[i] = split[n - 1];
816 split[n - 1].aff = NULL;
817 split[n - 1].map = NULL;
818 --n;
821 return split;
822 error:
823 free_split(split, map->n);
824 return NULL;
827 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
828 struct isl_aff_split *split, int n, int set)
830 int i;
831 int rational;
833 for (i = 0; i < n; ++i) {
834 isl_space *dim;
836 if (!split[i].map)
837 break;
838 dim = split[i].map->dim;
839 rational = split[i].map->n > 0 &&
840 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
841 if (i)
842 p = isl_printer_print_str(p, "; ");
843 p = print_space(dim, p, set, 0, rational, split[i].aff);
844 p = print_disjuncts(split[i].map, p, set, 0);
847 return p;
850 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
851 __isl_take isl_printer *p, int set)
853 struct isl_aff_split *split = NULL;
854 int rational;
856 if (map->n > 0)
857 split = split_aff(map);
858 if (split) {
859 p = print_split_map(p, split, map->n, set);
860 } else {
861 rational = map->n > 0 &&
862 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
863 p = print_space(map->dim, p, set, 0, rational, NULL);
864 p = print_disjuncts(map, p, set, 0);
866 free_split(split, map->n);
867 return p;
870 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
871 __isl_take isl_printer *p, int set)
873 if (isl_map_dim(map, isl_dim_param) > 0) {
874 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
875 p = isl_printer_print_str(p, s_to[0]);
877 p = isl_printer_print_str(p, s_open_set[0]);
878 p = isl_map_print_isl_body(map, p, set);
879 p = isl_printer_print_str(p, s_close_set[0]);
880 return p;
883 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
884 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
886 if (isl_map_dim(map, isl_dim_param) > 0) {
887 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
888 p = isl_printer_print_str(p, s_to[1]);
890 p = isl_printer_print_str(p, s_open_set[1]);
891 p = print_space(map->dim, p, set, 1, 0, aff);
892 p = print_disjuncts(map, p, set, 1);
893 p = isl_printer_print_str(p, s_close_set[1]);
895 return p;
898 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
899 __isl_take isl_printer *p, int set)
901 int i;
902 struct isl_aff_split *split = NULL;
904 if (map->n > 0)
905 split = split_aff(map);
907 if (!split)
908 return print_latex_map(map, p, NULL, set);
910 for (i = 0; i < map->n; ++i) {
911 if (!split[i].map)
912 break;
913 if (i)
914 p = isl_printer_print_str(p, " \\cup ");
915 p = print_latex_map(split[i].map, p, split[i].aff, set);
918 free_split(split, map->n);
919 return p;
922 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
923 __isl_keep isl_basic_map *bmap)
925 if (!p || !bmap)
926 goto error;
927 if (p->output_format == ISL_FORMAT_ISL)
928 return isl_basic_map_print_isl(bmap, p, 0, 0);
929 else if (p->output_format == ISL_FORMAT_OMEGA)
930 return isl_basic_map_print_omega(bmap, p);
931 isl_assert(bmap->ctx, 0, goto error);
932 error:
933 isl_printer_free(p);
934 return NULL;
937 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
938 const char *prefix, const char *suffix, unsigned output_format)
940 isl_printer *printer;
942 if (!bmap)
943 return;
945 printer = isl_printer_to_file(bmap->ctx, out);
946 printer = isl_printer_set_indent(printer, indent);
947 printer = isl_printer_set_prefix(printer, prefix);
948 printer = isl_printer_set_suffix(printer, suffix);
949 printer = isl_printer_set_output_format(printer, output_format);
950 isl_printer_print_basic_map(printer, bmap);
952 isl_printer_free(printer);
955 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
956 __isl_keep isl_basic_set *bset)
958 if (!p || !bset)
959 goto error;
961 if (p->output_format == ISL_FORMAT_ISL)
962 return isl_basic_map_print_isl(bset, p, 1, 0);
963 else if (p->output_format == ISL_FORMAT_POLYLIB)
964 return isl_basic_set_print_polylib(bset, p, 0);
965 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
966 return isl_basic_set_print_polylib(bset, p, 1);
967 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
968 return bset_print_constraints_polylib(bset, p);
969 else if (p->output_format == ISL_FORMAT_OMEGA)
970 return isl_basic_set_print_omega(bset, p);
971 isl_assert(p->ctx, 0, goto error);
972 error:
973 isl_printer_free(p);
974 return NULL;
977 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
978 const char *prefix, const char *suffix, unsigned output_format)
980 isl_printer *printer;
982 if (!bset)
983 return;
985 printer = isl_printer_to_file(bset->ctx, out);
986 printer = isl_printer_set_indent(printer, indent);
987 printer = isl_printer_set_prefix(printer, prefix);
988 printer = isl_printer_set_suffix(printer, suffix);
989 printer = isl_printer_set_output_format(printer, output_format);
990 isl_printer_print_basic_set(printer, bset);
992 isl_printer_free(printer);
995 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
996 __isl_keep isl_set *set)
998 if (!p || !set)
999 goto error;
1000 if (p->output_format == ISL_FORMAT_ISL)
1001 return isl_map_print_isl((isl_map *)set, p, 1);
1002 else if (p->output_format == ISL_FORMAT_POLYLIB)
1003 return isl_set_print_polylib(set, p, 0);
1004 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1005 return isl_set_print_polylib(set, p, 1);
1006 else if (p->output_format == ISL_FORMAT_OMEGA)
1007 return isl_set_print_omega(set, p);
1008 else if (p->output_format == ISL_FORMAT_LATEX)
1009 return isl_map_print_latex((isl_map *)set, p, 1);
1010 isl_assert(set->ctx, 0, goto error);
1011 error:
1012 isl_printer_free(p);
1013 return NULL;
1016 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1017 unsigned output_format)
1019 isl_printer *printer;
1021 if (!set)
1022 return;
1024 printer = isl_printer_to_file(set->ctx, out);
1025 printer = isl_printer_set_indent(printer, indent);
1026 printer = isl_printer_set_output_format(printer, output_format);
1027 printer = isl_printer_print_set(printer, set);
1029 isl_printer_free(printer);
1032 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1033 __isl_keep isl_map *map)
1035 if (!p || !map)
1036 goto error;
1038 if (p->output_format == ISL_FORMAT_ISL)
1039 return isl_map_print_isl(map, p, 0);
1040 else if (p->output_format == ISL_FORMAT_POLYLIB)
1041 return isl_map_print_polylib(map, p, 0);
1042 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1043 return isl_map_print_polylib(map, p, 1);
1044 else if (p->output_format == ISL_FORMAT_OMEGA)
1045 return isl_map_print_omega(map, p);
1046 else if (p->output_format == ISL_FORMAT_LATEX)
1047 return isl_map_print_latex(map, p, 0);
1048 isl_assert(map->ctx, 0, goto error);
1049 error:
1050 isl_printer_free(p);
1051 return NULL;
1054 struct isl_union_print_data {
1055 isl_printer *p;
1056 int set;
1057 int first;
1060 static int print_map_body(__isl_take isl_map *map, void *user)
1062 struct isl_union_print_data *data;
1063 data = (struct isl_union_print_data *)user;
1065 if (!data->first)
1066 data->p = isl_printer_print_str(data->p, "; ");
1067 data->first = 0;
1069 data->p = isl_map_print_isl_body(map, data->p, data->set);
1070 isl_map_free(map);
1072 return 0;
1075 static __isl_give isl_printer *isl_union_map_print_isl(
1076 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1078 struct isl_union_print_data data = { p, set, 1 };
1079 isl_space *dim;
1080 dim = isl_union_map_get_space(umap);
1081 if (isl_space_dim(dim, isl_dim_param) > 0) {
1082 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1083 p = isl_printer_print_str(p, s_to[0]);
1085 isl_space_free(dim);
1086 p = isl_printer_print_str(p, s_open_set[0]);
1087 isl_union_map_foreach_map(umap, &print_map_body, &data);
1088 p = data.p;
1089 p = isl_printer_print_str(p, s_close_set[0]);
1090 return p;
1093 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1095 struct isl_union_print_data *data;
1096 data = (struct isl_union_print_data *)user;
1098 if (!data->first)
1099 data->p = isl_printer_print_str(data->p, " \\cup ");
1100 data->first = 0;
1102 data->p = isl_map_print_latex(map, data->p, data->set);
1103 isl_map_free(map);
1105 return 0;
1108 static __isl_give isl_printer *isl_union_map_print_latex(
1109 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1111 struct isl_union_print_data data = { p, set, 1 };
1112 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1113 p = data.p;
1114 return p;
1117 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1118 __isl_keep isl_union_map *umap)
1120 if (!p || !umap)
1121 goto error;
1123 if (p->output_format == ISL_FORMAT_ISL)
1124 return isl_union_map_print_isl(umap, p, 0);
1125 if (p->output_format == ISL_FORMAT_LATEX)
1126 return isl_union_map_print_latex(umap, p, 0);
1128 isl_die(p->ctx, isl_error_invalid,
1129 "invalid output format for isl_union_map", goto error);
1130 error:
1131 isl_printer_free(p);
1132 return NULL;
1135 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1136 __isl_keep isl_union_set *uset)
1138 if (!p || !uset)
1139 goto error;
1141 if (p->output_format == ISL_FORMAT_ISL)
1142 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1143 if (p->output_format == ISL_FORMAT_LATEX)
1144 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1146 isl_die(p->ctx, isl_error_invalid,
1147 "invalid output format for isl_union_set", goto error);
1148 error:
1149 isl_printer_free(p);
1150 return NULL;
1153 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1154 unsigned output_format)
1156 isl_printer *printer;
1158 if (!map)
1159 return;
1161 printer = isl_printer_to_file(map->ctx, out);
1162 printer = isl_printer_set_indent(printer, indent);
1163 printer = isl_printer_set_output_format(printer, output_format);
1164 printer = isl_printer_print_map(printer, map);
1166 isl_printer_free(printer);
1169 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1171 int i;
1172 int n;
1174 for (i = 0, n = 0; i < rec->n; ++i)
1175 if (!isl_upoly_is_zero(rec->p[i]))
1176 ++n;
1178 return n;
1181 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
1182 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1184 int c = p->output_format == ISL_FORMAT_C;
1185 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1186 p = print_affine_of_len(dim, div, p,
1187 div->row[pos] + 1, div->n_col - 1, 1);
1188 p = isl_printer_print_str(p, c ? ", " : ")/");
1189 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1190 p = isl_printer_print_str(p, c ? ")" : "]");
1191 return p;
1194 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1195 __isl_take isl_printer *p, int first)
1197 struct isl_upoly_cst *cst;
1198 int neg;
1200 cst = isl_upoly_as_cst(up);
1201 if (!cst)
1202 goto error;
1203 neg = !first && isl_int_is_neg(cst->n);
1204 if (!first)
1205 p = isl_printer_print_str(p, neg ? " - " : " + ");
1206 if (neg)
1207 isl_int_neg(cst->n, cst->n);
1208 if (isl_int_is_zero(cst->d)) {
1209 int sgn = isl_int_sgn(cst->n);
1210 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1211 sgn == 0 ? "NaN" : "infty");
1212 } else
1213 p = isl_printer_print_isl_int(p, cst->n);
1214 if (neg)
1215 isl_int_neg(cst->n, cst->n);
1216 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1217 p = isl_printer_print_str(p, "/");
1218 p = isl_printer_print_isl_int(p, cst->d);
1220 return p;
1221 error:
1222 isl_printer_free(p);
1223 return NULL;
1226 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1227 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1229 unsigned total;
1231 total = isl_space_dim(dim, isl_dim_all);
1232 if (var < total)
1233 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 1, 0);
1234 else
1235 p = print_div(dim, div, var - total, p);
1236 return p;
1239 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1240 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1242 p = print_base(p, dim, div, var);
1243 if (exp == 1)
1244 return p;
1245 if (p->output_format == ISL_FORMAT_C) {
1246 int i;
1247 for (i = 1; i < exp; ++i) {
1248 p = isl_printer_print_str(p, "*");
1249 p = print_base(p, dim, div, var);
1251 } else {
1252 p = isl_printer_print_str(p, "^");
1253 p = isl_printer_print_int(p, exp);
1255 return p;
1258 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1259 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1260 __isl_take isl_printer *p, int outer)
1262 int i, n, first, print_parens;
1263 struct isl_upoly_rec *rec;
1265 if (!p || !up || !dim || !div)
1266 goto error;
1268 if (isl_upoly_is_cst(up))
1269 return upoly_print_cst(up, p, 1);
1271 rec = isl_upoly_as_rec(up);
1272 if (!rec)
1273 goto error;
1274 n = upoly_rec_n_non_zero(rec);
1275 print_parens = n > 1 ||
1276 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1277 if (print_parens)
1278 p = isl_printer_print_str(p, "(");
1279 for (i = 0, first = 1; i < rec->n; ++i) {
1280 if (isl_upoly_is_zero(rec->p[i]))
1281 continue;
1282 if (isl_upoly_is_negone(rec->p[i])) {
1283 if (!i)
1284 p = isl_printer_print_str(p, "-1");
1285 else if (first)
1286 p = isl_printer_print_str(p, "-");
1287 else
1288 p = isl_printer_print_str(p, " - ");
1289 } else if (isl_upoly_is_cst(rec->p[i]) &&
1290 !isl_upoly_is_one(rec->p[i]))
1291 p = upoly_print_cst(rec->p[i], p, first);
1292 else {
1293 if (!first)
1294 p = isl_printer_print_str(p, " + ");
1295 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1296 p = upoly_print(rec->p[i], dim, div, p, 0);
1298 first = 0;
1299 if (i == 0)
1300 continue;
1301 if (!isl_upoly_is_one(rec->p[i]) &&
1302 !isl_upoly_is_negone(rec->p[i]))
1303 p = isl_printer_print_str(p, " * ");
1304 p = print_pow(p, dim, div, rec->up.var, i);
1306 if (print_parens)
1307 p = isl_printer_print_str(p, ")");
1308 return p;
1309 error:
1310 isl_printer_free(p);
1311 return NULL;
1314 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1315 __isl_keep isl_qpolynomial *qp)
1317 if (!p || !qp)
1318 goto error;
1319 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1320 return p;
1321 error:
1322 isl_printer_free(p);
1323 return NULL;
1326 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1327 __isl_keep isl_qpolynomial *qp)
1329 if (!p || !qp)
1330 goto error;
1332 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1333 p = print_tuple(qp->dim, p, isl_dim_param, 0, 0, NULL);
1334 p = isl_printer_print_str(p, " -> ");
1336 p = isl_printer_print_str(p, "{ ");
1337 if (isl_space_dim(qp->dim, isl_dim_set) > 0 ||
1338 isl_space_is_named_or_nested(qp->dim, isl_dim_set)) {
1339 p = print_space(qp->dim, p, 1, 0, 0, NULL);
1340 p = isl_printer_print_str(p, " -> ");
1342 p = print_qpolynomial(p, qp);
1343 p = isl_printer_print_str(p, " }");
1344 return p;
1345 error:
1346 isl_printer_free(p);
1347 return NULL;
1350 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1351 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1353 isl_int den;
1355 isl_int_init(den);
1356 isl_qpolynomial_get_den(qp, &den);
1357 if (!isl_int_is_one(den)) {
1358 isl_qpolynomial *f;
1359 p = isl_printer_print_str(p, "(");
1360 qp = isl_qpolynomial_copy(qp);
1361 f = isl_qpolynomial_rat_cst(isl_space_copy(qp->dim),
1362 den, qp->dim->ctx->one);
1363 qp = isl_qpolynomial_mul(qp, f);
1365 if (qp)
1366 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1367 if (!isl_int_is_one(den)) {
1368 p = isl_printer_print_str(p, ")/");
1369 p = isl_printer_print_isl_int(p, den);
1370 isl_qpolynomial_free(qp);
1372 isl_int_clear(den);
1373 return p;
1376 __isl_give isl_printer *isl_printer_print_qpolynomial(
1377 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1379 if (!p || !qp)
1380 goto error;
1382 if (p->output_format == ISL_FORMAT_ISL)
1383 return print_qpolynomial_isl(p, qp);
1384 else if (p->output_format == ISL_FORMAT_C)
1385 return print_qpolynomial_c(p, qp->dim, qp);
1386 else
1387 isl_die(qp->dim->ctx, isl_error_unsupported,
1388 "output format not supported for isl_qpolynomials",
1389 goto error);
1390 error:
1391 isl_printer_free(p);
1392 return NULL;
1395 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1396 unsigned output_format)
1398 isl_printer *p;
1400 if (!qp)
1401 return;
1403 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1404 p = isl_printer_to_file(qp->dim->ctx, out);
1405 p = isl_printer_print_qpolynomial(p, qp);
1406 isl_printer_free(p);
1409 static __isl_give isl_printer *qpolynomial_fold_print(
1410 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1412 int i;
1414 if (fold->type == isl_fold_min)
1415 p = isl_printer_print_str(p, "min");
1416 else if (fold->type == isl_fold_max)
1417 p = isl_printer_print_str(p, "max");
1418 p = isl_printer_print_str(p, "(");
1419 for (i = 0; i < fold->n; ++i) {
1420 if (i)
1421 p = isl_printer_print_str(p, ", ");
1422 p = print_qpolynomial(p, fold->qp[i]);
1424 p = isl_printer_print_str(p, ")");
1425 return p;
1428 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1429 FILE *out, unsigned output_format)
1431 isl_printer *p;
1433 if (!fold)
1434 return;
1436 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1438 p = isl_printer_to_file(fold->dim->ctx, out);
1439 p = isl_printer_print_qpolynomial_fold(p, fold);
1441 isl_printer_free(p);
1444 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1445 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1447 int i = 0;
1449 for (i = 0; i < pwqp->n; ++i) {
1450 if (i)
1451 p = isl_printer_print_str(p, "; ");
1452 if (isl_space_dim(pwqp->dim, isl_dim_set) > 0 ||
1453 isl_space_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1454 p = print_space(pwqp->p[i].set->dim, p, 1, 0, 0, NULL);
1455 p = isl_printer_print_str(p, " -> ");
1457 p = print_qpolynomial(p, pwqp->p[i].qp);
1458 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1461 return p;
1464 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1465 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1467 if (!p || !pwqp)
1468 goto error;
1470 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1471 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1472 p = isl_printer_print_str(p, " -> ");
1474 p = isl_printer_print_str(p, "{ ");
1475 if (pwqp->n == 0) {
1476 if (isl_space_dim(pwqp->dim, isl_dim_set) > 0 ||
1477 isl_space_is_named_or_nested(pwqp->dim, isl_dim_set)) {
1478 p = print_space(pwqp->dim, p, 1, 0, 0, NULL);
1479 p = isl_printer_print_str(p, " -> ");
1481 p = isl_printer_print_str(p, "0");
1483 p = isl_pwqp_print_isl_body(p, pwqp);
1484 p = isl_printer_print_str(p, " }");
1485 return p;
1486 error:
1487 isl_printer_free(p);
1488 return NULL;
1491 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1492 unsigned output_format)
1494 isl_printer *p;
1496 if (!pwqp)
1497 return;
1499 p = isl_printer_to_file(pwqp->dim->ctx, out);
1500 p = isl_printer_set_output_format(p, output_format);
1501 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1503 isl_printer_free(p);
1506 static __isl_give isl_printer *isl_pwf_print_isl_body(
1507 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1509 int i = 0;
1511 for (i = 0; i < pwf->n; ++i) {
1512 if (i)
1513 p = isl_printer_print_str(p, "; ");
1514 if (isl_space_dim(pwf->dim, isl_dim_set) > 0 ||
1515 isl_space_is_named_or_nested(pwf->dim, isl_dim_set)) {
1516 p = print_space(pwf->p[i].set->dim, p, 1, 0, 0, NULL);
1517 p = isl_printer_print_str(p, " -> ");
1519 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1520 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1523 return p;
1526 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1527 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1529 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1530 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1531 p = isl_printer_print_str(p, " -> ");
1533 p = isl_printer_print_str(p, "{ ");
1534 if (pwf->n == 0) {
1535 if (isl_space_dim(pwf->dim, isl_dim_set) > 0 ||
1536 isl_space_is_named_or_nested(pwf->dim, isl_dim_set)) {
1537 p = print_space(pwf->dim, p, 1, 0, 0, NULL);
1538 p = isl_printer_print_str(p, " -> ");
1540 p = isl_printer_print_str(p, "0");
1542 p = isl_pwf_print_isl_body(p, pwf);
1543 p = isl_printer_print_str(p, " }");
1544 return p;
1547 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1548 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1550 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1551 __isl_keep isl_space *dim,
1552 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1554 if (type == isl_dim_div) {
1555 p = isl_printer_print_str(p, "floord(");
1556 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1557 p = isl_printer_print_str(p, ", ");
1558 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1559 p = isl_printer_print_str(p, ")");
1560 } else {
1561 const char *name;
1563 name = isl_space_get_dim_name(dim, type, pos);
1564 if (!name)
1565 name = "UNNAMED";
1566 p = isl_printer_print_str(p, name);
1568 return p;
1571 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1572 __isl_keep isl_space *dim,
1573 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1575 enum isl_dim_type type;
1577 if (pos == 0)
1578 return isl_printer_print_isl_int(p, c);
1580 if (isl_int_is_one(c))
1582 else if (isl_int_is_negone(c))
1583 p = isl_printer_print_str(p, "-");
1584 else {
1585 p = isl_printer_print_isl_int(p, c);
1586 p = isl_printer_print_str(p, "*");
1588 type = pos2type(dim, &pos);
1589 p = print_name_c(p, dim, bset, type, pos);
1590 return p;
1593 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1594 __isl_keep isl_space *dim,
1595 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1597 int i;
1598 int first;
1600 for (i = 0, first = 1; i < len; ++i) {
1601 int flip = 0;
1602 if (isl_int_is_zero(c[i]))
1603 continue;
1604 if (!first) {
1605 if (isl_int_is_neg(c[i])) {
1606 flip = 1;
1607 isl_int_neg(c[i], c[i]);
1608 p = isl_printer_print_str(p, " - ");
1609 } else
1610 p = isl_printer_print_str(p, " + ");
1612 first = 0;
1613 p = print_term_c(p, dim, bset, c[i], i);
1614 if (flip)
1615 isl_int_neg(c[i], c[i]);
1617 if (first)
1618 p = isl_printer_print_str(p, "0");
1619 return p;
1622 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1623 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1625 unsigned len = 1 + isl_basic_set_total_dim(bset);
1626 return print_partial_affine_c(p, dim, bset, c, len);
1629 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1630 __isl_keep isl_space *dim,
1631 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1633 if (!first)
1634 p = isl_printer_print_str(p, " && ");
1636 p = print_affine_c(p, dim, bset, c);
1637 p = isl_printer_print_str(p, " ");
1638 p = isl_printer_print_str(p, op);
1639 p = isl_printer_print_str(p, " 0");
1640 return p;
1643 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1644 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1646 int i, j;
1647 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1648 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1650 for (i = 0; i < bset->n_eq; ++i) {
1651 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1652 if (j < 0)
1653 p = print_constraint_c(p, dim, bset,
1654 bset->eq[i], "==", !i);
1655 else {
1656 if (i)
1657 p = isl_printer_print_str(p, " && ");
1658 p = isl_printer_print_str(p, "(");
1659 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1660 1 + total + j);
1661 p = isl_printer_print_str(p, ") % ");
1662 p = isl_printer_print_isl_int(p,
1663 bset->eq[i][1 + total + j]);
1664 p = isl_printer_print_str(p, " == 0");
1667 for (i = 0; i < bset->n_ineq; ++i)
1668 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1669 !bset->n_eq && !i);
1670 return p;
1673 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1674 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1676 int i;
1678 if (set->n == 0)
1679 p = isl_printer_print_str(p, "0");
1681 for (i = 0; i < set->n; ++i) {
1682 if (i)
1683 p = isl_printer_print_str(p, " || ");
1684 if (set->n > 1)
1685 p = isl_printer_print_str(p, "(");
1686 p = print_basic_set_c(p, dim, set->p[i]);
1687 if (set->n > 1)
1688 p = isl_printer_print_str(p, ")");
1690 return p;
1693 static __isl_give isl_printer *print_pw_qpolynomial_c(
1694 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1696 int i;
1698 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
1699 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1701 for (i = 0; i < pwqp->n; ++i) {
1702 p = isl_printer_print_str(p, "(");
1703 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1704 p = isl_printer_print_str(p, ") ? (");
1705 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1706 p = isl_printer_print_str(p, ") : ");
1709 p = isl_printer_print_str(p, "0");
1710 return p;
1713 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1714 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1716 if (!p || !pwqp)
1717 goto error;
1719 if (p->output_format == ISL_FORMAT_ISL)
1720 return print_pw_qpolynomial_isl(p, pwqp);
1721 else if (p->output_format == ISL_FORMAT_C)
1722 return print_pw_qpolynomial_c(p, pwqp);
1723 isl_assert(p->ctx, 0, goto error);
1724 error:
1725 isl_printer_free(p);
1726 return NULL;
1729 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1731 struct isl_union_print_data *data;
1732 data = (struct isl_union_print_data *)user;
1734 if (!data->first)
1735 data->p = isl_printer_print_str(data->p, "; ");
1736 data->first = 0;
1738 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1739 isl_pw_qpolynomial_free(pwqp);
1741 return 0;
1744 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1745 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1747 struct isl_union_print_data data = { p, 1, 1 };
1748 isl_space *dim;
1749 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1750 if (isl_space_dim(dim, isl_dim_param) > 0) {
1751 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1752 p = isl_printer_print_str(p, " -> ");
1754 isl_space_free(dim);
1755 p = isl_printer_print_str(p, "{ ");
1756 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1757 &data);
1758 p = data.p;
1759 p = isl_printer_print_str(p, " }");
1760 return p;
1763 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1764 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1766 if (!p || !upwqp)
1767 goto error;
1769 if (p->output_format == ISL_FORMAT_ISL)
1770 return print_union_pw_qpolynomial_isl(p, upwqp);
1771 isl_die(p->ctx, isl_error_invalid,
1772 "invalid output format for isl_union_pw_qpolynomial",
1773 goto error);
1774 error:
1775 isl_printer_free(p);
1776 return NULL;
1779 static __isl_give isl_printer *print_qpolynomial_fold_c(
1780 __isl_take isl_printer *p, __isl_keep isl_space *dim,
1781 __isl_keep isl_qpolynomial_fold *fold)
1783 int i;
1785 for (i = 0; i < fold->n - 1; ++i)
1786 if (fold->type == isl_fold_min)
1787 p = isl_printer_print_str(p, "min(");
1788 else if (fold->type == isl_fold_max)
1789 p = isl_printer_print_str(p, "max(");
1791 for (i = 0; i < fold->n; ++i) {
1792 if (i)
1793 p = isl_printer_print_str(p, ", ");
1794 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1795 if (i)
1796 p = isl_printer_print_str(p, ")");
1798 return p;
1801 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1802 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1804 if (!p || !fold)
1805 goto error;
1806 if (p->output_format == ISL_FORMAT_ISL)
1807 return qpolynomial_fold_print(fold, p);
1808 else if (p->output_format == ISL_FORMAT_C)
1809 return print_qpolynomial_fold_c(p, fold->dim, fold);
1810 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1811 goto error);
1812 error:
1813 isl_printer_free(p);
1814 return NULL;
1817 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1818 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1820 int i;
1822 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
1823 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1825 for (i = 0; i < pwf->n; ++i) {
1826 p = isl_printer_print_str(p, "(");
1827 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1828 p = isl_printer_print_str(p, ") ? (");
1829 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1830 p = isl_printer_print_str(p, ") : ");
1833 p = isl_printer_print_str(p, "0");
1834 return p;
1837 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1838 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1840 if (!p || !pwf)
1841 goto error;
1843 if (p->output_format == ISL_FORMAT_ISL)
1844 return print_pw_qpolynomial_fold_isl(p, pwf);
1845 else if (p->output_format == ISL_FORMAT_C)
1846 return print_pw_qpolynomial_fold_c(p, pwf);
1847 isl_assert(p->ctx, 0, goto error);
1848 error:
1849 isl_printer_free(p);
1850 return NULL;
1853 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1854 FILE *out, unsigned output_format)
1856 isl_printer *p;
1858 if (!pwf)
1859 return;
1861 p = isl_printer_to_file(pwf->dim->ctx, out);
1862 p = isl_printer_set_output_format(p, output_format);
1863 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1865 isl_printer_free(p);
1868 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1870 struct isl_union_print_data *data;
1871 data = (struct isl_union_print_data *)user;
1873 if (!data->first)
1874 data->p = isl_printer_print_str(data->p, "; ");
1875 data->first = 0;
1877 data->p = isl_pwf_print_isl_body(data->p, pwf);
1878 isl_pw_qpolynomial_fold_free(pwf);
1880 return 0;
1883 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1884 __isl_take isl_printer *p,
1885 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1887 struct isl_union_print_data data = { p, 1, 1 };
1888 isl_space *dim;
1889 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1890 if (isl_space_dim(dim, isl_dim_param) > 0) {
1891 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1892 p = isl_printer_print_str(p, " -> ");
1894 isl_space_free(dim);
1895 p = isl_printer_print_str(p, "{ ");
1896 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1897 &print_pwf_body, &data);
1898 p = data.p;
1899 p = isl_printer_print_str(p, " }");
1900 return p;
1903 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1904 __isl_take isl_printer *p,
1905 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1907 if (!p || !upwf)
1908 goto error;
1910 if (p->output_format == ISL_FORMAT_ISL)
1911 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1912 isl_die(p->ctx, isl_error_invalid,
1913 "invalid output format for isl_union_pw_qpolynomial_fold",
1914 goto error);
1915 error:
1916 isl_printer_free(p);
1917 return NULL;
1920 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
1921 __isl_keep isl_constraint *c)
1923 isl_basic_map *bmap;
1925 if (!p || !c)
1926 goto error;
1928 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
1929 p = isl_printer_print_basic_map(p, bmap);
1930 isl_basic_map_free(bmap);
1931 return p;
1932 error:
1933 isl_printer_free(p);
1934 return NULL;
1937 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
1938 __isl_keep isl_space *dim)
1940 if (!dim)
1941 goto error;
1943 if (isl_space_dim(dim, isl_dim_param) > 0) {
1944 p = print_tuple(dim, p, isl_dim_param, 0, 0, NULL);
1945 p = isl_printer_print_str(p, " -> ");
1948 p = isl_printer_print_str(p, "{ ");
1949 p = print_tuple(dim, p, isl_dim_in, 0, 0, NULL);
1950 p = isl_printer_print_str(p, " -> ");
1951 p = print_tuple(dim, p, isl_dim_out, 0, 0, NULL);
1952 p = isl_printer_print_str(p, " }");
1954 return p;
1955 error:
1956 isl_printer_free(p);
1957 return NULL;
1960 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
1961 __isl_keep isl_local_space *ls)
1963 unsigned total;
1964 unsigned n_div;
1966 if (!ls)
1967 goto error;
1969 total = isl_local_space_dim(ls, isl_dim_all);
1970 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
1971 p = print_tuple(ls->dim, p, isl_dim_param, 0, 0, NULL);
1972 p = isl_printer_print_str(p, " -> ");
1974 p = isl_printer_print_str(p, "{ ");
1975 p = print_tuple(ls->dim, p, isl_dim_in, 0, 0, NULL);
1976 p = isl_printer_print_str(p, " -> ");
1977 p = print_tuple(ls->dim, p, isl_dim_out, 0, 0, NULL);
1978 n_div = isl_local_space_dim(ls, isl_dim_div);
1979 if (n_div > 0) {
1980 int i;
1981 p = isl_printer_print_str(p, " : ");
1982 p = isl_printer_print_str(p, s_open_exists[0]);
1983 for (i = 0; i < n_div; ++i) {
1984 if (i)
1985 p = isl_printer_print_str(p, ", ");
1986 p = print_name(ls->dim, p, isl_dim_div, i, 0, 0);
1987 if (isl_int_is_zero(ls->div->row[i][0]))
1988 continue;
1989 p = isl_printer_print_str(p, " = [(");
1990 p = print_affine_of_len(ls->dim, ls->div, p,
1991 ls->div->row[i] + 1, 1 + total, 0);
1992 p = isl_printer_print_str(p, ")/");
1993 p = isl_printer_print_isl_int(p, ls->div->row[i][0]);
1994 p = isl_printer_print_str(p, "]");
1997 p = isl_printer_print_str(p, " }");
1998 return p;
1999 error:
2000 isl_printer_free(p);
2001 return NULL;
2004 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2005 __isl_keep isl_aff *aff)
2007 unsigned total;
2009 total = isl_local_space_dim(aff->ls, isl_dim_all);
2010 p = print_tuple(aff->ls->dim, p, isl_dim_set, 1, 0, NULL);
2011 p = isl_printer_print_str(p, " -> [");
2012 p = isl_printer_print_str(p, "(");
2013 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2014 aff->v->el + 1, 1 + total, 1);
2015 if (isl_int_is_one(aff->v->el[0]))
2016 p = isl_printer_print_str(p, ")");
2017 else {
2018 p = isl_printer_print_str(p, ")/");
2019 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2021 p = isl_printer_print_str(p, "]");
2023 return p;
2026 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2027 __isl_keep isl_aff *aff)
2029 if (!aff)
2030 goto error;
2032 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2033 p = print_tuple(aff->ls->dim, p, isl_dim_param, 0, 0, NULL);
2034 p = isl_printer_print_str(p, " -> ");
2036 p = isl_printer_print_str(p, "{ ");
2037 p = print_aff(p, aff);
2038 p = isl_printer_print_str(p, " }");
2039 return p;
2040 error:
2041 isl_printer_free(p);
2042 return NULL;
2045 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2046 __isl_keep isl_pw_aff *pwaff)
2048 int i;
2050 if (!pwaff)
2051 goto error;
2053 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2054 p = print_tuple(pwaff->dim, p, isl_dim_param, 0, 0, NULL);
2055 p = isl_printer_print_str(p, " -> ");
2057 p = isl_printer_print_str(p, "{ ");
2058 for (i = 0; i < pwaff->n; ++i) {
2059 if (i)
2060 p = isl_printer_print_str(p, "; ");
2061 p = print_aff(p, pwaff->p[i].aff);
2062 p = print_disjuncts((isl_map *)pwaff->p[i].set, p, 1, 0);
2064 p = isl_printer_print_str(p, " }");
2065 return p;
2066 error:
2067 isl_printer_free(p);
2068 return NULL;
2071 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2072 __isl_keep isl_local_space *ls, isl_int *c);
2074 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2075 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2077 if (type == isl_dim_div) {
2078 p = isl_printer_print_str(p, "floord(");
2079 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2080 p = isl_printer_print_str(p, ", ");
2081 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2082 p = isl_printer_print_str(p, ")");
2083 } else {
2084 const char *name;
2086 name = isl_space_get_dim_name(ls->dim, type, pos);
2087 if (!name)
2088 name = "UNNAMED";
2089 p = isl_printer_print_str(p, name);
2091 return p;
2094 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2095 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2097 enum isl_dim_type type;
2099 if (pos == 0)
2100 return isl_printer_print_isl_int(p, c);
2102 if (isl_int_is_one(c))
2104 else if (isl_int_is_negone(c))
2105 p = isl_printer_print_str(p, "-");
2106 else {
2107 p = isl_printer_print_isl_int(p, c);
2108 p = isl_printer_print_str(p, "*");
2110 type = pos2type(ls->dim, &pos);
2111 p = print_ls_name_c(p, ls, type, pos);
2112 return p;
2115 static __isl_give isl_printer *print_ls_partial_affine_c(
2116 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2117 isl_int *c, unsigned len)
2119 int i;
2120 int first;
2122 for (i = 0, first = 1; i < len; ++i) {
2123 int flip = 0;
2124 if (isl_int_is_zero(c[i]))
2125 continue;
2126 if (!first) {
2127 if (isl_int_is_neg(c[i])) {
2128 flip = 1;
2129 isl_int_neg(c[i], c[i]);
2130 p = isl_printer_print_str(p, " - ");
2131 } else
2132 p = isl_printer_print_str(p, " + ");
2134 first = 0;
2135 p = print_ls_term_c(p, ls, c[i], i);
2136 if (flip)
2137 isl_int_neg(c[i], c[i]);
2139 if (first)
2140 p = isl_printer_print_str(p, "0");
2141 return p;
2144 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2145 __isl_keep isl_local_space *ls, isl_int *c)
2147 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2148 return print_ls_partial_affine_c(p, ls, c, len);
2151 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2152 __isl_keep isl_aff *aff)
2154 unsigned total;
2156 total = isl_local_space_dim(aff->ls, isl_dim_all);
2157 p = isl_printer_print_str(p, "(");
2158 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2159 if (isl_int_is_one(aff->v->el[0]))
2160 p = isl_printer_print_str(p, ")");
2161 else {
2162 p = isl_printer_print_str(p, ")/");
2163 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2165 return p;
2168 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2169 __isl_keep isl_pw_aff *pwaff)
2171 int i;
2173 if (pwaff->n < 1)
2174 isl_die(p->ctx, isl_error_unsupported,
2175 "cannot print empty isl_pw_aff in C format", goto error);
2177 for (i = 0; i < pwaff->n - 1; ++i) {
2178 p = isl_printer_print_str(p, "(");
2179 p = print_set_c(p, pwaff->dim, pwaff->p[i].set);
2180 p = isl_printer_print_str(p, ") ? (");
2181 p = print_aff_c(p, pwaff->p[i].aff);
2182 p = isl_printer_print_str(p, ") : ");
2185 return print_aff_c(p, pwaff->p[pwaff->n - 1].aff);
2186 error:
2187 isl_printer_free(p);
2188 return NULL;
2191 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2192 __isl_keep isl_aff *aff)
2194 if (!p || !aff)
2195 goto error;
2197 if (p->output_format == ISL_FORMAT_ISL)
2198 return print_aff_isl(p, aff);
2199 else if (p->output_format == ISL_FORMAT_C)
2200 return print_aff_c(p, aff);
2201 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2202 goto error);
2203 error:
2204 isl_printer_free(p);
2205 return NULL;
2208 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2209 __isl_keep isl_pw_aff *pwaff)
2211 if (!p || !pwaff)
2212 goto error;
2214 if (p->output_format == ISL_FORMAT_ISL)
2215 return print_pw_aff_isl(p, pwaff);
2216 else if (p->output_format == ISL_FORMAT_C)
2217 return print_pw_aff_c(p, pwaff);
2218 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2219 goto error);
2220 error:
2221 isl_printer_free(p);
2222 return NULL;