+#include "isl_dim.h"
#include "isl_seq.h"
#include "isl_mat.h"
#include "isl_map_private.h"
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)
{
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);
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);
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_vec_free(vec);
return prod;
error:
isl_mat_free(ctx, mat);
- isl_vec_free(ctx, vec);
+ isl_vec_free(vec);
return NULL;
}
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) {
*
* 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;
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
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;
}
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;
return NULL;
}
+void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
+{
+ int i;
+
+ for (i = 0; i < mat->n_row; ++i)
+ isl_int_mul(mat->row[i][col], mat->row[i][col], m);
+}
+
+void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
+ isl_int m1, unsigned src1, isl_int m2, unsigned src2)
+{
+ int i;
+ isl_int tmp;
+
+ isl_int_init(tmp);
+ for (i = 0; i < mat->n_row; ++i) {
+ isl_int_mul(tmp, m1, mat->row[i][src1]);
+ isl_int_addmul(tmp, m2, mat->row[i][src2]);
+ isl_int_set(mat->row[i][dst], tmp);
+ }
+ isl_int_clear(tmp);
+}
+
+struct isl_mat *isl_mat_right_inverse(struct isl_ctx *ctx,
+ struct isl_mat *mat)
+{
+ struct isl_mat *inv;
+ int row;
+ isl_int a, b;
+
+ mat = isl_mat_cow(ctx, mat);
+ if (!mat)
+ return NULL;
+
+ inv = isl_mat_identity(ctx, mat->n_col);
+ inv = isl_mat_cow(ctx, inv);
+ if (!inv)
+ goto error;
+
+ isl_int_init(a);
+ isl_int_init(b);
+ for (row = 0; row < mat->n_row; ++row) {
+ int pivot, first, i, off;
+ pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
+ if (pivot < 0) {
+ isl_int_clear(a);
+ isl_int_clear(b);
+ goto error;
+ }
+ pivot += row;
+ if (pivot != row)
+ exchange(ctx, mat, &inv, NULL, row, pivot, row);
+ if (isl_int_is_neg(mat->row[row][row]))
+ oppose(ctx, mat, &inv, NULL, row, row);
+ first = row+1;
+ while ((off = isl_seq_first_non_zero(mat->row[row]+first,
+ mat->n_col-first)) != -1) {
+ first += off;
+ isl_int_fdiv_q(a, mat->row[row][first],
+ 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);
+ else
+ ++first;
+ }
+ for (i = 0; i < row; ++i) {
+ if (isl_int_is_zero(mat->row[row][i]))
+ continue;
+ isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
+ isl_int_divexact(b, mat->row[row][i], a);
+ isl_int_divexact(a, mat->row[row][row], a);
+ isl_int_neg(a, a);
+ isl_mat_col_combine(mat, i, a, i, b, row);
+ isl_mat_col_combine(inv, i, a, i, b, row);
+ }
+ }
+ isl_int_clear(b);
+
+ isl_int_set(a, mat->row[0][0]);
+ for (row = 1; row < mat->n_row; ++row)
+ isl_int_lcm(a, a, mat->row[row][row]);
+ if (isl_int_is_zero(a)){
+ isl_int_clear(a);
+ goto error;
+ }
+ for (row = 0; row < mat->n_row; ++row) {
+ isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
+ if (isl_int_is_one(mat->row[row][row]))
+ continue;
+ isl_mat_col_scale(inv, row, mat->row[row][row]);
+ }
+ isl_int_clear(a);
+
+ isl_mat_free(ctx, mat);
+
+ return inv;
+error:
+ isl_mat_free(ctx, mat);
+ 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)
{
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 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)
{
- struct isl_mat *t;
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(ctx, q, 0, n, has_div, mat->n_row);
+ t = isl_mat_product(ctx, 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(ctx, t);
+ return 0;
+}
+
+/* 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;
if (!bset || !mat)
goto error;
- bset = isl_basic_set_cow(ctx, bset);
+ 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->n_div == 0, goto error);
- isl_assert(ctx, 1+bset->dim == mat->n_row, goto error);
+ isl_assert(ctx, bset->dim->nparam == 0, goto error);
+ isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
- 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 (mat->n_col > mat->n_row)
+ bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0,
+ 0, 0);
+ 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;
+ }
+
+ if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
+ isl_mat_copy(ctx, 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_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, isl_mat_copy(ctx, mat));
- if (!t)
+ if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
+ isl_mat_copy(ctx, mat)) < 0)
goto error;
- for (i = 0; i < bset->n_ineq; ++i)
- isl_seq_swp_or_cpy(bset->ineq[i], t->row[i], t->n_col);
- isl_mat_free(ctx, t);
- bset->dim -= mat->n_row - mat->n_col;
- bset = isl_basic_set_simplify(ctx, bset);
- bset = isl_basic_set_finalize(ctx, bset);
+ if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
+ goto error2;
+
+ 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);
- isl_mat_free(ctx, mat);
return bset;
error:
isl_mat_free(ctx, mat);
- isl_basic_set_free(ctx, bset);
+error2:
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+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);
+ if (!set)
+ return NULL;
+
+ ctx = set->ctx;
+ for (i = 0; i < set->n; ++i) {
+ set->p[i] = isl_basic_set_preimage(set->p[i],
+ isl_mat_copy(ctx, mat));
+ if (!set->p[i])
+ goto error;
+ }
+ 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);
+ isl_mat_free(ctx, mat);
return NULL;
}
{
int i, j;
+ if (!mat) {
+ fprintf(out, "%*snull mat\n", indent, "");
+ return;
+ }
+
if (mat->n_row == 0)
fprintf(out, "%*s[]\n", indent, "");
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");
}
}
-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;
+ mat = isl_mat_cow(ctx, mat);
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;
}
{
int r;
+ mat = isl_mat_cow(ctx, mat);
if (!mat)
return NULL;
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]);
+}
+
+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;
+}