isl_mat_extend: properly extend matrices with reordered rows
[platform/upstream/isl.git] / isl_mat.c
index 0a26e72..10b81e3 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -1,3 +1,4 @@
+#include "isl_dim.h"
 #include "isl_seq.h"
 #include "isl_mat.h"
 #include "isl_map_private.h"
@@ -35,6 +36,55 @@ error:
        return NULL;
 }
 
+struct isl_mat *isl_mat_extend(struct isl_ctx *ctx, struct isl_mat *mat,
+       unsigned n_row, unsigned n_col)
+{
+       int i;
+       isl_int *old;
+
+       if (!mat)
+               return NULL;
+
+       if (mat->n_col >= n_col && mat->n_row >= n_row)
+               return mat;
+
+       if (mat->n_col < n_col) {
+               struct isl_mat *new_mat;
+
+               new_mat = isl_mat_alloc(ctx, n_row, n_col);
+               if (!new_mat)
+                       goto error;
+               for (i = 0; i < mat->n_row; ++i)
+                       isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
+               isl_mat_free(ctx, mat);
+               return new_mat;
+       }
+
+       mat = isl_mat_cow(ctx, mat);
+       if (!mat)
+               goto error;
+
+       assert(mat->ref == 1);
+       old = mat->block.data;
+       mat->block = isl_blk_extend(ctx, mat->block, n_row * mat->n_col);
+       if (isl_blk_is_error(mat->block))
+               goto error;
+       mat->row = isl_realloc_array(ctx, mat->row, isl_int *, n_row);
+       if (!mat->row)
+               goto error;
+
+       for (i = 0; i < mat->n_row; ++i)
+               mat->row[i] = mat->block.data + (mat->row[i] - old);
+       for (i = mat->n_row; i < n_row; ++i)
+               mat->row[i] = mat->block.data + i * mat->n_col;
+       mat->n_row = n_row;
+
+       return mat;
+error:
+       isl_mat_free(ctx, mat);
+       return NULL;
+}
+
 struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
        unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
 {
@@ -108,7 +158,7 @@ struct isl_mat *isl_mat_cow(struct isl_ctx *ctx, struct isl_mat *mat)
        if (!mat)
                return NULL;
 
-       if (mat->ref == 1 && !F_ISSET(mat, ISL_MAT_BORROWED))
+       if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
                return mat;
 
        mat2 = isl_mat_dup(ctx, mat);
@@ -124,7 +174,7 @@ void isl_mat_free(struct isl_ctx *ctx, struct isl_mat *mat)
        if (--mat->ref > 0)
                return;
 
-       if (!F_ISSET(mat, ISL_MAT_BORROWED))
+       if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
                isl_blk_free(ctx, mat->block);
        free(mat->row);
        free(mat);
@@ -357,6 +407,35 @@ error:
        return NULL;
 }
 
+struct isl_mat *isl_mat_right_kernel(struct isl_ctx *ctx, struct isl_mat *mat)
+{
+       int i, rank;
+       struct isl_mat *U = NULL;
+       struct isl_mat *K;
+
+       mat = isl_mat_left_hermite(ctx, mat, 0, &U, NULL);
+       if (!mat || !U)
+               goto error;
+
+       for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
+               while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
+                       ++i;
+               if (i >= mat->n_row)
+                       break;
+       }
+       K = isl_mat_alloc(ctx, U->n_row, U->n_col - rank);
+       if (!K)
+               goto error;
+       isl_mat_sub_copy(ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
+       isl_mat_free(ctx, mat);
+       isl_mat_free(ctx, U);
+       return K;
+error:
+       isl_mat_free(ctx, mat);
+       isl_mat_free(ctx, U);
+       return NULL;
+}
+
 struct isl_mat *isl_mat_lin_to_aff(struct isl_ctx *ctx, struct isl_mat *mat)
 {
        int i;
@@ -623,6 +702,52 @@ error:
        return NULL;
 }
 
+struct isl_mat *isl_mat_transpose(struct isl_ctx *ctx, struct isl_mat *mat)
+{
+       struct isl_mat *transpose = NULL;
+       int i, j;
+
+       if (mat->n_col == mat->n_row) {
+               mat = isl_mat_cow(ctx, mat);
+               if (!mat)
+                       return NULL;
+               for (i = 0; i < mat->n_row; ++i)
+                       for (j = i + 1; j < mat->n_col; ++j)
+                               isl_int_swap(mat->row[i][j], mat->row[j][i]);
+               return mat;
+       }
+       transpose = isl_mat_alloc(ctx, mat->n_col, mat->n_row);
+       if (!transpose)
+               goto error;
+       for (i = 0; i < mat->n_row; ++i)
+               for (j = 0; j < mat->n_col; ++j)
+                       isl_int_set(transpose->row[j][i], mat->row[i][j]);
+       isl_mat_free(ctx, mat);
+       return transpose;
+error:
+       isl_mat_free(ctx, mat);
+       return NULL;
+}
+
+struct isl_mat *isl_mat_swap_cols(struct isl_ctx *ctx,
+       struct isl_mat *mat, unsigned i, unsigned j)
+{
+       int r;
+
+       mat = isl_mat_cow(ctx, mat);
+       if (!mat)
+               return NULL;
+       isl_assert(ctx, i < mat->n_col, goto error);
+       isl_assert(ctx, j < mat->n_col, goto error);
+
+       for (r = 0; r < mat->n_row; ++r)
+               isl_int_swap(mat->row[r][i], mat->row[r][j]);
+       return mat;
+error:
+       isl_mat_free(ctx, mat);
+       return NULL;
+}
+
 struct isl_mat *isl_mat_swap_rows(struct isl_ctx *ctx,
        struct isl_mat *mat, unsigned i, unsigned j)
 {
@@ -674,29 +799,43 @@ error:
        return NULL;
 }
 
