logic.h
Go to the documentation of this file.
1 
6 #ifndef SYMENGINE_LOGIC_H
7 #define SYMENGINE_LOGIC_H
8 
9 #include <symengine/basic.h>
10 #include <symengine/sets.h>
11 
12 namespace SymEngine
13 {
14 typedef std::set<RCP<const Boolean>, RCPBasicKeyLess> set_boolean;
15 typedef std::vector<RCP<const Boolean>> vec_boolean;
16 
17 // Parent class for expressing boolean statements
18 class Boolean : public Basic
19 {
20 public:
21  virtual RCP<const Boolean> logical_not() const;
22 };
23 
24 // Booleans True and False
25 class BooleanAtom : public Boolean
26 {
27 private:
28  bool b_;
29 
30 public:
31  IMPLEMENT_TYPEID(SYMENGINE_BOOLEAN_ATOM)
32  BooleanAtom(bool b);
34  hash_t __hash__() const override;
35  bool get_val() const;
36  vec_basic get_args() const override;
37  bool __eq__(const Basic &o) const override;
39  int compare(const Basic &o) const override;
40  RCP<const Boolean> logical_not() const override;
41 };
42 
43 extern SYMENGINE_EXPORT RCP<const BooleanAtom> &boolTrue;
44 extern SYMENGINE_EXPORT RCP<const BooleanAtom> &boolFalse;
45 
46 inline RCP<const BooleanAtom> boolean(bool b)
47 {
48  return b ? boolTrue : boolFalse;
49 }
50 
51 // Represents `expr_` is inside set `set_`
52 // `set_` can be any `Set` including `Interval`, `FiniteSet`
53 class Contains : public Boolean
54 {
55 private:
56  RCP<const Basic> expr_;
57  RCP<const Set> set_;
58 
59 public:
60  IMPLEMENT_TYPEID(SYMENGINE_CONTAINS)
62  Contains(const RCP<const Basic> &expr, const RCP<const Set> &set);
63  hash_t __hash__() const override;
64  RCP<const Basic> get_expr() const;
65  RCP<const Set> get_set() const;
66  vec_basic get_args() const override;
67  bool __eq__(const Basic &o) const override;
68  RCP<const Basic> create(const RCP<const Basic> &lhs,
69  const RCP<const Set> &rhs) const;
71  int compare(const Basic &o) const override;
72 };
73 
74 RCP<const Boolean> contains(const RCP<const Basic> &expr,
75  const RCP<const Set> &set);
76 
77 typedef std::vector<std::pair<RCP<const Basic>, RCP<const Boolean>>>
79 
80 // Represents a piecewise function
81 // Keeps a vector of (Expr, Condition) pairs
82 class Piecewise : public Basic
83 {
84 private:
85  PiecewiseVec vec_;
86 
87 public:
88  IMPLEMENT_TYPEID(SYMENGINE_PIECEWISE)
90  Piecewise(PiecewiseVec &&vec);
91  bool is_canonical(const PiecewiseVec &vec);
92  hash_t __hash__() const override;
93  const PiecewiseVec &get_vec() const;
94  vec_basic get_args() const override;
95  bool __eq__(const Basic &o) const override;
97  int compare(const Basic &o) const override;
98 };
99 
100 // Vec is vector of pairs of RCP<const Basic> and RCP<const Boolean> to
101 // represent (Expr, Condition) pairs
102 RCP<const Basic> piecewise(const PiecewiseVec &vec);
103 
104 class And : public Boolean
105 {
106 private:
107  set_boolean container_;
108 
109 public:
110  IMPLEMENT_TYPEID(SYMENGINE_AND)
111  And(const set_boolean &s);
112  bool is_canonical(const set_boolean &container_);
114  hash_t __hash__() const override;
115  vec_basic get_args() const override;
116  RCP<const Basic> create(const set_boolean &a) const;
117  bool __eq__(const Basic &o) const override;
119  int compare(const Basic &o) const override;
120  const set_boolean &get_container() const;
121  RCP<const Boolean> logical_not() const override;
122 };
123 
124 class Or : public Boolean
125 {
126 private:
127  set_boolean container_;
128 
129 public:
130  IMPLEMENT_TYPEID(SYMENGINE_OR)
131  Or(const set_boolean &s);
132  bool is_canonical(const set_boolean &container_);
134  hash_t __hash__() const override;
135  vec_basic get_args() const override;
136  bool __eq__(const Basic &o) const override;
138  int compare(const Basic &o) const override;
139  const set_boolean &get_container() const;
140  RCP<const Boolean> logical_not() const override;
141 };
142 
143 class Not : public Boolean
144 {
145 private:
146  RCP<const Boolean> arg_;
147 
148 public:
149  IMPLEMENT_TYPEID(SYMENGINE_NOT)
150  Not(const RCP<const Boolean> &s);
151  bool is_canonical(const RCP<const Boolean> &s);
153  hash_t __hash__() const override;
154  vec_basic get_args() const override;
155  bool __eq__(const Basic &o) const override;
157  int compare(const Basic &o) const override;
158  RCP<const Boolean> get_arg() const;
159  RCP<const Boolean> logical_not() const override;
160 };
161 
162 class Xor : public Boolean
163 {
164 private:
165  vec_boolean container_;
166 
167 public:
168  IMPLEMENT_TYPEID(SYMENGINE_XOR)
169  Xor(const vec_boolean &s);
170  bool is_canonical(const vec_boolean &container_);
171  hash_t __hash__() const override;
172  vec_basic get_args() const override;
173  bool __eq__(const Basic &o) const override;
174  int compare(const Basic &o) const override;
175  const vec_boolean &get_container() const;
176 };
177 
178 class Relational : public TwoArgBasic<Boolean>
179 {
180 public:
182  Relational(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
184  virtual bool is_canonical(const RCP<const Basic> &lhs,
185  const RCP<const Basic> &rhs) const;
186 };
187 
188 class Equality : public Relational
189 {
191 public:
192  IMPLEMENT_TYPEID(SYMENGINE_EQUALITY)
193  Equality(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
194  RCP<const Basic> create(const RCP<const Basic> &lhs,
195  const RCP<const Basic> &rhs) const override;
196  RCP<const Boolean> logical_not() const override;
197 };
198 
199 class Unequality : public Relational
200 {
202 public:
203  IMPLEMENT_TYPEID(SYMENGINE_UNEQUALITY)
204  Unequality(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
205  RCP<const Basic> create(const RCP<const Basic> &lhs,
206  const RCP<const Basic> &rhs) const override;
207  RCP<const Boolean> logical_not() const override;
208 };
209 
210 class LessThan : public Relational
211 {
213 public:
214  IMPLEMENT_TYPEID(SYMENGINE_LESSTHAN)
215  LessThan(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
216  RCP<const Basic> create(const RCP<const Basic> &lhs,
217  const RCP<const Basic> &rhs) const override;
218  RCP<const Boolean> logical_not() const override;
219 };
220 
222 {
224 public:
225  IMPLEMENT_TYPEID(SYMENGINE_STRICTLESSTHAN)
226  StrictLessThan(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
227  RCP<const Basic> create(const RCP<const Basic> &lhs,
228  const RCP<const Basic> &rhs) const override;
229  RCP<const Boolean> logical_not() const override;
230 };
231 
232 inline bool is_a_Relational(const Basic &b)
233 {
234  return (b.get_type_code() == SYMENGINE_EQUALITY
235  || b.get_type_code() == SYMENGINE_UNEQUALITY
236  || b.get_type_code() == SYMENGINE_LESSTHAN
237  || b.get_type_code() == SYMENGINE_STRICTLESSTHAN);
238 }
239 
240 inline bool is_a_Boolean(const Basic &b)
241 {
242  return (b.get_type_code() == SYMENGINE_BOOLEAN_ATOM
243  || b.get_type_code() == SYMENGINE_CONTAINS
244  || b.get_type_code() == SYMENGINE_AND
245  || b.get_type_code() == SYMENGINE_OR
246  || b.get_type_code() == SYMENGINE_NOT
247  || b.get_type_code() == SYMENGINE_XOR || is_a_Relational(b));
248 }
249 
251 RCP<const Boolean> Eq(const RCP<const Basic> &lhs);
253 RCP<const Boolean> Eq(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
255 RCP<const Boolean> Ne(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
257 RCP<const Boolean> Ge(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
259 RCP<const Boolean> Gt(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
261 RCP<const Boolean> Le(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
263 RCP<const Boolean> Lt(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
264 
265 RCP<const Boolean> logical_and(const set_boolean &s);
266 RCP<const Boolean> logical_nand(const set_boolean &s);
267 RCP<const Boolean> logical_or(const set_boolean &s);
268 RCP<const Boolean> logical_not(const RCP<const Boolean> &s);
269 RCP<const Boolean> logical_nor(const set_boolean &s);
270 RCP<const Boolean> logical_xor(const vec_boolean &s);
271 RCP<const Boolean> logical_xnor(const vec_boolean &s);
272 } // namespace SymEngine
273 
274 #endif
The base class for SymEngine.
#define IMPLEMENT_TYPEID(SYMENGINE_ID)
Inline members and functions.
Definition: basic.h:340
hash_t __hash__() const override
Definition: logic.cpp:215
bool __eq__(const Basic &o) const override
Test equality.
Definition: logic.cpp:229
int compare(const Basic &o) const override
Structural equality comparator.
Definition: logic.cpp:236
vec_basic get_args() const override
Returns the list of arguments.
Definition: logic.cpp:223
The lowest unit of symbolic representation.
Definition: basic.h:97
vec_basic get_args() const override
Returns the list of arguments.
Definition: logic.cpp:26
hash_t __hash__() const override
Definition: logic.cpp:14
bool __eq__(const Basic &o) const override
Test equality.
Definition: logic.cpp:31
int compare(const Basic &o) const override
Structural equality comparator.
Definition: logic.cpp:37
bool __eq__(const Basic &o) const override
Test equality.
Definition: logic.cpp:82
int compare(const Basic &o) const override
Structural equality comparator.
Definition: logic.cpp:89
vec_basic get_args() const override
Returns the list of arguments.
Definition: logic.cpp:74
hash_t __hash__() const override
Definition: logic.cpp:56
Contains(const RCP< const Basic > &expr, const RCP< const Set > &set)
Constructor.
Definition: logic.cpp:53
RCP< const Basic > create(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const override
Method to construct classes with canonicalization.
Definition: logic.cpp:631
RCP< const Basic > create(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const override
Method to construct classes with canonicalization.
Definition: logic.cpp:700
vec_basic get_args() const override
Returns the list of arguments.
Definition: logic.cpp:352
int compare(const Basic &o) const override
Structural equality comparator.
Definition: logic.cpp:364
hash_t __hash__() const override
Definition: logic.cpp:345
bool __eq__(const Basic &o) const override
Test equality.
Definition: logic.cpp:359
vec_basic get_args() const override
Returns the list of arguments.
Definition: logic.cpp:291
hash_t __hash__() const override
Definition: logic.cpp:283
bool __eq__(const Basic &o) const override
Test equality.
Definition: logic.cpp:297
int compare(const Basic &o) const override
Structural equality comparator.
Definition: logic.cpp:303
vec_basic get_args() const override
Returns the list of arguments.
Definition: logic.cpp:186
Piecewise(PiecewiseVec &&vec)
Constructor.
Definition: logic.cpp:139
bool __eq__(const Basic &o) const override
Test equality.
Definition: logic.cpp:196
int compare(const Basic &o) const override
Structural equality comparator.
Definition: logic.cpp:202
hash_t __hash__() const override
Definition: logic.cpp:171
Relational(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Constructor.
Definition: logic.cpp:607
virtual bool is_canonical(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const
Definition: logic.cpp:612
RCP< const Basic > create(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const override
Method to construct classes with canonicalization.
Definition: logic.cpp:746
RCP< const Basic > create(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const override
Method to construct classes with canonicalization.
Definition: logic.cpp:671
bool __eq__(const Basic &o) const override
Test equality.
Definition: logic.cpp:407
hash_t __hash__() const override
Definition: logic.cpp:393
int compare(const Basic &o) const override
Definition: logic.cpp:414
vec_basic get_args() const override
Returns the list of arguments.
Definition: logic.cpp:401
Main namespace for SymEngine package.
Definition: add.cpp:19
RCP< const Boolean > Ge(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Convenience function returning LessThan object.
Definition: logic.cpp:733
RCP< const Boolean > Lt(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Returns the canonicalized StrictLessThan object from the arguments.
Definition: logic.cpp:757
RCP< const Boolean > Le(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Returns the canonicalized LessThan object from the arguments.
Definition: logic.cpp:711
RCP< const Boolean > Eq(const RCP< const Basic > &lhs)
Returns the canonicalized Equality object from a single argument.
Definition: logic.cpp:642
RCP< const Boolean > Gt(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Convenience function returning StrictLessThan object.
Definition: logic.cpp:779
RCP< const Boolean > Ne(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Returns the canonicalized Unequality object from the arguments.
Definition: logic.cpp:682