1 /********************* */
2 /*! \file normal_form.h
4 ** Original author: Tim King
5 ** Major contributors: none
6 ** Minor contributors (to current version): Dejan Jovanovic, Morgan Deters
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief [[ Add one-line brief description here ]]
14 ** [[ Add lengthier description here ]]
15 ** \todo document this file
18 #include "cvc4_private.h"
20 #ifndef __CVC4__THEORY__ARITH__NORMAL_FORM_H
21 #define __CVC4__THEORY__ARITH__NORMAL_FORM_H
23 #include "expr/node.h"
24 #include "expr/node_self_iterator.h"
25 #include "util/rational.h"
26 #include "theory/arith/delta_rational.h"
27 //#include "theory/arith/arith_utilities.h"
31 #include <ext/algorithm>
37 /***********************************************/
38 /***************** Normal Form *****************/
39 /***********************************************/
40 /***********************************************/
43 * Section 1: Languages
44 * The normal form for arithmetic nodes is defined by the language
45 * accepted by the following BNFs with some guard conditions.
46 * (The guard conditions are in Section 3 for completeness.)
50 * n.isVar() or is foreign
51 * n.getType() \in {Integer, Real}
55 * n.getKind() == kind::CONST_RATIONAL
57 * var_list := variable | (* [variable])
60 * isSorted varOrder [variable]
62 * monomial := constant | var_list | (* constant' var_list')
64 * \f$ constant' \not\in {0,1} \f$
66 * polynomial := monomial' | (+ [monomial])
69 * isStrictlySorted monoOrder [monomial]
70 * forall (\x -> x != 0) [monomial]
72 * rational_cmp := (|><| qpolynomial constant)
75 * not (exists constantMonomial (monomialList qpolynomial))
76 * (exists realMonomial (monomialList qpolynomial))
77 * abs(monomialCoefficient (head (monomialList qpolynomial))) == 1
79 * integer_cmp := (>= zpolynomial constant)
81 * not (exists constantMonomial (monomialList zpolynomial))
82 * (forall integerMonomial (monomialList zpolynomial))
83 * the gcd of all numerators of coefficients is 1
84 * the denominator of all coefficients and the constant is 1
85 * the leading coefficient is positive
87 * rational_eq := (= qvarlist qpolynomial)
89 * let allMonomials = (cons qvarlist (monomialList zpolynomial))
90 * let variableMonomials = (drop constantMonomial allMonomials)
91 * isStrictlySorted variableMonomials
92 * exists realMonomial variableMonomials
93 * is not empty qvarlist
95 * integer_eq := (= zmonomial zpolynomial)
97 * let allMonomials = (cons zmonomial (monomialList zpolynomial))
98 * let variableMonomials = (drop constantMonomial allMonomials)
99 * not (constantMonomial zmonomial)
100 * (forall integerMonomial allMonomials)
101 * isStrictlySorted variableMonomials
102 * the gcd of all numerators of coefficients is 1
103 * the denominator of all coefficients and the constant is 1
104 * the coefficient of monomial is positive
105 * the value of the coefficient of monomial is minimal in variableMonomials
107 * comparison := TRUE | FALSE
108 * | rational_cmp | (not rational_cmp)
109 * | rational_eq | (not rational_eq)
110 * | integer_cmp | (not integer_cmp)
111 * | integer_eq | (not integer_eq)
113 * Normal Form for terms := polynomial
114 * Normal Form for atoms := comparison
118 * Section 2: Helper Classes
119 * The langauges accepted by each of these defintions
120 * roughly corresponds to one of the following helper classes:
128 * Each of the classes obeys the following contracts/design decisions:
129 * -Calling isMember(Node node) on a node returns true iff that node is a
130 * a member of the language. Note: isMember is O(n).
131 * -Calling isNormalForm() on a helper class object returns true iff that
132 * helper class currently represents a normal form object.
133 * -If isNormalForm() is false, then this object must have been made
134 * using a mk*() factory function.
135 * -If isNormalForm() is true, calling getNode() on all of these classes
136 * returns a node that would be accepted by the corresponding language.
137 * And if isNormalForm() is false, returns Node::null().
138 * -Each of the classes is immutable.
139 * -Public facing constuctors have a 1-to-1 correspondence with one of
140 * production rules in the above grammar.
141 * -Public facing constuctors are required to fail in debug mode when the
142 * guards of the production rule are not strictly met.
143 * For example: Monomial(Constant(1),VarList(Variable(x))) must fail.
144 * -When a class has a Class parseClass(Node node) function,
145 * if isMember(node) is true, the function is required to return an instance
146 * of the helper class, instance, s.t. instance.getNode() == node.
147 * And if isMember(node) is false, this throws an assertion failure in debug
148 * mode and has undefined behaviour if not in debug mode.
149 * -Only public facing constructors, parseClass(node), and mk*() functions are
150 * considered privileged functions for the helper class.
151 * -Only privileged functions may use private constructors, and access
152 * private data members.
153 * -All non-privileged functions are considered utility functions and
154 * must use a privileged function in order to create an instance of the class.
158 * Section 3: Guard Conditions Misc.
161 * variable_order x y =
162 * if (meta_kind_variable x) and (meta_kind_variable y)
163 * then node_order x y
164 * else if (meta_kind_variable x)
166 * else if (meta_kind_variable y)
168 * else node_order x y
173 * | (* [variable]) -> len [variable]
177 * Empty -> (0,Node::null())
178 * | NonEmpty(vl) -> (var_list_len vl, vl)
180 * var_listOrder a b = tuple_cmp (order a) (order b)
182 * monomialVarList monomial =
183 * match monomial with
185 * | var_list -> NonEmpty(var_list)
186 * | (* constant' var_list') -> NonEmpty(var_list')
188 * monoOrder m0 m1 = var_listOrder (monomialVarList m0) (monomialVarList m1)
190 * integerMonomial mono =
191 * forall varHasTypeInteger (monomialVarList mono)
193 * realMonomial mono = not (integerMonomial mono)
195 * constantMonomial monomial =
196 * match monomial with
198 * | var_list -> false
199 * | (* constant' var_list') -> false
201 * monomialCoefficient monomial =
202 * match monomial with
203 * constant -> constant
204 * | var_list -> Constant(1)
205 * | (* constant' var_list') -> constant'
207 * monomialList polynomial =
208 * match polynomial with
209 * monomial -> monomial::[]
210 * | (+ [monomial]) -> [monomial]
214 * A NodeWrapper is a class that is a thinly veiled container of a Node object.
220 NodeWrapper(Node n
) : node(n
) {}
221 const Node
& getNode() const { return node
; }
222 };/* class NodeWrapper */
225 class Variable
: public NodeWrapper
{
227 Variable(Node n
) : NodeWrapper(n
) {
228 Assert(isMember(getNode()));
231 // TODO: check if it's a theory leaf also
232 static bool isMember(Node n
) {
233 Kind k
= n
.getKind();
235 case kind::CONST_RATIONAL
:
237 case kind::INTS_DIVISION
:
238 case kind::INTS_MODULUS
:
240 case kind::INTS_DIVISION_TOTAL
:
241 case kind::INTS_MODULUS_TOTAL
:
242 case kind::DIVISION_TOTAL
:
243 return isDivMember(n
);
245 case kind::TO_INTEGER
:
246 // Treat to_int as a variable; it is replaced in early preprocessing
250 return isLeafMember(n
);
254 static bool isLeafMember(Node n
);
255 static bool isDivMember(Node n
);
256 bool isDivLike() const{
257 return isDivMember(getNode());
260 bool isNormalForm() { return isMember(getNode()); }
262 bool isIntegral() const {
263 return getNode().getType().isInteger();
266 bool isMetaKindVariable() const {
267 return getNode().isVar();
270 bool operator<(const Variable
& v
) const {
271 bool thisIsVariable
= isMetaKindVariable();
272 bool vIsVariable
= v
.isMetaKindVariable();
274 if(thisIsVariable
== vIsVariable
){
275 bool thisIsInteger
= isIntegral();
276 bool vIsInteger
= v
.isIntegral();
277 if(thisIsInteger
== vIsInteger
){
278 return getNode() < v
.getNode();
280 return thisIsInteger
&& !vIsInteger
;
283 return thisIsVariable
&& !vIsVariable
;
287 bool operator==(const Variable
& v
) const { return getNode() == v
.getNode();}
289 size_t getComplexity() const;
290 };/* class Variable */
293 class Constant
: public NodeWrapper
{
295 Constant(Node n
) : NodeWrapper(n
) {
296 Assert(isMember(getNode()));
299 static bool isMember(Node n
) {
300 return n
.getKind() == kind::CONST_RATIONAL
;
303 bool isNormalForm() { return isMember(getNode()); }
305 static Constant
mkConstant(Node n
) {
306 Assert(n
.getKind() == kind::CONST_RATIONAL
);
310 static Constant
mkConstant(const Rational
& rat
);
312 static Constant
mkZero() {
313 return mkConstant(Rational(0));
316 static Constant
mkOne() {
317 return mkConstant(Rational(1));
320 const Rational
& getValue() const {
321 return getNode().getConst
<Rational
>();
324 static int absCmp(const Constant
& a
, const Constant
& b
);
325 bool isIntegral() const { return getValue().isIntegral(); }
327 int sgn() const { return getValue().sgn(); }
329 bool isZero() const { return sgn() == 0; }
330 bool isNegative() const { return sgn() < 0; }
331 bool isPositive() const { return sgn() > 0; }
333 bool isOne() const { return getValue() == 1; }
335 Constant
operator*(const Rational
& other
) const {
336 return mkConstant(getValue() * other
);
339 Constant
operator*(const Constant
& other
) const {
340 return mkConstant(getValue() * other
.getValue());
342 Constant
operator+(const Constant
& other
) const {
343 return mkConstant(getValue() + other
.getValue());
345 Constant
operator-() const {
346 return mkConstant(-getValue());
349 Constant
inverse() const{
351 return mkConstant(getValue().inverse());
354 bool operator<(const Constant
& other
) const {
355 return getValue() < other
.getValue();
358 bool operator==(const Constant
& other
) const {
359 //Rely on node uniqueness.
360 return getNode() == other
.getNode();
363 Constant
abs() const {
371 uint32_t length() const{
372 Assert(isIntegral());
373 return getValue().getNumerator().length();
376 size_t getComplexity() const;
378 };/* class Constant */
381 template <class GetNodeIterator
>
382 inline Node
makeNode(Kind k
, GetNodeIterator start
, GetNodeIterator end
) {
385 while(start
!= end
) {
386 nb
<< (*start
).getNode();
391 }/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */
394 template <class GetNodeIterator
, class T
>
395 static void copy_range(GetNodeIterator begin
, GetNodeIterator end
, std::vector
<T
>& result
){
397 result
.push_back(*begin
);
402 template <class GetNodeIterator
, class T
>
403 static void merge_ranges(GetNodeIterator first1
,
404 GetNodeIterator last1
,
405 GetNodeIterator first2
,
406 GetNodeIterator last2
,
407 std::vector
<T
>& result
) {
409 while(first1
!= last1
&& first2
!= last2
){
410 if( (*first1
) < (*first2
) ){
411 result
.push_back(*first1
);
414 result
.push_back(*first2
);
418 copy_range(first1
, last1
, result
);
419 copy_range(first2
, last2
, result
);
423 * A VarList is a sorted list of variables representing a product.
424 * If the VarList is empty, it represents an empty product or 1.
425 * If the VarList has size 1, it represents a single variable.
427 * A non-sorted VarList can never be successfully made in debug mode.
429 class VarList
: public NodeWrapper
{
432 static Node
multList(const std::vector
<Variable
>& list
) {
433 Assert(list
.size() >= 2);
435 return makeNode(kind::MULT
, list
.begin(), list
.end());
438 VarList() : NodeWrapper(Node::null()) {}
440 VarList(Node n
) : NodeWrapper(n
) {
441 Assert(isSorted(begin(), end()));
444 typedef expr::NodeSelfIterator internal_iterator
;
446 internal_iterator
internalBegin() const {
448 return expr::NodeSelfIterator::self(getNode());
450 return getNode().begin();
454 internal_iterator
internalEnd() const {
456 return expr::NodeSelfIterator::selfEnd(getNode());
458 return getNode().end();
466 internal_iterator d_iter
;
469 explicit iterator(internal_iterator i
) : d_iter(i
) {}
471 inline Variable
operator*() {
472 return Variable(*d_iter
);
475 bool operator==(const iterator
& i
) {
476 return d_iter
== i
.d_iter
;
479 bool operator!=(const iterator
& i
) {
480 return d_iter
!= i
.d_iter
;
483 iterator
operator++() {
488 iterator
operator++(int) {
489 return iterator(d_iter
++);
493 iterator
begin() const {
494 return iterator(internalBegin());
497 iterator
end() const {
498 return iterator(internalEnd());
501 Variable
getHead() const {
506 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
507 Assert(isSorted(begin(), end()));
510 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
511 Assert(l
.size() >= 2);
512 Assert(isSorted(begin(), end()));
515 static bool isMember(Node n
);
517 bool isNormalForm() const {
521 static VarList
mkEmptyVarList() {
526 /** There are no restrictions on the size of l */
527 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
529 return mkEmptyVarList();
530 } else if(l
.size() == 1) {
531 return VarList((*l
.begin()).getNode());
537 bool empty() const { return getNode().isNull(); }
538 bool singleton() const {
539 return !empty() && getNode().getKind() != kind::MULT
;
546 return getNode().getNumChildren();
549 static VarList
parseVarList(Node n
);
551 VarList
operator*(const VarList
& vl
) const;
553 int cmp(const VarList
& vl
) const;
555 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
557 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
559 bool isIntegral() const {
560 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
562 if(!var
.isIntegral()){
568 size_t getComplexity() const;
571 bool isSorted(iterator start
, iterator end
);
573 };/* class VarList */
576 class Monomial
: public NodeWrapper
{
580 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
581 NodeWrapper(n
), constant(c
), varList(vl
)
583 Assert(!c
.isZero() || vl
.empty() );
584 Assert( c
.isZero() || !vl
.empty() );
586 Assert(!c
.isOne() || !multStructured(n
));
589 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
593 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
596 static bool multStructured(Node n
) {
597 return n
.getKind() == kind::MULT
&&
598 n
[0].getKind() == kind::CONST_RATIONAL
&&
599 n
.getNumChildren() == 2;
604 Monomial(const Constant
& c
):
605 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
608 Monomial(const VarList
& vl
):
609 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
611 Assert( !varList
.empty() );
614 Monomial(const Constant
& c
, const VarList
& vl
):
615 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
617 Assert( !c
.isZero() );
618 Assert( !c
.isOne() );
619 Assert( !varList
.empty() );
621 Assert(multStructured(getNode()));
624 static bool isMember(TNode n
);
626 /** Makes a monomial with no restrictions on c and vl. */
627 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
629 static Monomial
mkMonomial(const Variable
& v
){
630 return Monomial(VarList(v
));
633 static Monomial
parseMonomial(Node n
);
635 static Monomial
mkZero() {
636 return Monomial(Constant::mkConstant(0));
638 static Monomial
mkOne() {
639 return Monomial(Constant::mkConstant(1));
641 const Constant
& getConstant() const { return constant
; }
642 const VarList
& getVarList() const { return varList
; }
644 bool isConstant() const {
645 return varList
.empty();
648 bool isZero() const {
649 return constant
.isZero();
652 bool coefficientIsOne() const {
653 return constant
.isOne();
656 bool absCoefficientIsOne() const {
657 return coefficientIsOne() || constant
.getValue() == -1;
660 bool constantIsPositive() const {
661 return getConstant().isPositive();
664 Monomial
operator*(const Rational
& q
) const;
665 Monomial
operator*(const Constant
& c
) const;
666 Monomial
operator*(const Monomial
& mono
) const;
668 Monomial
operator-() const{
669 return (*this) * Rational(-1);
673 int cmp(const Monomial
& mono
) const {
674 return getVarList().cmp(mono
.getVarList());
677 bool operator<(const Monomial
& vl
) const {
681 bool operator==(const Monomial
& vl
) const {
685 static bool isSorted(const std::vector
<Monomial
>& m
) {
686 return __gnu_cxx::is_sorted(m
.begin(), m
.end());
689 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
690 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
693 static void sort(std::vector
<Monomial
>& m
);
694 static void combineAdjacentMonomials(std::vector
<Monomial
>& m
);
697 * The variable product
699 bool integralVariables() const {
700 return getVarList().isIntegral();
704 * The coefficient of the monomial is integral.
706 bool integralCoefficient() const {
707 return getConstant().isIntegral();
711 * A Monomial is an "integral" monomial if the constant is integral.
713 bool isIntegral() const {
714 return integralCoefficient() && integralVariables();
717 /** Returns true if the VarList is a product of at least 2 Variables.*/
718 bool isNonlinear() const {
719 return getVarList().size() >= 2;
723 * Given a sorted list of monomials, this function transforms this
724 * into a strictly sorted list of monomials that does not contain zero.
726 //static std::vector<Monomial> sumLikeTerms(const std::vector<Monomial>& monos);
728 int absCmp(const Monomial
& other
) const{
729 return getConstant().getValue().absCmp(other
.getConstant().getValue());
731 // bool absLessThan(const Monomial& other) const{
732 // return getConstant().abs() < other.getConstant().abs();
735 uint32_t coefficientLength() const{
736 return getConstant().length();
740 static void printList(const std::vector
<Monomial
>& list
);
742 size_t getComplexity() const;
743 };/* class Monomial */
748 class Polynomial
: public NodeWrapper
{
752 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
753 Assert(isMember(getNode()));
756 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
757 Assert(m
.size() >= 2);
759 return makeNode(kind::PLUS
, m
.begin(), m
.end());
762 typedef expr::NodeSelfIterator internal_iterator
;
764 internal_iterator
internalBegin() const {
766 return expr::NodeSelfIterator::self(getNode());
768 return getNode().begin();
772 internal_iterator
internalEnd() const {
774 return expr::NodeSelfIterator::selfEnd(getNode());
776 return getNode().end();
780 bool singleton() const { return d_singleton
; }
783 static bool isMember(TNode n
) {
784 if(Monomial::isMember(n
)){
786 }else if(n
.getKind() == kind::PLUS
){
787 Assert(n
.getNumChildren() >= 2);
788 Node::iterator currIter
= n
.begin(), end
= n
.end();
789 Node prev
= *currIter
;
790 if(!Monomial::isMember(prev
)){
794 Monomial mprev
= Monomial::parseMonomial(prev
);
796 for(; currIter
!= end
; ++currIter
){
797 Node curr
= *currIter
;
798 if(!Monomial::isMember(curr
)){
801 Monomial mcurr
= Monomial::parseMonomial(curr
);
802 if(!(mprev
< mcurr
)){
815 internal_iterator d_iter
;
818 explicit iterator(internal_iterator i
) : d_iter(i
) {}
820 inline Monomial
operator*() {
821 return Monomial::parseMonomial(*d_iter
);
824 bool operator==(const iterator
& i
) {
825 return d_iter
== i
.d_iter
;
828 bool operator!=(const iterator
& i
) {
829 return d_iter
!= i
.d_iter
;
832 iterator
operator++() {
837 iterator
operator++(int) {
838 return iterator(d_iter
++);
842 iterator
begin() const { return iterator(internalBegin()); }
843 iterator
end() const { return iterator(internalEnd()); }
845 Polynomial(const Monomial
& m
):
846 NodeWrapper(m
.getNode()), d_singleton(true)
849 Polynomial(const std::vector
<Monomial
>& m
):
850 NodeWrapper(makePlusNode(m
)), d_singleton(false)
852 Assert( m
.size() >= 2);
853 Assert( Monomial::isStrictlySorted(m
) );
856 static Polynomial
mkPolynomial(const Variable
& v
){
857 return Monomial::mkMonomial(v
);
860 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
862 return Polynomial(Monomial::mkZero());
863 } else if(m
.size() == 1) {
864 return Polynomial((*m
.begin()));
866 return Polynomial(m
);
870 static Polynomial
parsePolynomial(Node n
) {
871 return Polynomial(n
);
874 static Polynomial
mkZero() {
875 return Polynomial(Monomial::mkZero());
877 static Polynomial
mkOne() {
878 return Polynomial(Monomial::mkOne());
880 bool isZero() const {
881 return singleton() && (getHead().isZero());
884 bool isConstant() const {
885 return singleton() && (getHead().isConstant());
888 bool containsConstant() const {
889 return getHead().isConstant();
892 uint32_t size() const{
896 Assert(getNode().getKind() == kind::PLUS
);
897 return getNode().getNumChildren();
901 Monomial
getHead() const {
905 Polynomial
getTail() const {
906 Assert(! singleton());
908 iterator tailStart
= begin();
910 std::vector
<Monomial
> subrange
;
911 copy_range(tailStart
, end(), subrange
);
912 return mkPolynomial(subrange
);
915 Monomial
minimumVariableMonomial() const;
916 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
918 void printList() const {
919 if(Debug
.isOn("normal-form")){
920 Debug("normal-form") << "start list" << std::endl
;
921 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
922 const Monomial
& m
=*i
;
925 Debug("normal-form") << "end list" << std::endl
;
929 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
930 bool allIntegralVariables() const {
931 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
932 if(!(*i
).integralVariables()){
940 * A Polynomial is an "integral" polynomial if all of the monomials are integral
941 * and all of the coefficients are Integral. */
942 bool isIntegral() const {
943 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
944 if(!(*i
).isIntegral()){
951 static Polynomial
sumPolynomials(const std::vector
<Polynomial
>& polynomials
);
953 /** Returns true if the polynomial contains a non-linear monomial.*/
954 bool isNonlinear() const;
958 * Selects a minimal monomial in the polynomial by the absolute value of
961 Monomial
selectAbsMinimum() const;
963 /** Returns true if the absolute value of the head coefficient is one. */
964 bool leadingCoefficientIsAbsOne() const;
965 bool leadingCoefficientIsPositive() const;
966 bool denominatorLCMIsOne() const;
967 bool numeratorGCDIsOne() const;
969 bool signNormalizedReducedSum() const {
970 return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne();
974 * Returns the Least Common Multiple of the denominators of the coefficients
977 Integer
denominatorLCM() const;
980 * Returns the GCD of the numerators of the monomials.
981 * Requires this to be an isIntegral() polynomial.
983 Integer
numeratorGCD() const;
986 * Returns the GCD of the coefficients of the monomials.
987 * Requires this to be an isIntegral() polynomial.
991 Polynomial
exactDivide(const Integer
& z
) const {
992 Assert(isIntegral());
993 Constant invz
= Constant::mkConstant(Rational(1,z
));
994 Polynomial prod
= (*this) * Monomial(invz
);
995 Assert(prod
.isIntegral());
999 Polynomial
operator+(const Polynomial
& vl
) const;
1000 Polynomial
operator-(const Polynomial
& vl
) const;
1001 Polynomial
operator-() const{
1002 return (*this) * Rational(-1);
1005 Polynomial
operator*(const Rational
& q
) const;
1006 Polynomial
operator*(const Constant
& c
) const;
1007 Polynomial
operator*(const Monomial
& mono
) const;
1009 Polynomial
operator*(const Polynomial
& poly
) const;
1012 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
1013 * The quotient and remainder of p divided by the non-zero integer z is:
1014 * q := [(* floor(coeff_i/z) mono_i )]
1015 * r := [(* rem(coeff_i/z) mono_i)]
1016 * computeQR(p,z) returns the node (+ q r).
1018 * q and r are members of the Polynomial class.
1020 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
1021 * (+ (+ 2 x (* 4 y)) (+ 1 x))
1023 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
1025 /** Returns the coefficient associated with the VarList in the polynomial. */
1026 Constant
getCoefficient(const VarList
& vl
) const;
1028 uint32_t maxLength() const{
1029 iterator i
= begin(), e
=end();
1033 uint32_t max
= (*i
).coefficientLength();
1036 uint32_t curr
= (*i
).coefficientLength();
1045 uint32_t numMonomials() const {
1046 if( getNode().getKind() == kind::PLUS
){
1047 return getNode().getNumChildren();
1055 const Rational
& asConstant() const{
1056 Assert(isConstant());
1057 return getNode().getConst
<Rational
>();
1058 //return getHead().getConstant().getValue();
1061 bool isVarList() const {
1063 return VarList::isMember(getNode());
1069 VarList
asVarList() const {
1070 Assert(isVarList());
1071 return getHead().getVarList();
1074 size_t getComplexity() const;
1076 friend class SumPair
;
1077 friend class Comparison
;
1079 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1080 Node
makeAbsCondition(Variable v
){
1081 return makeAbsCondition(v
, *this);
1084 /** Returns a node that if asserted ensures v is the abs of p.*/
1085 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1087 };/* class Polynomial */
1091 * SumPair is a utility class that extends polynomials for use in computations.
1092 * A SumPair is always a combination of (+ p c) where
1093 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1095 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1096 * is known to implicitly be equal to 0.
1098 * SumPairs do not have unique representations due to the potential for p = 0.
1099 * This makes them inappropriate for normal forms.
1101 class SumPair
: public NodeWrapper
{
1103 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1104 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1110 Assert(isNormalForm());
1115 SumPair(const Polynomial
& p
):
1116 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1118 Assert(isNormalForm());
1121 SumPair(const Polynomial
& p
, const Constant
& c
):
1122 NodeWrapper(toNode(p
, c
))
1124 Assert(isNormalForm());
1127 static bool isMember(TNode n
) {
1128 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1129 if(Constant::isMember(n
[1])){
1130 if(Polynomial::isMember(n
[0])){
1131 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1132 return p
.isZero() || (!p
.containsConstant());
1144 bool isNormalForm() const {
1145 return isMember(getNode());
1148 Polynomial
getPolynomial() const {
1149 return Polynomial::parsePolynomial(getNode()[0]);
1152 Constant
getConstant() const {
1153 return Constant::mkConstant((getNode())[1]);
1156 SumPair
operator+(const SumPair
& other
) const {
1157 return SumPair(getPolynomial() + other
.getPolynomial(),
1158 getConstant() + other
.getConstant());
1161 SumPair
operator*(const Constant
& c
) const {
1162 return SumPair(getPolynomial() * c
, getConstant() * c
);
1165 SumPair
operator-(const SumPair
& other
) const {
1166 return (*this) + (other
* Constant::mkConstant(-1));
1169 static SumPair
mkSumPair(const Polynomial
& p
);
1171 static SumPair
mkSumPair(const Variable
& var
){
1172 return SumPair(Polynomial::mkPolynomial(var
));
1175 static SumPair
parseSumPair(TNode n
){
1179 bool isIntegral() const{
1180 return getConstant().isIntegral() && getPolynomial().isIntegral();
1183 bool isConstant() const {
1184 return getPolynomial().isZero();
1187 bool isZero() const {
1188 return getConstant().isZero() && isConstant();
1191 uint32_t size() const{
1192 return getPolynomial().size();
1195 bool isNonlinear() const{
1196 return getPolynomial().isNonlinear();
1200 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1201 * The SumPair must be integral.
1203 Integer
gcd() const {
1204 Assert(isIntegral());
1205 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1208 uint32_t maxLength() const {
1209 Assert(isIntegral());
1210 return std::max(getPolynomial().maxLength(), getConstant().length());
1213 static SumPair
mkZero() {
1214 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1217 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1219 };/* class SumPair */
1221 /* class OrderedPolynomialPair { */
1223 /* Polynomial d_first; */
1224 /* Polynomial d_second; */
1226 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1231 /* /\** Returns the first part of the pair. *\/ */
1232 /* const Polynomial& getFirst() const { */
1233 /* return d_first; */
1236 /* /\** Returns the second part of the pair. *\/ */
1237 /* const Polynomial& getSecond() const { */
1238 /* return d_second; */
1241 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1242 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1244 /* /\** Returns true if both of the polynomials are constant. *\/ */
1245 /* bool isConstant() const; */
1248 /* * Evaluates an isConstant() ordered pair as if */
1249 /* * (k getFirst() getRight()) */
1251 /* bool evaluateConstant(Kind k) const; */
1254 /* * Returns the Least Common Multiple of the monomials */
1255 /* * on the lefthand side and the constant on the right. */
1257 /* Integer denominatorLCM() const; */
1259 /* /\** Constructs a SumPair. *\/ */
1260 /* SumPair toSumPair() const; */
1263 /* OrderedPolynomialPair divideByGCD() const; */
1264 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1267 /* * Returns true if all of the variables are integers, */
1268 /* * and the coefficients are integers. */
1270 /* bool isIntegral() const; */
1272 /* /\** Returns true if all of the variables are integers. *\/ */
1273 /* bool allIntegralVariables() const { */
1274 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1278 class Comparison
: public NodeWrapper
{
1281 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1282 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1284 Comparison(TNode n
);
1287 * Creates a node in normal form equivalent to (= l 0).
1288 * All variables in l are integral.
1290 static Node
mkIntEquality(const Polynomial
& l
);
1293 * Creates a comparison equivalent to (k l 0).
1294 * k is either GT or GEQ.
1295 * All variables in l are integral.
1297 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1300 * Creates a node equivalent to (= l 0).
1301 * It is not the case that all variables in l are integral.
1303 static Node
mkRatEquality(const Polynomial
& l
);
1306 * Creates a comparison equivalent to (k l 0).
1307 * k is either GT or GEQ.
1308 * It is not the case that all variables in l are integral.
1310 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1314 Comparison(bool val
) :
1315 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1319 * Given a literal to TheoryArith return a single kind to
1320 * to indicate its underlying structure.
1321 * The function returns the following in each case:
1322 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1323 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1324 * - (NOT (EQUAL left right)) -> DISTINCT
1325 * - (NOT (GT left right)) -> LEQ
1326 * - (NOT (GEQ left right)) -> LT
1327 * If none of these match, it returns UNDEFINED_KIND.
1329 static Kind
comparisonKind(TNode literal
);
1331 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1333 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1335 /** Returns true if the comparison is a boolean constant. */
1336 bool isBoolean() const;
1339 * Returns true if the comparison is either a boolean term,
1340 * in integer normal form or mixed normal form.
1342 bool isNormalForm() const;
1345 bool isNormalGT() const;
1346 bool isNormalGEQ() const;
1348 bool isNormalLT() const;
1349 bool isNormalLEQ() const;
1351 bool isNormalEquality() const;
1352 bool isNormalDistinct() const;
1353 bool isNormalEqualityOrDisequality() const;
1355 bool allIntegralVariables() const {
1356 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1358 bool rightIsConstant() const;
1361 Polynomial
getLeft() const;
1362 Polynomial
getRight() const;
1364 /* /\** Normal form check if at least one variable is real. *\/ */
1365 /* bool isMixedCompareNormalForm() const; */
1367 /* /\** Normal form check if at least one variable is real. *\/ */
1368 /* bool isMixedEqualsNormalForm() const; */
1370 /* /\** Normal form check is all variables are integer.*\/ */
1371 /* bool isIntegerCompareNormalForm() const; */
1373 /* /\** Normal form check is all variables are integer.*\/ */
1374 /* bool isIntegerEqualsNormalForm() const; */
1378 * Returns true if all of the variables are integers, the coefficients are integers,
1379 * and the right hand coefficient is an integer.
1381 bool debugIsIntegral() const;
1383 static Comparison
parseNormalForm(TNode n
);
1385 inline static bool isNormalAtom(TNode n
){
1386 Comparison parse
= Comparison::parseNormalForm(n
);
1387 return parse
.isNormalForm();
1390 size_t getComplexity() const;
1392 SumPair
toSumPair() const;
1394 Polynomial
normalizedVariablePart() const;
1395 DeltaRational
normalizedDeltaRational() const;
1397 };/* class Comparison */
1399 }/* CVC4::theory::arith namespace */
1400 }/* CVC4::theory namespace */
1401 }/* CVC4 namespace */
1403 #endif /* __CVC4__THEORY__ARITH__NORMAL_FORM_H */