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::IAND
: return isIAndMember(n
);
243 case kind::EXPONENTIAL
:
249 case kind::COTANGENT
:
251 case kind::ARCCOSINE
:
252 case kind::ARCTANGENT
:
253 case kind::ARCCOSECANT
:
254 case kind::ARCSECANT
:
255 case kind::ARCCOTANGENT
:
257 case kind::PI
: return isTranscendentalMember(n
);
259 case kind::TO_INTEGER
:
260 // Treat to_int as a variable; it is replaced in early preprocessing
263 default: return isLeafMember(n
);
267 static bool isLeafMember(Node n
);
268 static bool isIAndMember(Node n
);
269 static bool isDivMember(Node n
);
270 bool isDivLike() const{
271 return isDivMember(getNode());
273 static bool isTranscendentalMember(Node n
);
275 bool isNormalForm() { return isMember(getNode()); }
277 bool isIntegral() const {
278 return getNode().getType().isInteger();
281 bool isMetaKindVariable() const {
282 return getNode().isVar();
285 bool operator<(const Variable
& v
) const {
287 return cmp(this->getNode(), v
.getNode());
290 struct VariableNodeCmp
{
291 static inline int cmp(const Node
& n
, const Node
& m
) {
292 if ( n
== m
) { return 0; }
294 // this is now slightly off of the old variable order.
296 bool nIsInteger
= n
.getType().isInteger();
297 bool mIsInteger
= m
.getType().isInteger();
299 if(nIsInteger
== mIsInteger
){
300 bool nIsVariable
= n
.isVar();
301 bool mIsVariable
= m
.isVar();
303 if(nIsVariable
== mIsVariable
){
312 return -1; // nIsVariable => !mIsVariable
314 return 1; // !nIsVariable => mIsVariable
318 Assert(nIsInteger
!= mIsInteger
);
320 return 1; // nIsInteger => !mIsInteger
322 return -1; // !nIsInteger => mIsInteger
327 bool operator()(const Node
& n
, const Node
& m
) const {
328 return VariableNodeCmp::cmp(n
,m
) < 0;
332 bool operator==(const Variable
& v
) const { return getNode() == v
.getNode();}
334 size_t getComplexity() const;
335 };/* class Variable */
338 class Constant
: public NodeWrapper
{
340 Constant(Node n
) : NodeWrapper(n
) { Assert(isMember(getNode())); }
342 static bool isMember(Node n
) { return n
.getKind() == kind::CONST_RATIONAL
; }
344 bool isNormalForm() { return isMember(getNode()); }
346 static Constant
mkConstant(Node n
)
348 Assert(n
.getKind() == kind::CONST_RATIONAL
);
352 static Constant
mkConstant(const Rational
& rat
);
354 static Constant
mkZero() {
355 return mkConstant(Rational(0));
358 static Constant
mkOne() {
359 return mkConstant(Rational(1));
362 const Rational
& getValue() const {
363 return getNode().getConst
<Rational
>();
366 static int absCmp(const Constant
& a
, const Constant
& b
);
367 bool isIntegral() const { return getValue().isIntegral(); }
369 int sgn() const { return getValue().sgn(); }
371 bool isZero() const { return sgn() == 0; }
372 bool isNegative() const { return sgn() < 0; }
373 bool isPositive() const { return sgn() > 0; }
375 bool isOne() const { return getValue() == 1; }
377 Constant
operator*(const Rational
& other
) const {
378 return mkConstant(getValue() * other
);
381 Constant
operator*(const Constant
& other
) const {
382 return mkConstant(getValue() * other
.getValue());
384 Constant
operator+(const Constant
& other
) const {
385 return mkConstant(getValue() + other
.getValue());
387 Constant
operator-() const {
388 return mkConstant(-getValue());
391 Constant
inverse() const{
393 return mkConstant(getValue().inverse());
396 bool operator<(const Constant
& other
) const {
397 return getValue() < other
.getValue();
400 bool operator==(const Constant
& other
) const {
401 //Rely on node uniqueness.
402 return getNode() == other
.getNode();
405 Constant
abs() const {
413 uint32_t length() const{
414 Assert(isIntegral());
415 return getValue().getNumerator().length();
418 size_t getComplexity() const;
420 };/* class Constant */
423 template <class GetNodeIterator
>
424 inline Node
makeNode(Kind k
, GetNodeIterator start
, GetNodeIterator end
) {
427 while(start
!= end
) {
428 nb
<< (*start
).getNode();
433 }/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */
436 * A VarList is a sorted list of variables representing a product.
437 * If the VarList is empty, it represents an empty product or 1.
438 * If the VarList has size 1, it represents a single variable.
440 * A non-sorted VarList can never be successfully made in debug mode.
442 class VarList
: public NodeWrapper
{
445 static Node
multList(const std::vector
<Variable
>& list
) {
446 Assert(list
.size() >= 2);
448 return makeNode(kind::NONLINEAR_MULT
, list
.begin(), list
.end());
451 VarList() : NodeWrapper(Node::null()) {}
455 typedef expr::NodeSelfIterator internal_iterator
;
457 internal_iterator
internalBegin() const {
459 return expr::NodeSelfIterator::self(getNode());
461 return getNode().begin();
465 internal_iterator
internalEnd() const {
467 return expr::NodeSelfIterator::selfEnd(getNode());
469 return getNode().end();
475 class iterator
: public std::iterator
<std::input_iterator_tag
, Variable
> {
477 internal_iterator d_iter
;
480 explicit iterator(internal_iterator i
) : d_iter(i
) {}
482 inline Variable
operator*() {
483 return Variable(*d_iter
);
486 bool operator==(const iterator
& i
) {
487 return d_iter
== i
.d_iter
;
490 bool operator!=(const iterator
& i
) {
491 return d_iter
!= i
.d_iter
;
494 iterator
operator++() {
499 iterator
operator++(int) {
500 return iterator(d_iter
++);
504 iterator
begin() const {
505 return iterator(internalBegin());
508 iterator
end() const {
509 return iterator(internalEnd());
512 Variable
getHead() const {
517 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
518 Assert(isSorted(begin(), end()));
521 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
522 Assert(l
.size() >= 2);
523 Assert(isSorted(begin(), end()));
526 static bool isMember(Node n
);
528 bool isNormalForm() const {
532 static VarList
mkEmptyVarList() {
537 /** There are no restrictions on the size of l */
538 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
540 return mkEmptyVarList();
541 } else if(l
.size() == 1) {
542 return VarList((*l
.begin()).getNode());
548 bool empty() const { return getNode().isNull(); }
549 bool singleton() const {
550 return !empty() && getNode().getKind() != kind::NONLINEAR_MULT
;
557 return getNode().getNumChildren();
560 static VarList
parseVarList(Node n
);
562 VarList
operator*(const VarList
& vl
) const;
564 int cmp(const VarList
& vl
) const;
566 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
568 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
570 bool isIntegral() const {
571 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
573 if(!var
.isIntegral()){
579 size_t getComplexity() const;
582 bool isSorted(iterator start
, iterator end
);
584 };/* class VarList */
587 /** Constructors have side conditions. Use the static mkMonomial functions instead. */
588 class Monomial
: public NodeWrapper
{
592 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
593 NodeWrapper(n
), constant(c
), varList(vl
)
595 Assert(!c
.isZero() || vl
.empty());
596 Assert(c
.isZero() || !vl
.empty());
598 Assert(!c
.isOne() || !multStructured(n
));
601 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
605 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
608 static bool multStructured(Node n
) {
609 return n
.getKind() == kind::MULT
&&
610 n
[0].getKind() == kind::CONST_RATIONAL
&&
611 n
.getNumChildren() == 2;
614 Monomial(const Constant
& c
):
615 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
618 Monomial(const VarList
& vl
):
619 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
621 Assert(!varList
.empty());
624 Monomial(const Constant
& c
, const VarList
& vl
):
625 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
629 Assert(!varList
.empty());
631 Assert(multStructured(getNode()));
634 static bool isMember(TNode n
);
636 /** Makes a monomial with no restrictions on c and vl. */
637 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
639 /** If vl is empty, this make one. */
640 static Monomial
mkMonomial(const VarList
& vl
);
642 static Monomial
mkMonomial(const Constant
& c
){
646 static Monomial
mkMonomial(const Variable
& v
){
647 return Monomial(VarList(v
));
650 static Monomial
parseMonomial(Node n
);
652 static Monomial
mkZero() {
653 return Monomial(Constant::mkConstant(0));
655 static Monomial
mkOne() {
656 return Monomial(Constant::mkConstant(1));
658 const Constant
& getConstant() const { return constant
; }
659 const VarList
& getVarList() const { return varList
; }
661 bool isConstant() const {
662 return varList
.empty();
665 bool isZero() const {
666 return constant
.isZero();
669 bool coefficientIsOne() const {
670 return constant
.isOne();
673 bool absCoefficientIsOne() const {
674 return coefficientIsOne() || constant
.getValue() == -1;
677 bool constantIsPositive() const {
678 return getConstant().isPositive();
681 Monomial
operator*(const Rational
& q
) const;
682 Monomial
operator*(const Constant
& c
) const;
683 Monomial
operator*(const Monomial
& mono
) const;
685 Monomial
operator-() const{
686 return (*this) * Rational(-1);
690 int cmp(const Monomial
& mono
) const {
691 return getVarList().cmp(mono
.getVarList());
694 bool operator<(const Monomial
& vl
) const {
698 bool operator==(const Monomial
& vl
) const {
702 static bool isSorted(const std::vector
<Monomial
>& m
) {
703 return std::is_sorted(m
.begin(), m
.end());
706 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
707 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
710 static void sort(std::vector
<Monomial
>& m
);
711 static void combineAdjacentMonomials(std::vector
<Monomial
>& m
);
714 * The variable product
716 bool integralVariables() const {
717 return getVarList().isIntegral();
721 * The coefficient of the monomial is integral.
723 bool integralCoefficient() const {
724 return getConstant().isIntegral();
728 * A Monomial is an "integral" monomial if the constant is integral.
730 bool isIntegral() const {
731 return integralCoefficient() && integralVariables();
734 /** Returns true if the VarList is a product of at least 2 Variables.*/
735 bool isNonlinear() const {
736 return getVarList().size() >= 2;
740 * Given a sorted list of monomials, this function transforms this
741 * into a strictly sorted list of monomials that does not contain zero.
743 //static std::vector<Monomial> sumLikeTerms(const std::vector<Monomial>& monos);
745 int absCmp(const Monomial
& other
) const{
746 return getConstant().getValue().absCmp(other
.getConstant().getValue());
748 // bool absLessThan(const Monomial& other) const{
749 // return getConstant().abs() < other.getConstant().abs();
752 uint32_t coefficientLength() const{
753 return getConstant().length();
757 static void printList(const std::vector
<Monomial
>& list
);
759 size_t getComplexity() const;
760 };/* class Monomial */
765 class Polynomial
: public NodeWrapper
{
769 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
770 Assert(isMember(getNode()));
773 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
774 Assert(m
.size() >= 2);
776 return makeNode(kind::PLUS
, m
.begin(), m
.end());
779 typedef expr::NodeSelfIterator internal_iterator
;
781 internal_iterator
internalBegin() const {
783 return expr::NodeSelfIterator::self(getNode());
785 return getNode().begin();
789 internal_iterator
internalEnd() const {
791 return expr::NodeSelfIterator::selfEnd(getNode());
793 return getNode().end();
797 bool singleton() const { return d_singleton
; }
800 static bool isMember(TNode n
);
802 class iterator
: public std::iterator
<std::input_iterator_tag
, Monomial
> {
804 internal_iterator d_iter
;
807 explicit iterator(internal_iterator i
) : d_iter(i
) {}
809 inline Monomial
operator*() {
810 return Monomial::parseMonomial(*d_iter
);
813 bool operator==(const iterator
& i
) {
814 return d_iter
== i
.d_iter
;
817 bool operator!=(const iterator
& i
) {
818 return d_iter
!= i
.d_iter
;
821 iterator
operator++() {
826 iterator
operator++(int) {
827 return iterator(d_iter
++);
831 iterator
begin() const { return iterator(internalBegin()); }
832 iterator
end() const { return iterator(internalEnd()); }
834 Polynomial(const Monomial
& m
):
835 NodeWrapper(m
.getNode()), d_singleton(true)
838 Polynomial(const std::vector
<Monomial
>& m
):
839 NodeWrapper(makePlusNode(m
)), d_singleton(false)
841 Assert(m
.size() >= 2);
842 Assert(Monomial::isStrictlySorted(m
));
845 static Polynomial
mkPolynomial(const Constant
& c
){
846 return Polynomial(Monomial::mkMonomial(c
));
849 static Polynomial
mkPolynomial(const Variable
& v
){
850 return Polynomial(Monomial::mkMonomial(v
));
853 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
855 return Polynomial(Monomial::mkZero());
856 } else if(m
.size() == 1) {
857 return Polynomial((*m
.begin()));
859 return Polynomial(m
);
863 static Polynomial
parsePolynomial(Node n
) {
864 return Polynomial(n
);
867 static Polynomial
mkZero() {
868 return Polynomial(Monomial::mkZero());
870 static Polynomial
mkOne() {
871 return Polynomial(Monomial::mkOne());
873 bool isZero() const {
874 return singleton() && (getHead().isZero());
877 bool isConstant() const {
878 return singleton() && (getHead().isConstant());
881 bool containsConstant() const {
882 return getHead().isConstant();
885 uint32_t size() const{
889 Assert(getNode().getKind() == kind::PLUS
);
890 return getNode().getNumChildren();
894 Monomial
getHead() const {
898 Polynomial
getTail() const {
899 Assert(!singleton());
901 iterator tailStart
= begin();
903 std::vector
<Monomial
> subrange
;
904 std::copy(tailStart
, end(), std::back_inserter(subrange
));
905 return mkPolynomial(subrange
);
908 Monomial
minimumVariableMonomial() const;
909 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
911 void printList() const {
912 if(Debug
.isOn("normal-form")){
913 Debug("normal-form") << "start list" << std::endl
;
914 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
915 const Monomial
& m
=*i
;
918 Debug("normal-form") << "end list" << std::endl
;
922 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
923 bool allIntegralVariables() const {
924 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
925 if(!(*i
).integralVariables()){
933 * A Polynomial is an "integral" polynomial if all of the monomials are integral
934 * and all of the coefficients are Integral. */
935 bool isIntegral() const {
936 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
937 if(!(*i
).isIntegral()){
944 static Polynomial
sumPolynomials(const std::vector
<Polynomial
>& polynomials
);
946 /** Returns true if the polynomial contains a non-linear monomial.*/
947 bool isNonlinear() const;
949 /** Check whether this polynomial is only a single variable. */
950 bool isVariable() const
952 return singleton() && getHead().getVarList().singleton()
953 && getHead().coefficientIsOne();
955 /** Return the variable, given that isVariable() holds. */
956 Variable
getVariable() const
958 Assert(isVariable());
959 return getHead().getVarList().getHead();
963 * Selects a minimal monomial in the polynomial by the absolute value of
966 Monomial
selectAbsMinimum() const;
968 /** Returns true if the absolute value of the head coefficient is one. */
969 bool leadingCoefficientIsAbsOne() const;
970 bool leadingCoefficientIsPositive() const;
971 bool denominatorLCMIsOne() const;
972 bool numeratorGCDIsOne() const;
974 bool signNormalizedReducedSum() const {
975 return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne();
979 * Returns the Least Common Multiple of the denominators of the coefficients
982 Integer
denominatorLCM() const;
985 * Returns the GCD of the numerators of the monomials.
986 * Requires this to be an isIntegral() polynomial.
988 Integer
numeratorGCD() const;
991 * Returns the GCD of the coefficients of the monomials.
992 * Requires this to be an isIntegral() polynomial.
996 /** z must divide all of the coefficients of the polynomial. */
997 Polynomial
exactDivide(const Integer
& z
) const;
999 Polynomial
operator+(const Polynomial
& vl
) const;
1000 Polynomial
operator-(const Polynomial
& vl
) const;
1001 Polynomial
operator-() const{
1002 return (*this) * Rational(-1);
1005 Polynomial
operator*(const Rational
& q
) const;
1006 Polynomial
operator*(const Constant
& c
) const;
1007 Polynomial
operator*(const Monomial
& mono
) const;
1009 Polynomial
operator*(const Polynomial
& poly
) const;
1012 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
1013 * The quotient and remainder of p divided by the non-zero integer z is:
1014 * q := [(* floor(coeff_i/z) mono_i )]
1015 * r := [(* rem(coeff_i/z) mono_i)]
1016 * computeQR(p,z) returns the node (+ q r).
1018 * q and r are members of the Polynomial class.
1020 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
1021 * (+ (+ 2 x (* 4 y)) (+ 1 x))
1023 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
1025 /** Returns the coefficient associated with the VarList in the polynomial. */
1026 Constant
getCoefficient(const VarList
& vl
) const;
1028 uint32_t maxLength() const{
1029 iterator i
= begin(), e
=end();
1033 uint32_t max
= (*i
).coefficientLength();
1036 uint32_t curr
= (*i
).coefficientLength();
1045 uint32_t numMonomials() const {
1046 if( getNode().getKind() == kind::PLUS
){
1047 return getNode().getNumChildren();
1055 const Rational
& asConstant() const{
1056 Assert(isConstant());
1057 return getNode().getConst
<Rational
>();
1058 //return getHead().getConstant().getValue();
1061 bool isVarList() const {
1063 return VarList::isMember(getNode());
1069 VarList
asVarList() const {
1070 Assert(isVarList());
1071 return getHead().getVarList();
1074 size_t getComplexity() const;
1076 friend class SumPair
;
1077 friend class Comparison
;
1079 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1080 Node
makeAbsCondition(Variable v
){
1081 return makeAbsCondition(v
, *this);
1084 /** Returns a node that if asserted ensures v is the abs of p.*/
1085 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1087 };/* class Polynomial */
1091 * SumPair is a utility class that extends polynomials for use in computations.
1092 * A SumPair is always a combination of (+ p c) where
1093 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1095 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1096 * is known to implicitly be equal to 0.
1098 * SumPairs do not have unique representations due to the potential for p = 0.
1099 * This makes them inappropriate for normal forms.
1101 class SumPair
: public NodeWrapper
{
1103 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1104 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1107 SumPair(TNode n
) : NodeWrapper(n
) { Assert(isNormalForm()); }
1110 SumPair(const Polynomial
& p
):
1111 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1113 Assert(isNormalForm());
1116 SumPair(const Polynomial
& p
, const Constant
& c
):
1117 NodeWrapper(toNode(p
, c
))
1119 Assert(isNormalForm());
1122 static bool isMember(TNode n
) {
1123 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1124 if(Constant::isMember(n
[1])){
1125 if(Polynomial::isMember(n
[0])){
1126 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1127 return p
.isZero() || (!p
.containsConstant());
1139 bool isNormalForm() const {
1140 return isMember(getNode());
1143 Polynomial
getPolynomial() const {
1144 return Polynomial::parsePolynomial(getNode()[0]);
1147 Constant
getConstant() const {
1148 return Constant::mkConstant((getNode())[1]);
1151 SumPair
operator+(const SumPair
& other
) const {
1152 return SumPair(getPolynomial() + other
.getPolynomial(),
1153 getConstant() + other
.getConstant());
1156 SumPair
operator*(const Constant
& c
) const {
1157 return SumPair(getPolynomial() * c
, getConstant() * c
);
1160 SumPair
operator-(const SumPair
& other
) const {
1161 return (*this) + (other
* Constant::mkConstant(-1));
1164 static SumPair
mkSumPair(const Polynomial
& p
);
1166 static SumPair
mkSumPair(const Variable
& var
){
1167 return SumPair(Polynomial::mkPolynomial(var
));
1170 static SumPair
parseSumPair(TNode n
){
1174 bool isIntegral() const{
1175 return getConstant().isIntegral() && getPolynomial().isIntegral();
1178 bool isConstant() const {
1179 return getPolynomial().isZero();
1182 bool isZero() const {
1183 return getConstant().isZero() && isConstant();
1186 uint32_t size() const{
1187 return getPolynomial().size();
1190 bool isNonlinear() const{
1191 return getPolynomial().isNonlinear();
1195 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1196 * The SumPair must be integral.
1198 Integer
gcd() const {
1199 Assert(isIntegral());
1200 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1203 uint32_t maxLength() const {
1204 Assert(isIntegral());
1205 return std::max(getPolynomial().maxLength(), getConstant().length());
1208 static SumPair
mkZero() {
1209 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1212 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1214 };/* class SumPair */
1216 /* class OrderedPolynomialPair { */
1218 /* Polynomial d_first; */
1219 /* Polynomial d_second; */
1221 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1226 /* /\** Returns the first part of the pair. *\/ */
1227 /* const Polynomial& getFirst() const { */
1228 /* return d_first; */
1231 /* /\** Returns the second part of the pair. *\/ */
1232 /* const Polynomial& getSecond() const { */
1233 /* return d_second; */
1236 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1237 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1239 /* /\** Returns true if both of the polynomials are constant. *\/ */
1240 /* bool isConstant() const; */
1243 /* * Evaluates an isConstant() ordered pair as if */
1244 /* * (k getFirst() getRight()) */
1246 /* bool evaluateConstant(Kind k) const; */
1249 /* * Returns the Least Common Multiple of the monomials */
1250 /* * on the lefthand side and the constant on the right. */
1252 /* Integer denominatorLCM() const; */
1254 /* /\** Constructs a SumPair. *\/ */
1255 /* SumPair toSumPair() const; */
1258 /* OrderedPolynomialPair divideByGCD() const; */
1259 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1262 /* * Returns true if all of the variables are integers, */
1263 /* * and the coefficients are integers. */
1265 /* bool isIntegral() const; */
1267 /* /\** Returns true if all of the variables are integers. *\/ */
1268 /* bool allIntegralVariables() const { */
1269 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1273 class Comparison
: public NodeWrapper
{
1276 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1277 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1279 Comparison(TNode n
);
1282 * Creates a node in normal form equivalent to (= l 0).
1283 * All variables in l are integral.
1285 static Node
mkIntEquality(const Polynomial
& l
);
1288 * Creates a comparison equivalent to (k l 0).
1289 * k is either GT or GEQ.
1290 * All variables in l are integral.
1292 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1295 * Creates a node equivalent to (= l 0).
1296 * It is not the case that all variables in l are integral.
1298 static Node
mkRatEquality(const Polynomial
& l
);
1301 * Creates a comparison equivalent to (k l 0).
1302 * k is either GT or GEQ.
1303 * It is not the case that all variables in l are integral.
1305 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1309 Comparison(bool val
) :
1310 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1314 * Given a literal to TheoryArith return a single kind to
1315 * to indicate its underlying structure.
1316 * The function returns the following in each case:
1317 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1318 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1319 * - (NOT (EQUAL left right)) -> DISTINCT
1320 * - (NOT (GT left right)) -> LEQ
1321 * - (NOT (GEQ left right)) -> LT
1322 * If none of these match, it returns UNDEFINED_KIND.
1324 static Kind
comparisonKind(TNode literal
);
1326 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1328 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1330 /** Returns true if the comparison is a boolean constant. */
1331 bool isBoolean() const;
1334 * Returns true if the comparison is either a boolean term,
1335 * in integer normal form or mixed normal form.
1337 bool isNormalForm() const;
1340 bool isNormalGT() const;
1341 bool isNormalGEQ() const;
1343 bool isNormalLT() const;
1344 bool isNormalLEQ() const;
1346 bool isNormalEquality() const;
1347 bool isNormalDistinct() const;
1348 bool isNormalEqualityOrDisequality() const;
1350 bool allIntegralVariables() const {
1351 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1353 bool rightIsConstant() const;
1356 Polynomial
getLeft() const;
1357 Polynomial
getRight() const;
1359 /* /\** Normal form check if at least one variable is real. *\/ */
1360 /* bool isMixedCompareNormalForm() const; */
1362 /* /\** Normal form check if at least one variable is real. *\/ */
1363 /* bool isMixedEqualsNormalForm() const; */
1365 /* /\** Normal form check is all variables are integer.*\/ */
1366 /* bool isIntegerCompareNormalForm() const; */
1368 /* /\** Normal form check is all variables are integer.*\/ */
1369 /* bool isIntegerEqualsNormalForm() const; */
1373 * Returns true if all of the variables are integers, the coefficients are integers,
1374 * and the right hand coefficient is an integer.
1376 bool debugIsIntegral() const;
1378 static Comparison
parseNormalForm(TNode n
);
1380 inline static bool isNormalAtom(TNode n
){
1381 Comparison parse
= Comparison::parseNormalForm(n
);
1382 return parse
.isNormalForm();
1385 size_t getComplexity() const;
1387 SumPair
toSumPair() const;
1389 Polynomial
normalizedVariablePart() const;
1390 DeltaRational
normalizedDeltaRational() const;
1393 * Transforms a Comparison object into a stronger normal form:
1394 * Polynomial ~Kind~ Constant
1396 * From the comparison, this method resolved a negation (if present) and
1397 * moves everything to the left side.
1398 * If split_constant is false, the constant is always zero.
1399 * If split_constant is true, the polynomial has no constant term and is
1400 * normalized to have leading coefficient one.
1402 std::tuple
<Polynomial
, Kind
, Constant
> decompose(
1403 bool split_constant
= false) const;
1405 };/* class Comparison */
1407 }/* CVC4::theory::arith namespace */
1408 }/* CVC4::theory namespace */
1409 }/* CVC4 namespace */
1411 #endif /* CVC4__THEORY__ARITH__NORMAL_FORM_H */