1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Tim King, Morgan Deters, Gereon Kremer |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* [[ Add one-line brief description here ]] |
14 |
|
* |
15 |
|
* [[ Add lengthier description here ]] |
16 |
|
* \todo document this file |
17 |
|
*/ |
18 |
|
|
19 |
|
#include "cvc5_private.h" |
20 |
|
|
21 |
|
#ifndef CVC5__THEORY__ARITH__NORMAL_FORM_H |
22 |
|
#define CVC5__THEORY__ARITH__NORMAL_FORM_H |
23 |
|
|
24 |
|
#include <algorithm> |
25 |
|
|
26 |
|
#include "base/output.h" |
27 |
|
#include "expr/node.h" |
28 |
|
#include "expr/node_self_iterator.h" |
29 |
|
#include "theory/arith/delta_rational.h" |
30 |
|
#include "util/rational.h" |
31 |
|
|
32 |
|
namespace cvc5 { |
33 |
|
namespace theory { |
34 |
|
namespace arith { |
35 |
|
|
36 |
|
/***********************************************/ |
37 |
|
/***************** Normal Form *****************/ |
38 |
|
/***********************************************/ |
39 |
|
/***********************************************/ |
40 |
|
|
41 |
|
/** |
42 |
|
* Section 1: Languages |
43 |
|
* The normal form for arithmetic nodes is defined by the language |
44 |
|
* accepted by the following BNFs with some guard conditions. |
45 |
|
* (The guard conditions are in Section 3 for completeness.) |
46 |
|
* |
47 |
|
* variable := n |
48 |
|
* where |
49 |
|
* n.isVar() or is foreign |
50 |
|
* n.getType() \in {Integer, Real} |
51 |
|
* |
52 |
|
* constant := n |
53 |
|
* where |
54 |
|
* n.getKind() == kind::CONST_RATIONAL |
55 |
|
* |
56 |
|
* var_list := variable | (* [variable]) |
57 |
|
* where |
58 |
|
* len [variable] >= 2 |
59 |
|
* isSorted varOrder [variable] |
60 |
|
* |
61 |
|
* monomial := constant | var_list | (* constant' var_list') |
62 |
|
* where |
63 |
|
* \f$ constant' \not\in {0,1} \f$ |
64 |
|
* |
65 |
|
* polynomial := monomial' | (+ [monomial]) |
66 |
|
* where |
67 |
|
* len [monomial] >= 2 |
68 |
|
* isStrictlySorted monoOrder [monomial] |
69 |
|
* forall (\x -> x != 0) [monomial] |
70 |
|
* |
71 |
|
* rational_cmp := (|><| qpolynomial constant) |
72 |
|
* where |
73 |
|
* |><| is GEQ, or GT |
74 |
|
* not (exists constantMonomial (monomialList qpolynomial)) |
75 |
|
* (exists realMonomial (monomialList qpolynomial)) |
76 |
|
* abs(monomialCoefficient (head (monomialList qpolynomial))) == 1 |
77 |
|
* |
78 |
|
* integer_cmp := (>= zpolynomial constant) |
79 |
|
* where |
80 |
|
* not (exists constantMonomial (monomialList zpolynomial)) |
81 |
|
* (forall integerMonomial (monomialList zpolynomial)) |
82 |
|
* the gcd of all numerators of coefficients is 1 |
83 |
|
* the denominator of all coefficients and the constant is 1 |
84 |
|
* the leading coefficient is positive |
85 |
|
* |
86 |
|
* rational_eq := (= qvarlist qpolynomial) |
87 |
|
* where |
88 |
|
* let allMonomials = (cons qvarlist (monomialList zpolynomial)) |
89 |
|
* let variableMonomials = (drop constantMonomial allMonomials) |
90 |
|
* isStrictlySorted variableMonomials |
91 |
|
* exists realMonomial variableMonomials |
92 |
|
* is not empty qvarlist |
93 |
|
* |
94 |
|
* integer_eq := (= zmonomial zpolynomial) |
95 |
|
* where |
96 |
|
* let allMonomials = (cons zmonomial (monomialList zpolynomial)) |
97 |
|
* let variableMonomials = (drop constantMonomial allMonomials) |
98 |
|
* not (constantMonomial zmonomial) |
99 |
|
* (forall integerMonomial allMonomials) |
100 |
|
* isStrictlySorted variableMonomials |
101 |
|
* the gcd of all numerators of coefficients is 1 |
102 |
|
* the denominator of all coefficients and the constant is 1 |
103 |
|
* the coefficient of monomial is positive |
104 |
|
* the value of the coefficient of monomial is minimal in variableMonomials |
105 |
|
* |
106 |
|
* comparison := TRUE | FALSE |
107 |
|
* | rational_cmp | (not rational_cmp) |
108 |
|
* | rational_eq | (not rational_eq) |
109 |
|
* | integer_cmp | (not integer_cmp) |
110 |
|
* | integer_eq | (not integer_eq) |
111 |
|
* |
112 |
|
* Normal Form for terms := polynomial |
113 |
|
* Normal Form for atoms := comparison |
114 |
|
*/ |
115 |
|
|
116 |
|
/** |
117 |
|
* Section 2: Helper Classes |
118 |
|
* The langauges accepted by each of these defintions |
119 |
|
* roughly corresponds to one of the following helper classes: |
120 |
|
* Variable |
121 |
|
* Constant |
122 |
|
* VarList |
123 |
|
* Monomial |
124 |
|
* Polynomial |
125 |
|
* Comparison |
126 |
|
* |
127 |
|
* Each of the classes obeys the following contracts/design decisions: |
128 |
|
* -Calling isMember(Node node) on a node returns true iff that node is a |
129 |
|
* a member of the language. Note: isMember is O(n). |
130 |
|
* -Calling isNormalForm() on a helper class object returns true iff that |
131 |
|
* helper class currently represents a normal form object. |
132 |
|
* -If isNormalForm() is false, then this object must have been made |
133 |
|
* using a mk*() factory function. |
134 |
|
* -If isNormalForm() is true, calling getNode() on all of these classes |
135 |
|
* returns a node that would be accepted by the corresponding language. |
136 |
|
* And if isNormalForm() is false, returns Node::null(). |
137 |
|
* -Each of the classes is immutable. |
138 |
|
* -Public facing constuctors have a 1-to-1 correspondence with one of |
139 |
|
* production rules in the above grammar. |
140 |
|
* -Public facing constuctors are required to fail in debug mode when the |
141 |
|
* guards of the production rule are not strictly met. |
142 |
|
* For example: Monomial(Constant(1),VarList(Variable(x))) must fail. |
143 |
|
* -When a class has a Class parseClass(Node node) function, |
144 |
|
* if isMember(node) is true, the function is required to return an instance |
145 |
|
* of the helper class, instance, s.t. instance.getNode() == node. |
146 |
|
* And if isMember(node) is false, this throws an assertion failure in debug |
147 |
|
* mode and has undefined behaviour if not in debug mode. |
148 |
|
* -Only public facing constructors, parseClass(node), and mk*() functions are |
149 |
|
* considered privileged functions for the helper class. |
150 |
|
* -Only privileged functions may use private constructors, and access |
151 |
|
* private data members. |
152 |
|
* -All non-privileged functions are considered utility functions and |
153 |
|
* must use a privileged function in order to create an instance of the class. |
154 |
|
*/ |
155 |
|
|
156 |
|
/** |
157 |
|
* Section 3: Guard Conditions Misc. |
158 |
|
* |
159 |
|
* |
160 |
|
* variable_order x y = |
161 |
|
* if (meta_kind_variable x) and (meta_kind_variable y) |
162 |
|
* then node_order x y |
163 |
|
* else if (meta_kind_variable x) |
164 |
|
* then false |
165 |
|
* else if (meta_kind_variable y) |
166 |
|
* then true |
167 |
|
* else node_order x y |
168 |
|
* |
169 |
|
* var_list_len vl = |
170 |
|
* match vl with |
171 |
|
* variable -> 1 |
172 |
|
* | (* [variable]) -> len [variable] |
173 |
|
* |
174 |
|
* order res = |
175 |
|
* match res with |
176 |
|
* Empty -> (0,Node::null()) |
177 |
|
* | NonEmpty(vl) -> (var_list_len vl, vl) |
178 |
|
* |
179 |
|
* var_listOrder a b = tuple_cmp (order a) (order b) |
180 |
|
* |
181 |
|
* monomialVarList monomial = |
182 |
|
* match monomial with |
183 |
|
* constant -> Empty |
184 |
|
* | var_list -> NonEmpty(var_list) |
185 |
|
* | (* constant' var_list') -> NonEmpty(var_list') |
186 |
|
* |
187 |
|
* monoOrder m0 m1 = var_listOrder (monomialVarList m0) (monomialVarList m1) |
188 |
|
* |
189 |
|
* integerMonomial mono = |
190 |
|
* forall varHasTypeInteger (monomialVarList mono) |
191 |
|
* |
192 |
|
* realMonomial mono = not (integerMonomial mono) |
193 |
|
* |
194 |
|
* constantMonomial monomial = |
195 |
|
* match monomial with |
196 |
|
* constant -> true |
197 |
|
* | var_list -> false |
198 |
|
* | (* constant' var_list') -> false |
199 |
|
* |
200 |
|
* monomialCoefficient monomial = |
201 |
|
* match monomial with |
202 |
|
* constant -> constant |
203 |
|
* | var_list -> Constant(1) |
204 |
|
* | (* constant' var_list') -> constant' |
205 |
|
* |
206 |
|
* monomialList polynomial = |
207 |
|
* match polynomial with |
208 |
|
* monomial -> monomial::[] |
209 |
|
* | (+ [monomial]) -> [monomial] |
210 |
|
*/ |
211 |
|
|
212 |
|
/** |
213 |
|
* A NodeWrapper is a class that is a thinly veiled container of a Node object. |
214 |
|
*/ |
215 |
2206948773 |
class NodeWrapper { |
216 |
|
private: |
217 |
|
Node node; |
218 |
|
public: |
219 |
877350182 |
NodeWrapper(Node n) : node(n) {} |
220 |
4772110463 |
const Node& getNode() const { return node; } |
221 |
|
};/* class NodeWrapper */ |
222 |
|
|
223 |
|
|
224 |
76986491 |
class Variable : public NodeWrapper { |
225 |
|
public: |
226 |
59586107 |
Variable(Node n) : NodeWrapper(n) { Assert(isMember(getNode())); } |
227 |
|
|
228 |
|
// TODO: check if it's a theory leaf also |
229 |
211807102 |
static bool isMember(Node n) |
230 |
|
{ |
231 |
211807102 |
Kind k = n.getKind(); |
232 |
211807102 |
switch (k) |
233 |
|
{ |
234 |
587265 |
case kind::CONST_RATIONAL: return false; |
235 |
153041 |
case kind::INTS_DIVISION: |
236 |
|
case kind::INTS_MODULUS: |
237 |
|
case kind::DIVISION: |
238 |
|
case kind::INTS_DIVISION_TOTAL: |
239 |
|
case kind::INTS_MODULUS_TOTAL: |
240 |
153041 |
case kind::DIVISION_TOTAL: return isDivMember(n); |
241 |
342537 |
case kind::IAND: return isIAndMember(n); |
242 |
55499 |
case kind::POW2: return isPow2Member(n); |
243 |
2681423 |
case kind::EXPONENTIAL: |
244 |
|
case kind::SINE: |
245 |
|
case kind::COSINE: |
246 |
|
case kind::TANGENT: |
247 |
|
case kind::COSECANT: |
248 |
|
case kind::SECANT: |
249 |
|
case kind::COTANGENT: |
250 |
|
case kind::ARCSINE: |
251 |
|
case kind::ARCCOSINE: |
252 |
|
case kind::ARCTANGENT: |
253 |
|
case kind::ARCCOSECANT: |
254 |
|
case kind::ARCSECANT: |
255 |
|
case kind::ARCCOTANGENT: |
256 |
|
case kind::SQRT: |
257 |
2681423 |
case kind::PI: return isTranscendentalMember(n); |
258 |
33569 |
case kind::ABS: |
259 |
|
case kind::TO_INTEGER: |
260 |
|
// Treat to_int as a variable; it is replaced in early preprocessing |
261 |
|
// by a variable. |
262 |
33569 |
return true; |
263 |
207953768 |
default: return isLeafMember(n); |
264 |
|
} |
265 |
|
} |
266 |
|
|
267 |
|
static bool isLeafMember(Node n); |
268 |
|
static bool isIAndMember(Node n); |
269 |
|
static bool isPow2Member(Node n); |
270 |
|
static bool isDivMember(Node n); |
271 |
|
bool isDivLike() const{ |
272 |
|
return isDivMember(getNode()); |
273 |
|
} |
274 |
|
static bool isTranscendentalMember(Node n); |
275 |
|
|
276 |
|
bool isNormalForm() { return isMember(getNode()); } |
277 |
|
|
278 |
32024143 |
bool isIntegral() const { |
279 |
32024143 |
return getNode().getType().isInteger(); |
280 |
|
} |
281 |
|
|
282 |
|
bool isMetaKindVariable() const { |
283 |
|
return getNode().isVar(); |
284 |
|
} |
285 |
|
|
286 |
13728093 |
bool operator<(const Variable& v) const { |
287 |
|
VariableNodeCmp cmp; |
288 |
13728093 |
return cmp(this->getNode(), v.getNode()); |
289 |
|
} |
290 |
|
|
291 |
|
struct VariableNodeCmp { |
292 |
104201590 |
static inline int cmp(const Node& n, const Node& m) { |
293 |
104201590 |
if ( n == m ) { return 0; } |
294 |
|
|
295 |
|
// this is now slightly off of the old variable order. |
296 |
|
|
297 |
95378585 |
bool nIsInteger = n.getType().isInteger(); |
298 |
95378585 |
bool mIsInteger = m.getType().isInteger(); |
299 |
|
|
300 |
95378585 |
if(nIsInteger == mIsInteger){ |
301 |
94386458 |
bool nIsVariable = n.isVar(); |
302 |
94386458 |
bool mIsVariable = m.isVar(); |
303 |
|
|
304 |
94386458 |
if(nIsVariable == mIsVariable){ |
305 |
83517027 |
if(n < m){ |
306 |
46768347 |
return -1; |
307 |
|
}else{ |
308 |
36748680 |
Assert(n != m); |
309 |
36748680 |
return 1; |
310 |
|
} |
311 |
|
}else{ |
312 |
10869431 |
if(nIsVariable){ |
313 |
5409015 |
return -1; // nIsVariable => !mIsVariable |
314 |
|
}else{ |
315 |
5460416 |
return 1; // !nIsVariable => mIsVariable |
316 |
|
} |
317 |
|
} |
318 |
|
}else{ |
319 |
992127 |
Assert(nIsInteger != mIsInteger); |
320 |
992127 |
if(nIsInteger){ |
321 |
830596 |
return 1; // nIsInteger => !mIsInteger |
322 |
|
}else{ |
323 |
161531 |
return -1; // !nIsInteger => mIsInteger |
324 |
|
} |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
19149136 |
bool operator()(const Node& n, const Node& m) const { |
329 |
19149136 |
return VariableNodeCmp::cmp(n,m) < 0; |
330 |
|
} |
331 |
|
}; |
332 |
|
|
333 |
|
bool operator==(const Variable& v) const { return getNode() == v.getNode();} |
334 |
|
|
335 |
|
size_t getComplexity() const; |
336 |
|
};/* class Variable */ |
337 |
|
|
338 |
|
|
339 |
705691182 |
class Constant : public NodeWrapper { |
340 |
|
public: |
341 |
255941161 |
Constant(Node n) : NodeWrapper(n) { Assert(isMember(getNode())); } |
342 |
|
|
343 |
258730127 |
static bool isMember(Node n) { return n.getKind() == kind::CONST_RATIONAL; } |
344 |
|
|
345 |
|
bool isNormalForm() { return isMember(getNode()); } |
346 |
|
|
347 |
2208347 |
static Constant mkConstant(Node n) |
348 |
|
{ |
349 |
2208347 |
Assert(n.getKind() == kind::CONST_RATIONAL); |
350 |
2208347 |
return Constant(n); |
351 |
|
} |
352 |
|
|
353 |
|
static Constant mkConstant(const Rational& rat); |
354 |
|
|
355 |
824642 |
static Constant mkZero() { |
356 |
824642 |
return mkConstant(Rational(0)); |
357 |
|
} |
358 |
|
|
359 |
|
static Constant mkOne() { |
360 |
|
return mkConstant(Rational(1)); |
361 |
|
} |
362 |
|
|
363 |
562684650 |
const Rational& getValue() const { |
364 |
562684650 |
return getNode().getConst<Rational>(); |
365 |
|
} |
366 |
|
|
367 |
|
static int absCmp(const Constant& a, const Constant& b); |
368 |
19875269 |
bool isIntegral() const { return getValue().isIntegral(); } |
369 |
|
|
370 |
254380402 |
int sgn() const { return getValue().sgn(); } |
371 |
|
|
372 |
247166347 |
bool isZero() const { return sgn() == 0; } |
373 |
321903 |
bool isNegative() const { return sgn() < 0; } |
374 |
6892152 |
bool isPositive() const { return sgn() > 0; } |
375 |
|
|
376 |
215101326 |
bool isOne() const { return getValue() == 1; } |
377 |
|
|
378 |
8130988 |
Constant operator*(const Rational& other) const { |
379 |
8130988 |
return mkConstant(getValue() * other); |
380 |
|
} |
381 |
|
|
382 |
2366796 |
Constant operator*(const Constant& other) const { |
383 |
2366796 |
return mkConstant(getValue() * other.getValue()); |
384 |
|
} |
385 |
35544 |
Constant operator+(const Constant& other) const { |
386 |
35544 |
return mkConstant(getValue() + other.getValue()); |
387 |
|
} |
388 |
610866 |
Constant operator-() const { |
389 |
610866 |
return mkConstant(-getValue()); |
390 |
|
} |
391 |
|
|
392 |
371902 |
Constant inverse() const{ |
393 |
371902 |
Assert(!isZero()); |
394 |
371902 |
return mkConstant(getValue().inverse()); |
395 |
|
} |
396 |
|
|
397 |
|
bool operator<(const Constant& other) const { |
398 |
|
return getValue() < other.getValue(); |
399 |
|
} |
400 |
|
|
401 |
22540 |
bool operator==(const Constant& other) const { |
402 |
|
//Rely on node uniqueness. |
403 |
22540 |
return getNode() == other.getNode(); |
404 |
|
} |
405 |
|
|
406 |
257530 |
Constant abs() const { |
407 |
257530 |
if(isNegative()){ |
408 |
135036 |
return -(*this); |
409 |
|
}else{ |
410 |
122494 |
return (*this); |
411 |
|
} |
412 |
|
} |
413 |
|
|
414 |
274780 |
uint32_t length() const{ |
415 |
274780 |
Assert(isIntegral()); |
416 |
274780 |
return getValue().getNumerator().length(); |
417 |
|
} |
418 |
|
|
419 |
|
size_t getComplexity() const; |
420 |
|
|
421 |
|
};/* class Constant */ |
422 |
|
|
423 |
|
|
424 |
|
template <class GetNodeIterator> |
425 |
7209901 |
inline Node makeNode(Kind k, GetNodeIterator start, GetNodeIterator end) { |
426 |
14419802 |
NodeBuilder nb(k); |
427 |
|
|
428 |
44595121 |
while(start != end) { |
429 |
18692610 |
nb << (*start).getNode(); |
430 |
18692610 |
++start; |
431 |
|
} |
432 |
|
|
433 |
14419802 |
return Node(nb); |
434 |
|
}/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */ |
435 |
|
|
436 |
|
/** |
437 |
|
* A VarList is a sorted list of variables representing a product. |
438 |
|
* If the VarList is empty, it represents an empty product or 1. |
439 |
|
* If the VarList has size 1, it represents a single variable. |
440 |
|
* |
441 |
|
* A non-sorted VarList can never be successfully made in debug mode. |
442 |
|
*/ |
443 |
857924921 |
class VarList : public NodeWrapper { |
444 |
|
private: |
445 |
|
|
446 |
|
static Node multList(const std::vector<Variable>& list) { |
447 |
|
Assert(list.size() >= 2); |
448 |
|
|
449 |
|
return makeNode(kind::NONLINEAR_MULT, list.begin(), list.end()); |
450 |
|
} |
451 |
|
|
452 |
47131617 |
VarList() : NodeWrapper(Node::null()) {} |
453 |
|
|
454 |
|
VarList(Node n); |
455 |
|
|
456 |
|
typedef expr::NodeSelfIterator internal_iterator; |
457 |
|
|
458 |
238343704 |
internal_iterator internalBegin() const { |
459 |
238343704 |
if(singleton()){ |
460 |
208892837 |
return expr::NodeSelfIterator::self(getNode()); |
461 |
|
}else{ |
462 |
29450867 |
return getNode().begin(); |
463 |
|
} |
464 |
|
} |
465 |
|
|
466 |
238320599 |
internal_iterator internalEnd() const { |
467 |
238320599 |
if(singleton()){ |
468 |
208869732 |
return expr::NodeSelfIterator::selfEnd(getNode()); |
469 |
|
}else{ |
470 |
29450867 |
return getNode().end(); |
471 |
|
} |
472 |
|
} |
473 |
|
|
474 |
|
public: |
475 |
|
|
476 |
4057287972 |
class iterator : public std::iterator<std::input_iterator_tag, Variable> { |
477 |
|
private: |
478 |
|
internal_iterator d_iter; |
479 |
|
|
480 |
|
public: |
481 |
458894787 |
explicit iterator(internal_iterator i) : d_iter(i) {} |
482 |
|
|
483 |
59577784 |
inline Variable operator*() { |
484 |
59577784 |
return Variable(*d_iter); |
485 |
|
} |
486 |
|
|
487 |
382720620 |
bool operator==(const iterator& i) { |
488 |
382720620 |
return d_iter == i.d_iter; |
489 |
|
} |
490 |
|
|
491 |
272069411 |
bool operator!=(const iterator& i) { |
492 |
272069411 |
return d_iter != i.d_iter; |
493 |
|
} |
494 |
|
|
495 |
233993880 |
iterator operator++() { |
496 |
233993880 |
++d_iter; |
497 |
233993880 |
return *this; |
498 |
|
} |
499 |
|
|
500 |
|
iterator operator++(int) { |
501 |
|
return iterator(d_iter++); |
502 |
|
} |
503 |
|
}; |
504 |
|
|
505 |
229458946 |
iterator begin() const { |
506 |
229458946 |
return iterator(internalBegin()); |
507 |
|
} |
508 |
|
|
509 |
229435841 |
iterator end() const { |
510 |
229435841 |
return iterator(internalEnd()); |
511 |
|
} |
512 |
|
|
513 |
23105 |
Variable getHead() const { |
514 |
23105 |
Assert(!empty()); |
515 |
23105 |
return *(begin()); |
516 |
|
} |
517 |
|
|
518 |
4336583 |
VarList(Variable v) : NodeWrapper(v.getNode()) { |
519 |
4336583 |
Assert(isSorted(begin(), end())); |
520 |
4336583 |
} |
521 |
|
|
522 |
|
VarList(const std::vector<Variable>& l) : NodeWrapper(multList(l)) { |
523 |
|
Assert(l.size() >= 2); |
524 |
|
Assert(isSorted(begin(), end())); |
525 |
|
} |
526 |
|
|
527 |
|
static bool isMember(Node n); |
528 |
|
|
529 |
|
bool isNormalForm() const { |
530 |
|
return !empty(); |
531 |
|
} |
532 |
|
|
533 |
47131617 |
static VarList mkEmptyVarList() { |
534 |
47131617 |
return VarList(); |
535 |
|
} |
536 |
|
|
537 |
|
|
538 |
|
/** There are no restrictions on the size of l */ |
539 |
|
static VarList mkVarList(const std::vector<Variable>& l) { |
540 |
|
if(l.size() == 0) { |
541 |
|
return mkEmptyVarList(); |
542 |
|
} else if(l.size() == 1) { |
543 |
|
return VarList((*l.begin()).getNode()); |
544 |
|
} else { |
545 |
|
return VarList(l); |
546 |
|
} |
547 |
|
} |
548 |
|
|
549 |
1344336260 |
bool empty() const { return getNode().isNull(); } |
550 |
814160445 |
bool singleton() const { |
551 |
814160445 |
return !empty() && getNode().getKind() != kind::NONLINEAR_MULT; |
552 |
|
} |
553 |
|
|
554 |
337274216 |
int size() const { |
555 |
337274216 |
if(singleton()) |
556 |
278067530 |
return 1; |
557 |
|
else |
558 |
59206686 |
return getNode().getNumChildren(); |
559 |
|
} |
560 |
|
|
561 |
|
static VarList parseVarList(Node n); |
562 |
|
|
563 |
|
VarList operator*(const VarList& vl) const; |
564 |
|
|
565 |
|
int cmp(const VarList& vl) const; |
566 |
|
|
567 |
598229 |
bool operator<(const VarList& vl) const { return cmp(vl) < 0; } |
568 |
|
|
569 |
24108823 |
bool operator==(const VarList& vl) const { return cmp(vl) == 0; } |
570 |
|
|
571 |
38007378 |
bool isIntegral() const { |
572 |
66838505 |
for(iterator i = begin(), e=end(); i != e; ++i ){ |
573 |
60855270 |
Variable var = *i; |
574 |
32024143 |
if(!var.isIntegral()){ |
575 |
3193016 |
return false; |
576 |
|
} |
577 |
|
} |
578 |
34814362 |
return true; |
579 |
|
} |
580 |
|
size_t getComplexity() const; |
581 |
|
|
582 |
|
private: |
583 |
|
bool isSorted(iterator start, iterator end); |
584 |
|
|
585 |
|
};/* class VarList */ |
586 |
|
|
587 |
|
|
588 |
|
/** Constructors have side conditions. Use the static mkMonomial functions instead. */ |
589 |
454033583 |
class Monomial : public NodeWrapper { |
590 |
|
private: |
591 |
|
Constant constant; |
592 |
|
VarList varList; |
593 |
|
Monomial(Node n, const Constant& c, const VarList& vl): |
594 |
|
NodeWrapper(n), constant(c), varList(vl) |
595 |
|
{ |
596 |
|
Assert(!c.isZero() || vl.empty()); |
597 |
|
Assert(c.isZero() || !vl.empty()); |
598 |
|
|
599 |
|
Assert(!c.isOne() || !multStructured(n)); |
600 |
|
} |
601 |
|
|
602 |
70149991 |
static Node makeMultNode(const Constant& c, const VarList& vl) { |
603 |
70149991 |
Assert(!c.isZero()); |
604 |
70149991 |
Assert(!c.isOne()); |
605 |
70149991 |
Assert(!vl.empty()); |
606 |
70149991 |
return NodeManager::currentNM()->mkNode(kind::MULT, c.getNode(), vl.getNode()); |
607 |
|
} |
608 |
|
|
609 |
395796790 |
static bool multStructured(Node n) { |
610 |
719203582 |
return n.getKind() == kind::MULT && |
611 |
1510797162 |
n[0].getKind() == kind::CONST_RATIONAL && |
612 |
953296976 |
n.getNumChildren() == 2; |
613 |
|
} |
614 |
|
|
615 |
47131617 |
Monomial(const Constant& c): |
616 |
47131617 |
NodeWrapper(c.getNode()), constant(c), varList(VarList::mkEmptyVarList()) |
617 |
47131617 |
{ } |
618 |
|
|
619 |
123310787 |
Monomial(const VarList& vl): |
620 |
123310787 |
NodeWrapper(vl.getNode()), constant(Constant::mkConstant(1)), varList(vl) |
621 |
|
{ |
622 |
123310787 |
Assert(!varList.empty()); |
623 |
123310787 |
} |
624 |
|
|
625 |
70149991 |
Monomial(const Constant& c, const VarList& vl): |
626 |
70149991 |
NodeWrapper(makeMultNode(c,vl)), constant(c), varList(vl) |
627 |
|
{ |
628 |
70149991 |
Assert(!c.isZero()); |
629 |
70149991 |
Assert(!c.isOne()); |
630 |
70149991 |
Assert(!varList.empty()); |
631 |
|
|
632 |
70149991 |
Assert(multStructured(getNode())); |
633 |
70149991 |
} |
634 |
|
public: |
635 |
|
static bool isMember(TNode n); |
636 |
|
|
637 |
|
/** Makes a monomial with no restrictions on c and vl. */ |
638 |
|
static Monomial mkMonomial(const Constant& c, const VarList& vl); |
639 |
|
|
640 |
|
/** If vl is empty, this make one. */ |
641 |
|
static Monomial mkMonomial(const VarList& vl); |
642 |
|
|
643 |
862324 |
static Monomial mkMonomial(const Constant& c){ |
644 |
862324 |
return Monomial(c); |
645 |
|
} |
646 |
|
|
647 |
36216 |
static Monomial mkMonomial(const Variable& v){ |
648 |
36216 |
return Monomial(VarList(v)); |
649 |
|
} |
650 |
|
|
651 |
|
static Monomial parseMonomial(Node n); |
652 |
|
|
653 |
4061738 |
static Monomial mkZero() { |
654 |
4061738 |
return Monomial(Constant::mkConstant(0)); |
655 |
|
} |
656 |
546621 |
static Monomial mkOne() { |
657 |
546621 |
return Monomial(Constant::mkConstant(1)); |
658 |
|
} |
659 |
107450716 |
const Constant& getConstant() const { return constant; } |
660 |
302848931 |
const VarList& getVarList() const { return varList; } |
661 |
|
|
662 |
27732548 |
bool isConstant() const { |
663 |
27732548 |
return varList.empty(); |
664 |
|
} |
665 |
|
|
666 |
8164380 |
bool isZero() const { |
667 |
8164380 |
return constant.isZero(); |
668 |
|
} |
669 |
|
|
670 |
2508505 |
bool coefficientIsOne() const { |
671 |
2508505 |
return constant.isOne(); |
672 |
|
} |
673 |
|
|
674 |
1937459 |
bool absCoefficientIsOne() const { |
675 |
1937459 |
return coefficientIsOne() || constant.getValue() == -1; |
676 |
|
} |
677 |
|
|
678 |
|
bool constantIsPositive() const { |
679 |
|
return getConstant().isPositive(); |
680 |
|
} |
681 |
|
|
682 |
|
Monomial operator*(const Rational& q) const; |
683 |
|
Monomial operator*(const Constant& c) const; |
684 |
|
Monomial operator*(const Monomial& mono) const; |
685 |
|
|
686 |
807705 |
Monomial operator-() const{ |
687 |
807705 |
return (*this) * Rational(-1); |
688 |
|
} |
689 |
|
|
690 |
|
|
691 |
103481774 |
int cmp(const Monomial& mono) const { |
692 |
103481774 |
return getVarList().cmp(mono.getVarList()); |
693 |
|
} |
694 |
|
|
695 |
81900615 |
bool operator<(const Monomial& vl) const { |
696 |
81900615 |
return cmp(vl) < 0; |
697 |
|
} |
698 |
|
|
699 |
21581159 |
bool operator==(const Monomial& vl) const { |
700 |
21581159 |
return cmp(vl) == 0; |
701 |
|
} |
702 |
|
|
703 |
33763049 |
static bool isSorted(const std::vector<Monomial>& m) { |
704 |
33763049 |
return std::is_sorted(m.begin(), m.end()); |
705 |
|
} |
706 |
|
|
707 |
21740134 |
static bool isStrictlySorted(const std::vector<Monomial>& m) { |
708 |
21740134 |
return isSorted(m) && std::adjacent_find(m.begin(),m.end()) == m.end(); |
709 |
|
} |
710 |
|
|
711 |
|
static void sort(std::vector<Monomial>& m); |
712 |
|
static void combineAdjacentMonomials(std::vector<Monomial>& m); |
713 |
|
|
714 |
|
/** |
715 |
|
* The variable product |
716 |
|
*/ |
717 |
38005967 |
bool integralVariables() const { |
718 |
38005967 |
return getVarList().isIntegral(); |
719 |
|
} |
720 |
|
|
721 |
|
/** |
722 |
|
* The coefficient of the monomial is integral. |
723 |
|
*/ |
724 |
17795335 |
bool integralCoefficient() const { |
725 |
17795335 |
return getConstant().isIntegral(); |
726 |
|
} |
727 |
|
|
728 |
|
/** |
729 |
|
* A Monomial is an "integral" monomial if the constant is integral. |
730 |
|
*/ |
731 |
17795335 |
bool isIntegral() const { |
732 |
17795335 |
return integralCoefficient() && integralVariables(); |
733 |
|
} |
734 |
|
|
735 |
|
/** Returns true if the VarList is a product of at least 2 Variables.*/ |
736 |
74331 |
bool isNonlinear() const { |
737 |
74331 |
return getVarList().size() >= 2; |
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
|
* Given a sorted list of monomials, this function transforms this |
742 |
|
* into a strictly sorted list of monomials that does not contain zero. |
743 |
|
*/ |
744 |
|
//static std::vector<Monomial> sumLikeTerms(const std::vector<Monomial>& monos); |
745 |
|
|
746 |
8787026 |
int absCmp(const Monomial& other) const{ |
747 |
8787026 |
return getConstant().getValue().absCmp(other.getConstant().getValue()); |
748 |
|
} |
749 |
|
// bool absLessThan(const Monomial& other) const{ |
750 |
|
// return getConstant().abs() < other.getConstant().abs(); |
751 |
|
// } |
752 |
|
|
753 |
186812 |
uint32_t coefficientLength() const{ |
754 |
186812 |
return getConstant().length(); |
755 |
|
} |
756 |
|
|
757 |
|
void print() const; |
758 |
|
static void printList(const std::vector<Monomial>& list); |
759 |
|
|
760 |
|
size_t getComplexity() const; |
761 |
|
};/* class Monomial */ |
762 |
|
|
763 |
|
class SumPair; |
764 |
|
class Comparison;; |
765 |
|
|
766 |
102478147 |
class Polynomial : public NodeWrapper { |
767 |
|
private: |
768 |
|
bool d_singleton; |
769 |
|
|
770 |
53301645 |
Polynomial(TNode n) : NodeWrapper(n), d_singleton(Monomial::isMember(n)) { |
771 |
53301645 |
Assert(isMember(getNode())); |
772 |
53301645 |
} |
773 |
|
|
774 |
7209901 |
static Node makePlusNode(const std::vector<Monomial>& m) { |
775 |
7209901 |
Assert(m.size() >= 2); |
776 |
|
|
777 |
7209901 |
return makeNode(kind::PLUS, m.begin(), m.end()); |
778 |
|
} |
779 |
|
|
780 |
|
typedef expr::NodeSelfIterator internal_iterator; |
781 |
|
|
782 |
123183612 |
internal_iterator internalBegin() const { |
783 |
123183612 |
if(singleton()){ |
784 |
83069924 |
return expr::NodeSelfIterator::self(getNode()); |
785 |
|
}else{ |
786 |
40113688 |
return getNode().begin(); |
787 |
|
} |
788 |
|
} |
789 |
|
|
790 |
83830355 |
internal_iterator internalEnd() const { |
791 |
83830355 |
if(singleton()){ |
792 |
54080120 |
return expr::NodeSelfIterator::selfEnd(getNode()); |
793 |
|
}else{ |
794 |
29750235 |
return getNode().end(); |
795 |
|
} |
796 |
|
} |
797 |
|
|
798 |
236185997 |
bool singleton() const { return d_singleton; } |
799 |
|
|
800 |
|
public: |
801 |
|
static bool isMember(TNode n); |
802 |
|
|
803 |
952064701 |
class iterator : public std::iterator<std::input_iterator_tag, Monomial> { |
804 |
|
private: |
805 |
|
internal_iterator d_iter; |
806 |
|
|
807 |
|
public: |
808 |
207013967 |
explicit iterator(internal_iterator i) : d_iter(i) {} |
809 |
|
|
810 |
177953828 |
inline Monomial operator*() { |
811 |
177953828 |
return Monomial::parseMonomial(*d_iter); |
812 |
|
} |
813 |
|
|
814 |
180690 |
bool operator==(const iterator& i) { |
815 |
180690 |
return d_iter == i.d_iter; |
816 |
|
} |
817 |
|
|
818 |
197605869 |
bool operator!=(const iterator& i) { |
819 |
197605869 |
return d_iter != i.d_iter; |
820 |
|
} |
821 |
|
|
822 |
99288848 |
iterator operator++() { |
823 |
99288848 |
++d_iter; |
824 |
99288848 |
return *this; |
825 |
|
} |
826 |
|
|
827 |
|
iterator operator++(int) { |
828 |
|
return iterator(d_iter++); |
829 |
|
} |
830 |
|
}; |
831 |
|
|
832 |
123183612 |
iterator begin() const { return iterator(internalBegin()); } |
833 |
83830355 |
iterator end() const { return iterator(internalEnd()); } |
834 |
|
|
835 |
15791087 |
Polynomial(const Monomial& m): |
836 |
15791087 |
NodeWrapper(m.getNode()), d_singleton(true) |
837 |
15791087 |
{} |
838 |
|
|
839 |
7209901 |
Polynomial(const std::vector<Monomial>& m): |
840 |
7209901 |
NodeWrapper(makePlusNode(m)), d_singleton(false) |
841 |
|
{ |
842 |
7209901 |
Assert(m.size() >= 2); |
843 |
7209901 |
Assert(Monomial::isStrictlySorted(m)); |
844 |
7209901 |
} |
845 |
|
|
846 |
862324 |
static Polynomial mkPolynomial(const Constant& c){ |
847 |
862324 |
return Polynomial(Monomial::mkMonomial(c)); |
848 |
|
} |
849 |
|
|
850 |
36216 |
static Polynomial mkPolynomial(const Variable& v){ |
851 |
36216 |
return Polynomial(Monomial::mkMonomial(v)); |
852 |
|
} |
853 |
|
|
854 |
17700696 |
static Polynomial mkPolynomial(const std::vector<Monomial>& m) { |
855 |
17700696 |
if(m.size() == 0) { |
856 |
1754982 |
return Polynomial(Monomial::mkZero()); |
857 |
15945714 |
} else if(m.size() == 1) { |
858 |
8735813 |
return Polynomial((*m.begin())); |
859 |
|
} else { |
860 |
7209901 |
return Polynomial(m); |
861 |
|
} |
862 |
|
} |
863 |
|
|
864 |
53301645 |
static Polynomial parsePolynomial(Node n) { |
865 |
53301645 |
return Polynomial(n); |
866 |
|
} |
867 |
|
|
868 |
2306756 |
static Polynomial mkZero() { |
869 |
2306756 |
return Polynomial(Monomial::mkZero()); |
870 |
|
} |
871 |
546621 |
static Polynomial mkOne() { |
872 |
546621 |
return Polynomial(Monomial::mkOne()); |
873 |
|
} |
874 |
8317373 |
bool isZero() const { |
875 |
8317373 |
return singleton() && (getHead().isZero()); |
876 |
|
} |
877 |
|
|
878 |
12921375 |
bool isConstant() const { |
879 |
12921375 |
return singleton() && (getHead().isConstant()); |
880 |
|
} |
881 |
|
|
882 |
12462164 |
bool containsConstant() const { |
883 |
12462164 |
return getHead().isConstant(); |
884 |
|
} |
885 |
|
|
886 |
1508 |
uint32_t size() const{ |
887 |
1508 |
if(singleton()){ |
888 |
1166 |
return 1; |
889 |
|
}else{ |
890 |
342 |
Assert(getNode().getKind() == kind::PLUS); |
891 |
342 |
return getNode().getNumChildren(); |
892 |
|
} |
893 |
|
} |
894 |
|
|
895 |
44986452 |
Monomial getHead() const { |
896 |
44986452 |
return *(begin()); |
897 |
|
} |
898 |
|
|
899 |
2041265 |
Polynomial getTail() const { |
900 |
2041265 |
Assert(!singleton()); |
901 |
|
|
902 |
4082530 |
iterator tailStart = begin(); |
903 |
2041265 |
++tailStart; |
904 |
4082530 |
std::vector<Monomial> subrange; |
905 |
2041265 |
std::copy(tailStart, end(), std::back_inserter(subrange)); |
906 |
4082530 |
return mkPolynomial(subrange); |
907 |
|
} |
908 |
|
|
909 |
|
Monomial minimumVariableMonomial() const; |
910 |
|
bool variableMonomialAreStrictlyGreater(const Monomial& m) const; |
911 |
|
|
912 |
|
void printList() const { |
913 |
|
if(Debug.isOn("normal-form")){ |
914 |
|
Debug("normal-form") << "start list" << std::endl; |
915 |
|
for(iterator i = begin(), oend = end(); i != oend; ++i) { |
916 |
|
const Monomial& m =*i; |
917 |
|
m.print(); |
918 |
|
} |
919 |
|
Debug("normal-form") << "end list" << std::endl; |
920 |
|
} |
921 |
|
} |
922 |
|
|
923 |
|
/** A Polynomial is an "integral" polynomial if all of the monomials are integral. */ |
924 |
14624144 |
bool allIntegralVariables() const { |
925 |
33582780 |
for(iterator i = begin(), e=end(); i!=e; ++i){ |
926 |
20212030 |
if(!(*i).integralVariables()){ |
927 |
1253394 |
return false; |
928 |
|
} |
929 |
|
} |
930 |
13370750 |
return true; |
931 |
|
} |
932 |
|
|
933 |
|
/** |
934 |
|
* A Polynomial is an "integral" polynomial if all of the monomials are integral |
935 |
|
* and all of the coefficients are Integral. */ |
936 |
12338136 |
bool isIntegral() const { |
937 |
28193144 |
for(iterator i = begin(), e=end(); i!=e; ++i){ |
938 |
17795335 |
if(!(*i).isIntegral()){ |
939 |
1940327 |
return false; |
940 |
|
} |
941 |
|
} |
942 |
10397809 |
return true; |
943 |
|
} |
944 |
|
|
945 |
|
static Polynomial sumPolynomials(const std::vector<Polynomial>& polynomials); |
946 |
|
|
947 |
|
/** Returns true if the polynomial contains a non-linear monomial.*/ |
948 |
|
bool isNonlinear() const; |
949 |
|
|
950 |
|
/** Check whether this polynomial is only a single variable. */ |
951 |
620 |
bool isVariable() const |
952 |
|
{ |
953 |
1696 |
return singleton() && getHead().getVarList().singleton() |
954 |
2114 |
&& getHead().coefficientIsOne(); |
955 |
|
} |
956 |
|
/** Return the variable, given that isVariable() holds. */ |
957 |
|
Variable getVariable() const |
958 |
|
{ |
959 |
|
Assert(isVariable()); |
960 |
|
return getHead().getVarList().getHead(); |
961 |
|
} |
962 |
|
|
963 |
|
/** |
964 |
|
* Selects a minimal monomial in the polynomial by the absolute value of |
965 |
|
* the coefficient. |
966 |
|
*/ |
967 |
|
Monomial selectAbsMinimum() const; |
968 |
|
|
969 |
|
/** Returns true if the absolute value of the head coefficient is one. */ |
970 |
|
bool leadingCoefficientIsAbsOne() const; |
971 |
|
bool leadingCoefficientIsPositive() const; |
972 |
|
bool denominatorLCMIsOne() const; |
973 |
|
bool numeratorGCDIsOne() const; |
974 |
|
|
975 |
3461233 |
bool signNormalizedReducedSum() const { |
976 |
3461233 |
return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne(); |
977 |
|
} |
978 |
|
|
979 |
|
/** |
980 |
|
* Returns the Least Common Multiple of the denominators of the coefficients |
981 |
|
* of the monomials. |
982 |
|
*/ |
983 |
|
Integer denominatorLCM() const; |
984 |
|
|
985 |
|
/** |
986 |
|
* Returns the GCD of the numerators of the monomials. |
987 |
|
* Requires this to be an isIntegral() polynomial. |
988 |
|
*/ |
989 |
|
Integer numeratorGCD() const; |
990 |
|
|
991 |
|
/** |
992 |
|
* Returns the GCD of the coefficients of the monomials. |
993 |
|
* Requires this to be an isIntegral() polynomial. |
994 |
|
*/ |
995 |
|
Integer gcd() const; |
996 |
|
|
997 |
|
/** z must divide all of the coefficients of the polynomial. */ |
998 |
|
Polynomial exactDivide(const Integer& z) const; |
999 |
|
|
1000 |
|
Polynomial operator+(const Polynomial& vl) const; |
1001 |
|
Polynomial operator-(const Polynomial& vl) const; |
1002 |
902626 |
Polynomial operator-() const{ |
1003 |
902626 |
return (*this) * Rational(-1); |
1004 |
|
} |
1005 |
|
|
1006 |
|
Polynomial operator*(const Rational& q) const; |
1007 |
|
Polynomial operator*(const Constant& c) const; |
1008 |
|
Polynomial operator*(const Monomial& mono) const; |
1009 |
|
|
1010 |
|
Polynomial operator*(const Polynomial& poly) const; |
1011 |
|
|
1012 |
|
/** |
1013 |
|
* Viewing the integer polynomial as a list [(* coeff_i mono_i)] |
1014 |
|
* The quotient and remainder of p divided by the non-zero integer z is: |
1015 |
|
* q := [(* floor(coeff_i/z) mono_i )] |
1016 |
|
* r := [(* rem(coeff_i/z) mono_i)] |
1017 |
|
* computeQR(p,z) returns the node (+ q r). |
1018 |
|
* |
1019 |
|
* q and r are members of the Polynomial class. |
1020 |
|
* For example: |
1021 |
|
* computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns |
1022 |
|
* (+ (+ 2 x (* 4 y)) (+ 1 x)) |
1023 |
|
*/ |
1024 |
|
static Node computeQR(const Polynomial& p, const Integer& z); |
1025 |
|
|
1026 |
|
/** Returns the coefficient associated with the VarList in the polynomial. */ |
1027 |
|
Constant getCoefficient(const VarList& vl) const; |
1028 |
|
|
1029 |
87968 |
uint32_t maxLength() const{ |
1030 |
175936 |
iterator i = begin(), e=end(); |
1031 |
87968 |
if( i == e){ |
1032 |
|
return 1; |
1033 |
|
}else{ |
1034 |
87968 |
uint32_t max = (*i).coefficientLength(); |
1035 |
87968 |
++i; |
1036 |
285656 |
for(; i!=e; ++i){ |
1037 |
98844 |
uint32_t curr = (*i).coefficientLength(); |
1038 |
98844 |
if(curr > max){ |
1039 |
20440 |
max = curr; |
1040 |
|
} |
1041 |
|
} |
1042 |
87968 |
return max; |
1043 |
|
} |
1044 |
|
} |
1045 |
|
|
1046 |
5999474 |
uint32_t numMonomials() const { |
1047 |
5999474 |
if( getNode().getKind() == kind::PLUS ){ |
1048 |
29192 |
return getNode().getNumChildren(); |
1049 |
5970282 |
}else if(isZero()){ |
1050 |
|
return 0; |
1051 |
|
}else{ |
1052 |
5970282 |
return 1; |
1053 |
|
} |
1054 |
|
} |
1055 |
|
|
1056 |
301861 |
const Rational& asConstant() const{ |
1057 |
301861 |
Assert(isConstant()); |
1058 |
301861 |
return getNode().getConst<Rational>(); |
1059 |
|
//return getHead().getConstant().getValue(); |
1060 |
|
} |
1061 |
|
|
1062 |
3975547 |
bool isVarList() const { |
1063 |
3975547 |
if(singleton()){ |
1064 |
3614365 |
return VarList::isMember(getNode()); |
1065 |
|
}else{ |
1066 |
361182 |
return false; |
1067 |
|
} |
1068 |
|
} |
1069 |
|
|
1070 |
1198682 |
VarList asVarList() const { |
1071 |
1198682 |
Assert(isVarList()); |
1072 |
1198682 |
return getHead().getVarList(); |
1073 |
|
} |
1074 |
|
|
1075 |
|
size_t getComplexity() const; |
1076 |
|
|
1077 |
|
friend class SumPair; |
1078 |
|
friend class Comparison; |
1079 |
|
|
1080 |
|
/** Returns a node that if asserted ensures v is the abs of this polynomial.*/ |
1081 |
|
Node makeAbsCondition(Variable v){ |
1082 |
|
return makeAbsCondition(v, *this); |
1083 |
|
} |
1084 |
|
|
1085 |
|
/** Returns a node that if asserted ensures v is the abs of p.*/ |
1086 |
|
static Node makeAbsCondition(Variable v, Polynomial p); |
1087 |
|
|
1088 |
|
};/* class Polynomial */ |
1089 |
|
|
1090 |
|
|
1091 |
|
/** |
1092 |
|
* SumPair is a utility class that extends polynomials for use in computations. |
1093 |
|
* A SumPair is always a combination of (+ p c) where |
1094 |
|
* c is a constant and p is a polynomial such that p = 0 or !p.containsConstant(). |
1095 |
|
* |
1096 |
|
* These are a useful utility for representing the equation p = c as (+ p -c) where the pair |
1097 |
|
* is known to implicitly be equal to 0. |
1098 |
|
* |
1099 |
|
* SumPairs do not have unique representations due to the potential for p = 0. |
1100 |
|
* This makes them inappropriate for normal forms. |
1101 |
|
*/ |
1102 |
1989054 |
class SumPair : public NodeWrapper { |
1103 |
|
private: |
1104 |
1604142 |
static Node toNode(const Polynomial& p, const Constant& c){ |
1105 |
1604142 |
return NodeManager::currentNM()->mkNode(kind::PLUS, p.getNode(), c.getNode()); |
1106 |
|
} |
1107 |
|
|
1108 |
668 |
SumPair(TNode n) : NodeWrapper(n) { Assert(isNormalForm()); } |
1109 |
|
|
1110 |
|
public: |
1111 |
334 |
SumPair(const Polynomial& p): |
1112 |
334 |
NodeWrapper(toNode(p, Constant::mkConstant(0))) |
1113 |
|
{ |
1114 |
334 |
Assert(isNormalForm()); |
1115 |
334 |
} |
1116 |
|
|
1117 |
1603808 |
SumPair(const Polynomial& p, const Constant& c): |
1118 |
1603808 |
NodeWrapper(toNode(p, c)) |
1119 |
|
{ |
1120 |
1603808 |
Assert(isNormalForm()); |
1121 |
1603808 |
} |
1122 |
|
|
1123 |
1604810 |
static bool isMember(TNode n) { |
1124 |
1604810 |
if(n.getKind() == kind::PLUS && n.getNumChildren() == 2){ |
1125 |
1604810 |
if(Constant::isMember(n[1])){ |
1126 |
1604810 |
if(Polynomial::isMember(n[0])){ |
1127 |
3209620 |
Polynomial p = Polynomial::parsePolynomial(n[0]); |
1128 |
1604810 |
return p.isZero() || (!p.containsConstant()); |
1129 |
|
}else{ |
1130 |
|
return false; |
1131 |
|
} |
1132 |
|
}else{ |
1133 |
|
return false; |
1134 |
|
} |
1135 |
|
}else{ |
1136 |
|
return false; |
1137 |
|
} |
1138 |
|
} |
1139 |
|
|
1140 |
1604810 |
bool isNormalForm() const { |
1141 |
1604810 |
return isMember(getNode()); |
1142 |
|
} |
1143 |
|
|
1144 |
7208169 |
Polynomial getPolynomial() const { |
1145 |
7208169 |
return Polynomial::parsePolynomial(getNode()[0]); |
1146 |
|
} |
1147 |
|
|
1148 |
2208347 |
Constant getConstant() const { |
1149 |
2208347 |
return Constant::mkConstant((getNode())[1]); |
1150 |
|
} |
1151 |
|
|
1152 |
35544 |
SumPair operator+(const SumPair& other) const { |
1153 |
71088 |
return SumPair(getPolynomial() + other.getPolynomial(), |
1154 |
106632 |
getConstant() + other.getConstant()); |
1155 |
|
} |
1156 |
|
|
1157 |
94280 |
SumPair operator*(const Constant& c) const { |
1158 |
94280 |
return SumPair(getPolynomial() * c, getConstant() * c); |
1159 |
|
} |
1160 |
|
|
1161 |
334 |
SumPair operator-(const SumPair& other) const { |
1162 |
334 |
return (*this) + (other * Constant::mkConstant(-1)); |
1163 |
|
} |
1164 |
|
|
1165 |
|
static SumPair mkSumPair(const Polynomial& p); |
1166 |
|
|
1167 |
334 |
static SumPair mkSumPair(const Variable& var){ |
1168 |
334 |
return SumPair(Polynomial::mkPolynomial(var)); |
1169 |
|
} |
1170 |
|
|
1171 |
668 |
static SumPair parseSumPair(TNode n){ |
1172 |
668 |
return SumPair(n); |
1173 |
|
} |
1174 |
|
|
1175 |
284818 |
bool isIntegral() const{ |
1176 |
284818 |
return getConstant().isIntegral() && getPolynomial().isIntegral(); |
1177 |
|
} |
1178 |
|
|
1179 |
476085 |
bool isConstant() const { |
1180 |
476085 |
return getPolynomial().isZero(); |
1181 |
|
} |
1182 |
|
|
1183 |
2217 |
bool isZero() const { |
1184 |
2217 |
return getConstant().isZero() && isConstant(); |
1185 |
|
} |
1186 |
|
|
1187 |
|
uint32_t size() const{ |
1188 |
|
return getPolynomial().size(); |
1189 |
|
} |
1190 |
|
|
1191 |
37619 |
bool isNonlinear() const{ |
1192 |
37619 |
return getPolynomial().isNonlinear(); |
1193 |
|
} |
1194 |
|
|
1195 |
|
/** |
1196 |
|
* Returns the greatest common divisor of gcd(getPolynomial()) and getConstant(). |
1197 |
|
* The SumPair must be integral. |
1198 |
|
*/ |
1199 |
172656 |
Integer gcd() const { |
1200 |
172656 |
Assert(isIntegral()); |
1201 |
172656 |
return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator()); |
1202 |
|
} |
1203 |
|
|
1204 |
87968 |
uint32_t maxLength() const { |
1205 |
87968 |
Assert(isIntegral()); |
1206 |
87968 |
return std::max(getPolynomial().maxLength(), getConstant().length()); |
1207 |
|
} |
1208 |
|
|
1209 |
445 |
static SumPair mkZero() { |
1210 |
445 |
return SumPair(Polynomial::mkZero(), Constant::mkConstant(0)); |
1211 |
|
} |
1212 |
|
|
1213 |
|
static Node computeQR(const SumPair& sp, const Integer& div); |
1214 |
|
|
1215 |
|
};/* class SumPair */ |
1216 |
|
|
1217 |
|
/* class OrderedPolynomialPair { */ |
1218 |
|
/* private: */ |
1219 |
|
/* Polynomial d_first; */ |
1220 |
|
/* Polynomial d_second; */ |
1221 |
|
/* public: */ |
1222 |
|
/* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */ |
1223 |
|
/* : d_first(f), */ |
1224 |
|
/* d_second(s) */ |
1225 |
|
/* {} */ |
1226 |
|
|
1227 |
|
/* /\** Returns the first part of the pair. *\/ */ |
1228 |
|
/* const Polynomial& getFirst() const { */ |
1229 |
|
/* return d_first; */ |
1230 |
|
/* } */ |
1231 |
|
|
1232 |
|
/* /\** Returns the second part of the pair. *\/ */ |
1233 |
|
/* const Polynomial& getSecond() const { */ |
1234 |
|
/* return d_second; */ |
1235 |
|
/* } */ |
1236 |
|
|
1237 |
|
/* OrderedPolynomialPair operator*(const Constant& c) const; */ |
1238 |
|
/* OrderedPolynomialPair operator+(const Polynomial& p) const; */ |
1239 |
|
|
1240 |
|
/* /\** Returns true if both of the polynomials are constant. *\/ */ |
1241 |
|
/* bool isConstant() const; */ |
1242 |
|
|
1243 |
|
/* /\** */ |
1244 |
|
/* * Evaluates an isConstant() ordered pair as if */ |
1245 |
|
/* * (k getFirst() getRight()) */ |
1246 |
|
/* *\/ */ |
1247 |
|
/* bool evaluateConstant(Kind k) const; */ |
1248 |
|
|
1249 |
|
/* /\** */ |
1250 |
|
/* * Returns the Least Common Multiple of the monomials */ |
1251 |
|
/* * on the lefthand side and the constant on the right. */ |
1252 |
|
/* *\/ */ |
1253 |
|
/* Integer denominatorLCM() const; */ |
1254 |
|
|
1255 |
|
/* /\** Constructs a SumPair. *\/ */ |
1256 |
|
/* SumPair toSumPair() const; */ |
1257 |
|
|
1258 |
|
|
1259 |
|
/* OrderedPolynomialPair divideByGCD() const; */ |
1260 |
|
/* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */ |
1261 |
|
|
1262 |
|
/* /\** */ |
1263 |
|
/* * Returns true if all of the variables are integers, */ |
1264 |
|
/* * and the coefficients are integers. */ |
1265 |
|
/* *\/ */ |
1266 |
|
/* bool isIntegral() const; */ |
1267 |
|
|
1268 |
|
/* /\** Returns true if all of the variables are integers. *\/ */ |
1269 |
|
/* bool allIntegralVariables() const { */ |
1270 |
|
/* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */ |
1271 |
|
/* } */ |
1272 |
|
/* }; */ |
1273 |
|
|
1274 |
7845395 |
class Comparison : public NodeWrapper { |
1275 |
|
private: |
1276 |
|
|
1277 |
|
static Node toNode(Kind k, const Polynomial& l, const Constant& c); |
1278 |
|
static Node toNode(Kind k, const Polynomial& l, const Polynomial& r); |
1279 |
|
|
1280 |
|
Comparison(TNode n); |
1281 |
|
|
1282 |
|
/** |
1283 |
|
* Creates a node in normal form equivalent to (= l 0). |
1284 |
|
* All variables in l are integral. |
1285 |
|
*/ |
1286 |
|
static Node mkIntEquality(const Polynomial& l); |
1287 |
|
|
1288 |
|
/** |
1289 |
|
* Creates a comparison equivalent to (k l 0). |
1290 |
|
* k is either GT or GEQ. |
1291 |
|
* All variables in l are integral. |
1292 |
|
*/ |
1293 |
|
static Node mkIntInequality(Kind k, const Polynomial& l); |
1294 |
|
|
1295 |
|
/** |
1296 |
|
* Creates a node equivalent to (= l 0). |
1297 |
|
* It is not the case that all variables in l are integral. |
1298 |
|
*/ |
1299 |
|
static Node mkRatEquality(const Polynomial& l); |
1300 |
|
|
1301 |
|
/** |
1302 |
|
* Creates a comparison equivalent to (k l 0). |
1303 |
|
* k is either GT or GEQ. |
1304 |
|
* It is not the case that all variables in l are integral. |
1305 |
|
*/ |
1306 |
|
static Node mkRatInequality(Kind k, const Polynomial& l); |
1307 |
|
|
1308 |
|
public: |
1309 |
|
|
1310 |
301535 |
Comparison(bool val) : |
1311 |
301535 |
NodeWrapper(NodeManager::currentNM()->mkConst(val)) |
1312 |
301535 |
{ } |
1313 |
|
|
1314 |
|
/** |
1315 |
|
* Given a literal to TheoryArith return a single kind to |
1316 |
|
* to indicate its underlying structure. |
1317 |
|
* The function returns the following in each case: |
1318 |
|
* - (K left right) -> K where is either EQUAL, GT, or GEQ |
1319 |
|
* - (CONST_BOOLEAN b) -> CONST_BOOLEAN |
1320 |
|
* - (NOT (EQUAL left right)) -> DISTINCT |
1321 |
|
* - (NOT (GT left right)) -> LEQ |
1322 |
|
* - (NOT (GEQ left right)) -> LT |
1323 |
|
* If none of these match, it returns UNDEFINED_KIND. |
1324 |
|
*/ |
1325 |
|
static Kind comparisonKind(TNode literal); |
1326 |
|
|
1327 |
34587955 |
Kind comparisonKind() const { return comparisonKind(getNode()); } |
1328 |
|
|
1329 |
|
static Comparison mkComparison(Kind k, const Polynomial& l, const Polynomial& r); |
1330 |
|
|
1331 |
|
/** Returns true if the comparison is a boolean constant. */ |
1332 |
|
bool isBoolean() const; |
1333 |
|
|
1334 |
|
/** |
1335 |
|
* Returns true if the comparison is either a boolean term, |
1336 |
|
* in integer normal form or mixed normal form. |
1337 |
|
*/ |
1338 |
|
bool isNormalForm() const; |
1339 |
|
|
1340 |
|
private: |
1341 |
|
bool isNormalGT() const; |
1342 |
|
bool isNormalGEQ() const; |
1343 |
|
|
1344 |
|
bool isNormalLT() const; |
1345 |
|
bool isNormalLEQ() const; |
1346 |
|
|
1347 |
|
bool isNormalEquality() const; |
1348 |
|
bool isNormalDistinct() const; |
1349 |
|
bool isNormalEqualityOrDisequality() const; |
1350 |
|
|
1351 |
5947388 |
bool allIntegralVariables() const { |
1352 |
5947388 |
return getLeft().allIntegralVariables() && getRight().allIntegralVariables(); |
1353 |
|
} |
1354 |
|
bool rightIsConstant() const; |
1355 |
|
|
1356 |
|
public: |
1357 |
|
Polynomial getLeft() const; |
1358 |
|
Polynomial getRight() const; |
1359 |
|
|
1360 |
|
/* /\** Normal form check if at least one variable is real. *\/ */ |
1361 |
|
/* bool isMixedCompareNormalForm() const; */ |
1362 |
|
|
1363 |
|
/* /\** Normal form check if at least one variable is real. *\/ */ |
1364 |
|
/* bool isMixedEqualsNormalForm() const; */ |
1365 |
|
|
1366 |
|
/* /\** Normal form check is all variables are integer.*\/ */ |
1367 |
|
/* bool isIntegerCompareNormalForm() const; */ |
1368 |
|
|
1369 |
|
/* /\** Normal form check is all variables are integer.*\/ */ |
1370 |
|
/* bool isIntegerEqualsNormalForm() const; */ |
1371 |
|
|
1372 |
|
|
1373 |
|
/** |
1374 |
|
* Returns true if all of the variables are integers, the coefficients are integers, |
1375 |
|
* and the right hand coefficient is an integer. |
1376 |
|
*/ |
1377 |
|
bool debugIsIntegral() const; |
1378 |
|
|
1379 |
|
static Comparison parseNormalForm(TNode n); |
1380 |
|
|
1381 |
798586 |
inline static bool isNormalAtom(TNode n){ |
1382 |
1597172 |
Comparison parse = Comparison::parseNormalForm(n); |
1383 |
1597172 |
return parse.isNormalForm(); |
1384 |
|
} |
1385 |
|
|
1386 |
|
size_t getComplexity() const; |
1387 |
|
|
1388 |
|
SumPair toSumPair() const; |
1389 |
|
|
1390 |
|
Polynomial normalizedVariablePart() const; |
1391 |
|
DeltaRational normalizedDeltaRational() const; |
1392 |
|
|
1393 |
|
/** |
1394 |
|
* Transforms a Comparison object into a stronger normal form: |
1395 |
|
* Polynomial ~Kind~ Constant |
1396 |
|
* |
1397 |
|
* From the comparison, this method resolved a negation (if present) and |
1398 |
|
* moves everything to the left side. |
1399 |
|
* If split_constant is false, the constant is always zero. |
1400 |
|
* If split_constant is true, the polynomial has no constant term and is |
1401 |
|
* normalized to have leading coefficient one. |
1402 |
|
*/ |
1403 |
|
std::tuple<Polynomial, Kind, Constant> decompose( |
1404 |
|
bool split_constant = false) const; |
1405 |
|
|
1406 |
|
};/* class Comparison */ |
1407 |
|
|
1408 |
|
} // namespace arith |
1409 |
|
} // namespace theory |
1410 |
|
} // namespace cvc5 |
1411 |
|
|
1412 |
|
#endif /* CVC5__THEORY__ARITH__NORMAL_FORM_H */ |