isl_mat_extend: make sure the number of rows never decreases
[platform/upstream/isl.git] / isl_mat.c
index a2e06fb..c96017e 100644 (file)
--- a/isl_mat.c
+++ b/isl_mat.c
@@ -24,9 +24,12 @@ struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
        for (i = 0; i < n_row; ++i)
                mat->row[i] = mat->block.data + i * n_col;
 
+       mat->ctx = ctx;
+       isl_ctx_ref(ctx);
        mat->ref = 1;
        mat->n_row = n_row;
        mat->n_col = n_col;
+       mat->max_col = n_col;
        mat->flags = 0;
 
        return mat;
@@ -36,6 +39,62 @@ error:
        return NULL;
 }
 
+struct isl_mat *isl_mat_extend(struct isl_mat *mat,
+       unsigned n_row, unsigned n_col)
+{
+       int i;
+       isl_int *old;
+
+       if (!mat)
+               return NULL;
+
+       if (mat->max_col >= n_col && mat->n_row >= n_row) {
+               if (mat->n_col < n_col)
+                       mat->n_col = n_col;
+               return mat;
+       }
+
+       if (mat->max_col < n_col) {
+               struct isl_mat *new_mat;
+
+               if (n_row < mat->n_row)
+                       n_row = mat->n_row;
+               new_mat = isl_mat_alloc(mat->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(mat);
+               return new_mat;
+       }
+
+       mat = isl_mat_cow(mat);
+       if (!mat)
+               goto error;
+
+       assert(mat->ref == 1);
+       old = mat->block.data;
+       mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
+       if (isl_blk_is_error(mat->block))
+               goto error;
+       mat->row = isl_realloc_array(mat->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->max_col;
+       mat->n_row = n_row;
+       if (mat->n_col < n_col)
+               mat->n_col = n_col;
+
+       return mat;
+error:
+       isl_mat_free(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)
 {
@@ -50,6 +109,8 @@ struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
                goto error;
        for (i = 0; i < n_row; ++i)
                mat->row[i] = row[first_row+i] + first_col;
+       mat->ctx = ctx;
+       isl_ctx_ref(ctx);
        mat->ref = 1;
        mat->n_row = n_row;
        mat->n_col = n_col;
@@ -79,7 +140,7 @@ void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
                isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
 }
 
-struct isl_mat *isl_mat_copy(struct isl_ctx *ctx, struct isl_mat *mat)
+struct isl_mat *isl_mat_copy(struct isl_mat *mat)
 {
        if (!mat)
                return NULL;
@@ -88,14 +149,14 @@ struct isl_mat *isl_mat_copy(struct isl_ctx *ctx, struct isl_mat *mat)
        return mat;
 }
 
-struct isl_mat *isl_mat_dup(struct isl_ctx *ctx, struct isl_mat *mat)
+struct isl_mat *isl_mat_dup(struct isl_mat *mat)
 {
        int i;
        struct isl_mat *mat2;
 
        if (!mat)
                return NULL;
-       mat2 = isl_mat_alloc(ctx, mat->n_row, mat->n_col);
+       mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
        if (!mat2)
                return NULL;
        for (i = 0; i < mat->n_row; ++i)
@@ -103,21 +164,21 @@ struct isl_mat *isl_mat_dup(struct isl_ctx *ctx, struct isl_mat *mat)
        return mat2;
 }
 
-struct isl_mat *isl_mat_cow(struct isl_ctx *ctx, struct isl_mat *mat)
+struct isl_mat *isl_mat_cow(struct isl_mat *mat)
 {
        struct isl_mat *mat2;
        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);
-       isl_mat_free(ctx, mat);
+       mat2 = isl_mat_dup(mat);
+       isl_mat_free(mat);
        return mat2;
 }
 
-void isl_mat_free(struct isl_ctx *ctx, struct isl_mat *mat)
+void isl_mat_free(struct isl_mat *mat)
 {
        if (!mat)
                return;
@@ -125,8 +186,9 @@ void isl_mat_free(struct isl_ctx *ctx, struct isl_mat *mat)
        if (--mat->ref > 0)
                return;
 
-       if (!F_ISSET(mat, ISL_MAT_BORROWED))
-               isl_blk_free(ctx, mat->block);
+       if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
+               isl_blk_free(mat->ctx, mat->block);
+       isl_ctx_deref(mat->ctx);
        free(mat->row);
        free(mat);
 }
@@ -148,8 +210,7 @@ struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
        return mat;
 }
 
-struct isl_vec *isl_mat_vec_product(struct isl_ctx *ctx,
-       struct isl_mat *mat, struct isl_vec *vec)
+struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
 {
        int i;
        struct isl_vec *prod;
@@ -159,24 +220,24 @@ struct isl_vec *isl_mat_vec_product(struct isl_ctx *ctx,
 
        isl_assert(ctx, mat->n_col == vec->size, goto error);
 
-       prod = isl_vec_alloc(ctx, mat->n_row);
+       prod = isl_vec_alloc(mat->ctx, mat->n_row);
        if (!prod)
                goto error;
 
        for (i = 0; i < prod->size; ++i)
-               isl_seq_inner_product(mat->row[i], vec->block.data, vec->size,
+               isl_seq_inner_product(mat->row[i], vec->el, vec->size,
                                        &prod->block.data[i]);
-       isl_mat_free(ctx, mat);
-       isl_vec_free(ctx, vec);
+       isl_mat_free(mat);
+       isl_vec_free(vec);
        return prod;
 error:
-       isl_mat_free(ctx, mat);
-       isl_vec_free(ctx, vec);
+       isl_mat_free(mat);
+       isl_vec_free(vec);
        return NULL;
 }
 
-struct isl_mat *isl_mat_aff_direct_sum(struct isl_ctx *ctx,
-       struct isl_mat *left, struct isl_mat *right)
+struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
+       struct isl_mat *right)
 {
        int i;
        struct isl_mat *sum;
@@ -195,7 +256,7 @@ struct isl_mat *isl_mat_aff_direct_sum(struct isl_ctx *ctx,
            isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
            goto error);
 
-       sum = isl_mat_alloc(ctx, left->n_row, left->n_col + right->n_col - 1);
+       sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
        if (!sum)
                goto error;
        isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
@@ -216,16 +277,16 @@ struct isl_mat *isl_mat_aff_direct_sum(struct isl_ctx *ctx,
 
        isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
        isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
-       isl_mat_free(ctx, left);
-       isl_mat_free(ctx, right);
+       isl_mat_free(left);
+       isl_mat_free(right);
        return sum;
 error:
-       isl_mat_free(ctx, left);
-       isl_mat_free(ctx, right);
+       isl_mat_free(left);
+       isl_mat_free(right);
        return NULL;
 }
 
-static void exchange(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
+static void exchange(struct isl_mat *M, struct isl_mat **U,
        struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
 {
        int r;
@@ -236,7 +297,7 @@ static void exchange(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
                        isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
        }
        if (Q)
-               isl_mat_swap_rows(ctx, *Q, i, j);
+               isl_mat_swap_rows(*Q, i, j);
 }
 
 static void subtract(struct isl_mat *M, struct isl_mat **U,
@@ -255,7 +316,7 @@ static void subtract(struct isl_mat *M, struct isl_mat **U,
        }
 }
 
-static void oppose(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
+static void oppose(struct isl_mat *M, struct isl_mat **U,
        struct isl_mat **Q, unsigned row, unsigned col)
 {
        int r;
@@ -281,8 +342,8 @@ static void oppose(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
  * 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, int neg, struct isl_mat **U, struct isl_mat **Q)
+struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
+       struct isl_mat **U, struct isl_mat **Q)
 {
        isl_int c;
        int row, col;
@@ -293,16 +354,16 @@ struct isl_mat *isl_mat_left_hermite(struct isl_ctx *ctx,
                *Q = NULL;
        if (!M)
                goto error;
-       M = isl_mat_cow(ctx, M);
+       M = isl_mat_cow(M);
        if (!M)
                goto error;
        if (U) {
-               *U = isl_mat_identity(ctx, M->n_col);
+               *U = isl_mat_identity(M->ctx, M->n_col);
                if (!*U)
                        goto error;
        }
        if (Q) {
-               *Q = isl_mat_identity(ctx, M->n_col);
+               *Q = isl_mat_identity(M->ctx, M->n_col);
                if (!*Q)
                        goto error;
        }
@@ -316,9 +377,9 @@ struct isl_mat *isl_mat_left_hermite(struct isl_ctx *ctx,
                        continue;
                first += col;
                if (first != col)
-                       exchange(ctx, M, U, Q, row, first, col);
+                       exchange(M, U, Q, row, first, col);
                if (isl_int_is_neg(M->row[row][col]))
-                       oppose(ctx, M, U, Q, row, col);
+                       oppose(M, U, Q, row, col);
                first = col+1;
                while ((off = isl_seq_first_non_zero(M->row[row]+first,
                                                       M->n_col-first)) != -1) {
@@ -326,7 +387,7 @@ struct isl_mat *isl_mat_left_hermite(struct isl_ctx *ctx,
                        isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
                        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);
+                               exchange(M, U, Q, row, first, col);
                        else
                                ++first;
                }
@@ -348,23 +409,23 @@ struct isl_mat *isl_mat_left_hermite(struct isl_ctx *ctx,
        return M;
 error:
        if (Q) {
-               isl_mat_free(ctx, *Q);
+               isl_mat_free(*Q);
                *Q = NULL;
        }
        if (U) {
-               isl_mat_free(ctx, *U);
+               isl_mat_free(*U);
                *U = NULL;
        }
        return NULL;
 }
 
-struct isl_mat *isl_mat_right_kernel(struct isl_ctx *ctx, struct isl_mat *mat)
+struct isl_mat *isl_mat_right_kernel(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);
+       mat = isl_mat_left_hermite(mat, 0, &U, NULL);
        if (!mat || !U)
                goto error;
 
@@ -374,27 +435,27 @@ struct isl_mat *isl_mat_right_kernel(struct isl_ctx *ctx, struct isl_mat *mat)
                if (i >= mat->n_row)
                        break;
        }
-       K = isl_mat_alloc(ctx, U->n_row, U->n_col - rank);
+       K = isl_mat_alloc(U->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);
+       isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
+       isl_mat_free(mat);
+       isl_mat_free(U);
        return K;
 error:
-       isl_mat_free(ctx, mat);
-       isl_mat_free(ctx, U);
+       isl_mat_free(mat);
+       isl_mat_free(U);
        return NULL;
 }
 
-struct isl_mat *isl_mat_lin_to_aff(struct isl_ctx *ctx, struct isl_mat *mat)
+struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
 {
        int i;
        struct isl_mat *mat2;
 
        if (!mat)
                return NULL;
-       mat2 = isl_mat_alloc(ctx, 1+mat->n_row, 1+mat->n_col);
+       mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
        if (!mat2)
                return NULL;
        isl_int_set_si(mat2->row[0][0], 1);
@@ -403,7 +464,7 @@ struct isl_mat *isl_mat_lin_to_aff(struct isl_ctx *ctx, struct isl_mat *mat)
                isl_int_set_si(mat2->row[1+i][0], 0);
                isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
        }
-       isl_mat_free(ctx, mat);
+       isl_mat_free(mat);
        return mat2;
 }
 
@@ -431,38 +492,36 @@ static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
        return min;
 }
 
-static void inv_exchange(struct isl_ctx *ctx,
-       struct isl_mat *left, struct isl_mat *right,
+static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
        unsigned i, unsigned j)
 {
-       left = isl_mat_swap_rows(ctx, left, i, j);
-       right = isl_mat_swap_rows(ctx, right, i, j);
+       left = isl_mat_swap_rows(left, i, j);
+       right = isl_mat_swap_rows(right, i, j);
 }
 
-static void inv_oppose(struct isl_ctx *ctx,
+static void inv_oppose(
        struct isl_mat *left, struct isl_mat *right, unsigned row)
 {
        isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
        isl_seq_neg(right->row[row], right->row[row], right->n_col);
 }
 
-static void inv_subtract(struct isl_ctx *ctx,
-       struct isl_mat *left, struct isl_mat *right,
+static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
        unsigned row, unsigned i, isl_int m)
 {
        isl_int_neg(m, m);
        isl_seq_combine(left->row[i]+row,
-                       ctx->one, left->row[i]+row,
+                       left->ctx->one, left->row[i]+row,
                        m, left->row[row]+row,
                        left->n_col-row);
-       isl_seq_combine(right->row[i], ctx->one, right->row[i],
+       isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
                        m, right->row[row], right->n_col);
 }
 
 /* Compute inv(left)*right
  */
-struct isl_mat *isl_mat_inverse_product(struct isl_ctx *ctx,
-       struct isl_mat *left, struct isl_mat *right)
+struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
+       struct isl_mat *right)
 {
        int row;
        isl_int a, b;
@@ -470,16 +529,16 @@ struct isl_mat *isl_mat_inverse_product(struct isl_ctx *ctx,
        if (!left || !right)
                goto error;
 
-       isl_assert(ctx, left->n_row == left->n_col, goto error);
-       isl_assert(ctx, left->n_row == right->n_row, goto error);
+       isl_assert(left->ctx, left->n_row == left->n_col, goto error);
+       isl_assert(left->ctx, left->n_row == right->n_row, goto error);
 
        if (left->n_row == 0) {
-               isl_mat_free(ctx, left);
+               isl_mat_free(left);
                return right;
        }
 
-       left = isl_mat_cow(ctx, left);
-       right = isl_mat_cow(ctx, right);
+       left = isl_mat_cow(left);
+       right = isl_mat_cow(right);
        if (!left || !right)
                goto error;
 
@@ -495,18 +554,18 @@ struct isl_mat *isl_mat_inverse_product(struct isl_ctx *ctx,
                }
                pivot += row;
                if (pivot != row)
-                       inv_exchange(ctx, left, right, pivot, row);
+                       inv_exchange(left, right, pivot, row);
                if (isl_int_is_neg(left->row[row][row]))
-                       inv_oppose(ctx, left, right, row);
+                       inv_oppose(left, right, row);
                first = row+1;
                while ((off = row_first_non_zero(left->row+first,
                                        left->n_row-first, row)) != -1) {
                        first += off;
                        isl_int_fdiv_q(a, left->row[first][row],
                                        left->row[row][row]);
-                       inv_subtract(ctx, left, right, row, first, a);
+                       inv_subtract(left, right, row, first, a);
                        if (!isl_int_is_zero(left->row[first][row]))
-                               inv_exchange(ctx, left, right, row, first);
+                               inv_exchange(left, right, row, first);
                        else
                                ++first;
                }
@@ -543,11 +602,11 @@ struct isl_mat *isl_mat_inverse_product(struct isl_ctx *ctx,
        }
        isl_int_clear(a);
 
-       isl_mat_free(ctx, left);
+       isl_mat_free(left);
        return right;
 error:
-       isl_mat_free(ctx, left);
-       isl_mat_free(ctx, right);
+       isl_mat_free(left);
+       isl_mat_free(right);
        return NULL;
 }
 
@@ -574,19 +633,18 @@ void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
        isl_int_clear(tmp);
 }
 
-struct isl_mat *isl_mat_right_inverse(struct isl_ctx *ctx,
-       struct isl_mat *mat)
+struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
 {
        struct isl_mat *inv;
        int row;
        isl_int a, b;
 
-       mat = isl_mat_cow(ctx, mat);
+       mat = isl_mat_cow(mat);
        if (!mat)
                return NULL;
 
-       inv = isl_mat_identity(ctx, mat->n_col);
-       inv = isl_mat_cow(ctx, inv);
+       inv = isl_mat_identity(mat->ctx, mat->n_col);
+       inv = isl_mat_cow(inv);
        if (!inv)
                goto error;
 
@@ -602,9 +660,9 @@ struct isl_mat *isl_mat_right_inverse(struct isl_ctx *ctx,
                }
                pivot += row;
                if (pivot != row)
-                       exchange(ctx, mat, &inv, NULL, row, pivot, row);
+                       exchange(mat, &inv, NULL, row, pivot, row);
                if (isl_int_is_neg(mat->row[row][row]))
-                       oppose(ctx, mat, &inv, NULL, row, row);
+                       oppose(mat, &inv, NULL, row, row);
                first = row+1;
                while ((off = isl_seq_first_non_zero(mat->row[row]+first,
                                                    mat->n_col-first)) != -1) {
@@ -613,7 +671,7 @@ struct isl_mat *isl_mat_right_inverse(struct isl_ctx *ctx,
                                                    mat->row[row][row]);
                        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);
+                               exchange(mat, &inv, NULL, row, row, first);
                        else
                                ++first;
                }
@@ -645,39 +703,66 @@ struct isl_mat *isl_mat_right_inverse(struct isl_ctx *ctx,
        }
        isl_int_clear(a);
 
-       isl_mat_free(ctx, mat);
+       isl_mat_free(mat);
 
        return inv;
 error:
-       isl_mat_free(ctx, mat);
+       isl_mat_free(mat);
        return NULL;
 }
 
-struct isl_mat *isl_mat_transpose(struct isl_ctx *ctx, struct isl_mat *mat)
+struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
 {
+       struct isl_mat *transpose = NULL;
        int i, j;
 
-       mat = isl_mat_cow(ctx, mat);
+       if (mat->n_col == mat->n_row) {
+               mat = isl_mat_cow(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(mat->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(mat);
+       return transpose;
+error:
+       isl_mat_free(mat);
+       return NULL;
+}
+
+struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
+{
+       int r;
+
+       mat = isl_mat_cow(mat);
        if (!mat)
                return NULL;
-       isl_assert(ctx, mat->n_col == mat->n_row, 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]);
+       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);
+       isl_mat_free(mat);
        return NULL;
 }
 
-struct isl_mat *isl_mat_swap_rows(struct isl_ctx *ctx,
-       struct isl_mat *mat, unsigned i, unsigned j)
+struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
 {
        isl_int *t;
 
        if (!mat)
                return NULL;
-       mat = isl_mat_cow(ctx, mat);
+       mat = isl_mat_cow(mat);
        if (!mat)
                return NULL;
        t = mat->row[i];
@@ -686,8 +771,7 @@ struct isl_mat *isl_mat_swap_rows(struct isl_ctx *ctx,
        return mat;
 }
 
-struct isl_mat *isl_mat_product(struct isl_ctx *ctx,
-       struct isl_mat *left, struct isl_mat *right)
+struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right)
 {
        int i, j, k;
        struct isl_mat *prod;
@@ -695,7 +779,7 @@ struct isl_mat *isl_mat_product(struct isl_ctx *ctx,
        if (!left || !right)
                goto error;
        isl_assert(ctx, left->n_col == right->n_row, goto error);
-       prod = isl_mat_alloc(ctx, left->n_row, right->n_col);
+       prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
        if (!prod)
                goto error;
        if (left->n_col == 0) {
@@ -712,15 +796,54 @@ struct isl_mat *isl_mat_product(struct isl_ctx *ctx,
                                            left->row[i][k], right->row[k][j]);
                }
        }
-       isl_mat_free(ctx, left);
-       isl_mat_free(ctx, right);
+       isl_mat_free(left);
+       isl_mat_free(right);
        return prod;
 error:
-       isl_mat_free(ctx, left);
-       isl_mat_free(ctx, right);
+       isl_mat_free(left);
+       isl_mat_free(right);
        return NULL;
 }
 
+/* Replace the variables x in the rows q by x' given by x = M x',
+ * with M the matrix mat.
+ *
+ * If the number of new variables is greater than the original
+ * number of variables, then the rows q have already been
+ * preextended.  If the new number is smaller, then the coefficients
+ * of the divs, which are not changed, need to be shifted down.
+ * The row q may be the equalities, the inequalities or the
+ * div expressions.  In the latter case, has_div is true and
+ * we need to take into account the extra denominator column.
+ */
+static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
+       unsigned n_div, int has_div, struct isl_mat *mat)
+{
+       int i;
+       struct isl_mat *t;
+       int e;
+
+       if (mat->n_col >= mat->n_row)
+               e = 0;
+       else
+               e = mat->n_row - mat->n_col;
+       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_product(t, mat);
+       if (!t)
+               return -1;
+       for (i = 0; i < n; ++i) {
+               isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
+               isl_seq_cpy(q[i] + has_div + t->n_col,
+                           q[i] + has_div + t->n_col + e, n_div);
+               isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
+       }
+       isl_mat_free(t);
+       return 0;
+}
+
 /* Replace the variables x in bset by x' given by x = M x', with
  * M the matrix mat.
  *
@@ -731,21 +854,20 @@ error:
  * 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_ctx *ctx,
-       struct isl_basic_set *bset, struct isl_mat *mat)
+struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
+       struct isl_mat *mat)
 {
-       struct isl_mat *t;
-       int i;
+       struct isl_ctx *ctx;
 
        if (!bset || !mat)
                goto error;
 
+       ctx = bset->ctx;
        bset = isl_basic_set_cow(bset);
        if (!bset)
                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->n_out == mat->n_row, goto error);
 
        if (mat->n_col > mat->n_row)
@@ -758,40 +880,37 @@ struct isl_basic_set *isl_basic_set_preimage(struct isl_ctx *ctx,
                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);
-       t = isl_mat_product(ctx, t, isl_mat_copy(ctx, mat));
-       if (!t)
+       if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
+                       isl_mat_copy(mat)) < 0)
                goto error;
-       for (i = 0; i < bset->n_eq; ++i) {
-               isl_seq_swp_or_cpy(bset->eq[i], t->row[i], t->n_col);
-               isl_seq_clr(bset->eq[i]+t->n_col, bset->extra);
-       }
-       isl_mat_free(ctx, t);
 
-       t = isl_mat_sub_alloc(ctx, bset->ineq, 0, bset->n_ineq, 0, mat->n_row);
-       t = isl_mat_product(ctx, t, mat);
-       if (!t)
+       if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
+                       isl_mat_copy(mat)) < 0)
+               goto error;
+
+       if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
                goto error2;
-       for (i = 0; i < bset->n_ineq; ++i) {
-               isl_seq_swp_or_cpy(bset->ineq[i], t->row[i], t->n_col);
-               isl_seq_clr(bset->ineq[i]+t->n_col, bset->extra);
-       }
-       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);
 
        return bset;
 error:
-       isl_mat_free(ctx, mat);
+       isl_mat_free(mat);
 error2:
        isl_basic_set_free(bset);
        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);
@@ -800,8 +919,8 @@ 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],
-                                                   isl_mat_copy(ctx, mat));
+               set->p[i] = isl_basic_set_preimage(set->p[i],
+                                                   isl_mat_copy(mat));
                if (!set->p[i])
                        goto error;
        }
@@ -812,22 +931,21 @@ struct isl_set *isl_set_preimage(struct isl_ctx *ctx,
                set->dim->n_out += mat->n_col;
                set->dim->n_out -= mat->n_row;
        }
-       isl_mat_free(ctx, mat);
-       F_CLR(set, ISL_SET_NORMALIZED);
+       isl_mat_free(mat);
+       ISL_F_CLR(set, ISL_SET_NORMALIZED);
        return set;
 error:
        isl_set_free(set);
-       isl_mat_free(ctx, mat);
+       isl_mat_free(mat);
        return NULL;
 }
 
-void isl_mat_dump(struct isl_ctx *ctx, struct isl_mat *mat,
-                               FILE *out, int indent)
+void isl_mat_dump(struct isl_mat *mat, FILE *out, int indent)
 {
        int i, j;
 
        if (!mat) {
-               fprintf(out, "null mat\n");
+               fprintf(out, "%*snull mat\n", indent, "");
                return;
        }
 
@@ -851,12 +969,11 @@ void isl_mat_dump(struct isl_ctx *ctx, struct isl_mat *mat,
        }
 }
 
-struct isl_mat *isl_mat_drop_cols(struct isl_ctx *ctx, struct isl_mat *mat,
-                               unsigned col, unsigned n)
+struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
 {
        int r;
 
-       mat = isl_mat_cow(ctx, mat);
+       mat = isl_mat_cow(mat);
        if (!mat)
                return NULL;
 
@@ -869,12 +986,11 @@ struct isl_mat *isl_mat_drop_cols(struct isl_ctx *ctx, struct isl_mat *mat,
        return mat;
 }
 
-struct isl_mat *isl_mat_drop_rows(struct isl_ctx *ctx, struct isl_mat *mat,
-                               unsigned row, unsigned n)
+struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
 {
        int r;
 
-       mat = isl_mat_cow(ctx, mat);
+       mat = isl_mat_cow(mat);
        if (!mat)
                return NULL;
 
@@ -901,3 +1017,28 @@ 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_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(isl_mat_copy(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(H);
+       isl_mat_free(Q);
+       return M;
+error:
+       isl_mat_free(H);
+       isl_mat_free(Q);
+       isl_mat_free(M);
+       return NULL;
+}