add isl_mat_add_zero_rows
[platform/upstream/isl.git] / isl_mat.c
index 90e981f..ace8a43 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -107,7 +107,7 @@ error:
        return NULL;
 }
 
-struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
+__isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
        unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
 {
        int i;
@@ -134,6 +134,15 @@ error:
        return NULL;
 }
 
+__isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
+       unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
+{
+       if (!mat)
+               return NULL;
+       return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
+                                 first_col, n_col);
+}
+
 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
        unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
 {
@@ -248,6 +257,25 @@ error:
        return NULL;
 }
 
+__isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
+       int row, int col, int v)
+{
+       mat = isl_mat_cow(mat);
+       if (!mat)
+               return NULL;
+       if (row < 0 || row >= mat->n_row)
+               isl_die(mat->ctx, isl_error_invalid, "row out of range",
+                       goto error);
+       if (col < 0 || col >= mat->n_col)
+               isl_die(mat->ctx, isl_error_invalid, "column out of range",
+                       goto error);
+       isl_int_set_si(mat->row[row][col], v);
+       return mat;
+error:
+       isl_mat_free(mat);
+       return NULL;
+}
+
 struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
 {
        int i;
@@ -985,7 +1013,7 @@ static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
        if (has_div)
                for (i = 0; i < n; ++i)
                        isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
-       t = isl_mat_sub_alloc(mat->ctx, q, 0, n, has_div, mat->n_row);
+       t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
        t = isl_mat_product(t, mat);
        if (!t)
                return -1;
@@ -1097,6 +1125,73 @@ error:
        return NULL;
 }
 
+/* Replace the variables x starting at pos in the rows q
+ * by x' with x = M x' with M the matrix mat.
+ * That is, replace the corresponding coefficients c by c M.
+ */
+static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
+       unsigned pos, __isl_take isl_mat *mat)
+{
+       int i;
+       isl_mat *t;
+
+       t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
+       t = isl_mat_product(t, mat);
+       if (!t)
+               return -1;
+       for (i = 0; i < n; ++i)
+               isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
+       isl_mat_free(t);
+       return 0;
+}
+
+/* Replace the variables x of type "type" starting at "first" in "bset"
+ * by x' with x = M x' with M the matrix trans.
+ * That is, replace the corresponding coefficients c by c M.
+ *
+ * The transformation matrix should be a square matrix.
+ */
+__isl_give isl_basic_set *isl_basic_set_transform_dims(
+       __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
+       __isl_take isl_mat *trans)
+{
+       isl_ctx *ctx;
+       unsigned pos;
+
+       bset = isl_basic_set_cow(bset);
+       if (!bset || !trans)
+               goto error;
+
+       ctx = isl_basic_set_get_ctx(bset);
+       if (trans->n_row != trans->n_col)
+               isl_die(trans->ctx, isl_error_invalid,
+                       "expecting square transformation matrix", goto error);
+       if (first + trans->n_row > isl_basic_set_dim(bset, type))
+               isl_die(trans->ctx, isl_error_invalid,
+                       "oversized transformation matrix", goto error);
+
+       pos = isl_basic_set_offset(bset, type) + first;
+
+       if (transform(ctx, bset->eq, bset->n_eq, pos, isl_mat_copy(trans)) < 0)
+               goto error;
+       if (transform(ctx, bset->ineq, bset->n_ineq, pos,
+                     isl_mat_copy(trans)) < 0)
+               goto error;
+       if (transform(ctx, bset->div, bset->n_div, 1 + pos,
+                     isl_mat_copy(trans)) < 0)
+               goto error;
+
+       ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
+       ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
+
+       isl_mat_free(trans);
+       return bset;
+error:
+       isl_mat_free(trans);
+       isl_basic_set_free(bset);
+       return NULL;
+}
+
 void isl_mat_dump(struct isl_mat *mat, FILE *out, int indent)
 {
        int i, j;
@@ -1241,6 +1336,29 @@ __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
        return isl_mat_insert_rows(mat, mat->n_row, n);
 }
 
+__isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
+       unsigned row, unsigned n)
+{
+       int i;
+
+       mat = isl_mat_insert_rows(mat, row, n);
+       if (!mat)
+               return NULL;
+       
+       for (i = 0; i < n; ++i)
+               isl_seq_clr(mat->row[row + i], mat->n_col);
+
+       return mat;
+}
+
+__isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
+{
+       if (!mat)
+               return NULL;
+
+       return isl_mat_insert_zero_rows(mat, mat->n_row, n);
+}
+
 void isl_mat_col_submul(struct isl_mat *mat,
                        int dst_col, isl_int f, int src_col)
 {
@@ -1464,3 +1582,19 @@ __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
 
        return mat;
 }
+
+/* Number of initial non-zero columns.
+ */
+int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
+{
+       int i;
+
+       if (!mat)
+               return -1;
+
+       for (i = 0; i < mat->n_col; ++i)
+               if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
+                       break;
+
+       return i;
+}