From 093abd9827a83a22e2f06ff1e0f6f14d8ed9245c Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Sun, 15 Aug 2010 15:06:26 +0200 Subject: [PATCH] add public API for matrices Signed-off-by: Sven Verdoolaege --- Makefile.am | 1 + doc/user.pod | 36 ++++++++++++++++++++++++++++++++++++ include/isl_mat.h | 35 ++++++++++++----------------------- isl_affine_hull.c | 1 + isl_basis_reduction.h | 2 +- isl_coalesce.c | 1 + isl_convex_hull.c | 2 +- isl_equalities.c | 2 +- isl_factorization.c | 1 + isl_fold.c | 1 + isl_input.c | 1 + isl_map.c | 1 + isl_map_simplify.c | 1 + isl_mat.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- isl_mat_private.h | 20 ++++++++++++++++++++ isl_morph.c | 1 + isl_output.c | 1 + isl_polynomial.c | 1 + isl_tab.c | 2 +- isl_tab_pip.c | 1 + isl_transitive_closure.c | 1 + isl_vertices.c | 1 + polytope_scan.c | 1 + 23 files changed, 131 insertions(+), 28 deletions(-) create mode 100644 isl_mat_private.h diff --git a/Makefile.am b/Makefile.am index e9eb8a3..12c7e2d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -66,6 +66,7 @@ libisl_la_SOURCES = \ isl_map_private.h \ isl_map_piplib.h \ isl_mat.c \ + isl_mat_private.h \ isl_morph.c \ isl_morph.h \ isl_name.c \ diff --git a/doc/user.pod b/doc/user.pod index b49d43d..cbe08e3 100644 --- a/doc/user.pod +++ b/doc/user.pod @@ -1423,6 +1423,42 @@ In case of union relations, the optimum is computed per space. __isl_give isl_union_map *isl_union_map_lexmax( __isl_take isl_union_map *umap); +=head2 Matrices + +Matrices can be created, copied and freed using the following functions. + + #include + __isl_give isl_mat *isl_mat_alloc(struct isl_ctx *ctx, + unsigned n_row, unsigned n_col); + __isl_give isl_mat *isl_mat_copy(__isl_keep isl_mat *mat); + void isl_mat_free(__isl_take isl_mat *mat); + +Note that the elements of a newly created matrix may have arbitrary values. +The elements can be changed and inspected using the following functions. + + int isl_mat_rows(__isl_keep isl_mat *mat); + int isl_mat_cols(__isl_keep isl_mat *mat); + int isl_mat_get_element(__isl_keep isl_mat *mat, + int row, int col, isl_int *v); + __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat, + int row, int col, isl_int v); + +C will return a negative value if anything went wrong. +In that case, the value of C<*v> is undefined. + +The following function can be used to compute the (right) inverse +of a matrix, i.e., a matrix such that the product of the original +and the inverse (in that order) is a multiple of the identity matrix. +The input matrix is assumed to be of full row-rank. + + __isl_give isl_mat *isl_mat_right_inverse(__isl_take isl_mat *mat); + +The following function can be used to compute the (right) kernel +(or null space) of a matrix, i.e., a matrix such that the product of +the original and the kernel (in that order) is the zero matrix. + + __isl_give isl_mat *isl_mat_right_kernel(__isl_take isl_mat *mat); + =head2 Points Points are elements of a set. They can be used to construct diff --git a/include/isl_mat.h b/include/isl_mat.h index 4f81b71..8536df1 100644 --- a/include/isl_mat.h +++ b/include/isl_mat.h @@ -21,35 +21,24 @@ extern "C" { #endif -struct isl_mat { - int ref; - - struct isl_ctx *ctx; - -#define ISL_MAT_BORROWED (1 << 0) - unsigned flags; - - unsigned n_row; - unsigned n_col; - - isl_int **row; - - /* actual size of the rows in memory; n_col <= max_col */ - unsigned max_col; - - struct isl_blk block; -}; +struct isl_mat; typedef struct isl_mat isl_mat; -struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx, +__isl_give isl_mat *isl_mat_alloc(struct isl_ctx *ctx, unsigned n_row, unsigned n_col); struct isl_mat *isl_mat_dup(struct isl_mat *mat); struct isl_mat *isl_mat_extend(struct isl_mat *mat, unsigned n_row, unsigned n_col); struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row); -struct isl_mat *isl_mat_copy(struct isl_mat *mat); +__isl_give isl_mat *isl_mat_copy(__isl_keep isl_mat *mat); struct isl_mat *isl_mat_cow(struct isl_mat *mat); -void isl_mat_free(struct isl_mat *mat); +void isl_mat_free(__isl_take isl_mat *mat); + +int isl_mat_rows(__isl_keep isl_mat *mat); +int isl_mat_cols(__isl_keep isl_mat *mat); +int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v); +__isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat, + int row, int col, isl_int v); 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); @@ -76,8 +65,8 @@ struct isl_mat *isl_mat_inverse_product(struct isl_mat *left, struct isl_mat *right); struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right); struct isl_mat *isl_mat_transpose(struct isl_mat *mat); -struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat); -struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat); +__isl_give isl_mat *isl_mat_right_inverse(__isl_take isl_mat *mat); +__isl_give isl_mat *isl_mat_right_kernel(__isl_take isl_mat *mat); __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat); diff --git a/isl_affine_hull.c b/isl_affine_hull.c index dcb3d18..894670b 100644 --- a/isl_affine_hull.c +++ b/isl_affine_hull.c @@ -16,6 +16,7 @@ #include "isl_equalities.h" #include "isl_sample.h" #include "isl_tab.h" +#include struct isl_basic_map *isl_basic_map_implicit_equalities( struct isl_basic_map *bmap) diff --git a/isl_basis_reduction.h b/isl_basis_reduction.h index 8a00fb0..e9eb760 100644 --- a/isl_basis_reduction.h +++ b/isl_basis_reduction.h @@ -11,7 +11,7 @@ #define ISL_BASIS_REDUCTION_H #include "isl_set.h" -#include "isl_mat.h" +#include #include "isl_tab.h" #if defined(__cplusplus) diff --git a/isl_coalesce.c b/isl_coalesce.c index 86c3715..dbd2a7e 100644 --- a/isl_coalesce.c +++ b/isl_coalesce.c @@ -13,6 +13,7 @@ #include "isl_map_private.h" #include "isl_seq.h" #include "isl_tab.h" +#include #define STATUS_ERROR -1 #define STATUS_REDUNDANT 1 diff --git a/isl_convex_hull.c b/isl_convex_hull.c index d267747..b355cda 100644 --- a/isl_convex_hull.c +++ b/isl_convex_hull.c @@ -10,7 +10,7 @@ #include "isl_lp.h" #include "isl_map.h" #include "isl_map_private.h" -#include "isl_mat.h" +#include #include "isl_set.h" #include "isl_seq.h" #include "isl_equalities.h" diff --git a/isl_equalities.c b/isl_equalities.c index 60c5e94..8c36dbc 100644 --- a/isl_equalities.c +++ b/isl_equalities.c @@ -7,7 +7,7 @@ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium */ -#include "isl_mat.h" +#include #include "isl_seq.h" #include "isl_map_private.h" #include "isl_equalities.h" diff --git a/isl_factorization.c b/isl_factorization.c index 379222b..3b32e19 100644 --- a/isl_factorization.c +++ b/isl_factorization.c @@ -15,6 +15,7 @@ #include #include +#include static __isl_give isl_factorizer *isl_factorizer_alloc( __isl_take isl_morph *morph, int n_group) diff --git a/isl_fold.c b/isl_fold.c index 803a769..fb841f5 100644 --- a/isl_fold.c +++ b/isl_fold.c @@ -15,6 +15,7 @@ #include #include #include +#include static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc( enum isl_fold type, __isl_take isl_dim *dim, int n) diff --git a/isl_input.c b/isl_input.c index 4ac4416..4c5e6cf 100644 --- a/isl_input.c +++ b/isl_input.c @@ -22,6 +22,7 @@ #include "isl_obj.h" #include "isl_polynomial_private.h" #include +#include struct variable { char *name; diff --git a/isl_map.c b/isl_map.c index c6eeaf8..6b934f6 100644 --- a/isl_map.c +++ b/isl_map.c @@ -27,6 +27,7 @@ #include "isl_sample.h" #include "isl_tab.h" #include "isl_vec.h" +#include /* Maps dst positions to src positions */ struct isl_dim_map { diff --git a/isl_map_simplify.c b/isl_map_simplify.c index 767ad22..d862f9a 100644 --- a/isl_map_simplify.c +++ b/isl_map_simplify.c @@ -13,6 +13,7 @@ #include "isl_seq.h" #include "isl_tab.h" #include +#include static void swap_equality(struct isl_basic_map *bmap, int a, int b) { diff --git a/isl_mat.c b/isl_mat.c index 1c56388..f425359 100644 --- a/isl_mat.c +++ b/isl_mat.c @@ -9,7 +9,7 @@ #include "isl_dim.h" #include "isl_seq.h" -#include "isl_mat.h" +#include #include "isl_map_private.h" #include @@ -202,6 +202,49 @@ void isl_mat_free(struct isl_mat *mat) free(mat); } +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; +} + struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row) { int i; diff --git a/isl_mat_private.h b/isl_mat_private.h new file mode 100644 index 0000000..ef7474e --- /dev/null +++ b/isl_mat_private.h @@ -0,0 +1,20 @@ +#include + +struct isl_mat { + int ref; + + struct isl_ctx *ctx; + +#define ISL_MAT_BORROWED (1 << 0) + unsigned flags; + + unsigned n_row; + unsigned n_col; + + isl_int **row; + + /* actual size of the rows in memory; n_col <= max_col */ + unsigned max_col; + + struct isl_blk block; +}; diff --git a/isl_morph.c b/isl_morph.c index 20a4063..8bc4db8 100644 --- a/isl_morph.c +++ b/isl_morph.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include diff --git a/isl_output.c b/isl_output.c index e04fbc7..fc70284 100644 --- a/isl_output.c +++ b/isl_output.c @@ -18,6 +18,7 @@ #include #include #include +#include #include static const char *s_to[2] = { " -> ", " \\to " }; diff --git a/isl_polynomial.c b/isl_polynomial.c index a8dfc20..2d52894 100644 --- a/isl_polynomial.c +++ b/isl_polynomial.c @@ -16,6 +16,7 @@ #include #include #include +#include static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type) { diff --git a/isl_tab.c b/isl_tab.c index b8a80be..7a71ce6 100644 --- a/isl_tab.c +++ b/isl_tab.c @@ -7,7 +7,7 @@ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium */ -#include "isl_mat.h" +#include #include "isl_map_private.h" #include "isl_tab.h" #include "isl_seq.h" diff --git a/isl_tab_pip.c b/isl_tab_pip.c index 6fc6459..1cbc013 100644 --- a/isl_tab_pip.c +++ b/isl_tab_pip.c @@ -11,6 +11,7 @@ #include "isl_seq.h" #include "isl_tab.h" #include "isl_sample.h" +#include /* * The implementation of parametric integer linear programming in this file diff --git a/isl_transitive_closure.c b/isl_transitive_closure.c index 049a2f4..5b97a07 100644 --- a/isl_transitive_closure.c +++ b/isl_transitive_closure.c @@ -14,6 +14,7 @@ #include #include #include +#include int isl_map_is_transitively_closed(__isl_keep isl_map *map) { diff --git a/isl_vertices.c b/isl_vertices.c index 5d2b718..4abd3ae 100644 --- a/isl_vertices.c +++ b/isl_vertices.c @@ -15,6 +15,7 @@ #include #include #include +#include #define SELECTED 1 #define DESELECTED -1 diff --git a/polytope_scan.c b/polytope_scan.c index fbd80ae..269bd81 100644 --- a/polytope_scan.c +++ b/polytope_scan.c @@ -11,6 +11,7 @@ #include "isl_equalities.h" #include "isl_seq.h" #include "isl_scan.h" +#include /* The input of this program is the same as that of the "polytope_scan" * program from the barvinok distribution. -- 2.7.4