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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/* Constraint class implementation (non-inline functions).
Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
Copyright (C) 2010-2011 BUGSENG srl (http://bugseng.com)
This file is part of the Parma Polyhedra Library (PPL).
The PPL is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The PPL 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 this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
For the most up-to-date information see the Parma Polyhedra Library
site: http://www.cs.unipr.it/ppl/ . */
#include <ppl-config.h>
#include "Constraint.defs.hh"
#include "Variable.defs.hh"
#include "Congruence.defs.hh"
#include <iostream>
#include <sstream>
#include <stdexcept>
namespace PPL = Parma_Polyhedra_Library;
void
PPL::Constraint::throw_invalid_argument(const char* method,
const char* message) const {
std::ostringstream s;
s << "PPL::Constraint::" << method << ":" << std::endl
<< message;
throw std::invalid_argument(s.str());
}
void
PPL::Constraint::throw_dimension_incompatible(const char* method,
const char* name_var,
const Variable v) const {
std::ostringstream s;
s << "PPL::Constraint::" << method << ":" << std::endl
<< "this->space_dimension() == " << space_dimension() << ", "
<< name_var << ".space_dimension() == " << v.space_dimension() << ".";
throw std::invalid_argument(s.str());
}
PPL::Constraint
PPL::Constraint::construct_epsilon_geq_zero() {
Linear_Expression e = Variable(0);
Constraint c(e, NONSTRICT_INEQUALITY, NOT_NECESSARILY_CLOSED);
return c;
}
PPL::Constraint::Constraint(const Congruence& cg)
: Linear_Row(cg.is_equality()
// Size includes extra column for the inhomogeneous term.
? cg.space_dimension() + 1
: (throw_invalid_argument("Constraint(cg)",
"congruence cg must be an equality."),
0),
// Capacity also includes a column for the epsilon coefficient.
compute_capacity(cg.space_dimension() + 2, Row::max_size()),
Flags(NECESSARILY_CLOSED, LINE_OR_EQUALITY)) {
Constraint& c = *this;
// Copy coefficients and inhomogeneous term.
for (dimension_type i = cg.space_dimension() + 1; i-- > 0; )
c[i] = cg[i];
// Enforce normalization.
strong_normalize();
}
PPL::Constraint::Constraint(const Congruence& cg,
dimension_type sz,
dimension_type capacity)
: Linear_Row(cg.is_equality()
? sz
: (throw_invalid_argument("Constraint(cg, sz, c)",
"congruence cg must be an equality."),
0),
capacity,
Flags(NECESSARILY_CLOSED, LINE_OR_EQUALITY)) {
Constraint& c = *this;
// Copy coefficients.
PPL_ASSERT(sz > 0);
while (sz-- > 0)
c[sz] = cg[sz];
}
bool
PPL::Constraint::is_tautological() const {
PPL_ASSERT(size() > 0);
const Constraint& x = *this;
if (x.all_homogeneous_terms_are_zero())
if (is_equality())
return x[0] == 0;
else
// Non-strict inequality constraint.
return x[0] >= 0;
else
// There is a non-zero homogeneous coefficient.
if (is_necessarily_closed())
return false;
else {
// The constraint is NOT necessarily closed.
const dimension_type eps_index = size() - 1;
const int eps_sign = sgn(x[eps_index]);
if (eps_sign > 0)
// We have found the constraint epsilon >= 0.
return true;
if (eps_sign == 0)
// One of the `true' dimensions has a non-zero coefficient.
return false;
else {
// Here the epsilon coefficient is negative: strict inequality.
if (x[0] <= 0)
// A strict inequality such as `lhs - k > 0',
// where k is a non negative integer, cannot be trivially true.
return false;
// Checking for another non-zero coefficient.
for (dimension_type i = eps_index; --i > 0; )
if (x[i] != 0)
return false;
// We have the inequality `k > 0',
// where k is a positive integer.
return true;
}
}
}
bool
PPL::Constraint::is_inconsistent() const {
PPL_ASSERT(size() > 0);
const Constraint& x = *this;
if (x.all_homogeneous_terms_are_zero())
// The inhomogeneous term is the only non-zero coefficient.
if (is_equality())
return x[0] != 0;
else
// Non-strict inequality constraint.
return x[0] < 0;
else
// There is a non-zero homogeneous coefficient.
if (is_necessarily_closed())
return false;
else {
// The constraint is NOT necessarily closed.
const dimension_type eps_index = size() - 1;
if (x[eps_index] >= 0)
// If positive, we have found the constraint epsilon >= 0.
// If zero, one of the `true' dimensions has a non-zero coefficient.
// In both cases, it is not trivially false.
return false;
else {
// Here the epsilon coefficient is negative: strict inequality.
if (x[0] > 0)
// A strict inequality such as `lhs + k > 0',
// where k is a positive integer, cannot be trivially false.
return false;
// Checking for another non-zero coefficient.
for (dimension_type i = eps_index; --i > 0; )
if (x[i] != 0)
return false;
// We have the inequality `k > 0',
// where k is zero or a negative integer.
return true;
}
}
}
bool
PPL::Constraint::is_equivalent_to(const Constraint& y) const {
const Constraint& x = *this;
const dimension_type x_space_dim = x.space_dimension();
if (x_space_dim != y.space_dimension())
return false;
const Type x_type = x.type();
if (x_type != y.type()) {
// Check for special cases.
if (x.is_tautological())
return y.is_tautological();
else
return x.is_inconsistent() && y.is_inconsistent();
}
if (x_type == STRICT_INEQUALITY) {
// Due to the presence of epsilon-coefficients, syntactically
// different strict inequalities may actually encode the same
// topologically open half-space.
// First, drop the epsilon-coefficient ...
Linear_Expression x_expr(x);
Linear_Expression y_expr(y);
// ... then, re-normalize ...
x_expr.normalize();
y_expr.normalize();
// ... and finally check for syntactic equality.
for (dimension_type i = x_space_dim + 1; i-- > 0; )
if (x_expr[i] != y_expr[i])
return false;
return true;
}
// `x' and 'y' are of the same type and they are not strict inequalities;
// thus, the epsilon-coefficient, if present, is zero.
// It is sufficient to check for syntactic equality.
for (dimension_type i = x_space_dim + 1; i-- > 0; )
if (x[i] != y[i])
return false;
return true;
}
const PPL::Constraint* PPL::Constraint::zero_dim_false_p = 0;
const PPL::Constraint* PPL::Constraint::zero_dim_positivity_p = 0;
const PPL::Constraint* PPL::Constraint::epsilon_geq_zero_p = 0;
const PPL::Constraint* PPL::Constraint::epsilon_leq_one_p = 0;
void
PPL::Constraint::initialize() {
PPL_ASSERT(zero_dim_false_p == 0);
zero_dim_false_p
= new Constraint(Linear_Expression::zero() == Coefficient_one());
PPL_ASSERT(zero_dim_positivity_p == 0);
zero_dim_positivity_p
= new Constraint(Linear_Expression::zero() <= Coefficient_one());
PPL_ASSERT(epsilon_geq_zero_p == 0);
epsilon_geq_zero_p
= new Constraint(construct_epsilon_geq_zero());
PPL_ASSERT(epsilon_leq_one_p == 0);
epsilon_leq_one_p
= new Constraint(Linear_Expression::zero() < Coefficient_one());
}
void
PPL::Constraint::finalize() {
PPL_ASSERT(zero_dim_false_p != 0);
delete zero_dim_false_p;
zero_dim_false_p = 0;
PPL_ASSERT(zero_dim_positivity_p != 0);
delete zero_dim_positivity_p;
zero_dim_positivity_p = 0;
PPL_ASSERT(epsilon_geq_zero_p != 0);
delete epsilon_geq_zero_p;
epsilon_geq_zero_p = 0;
PPL_ASSERT(epsilon_leq_one_p != 0);
delete epsilon_leq_one_p;
epsilon_leq_one_p = 0;
}
/*! \relates Parma_Polyhedra_Library::Constraint */
std::ostream&
PPL::IO_Operators::operator<<(std::ostream& s, const Constraint& c) {
const dimension_type num_variables = c.space_dimension();
PPL_DIRTY_TEMP_COEFFICIENT(cv);
bool first = true;
for (dimension_type v = 0; v < num_variables; ++v) {
cv = c.coefficient(Variable(v));
if (cv != 0) {
if (!first) {
if (cv > 0)
s << " + ";
else {
s << " - ";
neg_assign(cv);
}
}
else
first = false;
if (cv == -1)
s << "-";
else if (cv != 1)
s << cv << "*";
s << PPL::Variable(v);
}
}
if (first)
s << Coefficient_zero();
const char* relation_symbol = 0;
switch (c.type()) {
case Constraint::EQUALITY:
relation_symbol = " = ";
break;
case Constraint::NONSTRICT_INEQUALITY:
relation_symbol = " >= ";
break;
case Constraint::STRICT_INEQUALITY:
relation_symbol = " > ";
break;
}
s << relation_symbol << -c.inhomogeneous_term();
return s;
}
/*! \relates Parma_Polyhedra_Library::Constraint */
std::ostream&
PPL::IO_Operators::operator<<(std::ostream& s, const Constraint::Type& t) {
const char* n = 0;
switch (t) {
case Constraint::EQUALITY:
n = "EQUALITY";
break;
case Constraint::NONSTRICT_INEQUALITY:
n = "NONSTRICT_INEQUALITY";
break;
case Constraint::STRICT_INEQUALITY:
n = "STRICT_INEQUALITY";
break;
}
s << n;
return s;
}
PPL_OUTPUT_DEFINITIONS(Constraint)
bool
PPL::Constraint::OK() const {
// Check the underlying Linear_Row object.
if (!Linear_Row::OK())
return false;
// Topology consistency checks.
const dimension_type min_size = is_necessarily_closed() ? 1 : 2;
if (size() < min_size) {
#ifndef NDEBUG
std::cerr << "Constraint has fewer coefficients than the minimum "
<< "allowed by its topology:"
<< std::endl
<< "size is " << size()
<< ", minimum is " << min_size << "."
<< std::endl;
#endif
return false;
}
if (is_equality() && !is_necessarily_closed() && (*this)[size() - 1] != 0) {
#ifndef NDEBUG
std::cerr << "Illegal constraint: an equality cannot be strict."
<< std::endl;
#endif
return false;
}
// Normalization check.
Constraint tmp = *this;
tmp.strong_normalize();
if (tmp != *this) {
#ifndef NDEBUG
std::cerr << "Constraint is not strongly normalized as it should be."
<< std::endl;
#endif
return false;
}
// All tests passed.
return true;
}
|