X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=isl_mat.c;h=129cdc749eb81f10d549d6e46f01aaff2c67d42b;hb=63fb8a7f484648c3caa25351c8c94ac2395ec563;hp=1c56388bcc0c4f25d9aa95b657dc73a806b95917;hpb=4efd438e0e0434a64d442e009ca7570874056fc3;p=platform%2Fupstream%2Fisl.git diff --git a/isl_mat.c b/isl_mat.c index 1c56388..129cdc7 100644 --- a/isl_mat.c +++ b/isl_mat.c @@ -1,17 +1,24 @@ /* * Copyright 2008-2009 Katholieke Universiteit Leuven * - * Use of this software is governed by the GNU LGPLv2.1 license + * Use of this software is governed by the MIT license * * Written by Sven Verdoolaege, K.U.Leuven, Departement * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium */ -#include "isl_dim.h" -#include "isl_seq.h" -#include "isl_mat.h" -#include "isl_map_private.h" -#include +#include +#include +#include +#include +#include +#include +#include + +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) @@ -54,6 +61,7 @@ struct isl_mat *isl_mat_extend(struct isl_mat *mat, { int i; isl_int *old; + isl_int **row; if (!mat) return NULL; @@ -86,9 +94,10 @@ struct isl_mat *isl_mat_extend(struct isl_mat *mat, 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); @@ -104,7 +113,7 @@ error: 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; @@ -131,6 +140,15 @@ error: 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) { @@ -187,22 +205,123 @@ struct isl_mat *isl_mat_cow(struct isl_mat *mat) return mat2; } -void isl_mat_free(struct isl_mat *mat) +void *isl_mat_free(struct isl_mat *mat) { if (!mat) - return; + return NULL; if (--mat->ref > 0) - return; + return NULL; 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); + + return NULL; +} + +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; +} + +/* Extract the element at row "row", oolumn "col" of "mat". + */ +__isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat, + int row, int col) +{ + isl_ctx *ctx; + + if (!mat) + return NULL; + ctx = isl_mat_get_ctx(mat); + if (row < 0 || row >= mat->n_row) + isl_die(ctx, isl_error_invalid, "row out of range", + return NULL); + if (col < 0 || col >= mat->n_col) + isl_die(ctx, isl_error_invalid, "column out of range", + return NULL); + return isl_val_int_from_isl_int(ctx, mat->row[row][col]); +} + +__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; } -struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row) +__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; +} + +/* Replace the element at row "row", column "col" of "mat" by "v". + */ +__isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat, + int row, int col, __isl_take isl_val *v) +{ + if (!v) + return isl_mat_free(mat); + if (!isl_val_is_int(v)) + isl_die(isl_val_get_ctx(v), isl_error_invalid, + "expecting integer value", goto error); + mat = isl_mat_set_element(mat, row, col, v->n); + isl_val_free(v); + return mat; +error: + isl_val_free(v); + return isl_mat_free(mat); +} + +__isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d) { int i; struct isl_mat *mat; @@ -212,13 +331,20 @@ struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row) 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; @@ -482,6 +608,7 @@ error: isl_mat_free(*U); *U = NULL; } + isl_mat_free(M); return NULL; } @@ -813,6 +940,7 @@ struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat) return inv; error: isl_mat_free(mat); + isl_mat_free(inv); return NULL; } @@ -821,6 +949,9 @@ struct isl_mat *isl_mat_transpose(struct isl_mat *mat) struct isl_mat *transpose = NULL; int i, j; + if (!mat) + return NULL; + if (mat->n_col == mat->n_row) { mat = isl_mat_cow(mat); if (!mat) @@ -876,7 +1007,8 @@ struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j) return mat; } -struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right) +__isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left, + __isl_take isl_mat *right) { int i, j, k; struct isl_mat *prod; @@ -890,6 +1022,8 @@ struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right) 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) { @@ -935,7 +1069,7 @@ static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n, 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; @@ -981,7 +1115,7 @@ struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset, if (!bset) goto error; } else if (mat->n_col < mat->n_row) { - bset->dim = isl_dim_cow(bset->dim); + bset->dim = isl_space_cow(bset->dim); if (!bset->dim) goto error; bset->dim->n_out -= mat->n_row - mat->n_col; @@ -1032,7 +1166,7 @@ struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat) 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; @@ -1047,7 +1181,74 @@ error: 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; @@ -1076,10 +1277,18 @@ void isl_mat_dump(struct isl_mat *mat, FILE *out, int indent) } } +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; + if (n == 0) + return mat; + mat = isl_mat_cow(mat); if (!mat) return NULL; @@ -1191,6 +1400,29 @@ __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n) 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) { @@ -1200,6 +1432,18 @@ void isl_mat_col_submul(struct isl_mat *mat, 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; @@ -1379,6 +1623,10 @@ __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; @@ -1388,6 +1636,21 @@ __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m) 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; @@ -1402,3 +1665,30 @@ __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat) 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; +}