isl_gmp.c: remove spurious include
[isl.git] / isl_lp.c
blob1d5ae6ef8624a5c62bf91a93ba7d7a39c9b6d18f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
12 #include <isl/lp.h>
13 #include <isl_seq.h>
14 #include "isl_tab.h"
15 #include <isl_options_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl_mat_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
22 enum isl_lp_result isl_tab_solve_lp(struct isl_basic_map *bmap, int maximize,
23 isl_int *f, isl_int denom, isl_int *opt,
24 isl_int *opt_denom,
25 struct isl_vec **sol)
27 struct isl_tab *tab;
28 enum isl_lp_result res;
29 unsigned dim = isl_basic_map_total_dim(bmap);
31 if (maximize)
32 isl_seq_neg(f, f, 1 + dim);
34 bmap = isl_basic_map_gauss(bmap, NULL);
35 tab = isl_tab_from_basic_map(bmap, 0);
36 res = isl_tab_min(tab, f, denom, opt, opt_denom, 0);
37 if (res == isl_lp_ok && sol) {
38 *sol = isl_tab_get_sample_value(tab);
39 if (!*sol)
40 res = isl_lp_error;
42 isl_tab_free(tab);
44 if (maximize)
45 isl_seq_neg(f, f, 1 + dim);
46 if (maximize && opt)
47 isl_int_neg(*opt, *opt);
49 return res;
52 /* Given a basic map "bmap" and an affine combination of the variables "f"
53 * with denominator "denom", set *opt / *opt_denom to the minimal
54 * (or maximal if "maximize" is true) value attained by f/d over "bmap",
55 * assuming the basic map is not empty and the expression cannot attain
56 * arbitrarily small (or large) values.
57 * If opt_denom is NULL, then *opt is rounded up (or down)
58 * to the nearest integer.
59 * The return value reflects the nature of the result (empty, unbounded,
60 * minmimal or maximal value returned in *opt).
62 enum isl_lp_result isl_basic_map_solve_lp(struct isl_basic_map *bmap, int max,
63 isl_int *f, isl_int d, isl_int *opt,
64 isl_int *opt_denom,
65 struct isl_vec **sol)
67 if (sol)
68 *sol = NULL;
70 if (!bmap)
71 return isl_lp_error;
73 return isl_tab_solve_lp(bmap, max, f, d, opt, opt_denom, sol);
76 enum isl_lp_result isl_basic_set_solve_lp(struct isl_basic_set *bset, int max,
77 isl_int *f, isl_int d, isl_int *opt,
78 isl_int *opt_denom,
79 struct isl_vec **sol)
81 return isl_basic_map_solve_lp((struct isl_basic_map *)bset, max,
82 f, d, opt, opt_denom, sol);
85 enum isl_lp_result isl_map_solve_lp(__isl_keep isl_map *map, int max,
86 isl_int *f, isl_int d, isl_int *opt,
87 isl_int *opt_denom,
88 struct isl_vec **sol)
90 int i;
91 isl_int o;
92 isl_int t;
93 isl_int opt_i;
94 isl_int opt_denom_i;
95 enum isl_lp_result res;
96 int max_div;
97 isl_vec *v = NULL;
99 if (!map)
100 return isl_lp_error;
101 if (map->n == 0)
102 return isl_lp_empty;
104 max_div = 0;
105 for (i = 0; i < map->n; ++i)
106 if (map->p[i]->n_div > max_div)
107 max_div = map->p[i]->n_div;
108 if (max_div > 0) {
109 unsigned total = isl_space_dim(map->dim, isl_dim_all);
110 v = isl_vec_alloc(map->ctx, 1 + total + max_div);
111 if (!v)
112 return isl_lp_error;
113 isl_seq_cpy(v->el, f, 1 + total);
114 isl_seq_clr(v->el + 1 + total, max_div);
115 f = v->el;
118 if (!opt && map->n > 1 && sol) {
119 isl_int_init(o);
120 opt = &o;
122 if (map->n > 0)
123 isl_int_init(opt_i);
124 if (map->n > 0 && opt_denom) {
125 isl_int_init(opt_denom_i);
126 isl_int_init(t);
129 res = isl_basic_map_solve_lp(map->p[0], max, f, d,
130 opt, opt_denom, sol);
131 if (res == isl_lp_error || res == isl_lp_unbounded)
132 goto done;
134 if (sol)
135 *sol = NULL;
137 for (i = 1; i < map->n; ++i) {
138 isl_vec *sol_i = NULL;
139 enum isl_lp_result res_i;
140 int better;
142 res_i = isl_basic_map_solve_lp(map->p[i], max, f, d,
143 &opt_i,
144 opt_denom ? &opt_denom_i : NULL,
145 sol ? &sol_i : NULL);
146 if (res_i == isl_lp_error || res_i == isl_lp_unbounded) {
147 res = res_i;
148 goto done;
150 if (res_i == isl_lp_empty)
151 continue;
152 if (res == isl_lp_empty) {
153 better = 1;
154 } else if (!opt_denom) {
155 if (max)
156 better = isl_int_gt(opt_i, *opt);
157 else
158 better = isl_int_lt(opt_i, *opt);
159 } else {
160 isl_int_mul(t, opt_i, *opt_denom);
161 isl_int_submul(t, *opt, opt_denom_i);
162 if (max)
163 better = isl_int_is_pos(t);
164 else
165 better = isl_int_is_neg(t);
167 if (better) {
168 res = res_i;
169 if (opt)
170 isl_int_set(*opt, opt_i);
171 if (opt_denom)
172 isl_int_set(*opt_denom, opt_denom_i);
173 if (sol) {
174 isl_vec_free(*sol);
175 *sol = sol_i;
177 } else
178 isl_vec_free(sol_i);
181 done:
182 isl_vec_free(v);
183 if (map->n > 0 && opt_denom) {
184 isl_int_clear(opt_denom_i);
185 isl_int_clear(t);
187 if (map->n > 0)
188 isl_int_clear(opt_i);
189 if (opt == &o)
190 isl_int_clear(o);
191 return res;
194 enum isl_lp_result isl_set_solve_lp(__isl_keep isl_set *set, int max,
195 isl_int *f, isl_int d, isl_int *opt,
196 isl_int *opt_denom,
197 struct isl_vec **sol)
199 return isl_map_solve_lp((struct isl_map *)set, max,
200 f, d, opt, opt_denom, sol);
203 /* Return the optimal (rational) value of "obj" over "bset", assuming
204 * that "obj" and "bset" have aligned parameters and divs.
205 * If "max" is set, then the maximal value is computed.
206 * Otherwise, the minimal value is computed.
208 * Return infinity or negative infinity if the optimal value is unbounded and
209 * NaN if "bset" is empty.
211 * Call isl_basic_set_solve_lp and translate the results.
213 static __isl_give isl_val *basic_set_opt_lp(
214 __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
216 isl_ctx *ctx;
217 isl_val *res;
218 enum isl_lp_result lp_res;
220 if (!bset || !obj)
221 return NULL;
223 ctx = isl_aff_get_ctx(obj);
224 res = isl_val_alloc(ctx);
225 if (!res)
226 return NULL;
227 lp_res = isl_basic_set_solve_lp(bset, max, obj->v->el + 1,
228 obj->v->el[0], &res->n, &res->d, NULL);
229 if (lp_res == isl_lp_ok)
230 return isl_val_normalize(res);
231 isl_val_free(res);
232 if (lp_res == isl_lp_error)
233 return NULL;
234 if (lp_res == isl_lp_empty)
235 return isl_val_nan(ctx);
236 if (max)
237 return isl_val_infty(ctx);
238 else
239 return isl_val_neginfty(ctx);
242 /* Return the optimal (rational) value of "obj" over "bset", assuming
243 * that "obj" and "bset" have aligned parameters.
244 * If "max" is set, then the maximal value is computed.
245 * Otherwise, the minimal value is computed.
247 * Return infinity or negative infinity if the optimal value is unbounded and
248 * NaN if "bset" is empty.
250 * Align the divs of "bset" and "obj" and call basic_set_opt_lp.
252 static __isl_give isl_val *isl_basic_set_opt_lp_val_aligned(
253 __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
255 int *exp1 = NULL;
256 int *exp2 = NULL;
257 isl_ctx *ctx;
258 isl_mat *bset_div = NULL;
259 isl_mat *div = NULL;
260 isl_val *res;
261 int bset_n_div, obj_n_div;
263 if (!bset || !obj)
264 return NULL;
266 ctx = isl_aff_get_ctx(obj);
267 if (!isl_space_is_equal(bset->dim, obj->ls->dim))
268 isl_die(ctx, isl_error_invalid,
269 "spaces don't match", return NULL);
271 bset_n_div = isl_basic_set_dim(bset, isl_dim_div);
272 obj_n_div = isl_aff_dim(obj, isl_dim_div);
273 if (bset_n_div == 0 && obj_n_div == 0)
274 return basic_set_opt_lp(bset, max, obj);
276 bset = isl_basic_set_copy(bset);
277 obj = isl_aff_copy(obj);
279 bset_div = isl_basic_set_get_divs(bset);
280 exp1 = isl_alloc_array(ctx, int, bset_n_div);
281 exp2 = isl_alloc_array(ctx, int, obj_n_div);
282 if (!bset_div || (bset_n_div && !exp1) || (obj_n_div && !exp2))
283 goto error;
285 div = isl_merge_divs(bset_div, obj->ls->div, exp1, exp2);
287 bset = isl_basic_set_expand_divs(bset, isl_mat_copy(div), exp1);
288 obj = isl_aff_expand_divs(obj, isl_mat_copy(div), exp2);
290 res = basic_set_opt_lp(bset, max, obj);
292 isl_mat_free(bset_div);
293 isl_mat_free(div);
294 free(exp1);
295 free(exp2);
296 isl_basic_set_free(bset);
297 isl_aff_free(obj);
299 return res;
300 error:
301 isl_mat_free(div);
302 isl_mat_free(bset_div);
303 free(exp1);
304 free(exp2);
305 isl_basic_set_free(bset);
306 isl_aff_free(obj);
307 return NULL;
310 /* Return the optimal (rational) value of "obj" over "bset".
311 * If "max" is set, then the maximal value is computed.
312 * Otherwise, the minimal value is computed.
314 * Return infinity or negative infinity if the optimal value is unbounded and
315 * NaN if "bset" is empty.
317 static __isl_give isl_val *isl_basic_set_opt_lp_val(
318 __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
320 isl_val *res;
322 if (!bset || !obj)
323 return NULL;
325 if (isl_space_match(bset->dim, isl_dim_param,
326 obj->ls->dim, isl_dim_param))
327 return isl_basic_set_opt_lp_val_aligned(bset, max, obj);
329 bset = isl_basic_set_copy(bset);
330 obj = isl_aff_copy(obj);
331 bset = isl_basic_set_align_params(bset, isl_aff_get_domain_space(obj));
332 obj = isl_aff_align_params(obj, isl_basic_set_get_space(bset));
334 res = isl_basic_set_opt_lp_val_aligned(bset, max, obj);
336 isl_basic_set_free(bset);
337 isl_aff_free(obj);
339 return res;
342 /* Return the minimal (rational) value of "obj" over "bset".
344 * Return negative infinity if the minimal value is unbounded and
345 * NaN if "bset" is empty.
347 __isl_give isl_val *isl_basic_set_min_lp_val(__isl_keep isl_basic_set *bset,
348 __isl_keep isl_aff *obj)
350 return isl_basic_set_opt_lp_val(bset, 0, obj);
353 /* Return the maximal (rational) value of "obj" over "bset".
355 * Return infinity if the maximal value is unbounded and
356 * NaN if "bset" is empty.
358 __isl_give isl_val *isl_basic_set_max_lp_val(__isl_keep isl_basic_set *bset,
359 __isl_keep isl_aff *obj)
361 return isl_basic_set_opt_lp_val(bset, 1, obj);