2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU GPLv2 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 #include <isl/val_gmp.h>
12 #include <isl/space.h>
15 #include <isl/constraint.h>
16 #include "isl_set_polylib.h"
17 #include "isl_map_polylib.h"
19 struct isl_basic_set
*isl_basic_set_new_from_polylib(Polyhedron
*P
,
20 struct isl_space
*dim
)
26 ctx
= isl_space_get_ctx(dim
);
27 isl_assert(ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, return NULL
);
29 return (struct isl_basic_set
*)
30 isl_basic_map_new_from_polylib(P
, dim
);
33 /* Return the number of equality constraints in the polyhedron description "P".
34 * The equality constraints have a zero in the first column.
35 * They also appear before the inequality constraints, but this code
36 * does not rely on this order.
38 static int polyhedron_n_eq(Polyhedron
*P
)
42 for (i
= 0; i
< P
->NbConstraints
; ++i
)
43 if (value_zero_p(P
->Constraint
[i
][0]))
49 /* Set the row "row" of "dst" to the values in array "src".
51 static __isl_give isl_mat
*set_row(__isl_take isl_mat
*dst
, int row
,
57 ctx
= isl_mat_get_ctx(dst
);
58 n
= isl_mat_cols(dst
);
59 for (i
= 0; i
< n
; ++i
) {
62 v
= isl_val_int_from_gmp(ctx
, src
[i
]);
63 dst
= isl_mat_set_element_val(dst
, row
, i
, v
);
69 /* Extract the "n_eq" equality constraints from "P", dropping the column
70 * that identifies equality constraints.
72 static __isl_give isl_mat
*extract_equalities(isl_ctx
*ctx
, Polyhedron
*P
,
78 eq
= isl_mat_alloc(ctx
, n_eq
, P
->Dimension
+ 1);
79 for (i
= 0, j
= 0; i
< P
->NbConstraints
; ++i
) {
80 if (!value_zero_p(P
->Constraint
[i
][0]))
82 eq
= set_row(eq
, j
++, P
->Constraint
[i
] + 1);
88 /* Extract the "n_ineq" inequality constraints from "P", dropping the column
89 * that identifies equality constraints.
91 static __isl_give isl_mat
*extract_inequalities(isl_ctx
*ctx
, Polyhedron
*P
,
97 ineq
= isl_mat_alloc(ctx
, n_ineq
, P
->Dimension
+ 1);
98 for (i
= 0, j
= 0; i
< P
->NbConstraints
; ++i
) {
99 if (value_zero_p(P
->Constraint
[i
][0]))
101 ineq
= set_row(ineq
, j
++, P
->Constraint
[i
] + 1);
107 __isl_give isl_basic_map
*isl_basic_map_new_from_polylib(Polyhedron
*P
,
108 __isl_take isl_space
*space
)
112 unsigned n_eq
, n_ineq
;
117 ctx
= isl_space_get_ctx(space
);
118 isl_assert(ctx
, P
, goto error
);
119 isl_assert(ctx
, P
->Dimension
>= isl_space_dim(space
, isl_dim_all
),
122 n_eq
= polyhedron_n_eq(P
);
123 n_ineq
= P
->NbConstraints
- n_eq
;
124 eq
= extract_equalities(ctx
, P
, n_eq
);
125 ineq
= extract_inequalities(ctx
, P
, n_ineq
);
127 return isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
128 isl_dim_in
, isl_dim_out
, isl_dim_div
, isl_dim_param
, isl_dim_cst
);
130 isl_space_free(space
);
134 struct isl_set
*isl_set_new_from_polylib(Polyhedron
*D
, struct isl_space
*dim
)
137 struct isl_set
*set
= NULL
;
142 ctx
= isl_space_get_ctx(dim
);
143 isl_assert(ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, return NULL
);
145 set
= isl_set_empty(isl_space_copy(dim
));
149 for (P
= D
; P
; P
= P
->next
)
150 set
= isl_set_union_disjoint(set
,
151 isl_set_from_basic_set(
152 isl_basic_set_new_from_polylib(P
, isl_space_copy(dim
))));
160 /* Store the elements of "c" in the rows of "M" starting at "pos",
161 * adding an extra initial column identifying equality constraints.
162 * In particular, add 0 if "eq" is set and 1 otherwise.
164 static Matrix
*add_constraints(Matrix
*M
, __isl_keep isl_mat
*c
, int eq
,
173 for (i
= 0; i
< n
; ++i
) {
175 value_set_si(M
->p
[pos
+ i
][0], 0);
177 value_set_si(M
->p
[pos
+ i
][0], 1);
179 for (j
= 0; 1 + j
< M
->NbColumns
; ++j
) {
181 v
= isl_mat_get_element_val(c
, i
, j
);
182 isl_val_get_num_gmp(v
, M
->p
[pos
+ i
][1 + j
]);
195 /* Return the constraints of "bmap" as a PolyLib Matrix.
197 static Matrix
*isl_basic_map_to_polylib_constraints(
198 __isl_keep isl_basic_map
*bmap
)
208 ineq
= isl_basic_map_inequalities_matrix(bmap
,
209 isl_dim_in
, isl_dim_out
, isl_dim_div
, isl_dim_param
, isl_dim_cst
);
210 eq
= isl_basic_map_equalities_matrix(bmap
,
211 isl_dim_in
, isl_dim_out
, isl_dim_div
, isl_dim_param
, isl_dim_cst
);
212 n_eq
= isl_mat_rows(eq
);
213 n_ineq
= isl_mat_rows(ineq
);
214 n_col
= isl_mat_cols(eq
);
217 if (n_eq
>= 0 && n_ineq
>= 0 && n_col
>= 0) {
218 M
= Matrix_Alloc(n_eq
+ n_ineq
, 1 + n_col
);
219 M
= add_constraints(M
, eq
, 1, 0);
220 M
= add_constraints(M
, ineq
, 0, n_eq
);
229 /* Return the constraints of "bset" as a PolyLib Matrix.
231 Matrix
*isl_basic_set_to_polylib_constraints(__isl_keep isl_basic_set
*bset
)
233 return isl_basic_map_to_polylib_constraints((isl_basic_map
*) bset
);
236 Polyhedron
*isl_basic_map_to_polylib(__isl_keep isl_basic_map
*bmap
)
245 if (isl_basic_map_is_rational(bmap
))
246 max_rays
= POL_NO_DUAL
;
248 max_rays
= POL_NO_DUAL
| POL_INTEGER
;
250 M
= isl_basic_map_to_polylib_constraints(bmap
);
254 P
= Constraints2Polyhedron(M
, max_rays
);
260 static isl_stat
add_basic_map(__isl_take isl_basic_map
*bmap
, void *user
)
262 Polyhedron
***next
= user
;
264 **next
= isl_basic_map_to_polylib(bmap
);
265 *next
= &(**next
)->next
;
267 isl_basic_map_free(bmap
);
271 Polyhedron
*isl_map_to_polylib(struct isl_map
*map
)
273 Polyhedron
*R
= NULL
;
274 Polyhedron
**next
= &R
;
279 if (isl_map_foreach_basic_map(map
, &add_basic_map
, &next
) < 0)
282 return R
? R
: Empty_Polyhedron(isl_map_dim(map
, isl_dim_all
));
288 Polyhedron
*isl_basic_set_to_polylib(__isl_keep isl_basic_set
*bset
)
290 return isl_basic_map_to_polylib((struct isl_basic_map
*)bset
);
293 Polyhedron
*isl_set_to_polylib(__isl_keep isl_set
*set
)
295 return isl_map_to_polylib((struct isl_map
*)set
);