+#include "isl_dim.h"
#include "isl_seq.h"
#include "isl_mat.h"
#include "isl_map_private.h"
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;
}
isl_int_mul(mat->row[i][col], mat->row[i][col], m);
}
-isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
+void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
isl_int m1, unsigned src1, isl_int m2, unsigned src2)
{
int i;
first += off;
isl_int_fdiv_q(a, mat->row[row][first],
mat->row[row][row]);
- subtract(ctx, mat, &inv, NULL, row, row, first, a);
+ 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
return NULL;
}
+/* 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_ctx *ctx,
struct isl_basic_set *bset, struct isl_mat *mat)
{
if (!bset || !mat)
goto error;
- bset = isl_basic_set_cow(ctx, bset);
+ 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(ctx, bset, 0, mat->n_col-1, 0,
+ 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);
}
isl_mat_free(ctx, t);
- bset = isl_basic_set_simplify(ctx, bset);
- bset = isl_basic_set_finalize(ctx, bset);
+ bset = isl_basic_set_simplify(bset);
+ bset = isl_basic_set_finalize(bset);
return bset;
error:
isl_mat_free(ctx, mat);
error2:
- isl_basic_set_free(ctx, bset);
+ isl_basic_set_free(bset);
return NULL;
}
{
int i;
- set = isl_set_cow(ctx, set);
+ 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(ctx, 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);
return set;
error:
- isl_set_free(ctx, set);
+ isl_set_free(set);
isl_mat_free(ctx, mat);
return NULL;
}
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;
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;
}