isl_qpolynomial_from_constraint: reimplement using isl_aff
[platform/upstream/isl.git] / isl_polynomial.c
index db081ee..2b7f1d6 100644 (file)
@@ -22,6 +22,8 @@
 #include <isl_mat_private.h>
 #include <isl_range.h>
 #include <isl_local_space_private.h>
+#include <isl_aff_private.h>
+#include <isl_config.h>
 
 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
 {
@@ -325,7 +327,7 @@ __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
        isl_assert(ctx, size >= 0, return NULL);
        rec = isl_calloc(ctx, struct isl_upoly_rec,
                        sizeof(struct isl_upoly_rec) +
-                       (size - 1) * sizeof(struct isl_upoly *));
+                       size * sizeof(struct isl_upoly *));
        if (!rec)
                return NULL;
 
@@ -473,8 +475,6 @@ error:
 
 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
 {
-       struct isl_upoly *dup;
-
        if (!up)
                return NULL;
 
@@ -798,7 +798,7 @@ __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
 {
        struct isl_upoly_rec *rec1;
        struct isl_upoly_rec *rec2;
-       struct isl_upoly_rec *res;
+       struct isl_upoly_rec *res = NULL;
        int i, j;
        int size;
 
@@ -1035,7 +1035,6 @@ void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
 {
        int i;
-       struct isl_upoly *up;
        struct isl_upoly_rec *rec;
        struct isl_upoly_cst *cst;
 
@@ -1419,6 +1418,12 @@ error:
        return NULL;
 }
 
+__isl_give isl_qpolynomial *isl_qpolynomial_scale(
+       __isl_take isl_qpolynomial *qp, isl_int v)
+{
+       return isl_qpolynomial_mul_isl_int(qp, v);
+}
+
 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
        __isl_take isl_qpolynomial *qp2)
 {
@@ -1467,26 +1472,36 @@ error:
 
 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
 {
+       if (!dim)
+               return NULL;
        return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
 }
 
 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
 {
+       if (!dim)
+               return NULL;
        return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
 }
 
 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
 {
+       if (!dim)
+               return NULL;
        return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
 }
 
 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
 {
+       if (!dim)
+               return NULL;
        return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
 }
 
 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
 {
+       if (!dim)
+               return NULL;
        return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
 }
 
@@ -1496,6 +1511,9 @@ __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
        struct isl_qpolynomial *qp;
        struct isl_upoly_cst *cst;
 
+       if (!dim)
+               return NULL;
+
        qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
        if (!qp)
                return NULL;
@@ -1647,12 +1665,22 @@ error:
        return NULL;
 }
 
-int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
+int isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
        __isl_keep isl_qpolynomial *qp2)
 {
+       int equal;
+
        if (!qp1 || !qp2)
                return -1;
 
+       equal = isl_dim_equal(qp1->dim, qp2->dim);
+       if (equal < 0 || !equal)
+               return equal;
+
+       equal = isl_mat_is_equal(qp1->div, qp2->div);
+       if (equal < 0 || !equal)
+               return equal;
+
        return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
 }
 
@@ -1980,13 +2008,13 @@ static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
  *
  * After the reduction, some divs may have become redundant or identical,
  * so we call substitute_non_divs and sort_divs.  If these functions
- * eliminate divs of merge * two or more divs into one, the coefficients
+ * eliminate divs or merge two or more divs into one, the coefficients
  * of the enclosing divs may have to be reduced again, so we call
  * ourselves recursively if the number of divs decreases.
  */
 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
 {
-       int i, j;
+       int i;
        isl_vec *aff = NULL;
        struct isl_upoly *s;
        unsigned n_div;
@@ -2158,7 +2186,7 @@ int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
        isl_assert(qp->dim->ctx, type == isl_dim_param ||
                                 type == isl_dim_set, return -1);
 
-       active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
+       active = isl_calloc_array(qp->dim->ctx, int, isl_dim_total(qp->dim));
        if (set_active(qp, active) < 0)
                goto error;
 
@@ -2178,6 +2206,90 @@ error:
        return -1;
 }
 
