-#include "isl_dim.h"
-#include "isl_seq.h"
-#include "isl_mat.h"
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
+#include <isl_ctx_private.h>
+#include <isl/space.h>
+#include <isl/seq.h>
+#include <isl_mat_private.h>
#include "isl_map_private.h"
+#include <isl_space_private.h>
+
+isl_ctx *isl_mat_get_ctx(__isl_keep isl_mat *mat)
+{
+ return mat ? mat->ctx : NULL;
+}
struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
unsigned n_row, unsigned n_col)
{
int i;
isl_int *old;
+ isl_int **row;
if (!mat)
return NULL;
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)
+ row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
+ if (!row)
goto error;
+ mat->row = row;
for (i = 0; i < mat->n_row; ++i)
mat->row[i] = mat->block.data + (mat->row[i] - old);
return NULL;
}
-struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
+__isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
{
int i;
return NULL;
}
+__isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
+ unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
+{
+ if (!mat)
+ return NULL;
+ return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
+ first_col, n_col);
+}
+
void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
{
free(mat);
}
-struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
+int isl_mat_rows(__isl_keep isl_mat *mat)
+{
+ return mat ? mat->n_row : -1;
+}
+
+int isl_mat_cols(__isl_keep isl_mat *mat)
+{
+ return mat ? mat->n_col : -1;
+}
+
+int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
+{
+ if (!mat)
+ return -1;
+ if (row < 0 || row >= mat->n_row)
+ isl_die(mat->ctx, isl_error_invalid, "row out of range",
+ return -1);
+ if (col < 0 || col >= mat->n_col)
+ isl_die(mat->ctx, isl_error_invalid, "column out of range",
+ return -1);
+ isl_int_set(*v, mat->row[row][col]);
+ return 0;
+}
+
+__isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
+ int row, int col, isl_int v)
+{
+ mat = isl_mat_cow(mat);
+ if (!mat)
+ return NULL;
+ if (row < 0 || row >= mat->n_row)
+ isl_die(mat->ctx, isl_error_invalid, "row out of range",
+ goto error);
+ if (col < 0 || col >= mat->n_col)
+ isl_die(mat->ctx, isl_error_invalid, "column out of range",
+ goto error);
+ isl_int_set(mat->row[row][col], v);
+ return mat;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+__isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
+ int row, int col, int v)
+{
+ mat = isl_mat_cow(mat);
+ if (!mat)
+ return NULL;
+ if (row < 0 || row >= mat->n_row)
+ isl_die(mat->ctx, isl_error_invalid, "row out of range",
+ goto error);
+ if (col < 0 || col >= mat->n_col)
+ isl_die(mat->ctx, isl_error_invalid, "column out of range",
+ goto error);
+ isl_int_set_si(mat->row[row][col], v);
+ return mat;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+__isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
{
int i;
struct isl_mat *mat;
return NULL;
for (i = 0; i < n_row; ++i) {
isl_seq_clr(mat->row[i], i);
- isl_int_set_si(mat->row[i][i], 1);
+ isl_int_set(mat->row[i][i], d);
isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
}
return mat;
}
+__isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
+{
+ if (!ctx)
+ return NULL;
+ return isl_mat_diag(ctx, n_row, ctx->one);
+}
+
struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
{
int i;
return NULL;
}
+__isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
+ __isl_take isl_vec *vec)
+{
+ struct isl_mat *vec_mat;
+ int i;
+
+ if (!mat || !vec)
+ goto error;
+ vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
+ if (!vec_mat)
+ goto error;
+ for (i = 0; i < vec->size; ++i)
+ isl_int_set(vec_mat->row[i][0], vec->el[i]);
+ vec_mat = isl_mat_inverse_product(mat, vec_mat);
+ isl_vec_free(vec);
+ if (!vec_mat)
+ return NULL;
+ vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
+ if (vec)
+ for (i = 0; i < vec->size; ++i)
+ isl_int_set(vec->el[i], vec_mat->row[i][0]);
+ isl_mat_free(vec_mat);
+ return vec;
+error:
+ isl_mat_free(mat);
+ isl_vec_free(vec);
+ return NULL;
+}
+
struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
{
int i, j;
isl_mat_free(*U);
*U = NULL;
}
+ isl_mat_free(M);
return NULL;
}
return NULL;
mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
if (!mat2)
- return NULL;
+ goto error;
isl_int_set_si(mat2->row[0][0], 1);
isl_seq_clr(mat2->row[0]+1, mat->n_col);
for (i = 0; i < mat->n_row; ++i) {
}
isl_mat_free(mat);
return mat2;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+/* Given two matrices M1 and M2, return the block matrix
+ *
+ * [ M1 0 ]
+ * [ 0 M2 ]
+ */
+__isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
+ __isl_take isl_mat *mat2)
+{
+ int i;
+ isl_mat *mat;
+
+ if (!mat1 || !mat2)
+ goto error;
+
+ mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
+ mat1->n_col + mat2->n_col);
+ if (!mat)
+ goto error;
+ for (i = 0; i < mat1->n_row; ++i) {
+ isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
+ isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
+ }
+ for (i = 0; i < mat2->n_row; ++i) {
+ isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
+ isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
+ mat2->row[i], mat2->n_col);
+ }
+ isl_mat_free(mat1);
+ isl_mat_free(mat2);
+ return mat;
+error:
+ isl_mat_free(mat1);
+ isl_mat_free(mat2);
+ return NULL;
}
static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
if (pivot < 0) {
isl_int_clear(a);
isl_int_clear(b);
- goto error;
+ isl_assert(mat->ctx, pivot >= 0, goto error);
}
pivot += row;
if (pivot != row)
return inv;
error:
isl_mat_free(mat);
+ isl_mat_free(inv);
return NULL;
}
if (left->n_col == 0) {
for (i = 0; i < prod->n_row; ++i)
isl_seq_clr(prod->row[i], prod->n_col);
+ isl_mat_free(left);
+ isl_mat_free(right);
return prod;
}
for (i = 0; i < prod->n_row; ++i) {
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_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
t = isl_mat_product(t, mat);
if (!t)
return -1;
isl_assert(ctx, bset->dim->nparam == 0, goto error);
isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
+ isl_assert(ctx, mat->n_col > 0, goto error);
- 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 (mat->n_col > mat->n_row) {
+ bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
+ if (!bset)
+ goto error;
+ } else if (mat->n_col < mat->n_row) {
+ bset->dim = isl_space_cow(bset->dim);
if (!bset->dim)
goto error;
bset->dim->n_out -= mat->n_row - mat->n_col;
goto error;
}
if (mat->n_col != mat->n_row) {
- set->dim = isl_dim_cow(set->dim);
+ set->dim = isl_space_cow(set->dim);
if (!set->dim)
goto error;
set->dim->n_out += mat->n_col;
return NULL;
}
-void isl_mat_dump(struct isl_mat *mat, FILE *out, int indent)
+/* Replace the variables x starting at pos in the rows q
+ * by x' with x = M x' with M the matrix mat.
+ * That is, replace the corresponding coefficients c by c M.
+ */
+static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
+ unsigned pos, __isl_take isl_mat *mat)
+{
+ int i;
+ isl_mat *t;
+
+ t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, 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] + pos, t->row[i], t->n_col);
+ isl_mat_free(t);
+ return 0;
+}
+
+/* Replace the variables x of type "type" starting at "first" in "bset"
+ * by x' with x = M x' with M the matrix trans.
+ * That is, replace the corresponding coefficients c by c M.
+ *
+ * The transformation matrix should be a square matrix.
+ */
+__isl_give isl_basic_set *isl_basic_set_transform_dims(
+ __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
+ __isl_take isl_mat *trans)
+{
+ isl_ctx *ctx;
+ unsigned pos;
+
+ bset = isl_basic_set_cow(bset);
+ if (!bset || !trans)
+ goto error;
+
+ ctx = isl_basic_set_get_ctx(bset);
+ if (trans->n_row != trans->n_col)
+ isl_die(trans->ctx, isl_error_invalid,
+ "expecting square transformation matrix", goto error);
+ if (first + trans->n_row > isl_basic_set_dim(bset, type))
+ isl_die(trans->ctx, isl_error_invalid,
+ "oversized transformation matrix", goto error);
+
+ pos = isl_basic_set_offset(bset, type) + first;
+
+ if (transform(ctx, bset->eq, bset->n_eq, pos, isl_mat_copy(trans)) < 0)
+ goto error;
+ if (transform(ctx, bset->ineq, bset->n_ineq, pos,
+ isl_mat_copy(trans)) < 0)
+ goto error;
+ if (transform(ctx, bset->div, bset->n_div, 1 + pos,
+ isl_mat_copy(trans)) < 0)
+ goto error;
+
+ ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
+ ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
+
+ isl_mat_free(trans);
+ return bset;
+error:
+ isl_mat_free(trans);
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
{
int i, j;
}
}
+void isl_mat_dump(__isl_keep isl_mat *mat)
+{
+ isl_mat_print_internal(mat, stderr, 0);
+}
+
struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
{
int r;
return mat;
}
+__isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
+ unsigned col, unsigned n)
+{
+ isl_mat *ext;
+
+ if (!mat)
+ return NULL;
+ if (n == 0)
+ return mat;
+
+ ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
+ if (!ext)
+ goto error;
+
+ isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
+ isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
+ col + n, col, mat->n_col - col);
+
+ isl_mat_free(mat);
+ return ext;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+__isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
+ unsigned first, unsigned n)
+{
+ int i;
+
+ if (!mat)
+ return NULL;
+ mat = isl_mat_insert_cols(mat, first, n);
+ if (!mat)
+ return NULL;
+
+ for (i = 0; i < mat->n_row; ++i)
+ isl_seq_clr(mat->row[i] + first, n);
+
+ return mat;
+}
+
+__isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
+{
+ if (!mat)
+ return NULL;
+
+ return isl_mat_insert_zero_cols(mat, mat->n_col, n);
+}
+
+__isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
+ unsigned row, unsigned n)
+{
+ isl_mat *ext;
+
+ if (!mat)
+ return NULL;
+ if (n == 0)
+ return mat;
+
+ ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
+ if (!ext)
+ goto error;
+
+ isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
+ isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
+ mat->n_row - row, 0, 0, mat->n_col);
+
+ isl_mat_free(mat);
+ return ext;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+__isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
+{
+ if (!mat)
+ return NULL;
+
+ return isl_mat_insert_rows(mat, mat->n_row, n);
+}
+
+__isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
+ unsigned row, unsigned n)
+{
+ int i;
+
+ mat = isl_mat_insert_rows(mat, row, n);
+ if (!mat)
+ return NULL;
+
+ for (i = 0; i < n; ++i)
+ isl_seq_clr(mat->row[row + i], mat->n_col);
+
+ return mat;
+}
+
+__isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
+{
+ if (!mat)
+ return NULL;
+
+ return isl_mat_insert_zero_rows(mat, mat->n_row, n);
+}
+
void isl_mat_col_submul(struct isl_mat *mat,
int dst_col, isl_int f, int src_col)
{
isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
}
+void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
+{
+ int i;
+
+ if (!mat)
+ return;
+
+ for (i = 0; i < mat->n_row; ++i)
+ isl_int_add(mat->row[i][dst_col],
+ mat->row[i][dst_col], 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;
isl_mat_free(M);
return NULL;
}
+
+__isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
+ __isl_take isl_mat *bot)
+{
+ struct isl_mat *mat;
+
+ if (!top || !bot)
+ goto error;
+
+ isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
+ if (top->n_row == 0) {
+ isl_mat_free(top);
+ return bot;
+ }
+ if (bot->n_row == 0) {
+ isl_mat_free(bot);
+ return top;
+ }
+
+ mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
+ if (!mat)
+ goto error;
+ isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
+ 0, 0, mat->n_col);
+ isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
+ 0, 0, mat->n_col);
+ isl_mat_free(top);
+ isl_mat_free(bot);
+ return mat;
+error:
+ isl_mat_free(top);
+ isl_mat_free(bot);
+ return NULL;
+}
+
+int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
+{
+ int i;
+
+ if (!mat1 || !mat2)
+ return -1;
+
+ if (mat1->n_row != mat2->n_row)
+ return 0;
+
+ if (mat1->n_col != mat2->n_col)
+ return 0;
+
+ for (i = 0; i < mat1->n_row; ++i)
+ if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
+ return 0;
+
+ return 1;
+}
+
+__isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
+{
+ struct isl_mat *mat;
+
+ if (!vec)
+ return NULL;
+ mat = isl_mat_alloc(vec->ctx, 1, vec->size);
+ if (!mat)
+ goto error;
+
+ isl_seq_cpy(mat->row[0], vec->el, vec->size);
+
+ isl_vec_free(vec);
+ return mat;
+error:
+ isl_vec_free(vec);
+ return NULL;
+}
+
+__isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
+ __isl_take isl_vec *bot)
+{
+ return isl_mat_concat(top, isl_mat_from_row_vec(bot));
+}
+
+__isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
+ unsigned dst_col, unsigned src_col, unsigned n)
+{
+ isl_mat *res;
+
+ if (!mat)
+ return NULL;
+ if (n == 0 || dst_col == src_col)
+ return mat;
+
+ res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
+ if (!res)
+ goto error;
+
+ if (dst_col < src_col) {
+ isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+ 0, 0, dst_col);
+ isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+ dst_col, src_col, n);
+ isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+ dst_col + n, dst_col, src_col - dst_col);
+ isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+ src_col + n, src_col + n,
+ res->n_col - src_col - n);
+ } else {
+ isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+ 0, 0, src_col);
+ isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+ src_col, src_col + n, dst_col - src_col);
+ isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+ dst_col, src_col, n);
+ isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
+ dst_col + n, dst_col + n,
+ res->n_col - dst_col - n);
+ }
+ isl_mat_free(mat);
+
+ return res;
+error:
+ isl_mat_free(mat);
+ return NULL;
+}
+
+void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
+{
+ int i;
+ isl_int g;
+
+ isl_int_set_si(*gcd, 0);
+ if (!mat)
+ return;
+
+ isl_int_init(g);
+ for (i = 0; i < mat->n_row; ++i) {
+ isl_seq_gcd(mat->row[i], mat->n_col, &g);
+ isl_int_gcd(*gcd, *gcd, g);
+ }
+ isl_int_clear(g);
+}
+
+__isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
+{
+ int i;
+
+ if (isl_int_is_one(m))
+ return mat;
+
+ mat = isl_mat_cow(mat);
+ if (!mat)
+ return NULL;
+
+ for (i = 0; i < mat->n_row; ++i)
+ isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
+
+ return mat;
+}
+
+__isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
+ isl_int m)
+{
+ if (isl_int_is_one(m))
+ return mat;
+
+ mat = isl_mat_cow(mat);
+ if (!mat)
+ return NULL;
+
+ isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
+
+ return mat;
+}
+
+__isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
+{
+ isl_int gcd;
+
+ if (!mat)
+ return NULL;
+
+ isl_int_init(gcd);
+ isl_mat_gcd(mat, &gcd);
+ mat = isl_mat_scale_down(mat, gcd);
+ isl_int_clear(gcd);
+
+ return mat;
+}
+
+__isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
+{
+ mat = isl_mat_cow(mat);
+ if (!mat)
+ return NULL;
+
+ isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
+
+ return mat;
+}
+
+/* Number of initial non-zero columns.
+ */
+int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
+{
+ int i;
+
+ if (!mat)
+ return -1;
+
+ for (i = 0; i < mat->n_col; ++i)
+ if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
+ break;
+
+ return i;
+}