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"
32 #if IS_SORTED_IN_GNUCXX_NAMESPACE
33 # include <ext/algorithm>
34 #endif /* IS_SORTED_IN_GNUCXX_NAMESPACE */
40 /***********************************************/
41 /***************** Normal Form *****************/
42 /***********************************************/
43 /***********************************************/
46 * Section 1: Languages
47 * The normal form for arithmetic nodes is defined by the language
48 * accepted by the following BNFs with some guard conditions.
49 * (The guard conditions are in Section 3 for completeness.)
53 * n.isVar() or is foreign
54 * n.getType() \in {Integer, Real}
58 * n.getKind() == kind::CONST_RATIONAL
60 * var_list := variable | (* [variable])
63 * isSorted varOrder [variable]
65 * monomial := constant | var_list | (* constant' var_list')
67 * \f$ constant' \not\in {0,1} \f$
69 * polynomial := monomial' | (+ [monomial])
72 * isStrictlySorted monoOrder [monomial]
73 * forall (\x -> x != 0) [monomial]
75 * rational_cmp := (|><| qpolynomial constant)
78 * not (exists constantMonomial (monomialList qpolynomial))
79 * (exists realMonomial (monomialList qpolynomial))
80 * abs(monomialCoefficient (head (monomialList qpolynomial))) == 1
82 * integer_cmp := (>= zpolynomial constant)
84 * not (exists constantMonomial (monomialList zpolynomial))
85 * (forall integerMonomial (monomialList zpolynomial))
86 * the gcd of all numerators of coefficients is 1
87 * the denominator of all coefficients and the constant is 1
88 * the leading coefficient is positive
90 * rational_eq := (= qvarlist qpolynomial)
92 * let allMonomials = (cons qvarlist (monomialList zpolynomial))
93 * let variableMonomials = (drop constantMonomial allMonomials)
94 * isStrictlySorted variableMonomials
95 * exists realMonomial variableMonomials
96 * is not empty qvarlist
98 * integer_eq := (= zmonomial zpolynomial)
100 * let allMonomials = (cons zmonomial (monomialList zpolynomial))
101 * let variableMonomials = (drop constantMonomial allMonomials)
102 * not (constantMonomial zmonomial)
103 * (forall integerMonomial allMonomials)
104 * isStrictlySorted variableMonomials
105 * the gcd of all numerators of coefficients is 1
106 * the denominator of all coefficients and the constant is 1
107 * the coefficient of monomial is positive
108 * the value of the coefficient of monomial is minimal in variableMonomials
110 * comparison := TRUE | FALSE
111 * | rational_cmp | (not rational_cmp)
112 * | rational_eq | (not rational_eq)
113 * | integer_cmp | (not integer_cmp)
114 * | integer_eq | (not integer_eq)
116 * Normal Form for terms := polynomial
117 * Normal Form for atoms := comparison
121 * Section 2: Helper Classes
122 * The langauges accepted by each of these defintions
123 * roughly corresponds to one of the following helper classes:
131 * Each of the classes obeys the following contracts/design decisions:
132 * -Calling isMember(Node node) on a node returns true iff that node is a
133 * a member of the language. Note: isMember is O(n).
134 * -Calling isNormalForm() on a helper class object returns true iff that
135 * helper class currently represents a normal form object.
136 * -If isNormalForm() is false, then this object must have been made
137 * using a mk*() factory function.
138 * -If isNormalForm() is true, calling getNode() on all of these classes
139 * returns a node that would be accepted by the corresponding language.
140 * And if isNormalForm() is false, returns Node::null().
141 * -Each of the classes is immutable.
142 * -Public facing constuctors have a 1-to-1 correspondence with one of
143 * production rules in the above grammar.
144 * -Public facing constuctors are required to fail in debug mode when the
145 * guards of the production rule are not strictly met.
146 * For example: Monomial(Constant(1),VarList(Variable(x))) must fail.
147 * -When a class has a Class parseClass(Node node) function,
148 * if isMember(node) is true, the function is required to return an instance
149 * of the helper class, instance, s.t. instance.getNode() == node.
150 * And if isMember(node) is false, this throws an assertion failure in debug
151 * mode and has undefined behaviour if not in debug mode.
152 * -Only public facing constructors, parseClass(node), and mk*() functions are
153 * considered privileged functions for the helper class.
154 * -Only privileged functions may use private constructors, and access
155 * private data members.
156 * -All non-privileged functions are considered utility functions and
157 * must use a privileged function in order to create an instance of the class.
161 * Section 3: Guard Conditions Misc.
164 * variable_order x y =
165 * if (meta_kind_variable x) and (meta_kind_variable y)
166 * then node_order x y
167 * else if (meta_kind_variable x)
169 * else if (meta_kind_variable y)
171 * else node_order x y
176 * | (* [variable]) -> len [variable]
180 * Empty -> (0,Node::null())
181 * | NonEmpty(vl) -> (var_list_len vl, vl)
183 * var_listOrder a b = tuple_cmp (order a) (order b)
185 * monomialVarList monomial =
186 * match monomial with
188 * | var_list -> NonEmpty(var_list)
189 * | (* constant' var_list') -> NonEmpty(var_list')
191 * monoOrder m0 m1 = var_listOrder (monomialVarList m0) (monomialVarList m1)
193 * integerMonomial mono =
194 * forall varHasTypeInteger (monomialVarList mono)
196 * realMonomial mono = not (integerMonomial mono)
198 * constantMonomial monomial =
199 * match monomial with
201 * | var_list -> false
202 * | (* constant' var_list') -> false
204 * monomialCoefficient monomial =
205 * match monomial with
206 * constant -> constant
207 * | var_list -> Constant(1)
208 * | (* constant' var_list') -> constant'
210 * monomialList polynomial =
211 * match polynomial with
212 * monomial -> monomial::[]
213 * | (+ [monomial]) -> [monomial]
217 * A NodeWrapper is a class that is a thinly veiled container of a Node object.
223 NodeWrapper(Node n
) : node(n
) {}
224 const Node
& getNode() const { return node
; }
225 };/* class NodeWrapper */
228 class Variable
: public NodeWrapper
{
230 Variable(Node n
) : NodeWrapper(n
) {
231 Assert(isMember(getNode()));
234 // TODO: check if it's a theory leaf also
235 static bool isMember(Node n
) {
236 Kind k
= n
.getKind();
238 case kind::CONST_RATIONAL
:
240 case kind::INTS_DIVISION
:
241 case kind::INTS_MODULUS
:
243 case kind::INTS_DIVISION_TOTAL
:
244 case kind::INTS_MODULUS_TOTAL
:
245 case kind::DIVISION_TOTAL
:
246 return isDivMember(n
);
248 case kind::TO_INTEGER
:
249 // Treat to_int as a variable; it is replaced in early preprocessing
253 return isLeafMember(n
);
257 static bool isLeafMember(Node n
);
258 static bool isDivMember(Node n
);
259 bool isDivLike() const{
260 return isDivMember(getNode());
263 bool isNormalForm() { return isMember(getNode()); }
265 bool isIntegral() const {
266 return getNode().getType().isInteger();
269 bool isMetaKindVariable() const {
270 return getNode().isVar();
273 bool operator<(const Variable
& v
) const {
275 return cmp(this->getNode(), v
.getNode());
278 struct VariableNodeCmp
{
279 static inline int cmp(Node n
, Node m
) {
280 if ( n
== m
) { return 0; }
282 // this is now slightly off of the old variable order.
284 bool nIsInteger
= n
.getType().isInteger();
285 bool mIsInteger
= m
.getType().isInteger();
287 if(nIsInteger
== mIsInteger
){
288 bool nIsVariable
= n
.isVar();
289 bool mIsVariable
= m
.isVar();
291 if(nIsVariable
== mIsVariable
){
300 return -1; // nIsVariable => !mIsVariable
302 return 1; // !nIsVariable => mIsVariable
306 Assert(nIsInteger
!= mIsInteger
);
308 return 1; // nIsInteger => !mIsInteger
310 return -1; // !nIsInteger => mIsInteger
315 bool operator()(Node n
, Node m
) const {
316 return VariableNodeCmp::cmp(n
,m
) < 0;
320 bool operator==(const Variable
& v
) const { return getNode() == v
.getNode();}
322 size_t getComplexity() const;
323 };/* class Variable */
326 class Constant
: public NodeWrapper
{
328 Constant(Node n
) : NodeWrapper(n
) {
329 Assert(isMember(getNode()));
332 static bool isMember(Node n
) {
333 return n
.getKind() == kind::CONST_RATIONAL
;
336 bool isNormalForm() { return isMember(getNode()); }
338 static Constant
mkConstant(Node n
) {
339 Assert(n
.getKind() == kind::CONST_RATIONAL
);
343 static Constant
mkConstant(const Rational
& rat
);
345 static Constant
mkZero() {
346 return mkConstant(Rational(0));
349 static Constant
mkOne() {
350 return mkConstant(Rational(1));
353 const Rational
& getValue() const {
354 return getNode().getConst
<Rational
>();
357 static int absCmp(const Constant
& a
, const Constant
& b
);
358 bool isIntegral() const { return getValue().isIntegral(); }
360 int sgn() const { return getValue().sgn(); }
362 bool isZero() const { return sgn() == 0; }
363 bool isNegative() const { return sgn() < 0; }
364 bool isPositive() const { return sgn() > 0; }
366 bool isOne() const { return getValue() == 1; }
368 Constant
operator*(const Rational
& other
) const {
369 return mkConstant(getValue() * other
);
372 Constant
operator*(const Constant
& other
) const {
373 return mkConstant(getValue() * other
.getValue());
375 Constant
operator+(const Constant
& other
) const {
376 return mkConstant(getValue() + other
.getValue());
378 Constant
operator-() const {
379 return mkConstant(-getValue());
382 Constant
inverse() const{
384 return mkConstant(getValue().inverse());
387 bool operator<(const Constant
& other
) const {
388 return getValue() < other
.getValue();
391 bool operator==(const Constant
& other
) const {
392 //Rely on node uniqueness.
393 return getNode() == other
.getNode();
396 Constant
abs() const {
404 uint32_t length() const{
405 Assert(isIntegral());
406 return getValue().getNumerator().length();
409 size_t getComplexity() const;
411 };/* class Constant */
414 template <class GetNodeIterator
>
415 inline Node
makeNode(Kind k
, GetNodeIterator start
, GetNodeIterator end
) {
418 while(start
!= end
) {
419 nb
<< (*start
).getNode();
424 }/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */
427 template <class GetNodeIterator
, class T
>
428 static void copy_range(GetNodeIterator begin
, GetNodeIterator end
, std::vector
<T
>& result
){
430 result
.push_back(*begin
);
435 template <class GetNodeIterator
, class T
>
436 static void merge_ranges(GetNodeIterator first1
,
437 GetNodeIterator last1
,
438 GetNodeIterator first2
,
439 GetNodeIterator last2
,
440 std::vector
<T
>& result
) {
442 while(first1
!= last1
&& first2
!= last2
){
443 if( (*first1
) < (*first2
) ){
444 result
.push_back(*first1
);
447 result
.push_back(*first2
);
451 copy_range(first1
, last1
, result
);
452 copy_range(first2
, last2
, result
);
455 template <class GetNodeIterator
, class T
, class Cmp
>
456 static void merge_ranges(GetNodeIterator first1
,
457 GetNodeIterator last1
,
458 GetNodeIterator first2
,
459 GetNodeIterator last2
,
460 std::vector
<T
>& result
,
463 while(first1
!= last1
&& first2
!= last2
){
464 if( cmp(*first1
, *first2
) ){
465 result
.push_back(*first1
);
468 result
.push_back(*first2
);
472 copy_range(first1
, last1
, result
);
473 copy_range(first2
, last2
, result
);
477 * A VarList is a sorted list of variables representing a product.
478 * If the VarList is empty, it represents an empty product or 1.
479 * If the VarList has size 1, it represents a single variable.
481 * A non-sorted VarList can never be successfully made in debug mode.
483 class VarList
: public NodeWrapper
{
486 static Node
multList(const std::vector
<Variable
>& list
) {
487 Assert(list
.size() >= 2);
489 return makeNode(kind::MULT
, list
.begin(), list
.end());
492 VarList() : NodeWrapper(Node::null()) {}
496 typedef expr::NodeSelfIterator internal_iterator
;
498 internal_iterator
internalBegin() const {
500 return expr::NodeSelfIterator::self(getNode());
502 return getNode().begin();
506 internal_iterator
internalEnd() const {
508 return expr::NodeSelfIterator::selfEnd(getNode());
510 return getNode().end();
516 class iterator
: public std::iterator
<std::input_iterator_tag
, Variable
> {
518 internal_iterator d_iter
;
521 explicit iterator(internal_iterator i
) : d_iter(i
) {}
523 inline Variable
operator*() {
524 return Variable(*d_iter
);
527 bool operator==(const iterator
& i
) {
528 return d_iter
== i
.d_iter
;
531 bool operator!=(const iterator
& i
) {
532 return d_iter
!= i
.d_iter
;
535 iterator
operator++() {
540 iterator
operator++(int) {
541 return iterator(d_iter
++);
545 iterator
begin() const {
546 return iterator(internalBegin());
549 iterator
end() const {
550 return iterator(internalEnd());
553 Variable
getHead() const {
558 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
559 Assert(isSorted(begin(), end()));
562 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
563 Assert(l
.size() >= 2);
564 Assert(isSorted(begin(), end()));
567 static bool isMember(Node n
);
569 bool isNormalForm() const {
573 static VarList
mkEmptyVarList() {
578 /** There are no restrictions on the size of l */
579 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
581 return mkEmptyVarList();
582 } else if(l
.size() == 1) {
583 return VarList((*l
.begin()).getNode());
589 bool empty() const { return getNode().isNull(); }
590 bool singleton() const {
591 return !empty() && getNode().getKind() != kind::MULT
;
598 return getNode().getNumChildren();
601 static VarList
parseVarList(Node n
);
603 VarList
operator*(const VarList
& vl
) const;
605 int cmp(const VarList
& vl
) const;
607 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
609 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
611 bool isIntegral() const {
612 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
614 if(!var
.isIntegral()){
620 size_t getComplexity() const;
623 bool isSorted(iterator start
, iterator end
);
625 };/* class VarList */
628 class Monomial
: public NodeWrapper
{
632 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
633 NodeWrapper(n
), constant(c
), varList(vl
)
635 Assert(!c
.isZero() || vl
.empty() );
636 Assert( c
.isZero() || !vl
.empty() );
638 Assert(!c
.isOne() || !multStructured(n
));
641 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
645 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
648 static bool multStructured(Node n
) {
649 return n
.getKind() == kind::MULT
&&
650 n
[0].getKind() == kind::CONST_RATIONAL
&&
651 n
.getNumChildren() == 2;
656 Monomial(const Constant
& c
):
657 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
660 Monomial(const VarList
& vl
):
661 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
663 Assert( !varList
.empty() );
666 Monomial(const Constant
& c
, const VarList
& vl
):
667 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
669 Assert( !c
.isZero() );
670 Assert( !c
.isOne() );
671 Assert( !varList
.empty() );
673 Assert(multStructured(getNode()));
676 static bool isMember(TNode n
);
678 /** Makes a monomial with no restrictions on c and vl. */
679 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
681 static Monomial
mkMonomial(const Variable
& v
){
682 return Monomial(VarList(v
));
685 static Monomial
parseMonomial(Node n
);
687 static Monomial
mkZero() {
688 return Monomial(Constant::mkConstant(0));
690 static Monomial
mkOne() {
691 return Monomial(Constant::mkConstant(1));
693 const Constant
& getConstant() const { return constant
; }
694 const VarList
& getVarList() const { return varList
; }
696 bool isConstant() const {
697 return varList
.empty();
700 bool isZero() const {
701 return constant
.isZero();
704 bool coefficientIsOne() const {
705 return constant
.isOne();
708 bool absCoefficientIsOne() const {
709 return coefficientIsOne() || constant
.getValue() == -1;
712 bool constantIsPositive() const {
713 return getConstant().isPositive();
716 Monomial
operator*(const Rational
& q
) const;
717 Monomial
operator*(const Constant
& c
) const;
718 Monomial
operator*(const Monomial
& mono
) const;
720 Monomial
operator-() const{
721 return (*this) * Rational(-1);
725 int cmp(const Monomial
& mono
) const {
726 return getVarList().cmp(mono
.getVarList());
729 bool operator<(const Monomial
& vl
) const {
733 bool operator==(const Monomial
& vl
) const {
737 static bool isSorted(const std::vector
<Monomial
>& m
) {
738 #if IS_SORTED_IN_GNUCXX_NAMESPACE
739 return __gnu_cxx::is_sorted(m
.begin(), m
.end());
740 #else /* IS_SORTED_IN_GNUCXX_NAMESPACE */
741 return std::is_sorted(m
.begin(), m
.end());
742 #endif /* IS_SORTED_IN_GNUCXX_NAMESPACE */
745 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
746 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
749 static void sort(std::vector
<Monomial
>& m
);
750 static void combineAdjacentMonomials(std::vector
<Monomial
>& m
);
753 * The variable product
755 bool integralVariables() const {
756 return getVarList().isIntegral();
760 * The coefficient of the monomial is integral.
762 bool integralCoefficient() const {
763 return getConstant().isIntegral();
767 * A Monomial is an "integral" monomial if the constant is integral.
769 bool isIntegral() const {
770 return integralCoefficient() && integralVariables();
773 /** Returns true if the VarList is a product of at least 2 Variables.*/
774 bool isNonlinear() const {
775 return getVarList().size() >= 2;
779 * Given a sorted list of monomials, this function transforms this
780 * into a strictly sorted list of monomials that does not contain zero.
782 //static std::vector<Monomial> sumLikeTerms(const std::vector<Monomial>& monos);
784 int absCmp(const Monomial
& other
) const{
785 return getConstant().getValue().absCmp(other
.getConstant().getValue());
787 // bool absLessThan(const Monomial& other) const{
788 // return getConstant().abs() < other.getConstant().abs();
791 uint32_t coefficientLength() const{
792 return getConstant().length();
796 static void printList(const std::vector
<Monomial
>& list
);
798 size_t getComplexity() const;
799 };/* class Monomial */
804 class Polynomial
: public NodeWrapper
{
808 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
809 Assert(isMember(getNode()));
812 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
813 Assert(m
.size() >= 2);
815 return makeNode(kind::PLUS
, m
.begin(), m
.end());
818 typedef expr::NodeSelfIterator internal_iterator
;
820 internal_iterator
internalBegin() const {
822 return expr::NodeSelfIterator::self(getNode());
824 return getNode().begin();
828 internal_iterator
internalEnd() const {
830 return expr::NodeSelfIterator::selfEnd(getNode());
832 return getNode().end();
836 bool singleton() const { return d_singleton
; }
839 static bool isMember(TNode n
);
843 internal_iterator d_iter
;
846 explicit iterator(internal_iterator i
) : d_iter(i
) {}
848 inline Monomial
operator*() {
849 return Monomial::parseMonomial(*d_iter
);
852 bool operator==(const iterator
& i
) {
853 return d_iter
== i
.d_iter
;
856 bool operator!=(const iterator
& i
) {
857 return d_iter
!= i
.d_iter
;
860 iterator
operator++() {
865 iterator
operator++(int) {
866 return iterator(d_iter
++);
870 iterator
begin() const { return iterator(internalBegin()); }
871 iterator
end() const { return iterator(internalEnd()); }
873 Polynomial(const Monomial
& m
):
874 NodeWrapper(m
.getNode()), d_singleton(true)
877 Polynomial(const std::vector
<Monomial
>& m
):
878 NodeWrapper(makePlusNode(m
)), d_singleton(false)
880 Assert( m
.size() >= 2);
881 Assert( Monomial::isStrictlySorted(m
) );
884 static Polynomial
mkPolynomial(const Variable
& v
){
885 return Monomial::mkMonomial(v
);
888 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
890 return Polynomial(Monomial::mkZero());
891 } else if(m
.size() == 1) {
892 return Polynomial((*m
.begin()));
894 return Polynomial(m
);
898 static Polynomial
parsePolynomial(Node n
) {
899 return Polynomial(n
);
902 static Polynomial
mkZero() {
903 return Polynomial(Monomial::mkZero());
905 static Polynomial
mkOne() {
906 return Polynomial(Monomial::mkOne());
908 bool isZero() const {
909 return singleton() && (getHead().isZero());
912 bool isConstant() const {
913 return singleton() && (getHead().isConstant());
916 bool containsConstant() const {
917 return getHead().isConstant();
920 uint32_t size() const{
924 Assert(getNode().getKind() == kind::PLUS
);
925 return getNode().getNumChildren();
929 Monomial
getHead() const {
933 Polynomial
getTail() const {
934 Assert(! singleton());
936 iterator tailStart
= begin();
938 std::vector
<Monomial
> subrange
;
939 copy_range(tailStart
, end(), subrange
);
940 return mkPolynomial(subrange
);
943 Monomial
minimumVariableMonomial() const;
944 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
946 void printList() const {
947 if(Debug
.isOn("normal-form")){
948 Debug("normal-form") << "start list" << std::endl
;
949 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
950 const Monomial
& m
=*i
;
953 Debug("normal-form") << "end list" << std::endl
;
957 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
958 bool allIntegralVariables() const {
959 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
960 if(!(*i
).integralVariables()){
968 * A Polynomial is an "integral" polynomial if all of the monomials are integral
969 * and all of the coefficients are Integral. */
970 bool isIntegral() const {
971 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
972 if(!(*i
).isIntegral()){
979 static Polynomial
sumPolynomials(const std::vector
<Polynomial
>& polynomials
);
981 /** Returns true if the polynomial contains a non-linear monomial.*/
982 bool isNonlinear() const;
986 * Selects a minimal monomial in the polynomial by the absolute value of
989 Monomial
selectAbsMinimum() const;
991 /** Returns true if the absolute value of the head coefficient is one. */
992 bool leadingCoefficientIsAbsOne() const;
993 bool leadingCoefficientIsPositive() const;
994 bool denominatorLCMIsOne() const;
995 bool numeratorGCDIsOne() const;
997 bool signNormalizedReducedSum() const {
998 return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne();
1002 * Returns the Least Common Multiple of the denominators of the coefficients
1005 Integer
denominatorLCM() const;
1008 * Returns the GCD of the numerators of the monomials.
1009 * Requires this to be an isIntegral() polynomial.
1011 Integer
numeratorGCD() const;
1014 * Returns the GCD of the coefficients of the monomials.
1015 * Requires this to be an isIntegral() polynomial.
1017 Integer
gcd() const;
1019 Polynomial
exactDivide(const Integer
& z
) const {
1020 Assert(isIntegral());
1021 Constant invz
= Constant::mkConstant(Rational(1,z
));
1022 Polynomial prod
= (*this) * Monomial(invz
);
1023 Assert(prod
.isIntegral());
1027 Polynomial
operator+(const Polynomial
& vl
) const;
1028 Polynomial
operator-(const Polynomial
& vl
) const;
1029 Polynomial
operator-() const{
1030 return (*this) * Rational(-1);
1033 Polynomial
operator*(const Rational
& q
) const;
1034 Polynomial
operator*(const Constant
& c
) const;
1035 Polynomial
operator*(const Monomial
& mono
) const;
1037 Polynomial
operator*(const Polynomial
& poly
) const;
1040 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
1041 * The quotient and remainder of p divided by the non-zero integer z is:
1042 * q := [(* floor(coeff_i/z) mono_i )]
1043 * r := [(* rem(coeff_i/z) mono_i)]
1044 * computeQR(p,z) returns the node (+ q r).
1046 * q and r are members of the Polynomial class.
1048 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
1049 * (+ (+ 2 x (* 4 y)) (+ 1 x))
1051 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
1053 /** Returns the coefficient associated with the VarList in the polynomial. */
1054 Constant
getCoefficient(const VarList
& vl
) const;
1056 uint32_t maxLength() const{
1057 iterator i
= begin(), e
=end();
1061 uint32_t max
= (*i
).coefficientLength();
1064 uint32_t curr
= (*i
).coefficientLength();
1073 uint32_t numMonomials() const {
1074 if( getNode().getKind() == kind::PLUS
){
1075 return getNode().getNumChildren();
1083 const Rational
& asConstant() const{
1084 Assert(isConstant());
1085 return getNode().getConst
<Rational
>();
1086 //return getHead().getConstant().getValue();
1089 bool isVarList() const {
1091 return VarList::isMember(getNode());
1097 VarList
asVarList() const {
1098 Assert(isVarList());
1099 return getHead().getVarList();
1102 size_t getComplexity() const;
1104 friend class SumPair
;
1105 friend class Comparison
;
1107 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1108 Node
makeAbsCondition(Variable v
){
1109 return makeAbsCondition(v
, *this);
1112 /** Returns a node that if asserted ensures v is the abs of p.*/
1113 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1115 };/* class Polynomial */
1119 * SumPair is a utility class that extends polynomials for use in computations.
1120 * A SumPair is always a combination of (+ p c) where
1121 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1123 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1124 * is known to implicitly be equal to 0.
1126 * SumPairs do not have unique representations due to the potential for p = 0.
1127 * This makes them inappropriate for normal forms.
1129 class SumPair
: public NodeWrapper
{
1131 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1132 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1138 Assert(isNormalForm());
1143 SumPair(const Polynomial
& p
):
1144 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1146 Assert(isNormalForm());
1149 SumPair(const Polynomial
& p
, const Constant
& c
):
1150 NodeWrapper(toNode(p
, c
))
1152 Assert(isNormalForm());
1155 static bool isMember(TNode n
) {
1156 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1157 if(Constant::isMember(n
[1])){
1158 if(Polynomial::isMember(n
[0])){
1159 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1160 return p
.isZero() || (!p
.containsConstant());
1172 bool isNormalForm() const {
1173 return isMember(getNode());
1176 Polynomial
getPolynomial() const {
1177 return Polynomial::parsePolynomial(getNode()[0]);
1180 Constant
getConstant() const {
1181 return Constant::mkConstant((getNode())[1]);
1184 SumPair
operator+(const SumPair
& other
) const {
1185 return SumPair(getPolynomial() + other
.getPolynomial(),
1186 getConstant() + other
.getConstant());
1189 SumPair
operator*(const Constant
& c
) const {
1190 return SumPair(getPolynomial() * c
, getConstant() * c
);
1193 SumPair
operator-(const SumPair
& other
) const {
1194 return (*this) + (other
* Constant::mkConstant(-1));
1197 static SumPair
mkSumPair(const Polynomial
& p
);
1199 static SumPair
mkSumPair(const Variable
& var
){
1200 return SumPair(Polynomial::mkPolynomial(var
));
1203 static SumPair
parseSumPair(TNode n
){
1207 bool isIntegral() const{
1208 return getConstant().isIntegral() && getPolynomial().isIntegral();
1211 bool isConstant() const {
1212 return getPolynomial().isZero();
1215 bool isZero() const {
1216 return getConstant().isZero() && isConstant();
1219 uint32_t size() const{
1220 return getPolynomial().size();
1223 bool isNonlinear() const{
1224 return getPolynomial().isNonlinear();
1228 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1229 * The SumPair must be integral.
1231 Integer
gcd() const {
1232 Assert(isIntegral());
1233 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1236 uint32_t maxLength() const {
1237 Assert(isIntegral());
1238 return std::max(getPolynomial().maxLength(), getConstant().length());
1241 static SumPair
mkZero() {
1242 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1245 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1247 };/* class SumPair */
1249 /* class OrderedPolynomialPair { */
1251 /* Polynomial d_first; */
1252 /* Polynomial d_second; */
1254 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1259 /* /\** Returns the first part of the pair. *\/ */
1260 /* const Polynomial& getFirst() const { */
1261 /* return d_first; */
1264 /* /\** Returns the second part of the pair. *\/ */
1265 /* const Polynomial& getSecond() const { */
1266 /* return d_second; */
1269 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1270 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1272 /* /\** Returns true if both of the polynomials are constant. *\/ */
1273 /* bool isConstant() const; */
1276 /* * Evaluates an isConstant() ordered pair as if */
1277 /* * (k getFirst() getRight()) */
1279 /* bool evaluateConstant(Kind k) const; */
1282 /* * Returns the Least Common Multiple of the monomials */
1283 /* * on the lefthand side and the constant on the right. */
1285 /* Integer denominatorLCM() const; */
1287 /* /\** Constructs a SumPair. *\/ */
1288 /* SumPair toSumPair() const; */
1291 /* OrderedPolynomialPair divideByGCD() const; */
1292 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1295 /* * Returns true if all of the variables are integers, */
1296 /* * and the coefficients are integers. */
1298 /* bool isIntegral() const; */
1300 /* /\** Returns true if all of the variables are integers. *\/ */
1301 /* bool allIntegralVariables() const { */
1302 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1306 class Comparison
: public NodeWrapper
{
1309 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1310 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1312 Comparison(TNode n
);
1315 * Creates a node in normal form equivalent to (= l 0).
1316 * All variables in l are integral.
1318 static Node
mkIntEquality(const Polynomial
& l
);
1321 * Creates a comparison equivalent to (k l 0).
1322 * k is either GT or GEQ.
1323 * All variables in l are integral.
1325 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1328 * Creates a node equivalent to (= l 0).
1329 * It is not the case that all variables in l are integral.
1331 static Node
mkRatEquality(const Polynomial
& l
);
1334 * Creates a comparison equivalent to (k l 0).
1335 * k is either GT or GEQ.
1336 * It is not the case that all variables in l are integral.
1338 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1342 Comparison(bool val
) :
1343 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1347 * Given a literal to TheoryArith return a single kind to
1348 * to indicate its underlying structure.
1349 * The function returns the following in each case:
1350 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1351 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1352 * - (NOT (EQUAL left right)) -> DISTINCT
1353 * - (NOT (GT left right)) -> LEQ
1354 * - (NOT (GEQ left right)) -> LT
1355 * If none of these match, it returns UNDEFINED_KIND.
1357 static Kind
comparisonKind(TNode literal
);
1359 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1361 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1363 /** Returns true if the comparison is a boolean constant. */
1364 bool isBoolean() const;
1367 * Returns true if the comparison is either a boolean term,
1368 * in integer normal form or mixed normal form.
1370 bool isNormalForm() const;
1373 bool isNormalGT() const;
1374 bool isNormalGEQ() const;
1376 bool isNormalLT() const;
1377 bool isNormalLEQ() const;
1379 bool isNormalEquality() const;
1380 bool isNormalDistinct() const;
1381 bool isNormalEqualityOrDisequality() const;
1383 bool allIntegralVariables() const {
1384 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1386 bool rightIsConstant() const;
1389 Polynomial
getLeft() const;
1390 Polynomial
getRight() const;
1392 /* /\** Normal form check if at least one variable is real. *\/ */
1393 /* bool isMixedCompareNormalForm() const; */
1395 /* /\** Normal form check if at least one variable is real. *\/ */
1396 /* bool isMixedEqualsNormalForm() const; */
1398 /* /\** Normal form check is all variables are integer.*\/ */
1399 /* bool isIntegerCompareNormalForm() const; */
1401 /* /\** Normal form check is all variables are integer.*\/ */
1402 /* bool isIntegerEqualsNormalForm() const; */
1406 * Returns true if all of the variables are integers, the coefficients are integers,
1407 * and the right hand coefficient is an integer.
1409 bool debugIsIntegral() const;
1411 static Comparison
parseNormalForm(TNode n
);
1413 inline static bool isNormalAtom(TNode n
){
1414 Comparison parse
= Comparison::parseNormalForm(n
);
1415 return parse
.isNormalForm();
1418 size_t getComplexity() const;
1420 SumPair
toSumPair() const;
1422 Polynomial
normalizedVariablePart() const;
1423 DeltaRational
normalizedDeltaRational() const;
1425 };/* class Comparison */
1427 }/* CVC4::theory::arith namespace */
1428 }/* CVC4::theory namespace */
1429 }/* CVC4 namespace */
1431 #endif /* __CVC4__THEORY__ARITH__NORMAL_FORM_H */