1 /******************************************************************************
2 * Top contributors (to current version):
3 * Tim King, Morgan Deters, Gereon Kremer
5 * This file is part of the cvc5 project.
7 * Copyright (c) 2009-2021 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.
11 * ****************************************************************************
13 * [[ Add one-line brief description here ]]
15 * [[ Add lengthier description here ]]
16 * \todo document this file
19 #include "cvc5_private.h"
21 #ifndef CVC5__THEORY__ARITH__NORMAL_FORM_H
22 #define CVC5__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"
36 /***********************************************/
37 /***************** Normal Form *****************/
38 /***********************************************/
39 /***********************************************/
42 * Section 1: Languages
43 * The normal form for arithmetic nodes is defined by the language
44 * accepted by the following BNFs with some guard conditions.
45 * (The guard conditions are in Section 3 for completeness.)
49 * n.isVar() or is foreign
50 * n.getType() \in {Integer, Real}
54 * n.getKind() == kind::CONST_RATIONAL
56 * var_list := variable | (* [variable])
59 * isSorted varOrder [variable]
61 * monomial := constant | var_list | (* constant' var_list')
63 * \f$ constant' \not\in {0,1} \f$
65 * polynomial := monomial' | (+ [monomial])
68 * isStrictlySorted monoOrder [monomial]
69 * forall (\x -> x != 0) [monomial]
71 * rational_cmp := (|><| qpolynomial constant)
74 * not (exists constantMonomial (monomialList qpolynomial))
75 * (exists realMonomial (monomialList qpolynomial))
76 * abs(monomialCoefficient (head (monomialList qpolynomial))) == 1
78 * integer_cmp := (>= zpolynomial constant)
80 * not (exists constantMonomial (monomialList zpolynomial))
81 * (forall integerMonomial (monomialList zpolynomial))
82 * the gcd of all numerators of coefficients is 1
83 * the denominator of all coefficients and the constant is 1
84 * the leading coefficient is positive
86 * rational_eq := (= qvarlist qpolynomial)
88 * let allMonomials = (cons qvarlist (monomialList zpolynomial))
89 * let variableMonomials = (drop constantMonomial allMonomials)
90 * isStrictlySorted variableMonomials
91 * exists realMonomial variableMonomials
92 * is not empty qvarlist
94 * integer_eq := (= zmonomial zpolynomial)
96 * let allMonomials = (cons zmonomial (monomialList zpolynomial))
97 * let variableMonomials = (drop constantMonomial allMonomials)
98 * not (constantMonomial zmonomial)
99 * (forall integerMonomial allMonomials)
100 * isStrictlySorted variableMonomials
101 * the gcd of all numerators of coefficients is 1
102 * the denominator of all coefficients and the constant is 1
103 * the coefficient of monomial is positive
104 * the value of the coefficient of monomial is minimal in variableMonomials
106 * comparison := TRUE | FALSE
107 * | rational_cmp | (not rational_cmp)
108 * | rational_eq | (not rational_eq)
109 * | integer_cmp | (not integer_cmp)
110 * | integer_eq | (not integer_eq)
112 * Normal Form for terms := polynomial
113 * Normal Form for atoms := comparison
117 * Section 2: Helper Classes
118 * The langauges accepted by each of these defintions
119 * roughly corresponds to one of the following helper classes:
127 * Each of the classes obeys the following contracts/design decisions:
128 * -Calling isMember(Node node) on a node returns true iff that node is a
129 * a member of the language. Note: isMember is O(n).
130 * -Calling isNormalForm() on a helper class object returns true iff that
131 * helper class currently represents a normal form object.
132 * -If isNormalForm() is false, then this object must have been made
133 * using a mk*() factory function.
134 * -If isNormalForm() is true, calling getNode() on all of these classes
135 * returns a node that would be accepted by the corresponding language.
136 * And if isNormalForm() is false, returns Node::null().
137 * -Each of the classes is immutable.
138 * -Public facing constuctors have a 1-to-1 correspondence with one of
139 * production rules in the above grammar.
140 * -Public facing constuctors are required to fail in debug mode when the
141 * guards of the production rule are not strictly met.
142 * For example: Monomial(Constant(1),VarList(Variable(x))) must fail.
143 * -When a class has a Class parseClass(Node node) function,
144 * if isMember(node) is true, the function is required to return an instance
145 * of the helper class, instance, s.t. instance.getNode() == node.
146 * And if isMember(node) is false, this throws an assertion failure in debug
147 * mode and has undefined behaviour if not in debug mode.
148 * -Only public facing constructors, parseClass(node), and mk*() functions are
149 * considered privileged functions for the helper class.
150 * -Only privileged functions may use private constructors, and access
151 * private data members.
152 * -All non-privileged functions are considered utility functions and
153 * must use a privileged function in order to create an instance of the class.
157 * Section 3: Guard Conditions Misc.
160 * variable_order x y =
161 * if (meta_kind_variable x) and (meta_kind_variable y)
162 * then node_order x y
163 * else if (meta_kind_variable x)
165 * else if (meta_kind_variable y)
167 * else node_order x y
172 * | (* [variable]) -> len [variable]
176 * Empty -> (0,Node::null())
177 * | NonEmpty(vl) -> (var_list_len vl, vl)
179 * var_listOrder a b = tuple_cmp (order a) (order b)
181 * monomialVarList monomial =
182 * match monomial with
184 * | var_list -> NonEmpty(var_list)
185 * | (* constant' var_list') -> NonEmpty(var_list')
187 * monoOrder m0 m1 = var_listOrder (monomialVarList m0) (monomialVarList m1)
189 * integerMonomial mono =
190 * forall varHasTypeInteger (monomialVarList mono)
192 * realMonomial mono = not (integerMonomial mono)
194 * constantMonomial monomial =
195 * match monomial with
197 * | var_list -> false
198 * | (* constant' var_list') -> false
200 * monomialCoefficient monomial =
201 * match monomial with
202 * constant -> constant
203 * | var_list -> Constant(1)
204 * | (* constant' var_list') -> constant'
206 * monomialList polynomial =
207 * match polynomial with
208 * monomial -> monomial::[]
209 * | (+ [monomial]) -> [monomial]
213 * A NodeWrapper is a class that is a thinly veiled container of a Node object.
219 NodeWrapper(Node n
) : node(n
) {}
220 const Node
& getNode() const { return node
; }
221 };/* class NodeWrapper */
224 class Variable
: public NodeWrapper
{
226 Variable(Node n
) : NodeWrapper(n
) { Assert(isMember(getNode())); }
228 // TODO: check if it's a theory leaf also
229 static bool isMember(Node n
)
231 Kind k
= n
.getKind();
234 case kind::CONST_RATIONAL
: return false;
235 case kind::INTS_DIVISION
:
236 case kind::INTS_MODULUS
:
238 case kind::INTS_DIVISION_TOTAL
:
239 case kind::INTS_MODULUS_TOTAL
:
240 case kind::DIVISION_TOTAL
: return isDivMember(n
);
241 case kind::IAND
: return isIAndMember(n
);
242 case kind::POW2
: return isPow2Member(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 isPow2Member(Node n
);
270 static bool isDivMember(Node n
);
271 bool isDivLike() const{
272 return isDivMember(getNode());
274 static bool isTranscendentalMember(Node n
);
276 bool isNormalForm() { return isMember(getNode()); }
278 bool isIntegral() const {
279 return getNode().getType().isInteger();
282 bool isMetaKindVariable() const {
283 return getNode().isVar();
286 bool operator<(const Variable
& v
) const {
288 return cmp(this->getNode(), v
.getNode());
291 struct VariableNodeCmp
{
292 static inline int cmp(const Node
& n
, const Node
& m
) {
293 if ( n
== m
) { return 0; }
295 // this is now slightly off of the old variable order.
297 bool nIsInteger
= n
.getType().isInteger();
298 bool mIsInteger
= m
.getType().isInteger();
300 if(nIsInteger
== mIsInteger
){
301 bool nIsVariable
= n
.isVar();
302 bool mIsVariable
= m
.isVar();
304 if(nIsVariable
== mIsVariable
){
313 return -1; // nIsVariable => !mIsVariable
315 return 1; // !nIsVariable => mIsVariable
319 Assert(nIsInteger
!= mIsInteger
);
321 return 1; // nIsInteger => !mIsInteger
323 return -1; // !nIsInteger => mIsInteger
328 bool operator()(const Node
& n
, const Node
& m
) const {
329 return VariableNodeCmp::cmp(n
,m
) < 0;
333 bool operator==(const Variable
& v
) const { return getNode() == v
.getNode();}
335 size_t getComplexity() const;
336 };/* 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();
477 internal_iterator d_iter
;
480 /* The following types are required by trait std::iterator_traits */
483 using iterator_category
= std::forward_iterator_tag
;
485 /** The type of the item */
486 using value_type
= Variable
;
488 /** The pointer type of the item */
489 using pointer
= Variable
*;
491 /** The reference type of the item */
492 using reference
= Variable
&;
494 /** The type returned when two iterators are subtracted */
495 using difference_type
= std::ptrdiff_t;
497 /* End of std::iterator_traits required types */
499 explicit iterator(internal_iterator i
) : d_iter(i
) {}
501 inline Variable
operator*() {
502 return Variable(*d_iter
);
505 bool operator==(const iterator
& i
) {
506 return d_iter
== i
.d_iter
;
509 bool operator!=(const iterator
& i
) {
510 return d_iter
!= i
.d_iter
;
513 iterator
operator++() {
518 iterator
operator++(int) {
519 return iterator(d_iter
++);
523 iterator
begin() const {
524 return iterator(internalBegin());
527 iterator
end() const {
528 return iterator(internalEnd());
531 Variable
getHead() const {
536 VarList(Variable v
) : NodeWrapper(v
.getNode()) {
537 Assert(isSorted(begin(), end()));
540 VarList(const std::vector
<Variable
>& l
) : NodeWrapper(multList(l
)) {
541 Assert(l
.size() >= 2);
542 Assert(isSorted(begin(), end()));
545 static bool isMember(Node n
);
547 bool isNormalForm() const {
551 static VarList
mkEmptyVarList() {
556 /** There are no restrictions on the size of l */
557 static VarList
mkVarList(const std::vector
<Variable
>& l
) {
559 return mkEmptyVarList();
560 } else if(l
.size() == 1) {
561 return VarList((*l
.begin()).getNode());
567 bool empty() const { return getNode().isNull(); }
568 bool singleton() const {
569 return !empty() && getNode().getKind() != kind::NONLINEAR_MULT
;
576 return getNode().getNumChildren();
579 static VarList
parseVarList(Node n
);
581 VarList
operator*(const VarList
& vl
) const;
583 int cmp(const VarList
& vl
) const;
585 bool operator<(const VarList
& vl
) const { return cmp(vl
) < 0; }
587 bool operator==(const VarList
& vl
) const { return cmp(vl
) == 0; }
589 bool isIntegral() const {
590 for(iterator i
= begin(), e
=end(); i
!= e
; ++i
){
592 if(!var
.isIntegral()){
598 size_t getComplexity() const;
601 bool isSorted(iterator start
, iterator end
);
603 };/* class VarList */
606 /** Constructors have side conditions. Use the static mkMonomial functions instead. */
607 class Monomial
: public NodeWrapper
{
611 Monomial(Node n
, const Constant
& c
, const VarList
& vl
):
612 NodeWrapper(n
), constant(c
), varList(vl
)
614 Assert(!c
.isZero() || vl
.empty());
615 Assert(c
.isZero() || !vl
.empty());
617 Assert(!c
.isOne() || !multStructured(n
));
620 static Node
makeMultNode(const Constant
& c
, const VarList
& vl
) {
624 return NodeManager::currentNM()->mkNode(kind::MULT
, c
.getNode(), vl
.getNode());
627 static bool multStructured(Node n
) {
628 return n
.getKind() == kind::MULT
&&
629 n
[0].getKind() == kind::CONST_RATIONAL
&&
630 n
.getNumChildren() == 2;
633 Monomial(const Constant
& c
):
634 NodeWrapper(c
.getNode()), constant(c
), varList(VarList::mkEmptyVarList())
637 Monomial(const VarList
& vl
):
638 NodeWrapper(vl
.getNode()), constant(Constant::mkConstant(1)), varList(vl
)
640 Assert(!varList
.empty());
643 Monomial(const Constant
& c
, const VarList
& vl
):
644 NodeWrapper(makeMultNode(c
,vl
)), constant(c
), varList(vl
)
648 Assert(!varList
.empty());
650 Assert(multStructured(getNode()));
653 static bool isMember(TNode n
);
655 /** Makes a monomial with no restrictions on c and vl. */
656 static Monomial
mkMonomial(const Constant
& c
, const VarList
& vl
);
658 /** If vl is empty, this make one. */
659 static Monomial
mkMonomial(const VarList
& vl
);
661 static Monomial
mkMonomial(const Constant
& c
){
665 static Monomial
mkMonomial(const Variable
& v
){
666 return Monomial(VarList(v
));
669 static Monomial
parseMonomial(Node n
);
671 static Monomial
mkZero() {
672 return Monomial(Constant::mkConstant(0));
674 static Monomial
mkOne() {
675 return Monomial(Constant::mkConstant(1));
677 const Constant
& getConstant() const { return constant
; }
678 const VarList
& getVarList() const { return varList
; }
680 bool isConstant() const {
681 return varList
.empty();
684 bool isZero() const {
685 return constant
.isZero();
688 bool coefficientIsOne() const {
689 return constant
.isOne();
692 bool absCoefficientIsOne() const {
693 return coefficientIsOne() || constant
.getValue() == -1;
696 bool constantIsPositive() const {
697 return getConstant().isPositive();
700 Monomial
operator*(const Rational
& q
) const;
701 Monomial
operator*(const Constant
& c
) const;
702 Monomial
operator*(const Monomial
& mono
) const;
704 Monomial
operator-() const{
705 return (*this) * Rational(-1);
709 int cmp(const Monomial
& mono
) const {
710 return getVarList().cmp(mono
.getVarList());
713 bool operator<(const Monomial
& vl
) const {
717 bool operator==(const Monomial
& vl
) const {
721 static bool isSorted(const std::vector
<Monomial
>& m
) {
722 return std::is_sorted(m
.begin(), m
.end());
725 static bool isStrictlySorted(const std::vector
<Monomial
>& m
) {
726 return isSorted(m
) && std::adjacent_find(m
.begin(),m
.end()) == m
.end();
729 static void sort(std::vector
<Monomial
>& m
);
730 static void combineAdjacentMonomials(std::vector
<Monomial
>& m
);
733 * The variable product
735 bool integralVariables() const {
736 return getVarList().isIntegral();
740 * The coefficient of the monomial is integral.
742 bool integralCoefficient() const {
743 return getConstant().isIntegral();
747 * A Monomial is an "integral" monomial if the constant is integral.
749 bool isIntegral() const {
750 return integralCoefficient() && integralVariables();
753 /** Returns true if the VarList is a product of at least 2 Variables.*/
754 bool isNonlinear() const {
755 return getVarList().size() >= 2;
759 * Given a sorted list of monomials, this function transforms this
760 * into a strictly sorted list of monomials that does not contain zero.
762 //static std::vector<Monomial> sumLikeTerms(const std::vector<Monomial>& monos);
764 int absCmp(const Monomial
& other
) const{
765 return getConstant().getValue().absCmp(other
.getConstant().getValue());
767 // bool absLessThan(const Monomial& other) const{
768 // return getConstant().abs() < other.getConstant().abs();
771 uint32_t coefficientLength() const{
772 return getConstant().length();
776 static void printList(const std::vector
<Monomial
>& list
);
778 size_t getComplexity() const;
779 };/* class Monomial */
784 class Polynomial
: public NodeWrapper
{
788 Polynomial(TNode n
) : NodeWrapper(n
), d_singleton(Monomial::isMember(n
)) {
789 Assert(isMember(getNode()));
792 static Node
makePlusNode(const std::vector
<Monomial
>& m
) {
793 Assert(m
.size() >= 2);
795 return makeNode(kind::PLUS
, m
.begin(), m
.end());
798 typedef expr::NodeSelfIterator internal_iterator
;
800 internal_iterator
internalBegin() const {
802 return expr::NodeSelfIterator::self(getNode());
804 return getNode().begin();
808 internal_iterator
internalEnd() const {
810 return expr::NodeSelfIterator::selfEnd(getNode());
812 return getNode().end();
816 bool singleton() const { return d_singleton
; }
819 static bool isMember(TNode n
);
823 internal_iterator d_iter
;
826 /* The following types are required by trait std::iterator_traits */
829 using iterator_category
= std::forward_iterator_tag
;
831 /** The type of the item */
832 using value_type
= Monomial
;
834 /** The pointer type of the item */
835 using pointer
= Monomial
*;
837 /** The reference type of the item */
838 using reference
= Monomial
&;
840 /** The type returned when two iterators are subtracted */
841 using difference_type
= std::ptrdiff_t;
843 /* End of std::iterator_traits required types */
845 explicit iterator(internal_iterator i
) : d_iter(i
) {}
847 inline Monomial
operator*() {
848 return Monomial::parseMonomial(*d_iter
);
851 bool operator==(const iterator
& i
) {
852 return d_iter
== i
.d_iter
;
855 bool operator!=(const iterator
& i
) {
856 return d_iter
!= i
.d_iter
;
859 iterator
operator++() {
864 iterator
operator++(int) {
865 return iterator(d_iter
++);
869 iterator
begin() const { return iterator(internalBegin()); }
870 iterator
end() const { return iterator(internalEnd()); }
872 Polynomial(const Monomial
& m
):
873 NodeWrapper(m
.getNode()), d_singleton(true)
876 Polynomial(const std::vector
<Monomial
>& m
):
877 NodeWrapper(makePlusNode(m
)), d_singleton(false)
879 Assert(m
.size() >= 2);
880 Assert(Monomial::isStrictlySorted(m
));
883 static Polynomial
mkPolynomial(const Constant
& c
){
884 return Polynomial(Monomial::mkMonomial(c
));
887 static Polynomial
mkPolynomial(const Variable
& v
){
888 return Polynomial(Monomial::mkMonomial(v
));
891 static Polynomial
mkPolynomial(const std::vector
<Monomial
>& m
) {
893 return Polynomial(Monomial::mkZero());
894 } else if(m
.size() == 1) {
895 return Polynomial((*m
.begin()));
897 return Polynomial(m
);
901 static Polynomial
parsePolynomial(Node n
) {
902 return Polynomial(n
);
905 static Polynomial
mkZero() {
906 return Polynomial(Monomial::mkZero());
908 static Polynomial
mkOne() {
909 return Polynomial(Monomial::mkOne());
911 bool isZero() const {
912 return singleton() && (getHead().isZero());
915 bool isConstant() const {
916 return singleton() && (getHead().isConstant());
919 bool containsConstant() const {
920 return getHead().isConstant();
923 uint32_t size() const{
927 Assert(getNode().getKind() == kind::PLUS
);
928 return getNode().getNumChildren();
932 Monomial
getHead() const {
936 Polynomial
getTail() const {
937 Assert(!singleton());
939 iterator tailStart
= begin();
941 std::vector
<Monomial
> subrange
;
942 std::copy(tailStart
, end(), std::back_inserter(subrange
));
943 return mkPolynomial(subrange
);
946 Monomial
minimumVariableMonomial() const;
947 bool variableMonomialAreStrictlyGreater(const Monomial
& m
) const;
949 void printList() const {
950 if(Debug
.isOn("normal-form")){
951 Debug("normal-form") << "start list" << std::endl
;
952 for(iterator i
= begin(), oend
= end(); i
!= oend
; ++i
) {
953 const Monomial
& m
=*i
;
956 Debug("normal-form") << "end list" << std::endl
;
960 /** A Polynomial is an "integral" polynomial if all of the monomials are integral. */
961 bool allIntegralVariables() const {
962 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
963 if(!(*i
).integralVariables()){
971 * A Polynomial is an "integral" polynomial if all of the monomials are integral
972 * and all of the coefficients are Integral. */
973 bool isIntegral() const {
974 for(iterator i
= begin(), e
=end(); i
!=e
; ++i
){
975 if(!(*i
).isIntegral()){
982 static Polynomial
sumPolynomials(const std::vector
<Polynomial
>& polynomials
);
984 /** Returns true if the polynomial contains a non-linear monomial.*/
985 bool isNonlinear() const;
987 /** Check whether this polynomial is only a single variable. */
988 bool isVariable() const
990 return singleton() && getHead().getVarList().singleton()
991 && getHead().coefficientIsOne();
993 /** Return the variable, given that isVariable() holds. */
994 Variable
getVariable() const
996 Assert(isVariable());
997 return getHead().getVarList().getHead();
1001 * Selects a minimal monomial in the polynomial by the absolute value of
1004 Monomial
selectAbsMinimum() const;
1006 /** Returns true if the absolute value of the head coefficient is one. */
1007 bool leadingCoefficientIsAbsOne() const;
1008 bool leadingCoefficientIsPositive() const;
1009 bool denominatorLCMIsOne() const;
1010 bool numeratorGCDIsOne() const;
1012 bool signNormalizedReducedSum() const {
1013 return leadingCoefficientIsPositive() && denominatorLCMIsOne() && numeratorGCDIsOne();
1017 * Returns the Least Common Multiple of the denominators of the coefficients
1020 Integer
denominatorLCM() const;
1023 * Returns the GCD of the numerators of the monomials.
1024 * Requires this to be an isIntegral() polynomial.
1026 Integer
numeratorGCD() const;
1029 * Returns the GCD of the coefficients of the monomials.
1030 * Requires this to be an isIntegral() polynomial.
1032 Integer
gcd() const;
1034 /** z must divide all of the coefficients of the polynomial. */
1035 Polynomial
exactDivide(const Integer
& z
) const;
1037 Polynomial
operator+(const Polynomial
& vl
) const;
1038 Polynomial
operator-(const Polynomial
& vl
) const;
1039 Polynomial
operator-() const{
1040 return (*this) * Rational(-1);
1043 Polynomial
operator*(const Rational
& q
) const;
1044 Polynomial
operator*(const Constant
& c
) const;
1045 Polynomial
operator*(const Monomial
& mono
) const;
1047 Polynomial
operator*(const Polynomial
& poly
) const;
1050 * Viewing the integer polynomial as a list [(* coeff_i mono_i)]
1051 * The quotient and remainder of p divided by the non-zero integer z is:
1052 * q := [(* floor(coeff_i/z) mono_i )]
1053 * r := [(* rem(coeff_i/z) mono_i)]
1054 * computeQR(p,z) returns the node (+ q r).
1056 * q and r are members of the Polynomial class.
1058 * computeQR( p = (+ 5 (* 3 x) (* 8 y)) , z = 2) returns
1059 * (+ (+ 2 x (* 4 y)) (+ 1 x))
1061 static Node
computeQR(const Polynomial
& p
, const Integer
& z
);
1063 /** Returns the coefficient associated with the VarList in the polynomial. */
1064 Constant
getCoefficient(const VarList
& vl
) const;
1066 uint32_t maxLength() const{
1067 iterator i
= begin(), e
=end();
1071 uint32_t max
= (*i
).coefficientLength();
1074 uint32_t curr
= (*i
).coefficientLength();
1083 uint32_t numMonomials() const {
1084 if( getNode().getKind() == kind::PLUS
){
1085 return getNode().getNumChildren();
1093 const Rational
& asConstant() const{
1094 Assert(isConstant());
1095 return getNode().getConst
<Rational
>();
1096 //return getHead().getConstant().getValue();
1099 bool isVarList() const {
1101 return VarList::isMember(getNode());
1107 VarList
asVarList() const {
1108 Assert(isVarList());
1109 return getHead().getVarList();
1112 size_t getComplexity() const;
1114 friend class SumPair
;
1115 friend class Comparison
;
1117 /** Returns a node that if asserted ensures v is the abs of this polynomial.*/
1118 Node
makeAbsCondition(Variable v
){
1119 return makeAbsCondition(v
, *this);
1122 /** Returns a node that if asserted ensures v is the abs of p.*/
1123 static Node
makeAbsCondition(Variable v
, Polynomial p
);
1125 };/* class Polynomial */
1129 * SumPair is a utility class that extends polynomials for use in computations.
1130 * A SumPair is always a combination of (+ p c) where
1131 * c is a constant and p is a polynomial such that p = 0 or !p.containsConstant().
1133 * These are a useful utility for representing the equation p = c as (+ p -c) where the pair
1134 * is known to implicitly be equal to 0.
1136 * SumPairs do not have unique representations due to the potential for p = 0.
1137 * This makes them inappropriate for normal forms.
1139 class SumPair
: public NodeWrapper
{
1141 static Node
toNode(const Polynomial
& p
, const Constant
& c
){
1142 return NodeManager::currentNM()->mkNode(kind::PLUS
, p
.getNode(), c
.getNode());
1145 SumPair(TNode n
) : NodeWrapper(n
) { Assert(isNormalForm()); }
1148 SumPair(const Polynomial
& p
):
1149 NodeWrapper(toNode(p
, Constant::mkConstant(0)))
1151 Assert(isNormalForm());
1154 SumPair(const Polynomial
& p
, const Constant
& c
):
1155 NodeWrapper(toNode(p
, c
))
1157 Assert(isNormalForm());
1160 static bool isMember(TNode n
) {
1161 if(n
.getKind() == kind::PLUS
&& n
.getNumChildren() == 2){
1162 if(Constant::isMember(n
[1])){
1163 if(Polynomial::isMember(n
[0])){
1164 Polynomial p
= Polynomial::parsePolynomial(n
[0]);
1165 return p
.isZero() || (!p
.containsConstant());
1177 bool isNormalForm() const {
1178 return isMember(getNode());
1181 Polynomial
getPolynomial() const {
1182 return Polynomial::parsePolynomial(getNode()[0]);
1185 Constant
getConstant() const {
1186 return Constant::mkConstant((getNode())[1]);
1189 SumPair
operator+(const SumPair
& other
) const {
1190 return SumPair(getPolynomial() + other
.getPolynomial(),
1191 getConstant() + other
.getConstant());
1194 SumPair
operator*(const Constant
& c
) const {
1195 return SumPair(getPolynomial() * c
, getConstant() * c
);
1198 SumPair
operator-(const SumPair
& other
) const {
1199 return (*this) + (other
* Constant::mkConstant(-1));
1202 static SumPair
mkSumPair(const Polynomial
& p
);
1204 static SumPair
mkSumPair(const Variable
& var
){
1205 return SumPair(Polynomial::mkPolynomial(var
));
1208 static SumPair
parseSumPair(TNode n
){
1212 bool isIntegral() const{
1213 return getConstant().isIntegral() && getPolynomial().isIntegral();
1216 bool isConstant() const {
1217 return getPolynomial().isZero();
1220 bool isZero() const {
1221 return getConstant().isZero() && isConstant();
1224 uint32_t size() const{
1225 return getPolynomial().size();
1228 bool isNonlinear() const{
1229 return getPolynomial().isNonlinear();
1233 * Returns the greatest common divisor of gcd(getPolynomial()) and getConstant().
1234 * The SumPair must be integral.
1236 Integer
gcd() const {
1237 Assert(isIntegral());
1238 return (getPolynomial().gcd()).gcd(getConstant().getValue().getNumerator());
1241 uint32_t maxLength() const {
1242 Assert(isIntegral());
1243 return std::max(getPolynomial().maxLength(), getConstant().length());
1246 static SumPair
mkZero() {
1247 return SumPair(Polynomial::mkZero(), Constant::mkConstant(0));
1250 static Node
computeQR(const SumPair
& sp
, const Integer
& div
);
1252 };/* class SumPair */
1254 /* class OrderedPolynomialPair { */
1256 /* Polynomial d_first; */
1257 /* Polynomial d_second; */
1259 /* OrderedPolynomialPair(const Polynomial& f, const Polynomial& s) */
1264 /* /\** Returns the first part of the pair. *\/ */
1265 /* const Polynomial& getFirst() const { */
1266 /* return d_first; */
1269 /* /\** Returns the second part of the pair. *\/ */
1270 /* const Polynomial& getSecond() const { */
1271 /* return d_second; */
1274 /* OrderedPolynomialPair operator*(const Constant& c) const; */
1275 /* OrderedPolynomialPair operator+(const Polynomial& p) const; */
1277 /* /\** Returns true if both of the polynomials are constant. *\/ */
1278 /* bool isConstant() const; */
1281 /* * Evaluates an isConstant() ordered pair as if */
1282 /* * (k getFirst() getRight()) */
1284 /* bool evaluateConstant(Kind k) const; */
1287 /* * Returns the Least Common Multiple of the monomials */
1288 /* * on the lefthand side and the constant on the right. */
1290 /* Integer denominatorLCM() const; */
1292 /* /\** Constructs a SumPair. *\/ */
1293 /* SumPair toSumPair() const; */
1296 /* OrderedPolynomialPair divideByGCD() const; */
1297 /* OrderedPolynomialPair multiplyConstant(const Constant& c) const; */
1300 /* * Returns true if all of the variables are integers, */
1301 /* * and the coefficients are integers. */
1303 /* bool isIntegral() const; */
1305 /* /\** Returns true if all of the variables are integers. *\/ */
1306 /* bool allIntegralVariables() const { */
1307 /* return getFirst().allIntegralVariables() && getSecond().allIntegralVariables(); */
1311 class Comparison
: public NodeWrapper
{
1314 static Node
toNode(Kind k
, const Polynomial
& l
, const Constant
& c
);
1315 static Node
toNode(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1317 Comparison(TNode n
);
1320 * Creates a node in normal form equivalent to (= l 0).
1321 * All variables in l are integral.
1323 static Node
mkIntEquality(const Polynomial
& l
);
1326 * Creates a comparison equivalent to (k l 0).
1327 * k is either GT or GEQ.
1328 * All variables in l are integral.
1330 static Node
mkIntInequality(Kind k
, const Polynomial
& l
);
1333 * Creates a node equivalent to (= l 0).
1334 * It is not the case that all variables in l are integral.
1336 static Node
mkRatEquality(const Polynomial
& l
);
1339 * Creates a comparison equivalent to (k l 0).
1340 * k is either GT or GEQ.
1341 * It is not the case that all variables in l are integral.
1343 static Node
mkRatInequality(Kind k
, const Polynomial
& l
);
1347 Comparison(bool val
) :
1348 NodeWrapper(NodeManager::currentNM()->mkConst(val
))
1352 * Given a literal to TheoryArith return a single kind to
1353 * to indicate its underlying structure.
1354 * The function returns the following in each case:
1355 * - (K left right) -> K where is either EQUAL, GT, or GEQ
1356 * - (CONST_BOOLEAN b) -> CONST_BOOLEAN
1357 * - (NOT (EQUAL left right)) -> DISTINCT
1358 * - (NOT (GT left right)) -> LEQ
1359 * - (NOT (GEQ left right)) -> LT
1360 * If none of these match, it returns UNDEFINED_KIND.
1362 static Kind
comparisonKind(TNode literal
);
1364 Kind
comparisonKind() const { return comparisonKind(getNode()); }
1366 static Comparison
mkComparison(Kind k
, const Polynomial
& l
, const Polynomial
& r
);
1368 /** Returns true if the comparison is a boolean constant. */
1369 bool isBoolean() const;
1372 * Returns true if the comparison is either a boolean term,
1373 * in integer normal form or mixed normal form.
1375 bool isNormalForm() const;
1378 bool isNormalGT() const;
1379 bool isNormalGEQ() const;
1381 bool isNormalLT() const;
1382 bool isNormalLEQ() const;
1384 bool isNormalEquality() const;
1385 bool isNormalDistinct() const;
1386 bool isNormalEqualityOrDisequality() const;
1388 bool allIntegralVariables() const {
1389 return getLeft().allIntegralVariables() && getRight().allIntegralVariables();
1391 bool rightIsConstant() const;
1394 Polynomial
getLeft() const;
1395 Polynomial
getRight() const;
1397 /* /\** Normal form check if at least one variable is real. *\/ */
1398 /* bool isMixedCompareNormalForm() const; */
1400 /* /\** Normal form check if at least one variable is real. *\/ */
1401 /* bool isMixedEqualsNormalForm() const; */
1403 /* /\** Normal form check is all variables are integer.*\/ */
1404 /* bool isIntegerCompareNormalForm() const; */
1406 /* /\** Normal form check is all variables are integer.*\/ */
1407 /* bool isIntegerEqualsNormalForm() const; */
1411 * Returns true if all of the variables are integers, the coefficients are integers,
1412 * and the right hand coefficient is an integer.
1414 bool debugIsIntegral() const;
1416 static Comparison
parseNormalForm(TNode n
);
1418 inline static bool isNormalAtom(TNode n
){
1419 Comparison parse
= Comparison::parseNormalForm(n
);
1420 return parse
.isNormalForm();
1423 size_t getComplexity() const;
1425 SumPair
toSumPair() const;
1427 Polynomial
normalizedVariablePart() const;
1428 DeltaRational
normalizedDeltaRational() const;
1431 * Transforms a Comparison object into a stronger normal form:
1432 * Polynomial ~Kind~ Constant
1434 * From the comparison, this method resolved a negation (if present) and
1435 * moves everything to the left side.
1436 * If split_constant is false, the constant is always zero.
1437 * If split_constant is true, the polynomial has no constant term and is
1438 * normalized to have leading coefficient one.
1440 std::tuple
<Polynomial
, Kind
, Constant
> decompose(
1441 bool split_constant
= false) const;
1443 };/* class Comparison */
1445 } // namespace arith
1446 } // namespace theory
1449 #endif /* CVC5__THEORY__ARITH__NORMAL_FORM_H */