File: VertexConesWith4ti2.cpp

package info (click to toggle)
latte-int 1.7.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 38,260 kB
  • sloc: cpp: 32,231; sh: 4,413; makefile: 811; perl: 300
file content (169 lines) | stat: -rw-r--r-- 5,033 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* VertexConesWith4ti2.cpp -- Compute vertex cones with 4ti2
	       
   Copyright 2007 Matthias Koeppe

   This file is part of LattE.
   
   LattE is free software; you can redistribute it and/or modify it
   under the terms of the version 2 of the GNU General Public License
   as published by the Free Software Foundation.

   LattE is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with LattE; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

// From 4ti2:
#include "groebner/BitSet.h"
#include "groebner/VectorArrayStream.h"
#include "groebner/LatticeBasis.h"
#include "groebner/RayAlgorithm.h"

#include "latte_gmp.h"
#include "latte_4ti2.h"
#include "VertexConesWith4ti2.h"
#include "dual.h"
#include "DualizationWith4ti2.h"

#include "print.h"

using namespace _4ti2_;

void
computeVertexConesWith4ti2(listVector* ineqs, int numOfVars,
			   bool &unbounded,
			   ConeConsumer &consumer)
{
  unbounded = false;
  listCone *cones = NULL;
  int num_ineqs = lengthListVector(ineqs);
  /* Create a matrix from the facets, with extra coordinates
     at the front for slack variables.  The 1 is related to the fact
     that the vectors of MATRIX already have 1 + numOfVars entries.
  */
  int lifted_dim = 1 + numOfVars + num_ineqs;
  BitSet *rs = new BitSet(lifted_dim);
  VectorArray *matrix
    = rays_to_4ti2_VectorArray(ineqs, 1 + numOfVars,
			       /* num_homogenization_vars: */ num_ineqs,
			       /* num_extra_rows: */ 0);
  /* Add negative identity matrix for the slack variables. */
  {
    int i;
    for (i = 0; i<num_ineqs; i++) {
      (*matrix)[i][i] = -1;
      rs->set(i);
    }
  }
  /* Make the homogenization coordinate (corresponding to the right-hand side) non-negative */
  rs->set(num_ineqs);

#if 0
  {
    std::ofstream file("cone_for_4ti2_vertex_cones_computation");
    file << matrix->get_number() << " " << lifted_dim << endl;
    print(file, *matrix, 0, lifted_dim);
    cerr << "Created file `cone_for_4ti2_vertex_cones_computation'" << endl;
  }
#endif
  
  VectorArray *rays = new VectorArray(0, matrix->get_size());
  lattice_basis(*matrix, *rays);
  VectorArray* subspace = new VectorArray(0, matrix->get_size());
  RayAlgorithm algorithm;
  algorithm.compute(*matrix, *rays, *subspace, *rs);
  delete rs;

#if 0
  {
    std::ofstream file("4ti2_vertex_cones_computation_output");
    file << rays->get_number() << " " << lifted_dim << "\n";
    print(file, *rays, 0, lifted_dim);
    cerr << "Created file `4ti2_vertex_cones_computation_output'" << endl;
  }
#endif
  
  assert(subspace->get_number() == 0); /* We assume polytopes,
					  thus a pointed
					  homogenization */
  delete matrix;
  delete subspace;
  
  int num_rays = rays->get_number();
  
  /* Walk through all rays of the homogenization; each gives a vertex
     of the polytope. */
  int i;
  for (i = 0; i<num_rays; i++) {
    ZZ denominator = convert_mpz_to_ZZ((*rays)[i][num_ineqs]);
    if (denominator == 0) {
      /* This ray corresponds to a ray of the (unbounded!) polyhedron;
	 ignore it. */
      unbounded = true;
    }
    else {
      listCone *cone = createListCone();
      vec_ZZ numerator;
      numerator.SetLength(numOfVars);
      int j;
      for (j = 0; j<numOfVars; j++)
	numerator[j] = convert_mpz_to_ZZ((*rays)[i][num_ineqs + 1 + j]);
      rationalVector *vertex_vector = new rationalVector(numerator, denominator);
      cone->vertex = new Vertex(vertex_vector);

      /* Compute the facets: */
      /* Find incident facets.
	 They are the facets whose corresponding slack variables are
	 zero. */
      listVector *ineq;
      for (j = 0, ineq = ineqs; j<num_ineqs; j++, ineq = ineq->rest) {
	if ((*rays)[i][j] == 0) {
	  /* Incident! */
	  vec_ZZ facet_vector;
	  facet_vector.SetLength(numOfVars);
	  int k;
	  for (k = 0; k<numOfVars; k++)
	    facet_vector[k] = - ineq->first[k + 1];
	  /* Cancel GCD: */
	  ZZ gcd;
	  for (k = 0; k<numOfVars; k++)
	    gcd = GCD(gcd, facet_vector[k]);
	  if (gcd != 0 && gcd != 1) {
	    for (k = 0; k<numOfVars; k++)
	      facet_vector[k] /= gcd;
	  }
	  cone->facets = new listVector(facet_vector, cone->facets);
	}
      }

      /* FIXME: To cheaply compute the rays of the vertex cone, we need
	 to get hold of the adjacent rays of the current ray of the
	 homogenization.

	 For the moment, don't compute the rays.  Later code will construct
	 them when needed.
      */

#if 0
      printCone(cone, numOfVars);
#endif
      
      consumer.ConsumeCone(cone);
    }
  }
  delete rays;
}

listCone *
computeVertexConesWith4ti2(listVector* matrix, int numOfVars,
			   bool &unbounded)
{
  CollectingConeConsumer ccc;
  computeVertexConesWith4ti2(matrix, numOfVars, unbounded, ccc);
  return ccc.Collected_Cones;
}