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/theory.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 (!isRelationOperator(k
)) &&
251 (Theory::isLeafOf(n
, theory::THEORY_ARITH
));
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 };/* class Variable */
292 class Constant
: public NodeWrapper
{
294 Constant(Node n
) : NodeWrapper(n
) {
295 Assert(isMember(getNode()));
298 static bool isMember(Node n
) {
299 return n
.getKind() == kind::CONST_RATIONAL
;
302 bool isNormalForm() { return isMember(getNode()); }
304 static Constant
mkConstant(Node n
) {
305 Assert(n
.getKind() == kind::CONST_RATIONAL
);
309 static Constant
mkConstant(const Rational
& rat
) {
310 return Constant(mkRationalNode(rat
));
313 static Constant
mkZero() {
314 return mkConstant(Rational(0));
317 static Constant
mkOne() {
318 return mkConstant(Rational(1));
321 const Rational
& getValue() const {
322 return getNode().getConst
<Rational
>();
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 };/* class Constant */
379 template <class GetNodeIterator
>
380 inline Node
makeNode(Kind k
, GetNodeIterator start
, GetNodeIterator end
) {
383 while(start
!= end
) {
384 nb
<< (*start
).getNode();
389 }/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */
392 template <class GetNodeIterator
, class T
>
393 static void copy_range(GetNodeIterator begin
, GetNodeIterator end
, std::vector
<T
>& result
){
395 result
.push_back(*begin
);
400 template <class GetNodeIterator
, class T
>
401 static void merge_ranges(GetNodeIterator first1
,
402 GetNodeIterator last1
,
403 GetNodeIterator first2
,
404 GetNodeIterator last2
,
405 std::vector
<T
>& result
) {
407 while(first1
!= last1
&& first2
!= last2
){
408 if( (*first1
) < (*first2
) ){
409 result
.push_back(*first1
);
412 result
.push_back(*first2
);
416 copy_range(first1
, last1
, result
);
417 copy_range(first2
, last2
, result
);
421 * A VarList is a sorted list of variables representing a product.
422 * If the VarList is empty, it represents an empty product or 1.
423 * If the VarList has size 1, it represents a single variable.
425 * A non-sorted VarList can never be successfully made in debug mode.
427 class VarList
: public NodeWrapper
{
430 static Node
multList(const std::vector
<Variable
>& list
) {
431 Assert(list
.size() >= 2);
433 return makeNode(kind::MULT
, list
.begin(), list
.end());
436 VarList() : NodeWrapper(Node::null()) {}
438 VarList(Node n
) : NodeWrapper(n
) {
439 Assert(isSorted(begin(), end()));
442 typedef expr::NodeSelfIterator internal_iterator
;
444 internal_iterator
internalBegin() const {
446 return expr::NodeSelfIterator::self(getNode());
448 return getNode().begin();
452 internal_iterator
internalEnd() const {
454 return expr::NodeSelfIterator::selfEnd(getNode());
456 return getNode().end();
464 internal_iterator d_iter
;
467 explicit iterator(internal_iterator i
) : d_iter(i
) {}
469 inline Variable
operator*() {
470 return Variable(*d_iter
);
473 bool operator==(const iterator
& i
) {
474 return d_iter
== i
.d_iter
;
477 bool operator!=(const iterator
& i
) {
478 return d_iter
!= i
.d_iter
;
481 iterator
operator++() {
486 iterator
operator++(int) {
487 return iterator(d_iter
++);
491 iterator
begin() const {
492 return iterator(internalBegin());
495 iterator
end() const {
496 return iterator(internalEnd());
499 Variable
getHead() const {
504 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
505 Assert(isSorted(begin(), end()));
508 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
509 Assert(l
.size() >= 2);
510 Assert(isSorted(begin(), end()));
513 static bool isMember(Node n
);
515 bool isNormalForm() const {
519 static VarList
mkEmptyVarList() {
524 /** There are no restrictions on the size of l */
525 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
527 return mkEmptyVarList();
528 } else if(l
.size() == 1) {
529 return VarList((*l
.begin()).getNode());
535 bool empty() const { return getNode().isNull(); }
536 bool singleton() const {
537 return !empty() && getNode().getKind() != kind::MULT
;
544 return getNode().getNumChildren();
547 static VarList
parseVarList(Node n
);
549 VarList
operator*(const VarList
& vl
) const;
551 int cmp(const VarList
& vl
) const;
553 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
555 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
557 bool isIntegral() const {
558 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
560 if(!var
.isIntegral()){
568 bool isSorted(iterator start
, iterator end
);
570 };/* class VarList */
573 class Monomial
: public NodeWrapper
{
577 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
578 NodeWrapper(n
), constant(c
), varList(vl
)
580 Assert(!c
.isZero() || vl
.empty() );
581 Assert( c
.isZero() || !vl
.empty() );
583 Assert(!c
.isOne() || !multStructured(n
));
586 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
590 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
593 static bool multStructured(Node n
) {
594 return n
.getKind() == kind::MULT
&&
595 n
[0].getKind() == kind::CONST_RATIONAL
&&
596 n
.getNumChildren() == 2;
601 Monomial(const Constant
& c
):
602 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
605 Monomial(const VarList
& vl
):
606 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
608 Assert( !varList
.empty() );
611 Monomial(const Constant
& c
, const VarList
& vl
):
612 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
614 Assert( !c
.isZero() );
615 Assert( !c
.isOne() );
616 Assert( !varList
.empty() );
618 Assert(multStructured(getNode()));
621 static bool isMember(TNode n
);
623 /** Makes a monomial with no restrictions on c and vl. */
624 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
626 static Monomial
mkMonomial(const Variable
& v
){
627 return Monomial(VarList(v
));
630 static Monomial
parseMonomial(Node n
);
632 static Monomial
mkZero() {
633 return Monomial(Constant::mkConstant(0));
635 static Monomial
mkOne() {
636 return Monomial(Constant::mkConstant(1));
638 const Constant
& getConstant() const { return constant
; }
639 const VarList
& getVarList() const { return varList
; }
641 bool isConstant() const {
642 return varList
.empty();
645 bool isZero() const {
646 return constant
.isZero();
649 bool coefficientIsOne() const {
650 return constant
.isOne();
653 bool absCoefficientIsOne() const {
654 return coefficientIsOne() || constant
.getValue() == -1;
657 bool constantIsPositive() const {
658 return getConstant().isPositive();
661 Monomial
operator*(const Rational
& q
) const;
662 Monomial
operator*(const Constant
& c
) const;
663 Monomial
operator*(const Monomial
& mono
) const;
665 Monomial
operator-() const{
666 return (*this) * Rational(-1);
670 int cmp(const Monomial
& mono
) const {
671 return getVarList().cmp(mono
.getVarList());
674 bool operator<(const Monomial
& vl
) const {
678 bool operator==(const Monomial
& vl
) const {
682 static bool isSorted(const std::vector
<Monomial
>& m
) {
683 return __gnu_cxx::is_sorted(m
.begin(), m
.end());
686 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
687 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
691 * The variable product
693 bool integralVariables() const {
694 return getVarList().isIntegral();
698 * The coefficient of the monomial is integral.
700 bool integralCoefficient() const {
701 return getConstant().isIntegral();
705 * A Monomial is an "integral" monomial if the constant is integral.
707 bool isIntegral() const {
708 return integralCoefficient() && integralVariables();
712 * Given a sorted list of monomials, this function transforms this
713 * into a strictly sorted list of monomials that does not contain zero.
715 static std::vector
<Monomial
> sumLikeTerms(const std::vector
<Monomial
>& monos
);
717 bool absLessThan(const Monomial
& other
) const{
718 return getConstant().abs() < other
.getConstant().abs();
721 uint32_t coefficientLength() const{
722 return getConstant().length();
726 static void printList(const std::vector
<Monomial
>& list
);
728 };/* class Monomial */
733 class Polynomial
: public NodeWrapper
{
737 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
738 Assert(isMember(getNode()));
741 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
742 Assert(m
.size() >= 2);
744 return makeNode(kind::PLUS
, m
.begin(), m
.end());
747 typedef expr::NodeSelfIterator internal_iterator
;
749 internal_iterator
internalBegin() const {
751 return expr::NodeSelfIterator::self(getNode());
753 return getNode().begin();
757 internal_iterator
internalEnd() const {
759 return expr::NodeSelfIterator::selfEnd(getNode());
761 return getNode().end();
765 bool singleton() const { return d_singleton
; }
768 static bool isMember(TNode n
) {
769 if(Monomial::isMember(n
)){
771 }else if(n
.getKind() == kind::PLUS
){
772 Assert(n
.getNumChildren() >= 2);
773 Node::iterator currIter
= n
.begin(), end
= n
.end();
774 Node prev
= *currIter
;
775 if(!Monomial::isMember(prev
)){
779 Monomial mprev
= Monomial::parseMonomial(prev
);
781 for(; currIter
!= end
; ++currIter
){
782 Node curr
= *currIter
;
783 if(!Monomial::isMember(curr
)){
786 Monomial mcurr
= Monomial::parseMonomial(curr
);
787 if(!(mprev
< mcurr
)){
800 internal_iterator d_iter
;
803 explicit iterator(internal_iterator i
) : d_iter(i
) {}
805 inline Monomial
operator*() {
806 return Monomial::parseMonomial(*d_iter
);
809 bool operator==(const iterator
& i
) {
810 return d_iter
== i
.d_iter
;
813 bool operator!=(const iterator
& i
) {
814 return d_iter
!= i
.d_iter
;
817 iterator
operator++() {
822 iterator
operator++(int) {
823 return iterator(d_iter
++);
827 iterator
begin() const { return iterator(internalBegin()); }
828 iterator
end() const { return iterator(internalEnd()); }
830 Polynomial(const Monomial
& m
):
831 NodeWrapper(m
.getNode()), d_singleton(true)
834 Polynomial(const std::vector
<Monomial
>& m
):
835 NodeWrapper(makePlusNode(m
)), d_singleton(false)
837 Assert( m
.size() >= 2);
838 Assert( Monomial::isStrictlySorted(m
) );
841 static Polynomial
mkPolynomial(const Variable
& v
){
842 return Monomial::mkMonomial(v
);
845 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
847 return Polynomial(Monomial::mkZero());
848 } else if(m
.size() == 1) {
849 return Polynomial((*m
.begin()));
851 return Polynomial(m
);
855 static Polynomial
parsePolynomial(Node n
) {
856 return Polynomial(n
);
859 static Polynomial
mkZero() {
860 return Polynomial(Monomial::mkZero());
862 static Polynomial
mkOne() {
863 return Polynomial(Monomial::mkOne());
865 bool isZero() const {
866 return singleton() && (getHead().isZero());
869 bool isConstant() const {
870 return singleton() && (getHead().isConstant());
873 bool containsConstant() const {
874 return getHead().isConstant();
877 uint32_t size() const{
881 Assert(getNode().getKind() == kind::PLUS
);
882 return getNode().getNumChildren();
886 Monomial
getHead() const {
890 Polynomial
getTail() const {
891 Assert(! singleton());
893 iterator tailStart
= begin();
895 std::vector
<Monomial
> subrange
;
896 copy_range(tailStart
, end(), subrange
);
897 return mkPolynomial(subrange
);
900 Monomial
minimumVariableMonomial() const;
901 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
903 void printList() const {
904 if(Debug
.isOn("normal-form")){
905 Debug("normal-form") << "start list" << std::endl
;
906 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
907 const Monomial
& m
=*i
;
910 Debug("normal-form") << "end list" << std::endl
;
914 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
915 bool allIntegralVariables() const {
916 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
917 if(!(*i
).integralVariables()){
925 * A Polynomial is an "integral" polynomial if all of the monomials are integral
926 * and all of the coefficients are Integral. */
927 bool isIntegral() const {
928 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
929 if(!(*i
).isIntegral()){
937 * Selects a minimal monomial in the polynomial by the absolute value of
940 Monomial
selectAbsMinimum() const;
942 /** Returns true if the absolute value of the head coefficient is one. */
943 bool leadingCoefficientIsAbsOne() const;
944 bool leadingCoefficientIsPositive() const;
945 bool denominatorLCMIsOne() const;
946 bool numeratorGCDIsOne() const;
948 bool signNormalizedReducedSum() const {
949 return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne();
953 * Returns the Least Common Multiple of the denominators of the coefficients
956 Integer
denominatorLCM() const;
959 * Returns the GCD of the numerators of the monomials.
960 * Requires this to be an isIntegral() polynomial.
962 Integer
numeratorGCD() const;
965 * Returns the GCD of the coefficients of the monomials.
966 * Requires this to be an isIntegral() polynomial.
970 Polynomial
exactDivide(const Integer
& z
) const {
971 Assert(isIntegral());
972 Constant invz
= Constant::mkConstant(Rational(1,z
));
973 Polynomial prod
= (*this) * Monomial(invz
);
974 Assert(prod
.isIntegral());
978 Polynomial
operator+(const Polynomial
& vl
) const;
979 Polynomial
operator-(const Polynomial
& vl
) const;
980 Polynomial
operator-() const{
981 return (*this) * Rational(-1);
984 Polynomial
operator*(const Rational
& q
) const;
985 Polynomial
operator*(const Constant
& c
) const;
986 Polynomial
operator*(const Monomial
& mono
) const;
988 Polynomial
operator*(const Polynomial
& poly
) const;
991 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
992 * The quotient and remainder of p divided by the non-zero integer z is:
993 * q := [(* floor(coeff_i/z) mono_i )]
994 * r := [(* rem(coeff_i/z) mono_i)]
995 * computeQR(p,z) returns the node (+ q r).
997 * q and r are members of the Polynomial class.
999 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
1000 * (+ (+ 2 x (* 4 y)) (+ 1 x))
1002 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
1004 /** Returns the coefficient associated with the VarList in the polynomial. */
1005 Constant
getCoefficient(const VarList
& vl
) const;
1007 uint32_t maxLength() const{
1008 iterator i
= begin(), e
=end();
1012 uint32_t max
= (*i
).coefficientLength();
1015 uint32_t curr
= (*i
).coefficientLength();
1024 uint32_t numMonomials() const {
1025 if( getNode().getKind() == kind::PLUS
){
1026 return getNode().getNumChildren();
1034 const Rational
& asConstant() const{
1035 Assert(isConstant());
1036 return getNode().getConst
<Rational
>();
1037 //return getHead().getConstant().getValue();
1040 bool isVarList() const {
1042 return VarList::isMember(getNode());
1048 VarList
asVarList() const {
1049 Assert(isVarList());
1050 return getHead().getVarList();
1053 friend class SumPair
;
1054 friend class Comparison
;
1056 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1057 Node
makeAbsCondition(Variable v
){
1058 return makeAbsCondition(v
, *this);
1061 /** Returns a node that if asserted ensures v is the abs of p.*/
1062 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1064 };/* class Polynomial */
1068 * SumPair is a utility class that extends polynomials for use in computations.
1069 * A SumPair is always a combination of (+ p c) where
1070 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1072 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1073 * is known to implicitly be equal to 0.
1075 * SumPairs do not have unique representations due to the potential for p = 0.
1076 * This makes them inappropriate for normal forms.
1078 class SumPair
: public NodeWrapper
{
1080 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1081 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1087 Assert(isNormalForm());
1092 SumPair(const Polynomial
& p
):
1093 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1095 Assert(isNormalForm());
1098 SumPair(const Polynomial
& p
, const Constant
& c
):
1099 NodeWrapper(toNode(p
, c
))
1101 Assert(isNormalForm());
1104 static bool isMember(TNode n
) {
1105 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1106 if(Constant::isMember(n
[1])){
1107 if(Polynomial::isMember(n
[0])){
1108 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1109 return p
.isZero() || (!p
.containsConstant());
1121 bool isNormalForm() const {
1122 return isMember(getNode());
1125 Polynomial
getPolynomial() const {
1126 return Polynomial::parsePolynomial(getNode()[0]);
1129 Constant
getConstant() const {
1130 return Constant::mkConstant((getNode())[1]);
1133 SumPair
operator+(const SumPair
& other
) const {
1134 return SumPair(getPolynomial() + other
.getPolynomial(),
1135 getConstant() + other
.getConstant());
1138 SumPair
operator*(const Constant
& c
) const {
1139 return SumPair(getPolynomial() * c
, getConstant() * c
);
1142 SumPair
operator-(const SumPair
& other
) const {
1143 return (*this) + (other
* Constant::mkConstant(-1));
1146 static SumPair
mkSumPair(const Polynomial
& p
);
1148 static SumPair
mkSumPair(const Variable
& var
){
1149 return SumPair(Polynomial::mkPolynomial(var
));
1152 static SumPair
parseSumPair(TNode n
){
1156 bool isIntegral() const{
1157 return getConstant().isIntegral() && getPolynomial().isIntegral();
1160 bool isConstant() const {
1161 return getPolynomial().isZero();
1164 bool isZero() const {
1165 return getConstant().isZero() && isConstant();
1169 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1170 * The SumPair must be integral.
1172 Integer
gcd() const {
1173 Assert(isIntegral());
1174 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1177 uint32_t maxLength() const {
1178 Assert(isIntegral());
1179 return std::max(getPolynomial().maxLength(), getConstant().length());
1182 static SumPair
mkZero() {
1183 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1186 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1188 };/* class SumPair */
1190 /* class OrderedPolynomialPair { */
1192 /* Polynomial d_first; */
1193 /* Polynomial d_second; */
1195 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1200 /* /\** Returns the first part of the pair. *\/ */
1201 /* const Polynomial& getFirst() const { */
1202 /* return d_first; */
1205 /* /\** Returns the second part of the pair. *\/ */
1206 /* const Polynomial& getSecond() const { */
1207 /* return d_second; */
1210 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1211 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1213 /* /\** Returns true if both of the polynomials are constant. *\/ */
1214 /* bool isConstant() const; */
1217 /* * Evaluates an isConstant() ordered pair as if */
1218 /* * (k getFirst() getRight()) */
1220 /* bool evaluateConstant(Kind k) const; */
1223 /* * Returns the Least Common Multiple of the monomials */
1224 /* * on the lefthand side and the constant on the right. */
1226 /* Integer denominatorLCM() const; */
1228 /* /\** Constructs a SumPair. *\/ */
1229 /* SumPair toSumPair() const; */
1232 /* OrderedPolynomialPair divideByGCD() const; */
1233 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1236 /* * Returns true if all of the variables are integers, */
1237 /* * and the coefficients are integers. */
1239 /* bool isIntegral() const; */
1241 /* /\** Returns true if all of the variables are integers. *\/ */
1242 /* bool allIntegralVariables() const { */
1243 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1247 class Comparison
: public NodeWrapper
{
1250 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1251 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1253 Comparison(TNode n
);
1256 * Creates a node in normal form equivalent to (= l 0).
1257 * All variables in l are integral.
1259 static Node
mkIntEquality(const Polynomial
& l
);
1262 * Creates a comparison equivalent to (k l 0).
1263 * k is either GT or GEQ.
1264 * All variables in l are integral.
1266 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1269 * Creates a node equivalent to (= l 0).
1270 * It is not the case that all variables in l are integral.
1272 static Node
mkRatEquality(const Polynomial
& l
);
1275 * Creates a comparison equivalent to (k l 0).
1276 * k is either GT or GEQ.
1277 * It is not the case that all variables in l are integral.
1279 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1283 Comparison(bool val
) :
1284 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1288 * Given a literal to TheoryArith return a single kind to
1289 * to indicate its underlying structure.
1290 * The function returns the following in each case:
1291 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1292 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1293 * - (NOT (EQUAL left right)) -> DISTINCT
1294 * - (NOT (GT left right)) -> LEQ
1295 * - (NOT (GEQ left right)) -> LT
1296 * If none of these match, it returns UNDEFINED_KIND.
1298 static Kind
comparisonKind(TNode literal
);
1300 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1302 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1304 /** Returns true if the comparison is a boolean constant. */
1305 bool isBoolean() const;
1308 * Returns true if the comparison is either a boolean term,
1309 * in integer normal form or mixed normal form.
1311 bool isNormalForm() const;
1314 bool isNormalGT() const;
1315 bool isNormalGEQ() const;
1317 bool isNormalLT() const;
1318 bool isNormalLEQ() const;
1320 bool isNormalEquality() const;
1321 bool isNormalDistinct() const;
1322 bool isNormalEqualityOrDisequality() const;
1324 bool allIntegralVariables() const {
1325 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1327 bool rightIsConstant() const;
1330 Polynomial
getLeft() const;
1331 Polynomial
getRight() const;
1333 /* /\** Normal form check if at least one variable is real. *\/ */
1334 /* bool isMixedCompareNormalForm() const; */
1336 /* /\** Normal form check if at least one variable is real. *\/ */
1337 /* bool isMixedEqualsNormalForm() const; */
1339 /* /\** Normal form check is all variables are integer.*\/ */
1340 /* bool isIntegerCompareNormalForm() const; */
1342 /* /\** Normal form check is all variables are integer.*\/ */
1343 /* bool isIntegerEqualsNormalForm() const; */
1347 * Returns true if all of the variables are integers, the coefficients are integers,
1348 * and the right hand coefficient is an integer.
1350 bool debugIsIntegral() const;
1352 static Comparison
parseNormalForm(TNode n
);
1354 inline static bool isNormalAtom(TNode n
){
1355 Comparison parse
= Comparison::parseNormalForm(n
);
1356 return parse
.isNormalForm();
1359 SumPair
toSumPair() const;
1361 Polynomial
normalizedVariablePart() const;
1362 DeltaRational
normalizedDeltaRational() const;
1364 };/* class Comparison */
1366 }/* CVC4::theory::arith namespace */
1367 }/* CVC4::theory namespace */
1368 }/* CVC4 namespace */
1370 #endif /* __CVC4__THEORY__ARITH__NORMAL_FORM_H */