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
17 #include <isl_polynomial_private.h>
18 #include <isl_printer_private.h>
19 #include <isl_dim_private.h>
20 #include <isl_map_private.h>
21 #include <isl_mat_private.h>
22 #include <isl_union_map.h>
24 static const char *s_to
[2] = { " -> ", " \\to " };
25 static const char *s_and
[2] = { " and ", " \\wedge " };
26 static const char *s_or
[2] = { " or ", " \\vee " };
27 static const char *s_le
[2] = { "<=", "\\le" };
28 static const char *s_ge
[2] = { ">=", "\\ge" };
29 static const char *s_open_set
[2] = { "{ ", "\\{\\, " };
30 static const char *s_close_set
[2] = { " }", " \\,\\}" };
31 static const char *s_open_list
[2] = { "[", "(" };
32 static const char *s_close_list
[2] = { "]", ")" };
33 static const char *s_such_that
[2] = { " : ", " \\mid " };
34 static const char *s_open_exists
[2] = { "exists (", "\\exists \\, " };
35 static const char *s_close_exists
[2] = { ")", "" };
36 static const char *s_div_prefix
[2] = { "e", "\\alpha_" };
37 static const char *s_param_prefix
[2] = { "p", "p_" };
38 static const char *s_input_prefix
[2] = { "i", "i_" };
39 static const char *s_output_prefix
[2] = { "o", "o_" };
41 static __isl_give isl_printer
*print_constraint_polylib(
42 struct isl_basic_map
*bmap
, int ineq
, int n
, __isl_take isl_printer
*p
)
45 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
46 unsigned n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
47 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
48 isl_int
*c
= ineq
? bmap
->ineq
[n
] : bmap
->eq
[n
];
50 p
= isl_printer_start_line(p
);
51 p
= isl_printer_print_int(p
, ineq
);
52 for (i
= 0; i
< n_out
; ++i
) {
53 p
= isl_printer_print_str(p
, " ");
54 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+n_in
+i
]);
56 for (i
= 0; i
< n_in
; ++i
) {
57 p
= isl_printer_print_str(p
, " ");
58 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+i
]);
60 for (i
= 0; i
< bmap
->n_div
; ++i
) {
61 p
= isl_printer_print_str(p
, " ");
62 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+n_in
+n_out
+i
]);
64 for (i
= 0; i
< nparam
; ++i
) {
65 p
= isl_printer_print_str(p
, " ");
66 p
= isl_printer_print_isl_int(p
, c
[1+i
]);
68 p
= isl_printer_print_str(p
, " ");
69 p
= isl_printer_print_isl_int(p
, c
[0]);
70 p
= isl_printer_end_line(p
);
74 static __isl_give isl_printer
*print_constraints_polylib(
75 struct isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
79 p
= isl_printer_set_isl_int_width(p
, 5);
81 for (i
= 0; i
< bmap
->n_eq
; ++i
)
82 p
= print_constraint_polylib(bmap
, 0, i
, p
);
83 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
84 p
= print_constraint_polylib(bmap
, 1, i
, p
);
89 static __isl_give isl_printer
*bset_print_constraints_polylib(
90 struct isl_basic_set
*bset
, __isl_take isl_printer
*p
)
92 return print_constraints_polylib((struct isl_basic_map
*)bset
, p
);
95 static __isl_give isl_printer
*isl_basic_map_print_polylib(
96 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
98 unsigned total
= isl_basic_map_total_dim(bmap
);
99 p
= isl_printer_start_line(p
);
100 p
= isl_printer_print_int(p
, bmap
->n_eq
+ bmap
->n_ineq
);
101 p
= isl_printer_print_str(p
, " ");
102 p
= isl_printer_print_int(p
, 1 + total
+ 1);
103 p
= isl_printer_end_line(p
);
104 return print_constraints_polylib(bmap
, p
);
107 static __isl_give isl_printer
*isl_basic_set_print_polylib(
108 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
110 return isl_basic_map_print_polylib((struct isl_basic_map
*)bset
, p
);
113 static __isl_give isl_printer
*isl_map_print_polylib(__isl_keep isl_map
*map
,
114 __isl_take isl_printer
*p
)
118 p
= isl_printer_start_line(p
);
119 p
= isl_printer_print_int(p
, map
->n
);
120 p
= isl_printer_end_line(p
);
121 for (i
= 0; i
< map
->n
; ++i
) {
122 p
= isl_printer_start_line(p
);
123 p
= isl_printer_end_line(p
);
124 p
= isl_basic_map_print_polylib(map
->p
[i
], p
);
129 static __isl_give isl_printer
*isl_set_print_polylib(__isl_keep isl_set
*set
,
130 __isl_take isl_printer
*p
)
132 return isl_map_print_polylib((struct isl_map
*)set
, p
);
135 static int count_same_name(__isl_keep isl_dim
*dim
,
136 enum isl_dim_type type
, unsigned pos
, const char *name
)
142 for (t
= isl_dim_param
; t
<= type
&& t
<= isl_dim_out
; ++t
) {
143 s
= t
== type
? pos
: isl_dim_size(dim
, t
);
144 for (p
= 0; p
< s
; ++p
) {
145 const char *n
= isl_dim_get_name(dim
, t
, p
);
146 if (n
&& !strcmp(n
, name
))
153 static __isl_give isl_printer
*print_name(__isl_keep isl_dim
*dim
,
154 __isl_take isl_printer
*p
, enum isl_dim_type type
, unsigned pos
,
161 name
= type
== isl_dim_div
? NULL
: isl_dim_get_name(dim
, type
, pos
);
165 if (type
== isl_dim_param
)
166 prefix
= s_param_prefix
[latex
];
167 else if (type
== isl_dim_div
)
168 prefix
= s_div_prefix
[latex
];
169 else if (set
|| type
== isl_dim_in
)
170 prefix
= s_input_prefix
[latex
];
172 prefix
= s_output_prefix
[latex
];
173 snprintf(buffer
, sizeof(buffer
), "%s%d", prefix
, pos
);
176 primes
= count_same_name(dim
, name
== buffer
? isl_dim_div
: type
,
178 p
= isl_printer_print_str(p
, name
);
180 p
= isl_printer_print_str(p
, "'");
184 static enum isl_dim_type
pos2type(__isl_keep isl_dim
*dim
, unsigned *pos
)
186 enum isl_dim_type type
;
187 unsigned n_in
= isl_dim_size(dim
, isl_dim_in
);
188 unsigned n_out
= isl_dim_size(dim
, isl_dim_out
);
189 unsigned nparam
= isl_dim_size(dim
, isl_dim_param
);
191 if (*pos
< 1 + nparam
) {
192 type
= isl_dim_param
;
194 } else if (*pos
< 1 + nparam
+ n_in
) {
197 } else if (*pos
< 1 + nparam
+ n_in
+ n_out
) {
199 *pos
-= 1 + nparam
+ n_in
;
202 *pos
-= 1 + nparam
+ n_in
+ n_out
;
208 static __isl_give isl_printer
*print_term(__isl_keep isl_dim
*dim
,
209 isl_int c
, unsigned pos
, __isl_take isl_printer
*p
, int set
, int latex
)
211 enum isl_dim_type type
;
214 return isl_printer_print_isl_int(p
, c
);
216 if (isl_int_is_one(c
))
218 else if (isl_int_is_negone(c
))
219 p
= isl_printer_print_str(p
, "-");
221 p
= isl_printer_print_isl_int(p
, c
);
222 type
= pos2type(dim
, &pos
);
223 p
= print_name(dim
, p
, type
, pos
, set
, latex
);
227 static __isl_give isl_printer
*print_affine_of_len(__isl_keep isl_dim
*dim
,
228 __isl_take isl_printer
*p
, isl_int
*c
, int len
, int set
)
233 for (i
= 0, first
= 1; i
< len
; ++i
) {
235 if (isl_int_is_zero(c
[i
]))
238 if (isl_int_is_neg(c
[i
])) {
240 isl_int_neg(c
[i
], c
[i
]);
241 p
= isl_printer_print_str(p
, " - ");
243 p
= isl_printer_print_str(p
, " + ");
246 p
= print_term(dim
, c
[i
], i
, p
, set
, 0);
248 isl_int_neg(c
[i
], c
[i
]);
251 p
= isl_printer_print_str(p
, "0");
255 static __isl_give isl_printer
*print_affine(__isl_keep isl_basic_map
*bmap
,
256 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, isl_int
*c
, int set
)
258 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
259 return print_affine_of_len(dim
, p
, c
, len
, set
);
262 static int defining_equality(__isl_keep isl_basic_map
*eq
,
263 __isl_keep isl_dim
*dim
, enum isl_dim_type type
, int pos
)
271 pos
+= isl_dim_offset(dim
, type
);
272 total
= isl_basic_map_total_dim(eq
);
274 for (i
= 0; i
< eq
->n_eq
; ++i
) {
275 if (isl_seq_last_non_zero(eq
->eq
[i
] + 1, total
) != pos
)
277 if (isl_int_is_one(eq
->eq
[i
][1 + pos
]))
278 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], 1 + total
);
285 /* offset is the offset of local_dim inside global_type of global_dim.
287 static __isl_give isl_printer
*print_nested_var_list(__isl_take isl_printer
*p
,
288 __isl_keep isl_dim
*global_dim
, enum isl_dim_type global_type
,
289 __isl_keep isl_dim
*local_dim
, enum isl_dim_type local_type
,
290 int set
, int latex
, __isl_keep isl_basic_map
*eq
, int offset
)
294 if (global_dim
!= local_dim
&& local_type
== isl_dim_out
)
295 offset
+= local_dim
->n_in
;
297 for (i
= 0; i
< isl_dim_size(local_dim
, local_type
); ++i
) {
299 p
= isl_printer_print_str(p
, ", ");
300 j
= defining_equality(eq
, global_dim
, global_type
, offset
+ i
);
302 int pos
= 1 + isl_dim_offset(global_dim
, global_type
)
304 p
= print_affine_of_len(eq
->dim
, p
, eq
->eq
[j
], pos
, set
);
306 p
= print_name(global_dim
, p
, global_type
, offset
+ i
,
313 static __isl_give isl_printer
*print_var_list(__isl_keep isl_dim
*dim
,
314 __isl_take isl_printer
*p
, enum isl_dim_type type
,
315 int set
, int latex
, __isl_keep isl_basic_map
*eq
)
317 return print_nested_var_list(p
, dim
, type
, dim
, type
, set
, latex
, eq
, 0);
320 static __isl_give isl_printer
*print_nested_map_dim(__isl_take isl_printer
*p
,
321 __isl_keep isl_dim
*global_dim
, enum isl_dim_type global_type
,
322 __isl_keep isl_dim
*local_dim
,
323 int set
, int latex
, __isl_keep isl_basic_map
*eq
, int offset
);
325 static __isl_give isl_printer
*print_nested_tuple(__isl_take isl_printer
*p
,
326 __isl_keep isl_dim
*global_dim
, enum isl_dim_type global_type
,
327 __isl_keep isl_dim
*local_dim
, enum isl_dim_type local_type
,
328 int set
, int latex
, __isl_keep isl_basic_map
*eq
, int offset
)
330 const char *name
= NULL
;
331 unsigned n
= isl_dim_size(local_dim
, local_type
);
332 if ((local_type
== isl_dim_in
|| local_type
== isl_dim_out
)) {
333 name
= isl_dim_get_tuple_name(local_dim
, local_type
);
336 p
= isl_printer_print_str(p
, "\\mathrm{");
337 p
= isl_printer_print_str(p
, name
);
339 p
= isl_printer_print_str(p
, "}");
342 if (!latex
|| n
!= 1 || name
)
343 p
= isl_printer_print_str(p
, s_open_list
[latex
]);
344 if ((local_type
== isl_dim_in
|| local_type
== isl_dim_out
) &&
345 local_dim
->nested
[local_type
- isl_dim_in
]) {
346 if (global_dim
!= local_dim
&& local_type
== isl_dim_out
)
347 offset
+= local_dim
->n_in
;
348 p
= print_nested_map_dim(p
, global_dim
, global_type
,
349 local_dim
->nested
[local_type
- isl_dim_in
],
350 set
, latex
, eq
, offset
);
352 p
= print_nested_var_list(p
, global_dim
, global_type
,
353 local_dim
, local_type
, set
, latex
, eq
, offset
);
354 if (!latex
|| n
!= 1 || name
)
355 p
= isl_printer_print_str(p
, s_close_list
[latex
]);
359 static __isl_give isl_printer
*print_tuple(__isl_keep isl_dim
*dim
,
360 __isl_take isl_printer
*p
, enum isl_dim_type type
,
361 int set
, int latex
, __isl_keep isl_basic_map
*eq
)
363 return print_nested_tuple(p
, dim
, type
, dim
, type
, set
, latex
, eq
, 0);
366 static __isl_give isl_printer
*print_nested_map_dim(__isl_take isl_printer
*p
,
367 __isl_keep isl_dim
*global_dim
, enum isl_dim_type global_type
,
368 __isl_keep isl_dim
*local_dim
,
369 int set
, int latex
, __isl_keep isl_basic_map
*eq
, int offset
)
371 p
= print_nested_tuple(p
, global_dim
, global_type
,
372 local_dim
, isl_dim_in
, set
, latex
, eq
, offset
);
373 p
= isl_printer_print_str(p
, s_to
[latex
]);
374 p
= print_nested_tuple(p
, global_dim
, global_type
,
375 local_dim
, isl_dim_out
, set
, latex
, eq
, offset
);
380 static __isl_give isl_printer
*print_dim(__isl_keep isl_dim
*dim
,
381 __isl_take isl_printer
*p
, int set
, int latex
,
382 __isl_keep isl_basic_map
*eq
)
385 p
= print_tuple(dim
, p
, isl_dim_set
, 1, latex
, eq
);
387 p
= print_tuple(dim
, p
, isl_dim_in
, 0, latex
, eq
);
388 p
= isl_printer_print_str(p
, s_to
[latex
]);
389 p
= print_tuple(dim
, p
, isl_dim_out
, 0, latex
, eq
);
395 static __isl_give isl_printer
*print_omega_parameters(__isl_keep isl_dim
*dim
,
396 __isl_take isl_printer
*p
)
398 if (isl_dim_size(dim
, isl_dim_param
) == 0)
401 p
= isl_printer_start_line(p
);
402 p
= isl_printer_print_str(p
, "symbolic ");
403 p
= print_var_list(dim
, p
, isl_dim_param
, 0, 0, NULL
);
404 p
= isl_printer_print_str(p
, ";");
405 p
= isl_printer_end_line(p
);
409 static __isl_give isl_printer
*print_constraint(struct isl_basic_map
*bmap
,
410 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
,
411 isl_int
*c
, int last
, const char *op
, int first_constraint
, int set
,
414 if (!first_constraint
)
415 p
= isl_printer_print_str(p
, s_and
[latex
]);
417 isl_int_abs(c
[last
], c
[last
]);
419 p
= print_term(dim
, c
[last
], last
, p
, set
, latex
);
421 p
= isl_printer_print_str(p
, " ");
422 p
= isl_printer_print_str(p
, op
);
423 p
= isl_printer_print_str(p
, " ");
425 isl_int_set_si(c
[last
], 0);
426 p
= print_affine(bmap
, dim
, p
, c
, set
);
431 static __isl_give isl_printer
*print_constraints(__isl_keep isl_basic_map
*bmap
,
432 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, int set
, int latex
)
436 unsigned total
= isl_basic_map_total_dim(bmap
);
438 c
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
442 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
443 int l
= isl_seq_last_non_zero(bmap
->eq
[i
], 1 + total
);
444 isl_assert(bmap
->ctx
, l
>= 0, goto error
);
445 if (isl_int_is_neg(bmap
->eq
[i
][l
]))
446 isl_seq_cpy(c
->el
, bmap
->eq
[i
], 1 + total
);
448 isl_seq_neg(c
->el
, bmap
->eq
[i
], 1 + total
);
449 p
= print_constraint(bmap
, dim
, p
, c
->el
, l
,
450 "=", i
== bmap
->n_eq
- 1, set
, latex
);
452 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
453 int l
= isl_seq_last_non_zero(bmap
->ineq
[i
], 1 + total
);
458 s
= isl_int_sgn(bmap
->ineq
[i
][l
]);
460 isl_seq_cpy(c
->el
, bmap
->ineq
[i
], 1 + total
);
462 isl_seq_neg(c
->el
, bmap
->ineq
[i
], 1 + total
);
463 op
= s
< 0 ? s_le
[latex
] : s_ge
[latex
];
464 p
= print_constraint(bmap
, dim
, p
, c
->el
, l
,
465 op
, !bmap
->n_eq
&& !i
, set
, latex
);
476 static __isl_give isl_printer
*print_omega_constraints(
477 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
, int set
)
479 if (bmap
->n_eq
+ bmap
->n_ineq
== 0)
482 p
= isl_printer_print_str(p
, ": ");
483 if (bmap
->n_div
> 0) {
485 p
= isl_printer_print_str(p
, "exists (");
486 for (i
= 0; i
< bmap
->n_div
; ++i
) {
488 p
= isl_printer_print_str(p
, ", ");
489 p
= print_name(bmap
->dim
, p
, isl_dim_div
, i
, 0, 0);
491 p
= isl_printer_print_str(p
, ": ");
493 p
= print_constraints(bmap
, bmap
->dim
, p
, set
, 0);
495 p
= isl_printer_print_str(p
, ")");
499 static __isl_give isl_printer
*basic_map_print_omega(
500 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
502 p
= isl_printer_print_str(p
, "{ [");
503 p
= print_var_list(bmap
->dim
, p
, isl_dim_in
, 0, 0, NULL
);
504 p
= isl_printer_print_str(p
, "] -> [");
505 p
= print_var_list(bmap
->dim
, p
, isl_dim_out
, 0, 0, NULL
);
506 p
= isl_printer_print_str(p
, "] ");
507 p
= print_omega_constraints(bmap
, p
, 0);
508 p
= isl_printer_print_str(p
, " }");
512 static __isl_give isl_printer
*isl_basic_map_print_omega(
513 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
515 p
= print_omega_parameters(bmap
->dim
, p
);
517 p
= isl_printer_start_line(p
);
518 p
= basic_map_print_omega(bmap
, p
);
519 p
= isl_printer_end_line(p
);
523 static __isl_give isl_printer
*basic_set_print_omega(
524 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
526 p
= isl_printer_print_str(p
, "{ [");
527 p
= print_var_list(bset
->dim
, p
, isl_dim_set
, 1, 0, NULL
);
528 p
= isl_printer_print_str(p
, "] ");
529 p
= print_omega_constraints((isl_basic_map
*)bset
, p
, 1);
530 p
= isl_printer_print_str(p
, " }");
534 static __isl_give isl_printer
*isl_basic_set_print_omega(
535 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
537 p
= print_omega_parameters(bset
->dim
, p
);
539 p
= isl_printer_start_line(p
);
540 p
= basic_set_print_omega(bset
, p
);
541 p
= isl_printer_end_line(p
);
545 static __isl_give isl_printer
*isl_map_print_omega(__isl_keep isl_map
*map
,
546 __isl_take isl_printer
*p
)
550 p
= print_omega_parameters(map
->dim
, p
);
552 p
= isl_printer_start_line(p
);
553 for (i
= 0; i
< map
->n
; ++i
) {
555 p
= isl_printer_print_str(p
, " union ");
556 p
= basic_map_print_omega(map
->p
[i
], p
);
558 p
= isl_printer_end_line(p
);
562 static __isl_give isl_printer
*isl_set_print_omega(__isl_keep isl_set
*set
,
563 __isl_take isl_printer
*p
)
567 p
= print_omega_parameters(set
->dim
, p
);
569 p
= isl_printer_start_line(p
);
570 for (i
= 0; i
< set
->n
; ++i
) {
572 p
= isl_printer_print_str(p
, " union ");
573 p
= basic_set_print_omega(set
->p
[i
], p
);
575 p
= isl_printer_end_line(p
);
579 static __isl_give isl_printer
*print_disjunct(__isl_keep isl_basic_map
*bmap
,
580 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, int set
, int latex
)
582 if (bmap
->n_div
> 0) {
584 p
= isl_printer_print_str(p
, s_open_exists
[latex
]);
585 for (i
= 0; i
< bmap
->n_div
; ++i
) {
587 p
= isl_printer_print_str(p
, ", ");
588 p
= print_name(dim
, p
, isl_dim_div
, i
, 0, latex
);
589 if (latex
|| isl_int_is_zero(bmap
->div
[i
][0]))
591 p
= isl_printer_print_str(p
, " = [(");
592 p
= print_affine(bmap
, dim
, p
, bmap
->div
[i
] + 1, set
);
593 p
= isl_printer_print_str(p
, ")/");
594 p
= isl_printer_print_isl_int(p
, bmap
->div
[i
][0]);
595 p
= isl_printer_print_str(p
, "]");
597 p
= isl_printer_print_str(p
, ": ");
600 p
= print_constraints(bmap
, dim
, p
, set
, latex
);
603 p
= isl_printer_print_str(p
, s_close_exists
[latex
]);
607 static __isl_give isl_printer
*isl_basic_map_print_isl(
608 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
, int latex
)
612 p
= isl_printer_start_line(p
);
613 if (isl_basic_map_dim(bmap
, isl_dim_param
) > 0) {
614 p
= print_tuple(bmap
->dim
, p
, isl_dim_param
, 0, latex
, NULL
);
615 p
= isl_printer_print_str(p
, " -> ");
617 p
= isl_printer_print_str(p
, "{ ");
618 p
= print_dim(bmap
->dim
, p
, 0, latex
, NULL
);
619 p
= isl_printer_print_str(p
, " : ");
620 p
= print_disjunct(bmap
, bmap
->dim
, p
, 0, latex
);
621 p
= isl_printer_print_str(p
, " }");
622 p
= isl_printer_end_line(p
);
626 static __isl_give isl_printer
*isl_basic_set_print_isl(
627 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
, int latex
)
631 p
= isl_printer_start_line(p
);
632 if (isl_basic_set_dim(bset
, isl_dim_param
) > 0) {
633 p
= print_tuple(bset
->dim
, p
, isl_dim_param
, 0, latex
, NULL
);
634 p
= isl_printer_print_str(p
, " -> ");
636 p
= isl_printer_print_str(p
, "{ ");
637 p
= print_dim(bset
->dim
, p
, 1, latex
, NULL
);
638 p
= isl_printer_print_str(p
, " : ");
639 p
= print_disjunct((isl_basic_map
*)bset
, bset
->dim
, p
, 1, latex
);
640 p
= isl_printer_print_str(p
, " }");
641 p
= isl_printer_end_line(p
);
645 static __isl_give isl_printer
*print_disjuncts(__isl_keep isl_map
*map
,
646 __isl_take isl_printer
*p
, int set
, int latex
)
650 if (isl_map_fast_is_universe(map
))
653 p
= isl_printer_print_str(p
, s_such_that
[latex
]);
655 p
= isl_printer_print_str(p
, "1 = 0");
656 for (i
= 0; i
< map
->n
; ++i
) {
658 p
= isl_printer_print_str(p
, s_or
[latex
]);
659 if (map
->n
> 1 && map
->p
[i
]->n_eq
+ map
->p
[i
]->n_ineq
> 1)
660 p
= isl_printer_print_str(p
, "(");
661 p
= print_disjunct(map
->p
[i
], map
->dim
, p
, set
, latex
);
662 if (map
->n
> 1 && map
->p
[i
]->n_eq
+ map
->p
[i
]->n_ineq
> 1)
663 p
= isl_printer_print_str(p
, ")");
668 struct isl_aff_split
{
673 static void free_split(__isl_take
struct isl_aff_split
*split
, int n
)
680 for (i
= 0; i
< n
; ++i
) {
681 isl_basic_map_free(split
[i
].aff
);
682 isl_map_free(split
[i
].map
);
688 static __isl_give isl_basic_map
*get_aff(__isl_take isl_basic_map
*bmap
)
691 unsigned nparam
, n_in
, n_out
, total
;
693 bmap
= isl_basic_map_cow(bmap
);
696 if (isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
) < 0)
699 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
700 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
701 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
702 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
703 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
704 j
= isl_seq_last_non_zero(bmap
->eq
[i
] + 1, total
);
705 if (j
>= nparam
&& j
< nparam
+ n_in
+ n_out
&&
706 (isl_int_is_one(bmap
->eq
[i
][1 + j
]) ||
707 isl_int_is_negone(bmap
->eq
[i
][1 + j
])))
709 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
713 bmap
= isl_basic_map_finalize(bmap
);
717 isl_basic_map_free(bmap
);
721 static int aff_split_cmp(const void *p1
, const void *p2
)
723 const struct isl_aff_split
*s1
, *s2
;
724 s1
= (const struct isl_aff_split
*) p1
;
725 s2
= (const struct isl_aff_split
*) p2
;
727 return isl_basic_map_fast_cmp(s1
->aff
, s2
->aff
);
730 static __isl_give isl_basic_map
*drop_aff(__isl_take isl_basic_map
*bmap
,
731 __isl_keep isl_basic_map
*aff
)
739 total
= isl_dim_total(bmap
->dim
);
741 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
742 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
745 for (j
= 0; j
< aff
->n_eq
; ++j
) {
746 if (!isl_seq_eq(bmap
->eq
[i
], aff
->eq
[j
], 1 + total
) &&
747 !isl_seq_is_neg(bmap
->eq
[i
], aff
->eq
[j
], 1 + total
))
749 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
757 isl_basic_map_free(bmap
);
761 static __isl_give
struct isl_aff_split
*split_aff(__isl_keep isl_map
*map
)
764 struct isl_aff_split
*split
;
766 split
= isl_calloc_array(map
->isl
, struct isl_aff_split
, map
->n
);
770 for (i
= 0; i
< map
->n
; ++i
) {
772 split
[i
].aff
= get_aff(isl_basic_map_copy(map
->p
[i
]));
773 bmap
= isl_basic_map_copy(map
->p
[i
]);
774 bmap
= isl_basic_map_cow(bmap
);
775 bmap
= drop_aff(bmap
, split
[i
].aff
);
776 split
[i
].map
= isl_map_from_basic_map(bmap
);
777 if (!split
[i
].aff
|| !split
[i
].map
)
781 qsort(split
, map
->n
, sizeof(struct isl_aff_split
), &aff_split_cmp
);
784 for (i
= n
- 1; i
>= 1; --i
) {
785 if (!isl_basic_map_fast_is_equal(split
[i
- 1].aff
,
788 isl_basic_map_free(split
[i
].aff
);
789 split
[i
- 1].map
= isl_map_union(split
[i
- 1].map
,
792 split
[i
] = split
[n
- 1];
793 split
[n
- 1].aff
= NULL
;
794 split
[n
- 1].map
= NULL
;
800 free_split(split
, map
->n
);
804 static __isl_give isl_printer
*print_split_map(__isl_take isl_printer
*p
,
805 struct isl_aff_split
*split
, int n
, int set
)
809 for (i
= 0; i
< n
; ++i
) {
814 dim
= split
[i
].map
->dim
;
816 p
= isl_printer_print_str(p
, "; ");
817 p
= print_dim(dim
, p
, set
, 0, split
[i
].aff
);
818 p
= print_disjuncts(split
[i
].map
, p
, set
, 0);
824 static __isl_give isl_printer
*isl_map_print_isl_body(__isl_keep isl_map
*map
,
825 __isl_take isl_printer
*p
, int set
)
827 struct isl_aff_split
*split
= NULL
;
830 split
= split_aff(map
);
832 p
= print_split_map(p
, split
, map
->n
, set
);
834 p
= print_dim(map
->dim
, p
, set
, 0, NULL
);
835 p
= print_disjuncts(map
, p
, set
, 0);
837 free_split(split
, map
->n
);
841 static __isl_give isl_printer
*isl_map_print_isl(__isl_keep isl_map
*map
,
842 __isl_take isl_printer
*p
, int set
)
844 if (isl_map_dim(map
, isl_dim_param
) > 0) {
845 p
= print_tuple(map
->dim
, p
, isl_dim_param
, set
, 0, NULL
);
846 p
= isl_printer_print_str(p
, s_to
[0]);
848 p
= isl_printer_print_str(p
, s_open_set
[0]);
849 p
= isl_map_print_isl_body(map
, p
, set
);
850 p
= isl_printer_print_str(p
, s_close_set
[0]);
854 static __isl_give isl_printer
*print_latex_map(__isl_keep isl_map
*map
,
855 __isl_take isl_printer
*p
, __isl_keep isl_basic_map
*aff
, int set
)
857 if (isl_map_dim(map
, isl_dim_param
) > 0) {
858 p
= print_tuple(map
->dim
, p
, isl_dim_param
, set
, 1, NULL
);
859 p
= isl_printer_print_str(p
, s_to
[1]);
861 p
= isl_printer_print_str(p
, s_open_set
[1]);
862 p
= print_dim(map
->dim
, p
, set
, 1, aff
);
863 p
= print_disjuncts(map
, p
, set
, 1);
864 p
= isl_printer_print_str(p
, s_close_set
[1]);
869 static __isl_give isl_printer
*isl_map_print_latex(__isl_keep isl_map
*map
,
870 __isl_take isl_printer
*p
, int set
)
873 struct isl_aff_split
*split
= NULL
;
876 split
= split_aff(map
);
879 return print_latex_map(map
, p
, NULL
, set
);
881 for (i
= 0; i
< map
->n
; ++i
) {
885 p
= isl_printer_print_str(p
, " \\cup ");
886 p
= print_latex_map(split
[i
].map
, p
, split
[i
].aff
, set
);
889 free_split(split
, map
->n
);
893 __isl_give isl_printer
*isl_printer_print_basic_map(__isl_take isl_printer
*p
,
894 __isl_keep isl_basic_map
*bmap
)
898 if (p
->output_format
== ISL_FORMAT_ISL
)
899 return isl_basic_map_print_isl(bmap
, p
, 0);
900 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
901 return isl_basic_map_print_omega(bmap
, p
);
902 isl_assert(bmap
->ctx
, 0, goto error
);
908 void isl_basic_map_print(__isl_keep isl_basic_map
*bmap
, FILE *out
, int indent
,
909 const char *prefix
, const char *suffix
, unsigned output_format
)
911 isl_printer
*printer
;
916 printer
= isl_printer_to_file(bmap
->ctx
, out
);
917 printer
= isl_printer_set_indent(printer
, indent
);
918 printer
= isl_printer_set_prefix(printer
, prefix
);
919 printer
= isl_printer_set_suffix(printer
, suffix
);
920 printer
= isl_printer_set_output_format(printer
, output_format
);
921 isl_printer_print_basic_map(printer
, bmap
);
923 isl_printer_free(printer
);
926 __isl_give isl_printer
*isl_printer_print_basic_set(__isl_take isl_printer
*p
,
927 __isl_keep isl_basic_set
*bset
)
932 if (p
->output_format
== ISL_FORMAT_ISL
)
933 return isl_basic_set_print_isl(bset
, p
, 0);
934 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
935 return isl_basic_set_print_polylib(bset
, p
);
936 else if (p
->output_format
== ISL_FORMAT_POLYLIB_CONSTRAINTS
)
937 return bset_print_constraints_polylib(bset
, p
);
938 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
939 return isl_basic_set_print_omega(bset
, p
);
940 isl_assert(p
->ctx
, 0, goto error
);
946 void isl_basic_set_print(struct isl_basic_set
*bset
, FILE *out
, int indent
,
947 const char *prefix
, const char *suffix
, unsigned output_format
)
949 isl_printer
*printer
;
954 printer
= isl_printer_to_file(bset
->ctx
, out
);
955 printer
= isl_printer_set_indent(printer
, indent
);
956 printer
= isl_printer_set_prefix(printer
, prefix
);
957 printer
= isl_printer_set_suffix(printer
, suffix
);
958 printer
= isl_printer_set_output_format(printer
, output_format
);
959 isl_printer_print_basic_set(printer
, bset
);
961 isl_printer_free(printer
);
964 __isl_give isl_printer
*isl_printer_print_set(__isl_take isl_printer
*p
,
965 __isl_keep isl_set
*set
)
969 if (p
->output_format
== ISL_FORMAT_ISL
)
970 return isl_map_print_isl((isl_map
*)set
, p
, 1);
971 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
972 return isl_set_print_polylib(set
, p
);
973 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
974 return isl_set_print_omega(set
, p
);
975 else if (p
->output_format
== ISL_FORMAT_LATEX
)
976 return isl_map_print_latex((isl_map
*)set
, p
, 1);
977 isl_assert(set
->ctx
, 0, goto error
);
983 void isl_set_print(struct isl_set
*set
, FILE *out
, int indent
,
984 unsigned output_format
)
986 isl_printer
*printer
;
991 printer
= isl_printer_to_file(set
->ctx
, out
);
992 printer
= isl_printer_set_indent(printer
, indent
);
993 printer
= isl_printer_set_output_format(printer
, output_format
);
994 printer
= isl_printer_print_set(printer
, set
);
996 isl_printer_free(printer
);
999 __isl_give isl_printer
*isl_printer_print_map(__isl_take isl_printer
*p
,
1000 __isl_keep isl_map
*map
)
1005 if (p
->output_format
== ISL_FORMAT_ISL
)
1006 return isl_map_print_isl(map
, p
, 0);
1007 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
1008 return isl_map_print_polylib(map
, p
);
1009 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
1010 return isl_map_print_omega(map
, p
);
1011 else if (p
->output_format
== ISL_FORMAT_LATEX
)
1012 return isl_map_print_latex(map
, p
, 0);
1013 isl_assert(map
->ctx
, 0, goto error
);
1015 isl_printer_free(p
);
1019 struct isl_union_print_data
{
1025 static int print_map_body(__isl_take isl_map
*map
, void *user
)
1027 struct isl_union_print_data
*data
;
1028 data
= (struct isl_union_print_data
*)user
;
1031 data
->p
= isl_printer_print_str(data
->p
, "; ");
1034 data
->p
= isl_map_print_isl_body(map
, data
->p
, data
->set
);
1040 static __isl_give isl_printer
*isl_union_map_print_isl(
1041 __isl_keep isl_union_map
*umap
, __isl_take isl_printer
*p
, int set
)
1043 struct isl_union_print_data data
= { p
, set
, 1 };
1045 dim
= isl_union_map_get_dim(umap
);
1046 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
1047 p
= print_tuple(dim
, p
, isl_dim_param
, set
, 0, NULL
);
1048 p
= isl_printer_print_str(p
, s_to
[0]);
1051 p
= isl_printer_print_str(p
, s_open_set
[0]);
1052 isl_union_map_foreach_map(umap
, &print_map_body
, &data
);
1054 p
= isl_printer_print_str(p
, s_close_set
[0]);
1058 static int print_latex_map_body(__isl_take isl_map
*map
, void *user
)
1060 struct isl_union_print_data
*data
;
1061 data
= (struct isl_union_print_data
*)user
;
1064 data
->p
= isl_printer_print_str(data
->p
, " \\cup ");
1067 data
->p
= isl_map_print_latex(map
, data
->p
, data
->set
);
1073 static __isl_give isl_printer
*isl_union_map_print_latex(
1074 __isl_keep isl_union_map
*umap
, __isl_take isl_printer
*p
, int set
)
1076 struct isl_union_print_data data
= { p
, set
, 1 };
1077 isl_union_map_foreach_map(umap
, &print_latex_map_body
, &data
);
1082 __isl_give isl_printer
*isl_printer_print_union_map(__isl_take isl_printer
*p
,
1083 __isl_keep isl_union_map
*umap
)
1088 if (p
->output_format
== ISL_FORMAT_ISL
)
1089 return isl_union_map_print_isl(umap
, p
, 0);
1090 if (p
->output_format
== ISL_FORMAT_LATEX
)
1091 return isl_union_map_print_latex(umap
, p
, 0);
1093 isl_die(p
->ctx
, isl_error_invalid
,
1094 "invalid output format for isl_union_map", goto error
);
1096 isl_printer_free(p
);
1100 __isl_give isl_printer
*isl_printer_print_union_set(__isl_take isl_printer
*p
,
1101 __isl_keep isl_union_set
*uset
)
1106 if (p
->output_format
== ISL_FORMAT_ISL
)
1107 return isl_union_map_print_isl((isl_union_map
*)uset
, p
, 1);
1108 if (p
->output_format
== ISL_FORMAT_LATEX
)
1109 return isl_union_map_print_latex((isl_union_map
*)uset
, p
, 1);
1111 isl_die(p
->ctx
, isl_error_invalid
,
1112 "invalid output format for isl_union_set", goto error
);
1114 isl_printer_free(p
);
1118 void isl_map_print(__isl_keep isl_map
*map
, FILE *out
, int indent
,
1119 unsigned output_format
)
1121 isl_printer
*printer
;
1126 printer
= isl_printer_to_file(map
->ctx
, out
);
1127 printer
= isl_printer_set_indent(printer
, indent
);
1128 printer
= isl_printer_set_output_format(printer
, output_format
);
1129 printer
= isl_printer_print_map(printer
, map
);
1131 isl_printer_free(printer
);
1134 void isl_union_map_dump(__isl_keep isl_union_map
*umap
)
1136 isl_printer
*printer
;
1141 printer
= isl_printer_to_file(isl_union_map_get_ctx(umap
), stderr
);
1142 printer
= isl_printer_print_union_map(printer
, umap
);
1143 printer
= isl_printer_end_line(printer
);
1145 isl_printer_free(printer
);
1148 void isl_union_set_dump(__isl_keep isl_union_set
*uset
)
1150 isl_printer
*printer
;
1155 printer
= isl_printer_to_file(isl_union_set_get_ctx(uset
), stderr
);
1156 printer
= isl_printer_print_union_set(printer
, uset
);
1157 printer
= isl_printer_end_line(printer
);
1159 isl_printer_free(printer
);
1162 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial
*upwqp
)
1164 isl_printer
*printer
;
1169 printer
= isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp
),
1171 printer
= isl_printer_print_union_pw_qpolynomial(printer
, upwqp
);
1172 printer
= isl_printer_end_line(printer
);
1174 isl_printer_free(printer
);
1177 void isl_union_pw_qpolynomial_fold_dump(
1178 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1180 isl_printer
*printer
;
1185 printer
= isl_printer_to_file(
1186 isl_union_pw_qpolynomial_fold_get_ctx(upwf
), stderr
);
1187 printer
= isl_printer_print_union_pw_qpolynomial_fold(printer
, upwf
);
1188 printer
= isl_printer_end_line(printer
);
1190 isl_printer_free(printer
);
1193 static int upoly_rec_n_non_zero(__isl_keep
struct isl_upoly_rec
*rec
)
1198 for (i
= 0, n
= 0; i
< rec
->n
; ++i
)
1199 if (!isl_upoly_is_zero(rec
->p
[i
]))
1205 static __isl_give isl_printer
*print_div(__isl_keep isl_dim
*dim
,
1206 __isl_keep isl_mat
*div
, int pos
, __isl_take isl_printer
*p
)
1208 int c
= p
->output_format
== ISL_FORMAT_C
;
1209 p
= isl_printer_print_str(p
, c
? "floord(" : "[(");
1210 p
= print_affine_of_len(dim
, p
, div
->row
[pos
] + 1, div
->n_col
- 1, 1);
1211 p
= isl_printer_print_str(p
, c
? ", " : ")/");
1212 p
= isl_printer_print_isl_int(p
, div
->row
[pos
][0]);
1213 p
= isl_printer_print_str(p
, c
? ")" : "]");
1217 static __isl_give isl_printer
*upoly_print_cst(__isl_keep
struct isl_upoly
*up
,
1218 __isl_take isl_printer
*p
, int first
)
1220 struct isl_upoly_cst
*cst
;
1223 cst
= isl_upoly_as_cst(up
);
1226 neg
= !first
&& isl_int_is_neg(cst
->n
);
1228 p
= isl_printer_print_str(p
, neg
? " - " : " + ");
1230 isl_int_neg(cst
->n
, cst
->n
);
1231 if (isl_int_is_zero(cst
->d
)) {
1232 int sgn
= isl_int_sgn(cst
->n
);
1233 p
= isl_printer_print_str(p
, sgn
< 0 ? "-infty" :
1234 sgn
== 0 ? "NaN" : "infty");
1236 p
= isl_printer_print_isl_int(p
, cst
->n
);
1238 isl_int_neg(cst
->n
, cst
->n
);
1239 if (!isl_int_is_zero(cst
->d
) && !isl_int_is_one(cst
->d
)) {
1240 p
= isl_printer_print_str(p
, "/");
1241 p
= isl_printer_print_isl_int(p
, cst
->d
);
1245 isl_printer_free(p
);
1249 static __isl_give isl_printer
*print_base(__isl_take isl_printer
*p
,
1250 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
, int var
)
1254 total
= isl_dim_total(dim
);
1256 p
= print_term(dim
, dim
->ctx
->one
, 1 + var
, p
, 1, 0);
1258 p
= print_div(dim
, div
, var
- total
, p
);
1262 static __isl_give isl_printer
*print_pow(__isl_take isl_printer
*p
,
1263 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
, int var
, int exp
)
1265 p
= print_base(p
, dim
, div
, var
);
1268 if (p
->output_format
== ISL_FORMAT_C
) {
1270 for (i
= 1; i
< exp
; ++i
) {
1271 p
= isl_printer_print_str(p
, "*");
1272 p
= print_base(p
, dim
, div
, var
);
1275 p
= isl_printer_print_str(p
, "^");
1276 p
= isl_printer_print_int(p
, exp
);
1281 static __isl_give isl_printer
*upoly_print(__isl_keep
struct isl_upoly
*up
,
1282 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
,
1283 __isl_take isl_printer
*p
, int outer
)
1285 int i
, n
, first
, print_parens
;
1286 struct isl_upoly_rec
*rec
;
1288 if (!p
|| !up
|| !dim
|| !div
)
1291 if (isl_upoly_is_cst(up
))
1292 return upoly_print_cst(up
, p
, 1);
1294 rec
= isl_upoly_as_rec(up
);
1297 n
= upoly_rec_n_non_zero(rec
);
1298 print_parens
= n
> 1 || (outer
&& rec
->up
.var
>= isl_dim_total(dim
));
1300 p
= isl_printer_print_str(p
, "(");
1301 for (i
= 0, first
= 1; i
< rec
->n
; ++i
) {
1302 if (isl_upoly_is_zero(rec
->p
[i
]))
1304 if (isl_upoly_is_negone(rec
->p
[i
])) {
1306 p
= isl_printer_print_str(p
, "-1");
1308 p
= isl_printer_print_str(p
, "-");
1310 p
= isl_printer_print_str(p
, " - ");
1311 } else if (isl_upoly_is_cst(rec
->p
[i
]) &&
1312 !isl_upoly_is_one(rec
->p
[i
]))
1313 p
= upoly_print_cst(rec
->p
[i
], p
, first
);
1316 p
= isl_printer_print_str(p
, " + ");
1317 if (i
== 0 || !isl_upoly_is_one(rec
->p
[i
]))
1318 p
= upoly_print(rec
->p
[i
], dim
, div
, p
, 0);
1323 if (!isl_upoly_is_one(rec
->p
[i
]) &&
1324 !isl_upoly_is_negone(rec
->p
[i
]))
1325 p
= isl_printer_print_str(p
, " * ");
1326 p
= print_pow(p
, dim
, div
, rec
->up
.var
, i
);
1329 p
= isl_printer_print_str(p
, ")");
1332 isl_printer_free(p
);
1336 __isl_give isl_printer
*isl_printer_print_qpolynomial(__isl_take isl_printer
*p
,
1337 __isl_keep isl_qpolynomial
*qp
)
1341 p
= upoly_print(qp
->upoly
, qp
->dim
, qp
->div
, p
, 1);
1344 isl_printer_free(p
);
1348 void isl_qpolynomial_print(__isl_keep isl_qpolynomial
*qp
, FILE *out
,
1349 unsigned output_format
)
1356 isl_assert(qp
->dim
->ctx
, output_format
== ISL_FORMAT_ISL
, return);
1357 p
= isl_printer_to_file(qp
->dim
->ctx
, out
);
1358 p
= isl_printer_print_qpolynomial(p
, qp
);
1359 isl_printer_free(p
);
1362 static __isl_give isl_printer
*qpolynomial_fold_print(
1363 __isl_keep isl_qpolynomial_fold
*fold
, __isl_take isl_printer
*p
)
1367 if (fold
->type
== isl_fold_min
)
1368 p
= isl_printer_print_str(p
, "min");
1369 else if (fold
->type
== isl_fold_max
)
1370 p
= isl_printer_print_str(p
, "max");
1371 p
= isl_printer_print_str(p
, "(");
1372 for (i
= 0; i
< fold
->n
; ++i
) {
1374 p
= isl_printer_print_str(p
, ", ");
1375 p
= isl_printer_print_qpolynomial(p
, fold
->qp
[i
]);
1377 p
= isl_printer_print_str(p
, ")");
1381 __isl_give isl_printer
*isl_printer_print_qpolynomial_fold(
1382 __isl_take isl_printer
*p
, __isl_keep isl_qpolynomial_fold
*fold
)
1386 p
= qpolynomial_fold_print(fold
, p
);
1389 isl_printer_free(p
);
1393 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold
*fold
,
1394 FILE *out
, unsigned output_format
)
1401 isl_assert(fold
->dim
->ctx
, output_format
== ISL_FORMAT_ISL
, return);
1403 p
= isl_printer_to_file(fold
->dim
->ctx
, out
);
1404 p
= isl_printer_print_qpolynomial_fold(p
, fold
);
1406 isl_printer_free(p
);
1409 static __isl_give isl_printer
*isl_pwqp_print_isl_body(
1410 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1414 for (i
= 0; i
< pwqp
->n
; ++i
) {
1416 p
= isl_printer_print_str(p
, "; ");
1417 if (isl_dim_size(pwqp
->p
[i
].set
->dim
, isl_dim_set
) > 0 ||
1418 isl_dim_get_tuple_name(pwqp
->p
[i
].set
->dim
, isl_dim_set
)) {
1419 p
= print_dim(pwqp
->p
[i
].set
->dim
, p
, 1, 0, NULL
);
1420 p
= isl_printer_print_str(p
, " -> ");
1422 p
= isl_printer_print_qpolynomial(p
, pwqp
->p
[i
].qp
);
1423 p
= print_disjuncts((isl_map
*)pwqp
->p
[i
].set
, p
, 1, 0);
1429 static __isl_give isl_printer
*print_pw_qpolynomial_isl(
1430 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1435 if (isl_dim_size(pwqp
->dim
, isl_dim_param
) > 0) {
1436 p
= print_tuple(pwqp
->dim
, p
, isl_dim_param
, 0, 0, NULL
);
1437 p
= isl_printer_print_str(p
, " -> ");
1439 p
= isl_printer_print_str(p
, "{ ");
1441 if (isl_dim_size(pwqp
->dim
, isl_dim_set
) > 0) {
1442 p
= print_dim(pwqp
->dim
, p
, 1, 0, NULL
);
1443 p
= isl_printer_print_str(p
, " -> ");
1445 p
= isl_printer_print_str(p
, "0");
1447 p
= isl_pwqp_print_isl_body(p
, pwqp
);
1448 p
= isl_printer_print_str(p
, " }");
1451 isl_printer_free(p
);
1455 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial
*pwqp
, FILE *out
,
1456 unsigned output_format
)
1463 p
= isl_printer_to_file(pwqp
->dim
->ctx
, out
);
1464 p
= isl_printer_set_output_format(p
, output_format
);
1465 p
= isl_printer_print_pw_qpolynomial(p
, pwqp
);
1467 isl_printer_free(p
);
1470 static __isl_give isl_printer
*isl_pwf_print_isl_body(
1471 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1475 for (i
= 0; i
< pwf
->n
; ++i
) {
1477 p
= isl_printer_print_str(p
, "; ");
1478 if (isl_dim_size(pwf
->p
[i
].set
->dim
, isl_dim_set
) > 0) {
1479 p
= print_dim(pwf
->p
[i
].set
->dim
, p
, 1, 0, NULL
);
1480 p
= isl_printer_print_str(p
, " -> ");
1482 p
= qpolynomial_fold_print(pwf
->p
[i
].fold
, p
);
1483 p
= print_disjuncts((isl_map
*)pwf
->p
[i
].set
, p
, 1, 0);
1489 static __isl_give isl_printer
*print_pw_qpolynomial_fold_isl(
1490 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1492 if (isl_dim_size(pwf
->dim
, isl_dim_param
) > 0) {
1493 p
= print_tuple(pwf
->dim
, p
, isl_dim_param
, 0, 0, NULL
);
1494 p
= isl_printer_print_str(p
, " -> ");
1496 p
= isl_printer_print_str(p
, "{ ");
1498 if (isl_dim_size(pwf
->dim
, isl_dim_set
) > 0) {
1499 p
= print_dim(pwf
->dim
, p
, 1, 0, NULL
);
1500 p
= isl_printer_print_str(p
, " -> ");
1502 p
= isl_printer_print_str(p
, "0");
1504 p
= isl_pwf_print_isl_body(p
, pwf
);
1505 p
= isl_printer_print_str(p
, " }");
1509 static __isl_give isl_printer
*print_affine_c(__isl_take isl_printer
*p
,
1510 __isl_keep isl_basic_set
*bset
, isl_int
*c
);
1512 static __isl_give isl_printer
*print_name_c(__isl_take isl_printer
*p
,
1513 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
)
1515 if (type
== isl_dim_div
) {
1516 p
= isl_printer_print_str(p
, "floord(");
1517 p
= print_affine_c(p
, bset
, bset
->div
[pos
] + 1);
1518 p
= isl_printer_print_str(p
, ", ");
1519 p
= isl_printer_print_isl_int(p
, bset
->div
[pos
][0]);
1520 p
= isl_printer_print_str(p
, ")");
1524 name
= isl_dim_get_name(bset
->dim
, type
, pos
);
1527 p
= isl_printer_print_str(p
, name
);
1532 static __isl_give isl_printer
*print_term_c(__isl_take isl_printer
*p
,
1533 __isl_keep isl_basic_set
*bset
, isl_int c
, unsigned pos
)
1535 enum isl_dim_type type
;
1536 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1539 return isl_printer_print_isl_int(p
, c
);
1541 if (isl_int_is_one(c
))
1543 else if (isl_int_is_negone(c
))
1544 p
= isl_printer_print_str(p
, "-");
1546 p
= isl_printer_print_isl_int(p
, c
);
1547 p
= isl_printer_print_str(p
, "*");
1549 type
= pos2type(bset
->dim
, &pos
);
1550 p
= print_name_c(p
, bset
, type
, pos
);
1554 static __isl_give isl_printer
*print_affine_c(__isl_take isl_printer
*p
,
1555 __isl_keep isl_basic_set
*bset
, isl_int
*c
)
1559 unsigned len
= 1 + isl_basic_set_total_dim(bset
);
1561 for (i
= 0, first
= 1; i
< len
; ++i
) {
1563 if (isl_int_is_zero(c
[i
]))
1566 if (isl_int_is_neg(c
[i
])) {
1568 isl_int_neg(c
[i
], c
[i
]);
1569 p
= isl_printer_print_str(p
, " - ");
1571 p
= isl_printer_print_str(p
, " + ");
1574 p
= print_term_c(p
, bset
, c
[i
], i
);
1576 isl_int_neg(c
[i
], c
[i
]);
1579 p
= isl_printer_print_str(p
, "0");
1583 static __isl_give isl_printer
*print_constraint_c(__isl_take isl_printer
*p
,
1584 __isl_keep isl_basic_set
*bset
, isl_int
*c
, const char *op
, int first
)
1587 p
= isl_printer_print_str(p
, " && ");
1589 p
= print_affine_c(p
, bset
, c
);
1590 p
= isl_printer_print_str(p
, " ");
1591 p
= isl_printer_print_str(p
, op
);
1592 p
= isl_printer_print_str(p
, " 0");
1596 static __isl_give isl_printer
*print_basic_set_c(__isl_take isl_printer
*p
,
1597 __isl_keep isl_basic_set
*bset
)
1601 for (i
= 0; i
< bset
->n_eq
; ++i
)
1602 p
= print_constraint_c(p
, bset
, bset
->eq
[i
], "==", !i
);
1603 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1604 p
= print_constraint_c(p
, bset
, bset
->ineq
[i
], ">=",
1609 static __isl_give isl_printer
*print_set_c(__isl_take isl_printer
*p
,
1610 __isl_keep isl_set
*set
)
1615 p
= isl_printer_print_str(p
, "0");
1617 for (i
= 0; i
< set
->n
; ++i
) {
1619 p
= isl_printer_print_str(p
, " || ");
1621 p
= isl_printer_print_str(p
, "(");
1622 p
= print_basic_set_c(p
, set
->p
[i
]);
1624 p
= isl_printer_print_str(p
, ")");
1629 static __isl_give isl_printer
*print_qpolynomial_c(__isl_take isl_printer
*p
,
1630 __isl_keep isl_qpolynomial
*qp
)
1635 isl_qpolynomial_get_den(qp
, &den
);
1636 if (!isl_int_is_one(den
)) {
1638 p
= isl_printer_print_str(p
, "(");
1639 qp
= isl_qpolynomial_copy(qp
);
1640 f
= isl_qpolynomial_rat_cst(isl_dim_copy(qp
->dim
),
1641 den
, qp
->dim
->ctx
->one
);
1642 qp
= isl_qpolynomial_mul(qp
, f
);
1645 p
= upoly_print(qp
->upoly
, qp
->dim
, qp
->div
, p
, 0);
1646 if (!isl_int_is_one(den
)) {
1647 p
= isl_printer_print_str(p
, ")/");
1648 p
= isl_printer_print_isl_int(p
, den
);
1649 isl_qpolynomial_free(qp
);
1655 static __isl_give isl_printer
*print_pw_qpolynomial_c(
1656 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwpq
)
1660 if (pwpq
->n
== 1 && isl_set_fast_is_universe(pwpq
->p
[0].set
))
1661 return print_qpolynomial_c(p
, pwpq
->p
[0].qp
);
1663 for (i
= 0; i
< pwpq
->n
; ++i
) {
1664 p
= isl_printer_print_str(p
, "(");
1665 p
= print_set_c(p
, pwpq
->p
[i
].set
);
1666 p
= isl_printer_print_str(p
, ") ? (");
1667 p
= print_qpolynomial_c(p
, pwpq
->p
[i
].qp
);
1668 p
= isl_printer_print_str(p
, ") : ");
1671 p
= isl_printer_print_str(p
, "0");
1675 __isl_give isl_printer
*isl_printer_print_pw_qpolynomial(
1676 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1681 if (p
->output_format
== ISL_FORMAT_ISL
)
1682 return print_pw_qpolynomial_isl(p
, pwqp
);
1683 else if (p
->output_format
== ISL_FORMAT_C
)
1684 return print_pw_qpolynomial_c(p
, pwqp
);
1685 isl_assert(p
->ctx
, 0, goto error
);
1687 isl_printer_free(p
);
1691 static int print_pwqp_body(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1693 struct isl_union_print_data
*data
;
1694 data
= (struct isl_union_print_data
*)user
;
1697 data
->p
= isl_printer_print_str(data
->p
, "; ");
1700 data
->p
= isl_pwqp_print_isl_body(data
->p
, pwqp
);
1701 isl_pw_qpolynomial_free(pwqp
);
1706 static __isl_give isl_printer
*print_union_pw_qpolynomial_isl(
1707 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_qpolynomial
*upwqp
)
1709 struct isl_union_print_data data
= { p
, 1, 1 };
1711 dim
= isl_union_pw_qpolynomial_get_dim(upwqp
);
1712 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
1713 p
= print_tuple(dim
, p
, isl_dim_param
, 1, 0, NULL
);
1714 p
= isl_printer_print_str(p
, " -> ");
1717 p
= isl_printer_print_str(p
, "{ ");
1718 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &print_pwqp_body
,
1721 p
= isl_printer_print_str(p
, " }");
1725 __isl_give isl_printer
*isl_printer_print_union_pw_qpolynomial(
1726 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_qpolynomial
*upwqp
)
1731 if (p
->output_format
== ISL_FORMAT_ISL
)
1732 return print_union_pw_qpolynomial_isl(p
, upwqp
);
1733 isl_die(p
->ctx
, isl_error_invalid
,
1734 "invalid output format for isl_union_pw_qpolynomial",
1737 isl_printer_free(p
);
1741 static __isl_give isl_printer
*print_qpolynomial_fold_c(
1742 __isl_take isl_printer
*p
, __isl_keep isl_qpolynomial_fold
*fold
)
1746 for (i
= 0; i
< fold
->n
- 1; ++i
)
1747 if (fold
->type
== isl_fold_min
)
1748 p
= isl_printer_print_str(p
, "min(");
1749 else if (fold
->type
== isl_fold_max
)
1750 p
= isl_printer_print_str(p
, "max(");
1752 for (i
= 0; i
< fold
->n
; ++i
) {
1754 p
= isl_printer_print_str(p
, ", ");
1755 p
= print_qpolynomial_c(p
, fold
->qp
[i
]);
1757 p
= isl_printer_print_str(p
, ")");
1762 static __isl_give isl_printer
*print_pw_qpolynomial_fold_c(
1763 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1767 if (pwf
->n
== 1 && isl_set_fast_is_universe(pwf
->p
[0].set
))
1768 return print_qpolynomial_fold_c(p
, pwf
->p
[0].fold
);
1770 for (i
= 0; i
< pwf
->n
; ++i
) {
1771 p
= isl_printer_print_str(p
, "(");
1772 p
= print_set_c(p
, pwf
->p
[i
].set
);
1773 p
= isl_printer_print_str(p
, ") ? (");
1774 p
= print_qpolynomial_fold_c(p
, pwf
->p
[i
].fold
);
1775 p
= isl_printer_print_str(p
, ") : ");
1778 p
= isl_printer_print_str(p
, "0");
1782 __isl_give isl_printer
*isl_printer_print_pw_qpolynomial_fold(
1783 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1788 if (p
->output_format
== ISL_FORMAT_ISL
)
1789 return print_pw_qpolynomial_fold_isl(p
, pwf
);
1790 else if (p
->output_format
== ISL_FORMAT_C
)
1791 return print_pw_qpolynomial_fold_c(p
, pwf
);
1792 isl_assert(p
->ctx
, 0, goto error
);
1794 isl_printer_free(p
);
1798 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold
*pwf
,
1799 FILE *out
, unsigned output_format
)
1806 p
= isl_printer_to_file(pwf
->dim
->ctx
, out
);
1807 p
= isl_printer_set_output_format(p
, output_format
);
1808 p
= isl_printer_print_pw_qpolynomial_fold(p
, pwf
);
1810 isl_printer_free(p
);
1813 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold
*pwf
, void *user
)
1815 struct isl_union_print_data
*data
;
1816 data
= (struct isl_union_print_data
*)user
;
1819 data
->p
= isl_printer_print_str(data
->p
, "; ");
1822 data
->p
= isl_pwf_print_isl_body(data
->p
, pwf
);
1823 isl_pw_qpolynomial_fold_free(pwf
);
1828 static __isl_give isl_printer
*print_union_pw_qpolynomial_fold_isl(
1829 __isl_take isl_printer
*p
,
1830 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1832 struct isl_union_print_data data
= { p
, 1, 1 };
1834 dim
= isl_union_pw_qpolynomial_fold_get_dim(upwf
);
1835 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
1836 p
= print_tuple(dim
, p
, isl_dim_param
, 1, 0, NULL
);
1837 p
= isl_printer_print_str(p
, " -> ");
1840 p
= isl_printer_print_str(p
, "{ ");
1841 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf
,
1842 &print_pwf_body
, &data
);
1844 p
= isl_printer_print_str(p
, " }");
1848 __isl_give isl_printer
*isl_printer_print_union_pw_qpolynomial_fold(
1849 __isl_take isl_printer
*p
,
1850 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1855 if (p
->output_format
== ISL_FORMAT_ISL
)
1856 return print_union_pw_qpolynomial_fold_isl(p
, upwf
);
1857 isl_die(p
->ctx
, isl_error_invalid
,
1858 "invalid output format for isl_union_pw_qpolynomial_fold",
1861 isl_printer_free(p
);