isl_set_project_out: always update dimension, even for empty sets
[platform/upstream/isl.git] / isl_sample.c
index f4b2ea1..ed62753 100644 (file)
@@ -1,3 +1,12 @@
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
 #include "isl_sample.h"
 #include "isl_sample_piplib.h"
 #include "isl_vec.h"
@@ -271,8 +280,8 @@ static struct isl_mat *tab_equalities(struct isl_tab *tab)
        if (!tab)
                return NULL;
 
-       isl_assert(tab->mat->ctx, tab->bset, return NULL);
-       bset = tab->bset;
+       bset = isl_tab_peek_bset(tab);
+       isl_assert(tab->mat->ctx, bset, return NULL);
 
        n_eq = tab->n_var - tab->n_col + tab->n_dead;
        if (tab->empty || n_eq == 0)
@@ -370,14 +379,14 @@ static struct isl_mat *initial_basis(struct isl_tab *tab)
  *
  * The initial basis is the identity matrix.  If the range in some direction
  * contains more than one integer value, we perform basis reduction based
- * on the value of ctx->gbr
+ * on the value of ctx->opt->gbr
  *     - ISL_GBR_NEVER:        never perform basis reduction
  *     - ISL_GBR_ONCE:         only perform basis reduction the first
  *                             time such a range is encountered
  *     - ISL_GBR_ALWAYS:       always perform basis reduction when
  *                             such a range is encountered
  *
- * When ctx->gbr is set to ISL_GBR_ALWAYS, then we allow the basis
+ * When ctx->opt->gbr is set to ISL_GBR_ALWAYS, then we allow the basis
  * reduction computation to return early.  That is, as soon as it
  * finds a reasonable first direction.
  */ 
@@ -411,7 +420,7 @@ struct isl_vec *isl_tab_sample(struct isl_tab *tab)
 
        ctx = tab->mat->ctx;
        dim = tab->n_var;
-       gbr = ctx->gbr;
+       gbr = ctx->opt->gbr;
 
        if (tab->n_unbounded == tab->n_var) {
                sample = isl_tab_get_sample_value(tab);
@@ -462,17 +471,18 @@ struct isl_vec *isl_tab_sample(struct isl_tab *tab)
                                goto error;
                        if (!empty && isl_tab_sample_is_integer(tab))
                                break;
-                       if (!empty && !reduced && ctx->gbr != ISL_GBR_NEVER &&
+                       if (!empty && !reduced &&
+                           ctx->opt->gbr != ISL_GBR_NEVER &&
                            isl_int_lt(min->el[level], max->el[level])) {
                                unsigned gbr_only_first;
-                               if (ctx->gbr == ISL_GBR_ONCE)
-                                       ctx->gbr = ISL_GBR_NEVER;
+                               if (ctx->opt->gbr == ISL_GBR_ONCE)
+                                       ctx->opt->gbr = ISL_GBR_NEVER;
                                tab->n_zero = level;
-                               gbr_only_first = ctx->gbr_only_first;
-                               ctx->gbr_only_first =
-                                       ctx->gbr == ISL_GBR_ALWAYS;
+                               gbr_only_first = ctx->opt->gbr_only_first;
+                               ctx->opt->gbr_only_first =
+                                       ctx->opt->gbr == ISL_GBR_ALWAYS;
                                tab = isl_tab_compute_reduced_basis(tab);
-                               ctx->gbr_only_first = gbr_only_first;
+                               ctx->opt->gbr_only_first = gbr_only_first;
                                if (!tab || !tab->basis)
                                        goto error;
                                reduced = 1;
@@ -487,7 +497,8 @@ struct isl_vec *isl_tab_sample(struct isl_tab *tab)
                        level--;
                        init = 0;
                        if (level >= 0)
-                               isl_tab_rollback(tab, snap[level]);
+                               if (isl_tab_rollback(tab, snap[level]) < 0)
+                                       goto error;
                        continue;
                }
                isl_int_neg(tab->basis->row[1 + level][0], min->el[level]);
@@ -515,13 +526,13 @@ struct isl_vec *isl_tab_sample(struct isl_tab *tab)
        } else
                sample = isl_vec_alloc(ctx, 0);
 
-       ctx->gbr = gbr;
+       ctx->opt->gbr = gbr;
        isl_vec_free(min);
        isl_vec_free(max);
        free(snap);
        return sample;
 error:
-       ctx->gbr = gbr;
+       ctx->opt->gbr = gbr;
        isl_vec_free(min);
        isl_vec_free(max);
        free(snap);
@@ -559,13 +570,21 @@ static struct isl_vec *sample_bounded(struct isl_basic_set *bset)
        ctx = bset->ctx;
 
        tab = isl_tab_from_basic_set(bset);
+       if (tab && tab->empty) {
+               isl_tab_free(tab);
+               ISL_F_SET(bset, ISL_BASIC_SET_EMPTY);
+               sample = isl_vec_alloc(bset->ctx, 0);
+               isl_basic_set_free(bset);
+               return sample;
+       }
+
+       if (isl_tab_track_bset(tab, isl_basic_set_copy(bset)) < 0)
+               goto error;
        if (!ISL_F_ISSET(bset, ISL_BASIC_SET_NO_IMPLICIT))
                tab = isl_tab_detect_implicit_equalities(tab);
        if (!tab)
                goto error;
 
-       tab->bset = isl_basic_set_copy(bset);
-
        sample = isl_tab_sample(tab);
        if (!sample)
                goto error;
@@ -910,6 +929,133 @@ error:
        return NULL;
 }
 
