1 /********************* */
2 /*! \file normal_form.h
4 ** Top contributors (to current version):
5 ** Tim King, Morgan Deters, Andrew Reynolds
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 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
) {
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
);
244 case kind::EXPONENTIAL
:
250 case kind::COTANGENT
:
252 case kind::ARCCOSINE
:
253 case kind::ARCTANGENT
:
254 case kind::ARCCOSECANT
:
255 case kind::ARCSECANT
:
256 case kind::ARCCOTANGENT
:
259 return isTranscendentalMember(n
);
261 case kind::TO_INTEGER
:
262 // Treat to_int as a variable; it is replaced in early preprocessing
266 return isLeafMember(n
);
270 static bool isLeafMember(Node n
);
271 static bool isDivMember(Node n
);
272 bool isDivLike() const{
273 return isDivMember(getNode());
275 static bool isTranscendentalMember(Node n
);
277 bool isNormalForm() { return isMember(getNode()); }
279 bool isIntegral() const {
280 return getNode().getType().isInteger();
283 bool isMetaKindVariable() const {
284 return getNode().isVar();
287 bool operator<(const Variable
& v
) const {
289 return cmp(this->getNode(), v
.getNode());
292 struct VariableNodeCmp
{
293 static inline int cmp(const Node
& n
, const Node
& m
) {
294 if ( n
== m
) { return 0; }
296 // this is now slightly off of the old variable order.
298 bool nIsInteger
= n
.getType().isInteger();
299 bool mIsInteger
= m
.getType().isInteger();
301 if(nIsInteger
== mIsInteger
){
302 bool nIsVariable
= n
.isVar();
303 bool mIsVariable
= m
.isVar();
305 if(nIsVariable
== mIsVariable
){
314 return -1; // nIsVariable => !mIsVariable
316 return 1; // !nIsVariable => mIsVariable
320 Assert(nIsInteger
!= mIsInteger
);
322 return 1; // nIsInteger => !mIsInteger
324 return -1; // !nIsInteger => mIsInteger
329 bool operator()(const Node
& n
, const Node
& m
) const {
330 return VariableNodeCmp::cmp(n
,m
) < 0;
334 bool operator==(const Variable
& v
) const { return getNode() == v
.getNode();}
336 size_t getComplexity() const;
337 };/* class Variable */
340 class Constant
: public NodeWrapper
{
342 Constant(Node n
) : NodeWrapper(n
) {
343 Assert(isMember(getNode()));
346 static bool isMember(Node n
) {
347 return n
.getKind() == kind::CONST_RATIONAL
;
350 bool isNormalForm() { return isMember(getNode()); }
352 static Constant
mkConstant(Node n
) {
353 Assert(n
.getKind() == kind::CONST_RATIONAL
);
357 static Constant
mkConstant(const Rational
& rat
);
359 static Constant
mkZero() {
360 return mkConstant(Rational(0));
363 static Constant
mkOne() {
364 return mkConstant(Rational(1));
367 const Rational
& getValue() const {
368 return getNode().getConst
<Rational
>();
371 static int absCmp(const Constant
& a
, const Constant
& b
);
372 bool isIntegral() const { return getValue().isIntegral(); }
374 int sgn() const { return getValue().sgn(); }
376 bool isZero() const { return sgn() == 0; }
377 bool isNegative() const { return sgn() < 0; }
378 bool isPositive() const { return sgn() > 0; }
380 bool isOne() const { return getValue() == 1; }
382 Constant
operator*(const Rational
& other
) const {
383 return mkConstant(getValue() * other
);
386 Constant
operator*(const Constant
& other
) const {
387 return mkConstant(getValue() * other
.getValue());
389 Constant
operator+(const Constant
& other
) const {
390 return mkConstant(getValue() + other
.getValue());
392 Constant
operator-() const {
393 return mkConstant(-getValue());
396 Constant
inverse() const{
398 return mkConstant(getValue().inverse());
401 bool operator<(const Constant
& other
) const {
402 return getValue() < other
.getValue();
405 bool operator==(const Constant
& other
) const {
406 //Rely on node uniqueness.
407 return getNode() == other
.getNode();
410 Constant
abs() const {
418 uint32_t length() const{
419 Assert(isIntegral());
420 return getValue().getNumerator().length();
423 size_t getComplexity() const;
425 };/* class Constant */
428 template <class GetNodeIterator
>
429 inline Node
makeNode(Kind k
, GetNodeIterator start
, GetNodeIterator end
) {
432 while(start
!= end
) {
433 nb
<< (*start
).getNode();
438 }/* makeNode<GetNodeIterator>(Kind, iterator, iterator) */
441 * A VarList is a sorted list of variables representing a product.
442 * If the VarList is empty, it represents an empty product or 1.
443 * If the VarList has size 1, it represents a single variable.
445 * A non-sorted VarList can never be successfully made in debug mode.
447 class VarList
: public NodeWrapper
{
450 static Node
multList(const std::vector
<Variable
>& list
) {
451 Assert(list
.size() >= 2);
453 return makeNode(kind::NONLINEAR_MULT
, list
.begin(), list
.end());
456 VarList() : NodeWrapper(Node::null()) {}
460 typedef expr::NodeSelfIterator internal_iterator
;
462 internal_iterator
internalBegin() const {
464 return expr::NodeSelfIterator::self(getNode());
466 return getNode().begin();
470 internal_iterator
internalEnd() const {
472 return expr::NodeSelfIterator::selfEnd(getNode());
474 return getNode().end();
480 class iterator
: public std::iterator
<std::input_iterator_tag
, Variable
> {
482 internal_iterator d_iter
;
485 explicit iterator(internal_iterator i
) : d_iter(i
) {}
487 inline Variable
operator*() {
488 return Variable(*d_iter
);
491 bool operator==(const iterator
& i
) {
492 return d_iter
== i
.d_iter
;
495 bool operator!=(const iterator
& i
) {
496 return d_iter
!= i
.d_iter
;
499 iterator
operator++() {
504 iterator
operator++(int) {
505 return iterator(d_iter
++);
509 iterator
begin() const {
510 return iterator(internalBegin());
513 iterator
end() const {
514 return iterator(internalEnd());
517 Variable
getHead() const {
522 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
523 Assert(isSorted(begin(), end()));
526 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
527 Assert(l
.size() >= 2);
528 Assert(isSorted(begin(), end()));
531 static bool isMember(Node n
);
533 bool isNormalForm() const {
537 static VarList
mkEmptyVarList() {
542 /** There are no restrictions on the size of l */
543 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
545 return mkEmptyVarList();
546 } else if(l
.size() == 1) {
547 return VarList((*l
.begin()).getNode());
553 bool empty() const { return getNode().isNull(); }
554 bool singleton() const {
555 return !empty() && getNode().getKind() != kind::NONLINEAR_MULT
;
562 return getNode().getNumChildren();
565 static VarList
parseVarList(Node n
);
567 VarList
operator*(const VarList
& vl
) const;
569 int cmp(const VarList
& vl
) const;
571 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
573 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
575 bool isIntegral() const {
576 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
578 if(!var
.isIntegral()){
584 size_t getComplexity() const;
587 bool isSorted(iterator start
, iterator end
);
589 };/* class VarList */
592 /** Constructors have side conditions. Use the static mkMonomial functions instead. */
593 class Monomial
: public NodeWrapper
{
597 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
598 NodeWrapper(n
), constant(c
), varList(vl
)
600 Assert(!c
.isZero() || vl
.empty() );
601 Assert( c
.isZero() || !vl
.empty() );
603 Assert(!c
.isOne() || !multStructured(n
));
606 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
610 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
613 static bool multStructured(Node n
) {
614 return n
.getKind() == kind::MULT
&&
615 n
[0].getKind() == kind::CONST_RATIONAL
&&
616 n
.getNumChildren() == 2;
619 Monomial(const Constant
& c
):
620 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
623 Monomial(const VarList
& vl
):
624 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
626 Assert( !varList
.empty() );
629 Monomial(const Constant
& c
, const VarList
& vl
):
630 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
632 Assert( !c
.isZero() );
633 Assert( !c
.isOne() );
634 Assert( !varList
.empty() );
636 Assert(multStructured(getNode()));
639 static bool isMember(TNode n
);
641 /** Makes a monomial with no restrictions on c and vl. */
642 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
644 /** If vl is empty, this make one. */
645 static Monomial
mkMonomial(const VarList
& vl
);
647 static Monomial
mkMonomial(const Constant
& c
){
651 static Monomial
mkMonomial(const Variable
& v
){
652 return Monomial(VarList(v
));
655 static Monomial
parseMonomial(Node n
);
657 static Monomial
mkZero() {
658 return Monomial(Constant::mkConstant(0));
660 static Monomial
mkOne() {
661 return Monomial(Constant::mkConstant(1));
663 const Constant
& getConstant() const { return constant
; }
664 const VarList
& getVarList() const { return varList
; }
666 bool isConstant() const {
667 return varList
.empty();
670 bool isZero() const {
671 return constant
.isZero();
674 bool coefficientIsOne() const {
675 return constant
.isOne();
678 bool absCoefficientIsOne() const {
679 return coefficientIsOne() || constant
.getValue() == -1;
682 bool constantIsPositive() const {
683 return getConstant().isPositive();
686 Monomial
operator*(const Rational
& q
) const;
687 Monomial
operator*(const Constant
& c
) const;
688 Monomial
operator*(const Monomial
& mono
) const;
690 Monomial
operator-() const{
691 return (*this) * Rational(-1);
695 int cmp(const Monomial
& mono
) const {
696 return getVarList().cmp(mono
.getVarList());
699 bool operator<(const Monomial
& vl
) const {
703 bool operator==(const Monomial
& vl
) const {
707 static bool isSorted(const std::vector
<Monomial
>& m
) {
708 return std::is_sorted(m
.begin(), m
.end());
711 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
712 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
715 static void sort(std::vector
<Monomial
>& m
);
716 static void combineAdjacentMonomials(std::vector
<Monomial
>& m
);
719 * The variable product
721 bool integralVariables() const {
722 return getVarList().isIntegral();
726 * The coefficient of the monomial is integral.
728 bool integralCoefficient() const {
729 return getConstant().isIntegral();
733 * A Monomial is an "integral" monomial if the constant is integral.
735 bool isIntegral() const {
736 return integralCoefficient() && integralVariables();
739 /** Returns true if the VarList is a product of at least 2 Variables.*/
740 bool isNonlinear() const {
741 return getVarList().size() >= 2;
745 * Given a sorted list of monomials, this function transforms this
746 * into a strictly sorted list of monomials that does not contain zero.
748 //static std::vector<Monomial> sumLikeTerms(const std::vector<Monomial>& monos);
750 int absCmp(const Monomial
& other
) const{
751 return getConstant().getValue().absCmp(other
.getConstant().getValue());
753 // bool absLessThan(const Monomial& other) const{
754 // return getConstant().abs() < other.getConstant().abs();
757 uint32_t coefficientLength() const{
758 return getConstant().length();
762 static void printList(const std::vector
<Monomial
>& list
);
764 size_t getComplexity() const;
765 };/* class Monomial */
770 class Polynomial
: public NodeWrapper
{
774 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
775 Assert(isMember(getNode()));
778 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
779 Assert(m
.size() >= 2);
781 return makeNode(kind::PLUS
, m
.begin(), m
.end());
784 typedef expr::NodeSelfIterator internal_iterator
;
786 internal_iterator
internalBegin() const {
788 return expr::NodeSelfIterator::self(getNode());
790 return getNode().begin();
794 internal_iterator
internalEnd() const {
796 return expr::NodeSelfIterator::selfEnd(getNode());
798 return getNode().end();
802 bool singleton() const { return d_singleton
; }
805 static bool isMember(TNode n
);
807 class iterator
: public std::iterator
<std::input_iterator_tag
, Monomial
> {
809 internal_iterator d_iter
;
812 explicit iterator(internal_iterator i
) : d_iter(i
) {}
814 inline Monomial
operator*() {
815 return Monomial::parseMonomial(*d_iter
);
818 bool operator==(const iterator
& i
) {
819 return d_iter
== i
.d_iter
;
822 bool operator!=(const iterator
& i
) {
823 return d_iter
!= i
.d_iter
;
826 iterator
operator++() {
831 iterator
operator++(int) {
832 return iterator(d_iter
++);
836 iterator
begin() const { return iterator(internalBegin()); }
837 iterator
end() const { return iterator(internalEnd()); }
839 Polynomial(const Monomial
& m
):
840 NodeWrapper(m
.getNode()), d_singleton(true)
843 Polynomial(const std::vector
<Monomial
>& m
):
844 NodeWrapper(makePlusNode(m
)), d_singleton(false)
846 Assert( m
.size() >= 2);
847 Assert( Monomial::isStrictlySorted(m
) );
850 static Polynomial
mkPolynomial(const Constant
& c
){
851 return Polynomial(Monomial::mkMonomial(c
));
854 static Polynomial
mkPolynomial(const Variable
& v
){
855 return Polynomial(Monomial::mkMonomial(v
));
858 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
860 return Polynomial(Monomial::mkZero());
861 } else if(m
.size() == 1) {
862 return Polynomial((*m
.begin()));
864 return Polynomial(m
);
868 static Polynomial
parsePolynomial(Node n
) {
869 return Polynomial(n
);
872 static Polynomial
mkZero() {
873 return Polynomial(Monomial::mkZero());
875 static Polynomial
mkOne() {
876 return Polynomial(Monomial::mkOne());
878 bool isZero() const {
879 return singleton() && (getHead().isZero());
882 bool isConstant() const {
883 return singleton() && (getHead().isConstant());
886 bool containsConstant() const {
887 return getHead().isConstant();
890 uint32_t size() const{
894 Assert(getNode().getKind() == kind::PLUS
);
895 return getNode().getNumChildren();
899 Monomial
getHead() const {
903 Polynomial
getTail() const {
904 Assert(! singleton());
906 iterator tailStart
= begin();
908 std::vector
<Monomial
> subrange
;
909 std::copy(tailStart
, end(), std::back_inserter(subrange
));
910 return mkPolynomial(subrange
);
913 Monomial
minimumVariableMonomial() const;
914 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
916 void printList() const {
917 if(Debug
.isOn("normal-form")){
918 Debug("normal-form") << "start list" << std::endl
;
919 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
920 const Monomial
& m
=*i
;
923 Debug("normal-form") << "end list" << std::endl
;
927 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
928 bool allIntegralVariables() const {
929 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
930 if(!(*i
).integralVariables()){
938 * A Polynomial is an "integral" polynomial if all of the monomials are integral
939 * and all of the coefficients are Integral. */
940 bool isIntegral() const {
941 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
942 if(!(*i
).isIntegral()){
949 static Polynomial
sumPolynomials(const std::vector
<Polynomial
>& polynomials
);
951 /** Returns true if the polynomial contains a non-linear monomial.*/
952 bool isNonlinear() const;
956 * Selects a minimal monomial in the polynomial by the absolute value of
959 Monomial
selectAbsMinimum() const;
961 /** Returns true if the absolute value of the head coefficient is one. */
962 bool leadingCoefficientIsAbsOne() const;
963 bool leadingCoefficientIsPositive() const;
964 bool denominatorLCMIsOne() const;
965 bool numeratorGCDIsOne() const;
967 bool signNormalizedReducedSum() const {
968 return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne();
972 * Returns the Least Common Multiple of the denominators of the coefficients
975 Integer
denominatorLCM() const;
978 * Returns the GCD of the numerators of the monomials.
979 * Requires this to be an isIntegral() polynomial.
981 Integer
numeratorGCD() const;
984 * Returns the GCD of the coefficients of the monomials.
985 * Requires this to be an isIntegral() polynomial.
989 /** z must divide all of the coefficients of the polynomial. */
990 Polynomial
exactDivide(const Integer
& z
) const;
992 Polynomial
operator+(const Polynomial
& vl
) const;
993 Polynomial
operator-(const Polynomial
& vl
) const;
994 Polynomial
operator-() const{
995 return (*this) * Rational(-1);
998 Polynomial
operator*(const Rational
& q
) const;
999 Polynomial
operator*(const Constant
& c
) const;
1000 Polynomial
operator*(const Monomial
& mono
) const;
1002 Polynomial
operator*(const Polynomial
& poly
) const;
1005 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
1006 * The quotient and remainder of p divided by the non-zero integer z is:
1007 * q := [(* floor(coeff_i/z) mono_i )]
1008 * r := [(* rem(coeff_i/z) mono_i)]
1009 * computeQR(p,z) returns the node (+ q r).
1011 * q and r are members of the Polynomial class.
1013 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
1014 * (+ (+ 2 x (* 4 y)) (+ 1 x))
1016 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
1018 /** Returns the coefficient associated with the VarList in the polynomial. */
1019 Constant
getCoefficient(const VarList
& vl
) const;
1021 uint32_t maxLength() const{
1022 iterator i
= begin(), e
=end();
1026 uint32_t max
= (*i
).coefficientLength();
1029 uint32_t curr
= (*i
).coefficientLength();
1038 uint32_t numMonomials() const {
1039 if( getNode().getKind() == kind::PLUS
){
1040 return getNode().getNumChildren();
1048 const Rational
& asConstant() const{
1049 Assert(isConstant());
1050 return getNode().getConst
<Rational
>();
1051 //return getHead().getConstant().getValue();
1054 bool isVarList() const {
1056 return VarList::isMember(getNode());
1062 VarList
asVarList() const {
1063 Assert(isVarList());
1064 return getHead().getVarList();
1067 size_t getComplexity() const;
1069 friend class SumPair
;
1070 friend class Comparison
;
1072 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1073 Node
makeAbsCondition(Variable v
){
1074 return makeAbsCondition(v
, *this);
1077 /** Returns a node that if asserted ensures v is the abs of p.*/
1078 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1080 };/* class Polynomial */
1084 * SumPair is a utility class that extends polynomials for use in computations.
1085 * A SumPair is always a combination of (+ p c) where
1086 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1088 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1089 * is known to implicitly be equal to 0.
1091 * SumPairs do not have unique representations due to the potential for p = 0.
1092 * This makes them inappropriate for normal forms.
1094 class SumPair
: public NodeWrapper
{
1096 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1097 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1103 Assert(isNormalForm());
1108 SumPair(const Polynomial
& p
):
1109 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1111 Assert(isNormalForm());
1114 SumPair(const Polynomial
& p
, const Constant
& c
):
1115 NodeWrapper(toNode(p
, c
))
1117 Assert(isNormalForm());
1120 static bool isMember(TNode n
) {
1121 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1122 if(Constant::isMember(n
[1])){
1123 if(Polynomial::isMember(n
[0])){
1124 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1125 return p
.isZero() || (!p
.containsConstant());
1137 bool isNormalForm() const {
1138 return isMember(getNode());
1141 Polynomial
getPolynomial() const {
1142 return Polynomial::parsePolynomial(getNode()[0]);
1145 Constant
getConstant() const {
1146 return Constant::mkConstant((getNode())[1]);
1149 SumPair
operator+(const SumPair
& other
) const {
1150 return SumPair(getPolynomial() + other
.getPolynomial(),
1151 getConstant() + other
.getConstant());
1154 SumPair
operator*(const Constant
& c
) const {
1155 return SumPair(getPolynomial() * c
, getConstant() * c
);
1158 SumPair
operator-(const SumPair
& other
) const {
1159 return (*this) + (other
* Constant::mkConstant(-1));
1162 static SumPair
mkSumPair(const Polynomial
& p
);
1164 static SumPair
mkSumPair(const Variable
& var
){
1165 return SumPair(Polynomial::mkPolynomial(var
));
1168 static SumPair
parseSumPair(TNode n
){
1172 bool isIntegral() const{
1173 return getConstant().isIntegral() && getPolynomial().isIntegral();
1176 bool isConstant() const {
1177 return getPolynomial().isZero();
1180 bool isZero() const {
1181 return getConstant().isZero() && isConstant();
1184 uint32_t size() const{
1185 return getPolynomial().size();
1188 bool isNonlinear() const{
1189 return getPolynomial().isNonlinear();
1193 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1194 * The SumPair must be integral.
1196 Integer
gcd() const {
1197 Assert(isIntegral());
1198 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1201 uint32_t maxLength() const {
1202 Assert(isIntegral());
1203 return std::max(getPolynomial().maxLength(), getConstant().length());
1206 static SumPair
mkZero() {
1207 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1210 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1212 };/* class SumPair */
1214 /* class OrderedPolynomialPair { */
1216 /* Polynomial d_first; */
1217 /* Polynomial d_second; */
1219 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1224 /* /\** Returns the first part of the pair. *\/ */
1225 /* const Polynomial& getFirst() const { */
1226 /* return d_first; */
1229 /* /\** Returns the second part of the pair. *\/ */
1230 /* const Polynomial& getSecond() const { */
1231 /* return d_second; */
1234 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1235 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1237 /* /\** Returns true if both of the polynomials are constant. *\/ */
1238 /* bool isConstant() const; */
1241 /* * Evaluates an isConstant() ordered pair as if */
1242 /* * (k getFirst() getRight()) */
1244 /* bool evaluateConstant(Kind k) const; */
1247 /* * Returns the Least Common Multiple of the monomials */
1248 /* * on the lefthand side and the constant on the right. */
1250 /* Integer denominatorLCM() const; */
1252 /* /\** Constructs a SumPair. *\/ */
1253 /* SumPair toSumPair() const; */
1256 /* OrderedPolynomialPair divideByGCD() const; */
1257 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1260 /* * Returns true if all of the variables are integers, */
1261 /* * and the coefficients are integers. */
1263 /* bool isIntegral() const; */
1265 /* /\** Returns true if all of the variables are integers. *\/ */
1266 /* bool allIntegralVariables() const { */
1267 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1271 class Comparison
: public NodeWrapper
{
1274 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1275 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1277 Comparison(TNode n
);
1280 * Creates a node in normal form equivalent to (= l 0).
1281 * All variables in l are integral.
1283 static Node
mkIntEquality(const Polynomial
& l
);
1286 * Creates a comparison equivalent to (k l 0).
1287 * k is either GT or GEQ.
1288 * All variables in l are integral.
1290 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1293 * Creates a node equivalent to (= l 0).
1294 * It is not the case that all variables in l are integral.
1296 static Node
mkRatEquality(const Polynomial
& l
);
1299 * Creates a comparison equivalent to (k l 0).
1300 * k is either GT or GEQ.
1301 * It is not the case that all variables in l are integral.
1303 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1307 Comparison(bool val
) :
1308 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1312 * Given a literal to TheoryArith return a single kind to
1313 * to indicate its underlying structure.
1314 * The function returns the following in each case:
1315 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1316 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1317 * - (NOT (EQUAL left right)) -> DISTINCT
1318 * - (NOT (GT left right)) -> LEQ
1319 * - (NOT (GEQ left right)) -> LT
1320 * If none of these match, it returns UNDEFINED_KIND.
1322 static Kind
comparisonKind(TNode literal
);
1324 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1326 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1328 /** Returns true if the comparison is a boolean constant. */
1329 bool isBoolean() const;
1332 * Returns true if the comparison is either a boolean term,
1333 * in integer normal form or mixed normal form.
1335 bool isNormalForm() const;
1338 bool isNormalGT() const;
1339 bool isNormalGEQ() const;
1341 bool isNormalLT() const;
1342 bool isNormalLEQ() const;
1344 bool isNormalEquality() const;
1345 bool isNormalDistinct() const;
1346 bool isNormalEqualityOrDisequality() const;
1348 bool allIntegralVariables() const {
1349 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1351 bool rightIsConstant() const;
1354 Polynomial
getLeft() const;
1355 Polynomial
getRight() const;
1357 /* /\** Normal form check if at least one variable is real. *\/ */
1358 /* bool isMixedCompareNormalForm() const; */
1360 /* /\** Normal form check if at least one variable is real. *\/ */
1361 /* bool isMixedEqualsNormalForm() const; */
1363 /* /\** Normal form check is all variables are integer.*\/ */
1364 /* bool isIntegerCompareNormalForm() const; */
1366 /* /\** Normal form check is all variables are integer.*\/ */
1367 /* bool isIntegerEqualsNormalForm() const; */
1371 * Returns true if all of the variables are integers, the coefficients are integers,
1372 * and the right hand coefficient is an integer.
1374 bool debugIsIntegral() const;
1376 static Comparison
parseNormalForm(TNode n
);
1378 inline static bool isNormalAtom(TNode n
){
1379 Comparison parse
= Comparison::parseNormalForm(n
);
1380 return parse
.isNormalForm();
1383 size_t getComplexity() const;
1385 SumPair
toSumPair() const;
1387 Polynomial
normalizedVariablePart() const;
1388 DeltaRational
normalizedDeltaRational() const;
1390 };/* class Comparison */
1392 }/* CVC4::theory::arith namespace */
1393 }/* CVC4::theory namespace */
1394 }/* CVC4 namespace */
1396 #endif /* CVC4__THEORY__ARITH__NORMAL_FORM_H */