+/* Remove divs that do not appear in the quasi-polynomial, nor in any
+ * of the divs that do appear in the quasi-polynomial.
+ */
+static __isl_give isl_qpolynomial *remove_redundant_divs(
+       __isl_take isl_qpolynomial *qp)
+{
+       int i, j;
+       int d;
+       int len;
+       int skip;
+       int *active = NULL;
+       int *reordering = NULL;
+       int redundant = 0;
+       int n_div;
+       isl_ctx *ctx;
+
+       if (!qp)
+               return NULL;
+       if (qp->div->n_row == 0)
+               return qp;
+
+       d = isl_dim_total(qp->dim);
+       len = qp->div->n_col - 2;
+       ctx = isl_qpolynomial_get_ctx(qp);
+       active = isl_calloc_array(ctx, int, len);
+       if (!active)
+               goto error;
+
+       if (up_set_active(qp->upoly, active, len) < 0)
+               goto error;
+
+       for (i = qp->div->n_row - 1; i >= 0; --i) {
+               if (!active[d + i]) {
+                       redundant = 1;
+                       continue;
+               }
+               for (j = 0; j < i; ++j) {
+                       if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
+                               continue;
+                       active[d + j] = 1;
+                       break;
+               }
+       }
+
+       if (!redundant) {
+               free(active);
+               return qp;
+       }
+
+       reordering = isl_alloc_array(qp->div->ctx, int, len);
+       if (!reordering)
+               goto error;
+
+       for (i = 0; i < d; ++i)
+               reordering[i] = i;
+
+       skip = 0;
+       n_div = qp->div->n_row;
+       for (i = 0; i < n_div; ++i) {
+               if (!active[d + i]) {
+                       qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
+                       qp->div = isl_mat_drop_cols(qp->div,
+                                                   2 + d + i - skip, 1);
+                       skip++;
+               }
+               reordering[d + i] = d + i - skip;
+       }
+
+       qp->upoly = reorder(qp->upoly, reordering);
+
+       if (!qp->upoly || !qp->div)
+               goto error;
+
+       free(active);
+       free(reordering);
+
+       return qp;
+error:
+       free(active);
+       free(reordering);
+       isl_qpolynomial_free(qp);
+       return NULL;
+}
+
 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
        unsigned first, unsigned n)
 {
@@ -2234,7 +2346,7 @@ __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
 {
        if (!qp)
                return NULL;
-       if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
+       if (n == 0 && !isl_dim_is_named_or_nested(qp->dim, type))
                return qp;
 
        qp = isl_qpolynomial_cow(qp);
@@ -2393,6 +2505,10 @@ error:
 #define PW isl_pw_qpolynomial
 #undef EL
 #define EL isl_qpolynomial
+#undef EL_IS_ZERO
+#define EL_IS_ZERO is_zero
+#undef ZERO
+#define ZERO zero
 #undef IS_ZERO
 #define IS_ZERO is_zero
 #undef FIELD
@@ -2429,7 +2545,6 @@ __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
 {
        int i, j, n;
        struct isl_pw_qpolynomial *res;
-       isl_set *set;
 
        if (!pwqp1 || !pwqp2)
                goto error;
@@ -2489,33 +2604,6 @@ error:
        return NULL;
 }
 
-__isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
-       __isl_take isl_pw_qpolynomial *pwqp)
-{
-       int i;
-
-       if (!pwqp)
-               return NULL;
-
-       if (isl_pw_qpolynomial_is_zero(pwqp))
-               return pwqp;
-
-       pwqp = isl_pw_qpolynomial_cow(pwqp);
-       if (!pwqp)
-               return NULL;
-
-       for (i = 0; i < pwqp->n; ++i) {
-               pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
-               if (!pwqp->p[i].qp)
-                       goto error;
-       }
-
-       return pwqp;
-error:
-       isl_pw_qpolynomial_free(pwqp);
-       return NULL;
-}
-
 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
        __isl_take isl_pw_qpolynomial *pwqp1,
        __isl_take isl_pw_qpolynomial *pwqp2)
@@ -2702,7 +2790,7 @@ __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
        unsigned g_pos;
        int *exp;
 
-       if (n == 0)
+       if (n == 0 && !isl_dim_is_named_or_nested(qp->dim, type))
                return qp;
 
        qp = isl_qpolynomial_cow(qp);
@@ -2714,7 +2802,7 @@ __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
 
        g_pos = pos(qp->dim, type) + first;
 
-       qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
+       qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
        if (!qp->div)
                goto error;
 
@@ -2857,39 +2945,47 @@ __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
        return isl_qpolynomial_alloc(dim, 0, up);
 }
 
-__isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
-       __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
+__isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
 {
-       isl_int denom;
-       isl_dim *dim;
+       isl_ctx *ctx;
        struct isl_upoly *up;
        isl_qpolynomial *qp;
-       int sgn;
 
-       if (!c)
+       if (!aff)
                return NULL;
 
-       isl_int_init(denom);
-
-       isl_constraint_get_coefficient(c, type, pos, &denom);
-       isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
-       sgn = isl_int_sgn(denom);
-       isl_int_abs(denom, denom);
-       up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
-                                       1 + isl_constraint_dim(c, isl_dim_all));
-       if (sgn < 0)
-               isl_int_neg(denom, denom);
-       isl_constraint_set_coefficient(c, type, pos, denom);
+       ctx = isl_aff_get_ctx(aff);
+       up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
+                                   aff->v->size - 1);
 
-       dim = isl_dim_copy(c->bmap->dim);
+       qp = isl_qpolynomial_alloc(isl_aff_get_dim(aff),
+                                   aff->ls->div->n_row, up);
+       if (!qp)
+               goto error;
 
-       isl_int_clear(denom);
-       isl_constraint_free(c);
+       isl_mat_free(qp->div);
+       qp->div = isl_mat_copy(aff->ls->div);
+       qp->div = isl_mat_cow(qp->div);
+       if (!qp->div)
+               goto error;
 
-       qp = isl_qpolynomial_alloc(dim, 0, up);
-       if (sgn > 0)
-               qp = isl_qpolynomial_neg(qp);
+       isl_aff_free(aff);
+       qp = reduce_divs(qp);
+       qp = remove_redundant_divs(qp);
        return qp;
+error:
+       isl_aff_free(aff);
+       return NULL;
+}
+
+__isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
+       __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
+{
+       isl_aff *aff;
+
+       aff = isl_constraint_get_bound(c, type, pos);
+       isl_constraint_free(c);
+       return isl_qpolynomial_from_aff(aff);
 }
 
 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
@@ -3631,8 +3727,6 @@ __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp
        int i;
        int n_sub;
        isl_ctx *ctx;
-       struct isl_upoly *up;
-       unsigned n_div;
        struct isl_upoly **subs;
        isl_mat *mat;