privately export isl_mat_unimodular_complete
[platform/upstream/isl.git] / isl_mat.c
index 00ac607..4947c3d 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -40,6 +40,7 @@ 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;
@@ -64,6 +65,7 @@ struct isl_mat *isl_mat_extend(struct isl_ctx *ctx, struct isl_mat *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;
@@ -71,7 +73,9 @@ struct isl_mat *isl_mat_extend(struct isl_ctx *ctx, struct isl_mat *mat,
        if (!mat->row)
                goto error;
 
-       for (i = 0; i < n_row; ++i)
+       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;
 
@@ -700,16 +704,26 @@ error:
 
 struct isl_mat *isl_mat_transpose(struct isl_ctx *ctx, struct isl_mat *mat)
 {
+       struct isl_mat *transpose = NULL;
        int i, j;
 
-       mat = isl_mat_cow(ctx, mat);
-       if (!mat)
-               return NULL;
-       isl_assert(ctx, mat->n_col == mat->n_row, goto error);
+       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 = i + 1; j < mat->n_col; ++j)
-                       isl_int_swap(mat->row[i][j], mat->row[j][i]);
-       return mat;
+               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;
@@ -973,3 +987,29 @@ void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
        for (i = 0; i < mat->n_row; ++i)
                isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
 }
+
+struct isl_mat *isl_mat_unimodular_complete(struct isl_ctx *ctx,
+                                               struct isl_mat *M, int row)
+{
+       int r;
+       struct isl_mat *H = NULL, *Q = NULL;
+
+       isl_assert(ctx, M->n_row == M->n_col, goto error);
+       M->n_row = row;
+       H = isl_mat_left_hermite(ctx, isl_mat_copy(ctx, M), 0, NULL, &Q);
+       M->n_row = M->n_col;
+       if (!H)
+               goto error;
+       for (r = 0; r < row; ++r)
+               isl_assert(ctx, isl_int_is_one(H->row[r][r]), goto error);
+       for (r = row; r < M->n_row; ++r)
+               isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
+       isl_mat_free(ctx, H);
+       isl_mat_free(ctx, Q);
+       return M;
+error:
+       isl_mat_free(ctx, H);
+       isl_mat_free(ctx, Q);
+       isl_mat_free(ctx, M);
+       return NULL;
+}