explicitly differentiate between spaces of maps, sets and parameter sets
[isl.git] / isl_output.c
blobc8f833d02112f94ecf0b8d52b15fb952b3d78233
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 (isl_space_is_params(dim))
422 else if (isl_space_is_set(dim))
423 p = print_tuple(dim, p, isl_dim_set, 1, latex, eq);
424 else {
425 p = print_tuple(dim, p, isl_dim_in, 0, latex, eq);
426 p = isl_printer_print_str(p, s_to[latex]);
427 p = print_tuple(dim, p, isl_dim_out, 0, latex, eq);
430 return p;
433 static __isl_give isl_printer *print_omega_parameters(__isl_keep isl_space *dim,
434 __isl_take isl_printer *p)
436 if (isl_space_dim(dim, isl_dim_param) == 0)
437 return p;
439 p = isl_printer_start_line(p);
440 p = isl_printer_print_str(p, "symbolic ");
441 p = print_var_list(dim, p, isl_dim_param, 0, 0, NULL);
442 p = isl_printer_print_str(p, ";");
443 p = isl_printer_end_line(p);
444 return p;
447 static __isl_give isl_printer *print_constraint(struct isl_basic_map *bmap,
448 __isl_keep isl_space *dim, __isl_take isl_printer *p,
449 isl_int *c, int last, const char *op, int first_constraint, int set,
450 int latex)
452 if (!first_constraint)
453 p = isl_printer_print_str(p, s_and[latex]);
455 isl_int_abs(c[last], c[last]);
457 p = print_term(dim, NULL, c[last], last, p, set, latex);
459 p = isl_printer_print_str(p, " ");
460 p = isl_printer_print_str(p, op);
461 p = isl_printer_print_str(p, " ");
463 isl_int_set_si(c[last], 0);
464 p = print_affine(bmap, dim, p, c, set);
466 return p;
469 static __isl_give isl_printer *print_constraints(__isl_keep isl_basic_map *bmap,
470 __isl_keep isl_space *dim, __isl_take isl_printer *p, int set, int latex)
472 int i;
473 struct isl_vec *c;
474 unsigned total = isl_basic_map_total_dim(bmap);
476 c = isl_vec_alloc(bmap->ctx, 1 + total);
477 if (!c)
478 goto error;
480 for (i = bmap->n_eq - 1; i >= 0; --i) {
481 int l = isl_seq_last_non_zero(bmap->eq[i], 1 + total);
482 if (l < 0) {
483 if (i != bmap->n_eq - 1)
484 p = isl_printer_print_str(p, s_and[latex]);
485 p = isl_printer_print_str(p, "0 = 0");
486 continue;
488 if (isl_int_is_neg(bmap->eq[i][l]))
489 isl_seq_cpy(c->el, bmap->eq[i], 1 + total);
490 else
491 isl_seq_neg(c->el, bmap->eq[i], 1 + total);
492 p = print_constraint(bmap, dim, p, c->el, l,
493 "=", i == bmap->n_eq - 1, set, latex);
495 for (i = 0; i < bmap->n_ineq; ++i) {
496 int l = isl_seq_last_non_zero(bmap->ineq[i], 1 + total);
497 int s;
498 const char *op;
499 if (l < 0)
500 continue;
501 s = isl_int_sgn(bmap->ineq[i][l]);
502 if (s < 0)
503 isl_seq_cpy(c->el, bmap->ineq[i], 1 + total);
504 else
505 isl_seq_neg(c->el, bmap->ineq[i], 1 + total);
506 op = s < 0 ? s_le[latex] : s_ge[latex];
507 p = print_constraint(bmap, dim, p, c->el, l,
508 op, !bmap->n_eq && !i, set, latex);
511 isl_vec_free(c);
513 return p;
514 error:
515 isl_vec_free(c);
516 isl_printer_free(p);
517 return NULL;
520 static __isl_give isl_printer *print_omega_constraints(
521 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p, int set)
523 if (bmap->n_eq + bmap->n_ineq == 0)
524 return p;
526 p = isl_printer_print_str(p, ": ");
527 if (bmap->n_div > 0) {
528 int i;
529 p = isl_printer_print_str(p, "exists (");
530 for (i = 0; i < bmap->n_div; ++i) {
531 if (i)
532 p = isl_printer_print_str(p, ", ");
533 p = print_name(bmap->dim, p, isl_dim_div, i, 0, 0);
535 p = isl_printer_print_str(p, ": ");
537 p = print_constraints(bmap, bmap->dim, p, set, 0);
538 if (bmap->n_div > 0)
539 p = isl_printer_print_str(p, ")");
540 return p;
543 static __isl_give isl_printer *basic_map_print_omega(
544 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
546 p = isl_printer_print_str(p, "{ [");
547 p = print_var_list(bmap->dim, p, isl_dim_in, 0, 0, NULL);
548 p = isl_printer_print_str(p, "] -> [");
549 p = print_var_list(bmap->dim, p, isl_dim_out, 0, 0, NULL);
550 p = isl_printer_print_str(p, "] ");
551 p = print_omega_constraints(bmap, p, 0);
552 p = isl_printer_print_str(p, " }");
553 return p;
556 static __isl_give isl_printer *isl_basic_map_print_omega(
557 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p)
559 p = print_omega_parameters(bmap->dim, p);
561 p = isl_printer_start_line(p);
562 p = basic_map_print_omega(bmap, p);
563 p = isl_printer_end_line(p);
564 return p;
567 static __isl_give isl_printer *basic_set_print_omega(
568 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
570 p = isl_printer_print_str(p, "{ [");
571 p = print_var_list(bset->dim, p, isl_dim_set, 1, 0, NULL);
572 p = isl_printer_print_str(p, "] ");
573 p = print_omega_constraints((isl_basic_map *)bset, p, 1);
574 p = isl_printer_print_str(p, " }");
575 return p;
578 static __isl_give isl_printer *isl_basic_set_print_omega(
579 __isl_keep isl_basic_set *bset, __isl_take isl_printer *p)
581 p = print_omega_parameters(bset->dim, p);
583 p = isl_printer_start_line(p);
584 p = basic_set_print_omega(bset, p);
585 p = isl_printer_end_line(p);
586 return p;
589 static __isl_give isl_printer *isl_map_print_omega(__isl_keep isl_map *map,
590 __isl_take isl_printer *p)
592 int i;
594 p = print_omega_parameters(map->dim, p);
596 p = isl_printer_start_line(p);
597 for (i = 0; i < map->n; ++i) {
598 if (i)
599 p = isl_printer_print_str(p, " union ");
600 p = basic_map_print_omega(map->p[i], p);
602 p = isl_printer_end_line(p);
603 return p;
606 static __isl_give isl_printer *isl_set_print_omega(__isl_keep isl_set *set,
607 __isl_take isl_printer *p)
609 int i;
611 p = print_omega_parameters(set->dim, p);
613 p = isl_printer_start_line(p);
614 for (i = 0; i < set->n; ++i) {
615 if (i)
616 p = isl_printer_print_str(p, " union ");
617 p = basic_set_print_omega(set->p[i], p);
619 p = isl_printer_end_line(p);
620 return p;
623 static __isl_give isl_printer *print_disjunct(__isl_keep isl_basic_map *bmap,
624 __isl_keep isl_space *dim, __isl_take isl_printer *p, int set, int latex)
626 if (bmap->n_div > 0) {
627 int i;
628 p = isl_printer_print_str(p, s_open_exists[latex]);
629 for (i = 0; i < bmap->n_div; ++i) {
630 if (i)
631 p = isl_printer_print_str(p, ", ");
632 p = print_name(dim, p, isl_dim_div, i, 0, latex);
633 if (latex || isl_int_is_zero(bmap->div[i][0]))
634 continue;
635 p = isl_printer_print_str(p, " = [(");
636 p = print_affine(bmap, dim, p, bmap->div[i] + 1, set);
637 p = isl_printer_print_str(p, ")/");
638 p = isl_printer_print_isl_int(p, bmap->div[i][0]);
639 p = isl_printer_print_str(p, "]");
641 p = isl_printer_print_str(p, ": ");
644 p = print_constraints(bmap, dim, p, set, latex);
646 if (bmap->n_div > 0)
647 p = isl_printer_print_str(p, s_close_exists[latex]);
648 return p;
651 static __isl_give isl_printer *isl_basic_map_print_isl(
652 __isl_keep isl_basic_map *bmap, __isl_take isl_printer *p,
653 int set, int latex)
655 int rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
656 if (isl_basic_map_dim(bmap, isl_dim_param) > 0) {
657 p = print_tuple(bmap->dim, p, isl_dim_param, 0, latex, NULL);
658 p = isl_printer_print_str(p, " -> ");
660 p = isl_printer_print_str(p, "{ ");
661 p = print_space(bmap->dim, p, set, latex, rational, NULL);
662 p = isl_printer_print_str(p, " : ");
663 p = print_disjunct(bmap, bmap->dim, p, set, latex);
664 p = isl_printer_print_str(p, " }");
665 return p;
668 static __isl_give isl_printer *print_disjuncts(__isl_keep isl_map *map,
669 __isl_take isl_printer *p, int set, int latex)
671 int i;
673 if (isl_map_plain_is_universe(map))
674 return p;
676 p = isl_printer_print_str(p, s_such_that[latex]);
677 if (map->n == 0)
678 p = isl_printer_print_str(p, "1 = 0");
679 for (i = 0; i < map->n; ++i) {
680 if (i)
681 p = isl_printer_print_str(p, s_or[latex]);
682 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
683 p = isl_printer_print_str(p, "(");
684 p = print_disjunct(map->p[i], map->dim, p, set, latex);
685 if (map->n > 1 && map->p[i]->n_eq + map->p[i]->n_ineq > 1)
686 p = isl_printer_print_str(p, ")");
688 return p;
691 /* Print the disjuncts of a map (or set).
692 * If the map turns out to be a universal parameter domain, then
693 * we need to print the colon. Otherwise, the output looks identical
694 * to the empty set.
696 static __isl_give isl_printer *print_disjuncts_map(__isl_keep isl_map *map,
697 __isl_take isl_printer *p, int set, int latex)
699 if (isl_map_plain_is_universe(map) && isl_space_is_params(map->dim))
700 return isl_printer_print_str(p, s_such_that[latex]);
701 else
702 return print_disjuncts(map, p, set, latex);
705 struct isl_aff_split {
706 isl_basic_map *aff;
707 isl_map *map;
710 static void free_split(__isl_take struct isl_aff_split *split, int n)
712 int i;
714 if (!split)
715 return;
717 for (i = 0; i < n; ++i) {
718 isl_basic_map_free(split[i].aff);
719 isl_map_free(split[i].map);
722 free(split);
725 static __isl_give isl_basic_map *get_aff(__isl_take isl_basic_map *bmap)
727 int i, j;
728 unsigned nparam, n_in, n_out, total;
730 bmap = isl_basic_map_cow(bmap);
731 if (!bmap)
732 return NULL;
733 if (isl_basic_map_free_inequality(bmap, bmap->n_ineq) < 0)
734 goto error;
736 nparam = isl_basic_map_dim(bmap, isl_dim_param);
737 n_in = isl_basic_map_dim(bmap, isl_dim_in);
738 n_out = isl_basic_map_dim(bmap, isl_dim_out);
739 total = isl_basic_map_dim(bmap, isl_dim_all);
740 for (i = bmap->n_eq - 1; i >= 0; --i) {
741 j = isl_seq_last_non_zero(bmap->eq[i] + 1, total);
742 if (j >= nparam && j < nparam + n_in + n_out &&
743 (isl_int_is_one(bmap->eq[i][1 + j]) ||
744 isl_int_is_negone(bmap->eq[i][1 + j])))
745 continue;
746 if (isl_basic_map_drop_equality(bmap, i) < 0)
747 goto error;
750 bmap = isl_basic_map_finalize(bmap);
752 return bmap;
753 error:
754 isl_basic_map_free(bmap);
755 return NULL;
758 static int aff_split_cmp(const void *p1, const void *p2)
760 const struct isl_aff_split *s1, *s2;
761 s1 = (const struct isl_aff_split *) p1;
762 s2 = (const struct isl_aff_split *) p2;
764 return isl_basic_map_plain_cmp(s1->aff, s2->aff);
767 static __isl_give isl_basic_map *drop_aff(__isl_take isl_basic_map *bmap,
768 __isl_keep isl_basic_map *aff)
770 int i, j;
771 unsigned total;
773 if (!bmap || !aff)
774 goto error;
776 total = isl_space_dim(bmap->dim, isl_dim_all);
778 for (i = bmap->n_eq - 1; i >= 0; --i) {
779 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
780 bmap->n_div) != -1)
781 continue;
782 for (j = 0; j < aff->n_eq; ++j) {
783 if (!isl_seq_eq(bmap->eq[i], aff->eq[j], 1 + total) &&
784 !isl_seq_is_neg(bmap->eq[i], aff->eq[j], 1 + total))
785 continue;
786 if (isl_basic_map_drop_equality(bmap, i) < 0)
787 goto error;
788 break;
792 return bmap;
793 error:
794 isl_basic_map_free(bmap);
795 return NULL;
798 static __isl_give struct isl_aff_split *split_aff(__isl_keep isl_map *map)
800 int i, n;
801 struct isl_aff_split *split;
802 isl_ctx *ctx;
804 ctx = isl_map_get_ctx(map);
805 split = isl_calloc_array(ctx, struct isl_aff_split, map->n);
806 if (!split)
807 return NULL;
809 for (i = 0; i < map->n; ++i) {
810 isl_basic_map *bmap;
811 split[i].aff = get_aff(isl_basic_map_copy(map->p[i]));
812 bmap = isl_basic_map_copy(map->p[i]);
813 bmap = isl_basic_map_cow(bmap);
814 bmap = drop_aff(bmap, split[i].aff);
815 split[i].map = isl_map_from_basic_map(bmap);
816 if (!split[i].aff || !split[i].map)
817 goto error;
820 qsort(split, map->n, sizeof(struct isl_aff_split), &aff_split_cmp);
822 n = map->n;
823 for (i = n - 1; i >= 1; --i) {
824 if (!isl_basic_map_plain_is_equal(split[i - 1].aff,
825 split[i].aff))
826 continue;
827 isl_basic_map_free(split[i].aff);
828 split[i - 1].map = isl_map_union(split[i - 1].map,
829 split[i].map);
830 if (i != n - 1)
831 split[i] = split[n - 1];
832 split[n - 1].aff = NULL;
833 split[n - 1].map = NULL;
834 --n;
837 return split;
838 error:
839 free_split(split, map->n);
840 return NULL;
843 static __isl_give isl_printer *print_split_map(__isl_take isl_printer *p,
844 struct isl_aff_split *split, int n, int set)
846 int i;
847 int rational;
849 for (i = 0; i < n; ++i) {
850 isl_space *dim;
852 if (!split[i].map)
853 break;
854 dim = split[i].map->dim;
855 rational = split[i].map->n > 0 &&
856 ISL_F_ISSET(split[i].map->p[0], ISL_BASIC_MAP_RATIONAL);
857 if (i)
858 p = isl_printer_print_str(p, "; ");
859 p = print_space(dim, p, set, 0, rational, split[i].aff);
860 p = print_disjuncts_map(split[i].map, p, set, 0);
863 return p;
866 static __isl_give isl_printer *isl_map_print_isl_body(__isl_keep isl_map *map,
867 __isl_take isl_printer *p, int set)
869 struct isl_aff_split *split = NULL;
870 int rational;
872 if (map->n > 0)
873 split = split_aff(map);
874 if (split) {
875 p = print_split_map(p, split, map->n, set);
876 } else {
877 rational = map->n > 0 &&
878 ISL_F_ISSET(map->p[0], ISL_BASIC_MAP_RATIONAL);
879 p = print_space(map->dim, p, set, 0, rational, NULL);
880 p = print_disjuncts_map(map, p, set, 0);
882 free_split(split, map->n);
883 return p;
886 static __isl_give isl_printer *isl_map_print_isl(__isl_keep isl_map *map,
887 __isl_take isl_printer *p, int set)
889 if (isl_map_dim(map, isl_dim_param) > 0) {
890 p = print_tuple(map->dim, p, isl_dim_param, set, 0, NULL);
891 p = isl_printer_print_str(p, s_to[0]);
893 p = isl_printer_print_str(p, s_open_set[0]);
894 p = isl_map_print_isl_body(map, p, set);
895 p = isl_printer_print_str(p, s_close_set[0]);
896 return p;
899 static __isl_give isl_printer *print_latex_map(__isl_keep isl_map *map,
900 __isl_take isl_printer *p, __isl_keep isl_basic_map *aff, int set)
902 if (isl_map_dim(map, isl_dim_param) > 0) {
903 p = print_tuple(map->dim, p, isl_dim_param, set, 1, NULL);
904 p = isl_printer_print_str(p, s_to[1]);
906 p = isl_printer_print_str(p, s_open_set[1]);
907 p = print_space(map->dim, p, set, 1, 0, aff);
908 p = print_disjuncts_map(map, p, set, 1);
909 p = isl_printer_print_str(p, s_close_set[1]);
911 return p;
914 static __isl_give isl_printer *isl_map_print_latex(__isl_keep isl_map *map,
915 __isl_take isl_printer *p, int set)
917 int i;
918 struct isl_aff_split *split = NULL;
920 if (map->n > 0)
921 split = split_aff(map);
923 if (!split)
924 return print_latex_map(map, p, NULL, set);
926 for (i = 0; i < map->n; ++i) {
927 if (!split[i].map)
928 break;
929 if (i)
930 p = isl_printer_print_str(p, " \\cup ");
931 p = print_latex_map(split[i].map, p, split[i].aff, set);
934 free_split(split, map->n);
935 return p;
938 __isl_give isl_printer *isl_printer_print_basic_map(__isl_take isl_printer *p,
939 __isl_keep isl_basic_map *bmap)
941 if (!p || !bmap)
942 goto error;
943 if (p->output_format == ISL_FORMAT_ISL)
944 return isl_basic_map_print_isl(bmap, p, 0, 0);
945 else if (p->output_format == ISL_FORMAT_OMEGA)
946 return isl_basic_map_print_omega(bmap, p);
947 isl_assert(bmap->ctx, 0, goto error);
948 error:
949 isl_printer_free(p);
950 return NULL;
953 void isl_basic_map_print(__isl_keep isl_basic_map *bmap, FILE *out, int indent,
954 const char *prefix, const char *suffix, unsigned output_format)
956 isl_printer *printer;
958 if (!bmap)
959 return;
961 printer = isl_printer_to_file(bmap->ctx, out);
962 printer = isl_printer_set_indent(printer, indent);
963 printer = isl_printer_set_prefix(printer, prefix);
964 printer = isl_printer_set_suffix(printer, suffix);
965 printer = isl_printer_set_output_format(printer, output_format);
966 isl_printer_print_basic_map(printer, bmap);
968 isl_printer_free(printer);
971 __isl_give isl_printer *isl_printer_print_basic_set(__isl_take isl_printer *p,
972 __isl_keep isl_basic_set *bset)
974 if (!p || !bset)
975 goto error;
977 if (p->output_format == ISL_FORMAT_ISL)
978 return isl_basic_map_print_isl(bset, p, 1, 0);
979 else if (p->output_format == ISL_FORMAT_POLYLIB)
980 return isl_basic_set_print_polylib(bset, p, 0);
981 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
982 return isl_basic_set_print_polylib(bset, p, 1);
983 else if (p->output_format == ISL_FORMAT_POLYLIB_CONSTRAINTS)
984 return bset_print_constraints_polylib(bset, p);
985 else if (p->output_format == ISL_FORMAT_OMEGA)
986 return isl_basic_set_print_omega(bset, p);
987 isl_assert(p->ctx, 0, goto error);
988 error:
989 isl_printer_free(p);
990 return NULL;
993 void isl_basic_set_print(struct isl_basic_set *bset, FILE *out, int indent,
994 const char *prefix, const char *suffix, unsigned output_format)
996 isl_printer *printer;
998 if (!bset)
999 return;
1001 printer = isl_printer_to_file(bset->ctx, out);
1002 printer = isl_printer_set_indent(printer, indent);
1003 printer = isl_printer_set_prefix(printer, prefix);
1004 printer = isl_printer_set_suffix(printer, suffix);
1005 printer = isl_printer_set_output_format(printer, output_format);
1006 isl_printer_print_basic_set(printer, bset);
1008 isl_printer_free(printer);
1011 __isl_give isl_printer *isl_printer_print_set(__isl_take isl_printer *p,
1012 __isl_keep isl_set *set)
1014 if (!p || !set)
1015 goto error;
1016 if (p->output_format == ISL_FORMAT_ISL)
1017 return isl_map_print_isl((isl_map *)set, p, 1);
1018 else if (p->output_format == ISL_FORMAT_POLYLIB)
1019 return isl_set_print_polylib(set, p, 0);
1020 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1021 return isl_set_print_polylib(set, p, 1);
1022 else if (p->output_format == ISL_FORMAT_OMEGA)
1023 return isl_set_print_omega(set, p);
1024 else if (p->output_format == ISL_FORMAT_LATEX)
1025 return isl_map_print_latex((isl_map *)set, p, 1);
1026 isl_assert(set->ctx, 0, goto error);
1027 error:
1028 isl_printer_free(p);
1029 return NULL;
1032 void isl_set_print(struct isl_set *set, FILE *out, int indent,
1033 unsigned output_format)
1035 isl_printer *printer;
1037 if (!set)
1038 return;
1040 printer = isl_printer_to_file(set->ctx, out);
1041 printer = isl_printer_set_indent(printer, indent);
1042 printer = isl_printer_set_output_format(printer, output_format);
1043 printer = isl_printer_print_set(printer, set);
1045 isl_printer_free(printer);
1048 __isl_give isl_printer *isl_printer_print_map(__isl_take isl_printer *p,
1049 __isl_keep isl_map *map)
1051 if (!p || !map)
1052 goto error;
1054 if (p->output_format == ISL_FORMAT_ISL)
1055 return isl_map_print_isl(map, p, 0);
1056 else if (p->output_format == ISL_FORMAT_POLYLIB)
1057 return isl_map_print_polylib(map, p, 0);
1058 else if (p->output_format == ISL_FORMAT_EXT_POLYLIB)
1059 return isl_map_print_polylib(map, p, 1);
1060 else if (p->output_format == ISL_FORMAT_OMEGA)
1061 return isl_map_print_omega(map, p);
1062 else if (p->output_format == ISL_FORMAT_LATEX)
1063 return isl_map_print_latex(map, p, 0);
1064 isl_assert(map->ctx, 0, goto error);
1065 error:
1066 isl_printer_free(p);
1067 return NULL;
1070 struct isl_union_print_data {
1071 isl_printer *p;
1072 int set;
1073 int first;
1076 static int print_map_body(__isl_take isl_map *map, void *user)
1078 struct isl_union_print_data *data;
1079 data = (struct isl_union_print_data *)user;
1081 if (!data->first)
1082 data->p = isl_printer_print_str(data->p, "; ");
1083 data->first = 0;
1085 data->p = isl_map_print_isl_body(map, data->p, data->set);
1086 isl_map_free(map);
1088 return 0;
1091 static __isl_give isl_printer *isl_union_map_print_isl(
1092 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1094 struct isl_union_print_data data = { p, set, 1 };
1095 isl_space *dim;
1096 dim = isl_union_map_get_space(umap);
1097 if (isl_space_dim(dim, isl_dim_param) > 0) {
1098 p = print_tuple(dim, p, isl_dim_param, set, 0, NULL);
1099 p = isl_printer_print_str(p, s_to[0]);
1101 isl_space_free(dim);
1102 p = isl_printer_print_str(p, s_open_set[0]);
1103 isl_union_map_foreach_map(umap, &print_map_body, &data);
1104 p = data.p;
1105 p = isl_printer_print_str(p, s_close_set[0]);
1106 return p;
1109 static int print_latex_map_body(__isl_take isl_map *map, void *user)
1111 struct isl_union_print_data *data;
1112 data = (struct isl_union_print_data *)user;
1114 if (!data->first)
1115 data->p = isl_printer_print_str(data->p, " \\cup ");
1116 data->first = 0;
1118 data->p = isl_map_print_latex(map, data->p, data->set);
1119 isl_map_free(map);
1121 return 0;
1124 static __isl_give isl_printer *isl_union_map_print_latex(
1125 __isl_keep isl_union_map *umap, __isl_take isl_printer *p, int set)
1127 struct isl_union_print_data data = { p, set, 1 };
1128 isl_union_map_foreach_map(umap, &print_latex_map_body, &data);
1129 p = data.p;
1130 return p;
1133 __isl_give isl_printer *isl_printer_print_union_map(__isl_take isl_printer *p,
1134 __isl_keep isl_union_map *umap)
1136 if (!p || !umap)
1137 goto error;
1139 if (p->output_format == ISL_FORMAT_ISL)
1140 return isl_union_map_print_isl(umap, p, 0);
1141 if (p->output_format == ISL_FORMAT_LATEX)
1142 return isl_union_map_print_latex(umap, p, 0);
1144 isl_die(p->ctx, isl_error_invalid,
1145 "invalid output format for isl_union_map", goto error);
1146 error:
1147 isl_printer_free(p);
1148 return NULL;
1151 __isl_give isl_printer *isl_printer_print_union_set(__isl_take isl_printer *p,
1152 __isl_keep isl_union_set *uset)
1154 if (!p || !uset)
1155 goto error;
1157 if (p->output_format == ISL_FORMAT_ISL)
1158 return isl_union_map_print_isl((isl_union_map *)uset, p, 1);
1159 if (p->output_format == ISL_FORMAT_LATEX)
1160 return isl_union_map_print_latex((isl_union_map *)uset, p, 1);
1162 isl_die(p->ctx, isl_error_invalid,
1163 "invalid output format for isl_union_set", goto error);
1164 error:
1165 isl_printer_free(p);
1166 return NULL;
1169 void isl_map_print(__isl_keep isl_map *map, FILE *out, int indent,
1170 unsigned output_format)
1172 isl_printer *printer;
1174 if (!map)
1175 return;
1177 printer = isl_printer_to_file(map->ctx, out);
1178 printer = isl_printer_set_indent(printer, indent);
1179 printer = isl_printer_set_output_format(printer, output_format);
1180 printer = isl_printer_print_map(printer, map);
1182 isl_printer_free(printer);
1185 static int upoly_rec_n_non_zero(__isl_keep struct isl_upoly_rec *rec)
1187 int i;
1188 int n;
1190 for (i = 0, n = 0; i < rec->n; ++i)
1191 if (!isl_upoly_is_zero(rec->p[i]))
1192 ++n;
1194 return n;
1197 static __isl_give isl_printer *print_div(__isl_keep isl_space *dim,
1198 __isl_keep isl_mat *div, int pos, __isl_take isl_printer *p)
1200 int c = p->output_format == ISL_FORMAT_C;
1201 p = isl_printer_print_str(p, c ? "floord(" : "[(");
1202 p = print_affine_of_len(dim, div, p,
1203 div->row[pos] + 1, div->n_col - 1, 1);
1204 p = isl_printer_print_str(p, c ? ", " : ")/");
1205 p = isl_printer_print_isl_int(p, div->row[pos][0]);
1206 p = isl_printer_print_str(p, c ? ")" : "]");
1207 return p;
1210 static __isl_give isl_printer *upoly_print_cst(__isl_keep struct isl_upoly *up,
1211 __isl_take isl_printer *p, int first)
1213 struct isl_upoly_cst *cst;
1214 int neg;
1216 cst = isl_upoly_as_cst(up);
1217 if (!cst)
1218 goto error;
1219 neg = !first && isl_int_is_neg(cst->n);
1220 if (!first)
1221 p = isl_printer_print_str(p, neg ? " - " : " + ");
1222 if (neg)
1223 isl_int_neg(cst->n, cst->n);
1224 if (isl_int_is_zero(cst->d)) {
1225 int sgn = isl_int_sgn(cst->n);
1226 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1227 sgn == 0 ? "NaN" : "infty");
1228 } else
1229 p = isl_printer_print_isl_int(p, cst->n);
1230 if (neg)
1231 isl_int_neg(cst->n, cst->n);
1232 if (!isl_int_is_zero(cst->d) && !isl_int_is_one(cst->d)) {
1233 p = isl_printer_print_str(p, "/");
1234 p = isl_printer_print_isl_int(p, cst->d);
1236 return p;
1237 error:
1238 isl_printer_free(p);
1239 return NULL;
1242 static __isl_give isl_printer *print_base(__isl_take isl_printer *p,
1243 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var)
1245 unsigned total;
1247 total = isl_space_dim(dim, isl_dim_all);
1248 if (var < total)
1249 p = print_term(dim, NULL, dim->ctx->one, 1 + var, p, 1, 0);
1250 else
1251 p = print_div(dim, div, var - total, p);
1252 return p;
1255 static __isl_give isl_printer *print_pow(__isl_take isl_printer *p,
1256 __isl_keep isl_space *dim, __isl_keep isl_mat *div, int var, int exp)
1258 p = print_base(p, dim, div, var);
1259 if (exp == 1)
1260 return p;
1261 if (p->output_format == ISL_FORMAT_C) {
1262 int i;
1263 for (i = 1; i < exp; ++i) {
1264 p = isl_printer_print_str(p, "*");
1265 p = print_base(p, dim, div, var);
1267 } else {
1268 p = isl_printer_print_str(p, "^");
1269 p = isl_printer_print_int(p, exp);
1271 return p;
1274 static __isl_give isl_printer *upoly_print(__isl_keep struct isl_upoly *up,
1275 __isl_keep isl_space *dim, __isl_keep isl_mat *div,
1276 __isl_take isl_printer *p, int outer)
1278 int i, n, first, print_parens;
1279 struct isl_upoly_rec *rec;
1281 if (!p || !up || !dim || !div)
1282 goto error;
1284 if (isl_upoly_is_cst(up))
1285 return upoly_print_cst(up, p, 1);
1287 rec = isl_upoly_as_rec(up);
1288 if (!rec)
1289 goto error;
1290 n = upoly_rec_n_non_zero(rec);
1291 print_parens = n > 1 ||
1292 (outer && rec->up.var >= isl_space_dim(dim, isl_dim_all));
1293 if (print_parens)
1294 p = isl_printer_print_str(p, "(");
1295 for (i = 0, first = 1; i < rec->n; ++i) {
1296 if (isl_upoly_is_zero(rec->p[i]))
1297 continue;
1298 if (isl_upoly_is_negone(rec->p[i])) {
1299 if (!i)
1300 p = isl_printer_print_str(p, "-1");
1301 else if (first)
1302 p = isl_printer_print_str(p, "-");
1303 else
1304 p = isl_printer_print_str(p, " - ");
1305 } else if (isl_upoly_is_cst(rec->p[i]) &&
1306 !isl_upoly_is_one(rec->p[i]))
1307 p = upoly_print_cst(rec->p[i], p, first);
1308 else {
1309 if (!first)
1310 p = isl_printer_print_str(p, " + ");
1311 if (i == 0 || !isl_upoly_is_one(rec->p[i]))
1312 p = upoly_print(rec->p[i], dim, div, p, 0);
1314 first = 0;
1315 if (i == 0)
1316 continue;
1317 if (!isl_upoly_is_one(rec->p[i]) &&
1318 !isl_upoly_is_negone(rec->p[i]))
1319 p = isl_printer_print_str(p, " * ");
1320 p = print_pow(p, dim, div, rec->up.var, i);
1322 if (print_parens)
1323 p = isl_printer_print_str(p, ")");
1324 return p;
1325 error:
1326 isl_printer_free(p);
1327 return NULL;
1330 static __isl_give isl_printer *print_qpolynomial(__isl_take isl_printer *p,
1331 __isl_keep isl_qpolynomial *qp)
1333 if (!p || !qp)
1334 goto error;
1335 p = upoly_print(qp->upoly, qp->dim, qp->div, p, 1);
1336 return p;
1337 error:
1338 isl_printer_free(p);
1339 return NULL;
1342 static __isl_give isl_printer *print_qpolynomial_isl(__isl_take isl_printer *p,
1343 __isl_keep isl_qpolynomial *qp)
1345 if (!p || !qp)
1346 goto error;
1348 if (isl_space_dim(qp->dim, isl_dim_param) > 0) {
1349 p = print_tuple(qp->dim, p, isl_dim_param, 0, 0, NULL);
1350 p = isl_printer_print_str(p, " -> ");
1352 p = isl_printer_print_str(p, "{ ");
1353 if (!isl_space_is_params(qp->dim)) {
1354 p = print_space(qp->dim, p, 1, 0, 0, NULL);
1355 p = isl_printer_print_str(p, " -> ");
1357 p = print_qpolynomial(p, qp);
1358 p = isl_printer_print_str(p, " }");
1359 return p;
1360 error:
1361 isl_printer_free(p);
1362 return NULL;
1365 static __isl_give isl_printer *print_qpolynomial_c(__isl_take isl_printer *p,
1366 __isl_keep isl_space *dim, __isl_keep isl_qpolynomial *qp)
1368 isl_int den;
1370 isl_int_init(den);
1371 isl_qpolynomial_get_den(qp, &den);
1372 if (!isl_int_is_one(den)) {
1373 isl_qpolynomial *f;
1374 p = isl_printer_print_str(p, "(");
1375 qp = isl_qpolynomial_copy(qp);
1376 f = isl_qpolynomial_rat_cst(isl_space_copy(qp->dim),
1377 den, qp->dim->ctx->one);
1378 qp = isl_qpolynomial_mul(qp, f);
1380 if (qp)
1381 p = upoly_print(qp->upoly, dim, qp->div, p, 0);
1382 if (!isl_int_is_one(den)) {
1383 p = isl_printer_print_str(p, ")/");
1384 p = isl_printer_print_isl_int(p, den);
1385 isl_qpolynomial_free(qp);
1387 isl_int_clear(den);
1388 return p;
1391 __isl_give isl_printer *isl_printer_print_qpolynomial(
1392 __isl_take isl_printer *p, __isl_keep isl_qpolynomial *qp)
1394 if (!p || !qp)
1395 goto error;
1397 if (p->output_format == ISL_FORMAT_ISL)
1398 return print_qpolynomial_isl(p, qp);
1399 else if (p->output_format == ISL_FORMAT_C)
1400 return print_qpolynomial_c(p, qp->dim, qp);
1401 else
1402 isl_die(qp->dim->ctx, isl_error_unsupported,
1403 "output format not supported for isl_qpolynomials",
1404 goto error);
1405 error:
1406 isl_printer_free(p);
1407 return NULL;
1410 void isl_qpolynomial_print(__isl_keep isl_qpolynomial *qp, FILE *out,
1411 unsigned output_format)
1413 isl_printer *p;
1415 if (!qp)
1416 return;
1418 isl_assert(qp->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1419 p = isl_printer_to_file(qp->dim->ctx, out);
1420 p = isl_printer_print_qpolynomial(p, qp);
1421 isl_printer_free(p);
1424 static __isl_give isl_printer *qpolynomial_fold_print(
1425 __isl_keep isl_qpolynomial_fold *fold, __isl_take isl_printer *p)
1427 int i;
1429 if (fold->type == isl_fold_min)
1430 p = isl_printer_print_str(p, "min");
1431 else if (fold->type == isl_fold_max)
1432 p = isl_printer_print_str(p, "max");
1433 p = isl_printer_print_str(p, "(");
1434 for (i = 0; i < fold->n; ++i) {
1435 if (i)
1436 p = isl_printer_print_str(p, ", ");
1437 p = print_qpolynomial(p, fold->qp[i]);
1439 p = isl_printer_print_str(p, ")");
1440 return p;
1443 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold *fold,
1444 FILE *out, unsigned output_format)
1446 isl_printer *p;
1448 if (!fold)
1449 return;
1451 isl_assert(fold->dim->ctx, output_format == ISL_FORMAT_ISL, return);
1453 p = isl_printer_to_file(fold->dim->ctx, out);
1454 p = isl_printer_print_qpolynomial_fold(p, fold);
1456 isl_printer_free(p);
1459 static __isl_give isl_printer *isl_pwqp_print_isl_body(
1460 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1462 int i = 0;
1464 for (i = 0; i < pwqp->n; ++i) {
1465 if (i)
1466 p = isl_printer_print_str(p, "; ");
1467 if (!isl_space_is_params(pwqp->dim)) {
1468 p = print_space(pwqp->p[i].set->dim, p, 1, 0, 0, NULL);
1469 p = isl_printer_print_str(p, " -> ");
1471 p = print_qpolynomial(p, pwqp->p[i].qp);
1472 p = print_disjuncts((isl_map *)pwqp->p[i].set, p, 1, 0);
1475 return p;
1478 static __isl_give isl_printer *print_pw_qpolynomial_isl(
1479 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1481 if (!p || !pwqp)
1482 goto error;
1484 if (isl_space_dim(pwqp->dim, isl_dim_param) > 0) {
1485 p = print_tuple(pwqp->dim, p, isl_dim_param, 0, 0, NULL);
1486 p = isl_printer_print_str(p, " -> ");
1488 p = isl_printer_print_str(p, "{ ");
1489 if (pwqp->n == 0) {
1490 if (!isl_space_is_params(pwqp->dim)) {
1491 p = print_space(pwqp->dim, p, 1, 0, 0, NULL);
1492 p = isl_printer_print_str(p, " -> ");
1494 p = isl_printer_print_str(p, "0");
1496 p = isl_pwqp_print_isl_body(p, pwqp);
1497 p = isl_printer_print_str(p, " }");
1498 return p;
1499 error:
1500 isl_printer_free(p);
1501 return NULL;
1504 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial *pwqp, FILE *out,
1505 unsigned output_format)
1507 isl_printer *p;
1509 if (!pwqp)
1510 return;
1512 p = isl_printer_to_file(pwqp->dim->ctx, out);
1513 p = isl_printer_set_output_format(p, output_format);
1514 p = isl_printer_print_pw_qpolynomial(p, pwqp);
1516 isl_printer_free(p);
1519 static __isl_give isl_printer *isl_pwf_print_isl_body(
1520 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1522 int i = 0;
1524 for (i = 0; i < pwf->n; ++i) {
1525 if (i)
1526 p = isl_printer_print_str(p, "; ");
1527 if (!isl_space_is_params(pwf->dim)) {
1528 p = print_space(pwf->p[i].set->dim, p, 1, 0, 0, NULL);
1529 p = isl_printer_print_str(p, " -> ");
1531 p = qpolynomial_fold_print(pwf->p[i].fold, p);
1532 p = print_disjuncts((isl_map *)pwf->p[i].set, p, 1, 0);
1535 return p;
1538 static __isl_give isl_printer *print_pw_qpolynomial_fold_isl(
1539 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1541 if (isl_space_dim(pwf->dim, isl_dim_param) > 0) {
1542 p = print_tuple(pwf->dim, p, isl_dim_param, 0, 0, NULL);
1543 p = isl_printer_print_str(p, " -> ");
1545 p = isl_printer_print_str(p, "{ ");
1546 if (pwf->n == 0) {
1547 if (!isl_space_is_params(pwf->dim)) {
1548 p = print_space(pwf->dim, p, 1, 0, 0, NULL);
1549 p = isl_printer_print_str(p, " -> ");
1551 p = isl_printer_print_str(p, "0");
1553 p = isl_pwf_print_isl_body(p, pwf);
1554 p = isl_printer_print_str(p, " }");
1555 return p;
1558 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1559 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c);
1561 static __isl_give isl_printer *print_name_c(__isl_take isl_printer *p,
1562 __isl_keep isl_space *dim,
1563 __isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned pos)
1565 if (type == isl_dim_div) {
1566 p = isl_printer_print_str(p, "floord(");
1567 p = print_affine_c(p, dim, bset, bset->div[pos] + 1);
1568 p = isl_printer_print_str(p, ", ");
1569 p = isl_printer_print_isl_int(p, bset->div[pos][0]);
1570 p = isl_printer_print_str(p, ")");
1571 } else {
1572 const char *name;
1574 name = isl_space_get_dim_name(dim, type, pos);
1575 if (!name)
1576 name = "UNNAMED";
1577 p = isl_printer_print_str(p, name);
1579 return p;
1582 static __isl_give isl_printer *print_term_c(__isl_take isl_printer *p,
1583 __isl_keep isl_space *dim,
1584 __isl_keep isl_basic_set *bset, isl_int c, unsigned pos)
1586 enum isl_dim_type type;
1588 if (pos == 0)
1589 return isl_printer_print_isl_int(p, c);
1591 if (isl_int_is_one(c))
1593 else if (isl_int_is_negone(c))
1594 p = isl_printer_print_str(p, "-");
1595 else {
1596 p = isl_printer_print_isl_int(p, c);
1597 p = isl_printer_print_str(p, "*");
1599 type = pos2type(dim, &pos);
1600 p = print_name_c(p, dim, bset, type, pos);
1601 return p;
1604 static __isl_give isl_printer *print_partial_affine_c(__isl_take isl_printer *p,
1605 __isl_keep isl_space *dim,
1606 __isl_keep isl_basic_set *bset, isl_int *c, unsigned len)
1608 int i;
1609 int first;
1611 for (i = 0, first = 1; i < len; ++i) {
1612 int flip = 0;
1613 if (isl_int_is_zero(c[i]))
1614 continue;
1615 if (!first) {
1616 if (isl_int_is_neg(c[i])) {
1617 flip = 1;
1618 isl_int_neg(c[i], c[i]);
1619 p = isl_printer_print_str(p, " - ");
1620 } else
1621 p = isl_printer_print_str(p, " + ");
1623 first = 0;
1624 p = print_term_c(p, dim, bset, c[i], i);
1625 if (flip)
1626 isl_int_neg(c[i], c[i]);
1628 if (first)
1629 p = isl_printer_print_str(p, "0");
1630 return p;
1633 static __isl_give isl_printer *print_affine_c(__isl_take isl_printer *p,
1634 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset, isl_int *c)
1636 unsigned len = 1 + isl_basic_set_total_dim(bset);
1637 return print_partial_affine_c(p, dim, bset, c, len);
1640 static __isl_give isl_printer *print_constraint_c(__isl_take isl_printer *p,
1641 __isl_keep isl_space *dim,
1642 __isl_keep isl_basic_set *bset, isl_int *c, const char *op, int first)
1644 if (!first)
1645 p = isl_printer_print_str(p, " && ");
1647 p = print_affine_c(p, dim, bset, c);
1648 p = isl_printer_print_str(p, " ");
1649 p = isl_printer_print_str(p, op);
1650 p = isl_printer_print_str(p, " 0");
1651 return p;
1654 static __isl_give isl_printer *print_basic_set_c(__isl_take isl_printer *p,
1655 __isl_keep isl_space *dim, __isl_keep isl_basic_set *bset)
1657 int i, j;
1658 unsigned n_div = isl_basic_set_dim(bset, isl_dim_div);
1659 unsigned total = isl_basic_set_total_dim(bset) - n_div;
1661 for (i = 0; i < bset->n_eq; ++i) {
1662 j = isl_seq_last_non_zero(bset->eq[i] + 1 + total, n_div);
1663 if (j < 0)
1664 p = print_constraint_c(p, dim, bset,
1665 bset->eq[i], "==", !i);
1666 else {
1667 if (i)
1668 p = isl_printer_print_str(p, " && ");
1669 p = isl_printer_print_str(p, "(");
1670 p = print_partial_affine_c(p, dim, bset, bset->eq[i],
1671 1 + total + j);
1672 p = isl_printer_print_str(p, ") % ");
1673 p = isl_printer_print_isl_int(p,
1674 bset->eq[i][1 + total + j]);
1675 p = isl_printer_print_str(p, " == 0");
1678 for (i = 0; i < bset->n_ineq; ++i)
1679 p = print_constraint_c(p, dim, bset, bset->ineq[i], ">=",
1680 !bset->n_eq && !i);
1681 return p;
1684 static __isl_give isl_printer *print_set_c(__isl_take isl_printer *p,
1685 __isl_keep isl_space *dim, __isl_keep isl_set *set)
1687 int i;
1689 if (set->n == 0)
1690 p = isl_printer_print_str(p, "0");
1692 for (i = 0; i < set->n; ++i) {
1693 if (i)
1694 p = isl_printer_print_str(p, " || ");
1695 if (set->n > 1)
1696 p = isl_printer_print_str(p, "(");
1697 p = print_basic_set_c(p, dim, set->p[i]);
1698 if (set->n > 1)
1699 p = isl_printer_print_str(p, ")");
1701 return p;
1704 static __isl_give isl_printer *print_pw_qpolynomial_c(
1705 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1707 int i;
1709 if (pwqp->n == 1 && isl_set_plain_is_universe(pwqp->p[0].set))
1710 return print_qpolynomial_c(p, pwqp->dim, pwqp->p[0].qp);
1712 for (i = 0; i < pwqp->n; ++i) {
1713 p = isl_printer_print_str(p, "(");
1714 p = print_set_c(p, pwqp->dim, pwqp->p[i].set);
1715 p = isl_printer_print_str(p, ") ? (");
1716 p = print_qpolynomial_c(p, pwqp->dim, pwqp->p[i].qp);
1717 p = isl_printer_print_str(p, ") : ");
1720 p = isl_printer_print_str(p, "0");
1721 return p;
1724 __isl_give isl_printer *isl_printer_print_pw_qpolynomial(
1725 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial *pwqp)
1727 if (!p || !pwqp)
1728 goto error;
1730 if (p->output_format == ISL_FORMAT_ISL)
1731 return print_pw_qpolynomial_isl(p, pwqp);
1732 else if (p->output_format == ISL_FORMAT_C)
1733 return print_pw_qpolynomial_c(p, pwqp);
1734 isl_assert(p->ctx, 0, goto error);
1735 error:
1736 isl_printer_free(p);
1737 return NULL;
1740 static int print_pwqp_body(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1742 struct isl_union_print_data *data;
1743 data = (struct isl_union_print_data *)user;
1745 if (!data->first)
1746 data->p = isl_printer_print_str(data->p, "; ");
1747 data->first = 0;
1749 data->p = isl_pwqp_print_isl_body(data->p, pwqp);
1750 isl_pw_qpolynomial_free(pwqp);
1752 return 0;
1755 static __isl_give isl_printer *print_union_pw_qpolynomial_isl(
1756 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1758 struct isl_union_print_data data = { p, 1, 1 };
1759 isl_space *dim;
1760 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1761 if (isl_space_dim(dim, isl_dim_param) > 0) {
1762 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1763 p = isl_printer_print_str(p, " -> ");
1765 isl_space_free(dim);
1766 p = isl_printer_print_str(p, "{ ");
1767 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &print_pwqp_body,
1768 &data);
1769 p = data.p;
1770 p = isl_printer_print_str(p, " }");
1771 return p;
1774 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial(
1775 __isl_take isl_printer *p, __isl_keep isl_union_pw_qpolynomial *upwqp)
1777 if (!p || !upwqp)
1778 goto error;
1780 if (p->output_format == ISL_FORMAT_ISL)
1781 return print_union_pw_qpolynomial_isl(p, upwqp);
1782 isl_die(p->ctx, isl_error_invalid,
1783 "invalid output format for isl_union_pw_qpolynomial",
1784 goto error);
1785 error:
1786 isl_printer_free(p);
1787 return NULL;
1790 static __isl_give isl_printer *print_qpolynomial_fold_c(
1791 __isl_take isl_printer *p, __isl_keep isl_space *dim,
1792 __isl_keep isl_qpolynomial_fold *fold)
1794 int i;
1796 for (i = 0; i < fold->n - 1; ++i)
1797 if (fold->type == isl_fold_min)
1798 p = isl_printer_print_str(p, "min(");
1799 else if (fold->type == isl_fold_max)
1800 p = isl_printer_print_str(p, "max(");
1802 for (i = 0; i < fold->n; ++i) {
1803 if (i)
1804 p = isl_printer_print_str(p, ", ");
1805 p = print_qpolynomial_c(p, dim, fold->qp[i]);
1806 if (i)
1807 p = isl_printer_print_str(p, ")");
1809 return p;
1812 __isl_give isl_printer *isl_printer_print_qpolynomial_fold(
1813 __isl_take isl_printer *p, __isl_keep isl_qpolynomial_fold *fold)
1815 if (!p || !fold)
1816 goto error;
1817 if (p->output_format == ISL_FORMAT_ISL)
1818 return qpolynomial_fold_print(fold, p);
1819 else if (p->output_format == ISL_FORMAT_C)
1820 return print_qpolynomial_fold_c(p, fold->dim, fold);
1821 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
1822 goto error);
1823 error:
1824 isl_printer_free(p);
1825 return NULL;
1828 static __isl_give isl_printer *print_pw_qpolynomial_fold_c(
1829 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1831 int i;
1833 if (pwf->n == 1 && isl_set_plain_is_universe(pwf->p[0].set))
1834 return print_qpolynomial_fold_c(p, pwf->dim, pwf->p[0].fold);
1836 for (i = 0; i < pwf->n; ++i) {
1837 p = isl_printer_print_str(p, "(");
1838 p = print_set_c(p, pwf->dim, pwf->p[i].set);
1839 p = isl_printer_print_str(p, ") ? (");
1840 p = print_qpolynomial_fold_c(p, pwf->dim, pwf->p[i].fold);
1841 p = isl_printer_print_str(p, ") : ");
1844 p = isl_printer_print_str(p, "0");
1845 return p;
1848 __isl_give isl_printer *isl_printer_print_pw_qpolynomial_fold(
1849 __isl_take isl_printer *p, __isl_keep isl_pw_qpolynomial_fold *pwf)
1851 if (!p || !pwf)
1852 goto error;
1854 if (p->output_format == ISL_FORMAT_ISL)
1855 return print_pw_qpolynomial_fold_isl(p, pwf);
1856 else if (p->output_format == ISL_FORMAT_C)
1857 return print_pw_qpolynomial_fold_c(p, pwf);
1858 isl_assert(p->ctx, 0, goto error);
1859 error:
1860 isl_printer_free(p);
1861 return NULL;
1864 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold *pwf,
1865 FILE *out, unsigned output_format)
1867 isl_printer *p;
1869 if (!pwf)
1870 return;
1872 p = isl_printer_to_file(pwf->dim->ctx, out);
1873 p = isl_printer_set_output_format(p, output_format);
1874 p = isl_printer_print_pw_qpolynomial_fold(p, pwf);
1876 isl_printer_free(p);
1879 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1881 struct isl_union_print_data *data;
1882 data = (struct isl_union_print_data *)user;
1884 if (!data->first)
1885 data->p = isl_printer_print_str(data->p, "; ");
1886 data->first = 0;
1888 data->p = isl_pwf_print_isl_body(data->p, pwf);
1889 isl_pw_qpolynomial_fold_free(pwf);
1891 return 0;
1894 static __isl_give isl_printer *print_union_pw_qpolynomial_fold_isl(
1895 __isl_take isl_printer *p,
1896 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1898 struct isl_union_print_data data = { p, 1, 1 };
1899 isl_space *dim;
1900 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1901 if (isl_space_dim(dim, isl_dim_param) > 0) {
1902 p = print_tuple(dim, p, isl_dim_param, 1, 0, NULL);
1903 p = isl_printer_print_str(p, " -> ");
1905 isl_space_free(dim);
1906 p = isl_printer_print_str(p, "{ ");
1907 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf,
1908 &print_pwf_body, &data);
1909 p = data.p;
1910 p = isl_printer_print_str(p, " }");
1911 return p;
1914 __isl_give isl_printer *isl_printer_print_union_pw_qpolynomial_fold(
1915 __isl_take isl_printer *p,
1916 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1918 if (!p || !upwf)
1919 goto error;
1921 if (p->output_format == ISL_FORMAT_ISL)
1922 return print_union_pw_qpolynomial_fold_isl(p, upwf);
1923 isl_die(p->ctx, isl_error_invalid,
1924 "invalid output format for isl_union_pw_qpolynomial_fold",
1925 goto error);
1926 error:
1927 isl_printer_free(p);
1928 return NULL;
1931 __isl_give isl_printer *isl_printer_print_constraint(__isl_take isl_printer *p,
1932 __isl_keep isl_constraint *c)
1934 isl_basic_map *bmap;
1936 if (!p || !c)
1937 goto error;
1939 bmap = isl_basic_map_from_constraint(isl_constraint_copy(c));
1940 p = isl_printer_print_basic_map(p, bmap);
1941 isl_basic_map_free(bmap);
1942 return p;
1943 error:
1944 isl_printer_free(p);
1945 return NULL;
1948 __isl_give isl_printer *isl_printer_print_space(__isl_take isl_printer *p,
1949 __isl_keep isl_space *dim)
1951 if (!dim)
1952 goto error;
1954 if (isl_space_dim(dim, isl_dim_param) > 0) {
1955 p = print_tuple(dim, p, isl_dim_param, 0, 0, NULL);
1956 p = isl_printer_print_str(p, " -> ");
1959 p = isl_printer_print_str(p, "{ ");
1960 if (isl_space_is_params(dim))
1961 p = isl_printer_print_str(p, s_such_that[0]);
1962 else
1963 p = print_space(dim, p, 0, 0, 0, NULL);
1964 p = isl_printer_print_str(p, " }");
1966 return p;
1967 error:
1968 isl_printer_free(p);
1969 return NULL;
1972 __isl_give isl_printer *isl_printer_print_local_space(__isl_take isl_printer *p,
1973 __isl_keep isl_local_space *ls)
1975 unsigned total;
1976 unsigned n_div;
1978 if (!ls)
1979 goto error;
1981 total = isl_local_space_dim(ls, isl_dim_all);
1982 if (isl_local_space_dim(ls, isl_dim_param) > 0) {
1983 p = print_tuple(ls->dim, p, isl_dim_param, 0, 0, NULL);
1984 p = isl_printer_print_str(p, " -> ");
1986 p = isl_printer_print_str(p, "{ ");
1987 p = print_space(ls->dim, p, 0, 0, 0, NULL);
1988 n_div = isl_local_space_dim(ls, isl_dim_div);
1989 if (n_div > 0) {
1990 int i;
1991 p = isl_printer_print_str(p, " : ");
1992 p = isl_printer_print_str(p, s_open_exists[0]);
1993 for (i = 0; i < n_div; ++i) {
1994 if (i)
1995 p = isl_printer_print_str(p, ", ");
1996 p = print_name(ls->dim, p, isl_dim_div, i, 0, 0);
1997 if (isl_int_is_zero(ls->div->row[i][0]))
1998 continue;
1999 p = isl_printer_print_str(p, " = [(");
2000 p = print_affine_of_len(ls->dim, ls->div, p,
2001 ls->div->row[i] + 1, 1 + total, 0);
2002 p = isl_printer_print_str(p, ")/");
2003 p = isl_printer_print_isl_int(p, ls->div->row[i][0]);
2004 p = isl_printer_print_str(p, "]");
2006 } else if (isl_space_is_params(ls->dim))
2007 p = isl_printer_print_str(p, s_such_that[0]);
2008 p = isl_printer_print_str(p, " }");
2009 return p;
2010 error:
2011 isl_printer_free(p);
2012 return NULL;
2015 static __isl_give isl_printer *print_aff(__isl_take isl_printer *p,
2016 __isl_keep isl_aff *aff)
2018 unsigned total;
2020 total = isl_local_space_dim(aff->ls, isl_dim_all);
2021 if (isl_space_is_params(aff->ls->dim))
2023 else {
2024 p = print_tuple(aff->ls->dim, p, isl_dim_set, 1, 0, NULL);
2025 p = isl_printer_print_str(p, " -> ");
2027 p = isl_printer_print_str(p, "[");
2028 p = isl_printer_print_str(p, "(");
2029 p = print_affine_of_len(aff->ls->dim, aff->ls->div, p,
2030 aff->v->el + 1, 1 + total, 1);
2031 if (isl_int_is_one(aff->v->el[0]))
2032 p = isl_printer_print_str(p, ")");
2033 else {
2034 p = isl_printer_print_str(p, ")/");
2035 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2037 p = isl_printer_print_str(p, "]");
2039 return p;
2042 static __isl_give isl_printer *print_aff_isl(__isl_take isl_printer *p,
2043 __isl_keep isl_aff *aff)
2045 if (!aff)
2046 goto error;
2048 if (isl_local_space_dim(aff->ls, isl_dim_param) > 0) {
2049 p = print_tuple(aff->ls->dim, p, isl_dim_param, 0, 0, NULL);
2050 p = isl_printer_print_str(p, " -> ");
2052 p = isl_printer_print_str(p, "{ ");
2053 p = print_aff(p, aff);
2054 p = isl_printer_print_str(p, " }");
2055 return p;
2056 error:
2057 isl_printer_free(p);
2058 return NULL;
2061 static __isl_give isl_printer *print_pw_aff_isl(__isl_take isl_printer *p,
2062 __isl_keep isl_pw_aff *pwaff)
2064 int i;
2066 if (!pwaff)
2067 goto error;
2069 if (isl_space_dim(pwaff->dim, isl_dim_param) > 0) {
2070 p = print_tuple(pwaff->dim, p, isl_dim_param, 0, 0, NULL);
2071 p = isl_printer_print_str(p, " -> ");
2073 p = isl_printer_print_str(p, "{ ");
2074 for (i = 0; i < pwaff->n; ++i) {
2075 if (i)
2076 p = isl_printer_print_str(p, "; ");
2077 p = print_aff(p, pwaff->p[i].aff);
2078 p = print_disjuncts((isl_map *)pwaff->p[i].set, p, 1, 0);
2080 p = isl_printer_print_str(p, " }");
2081 return p;
2082 error:
2083 isl_printer_free(p);
2084 return NULL;
2087 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2088 __isl_keep isl_local_space *ls, isl_int *c);
2090 static __isl_give isl_printer *print_ls_name_c(__isl_take isl_printer *p,
2091 __isl_keep isl_local_space *ls, enum isl_dim_type type, unsigned pos)
2093 if (type == isl_dim_div) {
2094 p = isl_printer_print_str(p, "floord(");
2095 p = print_ls_affine_c(p, ls, ls->div->row[pos] + 1);
2096 p = isl_printer_print_str(p, ", ");
2097 p = isl_printer_print_isl_int(p, ls->div->row[pos][0]);
2098 p = isl_printer_print_str(p, ")");
2099 } else {
2100 const char *name;
2102 name = isl_space_get_dim_name(ls->dim, type, pos);
2103 if (!name)
2104 name = "UNNAMED";
2105 p = isl_printer_print_str(p, name);
2107 return p;
2110 static __isl_give isl_printer *print_ls_term_c(__isl_take isl_printer *p,
2111 __isl_keep isl_local_space *ls, isl_int c, unsigned pos)
2113 enum isl_dim_type type;
2115 if (pos == 0)
2116 return isl_printer_print_isl_int(p, c);
2118 if (isl_int_is_one(c))
2120 else if (isl_int_is_negone(c))
2121 p = isl_printer_print_str(p, "-");
2122 else {
2123 p = isl_printer_print_isl_int(p, c);
2124 p = isl_printer_print_str(p, "*");
2126 type = pos2type(ls->dim, &pos);
2127 p = print_ls_name_c(p, ls, type, pos);
2128 return p;
2131 static __isl_give isl_printer *print_ls_partial_affine_c(
2132 __isl_take isl_printer *p, __isl_keep isl_local_space *ls,
2133 isl_int *c, unsigned len)
2135 int i;
2136 int first;
2138 for (i = 0, first = 1; i < len; ++i) {
2139 int flip = 0;
2140 if (isl_int_is_zero(c[i]))
2141 continue;
2142 if (!first) {
2143 if (isl_int_is_neg(c[i])) {
2144 flip = 1;
2145 isl_int_neg(c[i], c[i]);
2146 p = isl_printer_print_str(p, " - ");
2147 } else
2148 p = isl_printer_print_str(p, " + ");
2150 first = 0;
2151 p = print_ls_term_c(p, ls, c[i], i);
2152 if (flip)
2153 isl_int_neg(c[i], c[i]);
2155 if (first)
2156 p = isl_printer_print_str(p, "0");
2157 return p;
2160 static __isl_give isl_printer *print_ls_affine_c(__isl_take isl_printer *p,
2161 __isl_keep isl_local_space *ls, isl_int *c)
2163 unsigned len = 1 + isl_local_space_dim(ls, isl_dim_all);
2164 return print_ls_partial_affine_c(p, ls, c, len);
2167 static __isl_give isl_printer *print_aff_c(__isl_take isl_printer *p,
2168 __isl_keep isl_aff *aff)
2170 unsigned total;
2172 total = isl_local_space_dim(aff->ls, isl_dim_all);
2173 p = isl_printer_print_str(p, "(");
2174 p = print_ls_partial_affine_c(p, aff->ls, aff->v->el + 1, 1 + total);
2175 if (isl_int_is_one(aff->v->el[0]))
2176 p = isl_printer_print_str(p, ")");
2177 else {
2178 p = isl_printer_print_str(p, ")/");
2179 p = isl_printer_print_isl_int(p, aff->v->el[0]);
2181 return p;
2184 static __isl_give isl_printer *print_pw_aff_c(__isl_take isl_printer *p,
2185 __isl_keep isl_pw_aff *pwaff)
2187 int i;
2189 if (pwaff->n < 1)
2190 isl_die(p->ctx, isl_error_unsupported,
2191 "cannot print empty isl_pw_aff in C format", goto error);
2193 for (i = 0; i < pwaff->n - 1; ++i) {
2194 p = isl_printer_print_str(p, "(");
2195 p = print_set_c(p, pwaff->dim, pwaff->p[i].set);
2196 p = isl_printer_print_str(p, ") ? (");
2197 p = print_aff_c(p, pwaff->p[i].aff);
2198 p = isl_printer_print_str(p, ") : ");
2201 return print_aff_c(p, pwaff->p[pwaff->n - 1].aff);
2202 error:
2203 isl_printer_free(p);
2204 return NULL;
2207 __isl_give isl_printer *isl_printer_print_aff(__isl_take isl_printer *p,
2208 __isl_keep isl_aff *aff)
2210 if (!p || !aff)
2211 goto error;
2213 if (p->output_format == ISL_FORMAT_ISL)
2214 return print_aff_isl(p, aff);
2215 else if (p->output_format == ISL_FORMAT_C)
2216 return print_aff_c(p, aff);
2217 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2218 goto error);
2219 error:
2220 isl_printer_free(p);
2221 return NULL;
2224 __isl_give isl_printer *isl_printer_print_pw_aff(__isl_take isl_printer *p,
2225 __isl_keep isl_pw_aff *pwaff)
2227 if (!p || !pwaff)
2228 goto error;
2230 if (p->output_format == ISL_FORMAT_ISL)
2231 return print_pw_aff_isl(p, pwaff);
2232 else if (p->output_format == ISL_FORMAT_C)
2233 return print_pw_aff_c(p, pwaff);
2234 isl_die(p->ctx, isl_error_unsupported, "unsupported output format",
2235 goto error);
2236 error:
2237 isl_printer_free(p);
2238 return NULL;