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_union_map.h>
23 static const char *s_to
[2] = { " -> ", " \\to " };
24 static const char *s_and
[2] = { " and ", " \\wedge " };
25 static const char *s_or
[2] = { " or ", " \\vee " };
26 static const char *s_le
[2] = { "<=", "\\le" };
27 static const char *s_ge
[2] = { ">=", "\\ge" };
28 static const char *s_open_set
[2] = { "{ ", "\\{\\, " };
29 static const char *s_close_set
[2] = { " }", " \\,\\}" };
30 static const char *s_open_list
[2] = { "[", "(" };
31 static const char *s_close_list
[2] = { "]", ")" };
32 static const char *s_such_that
[2] = { " : ", " \\mid " };
33 static const char *s_open_exists
[2] = { "exists (", "\\exists \\, " };
34 static const char *s_close_exists
[2] = { ")", "" };
35 static const char *s_div_prefix
[2] = { "e", "\\alpha_" };
36 static const char *s_param_prefix
[2] = { "p", "p_" };
37 static const char *s_input_prefix
[2] = { "i", "i_" };
38 static const char *s_output_prefix
[2] = { "o", "o_" };
40 static __isl_give isl_printer
*print_constraint_polylib(
41 struct isl_basic_map
*bmap
, int ineq
, int n
, __isl_take isl_printer
*p
)
44 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
45 unsigned n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
46 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
47 isl_int
*c
= ineq
? bmap
->ineq
[n
] : bmap
->eq
[n
];
49 p
= isl_printer_start_line(p
);
50 p
= isl_printer_print_int(p
, ineq
);
51 for (i
= 0; i
< n_out
; ++i
) {
52 p
= isl_printer_print_str(p
, " ");
53 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+n_in
+i
]);
55 for (i
= 0; i
< n_in
; ++i
) {
56 p
= isl_printer_print_str(p
, " ");
57 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+i
]);
59 for (i
= 0; i
< bmap
->n_div
; ++i
) {
60 p
= isl_printer_print_str(p
, " ");
61 p
= isl_printer_print_isl_int(p
, c
[1+nparam
+n_in
+n_out
+i
]);
63 for (i
= 0; i
< nparam
; ++i
) {
64 p
= isl_printer_print_str(p
, " ");
65 p
= isl_printer_print_isl_int(p
, c
[1+i
]);
67 p
= isl_printer_print_str(p
, " ");
68 p
= isl_printer_print_isl_int(p
, c
[0]);
69 p
= isl_printer_end_line(p
);
73 static __isl_give isl_printer
*print_constraints_polylib(
74 struct isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
78 p
= isl_printer_set_isl_int_width(p
, 5);
80 for (i
= 0; i
< bmap
->n_eq
; ++i
)
81 p
= print_constraint_polylib(bmap
, 0, i
, p
);
82 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
83 p
= print_constraint_polylib(bmap
, 1, i
, p
);
88 static __isl_give isl_printer
*bset_print_constraints_polylib(
89 struct isl_basic_set
*bset
, __isl_take isl_printer
*p
)
91 return print_constraints_polylib((struct isl_basic_map
*)bset
, p
);
94 static __isl_give isl_printer
*isl_basic_map_print_polylib(
95 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
97 unsigned total
= isl_basic_map_total_dim(bmap
);
98 p
= isl_printer_start_line(p
);
99 p
= isl_printer_print_int(p
, bmap
->n_eq
+ bmap
->n_ineq
);
100 p
= isl_printer_print_str(p
, " ");
101 p
= isl_printer_print_int(p
, 1 + total
+ 1);
102 p
= isl_printer_end_line(p
);
103 return print_constraints_polylib(bmap
, p
);
106 static __isl_give isl_printer
*isl_basic_set_print_polylib(
107 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
109 return isl_basic_map_print_polylib((struct isl_basic_map
*)bset
, p
);
112 static __isl_give isl_printer
*isl_map_print_polylib(__isl_keep isl_map
*map
,
113 __isl_take isl_printer
*p
)
117 p
= isl_printer_start_line(p
);
118 p
= isl_printer_print_int(p
, map
->n
);
119 p
= isl_printer_end_line(p
);
120 for (i
= 0; i
< map
->n
; ++i
) {
121 p
= isl_printer_start_line(p
);
122 p
= isl_printer_end_line(p
);
123 p
= isl_basic_map_print_polylib(map
->p
[i
], p
);
128 static __isl_give isl_printer
*isl_set_print_polylib(__isl_keep isl_set
*set
,
129 __isl_take isl_printer
*p
)
131 return isl_map_print_polylib((struct isl_map
*)set
, p
);
134 static int count_same_name(__isl_keep isl_dim
*dim
,
135 enum isl_dim_type type
, unsigned pos
, const char *name
)
141 for (t
= isl_dim_param
; t
<= type
&& t
<= isl_dim_out
; ++t
) {
142 s
= t
== type
? pos
: isl_dim_size(dim
, t
);
143 for (p
= 0; p
< s
; ++p
) {
144 const char *n
= isl_dim_get_name(dim
, t
, p
);
145 if (n
&& !strcmp(n
, name
))
152 static __isl_give isl_printer
*print_name(__isl_keep isl_dim
*dim
,
153 __isl_take isl_printer
*p
, enum isl_dim_type type
, unsigned pos
,
160 name
= type
== isl_dim_div
? NULL
: isl_dim_get_name(dim
, type
, pos
);
164 if (type
== isl_dim_param
)
165 prefix
= s_param_prefix
[latex
];
166 else if (type
== isl_dim_div
)
167 prefix
= s_div_prefix
[latex
];
168 else if (set
|| type
== isl_dim_in
)
169 prefix
= s_input_prefix
[latex
];
171 prefix
= s_output_prefix
[latex
];
172 snprintf(buffer
, sizeof(buffer
), "%s%d", prefix
, pos
);
175 primes
= count_same_name(dim
, name
== buffer
? isl_dim_div
: type
,
177 p
= isl_printer_print_str(p
, name
);
179 p
= isl_printer_print_str(p
, "'");
183 static enum isl_dim_type
pos2type(__isl_keep isl_dim
*dim
, unsigned *pos
)
185 enum isl_dim_type type
;
186 unsigned n_in
= isl_dim_size(dim
, isl_dim_in
);
187 unsigned n_out
= isl_dim_size(dim
, isl_dim_out
);
188 unsigned nparam
= isl_dim_size(dim
, isl_dim_param
);
190 if (*pos
< 1 + nparam
) {
191 type
= isl_dim_param
;
193 } else if (*pos
< 1 + nparam
+ n_in
) {
196 } else if (*pos
< 1 + nparam
+ n_in
+ n_out
) {
198 *pos
-= 1 + nparam
+ n_in
;
201 *pos
-= 1 + nparam
+ n_in
+ n_out
;
207 static __isl_give isl_printer
*print_term(__isl_keep isl_dim
*dim
,
208 isl_int c
, unsigned pos
, __isl_take isl_printer
*p
, int set
, int latex
)
210 enum isl_dim_type type
;
213 return isl_printer_print_isl_int(p
, c
);
215 if (isl_int_is_one(c
))
217 else if (isl_int_is_negone(c
))
218 p
= isl_printer_print_str(p
, "-");
220 p
= isl_printer_print_isl_int(p
, c
);
221 type
= pos2type(dim
, &pos
);
222 p
= print_name(dim
, p
, type
, pos
, set
, latex
);
226 static __isl_give isl_printer
*print_affine_of_len(__isl_keep isl_dim
*dim
,
227 __isl_take isl_printer
*p
, isl_int
*c
, int len
, int set
)
232 for (i
= 0, first
= 1; i
< len
; ++i
) {
234 if (isl_int_is_zero(c
[i
]))
237 if (isl_int_is_neg(c
[i
])) {
239 isl_int_neg(c
[i
], c
[i
]);
240 p
= isl_printer_print_str(p
, " - ");
242 p
= isl_printer_print_str(p
, " + ");
245 p
= print_term(dim
, c
[i
], i
, p
, set
, 0);
247 isl_int_neg(c
[i
], c
[i
]);
250 p
= isl_printer_print_str(p
, "0");
254 static __isl_give isl_printer
*print_affine(__isl_keep isl_basic_map
*bmap
,
255 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, isl_int
*c
, int set
)
257 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
258 return print_affine_of_len(dim
, p
, c
, len
, set
);
261 static int defining_equality(__isl_keep isl_basic_map
*eq
,
262 enum isl_dim_type type
, int pos
)
270 pos
+= isl_dim_offset(eq
->dim
, type
);
271 total
= isl_basic_map_total_dim(eq
);
273 for (i
= 0; i
< eq
->n_eq
; ++i
) {
274 if (isl_seq_last_non_zero(eq
->eq
[i
] + 1, total
) != pos
)
276 if (isl_int_is_one(eq
->eq
[i
][1 + pos
]))
277 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], 1 + total
);
284 static __isl_give isl_printer
*print_var_list(__isl_keep isl_dim
*dim
,
285 __isl_take isl_printer
*p
, enum isl_dim_type type
, int set
, int latex
,
286 __isl_keep isl_basic_map
*eq
)
290 for (i
= 0; i
< isl_dim_size(dim
, type
); ++i
) {
292 p
= isl_printer_print_str(p
, ", ");
293 j
= defining_equality(eq
, type
, i
);
295 int pos
= 1 + isl_dim_offset(dim
, type
) + i
;
296 p
= print_affine_of_len(dim
, p
, eq
->eq
[j
], pos
, set
);
298 p
= print_name(dim
, p
, type
, i
, set
, latex
);
303 static __isl_give isl_printer
*print_tuple(__isl_keep isl_dim
*dim
,
304 __isl_take isl_printer
*p
, enum isl_dim_type type
, int set
, int latex
,
305 __isl_keep isl_basic_map
*eq
)
307 const char *name
= NULL
;
308 unsigned n
= isl_dim_size(dim
, type
);
309 if ((type
== isl_dim_in
|| type
== isl_dim_out
)) {
310 name
= isl_dim_get_tuple_name(dim
, type
);
313 p
= isl_printer_print_str(p
, "\\mathrm{");
314 p
= isl_printer_print_str(p
, name
);
316 p
= isl_printer_print_str(p
, "}");
319 if (!latex
|| n
!= 1 || name
)
320 p
= isl_printer_print_str(p
, s_open_list
[latex
]);
321 p
= print_var_list(dim
, p
, type
, set
, latex
, eq
);
322 if (!latex
|| n
!= 1 || name
)
323 p
= isl_printer_print_str(p
, s_close_list
[latex
]);
327 static __isl_give isl_printer
*print_dim(__isl_keep isl_dim
*dim
,
328 __isl_take isl_printer
*p
, int set
, int latex
,
329 __isl_keep isl_basic_map
*eq
)
332 p
= print_tuple(dim
, p
, isl_dim_set
, 1, latex
, eq
);
334 p
= print_tuple(dim
, p
, isl_dim_in
, 0, latex
, eq
);
335 p
= isl_printer_print_str(p
, s_to
[0]);
336 p
= print_tuple(dim
, p
, isl_dim_out
, 0, latex
, eq
);
342 static __isl_give isl_printer
*print_omega_parameters(__isl_keep isl_dim
*dim
,
343 __isl_take isl_printer
*p
)
345 if (isl_dim_size(dim
, isl_dim_param
) == 0)
348 p
= isl_printer_start_line(p
);
349 p
= isl_printer_print_str(p
, "symbolic ");
350 p
= print_var_list(dim
, p
, isl_dim_param
, 0, 0, NULL
);
351 p
= isl_printer_print_str(p
, ";");
352 p
= isl_printer_end_line(p
);
356 static __isl_give isl_printer
*print_constraint(struct isl_basic_map
*bmap
,
357 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
,
358 isl_int
*c
, int last
, const char *op
, int first_constraint
, int set
,
361 if (!first_constraint
)
362 p
= isl_printer_print_str(p
, s_and
[latex
]);
364 isl_int_abs(c
[last
], c
[last
]);
366 p
= print_term(dim
, c
[last
], last
, p
, set
, latex
);
368 p
= isl_printer_print_str(p
, " ");
369 p
= isl_printer_print_str(p
, op
);
370 p
= isl_printer_print_str(p
, " ");
372 isl_int_set_si(c
[last
], 0);
373 p
= print_affine(bmap
, dim
, p
, c
, set
);
378 static __isl_give isl_printer
*print_constraints(__isl_keep isl_basic_map
*bmap
,
379 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, int set
, int latex
)
383 unsigned total
= isl_basic_map_total_dim(bmap
);
385 c
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
389 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
390 int l
= isl_seq_last_non_zero(bmap
->eq
[i
], 1 + total
);
391 isl_assert(bmap
->ctx
, l
>= 0, goto error
);
392 if (isl_int_is_neg(bmap
->eq
[i
][l
]))
393 isl_seq_cpy(c
->el
, bmap
->eq
[i
], 1 + total
);
395 isl_seq_neg(c
->el
, bmap
->eq
[i
], 1 + total
);
396 p
= print_constraint(bmap
, dim
, p
, c
->el
, l
,
397 "=", i
== bmap
->n_eq
- 1, set
, latex
);
399 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
400 int l
= isl_seq_last_non_zero(bmap
->ineq
[i
], 1 + total
);
405 s
= isl_int_sgn(bmap
->ineq
[i
][l
]);
407 isl_seq_cpy(c
->el
, bmap
->ineq
[i
], 1 + total
);
409 isl_seq_neg(c
->el
, bmap
->ineq
[i
], 1 + total
);
410 op
= s
< 0 ? s_le
[latex
] : s_ge
[latex
];
411 p
= print_constraint(bmap
, dim
, p
, c
->el
, l
,
412 op
, !bmap
->n_eq
&& !i
, set
, latex
);
423 static __isl_give isl_printer
*print_omega_constraints(
424 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
, int set
)
426 if (bmap
->n_eq
+ bmap
->n_ineq
== 0)
429 p
= isl_printer_print_str(p
, ": ");
430 if (bmap
->n_div
> 0) {
432 p
= isl_printer_print_str(p
, "exists (");
433 for (i
= 0; i
< bmap
->n_div
; ++i
) {
435 p
= isl_printer_print_str(p
, ", ");
436 p
= print_name(bmap
->dim
, p
, isl_dim_div
, i
, 0, 0);
438 p
= isl_printer_print_str(p
, ": ");
440 p
= print_constraints(bmap
, bmap
->dim
, p
, set
, 0);
442 p
= isl_printer_print_str(p
, ")");
446 static __isl_give isl_printer
*basic_map_print_omega(
447 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
449 p
= isl_printer_print_str(p
, "{ [");
450 p
= print_var_list(bmap
->dim
, p
, isl_dim_in
, 0, 0, NULL
);
451 p
= isl_printer_print_str(p
, "] -> [");
452 p
= print_var_list(bmap
->dim
, p
, isl_dim_out
, 0, 0, NULL
);
453 p
= isl_printer_print_str(p
, "] ");
454 p
= print_omega_constraints(bmap
, p
, 0);
455 p
= isl_printer_print_str(p
, " }");
459 static __isl_give isl_printer
*isl_basic_map_print_omega(
460 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
)
462 p
= print_omega_parameters(bmap
->dim
, p
);
464 p
= isl_printer_start_line(p
);
465 p
= basic_map_print_omega(bmap
, p
);
466 p
= isl_printer_end_line(p
);
470 static __isl_give isl_printer
*basic_set_print_omega(
471 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
473 p
= isl_printer_print_str(p
, "{ [");
474 p
= print_var_list(bset
->dim
, p
, isl_dim_set
, 1, 0, NULL
);
475 p
= isl_printer_print_str(p
, "] ");
476 p
= print_omega_constraints((isl_basic_map
*)bset
, p
, 1);
477 p
= isl_printer_print_str(p
, " }");
481 static __isl_give isl_printer
*isl_basic_set_print_omega(
482 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
)
484 p
= print_omega_parameters(bset
->dim
, p
);
486 p
= isl_printer_start_line(p
);
487 p
= basic_set_print_omega(bset
, p
);
488 p
= isl_printer_end_line(p
);
492 static __isl_give isl_printer
*isl_map_print_omega(__isl_keep isl_map
*map
,
493 __isl_take isl_printer
*p
)
497 p
= print_omega_parameters(map
->dim
, p
);
499 p
= isl_printer_start_line(p
);
500 for (i
= 0; i
< map
->n
; ++i
) {
502 p
= isl_printer_print_str(p
, " union ");
503 p
= basic_map_print_omega(map
->p
[i
], p
);
505 p
= isl_printer_end_line(p
);
509 static __isl_give isl_printer
*isl_set_print_omega(__isl_keep isl_set
*set
,
510 __isl_take isl_printer
*p
)
514 p
= print_omega_parameters(set
->dim
, p
);
516 p
= isl_printer_start_line(p
);
517 for (i
= 0; i
< set
->n
; ++i
) {
519 p
= isl_printer_print_str(p
, " union ");
520 p
= basic_set_print_omega(set
->p
[i
], p
);
522 p
= isl_printer_end_line(p
);
526 static __isl_give isl_printer
*print_disjunct(__isl_keep isl_basic_map
*bmap
,
527 __isl_keep isl_dim
*dim
, __isl_take isl_printer
*p
, int set
, int latex
)
529 if (bmap
->n_div
> 0) {
531 p
= isl_printer_print_str(p
, s_open_exists
[latex
]);
532 for (i
= 0; i
< bmap
->n_div
; ++i
) {
534 p
= isl_printer_print_str(p
, ", ");
535 p
= print_name(dim
, p
, isl_dim_div
, i
, 0, latex
);
536 if (latex
|| isl_int_is_zero(bmap
->div
[i
][0]))
538 p
= isl_printer_print_str(p
, " = [(");
539 p
= print_affine(bmap
, dim
, p
, bmap
->div
[i
] + 1, set
);
540 p
= isl_printer_print_str(p
, ")/");
541 p
= isl_printer_print_isl_int(p
, bmap
->div
[i
][0]);
542 p
= isl_printer_print_str(p
, "]");
544 p
= isl_printer_print_str(p
, ": ");
547 p
= print_constraints(bmap
, dim
, p
, set
, latex
);
550 p
= isl_printer_print_str(p
, s_close_exists
[latex
]);
554 static __isl_give isl_printer
*isl_basic_map_print_isl(
555 __isl_keep isl_basic_map
*bmap
, __isl_take isl_printer
*p
, int latex
)
559 p
= isl_printer_start_line(p
);
560 if (isl_basic_map_dim(bmap
, isl_dim_param
) > 0) {
561 p
= print_tuple(bmap
->dim
, p
, isl_dim_param
, 0, latex
, NULL
);
562 p
= isl_printer_print_str(p
, " -> ");
564 p
= isl_printer_print_str(p
, "{ ");
565 p
= print_dim(bmap
->dim
, p
, 0, latex
, NULL
);
566 p
= isl_printer_print_str(p
, " : ");
567 p
= print_disjunct(bmap
, bmap
->dim
, p
, 0, latex
);
568 p
= isl_printer_print_str(p
, " }");
569 p
= isl_printer_end_line(p
);
573 static __isl_give isl_printer
*isl_basic_set_print_isl(
574 __isl_keep isl_basic_set
*bset
, __isl_take isl_printer
*p
, int latex
)
578 p
= isl_printer_start_line(p
);
579 if (isl_basic_set_dim(bset
, isl_dim_param
) > 0) {
580 p
= print_tuple(bset
->dim
, p
, isl_dim_param
, 0, latex
, NULL
);
581 p
= isl_printer_print_str(p
, " -> ");
583 p
= isl_printer_print_str(p
, "{ ");
584 p
= print_dim(bset
->dim
, p
, 1, latex
, NULL
);
585 p
= isl_printer_print_str(p
, " : ");
586 p
= print_disjunct((isl_basic_map
*)bset
, bset
->dim
, p
, 1, latex
);
587 p
= isl_printer_print_str(p
, " }");
588 p
= isl_printer_end_line(p
);
592 static __isl_give isl_printer
*print_disjuncts(__isl_keep isl_map
*map
,
593 __isl_take isl_printer
*p
, int set
, int latex
)
597 if (isl_map_fast_is_universe(map
))
600 p
= isl_printer_print_str(p
, s_such_that
[latex
]);
602 p
= isl_printer_print_str(p
, "1 = 0");
603 for (i
= 0; i
< map
->n
; ++i
) {
605 p
= isl_printer_print_str(p
, s_or
[latex
]);
606 if (map
->n
> 1 && map
->p
[i
]->n_eq
+ map
->p
[i
]->n_ineq
> 1)
607 p
= isl_printer_print_str(p
, "(");
608 p
= print_disjunct(map
->p
[i
], map
->dim
, p
, set
, latex
);
609 if (map
->n
> 1 && map
->p
[i
]->n_eq
+ map
->p
[i
]->n_ineq
> 1)
610 p
= isl_printer_print_str(p
, ")");
615 struct isl_aff_split
{
620 static void free_split(__isl_take
struct isl_aff_split
*split
, int n
)
627 for (i
= 0; i
< n
; ++i
) {
628 isl_basic_map_free(split
[i
].aff
);
629 isl_map_free(split
[i
].map
);
635 static __isl_give isl_basic_map
*get_aff(__isl_take isl_basic_map
*bmap
)
638 unsigned nparam
, n_in
, n_out
, total
;
640 bmap
= isl_basic_map_cow(bmap
);
643 if (isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
) < 0)
646 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
647 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
648 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
649 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
650 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
651 j
= isl_seq_last_non_zero(bmap
->eq
[i
] + 1, total
);
652 if (j
>= nparam
&& j
< nparam
+ n_in
+ n_out
&&
653 (isl_int_is_one(bmap
->eq
[i
][1 + j
]) ||
654 isl_int_is_negone(bmap
->eq
[i
][1 + j
])))
656 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
660 bmap
= isl_basic_map_finalize(bmap
);
664 isl_basic_map_free(bmap
);
668 static int aff_split_cmp(const void *p1
, const void *p2
)
670 const struct isl_aff_split
*s1
, *s2
;
671 s1
= (const struct isl_aff_split
*) p1
;
672 s2
= (const struct isl_aff_split
*) p2
;
674 return isl_basic_map_fast_cmp(s1
->aff
, s2
->aff
);
677 static __isl_give isl_basic_map
*drop_aff(__isl_take isl_basic_map
*bmap
,
678 __isl_keep isl_basic_map
*aff
)
686 total
= isl_dim_total(bmap
->dim
);
688 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
689 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
692 for (j
= 0; j
< aff
->n_eq
; ++j
) {
693 if (!isl_seq_eq(bmap
->eq
[i
], aff
->eq
[j
], 1 + total
) &&
694 !isl_seq_is_neg(bmap
->eq
[i
], aff
->eq
[j
], 1 + total
))
696 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
704 isl_basic_map_free(bmap
);
708 static __isl_give
struct isl_aff_split
*split_aff(__isl_keep isl_map
*map
)
711 struct isl_aff_split
*split
;
713 split
= isl_calloc_array(map
->isl
, struct isl_aff_split
, map
->n
);
717 for (i
= 0; i
< map
->n
; ++i
) {
719 split
[i
].aff
= get_aff(isl_basic_map_copy(map
->p
[i
]));
720 bmap
= isl_basic_map_copy(map
->p
[i
]);
721 bmap
= isl_basic_map_cow(bmap
);
722 bmap
= drop_aff(bmap
, split
[i
].aff
);
723 split
[i
].map
= isl_map_from_basic_map(bmap
);
724 if (!split
[i
].aff
|| !split
[i
].map
)
728 qsort(split
, map
->n
, sizeof(struct isl_aff_split
), &aff_split_cmp
);
731 for (i
= n
- 1; i
>= 1; --i
) {
732 if (!isl_basic_map_fast_is_equal(split
[i
- 1].aff
,
735 isl_basic_map_free(split
[i
].aff
);
736 split
[i
- 1].map
= isl_map_union(split
[i
- 1].map
,
739 split
[i
] = split
[n
- 1];
740 split
[n
- 1].aff
= NULL
;
741 split
[n
- 1].map
= NULL
;
747 free_split(split
, map
->n
);
751 static __isl_give isl_printer
*print_split_map(__isl_take isl_printer
*p
,
752 struct isl_aff_split
*split
, int n
, int set
)
756 for (i
= 0; i
< n
; ++i
) {
761 dim
= split
[i
].map
->dim
;
763 p
= isl_printer_print_str(p
, "; ");
764 p
= print_dim(dim
, p
, set
, 0, split
[i
].aff
);
765 p
= print_disjuncts(split
[i
].map
, p
, set
, 0);
771 static __isl_give isl_printer
*isl_map_print_isl_body(__isl_keep isl_map
*map
,
772 __isl_take isl_printer
*p
, int set
)
774 struct isl_aff_split
*split
= NULL
;
777 split
= split_aff(map
);
779 p
= print_split_map(p
, split
, map
->n
, set
);
781 p
= print_dim(map
->dim
, p
, set
, 0, NULL
);
782 p
= print_disjuncts(map
, p
, set
, 0);
784 free_split(split
, map
->n
);
788 static __isl_give isl_printer
*isl_map_print_isl(__isl_keep isl_map
*map
,
789 __isl_take isl_printer
*p
, int set
)
791 if (isl_map_dim(map
, isl_dim_param
) > 0) {
792 p
= print_tuple(map
->dim
, p
, isl_dim_param
, set
, 0, NULL
);
793 p
= isl_printer_print_str(p
, s_to
[0]);
795 p
= isl_printer_print_str(p
, s_open_set
[0]);
796 p
= isl_map_print_isl_body(map
, p
, set
);
797 p
= isl_printer_print_str(p
, s_close_set
[0]);
801 static __isl_give isl_printer
*print_latex_map(__isl_keep isl_map
*map
,
802 __isl_take isl_printer
*p
, __isl_keep isl_basic_map
*aff
, int set
)
804 if (isl_map_dim(map
, isl_dim_param
) > 0) {
805 p
= print_tuple(map
->dim
, p
, isl_dim_param
, set
, 1, NULL
);
806 p
= isl_printer_print_str(p
, s_to
[1]);
808 p
= isl_printer_print_str(p
, s_open_set
[1]);
809 p
= print_dim(map
->dim
, p
, set
, 1, aff
);
810 p
= print_disjuncts(map
, p
, set
, 1);
811 p
= isl_printer_print_str(p
, s_close_set
[1]);
816 static __isl_give isl_printer
*isl_map_print_latex(__isl_keep isl_map
*map
,
817 __isl_take isl_printer
*p
, int set
)
820 struct isl_aff_split
*split
= NULL
;
823 split
= split_aff(map
);
826 return print_latex_map(map
, p
, NULL
, set
);
828 for (i
= 0; i
< map
->n
; ++i
) {
832 p
= isl_printer_print_str(p
, " \\cup ");
833 p
= print_latex_map(split
[i
].map
, p
, split
[i
].aff
, set
);
836 free_split(split
, map
->n
);
840 __isl_give isl_printer
*isl_printer_print_basic_map(__isl_take isl_printer
*p
,
841 __isl_keep isl_basic_map
*bmap
)
845 if (p
->output_format
== ISL_FORMAT_ISL
)
846 return isl_basic_map_print_isl(bmap
, p
, 0);
847 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
848 return isl_basic_map_print_omega(bmap
, p
);
849 isl_assert(bmap
->ctx
, 0, goto error
);
855 void isl_basic_map_print(__isl_keep isl_basic_map
*bmap
, FILE *out
, int indent
,
856 const char *prefix
, const char *suffix
, unsigned output_format
)
858 isl_printer
*printer
;
863 printer
= isl_printer_to_file(bmap
->ctx
, out
);
864 printer
= isl_printer_set_indent(printer
, indent
);
865 printer
= isl_printer_set_prefix(printer
, prefix
);
866 printer
= isl_printer_set_suffix(printer
, suffix
);
867 printer
= isl_printer_set_output_format(printer
, output_format
);
868 isl_printer_print_basic_map(printer
, bmap
);
870 isl_printer_free(printer
);
873 __isl_give isl_printer
*isl_printer_print_basic_set(__isl_take isl_printer
*p
,
874 __isl_keep isl_basic_set
*bset
)
879 if (p
->output_format
== ISL_FORMAT_ISL
)
880 return isl_basic_set_print_isl(bset
, p
, 0);
881 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
882 return isl_basic_set_print_polylib(bset
, p
);
883 else if (p
->output_format
== ISL_FORMAT_POLYLIB_CONSTRAINTS
)
884 return bset_print_constraints_polylib(bset
, p
);
885 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
886 return isl_basic_set_print_omega(bset
, p
);
887 isl_assert(p
->ctx
, 0, goto error
);
893 void isl_basic_set_print(struct isl_basic_set
*bset
, FILE *out
, int indent
,
894 const char *prefix
, const char *suffix
, unsigned output_format
)
896 isl_printer
*printer
;
901 printer
= isl_printer_to_file(bset
->ctx
, out
);
902 printer
= isl_printer_set_indent(printer
, indent
);
903 printer
= isl_printer_set_prefix(printer
, prefix
);
904 printer
= isl_printer_set_suffix(printer
, suffix
);
905 printer
= isl_printer_set_output_format(printer
, output_format
);
906 isl_printer_print_basic_set(printer
, bset
);
908 isl_printer_free(printer
);
911 __isl_give isl_printer
*isl_printer_print_set(__isl_take isl_printer
*p
,
912 __isl_keep isl_set
*set
)
916 if (p
->output_format
== ISL_FORMAT_ISL
)
917 return isl_map_print_isl((isl_map
*)set
, p
, 1);
918 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
919 return isl_set_print_polylib(set
, p
);
920 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
921 return isl_set_print_omega(set
, p
);
922 else if (p
->output_format
== ISL_FORMAT_LATEX
)
923 return isl_map_print_latex((isl_map
*)set
, p
, 1);
924 isl_assert(set
->ctx
, 0, goto error
);
930 void isl_set_print(struct isl_set
*set
, FILE *out
, int indent
,
931 unsigned output_format
)
933 isl_printer
*printer
;
938 printer
= isl_printer_to_file(set
->ctx
, out
);
939 printer
= isl_printer_set_indent(printer
, indent
);
940 printer
= isl_printer_set_output_format(printer
, output_format
);
941 printer
= isl_printer_print_set(printer
, set
);
943 isl_printer_free(printer
);
946 __isl_give isl_printer
*isl_printer_print_map(__isl_take isl_printer
*p
,
947 __isl_keep isl_map
*map
)
952 if (p
->output_format
== ISL_FORMAT_ISL
)
953 return isl_map_print_isl(map
, p
, 0);
954 else if (p
->output_format
== ISL_FORMAT_POLYLIB
)
955 return isl_map_print_polylib(map
, p
);
956 else if (p
->output_format
== ISL_FORMAT_OMEGA
)
957 return isl_map_print_omega(map
, p
);
958 else if (p
->output_format
== ISL_FORMAT_LATEX
)
959 return isl_map_print_latex(map
, p
, 0);
960 isl_assert(map
->ctx
, 0, goto error
);
966 struct isl_union_print_data
{
972 static int print_map_body(__isl_take isl_map
*map
, void *user
)
974 struct isl_union_print_data
*data
;
975 data
= (struct isl_union_print_data
*)user
;
978 data
->p
= isl_printer_print_str(data
->p
, "; ");
981 data
->p
= isl_map_print_isl_body(map
, data
->p
, data
->set
);
987 static __isl_give isl_printer
*isl_union_map_print_isl(
988 __isl_keep isl_union_map
*umap
, __isl_take isl_printer
*p
, int set
)
990 struct isl_union_print_data data
= { p
, set
, 1 };
992 dim
= isl_union_map_get_dim(umap
);
993 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
994 p
= print_tuple(dim
, p
, isl_dim_param
, set
, 0, NULL
);
995 p
= isl_printer_print_str(p
, s_to
[0]);
998 p
= isl_printer_print_str(p
, s_open_set
[0]);
999 isl_union_map_foreach_map(umap
, &print_map_body
, &data
);
1001 p
= isl_printer_print_str(p
, s_close_set
[0]);
1005 static int print_latex_map_body(__isl_take isl_map
*map
, void *user
)
1007 struct isl_union_print_data
*data
;
1008 data
= (struct isl_union_print_data
*)user
;
1011 data
->p
= isl_printer_print_str(data
->p
, " \\cup ");
1014 data
->p
= isl_map_print_latex(map
, data
->p
, data
->set
);
1020 static __isl_give isl_printer
*isl_union_map_print_latex(
1021 __isl_keep isl_union_map
*umap
, __isl_take isl_printer
*p
, int set
)
1023 struct isl_union_print_data data
= { p
, set
, 1 };
1024 isl_union_map_foreach_map(umap
, &print_latex_map_body
, &data
);
1029 __isl_give isl_printer
*isl_printer_print_union_map(__isl_take isl_printer
*p
,
1030 __isl_keep isl_union_map
*umap
)
1035 if (p
->output_format
== ISL_FORMAT_ISL
)
1036 return isl_union_map_print_isl(umap
, p
, 0);
1037 if (p
->output_format
== ISL_FORMAT_LATEX
)
1038 return isl_union_map_print_latex(umap
, p
, 0);
1040 isl_die(p
->ctx
, isl_error_invalid
,
1041 "invalid output format for isl_union_map", goto error
);
1043 isl_printer_free(p
);
1047 __isl_give isl_printer
*isl_printer_print_union_set(__isl_take isl_printer
*p
,
1048 __isl_keep isl_union_set
*uset
)
1053 if (p
->output_format
== ISL_FORMAT_ISL
)
1054 return isl_union_map_print_isl((isl_union_map
*)uset
, p
, 1);
1055 if (p
->output_format
== ISL_FORMAT_LATEX
)
1056 return isl_union_map_print_latex((isl_union_map
*)uset
, p
, 1);
1058 isl_die(p
->ctx
, isl_error_invalid
,
1059 "invalid output format for isl_union_set", goto error
);
1061 isl_printer_free(p
);
1065 void isl_map_print(__isl_keep isl_map
*map
, FILE *out
, int indent
,
1066 unsigned output_format
)
1068 isl_printer
*printer
;
1073 printer
= isl_printer_to_file(map
->ctx
, out
);
1074 printer
= isl_printer_set_indent(printer
, indent
);
1075 printer
= isl_printer_set_output_format(printer
, output_format
);
1076 printer
= isl_printer_print_map(printer
, map
);
1078 isl_printer_free(printer
);
1081 static int upoly_rec_n_non_zero(__isl_keep
struct isl_upoly_rec
*rec
)
1086 for (i
= 0, n
= 0; i
< rec
->n
; ++i
)
1087 if (!isl_upoly_is_zero(rec
->p
[i
]))
1093 static __isl_give isl_printer
*print_div(__isl_keep isl_dim
*dim
,
1094 __isl_keep isl_mat
*div
, int pos
, __isl_take isl_printer
*p
)
1096 int c
= p
->output_format
== ISL_FORMAT_C
;
1097 p
= isl_printer_print_str(p
, c
? "floord(" : "[(");
1098 p
= print_affine_of_len(dim
, p
, div
->row
[pos
] + 1, div
->n_col
- 1, 1);
1099 p
= isl_printer_print_str(p
, c
? ", " : ")/");
1100 p
= isl_printer_print_isl_int(p
, div
->row
[pos
][0]);
1101 p
= isl_printer_print_str(p
, c
? ")" : "]");
1105 static __isl_give isl_printer
*upoly_print_cst(__isl_keep
struct isl_upoly
*up
,
1106 __isl_take isl_printer
*p
, int first
)
1108 struct isl_upoly_cst
*cst
;
1111 cst
= isl_upoly_as_cst(up
);
1114 neg
= !first
&& isl_int_is_neg(cst
->n
);
1116 p
= isl_printer_print_str(p
, neg
? " - " : " + ");
1118 isl_int_neg(cst
->n
, cst
->n
);
1119 if (isl_int_is_zero(cst
->d
)) {
1120 int sgn
= isl_int_sgn(cst
->n
);
1121 p
= isl_printer_print_str(p
, sgn
< 0 ? "-infty" :
1122 sgn
== 0 ? "NaN" : "infty");
1124 p
= isl_printer_print_isl_int(p
, cst
->n
);
1126 isl_int_neg(cst
->n
, cst
->n
);
1127 if (!isl_int_is_zero(cst
->d
) && !isl_int_is_one(cst
->d
)) {
1128 p
= isl_printer_print_str(p
, "/");
1129 p
= isl_printer_print_isl_int(p
, cst
->d
);
1133 isl_printer_free(p
);
1137 static __isl_give isl_printer
*print_base(__isl_take isl_printer
*p
,
1138 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
, int var
)
1142 total
= isl_dim_total(dim
);
1144 p
= print_term(dim
, dim
->ctx
->one
, 1 + var
, p
, 1, 0);
1146 p
= print_div(dim
, div
, var
- total
, p
);
1150 static __isl_give isl_printer
*print_pow(__isl_take isl_printer
*p
,
1151 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
, int var
, int exp
)
1153 p
= print_base(p
, dim
, div
, var
);
1156 if (p
->output_format
== ISL_FORMAT_C
) {
1158 for (i
= 1; i
< exp
; ++i
) {
1159 p
= isl_printer_print_str(p
, "*");
1160 p
= print_base(p
, dim
, div
, var
);
1163 p
= isl_printer_print_str(p
, "^");
1164 p
= isl_printer_print_int(p
, exp
);
1169 static __isl_give isl_printer
*upoly_print(__isl_keep
struct isl_upoly
*up
,
1170 __isl_keep isl_dim
*dim
, __isl_keep isl_mat
*div
,
1171 __isl_take isl_printer
*p
)
1174 struct isl_upoly_rec
*rec
;
1176 if (!p
|| !up
|| !dim
|| !div
)
1179 if (isl_upoly_is_cst(up
))
1180 return upoly_print_cst(up
, p
, 1);
1182 rec
= isl_upoly_as_rec(up
);
1185 n
= upoly_rec_n_non_zero(rec
);
1187 p
= isl_printer_print_str(p
, "(");
1188 for (i
= 0, first
= 1; i
< rec
->n
; ++i
) {
1189 if (isl_upoly_is_zero(rec
->p
[i
]))
1191 if (isl_upoly_is_negone(rec
->p
[i
])) {
1193 p
= isl_printer_print_str(p
, "-1");
1195 p
= isl_printer_print_str(p
, "-");
1197 p
= isl_printer_print_str(p
, " - ");
1198 } else if (isl_upoly_is_cst(rec
->p
[i
]) &&
1199 !isl_upoly_is_one(rec
->p
[i
]))
1200 p
= upoly_print_cst(rec
->p
[i
], p
, first
);
1203 p
= isl_printer_print_str(p
, " + ");
1204 if (i
== 0 || !isl_upoly_is_one(rec
->p
[i
]))
1205 p
= upoly_print(rec
->p
[i
], dim
, div
, p
);
1210 if (!isl_upoly_is_one(rec
->p
[i
]) &&
1211 !isl_upoly_is_negone(rec
->p
[i
]))
1212 p
= isl_printer_print_str(p
, " * ");
1213 p
= print_pow(p
, dim
, div
, rec
->up
.var
, i
);
1216 p
= isl_printer_print_str(p
, ")");
1219 isl_printer_free(p
);
1223 __isl_give isl_printer
*isl_printer_print_qpolynomial(__isl_take isl_printer
*p
,
1224 __isl_keep isl_qpolynomial
*qp
)
1228 p
= upoly_print(qp
->upoly
, qp
->dim
, qp
->div
, p
);
1231 isl_printer_free(p
);
1235 void isl_qpolynomial_print(__isl_keep isl_qpolynomial
*qp
, FILE *out
,
1236 unsigned output_format
)
1243 isl_assert(qp
->dim
->ctx
, output_format
== ISL_FORMAT_ISL
, return);
1244 p
= isl_printer_to_file(qp
->dim
->ctx
, out
);
1245 p
= isl_printer_print_qpolynomial(p
, qp
);
1246 isl_printer_free(p
);
1249 static __isl_give isl_printer
*qpolynomial_fold_print(
1250 __isl_keep isl_qpolynomial_fold
*fold
, __isl_take isl_printer
*p
)
1254 if (fold
->type
== isl_fold_min
)
1255 p
= isl_printer_print_str(p
, "min");
1256 else if (fold
->type
== isl_fold_max
)
1257 p
= isl_printer_print_str(p
, "max");
1258 p
= isl_printer_print_str(p
, "(");
1259 for (i
= 0; i
< fold
->n
; ++i
) {
1261 p
= isl_printer_print_str(p
, ", ");
1262 p
= isl_printer_print_qpolynomial(p
, fold
->qp
[i
]);
1264 p
= isl_printer_print_str(p
, ")");
1268 __isl_give isl_printer
*isl_printer_print_qpolynomial_fold(
1269 __isl_take isl_printer
*p
, __isl_keep isl_qpolynomial_fold
*fold
)
1273 p
= qpolynomial_fold_print(fold
, p
);
1276 isl_printer_free(p
);
1280 void isl_qpolynomial_fold_print(__isl_keep isl_qpolynomial_fold
*fold
,
1281 FILE *out
, unsigned output_format
)
1288 isl_assert(fold
->dim
->ctx
, output_format
== ISL_FORMAT_ISL
, return);
1290 p
= isl_printer_to_file(fold
->dim
->ctx
, out
);
1291 p
= isl_printer_print_qpolynomial_fold(p
, fold
);
1293 isl_printer_free(p
);
1296 static __isl_give isl_printer
*isl_pwqp_print_isl_body(
1297 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1301 for (i
= 0; i
< pwqp
->n
; ++i
) {
1303 p
= isl_printer_print_str(p
, "; ");
1304 if (isl_dim_size(pwqp
->p
[i
].set
->dim
, isl_dim_set
) > 0 ||
1305 isl_dim_get_tuple_name(pwqp
->p
[i
].set
->dim
, isl_dim_set
)) {
1306 p
= print_dim(pwqp
->p
[i
].set
->dim
, p
, 1, 0, NULL
);
1307 p
= isl_printer_print_str(p
, " -> ");
1309 p
= isl_printer_print_qpolynomial(p
, pwqp
->p
[i
].qp
);
1310 p
= print_disjuncts((isl_map
*)pwqp
->p
[i
].set
, p
, 1, 0);
1316 static __isl_give isl_printer
*print_pw_qpolynomial_isl(
1317 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1322 if (isl_dim_size(pwqp
->dim
, isl_dim_param
) > 0) {
1323 p
= print_tuple(pwqp
->dim
, p
, isl_dim_param
, 0, 0, NULL
);
1324 p
= isl_printer_print_str(p
, " -> ");
1326 p
= isl_printer_print_str(p
, "{ ");
1328 if (isl_dim_size(pwqp
->dim
, isl_dim_set
) > 0) {
1329 p
= print_dim(pwqp
->dim
, p
, 1, 0, NULL
);
1330 p
= isl_printer_print_str(p
, " -> ");
1332 p
= isl_printer_print_str(p
, "0");
1334 p
= isl_pwqp_print_isl_body(p
, pwqp
);
1335 p
= isl_printer_print_str(p
, " }");
1338 isl_printer_free(p
);
1342 void isl_pw_qpolynomial_print(__isl_keep isl_pw_qpolynomial
*pwqp
, FILE *out
,
1343 unsigned output_format
)
1350 p
= isl_printer_to_file(pwqp
->dim
->ctx
, out
);
1351 p
= isl_printer_set_output_format(p
, output_format
);
1352 p
= isl_printer_print_pw_qpolynomial(p
, pwqp
);
1354 isl_printer_free(p
);
1357 static __isl_give isl_printer
*isl_pwf_print_isl_body(
1358 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1362 for (i
= 0; i
< pwf
->n
; ++i
) {
1364 p
= isl_printer_print_str(p
, "; ");
1365 if (isl_dim_size(pwf
->p
[i
].set
->dim
, isl_dim_set
) > 0) {
1366 p
= print_dim(pwf
->p
[i
].set
->dim
, p
, 1, 0, NULL
);
1367 p
= isl_printer_print_str(p
, " -> ");
1369 p
= qpolynomial_fold_print(pwf
->p
[i
].fold
, p
);
1370 p
= print_disjuncts((isl_map
*)pwf
->p
[i
].set
, p
, 1, 0);
1376 static __isl_give isl_printer
*print_pw_qpolynomial_fold_isl(
1377 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1379 if (isl_dim_size(pwf
->dim
, isl_dim_param
) > 0) {
1380 p
= print_tuple(pwf
->dim
, p
, isl_dim_param
, 0, 0, NULL
);
1381 p
= isl_printer_print_str(p
, " -> ");
1383 p
= isl_printer_print_str(p
, "{ ");
1385 if (isl_dim_size(pwf
->dim
, isl_dim_set
) > 0) {
1386 p
= print_dim(pwf
->dim
, p
, 1, 0, NULL
);
1387 p
= isl_printer_print_str(p
, " -> ");
1389 p
= isl_printer_print_str(p
, "0");
1391 p
= isl_pwf_print_isl_body(p
, pwf
);
1392 p
= isl_printer_print_str(p
, " }");
1396 static __isl_give isl_printer
*print_affine_c(__isl_take isl_printer
*p
,
1397 __isl_keep isl_basic_set
*bset
, isl_int
*c
);
1399 static __isl_give isl_printer
*print_name_c(__isl_take isl_printer
*p
,
1400 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
)
1402 if (type
== isl_dim_div
) {
1403 p
= isl_printer_print_str(p
, "floord(");
1404 p
= print_affine_c(p
, bset
, bset
->div
[pos
] + 1);
1405 p
= isl_printer_print_str(p
, ", ");
1406 p
= isl_printer_print_isl_int(p
, bset
->div
[pos
][0]);
1407 p
= isl_printer_print_str(p
, ")");
1411 name
= isl_dim_get_name(bset
->dim
, type
, pos
);
1414 p
= isl_printer_print_str(p
, name
);
1419 static __isl_give isl_printer
*print_term_c(__isl_take isl_printer
*p
,
1420 __isl_keep isl_basic_set
*bset
, isl_int c
, unsigned pos
)
1422 enum isl_dim_type type
;
1423 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1426 return isl_printer_print_isl_int(p
, c
);
1428 if (isl_int_is_one(c
))
1430 else if (isl_int_is_negone(c
))
1431 p
= isl_printer_print_str(p
, "-");
1433 p
= isl_printer_print_isl_int(p
, c
);
1434 p
= isl_printer_print_str(p
, "*");
1436 type
= pos2type(bset
->dim
, &pos
);
1437 p
= print_name_c(p
, bset
, type
, pos
);
1441 static __isl_give isl_printer
*print_affine_c(__isl_take isl_printer
*p
,
1442 __isl_keep isl_basic_set
*bset
, isl_int
*c
)
1446 unsigned len
= 1 + isl_basic_set_total_dim(bset
);
1448 for (i
= 0, first
= 1; i
< len
; ++i
) {
1450 if (isl_int_is_zero(c
[i
]))
1453 if (isl_int_is_neg(c
[i
])) {
1455 isl_int_neg(c
[i
], c
[i
]);
1456 p
= isl_printer_print_str(p
, " - ");
1458 p
= isl_printer_print_str(p
, " + ");
1461 p
= print_term_c(p
, bset
, c
[i
], i
);
1463 isl_int_neg(c
[i
], c
[i
]);
1466 p
= isl_printer_print_str(p
, "0");
1470 static __isl_give isl_printer
*print_constraint_c(__isl_take isl_printer
*p
,
1471 __isl_keep isl_basic_set
*bset
, isl_int
*c
, const char *op
, int first
)
1474 p
= isl_printer_print_str(p
, " && ");
1476 p
= print_affine_c(p
, bset
, c
);
1477 p
= isl_printer_print_str(p
, " ");
1478 p
= isl_printer_print_str(p
, op
);
1479 p
= isl_printer_print_str(p
, " 0");
1483 static __isl_give isl_printer
*print_basic_set_c(__isl_take isl_printer
*p
,
1484 __isl_keep isl_basic_set
*bset
)
1488 for (i
= 0; i
< bset
->n_eq
; ++i
)
1489 p
= print_constraint_c(p
, bset
, bset
->eq
[i
], "==", !i
);
1490 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1491 p
= print_constraint_c(p
, bset
, bset
->ineq
[i
], ">=",
1496 static __isl_give isl_printer
*print_set_c(__isl_take isl_printer
*p
,
1497 __isl_keep isl_set
*set
)
1502 p
= isl_printer_print_str(p
, "0");
1504 for (i
= 0; i
< set
->n
; ++i
) {
1506 p
= isl_printer_print_str(p
, " || ");
1508 p
= isl_printer_print_str(p
, "(");
1509 p
= print_basic_set_c(p
, set
->p
[i
]);
1511 p
= isl_printer_print_str(p
, ")");
1516 static __isl_give isl_printer
*print_qpolynomial_c(__isl_take isl_printer
*p
,
1517 __isl_keep isl_qpolynomial
*qp
)
1522 isl_qpolynomial_get_den(qp
, &den
);
1523 if (!isl_int_is_one(den
)) {
1525 p
= isl_printer_print_str(p
, "(");
1526 qp
= isl_qpolynomial_copy(qp
);
1527 f
= isl_qpolynomial_rat_cst(isl_dim_copy(qp
->dim
),
1528 den
, qp
->dim
->ctx
->one
);
1529 qp
= isl_qpolynomial_mul(qp
, f
);
1532 p
= upoly_print(qp
->upoly
, qp
->dim
, qp
->div
, p
);
1533 if (!isl_int_is_one(den
)) {
1534 p
= isl_printer_print_str(p
, ")/");
1535 p
= isl_printer_print_isl_int(p
, den
);
1536 isl_qpolynomial_free(qp
);
1542 static __isl_give isl_printer
*print_pw_qpolynomial_c(
1543 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwpq
)
1547 if (pwpq
->n
== 1 && isl_set_fast_is_universe(pwpq
->p
[0].set
))
1548 return print_qpolynomial_c(p
, pwpq
->p
[0].qp
);
1550 for (i
= 0; i
< pwpq
->n
; ++i
) {
1551 p
= isl_printer_print_str(p
, "(");
1552 p
= print_set_c(p
, pwpq
->p
[i
].set
);
1553 p
= isl_printer_print_str(p
, ") ? (");
1554 p
= print_qpolynomial_c(p
, pwpq
->p
[i
].qp
);
1555 p
= isl_printer_print_str(p
, ") : ");
1558 p
= isl_printer_print_str(p
, "0");
1562 __isl_give isl_printer
*isl_printer_print_pw_qpolynomial(
1563 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial
*pwqp
)
1568 if (p
->output_format
== ISL_FORMAT_ISL
)
1569 return print_pw_qpolynomial_isl(p
, pwqp
);
1570 else if (p
->output_format
== ISL_FORMAT_C
)
1571 return print_pw_qpolynomial_c(p
, pwqp
);
1572 isl_assert(p
->ctx
, 0, goto error
);
1574 isl_printer_free(p
);
1578 static int print_pwqp_body(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1580 struct isl_union_print_data
*data
;
1581 data
= (struct isl_union_print_data
*)user
;
1584 data
->p
= isl_printer_print_str(data
->p
, "; ");
1587 data
->p
= isl_pwqp_print_isl_body(data
->p
, pwqp
);
1588 isl_pw_qpolynomial_free(pwqp
);
1593 static __isl_give isl_printer
*print_union_pw_qpolynomial_isl(
1594 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_qpolynomial
*upwqp
)
1596 struct isl_union_print_data data
= { p
, 1, 1 };
1598 dim
= isl_union_pw_qpolynomial_get_dim(upwqp
);
1599 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
1600 p
= print_tuple(dim
, p
, isl_dim_param
, 1, 0, NULL
);
1601 p
= isl_printer_print_str(p
, " -> ");
1604 p
= isl_printer_print_str(p
, "{ ");
1605 isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &print_pwqp_body
,
1608 p
= isl_printer_print_str(p
, " }");
1612 __isl_give isl_printer
*isl_printer_print_union_pw_qpolynomial(
1613 __isl_take isl_printer
*p
, __isl_keep isl_union_pw_qpolynomial
*upwqp
)
1618 if (p
->output_format
== ISL_FORMAT_ISL
)
1619 return print_union_pw_qpolynomial_isl(p
, upwqp
);
1620 isl_die(p
->ctx
, isl_error_invalid
,
1621 "invalid output format for isl_union_pw_qpolynomial",
1624 isl_printer_free(p
);
1628 static __isl_give isl_printer
*print_qpolynomial_fold_c(
1629 __isl_take isl_printer
*p
, __isl_keep isl_qpolynomial_fold
*fold
)
1633 for (i
= 0; i
< fold
->n
- 1; ++i
)
1634 if (fold
->type
== isl_fold_min
)
1635 p
= isl_printer_print_str(p
, "min(");
1636 else if (fold
->type
== isl_fold_max
)
1637 p
= isl_printer_print_str(p
, "max(");
1639 for (i
= 0; i
< fold
->n
; ++i
) {
1641 p
= isl_printer_print_str(p
, ", ");
1642 p
= print_qpolynomial_c(p
, fold
->qp
[i
]);
1644 p
= isl_printer_print_str(p
, ")");
1649 static __isl_give isl_printer
*print_pw_qpolynomial_fold_c(
1650 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1654 if (pwf
->n
== 1 && isl_set_fast_is_universe(pwf
->p
[0].set
))
1655 return print_qpolynomial_fold_c(p
, pwf
->p
[0].fold
);
1657 for (i
= 0; i
< pwf
->n
; ++i
) {
1658 p
= isl_printer_print_str(p
, "(");
1659 p
= print_set_c(p
, pwf
->p
[i
].set
);
1660 p
= isl_printer_print_str(p
, ") ? (");
1661 p
= print_qpolynomial_fold_c(p
, pwf
->p
[i
].fold
);
1662 p
= isl_printer_print_str(p
, ") : ");
1665 p
= isl_printer_print_str(p
, "0");
1669 __isl_give isl_printer
*isl_printer_print_pw_qpolynomial_fold(
1670 __isl_take isl_printer
*p
, __isl_keep isl_pw_qpolynomial_fold
*pwf
)
1675 if (p
->output_format
== ISL_FORMAT_ISL
)
1676 return print_pw_qpolynomial_fold_isl(p
, pwf
);
1677 else if (p
->output_format
== ISL_FORMAT_C
)
1678 return print_pw_qpolynomial_fold_c(p
, pwf
);
1679 isl_assert(p
->ctx
, 0, goto error
);
1681 isl_printer_free(p
);
1685 void isl_pw_qpolynomial_fold_print(__isl_keep isl_pw_qpolynomial_fold
*pwf
,
1686 FILE *out
, unsigned output_format
)
1693 p
= isl_printer_to_file(pwf
->dim
->ctx
, out
);
1694 p
= isl_printer_set_output_format(p
, output_format
);
1695 p
= isl_printer_print_pw_qpolynomial_fold(p
, pwf
);
1697 isl_printer_free(p
);
1700 static int print_pwf_body(__isl_take isl_pw_qpolynomial_fold
*pwf
, void *user
)
1702 struct isl_union_print_data
*data
;
1703 data
= (struct isl_union_print_data
*)user
;
1706 data
->p
= isl_printer_print_str(data
->p
, "; ");
1709 data
->p
= isl_pwf_print_isl_body(data
->p
, pwf
);
1710 isl_pw_qpolynomial_fold_free(pwf
);
1715 static __isl_give isl_printer
*print_union_pw_qpolynomial_fold_isl(
1716 __isl_take isl_printer
*p
,
1717 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1719 struct isl_union_print_data data
= { p
, 1, 1 };
1721 dim
= isl_union_pw_qpolynomial_fold_get_dim(upwf
);
1722 if (isl_dim_size(dim
, isl_dim_param
) > 0) {
1723 p
= print_tuple(dim
, p
, isl_dim_param
, 1, 0, NULL
);
1724 p
= isl_printer_print_str(p
, " -> ");
1727 p
= isl_printer_print_str(p
, "{ ");
1728 isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(upwf
,
1729 &print_pwf_body
, &data
);
1731 p
= isl_printer_print_str(p
, " }");
1735 __isl_give isl_printer
*isl_printer_print_union_pw_qpolynomial_fold(
1736 __isl_take isl_printer
*p
,
1737 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1742 if (p
->output_format
== ISL_FORMAT_ISL
)
1743 return print_union_pw_qpolynomial_fold_isl(p
, upwf
);
1744 isl_die(p
->ctx
, isl_error_invalid
,
1745 "invalid output format for isl_union_pw_qpolynomial_fold",
1748 isl_printer_free(p
);