-struct isl_basic_set *isl_basic_set_preimage(struct isl_ctx *ctx,
-       struct isl_basic_set *bset, struct isl_mat *mat)
+/* Replace the variables x in bset by x' given by x = M x', with
+ * M the matrix mat.
+ *
+ * If there are fewer variables x' then there are x, then we perform
+ * the transformation in place, which that, in principle,
+ * this frees up some extra variables as the number
+ * of columns remains constant, but we would have to extend
+ * the div array too as the number of rows in this array is assumed
+ * to be equal to extra.
+ */
+struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
+       struct isl_mat *mat)
 {
+       struct isl_ctx *ctx;
        struct isl_mat *t;
        int i;
 
        if (!bset || !mat)
                goto error;
 
+       ctx = bset->ctx;
        bset = isl_basic_set_cow(bset);
        if (!bset)
                goto error;
 
-       isl_assert(ctx, bset->nparam == 0, goto error);
+       isl_assert(ctx, bset->dim->nparam == 0, goto error);
        isl_assert(ctx, bset->n_div == 0, goto error);
-       isl_assert(ctx, 1+bset->dim == mat->n_row, goto error);
+       isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
 
        if (mat->n_col > mat->n_row)
                bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0,
                                                0, 0);
-       else {
-               bset->dim -= mat->n_row - mat->n_col;
-               bset->extra += mat->n_row - mat->n_col;
+       else if (mat->n_col < mat->n_row) {
+               bset->dim = isl_dim_cow(bset->dim);
+               if (!bset->dim)
+                       goto error;
+               bset->dim->n_out -= mat->n_row - mat->n_col;
        }
 
        t = isl_mat_sub_alloc(ctx, bset->eq, 0, bset->n_eq, 0, mat->n_row);
@@ -719,6 +858,12 @@ struct isl_basic_set *isl_basic_set_preimage(struct isl_ctx *ctx,
        }
        isl_mat_free(ctx, t);
 
+       ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
+       ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
+       ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
+       ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
+       ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
+
        bset = isl_basic_set_simplify(bset);
        bset = isl_basic_set_finalize(bset);
 
@@ -730,9 +875,9 @@ error2:
        return NULL;
 }
 
-struct isl_set *isl_set_preimage(struct isl_ctx *ctx,
-       struct isl_set *set, struct isl_mat *mat)
+struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
 {
+       struct isl_ctx *ctx;
        int i;
 
        set = isl_set_cow(set);
@@ -741,14 +886,20 @@ struct isl_set *isl_set_preimage(struct isl_ctx *ctx,
 
        ctx = set->ctx;
        for (i = 0; i < set->n; ++i) {
-               set->p[i] = isl_basic_set_preimage(ctx, set->p[i],
+               set->p[i] = isl_basic_set_preimage(set->p[i],
                                                    isl_mat_copy(ctx, mat));
                if (!set->p[i])
                        goto error;
        }
-       set->dim += mat->n_col;
-       set->dim -= mat->n_row;
+       if (mat->n_col != mat->n_row) {
+               set->dim = isl_dim_cow(set->dim);
+               if (!set->dim)
+                       goto error;
+               set->dim->n_out += mat->n_col;
+               set->dim->n_out -= mat->n_row;
+       }
        isl_mat_free(ctx, mat);
+       ISL_F_CLR(set, ISL_SET_NORMALIZED);
        return set;
 error:
        isl_set_free(set);
@@ -762,7 +913,7 @@ void isl_mat_dump(struct isl_ctx *ctx, struct isl_mat *mat,
        int i, j;
 
        if (!mat) {
-               fprintf(out, "null mat\n");
+               fprintf(out, "%*snull mat\n", indent, "");
                return;
        }
 
@@ -791,6 +942,7 @@ struct isl_mat *isl_mat_drop_cols(struct isl_ctx *ctx, struct isl_mat *mat,
 {
        int r;
 
+       mat = isl_mat_cow(ctx, mat);
        if (!mat)
                return NULL;
 
@@ -808,6 +960,7 @@ struct isl_mat *isl_mat_drop_rows(struct isl_ctx *ctx, struct isl_mat *mat,
 {
        int r;
 
+       mat = isl_mat_cow(ctx, mat);
        if (!mat)
                return NULL;
 
@@ -817,3 +970,20 @@ struct isl_mat *isl_mat_drop_rows(struct isl_ctx *ctx, struct isl_mat *mat,
        mat->n_row -= n;
        return mat;
 }
+
+void isl_mat_col_submul(struct isl_mat *mat,
+                       int dst_col, isl_int f, int src_col)
+{
+       int i;
+
+       for (i = 0; i < mat->n_row; ++i)
+               isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
+}
+
+void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
+{
+       int i;
+
+       for (i = 0; i < mat->n_row; ++i)
+               isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
+}