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
, int ext
)
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);
104 p
= isl_printer_print_str(p
, " ");
105 p
= isl_printer_print_int(p
,
106 isl_basic_map_dim(bmap
, isl_dim_out
));
107 p
= isl_printer_print_str(p
, " ");
108 p
= isl_printer_print_int(p
,
109 isl_basic_map_dim(bmap
, isl_dim_in
));
110 p
= isl_printer_print_str(p
, " ");
111 p
= isl_printer_print_int(p
,
112 isl_basic_map_dim(bmap
, isl_dim_div
));
113 p
= isl_printer_print_str(p
, " ");
114 p
= isl_printer_print_int(p
,
115 isl_basic_map_dim(bmap
, isl_dim_param
));
117 p
= isl_printer_end_line(p
);
118 return print_constraints_polylib(bmap
, p
);
121 static __isl_give isl_printer
*isl_basic_set_print_polylib(
122 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
, int ext
)
124 return isl_basic_map_print_polylib((struct isl_basic_map
*)bset
, p
, ext
);
127 static __isl_give isl_printer
*isl_map_print_polylib(__isl_keep isl_map
*map
,
128 __isl_take isl_printer
*p
, int ext
)
132 p
= isl_printer_start_line(p
);
133 p
= isl_printer_print_int(p
, map
->n
);
134 p
= isl_printer_end_line(p
);
135 for (i
= 0; i
< map
->n
; ++i
) {
136 p
= isl_printer_start_line(p
);
137 p
= isl_printer_end_line(p
);
138 p
= isl_basic_map_print_polylib(map
->p
[i
], p
, ext
);
143 static __isl_give isl_printer
*isl_set_print_polylib(__isl_keep isl_set
*set
,
144 __isl_take isl_printer
*p
, int ext
)
146 return isl_map_print_polylib((struct isl_map
*)set
, p
, ext
);
149 static int count_same_name(__isl_keep isl_dim
*dim
,
150 enum isl_dim_type type
, unsigned pos
, const char *name
)
156 for (t
= isl_dim_param
; t
<= type
&& t
<= isl_dim_out
; ++t
) {
157 s
= t
== type
? pos
: isl_dim_size(dim
, t
);
158 for (p
= 0; p
< s
; ++p
) {
159 const char *n
= isl_dim_get_name(dim
, t
, p
);
160 if (n
&& !strcmp(n
, name
))
167 static __isl_give isl_printer
*print_name(__isl_keep isl_dim
*dim
,
168 __isl_take isl_printer
*p
, enum isl_dim_type type
, unsigned pos
,
175 name
= type
== isl_dim_div
? NULL
: isl_dim_get_name(dim
, type
, pos
);
179 if (type
== isl_dim_param
)
180 prefix
= s_param_prefix
[latex
];
181 else if (type
== isl_dim_div
)
182 prefix
= s_div_prefix
[latex
];
183 else if (set
|| type
== isl_dim_in
)
184 prefix
= s_input_prefix
[latex
];
186 prefix
= s_output_prefix
[latex
];
187 snprintf(buffer
, sizeof(buffer
), "%s%d", prefix
, pos
);
190 primes
= count_same_name(dim
, name
== buffer
? isl_dim_div
: type
,
192 p
= isl_printer_print_str(p
, name
);
194 p
= isl_printer_print_str(p
, "'");
198 static enum isl_dim_type
pos2type(__isl_keep isl_dim
*dim
, unsigned *pos
)
200 enum isl_dim_type type
;
201 unsigned n_in
= isl_dim_size(dim
, isl_dim_in
);
202 unsigned n_out
= isl_dim_size(dim
, isl_dim_out
);
203 unsigned nparam
= isl_dim_size(dim
, isl_dim_param
);
205 if (*pos
< 1 + nparam
) {
206 type
= isl_dim_param
;
208 } else if (*pos
< 1 + nparam
+ n_in
) {
211 } else if (*pos
< 1 + nparam
+ n_in
+ n_out
) {
213 *pos
-= 1 + nparam
+ n_in
;
216 *pos
-= 1 + nparam
+ n_in
+ n_out
;
222 static __isl_give isl_printer
*print_div(__isl_keep isl_dim
*dim
,
223 __isl_keep isl_mat
*div
, int pos
, __isl_take isl_printer
*p
);
225 static __isl_give isl_printer
*print_term(__isl_keep isl_dim
*dim
,
226 __isl_keep isl_mat
*div
,
227 isl_int c
, unsigned pos
, __isl_take isl_printer
*p
, int set
, int latex
)
229 enum isl_dim_type type
;
233 return isl_printer_print_isl_int(p
, c
);
235 type
= pos2type(dim
, &pos
);
236 print_div_def
= type
== isl_dim_div
&& div
&&
237 !isl_int_is_zero(div
->row
[pos
][0]);
239 if (isl_int_is_one(c
))
241 else if (isl_int_is_negone(c
))
242 p
= isl_printer_print_str(p
, "-");
244 p
= isl_printer_print_isl_int(p
, c
);
245 if (p
->output_format
== ISL_FORMAT_C
|| print_div_def
)
246 p
= isl_printer_print_str(p
, "*");
249 p
= print_div(dim
, div
, pos
, p
);
251 p
= print_name(dim
, p
, type
, pos
, set
, latex
);
255 static __isl_give isl_printer
*print_affine_of_len(__isl_keep isl_dim
*dim
,
256 __isl_keep isl_mat
*div
,
257 __isl_take isl_printer
*p
, isl_int
*c
, int len
, int set
)
262 for (i
= 0, first
= 1; i
< len
; ++i
) {
264 if (isl_int_is_zero(c
[i
]))
267 if (isl_int_is_neg(c
[i
])) {
269 isl_int_neg(c
[i
], c
[i
]);
270 p
= isl_printer_print_str(p
, " - ");
272 p
= isl_printer_print_str(p
, " + ");
275 p
= print_term(dim
, div
, c
[i
], i
, p
, set
, 0);
277 isl_int_neg(c
[i
], c
[i
]);
280 p
= isl_printer_print_str(p
, "0");
284 static __isl_give isl_printer
*print_affine(__isl_keep isl_basic_map
*bmap
,
285 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, isl_int
*c
, int set
)
287 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
288 return print_affine_of_len(dim
, NULL
, p
, c
, len
, set
);
291 static int defining_equality(__isl_keep isl_basic_map
*eq
,
292 __isl_keep isl_dim
*dim
, enum isl_dim_type type
, int pos
)
300 pos
+= isl_dim_offset(dim
, type
);
301 total
= isl_basic_map_total_dim(eq
);
303 for (i
= 0; i
< eq
->n_eq
; ++i
) {
304 if (isl_seq_last_non_zero(eq
->eq
[i
] + 1, total
) != pos
)
306 if (isl_int_is_one(eq
->eq
[i
][1 + pos
]))
307 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], 1 + total
);
314 /* offset is the offset of local_dim inside global_type of global_dim.
316 static __isl_give isl_printer
*print_nested_var_list(__isl_take isl_printer
*p
,
317 __isl_keep isl_dim
*global_dim
, enum isl_dim_type global_type
,
318 __isl_keep isl_dim
*local_dim
, enum isl_dim_type local_type
,
319 int set
, int latex
, __isl_keep isl_basic_map
*eq
, int offset
)
323 if (global_dim
!= local_dim
&& local_type
== isl_dim_out
)
324 offset
+= local_dim
->n_in
;
326 for (i
= 0; i
< isl_dim_size(local_dim
, local_type
); ++i
) {
328 p
= isl_printer_print_str(p
, ", ");
329 j
= defining_equality(eq
, global_dim
, global_type
, offset
+ i
);
331 int pos
= 1 + isl_dim_offset(global_dim
, global_type
)
333 p
= print_affine_of_len(eq
->dim
, NULL
,
334 p
, eq
->eq
[j
], pos
, set
);
336 p
= print_name(global_dim
, p
, global_type
, offset
+ i
,
343 static __isl_give isl_printer
*print_var_list(__isl_keep isl_dim
*dim
,
344 __isl_take isl_printer
*p
, enum isl_dim_type type
,
345 int set
, int latex
, __isl_keep isl_basic_map
*eq
)
347 return print_nested_var_list(p
, dim
, type
, dim
, type
, set
, latex
, eq
, 0);
350 static __isl_give isl_printer
*print_nested_map_dim(__isl_take isl_printer
*p
,
351 __isl_keep isl_dim
*global_dim
, enum isl_dim_type global_type
,
352 __isl_keep isl_dim
*local_dim
,
353 int set
, int latex
, __isl_keep isl_basic_map
*eq
, int offset
);
355 static __isl_give isl_printer
*print_nested_tuple(__isl_take isl_printer
*p
,
356 __isl_keep isl_dim
*global_dim
, enum isl_dim_type global_type
,
357 __isl_keep isl_dim
*local_dim
, enum isl_dim_type local_type
,
358 int set
, int latex
, __isl_keep isl_basic_map
*eq
, int offset
)
360 const char *name
= NULL
;
361 unsigned n
= isl_dim_size(local_dim
, local_type
);
362 if ((local_type
== isl_dim_in
|| local_type
== isl_dim_out
)) {
363 name
= isl_dim_get_tuple_name(local_dim
, local_type
);
366 p
= isl_printer_print_str(p
, "\\mathrm{");
367 p
= isl_printer_print_str(p
, name
);
369 p
= isl_printer_print_str(p
, "}");
372 if (!latex
|| n
!= 1 || name
)
373 p
= isl_printer_print_str(p
, s_open_list
[latex
]);
374 if ((local_type
== isl_dim_in
|| local_type
== isl_dim_out
) &&
375 local_dim
->nested
[local_type
- isl_dim_in
]) {
376 if (global_dim
!= local_dim
&& local_type
== isl_dim_out
)
377 offset
+= local_dim
->n_in
;
378 p
= print_nested_map_dim(p
, global_dim
, global_type
,
379 local_dim
->nested
[local_type
- isl_dim_in
],
380 set
, latex
, eq
, offset
);
382 p
= print_nested_var_list(p
, global_dim
, global_type
,
383 local_dim
, local_type
, set
, latex
, eq
, offset
);
384 if (!latex
|| n
!= 1 || name
)
385 p
= isl_printer_print_str(p
, s_close_list
[latex
]);
389 static __isl_give isl_printer
*print_tuple(__isl_keep isl_dim
*dim
,
390 __isl_take isl_printer
*p
, enum isl_dim_type type
,
391 int set
, int latex
, __isl_keep isl_basic_map
*eq
)
393 return print_nested_tuple(p
, dim
, type
, dim
, type
, set
, latex
, eq
, 0);
396 static __isl_give isl_printer
*print_nested_map_dim(__isl_take isl_printer
*p
,
397 __isl_keep isl_dim
*global_dim
, enum isl_dim_type global_type
,
398 __isl_keep isl_dim
*local_dim
,
399 int set
, int latex
, __isl_keep isl_basic_map
*eq
, int offset
)
401 p
= print_nested_tuple(p
, global_dim
, global_type
,
402 local_dim
, isl_dim_in
, set
, latex
, eq
, offset
);
403 p
= isl_printer_print_str(p
, s_to
[latex
]);
404 p
= print_nested_tuple(p
, global_dim
, global_type
,
405 local_dim
, isl_dim_out
, set
, latex
, eq
, offset
);
410 static __isl_give isl_printer
*print_dim(__isl_keep isl_dim
*dim
,
411 __isl_take isl_printer
*p
, int set
, int latex
,
412 __isl_keep isl_basic_map
*eq
)
415 p
= print_tuple(dim
, p
, isl_dim_set
, 1, latex
, eq
);
417 p
= print_tuple(dim
, p
, isl_dim_in
, 0, latex
, eq
);
418 p
= isl_printer_print_str(p
, s_to
[latex
]);
419 p
= print_tuple(dim
, p
, isl_dim_out
, 0, latex
, eq
);
425 static __isl_give isl_printer
*print_omega_parameters(__isl_keep isl_dim
*dim
,
426 __isl_take isl_printer
*p
)
428 if (isl_dim_size(dim
, isl_dim_param
) == 0)
431 p
= isl_printer_start_line(p
);
432 p
= isl_printer_print_str(p
, "symbolic ");
433 p
= print_var_list(dim
, p
, isl_dim_param
, 0, 0, NULL
);
434 p
= isl_printer_print_str(p
, ";");
435 p
= isl_printer_end_line(p
);
439 static __isl_give isl_printer
*print_constraint(struct isl_basic_map
*bmap
,
440 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
,
441 isl_int
*c
, int last
, const char *op
, int first_constraint
, int set
,
444 if (!first_constraint
)
445 p
= isl_printer_print_str(p
, s_and
[latex
]);
447 isl_int_abs(c
[last
], c
[last
]);
449 p
= print_term(dim
, NULL
, c
[last
], last
, p
, set
, latex
);
451 p
= isl_printer_print_str(p
, " ");
452 p
= isl_printer_print_str(p
, op
);
453 p
= isl_printer_print_str(p
, " ");
455 isl_int_set_si(c
[last
], 0);
456 p
= print_affine(bmap
, dim
, p
, c
, set
);
461 static __isl_give isl_printer
*print_constraints(__isl_keep isl_basic_map
*bmap
,
462 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, int set
, int latex
)
466 unsigned total
= isl_basic_map_total_dim(bmap
);
468 c
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
472 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
473 int l
= isl_seq_last_non_zero(bmap
->eq
[i
], 1 + total
);
474 isl_assert(bmap
->ctx
, l
>= 0, goto error
);
475 if (isl_int_is_neg(bmap
->eq
[i
][l
]))
476 isl_seq_cpy(c
->el
, bmap
->eq
[i
], 1 + total
);
478 isl_seq_neg(c
->el
, bmap
->eq
[i
], 1 + total
);
479 p
= print_constraint(bmap
, dim
, p
, c
->el
, l
,
480 "=", i
== bmap
->n_eq
- 1, set
, latex
);
482 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
483 int l
= isl_seq_last_non_zero(bmap
->ineq
[i
], 1 + total
);
488 s
= isl_int_sgn(bmap
->ineq
[i
][l
]);
490 isl_seq_cpy(c
->el
, bmap
->ineq
[i
], 1 + total
);
492 isl_seq_neg(c
->el
, bmap
->ineq
[i
], 1 + total
);
493 op
= s
< 0 ? s_le
[latex
] : s_ge
[latex
];
494 p
= print_constraint(bmap
, dim
, p
, c
->el
, l
,
495 op
, !bmap
->n_eq
&& !i
, set
, latex
);
507 static __isl_give isl_printer
*print_omega_constraints(
508 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
, int set
)
510 if (bmap
->n_eq
+ bmap
->n_ineq
== 0)
513 p
= isl_printer_print_str(p
, ": ");
514 if (bmap
->n_div
> 0) {
516 p
= isl_printer_print_str(p
, "exists (");
517 for (i
= 0; i
< bmap
->n_div
; ++i
) {
519 p
= isl_printer_print_str(p
, ", ");
520 p
= print_name(bmap
->dim
, p
, isl_dim_div
, i
, 0, 0);
522 p
= isl_printer_print_str(p
, ": ");
524 p
= print_constraints(bmap
, bmap
->dim
, p
, set
, 0);
526 p
= isl_printer_print_str(p
, ")");
530 static __isl_give isl_printer
*basic_map_print_omega(
531 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
533 p
= isl_printer_print_str(p
, "{ [");
534 p
= print_var_list(bmap
->dim
, p
, isl_dim_in
, 0, 0, NULL
);
535 p
= isl_printer_print_str(p
, "] -> [");
536 p
= print_var_list(bmap
->dim
, p
, isl_dim_out
, 0, 0, NULL
);
537 p
= isl_printer_print_str(p
, "] ");
538 p
= print_omega_constraints(bmap
, p
, 0);
539 p
= isl_printer_print_str(p
, " }");
543 static __isl_give isl_printer
*isl_basic_map_print_omega(
544 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
546 p
= print_omega_parameters(bmap
->dim
, p
);
548 p
= isl_printer_start_line(p
);
549 p
= basic_map_print_omega(bmap
, p
);
550 p
= isl_printer_end_line(p
);
554 static __isl_give isl_printer
*basic_set_print_omega(
555 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
557 p
= isl_printer_print_str(p
, "{ [");
558 p
= print_var_list(bset
->dim
, p
, isl_dim_set
, 1, 0, NULL
);
559 p
= isl_printer_print_str(p
, "] ");
560 p
= print_omega_constraints((isl_basic_map
*)bset
, p
, 1);
561 p
= isl_printer_print_str(p
, " }");
565 static __isl_give isl_printer
*isl_basic_set_print_omega(
566 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
568 p
= print_omega_parameters(bset
->dim
, p
);
570 p
= isl_printer_start_line(p
);
571 p
= basic_set_print_omega(bset
, p
);
572 p
= isl_printer_end_line(p
);
576 static __isl_give isl_printer
*isl_map_print_omega(__isl_keep isl_map
*map
,
577 __isl_take isl_printer
*p
)
581 p
= print_omega_parameters(map
->dim
, p
);
583 p
= isl_printer_start_line(p
);
584 for (i
= 0; i
< map
->n
; ++i
) {
586 p
= isl_printer_print_str(p
, " union ");
587 p
= basic_map_print_omega(map
->p
[i
], p
);
589 p
= isl_printer_end_line(p
);
593 static __isl_give isl_printer
*isl_set_print_omega(__isl_keep isl_set
*set
,
594 __isl_take isl_printer
*p
)
598 p
= print_omega_parameters(set
->dim
, p
);
600 p
= isl_printer_start_line(p
);
601 for (i
= 0; i
< set
->n
; ++i
) {
603 p
= isl_printer_print_str(p
, " union ");
604 p
= basic_set_print_omega(set
->p
[i
], p
);
606 p
= isl_printer_end_line(p
);
610 static __isl_give isl_printer
*print_disjunct(__isl_keep isl_basic_map
*bmap
,
611 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, int set
, int latex
)
613 if (bmap
->n_div
> 0) {
615 p
= isl_printer_print_str(p
, s_open_exists
[latex
]);
616 for (i
= 0; i
< bmap
->n_div
; ++i
) {
618 p
= isl_printer_print_str(p
, ", ");
619 p
= print_name(dim
, p
, isl_dim_div
, i
, 0, latex
);
620 if (latex
|| isl_int_is_zero(bmap
->div
[i
][0]))
622 p
= isl_printer_print_str(p
, " = [(");
623 p
= print_affine(bmap
, dim
, p
, bmap
->div
[i
] + 1, set
);
624 p
= isl_printer_print_str(p
, ")/");
625 p
= isl_printer_print_isl_int(p
, bmap
->div
[i
][0]);
626 p
= isl_printer_print_str(p
, "]");
628 p
= isl_printer_print_str(p
, ": ");
631 p
= print_constraints(bmap
, dim
, p
, set
, latex
);
634 p
= isl_printer_print_str(p
, s_close_exists
[latex
]);
638 static __isl_give isl_printer
*isl_basic_map_print_isl(
639 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
, int latex
)
643 p
= isl_printer_start_line(p
);
644 if (isl_basic_map_dim(bmap
, isl_dim_param
) > 0) {
645 p
= print_tuple(bmap
->dim
, p
, isl_dim_param
, 0, latex
, NULL
);
646 p
= isl_printer_print_str(p
, " -> ");
648 p
= isl_printer_print_str(p
, "{ ");
649 p
= print_dim(bmap
->dim
, p
, 0, latex
, NULL
);
650 p
= isl_printer_print_str(p
, " : ");
651 p
= print_disjunct(bmap
, bmap
->dim
, p
, 0, latex
);
652 p
= isl_printer_print_str(p
, " }");
656 static __isl_give isl_printer
*isl_basic_set_print_isl(
657 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
, int latex
)
661 p
= isl_printer_start_line(p
);
662 if (isl_basic_set_dim(bset
, isl_dim_param
) > 0) {
663 p
= print_tuple(bset
->dim
, p
, isl_dim_param
, 0, latex
, NULL
);
664 p
= isl_printer_print_str(p
, " -> ");
666 p
= isl_printer_print_str(p
, "{ ");
667 p
= print_dim(bset
->dim
, p
, 1, latex
, NULL
);
668 p
= isl_printer_print_str(p
, " : ");
669 p
= print_disjunct((isl_basic_map
*)bset
, bset
->dim
, p
, 1, latex
);
670 p
= isl_printer_print_str(p
, " }");
674 static __isl_give isl_printer
*print_disjuncts(__isl_keep isl_map
*map
,
675 __isl_take isl_printer
*p
, int set
, int latex
)
679 if (isl_map_fast_is_universe(map
))
682 p
= isl_printer_print_str(p
, s_such_that
[latex
]);
684 p
= isl_printer_print_str(p
, "1 = 0");
685 for (i
= 0; i
< map
->n
; ++i
) {
687 p
= isl_printer_print_str(p
, s_or
[latex
]);
688 if (map
->n
> 1 && map
->p
[i
]->n_eq
+ map
->p
[i
]->n_ineq
> 1)
689 p
= isl_printer_print_str(p
, "(");
690 p
= print_disjunct(map
->p
[i
], map
->dim
, p
, set
, latex
);
691 if (map
->n
> 1 && map
->p
[i
]->n_eq
+ map
->p
[i
]->n_ineq
> 1)
692 p
= isl_printer_print_str(p
, ")");
697 struct isl_aff_split
{
702 static void free_split(__isl_take
struct isl_aff_split
*split
, int n
)
709 for (i
= 0; i
< n
; ++i
) {
710 isl_basic_map_free(split
[i
].aff
);
711 isl_map_free(split
[i
].map
);
717 static __isl_give isl_basic_map
*get_aff(__isl_take isl_basic_map
*bmap
)
720 unsigned nparam
, n_in
, n_out
, total
;
722 bmap
= isl_basic_map_cow(bmap
);
725 if (isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
) < 0)
728 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
729 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
730 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
731 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
732 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
733 j
= isl_seq_last_non_zero(bmap
->eq
[i
] + 1, total
);
734 if (j
>= nparam
&& j
< nparam
+ n_in
+ n_out
&&
735 (isl_int_is_one(bmap
->eq
[i
][1 + j
]) ||
736 isl_int_is_negone(bmap
->eq
[i
][1 + j
])))
738 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
742 bmap
= isl_basic_map_finalize(bmap
);
746 isl_basic_map_free(bmap
);
750 static int aff_split_cmp(const void *p1
, const void *p2
)
752 const struct isl_aff_split
*s1
, *s2
;
753 s1
= (const struct isl_aff_split
*) p1
;
754 s2
= (const struct isl_aff_split
*) p2
;
756 return isl_basic_map_fast_cmp(s1
->aff
, s2
->aff
);
759 static __isl_give isl_basic_map
*drop_aff(__isl_take isl_basic_map
*bmap
,
760 __isl_keep isl_basic_map
*aff
)
768 total
= isl_dim_total(bmap
->dim
);
770 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
771 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
774 for (j
= 0; j
< aff
->n_eq
; ++j
) {
775 if (!isl_seq_eq(bmap
->eq
[i
], aff
->eq
[j
], 1 + total
) &&
776 !isl_seq_is_neg(bmap
->eq
[i
], aff
->eq
[j
], 1 + total
))
778 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
786 isl_basic_map_free(bmap
);
790 static __isl_give
struct isl_aff_split
*split_aff(__isl_keep isl_map
*map
)
793 struct isl_aff_split
*split
;
795 split
= isl_calloc_array(map
->isl
, struct isl_aff_split
, map
->n
);
799 for (i
= 0; i
< map
->n
; ++i
) {
801 split
[i
].aff
= get_aff(isl_basic_map_copy(map
->p
[i
]));
802 bmap
= isl_basic_map_copy(map
->p
[i
]);
803 bmap
= isl_basic_map_cow(bmap
);
804 bmap
= drop_aff(bmap
, split
[i
].aff
);
805 split
[i
].map
= isl_map_from_basic_map(bmap
);
806 if (!split
[i
].aff
|| !split
[i
].map
)
810 qsort(split
, map
->n
, sizeof(struct isl_aff_split
), &aff_split_cmp
);
813 for (i
= n
- 1; i
>= 1; --i
) {
814 if (!isl_basic_map_fast_is_equal(split
[i
- 1].aff
,
817 isl_basic_map_free(split
[i
].aff
);
818 split
[i
- 1].map
= isl_map_union(split
[i
- 1].map
,
821 split
[i
] = split
[n
- 1];
822 split
[n
- 1].aff
= NULL
;
823 split
[n
- 1].map
= NULL
;
829 free_split(split
, map
->n
);
833 static __isl_give isl_printer
*print_split_map(__isl_take isl_printer
*p
,
834 struct isl_aff_split
*split
, int n
, int set
)
838 for (i
= 0; i
< n
; ++i
) {
843 dim
= split
[i
].map
->dim
;
845 p
= isl_printer_print_str(p
, "; ");
846 p
= print_dim(dim
, p
, set
, 0, split
[i
].aff
);
847 p
= print_disjuncts(split
[i
].map
, p
, set
, 0);
853 static __isl_give isl_printer
*isl_map_print_isl_body(__isl_keep isl_map
*map
,
854 __isl_take isl_printer
*p
, int set
)
856 struct isl_aff_split
*split
= NULL
;
859 split
= split_aff(map
);
861 p
= print_split_map(p
, split
, map
->n
, set
);
863 p
= print_dim(map
->dim
, p
, set
, 0, NULL
);
864 p
= print_disjuncts(map
, p
, set
, 0);
866 free_split(split
, map
->n
);
870 static __isl_give isl_printer
*isl_map_print_isl(__isl_keep isl_map
*map
,
871 __isl_take isl_printer
*p
, int set
)
873 if (isl_map_dim(map
, isl_dim_param
) > 0) {
874 p
= print_tuple(map
->dim
, p
, isl_dim_param
, set
, 0, NULL
);
875 p
= isl_printer_print_str(p
, s_to
[0]);
877 p
= isl_printer_print_str(p
, s_open_set
[0]);
878 p
= isl_map_print_isl_body(map
, p
, set
);
879 p
= isl_printer_print_str(p
, s_close_set
[0]);
883 static __isl_give isl_printer
*print_latex_map(__isl_keep isl_map
*map
,
884 __isl_take isl_printer
*p
, __isl_keep isl_basic_map
*aff
, int set
)
886 if (isl_map_dim(map
, isl_dim_param
) > 0) {
887 p
= print_tuple(map
->dim
, p
, isl_dim_param
, set
, 1, NULL
);
888 p
= isl_printer_print_str(p
, s_to
[1]);
890 p
= isl_printer_print_str(p
, s_open_set
[1]);
891 p
= print_dim(map
->dim
, p
, set
, 1, aff
);
892 p
= print_disjuncts(map
, p
, set
, 1);
893 p
= isl_printer_print_str(p
, s_close_set
[1]);
898 static __isl_give isl_printer
*isl_map_print_latex(__isl_keep isl_map
*map
,
899 __isl_take isl_printer
*p
, int set
)
902 struct isl_aff_split
*split
= NULL
;
905 split
= split_aff(map
);
908 return print_latex_map(map
, p
, NULL
, set
);
910 for (i
= 0; i
< map
->n
; ++i
) {
914 p
= isl_printer_print_str(p
, " \\cup ");
915 p
= print_latex_map(split
[i
].map
, p
, split
[i
].aff
, set
);
918 free_split(split
, map
->n
);
922 __isl_give isl_printer
*isl_printer_print_basic_map(__isl_take isl_printer
*p
,
923 __isl_keep isl_basic_map
*bmap
)
927 if (p
->output_format
== ISL_FORMAT_ISL
)
928 return isl_basic_map_print_isl(bmap
, p
, 0);
929 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
930 return isl_basic_map_print_omega(bmap
, p
);
931 isl_assert(bmap
->ctx
, 0, goto error
);
937 void isl_basic_map_print(__isl_keep isl_basic_map
*bmap
, FILE *out
, int indent
,
938 const char *prefix
, const char *suffix
, unsigned output_format
)
940 isl_printer
*printer
;
945 printer
= isl_printer_to_file(bmap
->ctx
, out
);
946 printer
= isl_printer_set_indent(printer
, indent
);
947 printer
= isl_printer_set_prefix(printer
, prefix
);
948 printer
= isl_printer_set_suffix(printer
, suffix
);
949 printer
= isl_printer_set_output_format(printer
, output_format
);
950 isl_printer_print_basic_map(printer
, bmap
);
952 isl_printer_free(printer
);
955 __isl_give isl_printer
*isl_printer_print_basic_set(__isl_take isl_printer
*p
,
956 __isl_keep isl_basic_set
*bset
)
961 if (p
->output_format
== ISL_FORMAT_ISL
)
962 return isl_basic_set_print_isl(bset
, p
, 0);
963 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
964 return isl_basic_set_print_polylib(bset
, p
, 0);
965 else if (p
->output_format
== ISL_FORMAT_EXT_POLYLIB
)
966 return isl_basic_set_print_polylib(bset
, p
, 1);
967 else if (p
->output_format
== ISL_FORMAT_POLYLIB_CONSTRAINTS
)
968 return bset_print_constraints_polylib(bset
, p
);
969 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
970 return isl_basic_set_print_omega(bset
, p
);
971 isl_assert(p
->ctx
, 0, goto error
);
977 void isl_basic_set_print(struct isl_basic_set
*bset
, FILE *out
, int indent
,
978 const char *prefix
, const char *suffix
, unsigned output_format
)
980 isl_printer
*printer
;
985 printer
= isl_printer_to_file(bset
->ctx
, out
);
986 printer
= isl_printer_set_indent(printer
, indent
);
987 printer
= isl_printer_set_prefix(printer
, prefix
);
988 printer
= isl_printer_set_suffix(printer
, suffix
);
989 printer
= isl_printer_set_output_format(printer
, output_format
);
990 isl_printer_print_basic_set(printer
, bset
);
992 isl_printer_free(printer
);
995 __isl_give isl_printer
*isl_printer_print_set(__isl_take isl_printer
*p
,
996 __isl_keep isl_set
*set
)
1000 if (p
->output_format
== ISL_FORMAT_ISL
)
1001 return isl_map_print_isl((isl_map
*)set
, p
, 1);
1002 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
1003 return isl_set_print_polylib(set
, p
, 0);
1004 else if (p
->output_format
== ISL_FORMAT_EXT_POLYLIB
)
1005 return isl_set_print_polylib(set
, p
, 1);
1006 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
1007 return isl_set_print_omega(set
, p
);
1008 else if (p
->output_format
== ISL_FORMAT_LATEX
)
1009 return isl_map_print_latex((isl_map
*)set
, p
, 1);
1010 isl_assert(set
->ctx
, 0, goto error
);
1012 isl_printer_free(p
);
1016 void isl_set_print(struct isl_set
*set
, FILE *out
, int indent
,
1017 unsigned output_format
)
1019 isl_printer
*printer
;
1024 printer
= isl_printer_to_file(set
->ctx
, out
);
1025 printer
= isl_printer_set_indent(printer
, indent
);
1026 printer
= isl_printer_set_output_format(printer
, output_format
);
1027 printer
= isl_printer_print_set(printer
, set
);
1029 isl_printer_free(printer
);
1032 __isl_give isl_printer
*isl_printer_print_map(__isl_take isl_printer
*p
,
1033 __isl_keep isl_map
*map
)
1038 if (p
->output_format
== ISL_FORMAT_ISL
)
1039 return isl_map_print_isl(map
, p
, 0);
1040 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
1041 return isl_map_print_polylib(map
, p
, 0);
1042 else if (p
->output_format
== ISL_FORMAT_EXT_POLYLIB
)
1043 return isl_map_print_polylib(map
, p
, 1);
1044 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
1045 return isl_map_print_omega(map
, p
);
1046 else if (p
->output_format
== ISL_FORMAT_LATEX
)
1047 return isl_map_print_latex(map
, p
, 0);
1048 isl_assert(map
->ctx
, 0, goto error
);
1050 isl_printer_free(p
);
1054 struct isl_union_print_data
{
1060 static int print_map_body(__isl_take isl_map
*map
, void *user
)
1062 struct isl_union_print_data
*data
;
1063 data
= (struct isl_union_print_data
*)user
;
1066 data
->p
= isl_printer_print_str(data
->p
, "; ");
1069 data
->p
= isl_map_print_isl_body(map
, data
->p
, data
->set
);
1075 static __isl_give isl_printer
*isl_union_map_print_isl(
1076 __isl_keep isl_union_map
*umap
, __isl_take isl_printer
*p
, int set
)
1078 struct isl_union_print_data data
= { p
, set
, 1 };
1080 dim
= isl_union_map_get_dim(umap
);
1081 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
1082 p
= print_tuple(dim
, p
, isl_dim_param
, set
, 0, NULL
);
1083 p
= isl_printer_print_str(p
, s_to
[0]);
1086 p
= isl_printer_print_str(p
, s_open_set
[0]);
1087 isl_union_map_foreach_map(umap
, &print_map_body
, &data
);
1089 p
= isl_printer_print_str(p
, s_close_set
[0]);
1093 static int print_latex_map_body(__isl_take isl_map
*map
, void *user
)
1095 struct isl_union_print_data
*data
;
1096 data
= (struct isl_union_print_data
*)user
;
1099 data
->p
= isl_printer_print_str(data
->p
, " \\cup ");
1102 data
->p
= isl_map_print_latex(map
, data
->p
, data
->set
);
1108 static __isl_give isl_printer
*isl_union_map_print_latex(
1109 __isl_keep isl_union_map
*umap
, __isl_take isl_printer
*p
, int set
)
1111 struct isl_union_print_data data
= { p
, set
, 1 };
1112 isl_union_map_foreach_map(umap
, &print_latex_map_body
, &data
);
1117 __isl_give isl_printer
*isl_printer_print_union_map(__isl_take isl_printer
*p
,
1118 __isl_keep isl_union_map
*umap
)
1123 if (p
->output_format
== ISL_FORMAT_ISL
)
1124 return isl_union_map_print_isl(umap
, p
, 0);
1125 if (p
->output_format
== ISL_FORMAT_LATEX
)
1126 return isl_union_map_print_latex(umap
, p
, 0);
1128 isl_die(p
->ctx
, isl_error_invalid
,
1129 "invalid output format for isl_union_map", goto error
);
1131 isl_printer_free(p
);
1135 __isl_give isl_printer
*isl_printer_print_union_set(__isl_take isl_printer
*p
,
1136 __isl_keep isl_union_set
*uset
)
1141 if (p
->output_format
== ISL_FORMAT_ISL
)
1142 return isl_union_map_print_isl((isl_union_map
*)uset
, p
, 1);
1143 if (p
->output_format
== ISL_FORMAT_LATEX
)
1144 return isl_union_map_print_latex((isl_union_map
*)uset
, p
, 1);
1146 isl_die(p
->ctx
, isl_error_invalid
,
1147 "invalid output format for isl_union_set", goto error
);
1149 isl_printer_free(p
);
1153 void isl_map_print(__isl_keep isl_map
*map
, FILE *out
, int indent
,
1154 unsigned output_format
)
1156 isl_printer
*printer
;
1161 printer
= isl_printer_to_file(map
->ctx
, out
);
1162 printer
= isl_printer_set_indent(printer
, indent
);
1163 printer
= isl_printer_set_output_format(printer
, output_format
);
1164 printer
= isl_printer_print_map(printer
, map
);
1166 isl_printer_free(printer
);
1169 void isl_basic_map_dump(__isl_keep isl_basic_map
*bmap
)
1171 isl_printer
*printer
;
1176 printer
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), stderr
);
1177 printer
= isl_printer_print_basic_map(printer
, bmap
);
1178 printer
= isl_printer_end_line(printer
);
1180 isl_printer_free(printer
);
1183 void isl_basic_set_dump(__isl_keep isl_basic_set
*bset
)
1185 isl_printer
*printer
;
1190 printer
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), stderr
);
1191 printer
= isl_printer_print_basic_set(printer
, bset
);
1192 printer
= isl_printer_end_line(printer
);
1194 isl_printer_free(printer
);
1197 void isl_map_dump(__isl_keep isl_map
*map
)
1199 isl_printer
*printer
;
1204 printer
= isl_printer_to_file(isl_map_get_ctx(map
), stderr
);
1205 printer
= isl_printer_print_map(printer
, map
);
1206 printer
= isl_printer_end_line(printer
);
1208 isl_printer_free(printer
);
1211 void isl_set_dump(__isl_keep isl_set
*set
)
1213 isl_printer
*printer
;
1218 printer
= isl_printer_to_file(isl_set_get_ctx(set
), stderr
);
1219 printer
= isl_printer_print_set(printer
, set
);
1220 printer
= isl_printer_end_line(printer
);
1222 isl_printer_free(printer
);
1225 void isl_union_map_dump(__isl_keep isl_union_map
*umap
)
1227 isl_printer
*printer
;
1232 printer
= isl_printer_to_file(isl_union_map_get_ctx(umap
), stderr
);
1233 printer
= isl_printer_print_union_map(printer
, umap
);
1234 printer
= isl_printer_end_line(printer
);
1236 isl_printer_free(printer
);
1239 void isl_union_set_dump(__isl_keep isl_union_set
*uset
)
1241 isl_printer
*printer
;
1246 printer
= isl_printer_to_file(isl_union_set_get_ctx(uset
), stderr
);
1247 printer
= isl_printer_print_union_set(printer
, uset
);
1248 printer
= isl_printer_end_line(printer
);
1250 isl_printer_free(printer
);
1253 void isl_union_pw_qpolynomial_dump(__isl_keep isl_union_pw_qpolynomial
*upwqp
)
1255 isl_printer
*printer
;
1260 printer
= isl_printer_to_file(isl_union_pw_qpolynomial_get_ctx(upwqp
),
1262 printer
= isl_printer_print_union_pw_qpolynomial(printer
, upwqp
);
1263 printer
= isl_printer_end_line(printer
);
1265 isl_printer_free(printer
);
1268 void isl_qpolynomial_dump(__isl_keep isl_qpolynomial
*qp
)
1270 isl_printer
*printer
;
1275 printer
= isl_printer_to_file(isl_qpolynomial_get_ctx(qp
), stderr
);
1276 printer
= isl_printer_print_qpolynomial(printer
, qp
);
1277 printer
= isl_printer_end_line(printer
);
1279 isl_printer_free(printer
);
1282 void isl_qpolynomial_fold_dump(__isl_keep isl_qpolynomial_fold
*fold
)
1284 isl_printer
*printer
;
1289 printer
= isl_printer_to_file(isl_qpolynomial_fold_get_ctx(fold
),
1291 printer
= isl_printer_print_qpolynomial_fold(printer
, fold
);
1292 printer
= isl_printer_end_line(printer
);
1294 isl_printer_free(printer
);
1297 void isl_pw_qpolynomial_dump(__isl_keep isl_pw_qpolynomial
*pwqp
)
1299 isl_printer
*printer
;
1304 printer
= isl_printer_to_file(isl_pw_qpolynomial_get_ctx(pwqp
), stderr
);
1305 printer
= isl_printer_print_pw_qpolynomial(printer
, pwqp
);
1306 printer
= isl_printer_end_line(printer
);
1308 isl_printer_free(printer
);
1311 void isl_pw_qpolynomial_fold_dump(__isl_keep isl_pw_qpolynomial_fold
*pwf
)
1313 isl_printer
*printer
;
1318 printer
= isl_printer_to_file(
1319 isl_pw_qpolynomial_fold_get_ctx(pwf
), stderr
);
1320 printer
= isl_printer_print_pw_qpolynomial_fold(printer
, pwf
);
1321 printer
= isl_printer_end_line(printer
);
1323 isl_printer_free(printer
);
1326 void isl_union_pw_qpolynomial_fold_dump(
1327 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1329 isl_printer
*printer
;
1334 printer
= isl_printer_to_file(
1335 isl_union_pw_qpolynomial_fold_get_ctx(upwf
), stderr
);
1336 printer
= isl_printer_print_union_pw_qpolynomial_fold(printer
, upwf
);
1337 printer
= isl_printer_end_line(printer
);
1339 isl_printer_free(printer
);
1342 static int upoly_rec_n_non_zero(__isl_keep
struct isl_upoly_rec
*rec
)
1347 for (i
= 0, n
= 0; i
< rec
->n
; ++i
)
1348 if (!isl_upoly_is_zero(rec
->p
[i
]))
1354 static __isl_give isl_printer
*print_div(__isl_keep isl_dim
*dim
,
1355 __isl_keep isl_mat
*div
, int pos
, __isl_take isl_printer
*p
)
1357 int c
= p
->output_format
== ISL_FORMAT_C
;
1358 p
= isl_printer_print_str(p
, c
? "floord(" : "[(");
1359 p
= print_affine_of_len(dim
, div
, p
,
1360 div
->row
[pos
] + 1, div
->n_col
- 1, 1);
1361 p
= isl_printer_print_str(p
, c
? ", " : ")/");
1362 p
= isl_printer_print_isl_int(p
, div
->row
[pos
][0]);
1363 p
= isl_printer_print_str(p
, c
? ")" : "]");
1367 static __isl_give isl_printer
*upoly_print_cst(__isl_keep
struct isl_upoly
*up
,
1368 __isl_take isl_printer
*p
, int first
)
1370 struct isl_upoly_cst
*cst
;
1373 cst
= isl_upoly_as_cst(up
);
1376 neg
= !first
&& isl_int_is_neg(cst
->n
);
1378 p
= isl_printer_print_str(p
, neg
? " - " : " + ");
1380 isl_int_neg(cst
->n
, cst
->n
);
1381 if (isl_int_is_zero(cst
->d
)) {
1382 int sgn
= isl_int_sgn(cst
->n
);
1383 p
= isl_printer_print_str(p
, sgn
< 0 ? "-infty" :
1384 sgn
== 0 ? "NaN" : "infty");
1386 p
= isl_printer_print_isl_int(p
, cst
->n
);
1388 isl_int_neg(cst
->n
, cst
->n
);
1389 if (!isl_int_is_zero(cst
->d
) && !isl_int_is_one(cst
->d
)) {
1390 p
= isl_printer_print_str(p
, "/");
1391 p
= isl_printer_print_isl_int(p
, cst
->d
);
1395 isl_printer_free(p
);
1399 static __isl_give isl_printer
*print_base(__isl_take isl_printer
*p
,
1400 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
, int var
)
1404 total
= isl_dim_total(dim
);
1406 p
= print_term(dim
, NULL
, dim
->ctx
->one
, 1 + var
, p
, 1, 0);
1408 p
= print_div(dim
, div
, var
- total
, p
);
1412 static __isl_give isl_printer
*print_pow(__isl_take isl_printer
*p
,
1413 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
, int var
, int exp
)
1415 p
= print_base(p
, dim
, div
, var
);
1418 if (p
->output_format
== ISL_FORMAT_C
) {
1420 for (i
= 1; i
< exp
; ++i
) {
1421 p
= isl_printer_print_str(p
, "*");
1422 p
= print_base(p
, dim
, div
, var
);
1425 p
= isl_printer_print_str(p
, "^");
1426 p
= isl_printer_print_int(p
, exp
);
1431 static __isl_give isl_printer
*upoly_print(__isl_keep
struct isl_upoly
*up
,
1432 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
,
1433 __isl_take isl_printer
*p
, int outer
)
1435 int i
, n
, first
, print_parens
;
1436 struct isl_upoly_rec
*rec
;
1438 if (!p
|| !up
|| !dim
|| !div
)
1441 if (isl_upoly_is_cst(up
))
1442 return upoly_print_cst(up
, p
, 1);
1444 rec
= isl_upoly_as_rec(up
);
1447 n
= upoly_rec_n_non_zero(rec
);
1448 print_parens
= n
> 1 || (outer
&& rec
->up
.var
>= isl_dim_total(dim
));
1450 p
= isl_printer_print_str(p
, "(");
1451 for (i
= 0, first
= 1; i
< rec
->n
; ++i
) {
1452 if (isl_upoly_is_zero(rec
->p
[i
]))
1454 if (isl_upoly_is_negone(rec
->p
[i
])) {
1456 p
= isl_printer_print_str(p
, "-1");
1458 p
= isl_printer_print_str(p
, "-");
1460 p
= isl_printer_print_str(p
, " - ");
1461 } else if (isl_upoly_is_cst(rec
->p
[i
]) &&
1462 !isl_upoly_is_one(rec
->p
[i
]))
1463 p
= upoly_print_cst(rec
->p
[i
], p
, first
);
1466 p
= isl_printer_print_str(p
, " + ");
1467 if (i
== 0 || !isl_upoly_is_one(rec
->p
[i
]))
1468 p
= upoly_print(rec
->p
[i
], dim
, div
, p
, 0);
1473 if (!isl_upoly_is_one(rec
->p
[i
]) &&
1474 !isl_upoly_is_negone(rec
->p
[i
]))
1475 p
= isl_printer_print_str(p
, " * ");
1476 p
= print_pow(p
, dim
, div
, rec
->up
.var
, i
);
1479 p
= isl_printer_print_str(p
, ")");
1482 isl_printer_free(p
);
1486 __isl_give isl_printer
*isl_printer_print_qpolynomial(__isl_take isl_printer
*p
,
1487 __isl_keep isl_qpolynomial
*qp
)
1491 p
= upoly_print(qp
->upoly
, qp
->dim
, qp
->div
, p
, 1);
1494 isl_printer_free(p
);
1498 void isl_qpolynomial_print(__isl_keep isl_qpolynomial
*qp
, FILE *out
,
1499 unsigned output_format
)
1506 isl_assert(qp
->dim
->ctx
, output_format
== ISL_FORMAT_ISL
, return);
1507 p
= isl_printer_to_file(qp
->dim
->ctx
, out
);
1508 p
= isl_printer_print_qpolynomial(p
, qp
);
1509 isl_printer_free(p
);
1512 static __isl_give isl_printer
*qpolynomial_fold_print(
1513 __isl_keep isl_qpolynomial_fold
*fold
, __isl_take isl_printer
*p
)
1517 if (fold
->type
== isl_fold_min
)
1518 p
= isl_printer_print_str(p
, "min");
1519 else if (fold
->type
== isl_fold_max
)
1520 p
= isl_printer_print_str(p
, "max");
1521 p
= isl_printer_print_str(p
, "(");
1522 for (i
= 0; i
< fold
->n
; ++i
) {
1524 p
= isl_printer_print_str(p
, ", ");
1525 p
= isl_printer_print_qpolynomial(p
, fold
->qp
[i
]);
1527 p
= isl_printer_print_str(p
, ")");
1531 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold
*fold
,
1532 FILE *out
, unsigned output_format
)
1539 isl_assert(fold
->dim
->ctx
, output_format
== ISL_FORMAT_ISL
, return);
1541 p
= isl_printer_to_file(fold
->dim
->ctx
, out
);
1542 p
= isl_printer_print_qpolynomial_fold(p
, fold
);
1544 isl_printer_free(p
);
1547 static __isl_give isl_printer
*isl_pwqp_print_isl_body(
1548 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1552 for (i
= 0; i
< pwqp
->n
; ++i
) {
1554 p
= isl_printer_print_str(p
, "; ");
1555 if (isl_dim_size(pwqp
->dim
, isl_dim_set
) > 0 ||
1556 isl_dim_is_named_or_nested(pwqp
->dim
, isl_dim_set
)) {
1557 p
= print_dim(pwqp
->p
[i
].set
->dim
, p
, 1, 0, NULL
);
1558 p
= isl_printer_print_str(p
, " -> ");
1560 p
= isl_printer_print_qpolynomial(p
, pwqp
->p
[i
].qp
);
1561 p
= print_disjuncts((isl_map
*)pwqp
->p
[i
].set
, p
, 1, 0);
1567 static __isl_give isl_printer
*print_pw_qpolynomial_isl(
1568 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1573 if (isl_dim_size(pwqp
->dim
, isl_dim_param
) > 0) {
1574 p
= print_tuple(pwqp
->dim
, p
, isl_dim_param
, 0, 0, NULL
);
1575 p
= isl_printer_print_str(p
, " -> ");
1577 p
= isl_printer_print_str(p
, "{ ");
1579 if (isl_dim_size(pwqp
->dim
, isl_dim_set
) > 0 ||
1580 isl_dim_is_named_or_nested(pwqp
->dim
, isl_dim_set
)) {
1581 p
= print_dim(pwqp
->dim
, p
, 1, 0, NULL
);
1582 p
= isl_printer_print_str(p
, " -> ");
1584 p
= isl_printer_print_str(p
, "0");
1586 p
= isl_pwqp_print_isl_body(p
, pwqp
);
1587 p
= isl_printer_print_str(p
, " }");
1590 isl_printer_free(p
);
1594 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial
*pwqp
, FILE *out
,
1595 unsigned output_format
)
1602 p
= isl_printer_to_file(pwqp
->dim
->ctx
, out
);
1603 p
= isl_printer_set_output_format(p
, output_format
);
1604 p
= isl_printer_print_pw_qpolynomial(p
, pwqp
);
1606 isl_printer_free(p
);
1609 static __isl_give isl_printer
*isl_pwf_print_isl_body(
1610 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1614 for (i
= 0; i
< pwf
->n
; ++i
) {
1616 p
= isl_printer_print_str(p
, "; ");
1617 if (isl_dim_size(pwf
->dim
, isl_dim_set
) > 0 ||
1618 isl_dim_is_named_or_nested(pwf
->dim
, isl_dim_set
)) {
1619 p
= print_dim(pwf
->p
[i
].set
->dim
, p
, 1, 0, NULL
);
1620 p
= isl_printer_print_str(p
, " -> ");
1622 p
= qpolynomial_fold_print(pwf
->p
[i
].fold
, p
);
1623 p
= print_disjuncts((isl_map
*)pwf
->p
[i
].set
, p
, 1, 0);
1629 static __isl_give isl_printer
*print_pw_qpolynomial_fold_isl(
1630 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1632 if (isl_dim_size(pwf
->dim
, isl_dim_param
) > 0) {
1633 p
= print_tuple(pwf
->dim
, p
, isl_dim_param
, 0, 0, NULL
);
1634 p
= isl_printer_print_str(p
, " -> ");
1636 p
= isl_printer_print_str(p
, "{ ");
1638 if (isl_dim_size(pwf
->dim
, isl_dim_set
) > 0 ||
1639 isl_dim_is_named_or_nested(pwf
->dim
, isl_dim_set
)) {
1640 p
= print_dim(pwf
->dim
, p
, 1, 0, NULL
);
1641 p
= isl_printer_print_str(p
, " -> ");
1643 p
= isl_printer_print_str(p
, "0");
1645 p
= isl_pwf_print_isl_body(p
, pwf
);
1646 p
= isl_printer_print_str(p
, " }");
1650 static __isl_give isl_printer
*print_affine_c(__isl_take isl_printer
*p
,
1651 __isl_keep isl_dim
*dim
, __isl_keep isl_basic_set
*bset
, isl_int
*c
);
1653 static __isl_give isl_printer
*print_name_c(__isl_take isl_printer
*p
,
1654 __isl_keep isl_dim
*dim
,
1655 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
)
1657 if (type
== isl_dim_div
) {
1658 p
= isl_printer_print_str(p
, "floord(");
1659 p
= print_affine_c(p
, dim
, bset
, bset
->div
[pos
] + 1);
1660 p
= isl_printer_print_str(p
, ", ");
1661 p
= isl_printer_print_isl_int(p
, bset
->div
[pos
][0]);
1662 p
= isl_printer_print_str(p
, ")");
1666 name
= isl_dim_get_name(dim
, type
, pos
);
1669 p
= isl_printer_print_str(p
, name
);
1674 static __isl_give isl_printer
*print_term_c(__isl_take isl_printer
*p
,
1675 __isl_keep isl_dim
*dim
,
1676 __isl_keep isl_basic_set
*bset
, isl_int c
, unsigned pos
)
1678 enum isl_dim_type type
;
1679 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1682 return isl_printer_print_isl_int(p
, c
);
1684 if (isl_int_is_one(c
))
1686 else if (isl_int_is_negone(c
))
1687 p
= isl_printer_print_str(p
, "-");
1689 p
= isl_printer_print_isl_int(p
, c
);
1690 p
= isl_printer_print_str(p
, "*");
1692 type
= pos2type(dim
, &pos
);
1693 p
= print_name_c(p
, dim
, bset
, type
, pos
);
1697 static __isl_give isl_printer
*print_partial_affine_c(__isl_take isl_printer
*p
,
1698 __isl_keep isl_dim
*dim
,
1699 __isl_keep isl_basic_set
*bset
, isl_int
*c
, unsigned len
)
1704 for (i
= 0, first
= 1; i
< len
; ++i
) {
1706 if (isl_int_is_zero(c
[i
]))
1709 if (isl_int_is_neg(c
[i
])) {
1711 isl_int_neg(c
[i
], c
[i
]);
1712 p
= isl_printer_print_str(p
, " - ");
1714 p
= isl_printer_print_str(p
, " + ");
1717 p
= print_term_c(p
, dim
, bset
, c
[i
], i
);
1719 isl_int_neg(c
[i
], c
[i
]);
1722 p
= isl_printer_print_str(p
, "0");
1726 static __isl_give isl_printer
*print_affine_c(__isl_take isl_printer
*p
,
1727 __isl_keep isl_dim
*dim
, __isl_keep isl_basic_set
*bset
, isl_int
*c
)
1729 unsigned len
= 1 + isl_basic_set_total_dim(bset
);
1730 return print_partial_affine_c(p
, dim
, bset
, c
, len
);
1733 static __isl_give isl_printer
*print_constraint_c(__isl_take isl_printer
*p
,
1734 __isl_keep isl_dim
*dim
,
1735 __isl_keep isl_basic_set
*bset
, isl_int
*c
, const char *op
, int first
)
1738 p
= isl_printer_print_str(p
, " && ");
1740 p
= print_affine_c(p
, dim
, bset
, c
);
1741 p
= isl_printer_print_str(p
, " ");
1742 p
= isl_printer_print_str(p
, op
);
1743 p
= isl_printer_print_str(p
, " 0");
1747 static __isl_give isl_printer
*print_basic_set_c(__isl_take isl_printer
*p
,
1748 __isl_keep isl_dim
*dim
, __isl_keep isl_basic_set
*bset
)
1751 unsigned n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1752 unsigned total
= isl_basic_set_total_dim(bset
) - n_div
;
1754 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1755 j
= isl_seq_last_non_zero(bset
->eq
[i
] + 1 + total
, n_div
);
1757 p
= print_constraint_c(p
, dim
, bset
,
1758 bset
->eq
[i
], "==", !i
);
1761 p
= isl_printer_print_str(p
, " && ");
1762 p
= isl_printer_print_str(p
, "(");
1763 p
= print_partial_affine_c(p
, dim
, bset
, bset
->eq
[i
],
1765 p
= isl_printer_print_str(p
, ") % ");
1766 p
= isl_printer_print_isl_int(p
,
1767 bset
->eq
[i
][1 + total
+ j
]);
1768 p
= isl_printer_print_str(p
, " == 0");
1771 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1772 p
= print_constraint_c(p
, dim
, bset
, bset
->ineq
[i
], ">=",
1777 static __isl_give isl_printer
*print_set_c(__isl_take isl_printer
*p
,
1778 __isl_keep isl_dim
*dim
, __isl_keep isl_set
*set
)
1783 p
= isl_printer_print_str(p
, "0");
1785 for (i
= 0; i
< set
->n
; ++i
) {
1787 p
= isl_printer_print_str(p
, " || ");
1789 p
= isl_printer_print_str(p
, "(");
1790 p
= print_basic_set_c(p
, dim
, set
->p
[i
]);
1792 p
= isl_printer_print_str(p
, ")");
1797 static __isl_give isl_printer
*print_qpolynomial_c(__isl_take isl_printer
*p
,
1798 __isl_keep isl_dim
*dim
, __isl_keep isl_qpolynomial
*qp
)
1803 isl_qpolynomial_get_den(qp
, &den
);
1804 if (!isl_int_is_one(den
)) {
1806 p
= isl_printer_print_str(p
, "(");
1807 qp
= isl_qpolynomial_copy(qp
);
1808 f
= isl_qpolynomial_rat_cst(isl_dim_copy(qp
->dim
),
1809 den
, qp
->dim
->ctx
->one
);
1810 qp
= isl_qpolynomial_mul(qp
, f
);
1813 p
= upoly_print(qp
->upoly
, dim
, qp
->div
, p
, 0);
1814 if (!isl_int_is_one(den
)) {
1815 p
= isl_printer_print_str(p
, ")/");
1816 p
= isl_printer_print_isl_int(p
, den
);
1817 isl_qpolynomial_free(qp
);
1823 static __isl_give isl_printer
*print_pw_qpolynomial_c(
1824 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1828 if (pwqp
->n
== 1 && isl_set_fast_is_universe(pwqp
->p
[0].set
))
1829 return print_qpolynomial_c(p
, pwqp
->dim
, pwqp
->p
[0].qp
);
1831 for (i
= 0; i
< pwqp
->n
; ++i
) {
1832 p
= isl_printer_print_str(p
, "(");
1833 p
= print_set_c(p
, pwqp
->dim
, pwqp
->p
[i
].set
);
1834 p
= isl_printer_print_str(p
, ") ? (");
1835 p
= print_qpolynomial_c(p
, pwqp
->dim
, pwqp
->p
[i
].qp
);
1836 p
= isl_printer_print_str(p
, ") : ");
1839 p
= isl_printer_print_str(p
, "0");
1843 __isl_give isl_printer
*isl_printer_print_pw_qpolynomial(
1844 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1849 if (p
->output_format
== ISL_FORMAT_ISL
)
1850 return print_pw_qpolynomial_isl(p
, pwqp
);
1851 else if (p
->output_format
== ISL_FORMAT_C
)
1852 return print_pw_qpolynomial_c(p
, pwqp
);
1853 isl_assert(p
->ctx
, 0, goto error
);
1855 isl_printer_free(p
);
1859 static int print_pwqp_body(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1861 struct isl_union_print_data
*data
;
1862 data
= (struct isl_union_print_data
*)user
;
1865 data
->p
= isl_printer_print_str(data
->p
, "; ");
1868 data
->p
= isl_pwqp_print_isl_body(data
->p
, pwqp
);
1869 isl_pw_qpolynomial_free(pwqp
);
1874 static __isl_give isl_printer
*print_union_pw_qpolynomial_isl(
1875 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_qpolynomial
*upwqp
)
1877 struct isl_union_print_data data
= { p
, 1, 1 };
1879 dim
= isl_union_pw_qpolynomial_get_dim(upwqp
);
1880 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
1881 p
= print_tuple(dim
, p
, isl_dim_param
, 1, 0, NULL
);
1882 p
= isl_printer_print_str(p
, " -> ");
1885 p
= isl_printer_print_str(p
, "{ ");
1886 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &print_pwqp_body
,
1889 p
= isl_printer_print_str(p
, " }");
1893 __isl_give isl_printer
*isl_printer_print_union_pw_qpolynomial(
1894 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_qpolynomial
*upwqp
)
1899 if (p
->output_format
== ISL_FORMAT_ISL
)
1900 return print_union_pw_qpolynomial_isl(p
, upwqp
);
1901 isl_die(p
->ctx
, isl_error_invalid
,
1902 "invalid output format for isl_union_pw_qpolynomial",
1905 isl_printer_free(p
);
1909 static __isl_give isl_printer
*print_qpolynomial_fold_c(
1910 __isl_take isl_printer
*p
, __isl_keep isl_dim
*dim
,
1911 __isl_keep isl_qpolynomial_fold
*fold
)
1915 for (i
= 0; i
< fold
->n
- 1; ++i
)
1916 if (fold
->type
== isl_fold_min
)
1917 p
= isl_printer_print_str(p
, "min(");
1918 else if (fold
->type
== isl_fold_max
)
1919 p
= isl_printer_print_str(p
, "max(");
1921 for (i
= 0; i
< fold
->n
; ++i
) {
1923 p
= isl_printer_print_str(p
, ", ");
1924 p
= print_qpolynomial_c(p
, dim
, fold
->qp
[i
]);
1926 p
= isl_printer_print_str(p
, ")");
1931 __isl_give isl_printer
*isl_printer_print_qpolynomial_fold(
1932 __isl_take isl_printer
*p
, __isl_keep isl_qpolynomial_fold
*fold
)
1936 if (p
->output_format
== ISL_FORMAT_ISL
)
1937 return qpolynomial_fold_print(fold
, p
);
1938 else if (p
->output_format
== ISL_FORMAT_C
)
1939 return print_qpolynomial_fold_c(p
, fold
->dim
, fold
);
1940 isl_die(p
->ctx
, isl_error_unsupported
, "unsupported output format",
1943 isl_printer_free(p
);
1947 static __isl_give isl_printer
*print_pw_qpolynomial_fold_c(
1948 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1952 if (pwf
->n
== 1 && isl_set_fast_is_universe(pwf
->p
[0].set
))
1953 return print_qpolynomial_fold_c(p
, pwf
->dim
, pwf
->p
[0].fold
);
1955 for (i
= 0; i
< pwf
->n
; ++i
) {
1956 p
= isl_printer_print_str(p
, "(");
1957 p
= print_set_c(p
, pwf
->dim
, pwf
->p
[i
].set
);
1958 p
= isl_printer_print_str(p
, ") ? (");
1959 p
= print_qpolynomial_fold_c(p
, pwf
->dim
, pwf
->p
[i
].fold
);
1960 p
= isl_printer_print_str(p
, ") : ");
1963 p
= isl_printer_print_str(p
, "0");
1967 __isl_give isl_printer
*isl_printer_print_pw_qpolynomial_fold(
1968 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1973 if (p
->output_format
== ISL_FORMAT_ISL
)
1974 return print_pw_qpolynomial_fold_isl(p
, pwf
);
1975 else if (p
->output_format
== ISL_FORMAT_C
)
1976 return print_pw_qpolynomial_fold_c(p
, pwf
);
1977 isl_assert(p
->ctx
, 0, goto error
);
1979 isl_printer_free(p
);
1983 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold
*pwf
,
1984 FILE *out
, unsigned output_format
)
1991 p
= isl_printer_to_file(pwf
->dim
->ctx
, out
);
1992 p
= isl_printer_set_output_format(p
, output_format
);
1993 p
= isl_printer_print_pw_qpolynomial_fold(p
, pwf
);
1995 isl_printer_free(p
);
1998 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold
*pwf
, void *user
)
2000 struct isl_union_print_data
*data
;
2001 data
= (struct isl_union_print_data
*)user
;
2004 data
->p
= isl_printer_print_str(data
->p
, "; ");
2007 data
->p
= isl_pwf_print_isl_body(data
->p
, pwf
);
2008 isl_pw_qpolynomial_fold_free(pwf
);
2013 static __isl_give isl_printer
*print_union_pw_qpolynomial_fold_isl(
2014 __isl_take isl_printer
*p
,
2015 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
2017 struct isl_union_print_data data
= { p
, 1, 1 };
2019 dim
= isl_union_pw_qpolynomial_fold_get_dim(upwf
);
2020 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
2021 p
= print_tuple(dim
, p
, isl_dim_param
, 1, 0, NULL
);
2022 p
= isl_printer_print_str(p
, " -> ");
2025 p
= isl_printer_print_str(p
, "{ ");
2026 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf
,
2027 &print_pwf_body
, &data
);
2029 p
= isl_printer_print_str(p
, " }");
2033 __isl_give isl_printer
*isl_printer_print_union_pw_qpolynomial_fold(
2034 __isl_take isl_printer
*p
,
2035 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
2040 if (p
->output_format
== ISL_FORMAT_ISL
)
2041 return print_union_pw_qpolynomial_fold_isl(p
, upwf
);
2042 isl_die(p
->ctx
, isl_error_invalid
,
2043 "invalid output format for isl_union_pw_qpolynomial_fold",
2046 isl_printer_free(p
);