test box and bernoulli summation algorithms
[barvinok.git] / isl_aff_polylib.c
blobf5677688e75a0e37299694a1fdbbf5112f0daaf8
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 Ecole Normale Superieure
5 * Use of this software is governed by the GNU GPLv2+ licenses
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 #include <isl/val_gmp.h>
13 #include "isl_aff_polylib.h"
15 /* Fill in row "row" of "M" from "aff".
16 * The input is assumed not to involve any integer divisions.
18 * The order of the coefficients in the rows of "M" is
20 * input-coefficients parameter-coefficients constant-term denominator
22 static Matrix *add_row(Matrix *M, int row, __isl_take isl_aff *aff)
24 enum isl_dim_type types[] = { isl_dim_in, isl_dim_param };
25 int i, j, k;
26 isl_val *d, *v;
28 if (!M || !aff)
29 goto error;
31 if (isl_aff_dim(aff, isl_dim_div) != 0)
32 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
33 "cannot handle integer divisions", goto error);
35 d = isl_aff_get_denominator_val(aff);
36 aff = isl_aff_scale_val(aff, isl_val_copy(d));
38 k = 0;
39 for (i = 0; i < 2; ++i) {
40 int n = isl_aff_dim(aff, types[i]);
41 for (j = 0; j < n; ++j) {
42 v = isl_aff_get_coefficient_val(aff, types[i], j);
43 isl_val_get_num_gmp(v, M->p[row][k++]);
44 isl_val_free(v);
48 v = isl_aff_get_constant_val(aff);
49 isl_val_get_num_gmp(v, M->p[row][k++]);
50 isl_val_free(v);
52 isl_val_get_num_gmp(d, M->p[row][k++]);
53 isl_val_free(d);
55 isl_aff_free(aff);
57 return M;
58 error:
59 isl_aff_free(aff);
60 if (M)
61 Matrix_Free(M);
62 return NULL;
65 /* Convert the multi-affine expression "ma" to a PolyLib Matrix.
66 * The input is assumed not to involve any integer divisions.
68 * The order of the coefficients in the rows of the Matrix is
70 * input-coefficients parameter-coefficients constant-term denominator
72 Matrix *isl_multi_aff_to_polylib(__isl_keep isl_multi_aff *ma)
74 Matrix *M;
75 unsigned nparam, n_in, n_out;
76 int i;
78 if (!ma)
79 return NULL;
81 nparam = isl_multi_aff_dim(ma, isl_dim_param);
82 n_in = isl_multi_aff_dim(ma, isl_dim_in);
83 n_out = isl_multi_aff_dim(ma, isl_dim_out);
84 M = Matrix_Alloc(n_out, n_in + nparam + 1 + 1);
85 for (i = 0; i < n_out; ++i)
86 M = add_row(M, i, isl_multi_aff_get_aff(ma, i));
88 return M;