add isl_basic_set_interval and isl_basic_set_product
[platform/upstream/isl.git] / isl_mat.c
index 9b4558c..0a26e72 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -238,10 +238,10 @@ static void exchange(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
                isl_mat_swap_rows(ctx, *Q, i, j);
 }
 
-static void subtract(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
+static void subtract(struct isl_mat *M, struct isl_mat **U,
        struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
 {
-       int r, c;
+       int r;
        for (r = row; r < M->n_row; ++r)
                isl_int_submul(M->row[r][j], m, M->row[r][i]);
        if (U) {
@@ -275,11 +275,13 @@ static void oppose(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
  *
  * with U and Q unimodular matrices and H a matrix in column echelon form
  * such that on each echelon row the entries in the non-echelon column
- * are non-negative and stricly smaller than the entries in the echelon
- * column.  If U or Q are NULL, then these matrices are not computed.
+ * are non-negative (if neg == 0) or non-positive (if neg == 1)
+ * and stricly smaller (in absolute value) than the entries in the echelon
+ * column.
+ * If U or Q are NULL, then these matrices are not computed.
  */
 struct isl_mat *isl_mat_left_hermite(struct isl_ctx *ctx,
-       struct isl_mat *M, struct isl_mat **U, struct isl_mat **Q)
+       struct isl_mat *M, int neg, struct isl_mat **U, struct isl_mat **Q)
 {
        isl_int c;
        int row, col;
@@ -321,7 +323,7 @@ struct isl_mat *isl_mat_left_hermite(struct isl_ctx *ctx,
                                                       M->n_col-first)) != -1) {
                        first += off;
                        isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
-                       subtract(ctx, M, U, Q, row, col, first, c);
+                       subtract(M, U, Q, row, col, first, c);
                        if (!isl_int_is_zero(M->row[row][first]))
                                exchange(ctx, M, U, Q, row, first, col);
                        else
@@ -330,10 +332,13 @@ struct isl_mat *isl_mat_left_hermite(struct isl_ctx *ctx,
                for (i = 0; i < col; ++i) {
                        if (isl_int_is_zero(M->row[row][i]))
                                continue;
-                       isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
+                       if (neg)
+                               isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
+                       else
+                               isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
                        if (isl_int_is_zero(c))
                                continue;
-                       subtract(ctx, M, U, Q, row, col, i, c);
+                       subtract(M, U, Q, row, col, i, c);
                }
                ++col;
        }
@@ -524,7 +529,7 @@ void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
                isl_int_mul(mat->row[i][col], mat->row[i][col], m);
 }
 
-isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
+void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
        isl_int m1, unsigned src1, isl_int m2, unsigned src2)
 {
        int i;
@@ -576,7 +581,7 @@ struct isl_mat *isl_mat_right_inverse(struct isl_ctx *ctx,
                        first += off;
                        isl_int_fdiv_q(a, mat->row[row][first],
                                                    mat->row[row][row]);
-                       subtract(ctx, mat, &inv, NULL, row, row, first, a);
+                       subtract(mat, &inv, NULL, row, row, first, a);
                        if (!isl_int_is_zero(mat->row[row][first]))
                                exchange(ctx, mat, &inv, NULL, row, row, first);
                        else
@@ -678,7 +683,7 @@ struct isl_basic_set *isl_basic_set_preimage(struct isl_ctx *ctx,
        if (!bset || !mat)
                goto error;
 
-       bset = isl_basic_set_cow(ctx, bset);
+       bset = isl_basic_set_cow(bset);
        if (!bset)
                goto error;
 
@@ -687,7 +692,7 @@ struct isl_basic_set *isl_basic_set_preimage(struct isl_ctx *ctx,
        isl_assert(ctx, 1+bset->dim == mat->n_row, goto error);
 
        if (mat->n_col > mat->n_row)
-               bset = isl_basic_set_extend(ctx, bset, 0, mat->n_col-1, 0,
+               bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0,
                                                0, 0);
        else {
                bset->dim -= mat->n_row - mat->n_col;
@@ -714,14 +719,14 @@ struct isl_basic_set *isl_basic_set_preimage(struct isl_ctx *ctx,
        }
        isl_mat_free(ctx, t);
 
-       bset = isl_basic_set_simplify(ctx, bset);
-       bset = isl_basic_set_finalize(ctx, bset);
+       bset = isl_basic_set_simplify(bset);
+       bset = isl_basic_set_finalize(bset);
 
        return bset;
 error:
        isl_mat_free(ctx, mat);
 error2:
-       isl_basic_set_free(ctx, bset);
+       isl_basic_set_free(bset);
        return NULL;
 }
 
@@ -730,10 +735,11 @@ struct isl_set *isl_set_preimage(struct isl_ctx *ctx,
 {
        int i;
 
-       set = isl_set_cow(ctx, set);
+       set = isl_set_cow(set);
        if (!set)
                return NULL;
 
+       ctx = set->ctx;
        for (i = 0; i < set->n; ++i) {
                set->p[i] = isl_basic_set_preimage(ctx, set->p[i],
                                                    isl_mat_copy(ctx, mat));
@@ -745,7 +751,7 @@ struct isl_set *isl_set_preimage(struct isl_ctx *ctx,
        isl_mat_free(ctx, mat);
        return set;
 error:
-       isl_set_free(ctx, set);
+       isl_set_free(set);
        isl_mat_free(ctx, mat);
        return NULL;
 }
@@ -771,7 +777,7 @@ void isl_mat_dump(struct isl_ctx *ctx, struct isl_mat *mat,
                for (j = 0; j < mat->n_col; ++j) {
                        if (j)
                            fprintf(out, ",");
-                       isl_int_print(out, mat->row[i][j]);
+                       isl_int_print(out, mat->row[i][j], 0);
                }
                if (i == mat->n_row-1)
                        fprintf(out, "]]\n");
@@ -780,20 +786,20 @@ void isl_mat_dump(struct isl_ctx *ctx, struct isl_mat *mat,
        }
 }
 
-struct isl_mat *isl_mat_drop_col(struct isl_ctx *ctx, struct isl_mat *mat,
-                               unsigned col)
+struct isl_mat *isl_mat_drop_cols(struct isl_ctx *ctx, struct isl_mat *mat,
+                               unsigned col, unsigned n)
 {
        int r;
 
        if (!mat)
                return NULL;
 
-       if (col != mat->n_col-1) {
+       if (col != mat->n_col-n) {
                for (r = 0; r < mat->n_row; ++r)
-                       isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+1,
-                                       mat->n_col - col - 1);
+                       isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
+                                       mat->n_col - col - n);
        }
-       mat->n_col--;
+       mat->n_col -= n;
        return mat;
 }