1 /********************* */
2 /*! \file normal_form.h
4 ** Top contributors (to current version):
5 ** Tim King, Morgan Deters, Mathias Preiner
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\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
26 #include "base/output.h"
27 #include "expr/node.h"
28 #include "expr/node_self_iterator.h"
29 #include "theory/arith/delta_rational.h"
30 #include "util/rational.h"
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
) { Assert(isMember(getNode())); }
229 // TODO: check if it's a theory leaf also
230 static bool isMember(Node n
)
232 Kind k
= n
.getKind();
235 case kind::CONST_RATIONAL
: return false;
236 case kind::INTS_DIVISION
:
237 case kind::INTS_MODULUS
:
239 case kind::INTS_DIVISION_TOTAL
:
240 case kind::INTS_MODULUS_TOTAL
:
241 case kind::DIVISION_TOTAL
: return isDivMember(n
);
242 case kind::EXPONENTIAL
:
248 case kind::COTANGENT
:
250 case kind::ARCCOSINE
:
251 case kind::ARCTANGENT
:
252 case kind::ARCCOSECANT
:
253 case kind::ARCSECANT
:
254 case kind::ARCCOTANGENT
:
256 case kind::PI
: return isTranscendentalMember(n
);
258 case kind::TO_INTEGER
:
259 // Treat to_int as a variable; it is replaced in early preprocessing
262 default: return isLeafMember(n
);
266 static bool isLeafMember(Node n
);
267 static bool isDivMember(Node n
);
268 bool isDivLike() const{
269 return isDivMember(getNode());
271 static bool isTranscendentalMember(Node n
);
273 bool isNormalForm() { return isMember(getNode()); }
275 bool isIntegral() const {
276 return getNode().getType().isInteger();
279 bool isMetaKindVariable() const {
280 return getNode().isVar();
283 bool operator<(const Variable
& v
) const {
285 return cmp(this->getNode(), v
.getNode());
288 struct VariableNodeCmp
{
289 static inline int cmp(const Node
& n
, const Node
& m
) {
290 if ( n
== m
) { return 0; }
292 // this is now slightly off of the old variable order.
294 bool nIsInteger
= n
.getType().isInteger();
295 bool mIsInteger
= m
.getType().isInteger();
297 if(nIsInteger
== mIsInteger
){
298 bool nIsVariable
= n
.isVar();
299 bool mIsVariable
= m
.isVar();
301 if(nIsVariable
== mIsVariable
){
310 return -1; // nIsVariable => !mIsVariable
312 return 1; // !nIsVariable => mIsVariable
316 Assert(nIsInteger
!= mIsInteger
);
318 return 1; // nIsInteger => !mIsInteger
320 return -1; // !nIsInteger => mIsInteger
325 bool operator()(const Node
& n
, const Node
& m
) const {
326 return VariableNodeCmp::cmp(n
,m
) < 0;
330 bool operator==(const Variable
& v
) const { return getNode() == v
.getNode();}
332 size_t getComplexity() const;
333 };/* class Variable */
336 class Constant
: public NodeWrapper
{
338 Constant(Node n
) : NodeWrapper(n
) { Assert(isMember(getNode())); }
340 static bool isMember(Node n
) { return n
.getKind() == kind::CONST_RATIONAL
; }
342 bool isNormalForm() { return isMember(getNode()); }
344 static Constant
mkConstant(Node n
)
346 Assert(n
.getKind() == kind::CONST_RATIONAL
);
350 static Constant
mkConstant(const Rational
& rat
);
352 static Constant
mkZero() {
353 return mkConstant(Rational(0));
356 static Constant
mkOne() {
357 return mkConstant(Rational(1));
360 const Rational
& getValue() const {
361 return getNode().getConst
<Rational
>();
364 static int absCmp(const Constant
& a
, const Constant
& b
);
365 bool isIntegral() const { return getValue().isIntegral(); }
367 int sgn() const { return getValue().sgn(); }
369 bool isZero() const { return sgn() == 0; }
370 bool isNegative() const { return sgn() < 0; }
371 bool isPositive() const { return sgn() > 0; }
373 bool isOne() const { return getValue() == 1; }
375 Constant
operator*(const Rational
& other
) const {
376 return mkConstant(getValue() * other
);
379 Constant
operator*(const Constant
& other
) const {
380 return mkConstant(getValue() * other
.getValue());
382 Constant
operator+(const Constant
& other
) const {
383 return mkConstant(getValue() + other
.getValue());
385 Constant
operator-() const {
386 return mkConstant(-getValue());
389 Constant
inverse() const{
391 return mkConstant(getValue().inverse());
394 bool operator<(const Constant
& other
) const {
395 return getValue() < other
.getValue();
398 bool operator==(const Constant
& other
) const {
399 //Rely on node uniqueness.
400 return getNode() == other
.getNode();
403 Constant
abs() const {
411 uint32_t length() const{
412 Assert(isIntegral());
413 return getValue().getNumerator().length();
416 size_t getComplexity() const;
418 };/* class Constant */
421 template <class GetNodeIterator
>
422 inline Node
makeNode(Kind k
, GetNodeIterator start
, GetNodeIterator end
) {
425 while(start
!= end
) {
426 nb
<< (*start
).getNode();
431 }/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */
434 * A VarList is a sorted list of variables representing a product.
435 * If the VarList is empty, it represents an empty product or 1.
436 * If the VarList has size 1, it represents a single variable.
438 * A non-sorted VarList can never be successfully made in debug mode.
440 class VarList
: public NodeWrapper
{
443 static Node
multList(const std::vector
<Variable
>& list
) {
444 Assert(list
.size() >= 2);
446 return makeNode(kind::NONLINEAR_MULT
, list
.begin(), list
.end());
449 VarList() : NodeWrapper(Node::null()) {}
453 typedef expr::NodeSelfIterator internal_iterator
;
455 internal_iterator
internalBegin() const {
457 return expr::NodeSelfIterator::self(getNode());
459 return getNode().begin();
463 internal_iterator
internalEnd() const {
465 return expr::NodeSelfIterator::selfEnd(getNode());
467 return getNode().end();
473 class iterator
: public std::iterator
<std::input_iterator_tag
, Variable
> {
475 internal_iterator d_iter
;
478 explicit iterator(internal_iterator i
) : d_iter(i
) {}
480 inline Variable
operator*() {
481 return Variable(*d_iter
);
484 bool operator==(const iterator
& i
) {
485 return d_iter
== i
.d_iter
;
488 bool operator!=(const iterator
& i
) {
489 return d_iter
!= i
.d_iter
;
492 iterator
operator++() {
497 iterator
operator++(int) {
498 return iterator(d_iter
++);
502 iterator
begin() const {
503 return iterator(internalBegin());
506 iterator
end() const {
507 return iterator(internalEnd());
510 Variable
getHead() const {
515 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
516 Assert(isSorted(begin(), end()));
519 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
520 Assert(l
.size() >= 2);
521 Assert(isSorted(begin(), end()));
524 static bool isMember(Node n
);
526 bool isNormalForm() const {
530 static VarList
mkEmptyVarList() {
535 /** There are no restrictions on the size of l */
536 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
538 return mkEmptyVarList();
539 } else if(l
.size() == 1) {
540 return VarList((*l
.begin()).getNode());
546 bool empty() const { return getNode().isNull(); }
547 bool singleton() const {
548 return !empty() && getNode().getKind() != kind::NONLINEAR_MULT
;
555 return getNode().getNumChildren();
558 static VarList
parseVarList(Node n
);
560 VarList
operator*(const VarList
& vl
) const;
562 int cmp(const VarList
& vl
) const;
564 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
566 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
568 bool isIntegral() const {
569 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
571 if(!var
.isIntegral()){
577 size_t getComplexity() const;
580 bool isSorted(iterator start
, iterator end
);
582 };/* class VarList */
585 /** Constructors have side conditions. Use the static mkMonomial functions instead. */
586 class Monomial
: public NodeWrapper
{
590 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
591 NodeWrapper(n
), constant(c
), varList(vl
)
593 Assert(!c
.isZero() || vl
.empty());
594 Assert(c
.isZero() || !vl
.empty());
596 Assert(!c
.isOne() || !multStructured(n
));
599 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
603 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
606 static bool multStructured(Node n
) {
607 return n
.getKind() == kind::MULT
&&
608 n
[0].getKind() == kind::CONST_RATIONAL
&&
609 n
.getNumChildren() == 2;
612 Monomial(const Constant
& c
):
613 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
616 Monomial(const VarList
& vl
):
617 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
619 Assert(!varList
.empty());
622 Monomial(const Constant
& c
, const VarList
& vl
):
623 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
627 Assert(!varList
.empty());
629 Assert(multStructured(getNode()));
632 static bool isMember(TNode n
);
634 /** Makes a monomial with no restrictions on c and vl. */
635 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
637 /** If vl is empty, this make one. */
638 static Monomial
mkMonomial(const VarList
& vl
);
640 static Monomial
mkMonomial(const Constant
& c
){
644 static Monomial
mkMonomial(const Variable
& v
){
645 return Monomial(VarList(v
));
648 static Monomial
parseMonomial(Node n
);
650 static Monomial
mkZero() {
651 return Monomial(Constant::mkConstant(0));
653 static Monomial
mkOne() {
654 return Monomial(Constant::mkConstant(1));
656 const Constant
& getConstant() const { return constant
; }
657 const VarList
& getVarList() const { return varList
; }
659 bool isConstant() const {
660 return varList
.empty();
663 bool isZero() const {
664 return constant
.isZero();
667 bool coefficientIsOne() const {
668 return constant
.isOne();
671 bool absCoefficientIsOne() const {
672 return coefficientIsOne() || constant
.getValue() == -1;
675 bool constantIsPositive() const {
676 return getConstant().isPositive();
679 Monomial
operator*(const Rational
& q
) const;
680 Monomial
operator*(const Constant
& c
) const;
681 Monomial
operator*(const Monomial
& mono
) const;
683 Monomial
operator-() const{
684 return (*this) * Rational(-1);
688 int cmp(const Monomial
& mono
) const {
689 return getVarList().cmp(mono
.getVarList());
692 bool operator<(const Monomial
& vl
) const {
696 bool operator==(const Monomial
& vl
) const {
700 static bool isSorted(const std::vector
<Monomial
>& m
) {
701 return std::is_sorted(m
.begin(), m
.end());
704 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
705 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
708 static void sort(std::vector
<Monomial
>& m
);
709 static void combineAdjacentMonomials(std::vector
<Monomial
>& m
);
712 * The variable product
714 bool integralVariables() const {
715 return getVarList().isIntegral();
719 * The coefficient of the monomial is integral.
721 bool integralCoefficient() const {
722 return getConstant().isIntegral();
726 * A Monomial is an "integral" monomial if the constant is integral.
728 bool isIntegral() const {
729 return integralCoefficient() && integralVariables();
732 /** Returns true if the VarList is a product of at least 2 Variables.*/
733 bool isNonlinear() const {
734 return getVarList().size() >= 2;
738 * Given a sorted list of monomials, this function transforms this
739 * into a strictly sorted list of monomials that does not contain zero.
741 //static std::vector<Monomial> sumLikeTerms(const std::vector<Monomial>& monos);
743 int absCmp(const Monomial
& other
) const{
744 return getConstant().getValue().absCmp(other
.getConstant().getValue());
746 // bool absLessThan(const Monomial& other) const{
747 // return getConstant().abs() < other.getConstant().abs();
750 uint32_t coefficientLength() const{
751 return getConstant().length();
755 static void printList(const std::vector
<Monomial
>& list
);
757 size_t getComplexity() const;
758 };/* class Monomial */
763 class Polynomial
: public NodeWrapper
{
767 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
768 Assert(isMember(getNode()));
771 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
772 Assert(m
.size() >= 2);
774 return makeNode(kind::PLUS
, m
.begin(), m
.end());
777 typedef expr::NodeSelfIterator internal_iterator
;
779 internal_iterator
internalBegin() const {
781 return expr::NodeSelfIterator::self(getNode());
783 return getNode().begin();
787 internal_iterator
internalEnd() const {
789 return expr::NodeSelfIterator::selfEnd(getNode());
791 return getNode().end();
795 bool singleton() const { return d_singleton
; }
798 static bool isMember(TNode n
);
800 class iterator
: public std::iterator
<std::input_iterator_tag
, Monomial
> {
802 internal_iterator d_iter
;
805 explicit iterator(internal_iterator i
) : d_iter(i
) {}
807 inline Monomial
operator*() {
808 return Monomial::parseMonomial(*d_iter
);
811 bool operator==(const iterator
& i
) {
812 return d_iter
== i
.d_iter
;
815 bool operator!=(const iterator
& i
) {
816 return d_iter
!= i
.d_iter
;
819 iterator
operator++() {
824 iterator
operator++(int) {
825 return iterator(d_iter
++);
829 iterator
begin() const { return iterator(internalBegin()); }
830 iterator
end() const { return iterator(internalEnd()); }
832 Polynomial(const Monomial
& m
):
833 NodeWrapper(m
.getNode()), d_singleton(true)
836 Polynomial(const std::vector
<Monomial
>& m
):
837 NodeWrapper(makePlusNode(m
)), d_singleton(false)
839 Assert(m
.size() >= 2);
840 Assert(Monomial::isStrictlySorted(m
));
843 static Polynomial
mkPolynomial(const Constant
& c
){
844 return Polynomial(Monomial::mkMonomial(c
));
847 static Polynomial
mkPolynomial(const Variable
& v
){
848 return Polynomial(Monomial::mkMonomial(v
));
851 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
853 return Polynomial(Monomial::mkZero());
854 } else if(m
.size() == 1) {
855 return Polynomial((*m
.begin()));
857 return Polynomial(m
);
861 static Polynomial
parsePolynomial(Node n
) {
862 return Polynomial(n
);
865 static Polynomial
mkZero() {
866 return Polynomial(Monomial::mkZero());
868 static Polynomial
mkOne() {
869 return Polynomial(Monomial::mkOne());
871 bool isZero() const {
872 return singleton() && (getHead().isZero());
875 bool isConstant() const {
876 return singleton() && (getHead().isConstant());
879 bool containsConstant() const {
880 return getHead().isConstant();
883 uint32_t size() const{
887 Assert(getNode().getKind() == kind::PLUS
);
888 return getNode().getNumChildren();
892 Monomial
getHead() const {
896 Polynomial
getTail() const {
897 Assert(!singleton());
899 iterator tailStart
= begin();
901 std::vector
<Monomial
> subrange
;
902 std::copy(tailStart
, end(), std::back_inserter(subrange
));
903 return mkPolynomial(subrange
);
906 Monomial
minimumVariableMonomial() const;
907 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
909 void printList() const {
910 if(Debug
.isOn("normal-form")){
911 Debug("normal-form") << "start list" << std::endl
;
912 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
913 const Monomial
& m
=*i
;
916 Debug("normal-form") << "end list" << std::endl
;
920 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
921 bool allIntegralVariables() const {
922 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
923 if(!(*i
).integralVariables()){
931 * A Polynomial is an "integral" polynomial if all of the monomials are integral
932 * and all of the coefficients are Integral. */
933 bool isIntegral() const {
934 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
935 if(!(*i
).isIntegral()){
942 static Polynomial
sumPolynomials(const std::vector
<Polynomial
>& polynomials
);
944 /** Returns true if the polynomial contains a non-linear monomial.*/
945 bool isNonlinear() const;
949 * Selects a minimal monomial in the polynomial by the absolute value of
952 Monomial
selectAbsMinimum() const;
954 /** Returns true if the absolute value of the head coefficient is one. */
955 bool leadingCoefficientIsAbsOne() const;
956 bool leadingCoefficientIsPositive() const;
957 bool denominatorLCMIsOne() const;
958 bool numeratorGCDIsOne() const;
960 bool signNormalizedReducedSum() const {
961 return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne();
965 * Returns the Least Common Multiple of the denominators of the coefficients
968 Integer
denominatorLCM() const;
971 * Returns the GCD of the numerators of the monomials.
972 * Requires this to be an isIntegral() polynomial.
974 Integer
numeratorGCD() const;
977 * Returns the GCD of the coefficients of the monomials.
978 * Requires this to be an isIntegral() polynomial.
982 /** z must divide all of the coefficients of the polynomial. */
983 Polynomial
exactDivide(const Integer
& z
) const;
985 Polynomial
operator+(const Polynomial
& vl
) const;
986 Polynomial
operator-(const Polynomial
& vl
) const;
987 Polynomial
operator-() const{
988 return (*this) * Rational(-1);
991 Polynomial
operator*(const Rational
& q
) const;
992 Polynomial
operator*(const Constant
& c
) const;
993 Polynomial
operator*(const Monomial
& mono
) const;
995 Polynomial
operator*(const Polynomial
& poly
) const;
998 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
999 * The quotient and remainder of p divided by the non-zero integer z is:
1000 * q := [(* floor(coeff_i/z) mono_i )]
1001 * r := [(* rem(coeff_i/z) mono_i)]
1002 * computeQR(p,z) returns the node (+ q r).
1004 * q and r are members of the Polynomial class.
1006 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
1007 * (+ (+ 2 x (* 4 y)) (+ 1 x))
1009 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
1011 /** Returns the coefficient associated with the VarList in the polynomial. */
1012 Constant
getCoefficient(const VarList
& vl
) const;
1014 uint32_t maxLength() const{
1015 iterator i
= begin(), e
=end();
1019 uint32_t max
= (*i
).coefficientLength();
1022 uint32_t curr
= (*i
).coefficientLength();
1031 uint32_t numMonomials() const {
1032 if( getNode().getKind() == kind::PLUS
){
1033 return getNode().getNumChildren();
1041 const Rational
& asConstant() const{
1042 Assert(isConstant());
1043 return getNode().getConst
<Rational
>();
1044 //return getHead().getConstant().getValue();
1047 bool isVarList() const {
1049 return VarList::isMember(getNode());
1055 VarList
asVarList() const {
1056 Assert(isVarList());
1057 return getHead().getVarList();
1060 size_t getComplexity() const;
1062 friend class SumPair
;
1063 friend class Comparison
;
1065 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1066 Node
makeAbsCondition(Variable v
){
1067 return makeAbsCondition(v
, *this);
1070 /** Returns a node that if asserted ensures v is the abs of p.*/
1071 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1073 };/* class Polynomial */
1077 * SumPair is a utility class that extends polynomials for use in computations.
1078 * A SumPair is always a combination of (+ p c) where
1079 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1081 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1082 * is known to implicitly be equal to 0.
1084 * SumPairs do not have unique representations due to the potential for p = 0.
1085 * This makes them inappropriate for normal forms.
1087 class SumPair
: public NodeWrapper
{
1089 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1090 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1093 SumPair(TNode n
) : NodeWrapper(n
) { Assert(isNormalForm()); }
1096 SumPair(const Polynomial
& p
):
1097 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1099 Assert(isNormalForm());
1102 SumPair(const Polynomial
& p
, const Constant
& c
):
1103 NodeWrapper(toNode(p
, c
))
1105 Assert(isNormalForm());
1108 static bool isMember(TNode n
) {
1109 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1110 if(Constant::isMember(n
[1])){
1111 if(Polynomial::isMember(n
[0])){
1112 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1113 return p
.isZero() || (!p
.containsConstant());
1125 bool isNormalForm() const {
1126 return isMember(getNode());
1129 Polynomial
getPolynomial() const {
1130 return Polynomial::parsePolynomial(getNode()[0]);
1133 Constant
getConstant() const {
1134 return Constant::mkConstant((getNode())[1]);
1137 SumPair
operator+(const SumPair
& other
) const {
1138 return SumPair(getPolynomial() + other
.getPolynomial(),
1139 getConstant() + other
.getConstant());
1142 SumPair
operator*(const Constant
& c
) const {
1143 return SumPair(getPolynomial() * c
, getConstant() * c
);
1146 SumPair
operator-(const SumPair
& other
) const {
1147 return (*this) + (other
* Constant::mkConstant(-1));
1150 static SumPair
mkSumPair(const Polynomial
& p
);
1152 static SumPair
mkSumPair(const Variable
& var
){
1153 return SumPair(Polynomial::mkPolynomial(var
));
1156 static SumPair
parseSumPair(TNode n
){
1160 bool isIntegral() const{
1161 return getConstant().isIntegral() && getPolynomial().isIntegral();
1164 bool isConstant() const {
1165 return getPolynomial().isZero();
1168 bool isZero() const {
1169 return getConstant().isZero() && isConstant();
1172 uint32_t size() const{
1173 return getPolynomial().size();
1176 bool isNonlinear() const{
1177 return getPolynomial().isNonlinear();
1181 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1182 * The SumPair must be integral.
1184 Integer
gcd() const {
1185 Assert(isIntegral());
1186 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1189 uint32_t maxLength() const {
1190 Assert(isIntegral());
1191 return std::max(getPolynomial().maxLength(), getConstant().length());
1194 static SumPair
mkZero() {
1195 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1198 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1200 };/* class SumPair */
1202 /* class OrderedPolynomialPair { */
1204 /* Polynomial d_first; */
1205 /* Polynomial d_second; */
1207 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1212 /* /\** Returns the first part of the pair. *\/ */
1213 /* const Polynomial& getFirst() const { */
1214 /* return d_first; */
1217 /* /\** Returns the second part of the pair. *\/ */
1218 /* const Polynomial& getSecond() const { */
1219 /* return d_second; */
1222 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1223 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1225 /* /\** Returns true if both of the polynomials are constant. *\/ */
1226 /* bool isConstant() const; */
1229 /* * Evaluates an isConstant() ordered pair as if */
1230 /* * (k getFirst() getRight()) */
1232 /* bool evaluateConstant(Kind k) const; */
1235 /* * Returns the Least Common Multiple of the monomials */
1236 /* * on the lefthand side and the constant on the right. */
1238 /* Integer denominatorLCM() const; */
1240 /* /\** Constructs a SumPair. *\/ */
1241 /* SumPair toSumPair() const; */
1244 /* OrderedPolynomialPair divideByGCD() const; */
1245 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1248 /* * Returns true if all of the variables are integers, */
1249 /* * and the coefficients are integers. */
1251 /* bool isIntegral() const; */
1253 /* /\** Returns true if all of the variables are integers. *\/ */
1254 /* bool allIntegralVariables() const { */
1255 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1259 class Comparison
: public NodeWrapper
{
1262 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1263 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1265 Comparison(TNode n
);
1268 * Creates a node in normal form equivalent to (= l 0).
1269 * All variables in l are integral.
1271 static Node
mkIntEquality(const Polynomial
& l
);
1274 * Creates a comparison equivalent to (k l 0).
1275 * k is either GT or GEQ.
1276 * All variables in l are integral.
1278 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1281 * Creates a node equivalent to (= l 0).
1282 * It is not the case that all variables in l are integral.
1284 static Node
mkRatEquality(const Polynomial
& l
);
1287 * Creates a comparison equivalent to (k l 0).
1288 * k is either GT or GEQ.
1289 * It is not the case that all variables in l are integral.
1291 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1295 Comparison(bool val
) :
1296 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1300 * Given a literal to TheoryArith return a single kind to
1301 * to indicate its underlying structure.
1302 * The function returns the following in each case:
1303 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1304 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1305 * - (NOT (EQUAL left right)) -> DISTINCT
1306 * - (NOT (GT left right)) -> LEQ
1307 * - (NOT (GEQ left right)) -> LT
1308 * If none of these match, it returns UNDEFINED_KIND.
1310 static Kind
comparisonKind(TNode literal
);
1312 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1314 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1316 /** Returns true if the comparison is a boolean constant. */
1317 bool isBoolean() const;
1320 * Returns true if the comparison is either a boolean term,
1321 * in integer normal form or mixed normal form.
1323 bool isNormalForm() const;
1326 bool isNormalGT() const;
1327 bool isNormalGEQ() const;
1329 bool isNormalLT() const;
1330 bool isNormalLEQ() const;
1332 bool isNormalEquality() const;
1333 bool isNormalDistinct() const;
1334 bool isNormalEqualityOrDisequality() const;
1336 bool allIntegralVariables() const {
1337 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1339 bool rightIsConstant() const;
1342 Polynomial
getLeft() const;
1343 Polynomial
getRight() const;
1345 /* /\** Normal form check if at least one variable is real. *\/ */
1346 /* bool isMixedCompareNormalForm() const; */
1348 /* /\** Normal form check if at least one variable is real. *\/ */
1349 /* bool isMixedEqualsNormalForm() const; */
1351 /* /\** Normal form check is all variables are integer.*\/ */
1352 /* bool isIntegerCompareNormalForm() const; */
1354 /* /\** Normal form check is all variables are integer.*\/ */
1355 /* bool isIntegerEqualsNormalForm() const; */
1359 * Returns true if all of the variables are integers, the coefficients are integers,
1360 * and the right hand coefficient is an integer.
1362 bool debugIsIntegral() const;
1364 static Comparison
parseNormalForm(TNode n
);
1366 inline static bool isNormalAtom(TNode n
){
1367 Comparison parse
= Comparison::parseNormalForm(n
);
1368 return parse
.isNormalForm();
1371 size_t getComplexity() const;
1373 SumPair
toSumPair() const;
1375 Polynomial
normalizedVariablePart() const;
1376 DeltaRational
normalizedDeltaRational() const;
1378 };/* class Comparison */
1380 }/* CVC4::theory::arith namespace */
1381 }/* CVC4::theory namespace */
1382 }/* CVC4 namespace */
1384 #endif /* CVC4__THEORY__ARITH__NORMAL_FORM_H */