+static void vec_sum_of_neg(struct isl_vec *v, isl_int *s)
+{
+       int i;
+
+       isl_int_set_si(*s, 0);
+
+       for (i = 0; i < v->size; ++i)
+               if (isl_int_is_neg(v->el[i]))
+                       isl_int_add(*s, *s, v->el[i]);
+}
+
+/* Given a tableau "tab", a tableau "tab_cone" that corresponds
+ * to the recession cone and the inverse of a new basis U = inv(B),
+ * with the unbounded directions in B last,
+ * add constraints to "tab" that ensure any rational value
+ * in the unbounded directions can be rounded up to an integer value.
+ *
+ * The new basis is given by x' = B x, i.e., x = U x'.
+ * For any rational value of the last tab->n_unbounded coordinates
+ * in the update tableau, the value that is obtained by rounding
+ * up this value should be contained in the original tableau.
+ * For any constraint "a x + c >= 0", we therefore need to add
+ * a constraint "a x + c + s >= 0", with s the sum of all negative
+ * entries in the last elements of "a U".
+ *
+ * Since we are not interested in the first entries of any of the "a U",
+ * we first drop the columns of U that correpond to bounded directions.
+ */
+static int tab_shift_cone(struct isl_tab *tab,
+       struct isl_tab *tab_cone, struct isl_mat *U)
+{
+       int i;
+       isl_int v;
+       struct isl_basic_set *bset = NULL;
+
+       if (tab && tab->n_unbounded == 0) {
+               isl_mat_free(U);
+               return 0;
+       }
+       isl_int_init(v);
+       if (!tab || !tab_cone || !U)
+               goto error;
+       bset = isl_tab_peek_bset(tab_cone);
+       U = isl_mat_drop_cols(U, 0, tab->n_var - tab->n_unbounded);
+       for (i = 0; i < bset->n_ineq; ++i) {
+               int ok;
+               struct isl_vec *row = NULL;
+               if (isl_tab_is_equality(tab_cone, tab_cone->n_eq + i))
+                       continue;
+               row = isl_vec_alloc(bset->ctx, tab_cone->n_var);
+               if (!row)
+                       goto error;
+               isl_seq_cpy(row->el, bset->ineq[i] + 1, tab_cone->n_var);
+               row = isl_vec_mat_product(row, isl_mat_copy(U));
+               if (!row)
+                       goto error;
+               vec_sum_of_neg(row, &v);
+               isl_vec_free(row);
+               if (isl_int_is_zero(v))
+                       continue;
+               tab = isl_tab_extend(tab, 1);
+               isl_int_add(bset->ineq[i][0], bset->ineq[i][0], v);
+               ok = isl_tab_add_ineq(tab, bset->ineq[i]) >= 0;
+               isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], v);
+               if (!ok)
+                       goto error;
+       }
+
+       isl_mat_free(U);
+       isl_int_clear(v);
+       return 0;
+error:
+       isl_mat_free(U);
+       isl_int_clear(v);
+       return -1;
+}
+
+/* Compute and return an initial basis for the possibly
+ * unbounded tableau "tab".  "tab_cone" is a tableau
+ * for the corresponding recession cone.
+ * Additionally, add constraints to "tab" that ensure
+ * that any rational value for the unbounded directions
+ * can be rounded up to an integer value.
+ *
+ * If the tableau is bounded, i.e., if the recession cone
+ * is zero-dimensional, then we just use inital_basis.
+ * Otherwise, we construct a basis whose first directions
+ * correspond to equalities, followed by bounded directions,
+ * i.e., equalities in the recession cone.
+ * The remaining directions are then unbounded.
+ */
+int isl_tab_set_initial_basis_with_cone(struct isl_tab *tab,
+       struct isl_tab *tab_cone)
+{
+       struct isl_mat *eq;
+       struct isl_mat *cone_eq;
+       struct isl_mat *U, *Q;
+
+       if (!tab || !tab_cone)
+               return -1;
+
+       if (tab_cone->n_col == tab_cone->n_dead) {
+               tab->basis = initial_basis(tab);
+               return tab->basis ? 0 : -1;
+       }
+
+       eq = tab_equalities(tab);
+       if (!eq)
+               return -1;
+       tab->n_zero = eq->n_row;
+       cone_eq = tab_equalities(tab_cone);
+       eq = isl_mat_concat(eq, cone_eq);
+       if (!eq)
+               return -1;
+       tab->n_unbounded = tab->n_var - (eq->n_row - tab->n_zero);
+       eq = isl_mat_left_hermite(eq, 0, &U, &Q);
+       if (!eq)
+               return -1;
+       isl_mat_free(eq);
+       tab->basis = isl_mat_lin_to_aff(Q);
+       if (tab_shift_cone(tab, tab_cone, U) < 0)
+               return -1;
+       if (!tab->basis)
+               return -1;
+       return 0;
+}
+
 /* Compute and return a sample point in bset using generalized basis
  * reduction.  We first check if the input set has a non-trivial
  * recession cone.  If so, we perform some extra preprocessing in
@@ -989,7 +1135,7 @@ static struct isl_vec *basic_set_sample(struct isl_basic_set *bset, int bounded)
        if (dim == 1)
                return interval_sample(bset);
 
-       switch (bset->ctx->ilp_solver) {
+       switch (bset->ctx->opt->ilp_solver) {
        case ISL_ILP_PIP:
                return pip_sample(bset);
        case ISL_ILP_GBR:
@@ -1039,7 +1185,7 @@ __isl_give isl_basic_set *isl_basic_set_from_vec(__isl_take isl_vec *vec)
                isl_int_neg(bset->eq[k][0], vec->el[1 + i]);
                isl_int_set(bset->eq[k][1 + i], vec->el[0]);
        }
-       isl_vec_free(vec);
+       bset->sample = vec;
 
        return bset;
 error: