4 #include <isl/val_gmp.h>
8 #include <isl_set_polylib.h>
9 #include <barvinok/polylib.h>
12 static __isl_give isl_mat
*extract_equalities(isl_ctx
*ctx
, Matrix
*M
)
20 for (i
= 0; i
< M
->NbRows
; ++i
)
21 if (value_zero_p(M
->p
[i
][0]))
24 eq
= isl_mat_alloc(ctx
, n
, M
->NbColumns
- 1);
25 for (i
= 0; i
< M
->NbRows
; ++i
) {
26 if (!value_zero_p(M
->p
[i
][0]))
28 for (j
= 0; j
< M
->NbColumns
- 1; ++j
) {
29 v
= isl_val_int_from_gmp(ctx
, M
->p
[i
][1 + j
]);
30 eq
= isl_mat_set_element_val(eq
, i
, j
, v
);
37 static __isl_give isl_mat
*extract_inequalities(isl_ctx
*ctx
, Matrix
*M
)
45 for (i
= 0; i
< M
->NbRows
; ++i
)
46 if (!value_zero_p(M
->p
[i
][0]))
49 ineq
= isl_mat_alloc(ctx
, n
, M
->NbColumns
- 1);
50 for (i
= 0; i
< M
->NbRows
; ++i
) {
51 if (value_zero_p(M
->p
[i
][0]))
53 for (j
= 0; j
< M
->NbColumns
- 1; ++j
) {
54 v
= isl_val_int_from_gmp(ctx
, M
->p
[i
][1 + j
]);
55 ineq
= isl_mat_set_element_val(ineq
, i
, j
, v
);
62 enum order_sign
isl_polyhedron_affine_sign(Polyhedron
*D
, Matrix
*T
,
63 struct barvinok_options
*options
)
66 isl_ctx
*ctx
= isl_ctx_alloc();
71 isl_val
*min
, *max
= NULL
;
73 enum order_sign sign
= order_undefined
;
75 assert(D
->Dimension
== T
->NbColumns
- 1);
77 space
= isl_space_set_alloc(ctx
, 0, D
->Dimension
);
78 ls
= isl_local_space_from_space(isl_space_copy(space
));
79 bset
= isl_basic_set_new_from_polylib(D
, space
);
80 aff
= isl_aff_zero_on_domain(ls
);
81 for (i
= 0; i
< D
->Dimension
; ++i
) {
82 v
= isl_val_int_from_gmp(ctx
, T
->p
[0][i
]);
83 aff
= isl_aff_set_coefficient_val(aff
, isl_dim_in
, i
, v
);
85 v
= isl_val_int_from_gmp(ctx
, T
->p
[0][D
->Dimension
]);
86 aff
= isl_aff_set_constant_val(aff
, v
);
87 v
= isl_val_int_from_gmp(ctx
, T
->p
[1][D
->Dimension
]);
88 aff
= isl_aff_scale_down_val(aff
, v
);
90 min
= isl_basic_set_min_lp_val(bset
, aff
);
91 min
= isl_val_ceil(min
);
94 if (isl_val_is_nan(min
))
95 sign
= order_undefined
;
96 else if (isl_val_is_pos(min
))
99 max
= isl_basic_set_max_lp_val(bset
, aff
);
100 max
= isl_val_floor(max
);
103 if (isl_val_is_neg(max
))
105 else if (isl_val_is_zero(min
) && isl_val_is_zero(max
))
107 else if (isl_val_is_zero(min
))
109 else if (isl_val_is_zero(max
))
112 sign
= order_unknown
;
115 isl_basic_set_free(bset
);
124 static enum lp_result
isl_lp_result2lp_result(enum isl_lp_result res
)
129 case isl_lp_unbounded
:
138 enum lp_result
isl_constraints_opt(Matrix
*C
, Value
*obj
, Value denom
,
139 enum lp_dir dir
, Value
*opt
)
142 isl_ctx
*ctx
= isl_ctx_alloc();
149 enum isl_lp_result res
;
150 int max
= dir
== lp_max
;
152 eq
= extract_equalities(ctx
, C
);
153 ineq
= extract_inequalities(ctx
, C
);
154 space
= isl_space_set_alloc(ctx
, 0, C
->NbColumns
- 2);
155 ls
= isl_local_space_from_space(isl_space_copy(space
));
156 bset
= isl_basic_set_from_constraint_matrices(space
, eq
, ineq
,
157 isl_dim_set
, isl_dim_div
, isl_dim_param
, isl_dim_cst
);
158 aff
= isl_aff_zero_on_domain(ls
);
159 for (i
= 0; i
< C
->NbColumns
- 2; ++i
) {
160 v
= isl_val_int_from_gmp(ctx
, obj
[i
]);
161 aff
= isl_aff_set_coefficient_val(aff
, isl_dim_in
, i
, v
);
163 v
= isl_val_int_from_gmp(ctx
, obj
[C
->NbColumns
- 2]);
164 aff
= isl_aff_set_constant_val(aff
, v
);
165 v
= isl_val_int_from_gmp(ctx
, denom
);
166 aff
= isl_aff_scale_down_val(aff
, v
);
169 v
= isl_val_floor(isl_basic_set_max_lp_val(bset
, aff
));
171 v
= isl_val_ceil(isl_basic_set_min_lp_val(bset
, aff
));
174 else if (isl_val_is_nan(v
))
176 else if (!isl_val_is_rat(v
))
177 res
= isl_lp_unbounded
;
180 isl_val_get_num_gmp(v
, *opt
);
185 isl_basic_set_free(bset
);
188 return isl_lp_result2lp_result(res
);