carl  24.04
Computer ARithmetic Library
Relation.h
Go to the documentation of this file.
1 /**
2  * @file Relation.h
3  * @ingroup constraints
4  * @author Sebastian Junges
5  */
6 
7 #pragma once
8 
10 
11 #include "Sign.h"
12 
13 #include <cassert>
14 #include <iostream>
15 #include <memory>
16 #include <sstream>
17 
18 namespace carl {
19 
20 enum class Relation { EQ = 0, NEQ = 1, LESS = 2, LEQ = 4, GREATER = 3, GEQ = 5 };
21 
22 inline std::ostream& operator<<(std::ostream& os, const Relation& r) {
23  switch (r) {
24  case Relation::EQ: os << "="; break;
25  case Relation::NEQ: os << "!="; break;
26  case Relation::LESS: os << "<"; break;
27  case Relation::LEQ: os << "<="; break;
28  case Relation::GREATER: os << ">"; break;
29  case Relation::GEQ: os << ">="; break;
30  default:
31  CARL_LOG_ERROR("carl.relation", "Invalid relation " << std::underlying_type_t<Relation>(r));
32  assert(false && "Invalid relation");
33  }
34  return os;
35 }
36 
37 /**
38  * Inverts the given relation symbol.
39  */
41  switch (r) {
42  case Relation::EQ: return Relation::NEQ;
43  case Relation::NEQ: return Relation::EQ;
44  case Relation::LESS: return Relation::GEQ;
45  case Relation::LEQ: return Relation::GREATER;
46  case Relation::GREATER: return Relation::LEQ;
47  case Relation::GEQ: return Relation::LESS;
48  default:
49  CARL_LOG_ERROR("carl.relation", "Invalid relation " << std::underlying_type_t<Relation>(r));
50  assert(false && "Invalid relation");
51  }
52  return Relation::EQ;
53 }
54 
55 /**
56  * Turns around the given relation symbol, in the sense that LESS (LEQ) and GREATER (GEQ) are swapped.
57  */
59  switch (r) {
60  case Relation::EQ: return Relation::EQ;
61  case Relation::NEQ: return Relation::NEQ;
62  case Relation::LESS: return Relation::GREATER;
63  case Relation::LEQ: return Relation::GEQ;
64  case Relation::GREATER: return Relation::LESS;
65  case Relation::GEQ: return Relation::LEQ;
66  default:
67  CARL_LOG_ERROR("carl.relation", "Invalid relation " << std::underlying_type_t<Relation>(r));
68  assert(false && "Invalid relation");
69  }
70  return Relation::EQ;
71 }
72 
73 inline std::string toString(Relation r) {
74  std::stringstream ss;
75  ss << r;
76  return ss.str();
77 }
78 
79 inline bool is_strict(Relation r) {
80  return r == Relation::LESS || r == Relation::GREATER || r == Relation::NEQ;
81 }
82 inline bool is_weak(Relation r) {
83  return !is_strict(r);
84 }
85 
86 inline bool evaluate(Sign s, Relation r) {
87  switch (s) {
88  case Sign::NEGATIVE:
89  return r == Relation::NEQ || r == Relation::LESS || r == Relation::LEQ;
90  case Sign::ZERO:
91  return r == Relation::EQ || r == Relation::LEQ || r == Relation::GEQ;
92  case Sign::POSITIVE:
93  return r == Relation::NEQ || r == Relation::GREATER || r == Relation::GEQ;
94  default:
95  CARL_LOG_ERROR("carl.relation", "Evaluating invalid sign " << std::underlying_type_t<Sign>(s));
96  assert(false && "Invalid sign");
97  }
98  return false;
99 }
100 
101 template<typename T>
102 inline bool evaluate(const T& t, Relation r) {
103  return evaluate(sgn(t), r);
104 }
105 
106 template<typename T1, typename T2>
107 inline bool evaluate(const T1& lhs, Relation r, const T2& rhs) {
108  switch (r) {
109  case Relation::EQ:
110  return (lhs == rhs);
111  break;
112  case Relation::NEQ:
113  return (lhs != rhs) ;
114  break;
115  case Relation::LESS:
116  return (lhs < rhs);
117  break;
118  case Relation::LEQ:
119  return (lhs <= rhs);
120  break;
121  case Relation::GREATER:
122  return (lhs > rhs);
123  break;
124  case Relation::GEQ:
125  return (lhs >= rhs);
126  break;
127  }
128  assert(false);
129  return false;
130 }
131 
132 }
133 
134 namespace std {
135 
136 template<>
137 struct hash<carl::Relation> {
138  std::size_t operator()(const carl::Relation& rel) const {
139  return std::size_t(rel);
140  }
141 };
142 
143 }
A small wrapper that configures logging for carl.
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
carl is the main namespace for the library.
std::string toString(Relation r)
Definition: Relation.h:73
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.
Sign
This class represents the sign of a number .
Definition: Sign.h:20
@ NEGATIVE
Indicates that .
@ ZERO
Indicates that .
@ POSITIVE
Indicates that .
bool evaluate(const BasicConstraint< Poly > &c, const Assignment< Number > &m)
Definition: Evaluation.h:10
bool is_strict(Relation r)
Definition: Relation.h:79
Sign sgn(const Number &n)
Obtain the sign of the given number.
Definition: Sign.h:54
Relation inverse(Relation r)
Inverts the given relation symbol.
Definition: Relation.h:40
Relation turn_around(Relation r)
Turns around the given relation symbol, in the sense that LESS (LEQ) and GREATER (GEQ) are swapped.
Definition: Relation.h:58
@ GREATER
Definition: SignCondition.h:15
Relation
Definition: Relation.h:20
std::size_t operator()(const carl::Relation &rel) const
Definition: Relation.h:138