carl  24.04
Computer ARithmetic Library
BasicConstraint.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 namespace carl {
7 
8 #define CONSTRAINT_HASH(_lhs, _rel, _type) (size_t)((size_t)(std::hash<_type>()(_lhs) << 3) ^ (size_t)_rel)
9 
10 /**
11  * Represent a polynomial (in)equality against zero. Such an (in)equality
12  * can be seen as an atomic formula/atom for the theory of real arithmetic.
13  */
14 template<typename Pol>
16  /// The polynomial which is compared by this constraint to zero.
18  /// The relation symbol comparing the polynomial considered by this constraint to zero.
20  /// Cache for the hash.
21  std::size_t m_hash;
22 
23 public:
24  BasicConstraint(bool is_true) : m_lhs(typename Pol::NumberType(!is_true)), m_relation(Relation::EQ), m_hash(CONSTRAINT_HASH(m_lhs, m_relation, Pol)) {}
25 
27 
29 
30  /**
31  * @return The considered polynomial being the left-hand side of this constraint. Hence, the right-hand side of any constraint is always 0.
32  */
33  const Pol& lhs() const {
34  return m_lhs;
35  }
36 
37  /**
38  * @return The considered polynomial being the left-hand side of this constraint. Hence, the right-hand side of any constraint is always 0.
39  */
40  void set_lhs(Pol&& lhs) {
41  m_lhs = std::move(lhs);
43  }
44 
45  /**
46  * @return The relation symbol of this constraint.
47  */
48  Relation relation() const {
49  return m_relation;
50  }
51 
52  /**
53  * @return The relation symbol of this constraint.
54  */
55  void set_relation(Relation rel) {
56  m_relation = rel;
58  }
59 
60  /**
61  * @return A hash value for this constraint.
62  */
63  size_t hash() const {
64  return m_hash;
65  }
66 
67  bool is_trivial_true() const {
68  if(is_constant(m_lhs)) {
69  if(m_lhs.constant_part() == 0) {
70  return is_weak(m_relation);
71  } else if(m_lhs.constant_part() > 0) {
73  } else {
74  assert(m_lhs.constant_part() < 0);
76  }
77  }
78  return false;
79  }
80 
81  bool is_trivial_false() const {
82  if(is_constant(m_lhs)) {
83  if(m_lhs.constant_part() == 0) {
84  return is_strict(m_relation);
85  } else if(m_lhs.constant_part() > 0) {
87  } else {
88  assert(m_lhs.constant_part() < 0);
90  }
91  }
92  return false;
93  }
94 
95  unsigned is_consistent() const {
96  if (is_trivial_false()) return 0;
97  else if (is_trivial_true()) return 1;
98  else return 2;
99  }
100 
103  }
104 };
105 
106 template<typename P>
107 bool operator==(const BasicConstraint<P>& lhs, const BasicConstraint<P>& rhs) {
108  return lhs.hash() == rhs.hash() && lhs.relation() == rhs.relation() && lhs.lhs() == rhs.lhs();
109 }
110 
111 template<typename P>
112 bool operator!=(const BasicConstraint<P>& lhs, const BasicConstraint<P>& rhs) {
113  return !(lhs == rhs);
114 }
115 
116 template<typename P>
117 bool operator<(const BasicConstraint<P>& lhs, const BasicConstraint<P>& rhs) {
118  return lhs.hash() < rhs.hash() || (lhs.hash() == rhs.hash() && (lhs.relation() < rhs.relation() || (lhs.relation() == rhs.relation() && lhs.lhs() < rhs.lhs())));
119  // return lhs.lhs() < rhs.lhs() || (lhs.lhs() == rhs.lhs() && lhs.relation() < rhs.relation());
120 }
121 
122 template<typename P>
123 bool operator<=(const BasicConstraint<P>& lhs, const BasicConstraint<P>& rhs) {
124  return lhs == rhs || lhs < rhs;
125 }
126 
127 template<typename P>
128 bool operator>(const BasicConstraint<P>& lhs, const BasicConstraint<P>& rhs) {
129  return rhs < lhs;
130 }
131 
132 template<typename P>
133 bool operator>=(const BasicConstraint<P>& lhs, const BasicConstraint<P>& rhs) {
134  return rhs <= lhs;
135 }
136 
137 
138 template<typename Pol>
140  variables(c.lhs(), vars);
141 }
142 
143 /**
144  * Prints the given constraint on the given stream.
145  * @param os The stream to print the given constraint on.
146  * @param c The formula to print.
147  * @return The stream after printing the given constraint on it.
148  */
149 template<typename Poly>
150 std::ostream& operator<<(std::ostream& os, const BasicConstraint<Poly>& c) {
151  return os << c.lhs() << " " << c.relation() << " 0";
152 }
153 
154 } // namespace carl
155 
156 namespace std {
157 /**
158  * Implements std::hash for constraints.
159  */
160 template<typename Pol>
161 struct hash<carl::BasicConstraint<Pol>> {
162  /**
163  * @param constraint The constraint to get the hash for.
164  * @return The hash of the given constraint.
165  */
166  std::size_t operator()(const carl::BasicConstraint<Pol>& constraint) const {
167  return constraint.hash();
168  }
169 };
170 
171 /**
172  * Implements std::hash for vectors of constraints.
173  */
174 template<typename Pol>
175 struct hash<std::vector<carl::BasicConstraint<Pol>>> {
176  /**
177  * @param _arg The vector of constraints to get the hash for.
178  * @return The hash of the given vector of constraints.
179  */
180  std::size_t operator()(const std::vector<carl::BasicConstraint<Pol>>& arg) const {
181  std::size_t seed = arg.size();
182  for(auto& c : arg) {
183  seed ^= std::hash<carl::BasicConstraint<Pol>>()(c) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
184  }
185  return seed;
186  }
187 };
188 
189 } // namespace std
#define CONSTRAINT_HASH(_lhs, _rel, _type)
MultivariatePolynomial< Rational > Pol
Definition: HornerTest.cpp:17
carl is the main namespace for the library.
bool operator>(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator<(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool is_constant(const ContextPolynomial< Coeff, Ordering, Policies > &p)
bool is_weak(Relation r)
Definition: Relation.h:82
std::ostream & operator<<(std::ostream &os, const BasicConstraint< Poly > &c)
Prints the given constraint on the given stream.
bool is_strict(Relation r)
Definition: Relation.h:79
bool operator!=(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator<=(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
Relation inverse(Relation r)
Inverts the given relation symbol.
Definition: Relation.h:40
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
bool operator>=(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
@ GREATER
Definition: SignCondition.h:15
Relation
Definition: Relation.h:20
void variables(const BasicConstraint< Pol > &c, carlVariables &vars)
Represent a polynomial (in)equality against zero.
BasicConstraint(const Pol &lhs, const Relation rel)
void set_relation(Relation rel)
bool is_trivial_true() const
Pol m_lhs
The polynomial which is compared by this constraint to zero.
std::size_t m_hash
Cache for the hash.
const Pol & lhs() const
Relation m_relation
The relation symbol comparing the polynomial considered by this constraint to zero.
BasicConstraint(bool is_true)
BasicConstraint< Pol > negation() const
BasicConstraint(Pol &&lhs, const Relation rel)
bool is_trivial_false() const
unsigned is_consistent() const
Relation relation() const
void set_lhs(Pol &&lhs)
std::size_t operator()(const carl::BasicConstraint< Pol > &constraint) const
std::size_t operator()(const std::vector< carl::BasicConstraint< Pol >> &arg) const