1 /* latte_cddlib.cpp -- Interface to cddlib
2
3 Copyright 2006, 2007 Matthias Koeppe
4
5 This file is part of LattE.
6
7 LattE is free software; you can redistribute it and/or modify it
8 under the terms of the version 2 of the GNU General Public License
9 as published by the Free Software Foundation.
10
11 LattE is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with LattE; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21 #include <cassert>
22
23 #include "latte_cddlib.h"
24 #include "latte_gmp.h"
25
init_cddlib_class()26 init_cddlib_class::init_cddlib_class()
27 {
28 dd_set_global_constants();
29 }
30
31 init_cddlib_class init_cddlib;
32
check_cddlib_error(dd_ErrorType error,const char * proc)33 void check_cddlib_error(dd_ErrorType error, const char *proc)
34 {
35 if (error != dd_NoError) {
36 cerr << "CDDLIB error in " << proc << ": " << endl;
37 dd_WriteErrorMessages(stderr, error);
38 exit(1);
39 }
40 }
41
42 dd_MatrixPtr
rays_to_cddlib_matrix(listVector * rays,int numOfVars,int num_homogenization_vars,int num_extra_rows)43 rays_to_cddlib_matrix(listVector *rays, int numOfVars,
44 int num_homogenization_vars,
45 int num_extra_rows)
46 {
47 dd_set_global_constants();
48 int num_rays = lengthListVector(rays);
49 int numOfVars_hom = numOfVars + num_homogenization_vars;
50 dd_MatrixPtr matrix = dd_CreateMatrix(num_rays + num_extra_rows, numOfVars_hom);
51 matrix->numbtype = dd_Rational;
52 matrix->representation = dd_Generator;
53 int i, j;
54 listVector *ray;
55 mpq_class x;
56 for (i = 0, ray = rays; i<num_rays; i++, ray = ray->rest) {
57 for (j = 0; j<numOfVars; j++) {
58 x = convert_ZZ_to_mpq(ray->first[j]);
59 dd_set(matrix->matrix[i][j + num_homogenization_vars], x.get_mpq_t());
60 }
61 }
62 return matrix;
63 }
64
65 dd_PolyhedraPtr
cone_to_cddlib_polyhedron(listCone * cone,int numOfVars)66 cone_to_cddlib_polyhedron(listCone *cone, int numOfVars)
67 {
68 dd_MatrixPtr matrix = rays_to_cddlib_matrix(cone->rays, numOfVars);
69 dd_ErrorType error;
70 dd_PolyhedraPtr poly = dd_DDMatrix2Poly(matrix, &error);
71 check_cddlib_error(error, "cone_to_cddlib_polyhedron");
72 return poly;
73 }
74
75 listCone *
cddlib_matrix_to_cone(dd_MatrixPtr matrix)76 cddlib_matrix_to_cone(dd_MatrixPtr matrix)
77 {
78 int numOfVars = matrix->colsize - 1;
79 assert(matrix->representation == dd_Generator);
80 listCone *result = createListCone();
81 result->vertex = new Vertex(new rationalVector(numOfVars));
82 int i;
83 for (i = matrix->rowsize - 1; i>=0; i--) {
84 vec_ZZ ray;
85 ray.SetLength(numOfVars);
86 int j;
87 {
88 /* Check generator is homogeneous */
89 mpq_class x(matrix->matrix[i][0]);
90 assert(x == 0);
91 }
92 for (j = 0; j<numOfVars; j++) {
93 ray[j] = convert_mpq_to_ZZ(matrix->matrix[i][j + 1]);
94 }
95 result->rays = new listVector(ray, result->rays);
96 }
97 return result;
98 }
99
100 void
cddlib_matrix_to_equations_and_inequalities(dd_MatrixPtr matrix,listVector ** equations,listVector ** inequalities)101 cddlib_matrix_to_equations_and_inequalities(dd_MatrixPtr matrix,
102 listVector **equations,
103 listVector **inequalities)
104 {
105 assert(matrix->representation == dd_Inequality);
106 int numOfVars = matrix->colsize - 1;
107 *equations = NULL;
108 *inequalities = NULL;
109 int i;
110 for (i = matrix->rowsize - 1; i>=0; i--) {
111 vec_ZZ ineq;
112 ineq.SetLength(numOfVars + 1);
113 int j;
114 for (j = 0; j<=numOfVars; j++) {
115 ineq[j] = convert_mpq_to_ZZ(matrix->matrix[i][j]);
116 }
117 if (set_member(i + 1, matrix->linset))
118 (*equations) = new listVector(ineq, *equations);
119 else
120 (*inequalities) = new listVector(ineq, *inequalities);
121 }
122 }
123
124