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;
35  bool get_val() const;
36  virtual vec_basic get_args() const;
37  virtual bool __eq__(const Basic &o) const;
39  virtual int compare(const Basic &o) const;
40  virtual RCP<const Boolean> logical_not() const;
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;
64  RCP<const Basic> get_expr() const;
65  RCP<const Set> get_set() const;
66  virtual vec_basic get_args() const;
67  virtual bool __eq__(const Basic &o) const;
68  RCP<const Basic> create(const RCP<const Basic> &lhs,
69  const RCP<const Set> &rhs) const;
71  virtual int compare(const Basic &o) const;
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  hash_t __hash__() const;
92  const PiecewiseVec &get_vec() const;
93  virtual vec_basic get_args() const;
94  virtual bool __eq__(const Basic &o) const;
96  virtual int compare(const Basic &o) const;
97 };
98 
99 // Vec is vector of pairs of RCP<const Basic> and RCP<const Boolean> to
100 // represent (Expr, Condition) pairs
101 inline RCP<const Basic> piecewise(PiecewiseVec &&vec)
102 {
103  return make_rcp<Piecewise>(std::move(vec));
104 }
105 
106 class And : public Boolean
107 {
108 private:
109  set_boolean container_;
110 
111 public:
112  IMPLEMENT_TYPEID(SYMENGINE_AND)
113  And(const set_boolean &s);
114  bool is_canonical(const set_boolean &container_);
116  hash_t __hash__() const;
117  virtual vec_basic get_args() const;
118  RCP<const Basic> create(const set_boolean &a) const;
119  virtual bool __eq__(const Basic &o) const;
121  virtual int compare(const Basic &o) const;
122  const set_boolean &get_container() const;
123  virtual RCP<const Boolean> logical_not() const;
124 };
125 
126 class Or : public Boolean
127 {
128 private:
129  set_boolean container_;
130 
131 public:
132  IMPLEMENT_TYPEID(SYMENGINE_OR)
133  Or(const set_boolean &s);
134  bool is_canonical(const set_boolean &container_);
136  hash_t __hash__() const;
137  virtual vec_basic get_args() const;
138  virtual bool __eq__(const Basic &o) const;
140  virtual int compare(const Basic &o) const;
141  const set_boolean &get_container() const;
142  virtual RCP<const Boolean> logical_not() const;
143 };
144 
145 class Not : public Boolean
146 {
147 private:
148  RCP<const Boolean> arg_;
149 
150 public:
151  IMPLEMENT_TYPEID(SYMENGINE_NOT)
152  Not(const RCP<const Boolean> &s);
153  bool is_canonical(const RCP<const Boolean> &s);
155  hash_t __hash__() const;
156  virtual vec_basic get_args() const;
157  virtual bool __eq__(const Basic &o) const;
159  virtual int compare(const Basic &o) const;
160  RCP<const Boolean> get_arg() const;
161  virtual RCP<const Boolean> logical_not() const;
162 };
163 
164 class Xor : public Boolean
165 {
166 private:
167  vec_boolean container_;
168 
169 public:
170  IMPLEMENT_TYPEID(SYMENGINE_XOR)
171  Xor(const vec_boolean &s);
172  bool is_canonical(const vec_boolean &container_);
173  hash_t __hash__() const;
174  virtual vec_basic get_args() const;
175  virtual bool __eq__(const Basic &o) const;
176  virtual int compare(const Basic &o) const;
177  const vec_boolean &get_container() const;
178 };
179 
180 class Relational : public TwoArgBasic<Boolean>
181 {
182 public:
184  Relational(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
186  virtual bool is_canonical(const RCP<const Basic> &lhs,
187  const RCP<const Basic> &rhs) const;
188 };
189 
190 class Equality : public Relational
191 {
193 public:
194  IMPLEMENT_TYPEID(SYMENGINE_EQUALITY)
195  Equality(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
196  virtual RCP<const Basic> create(const RCP<const Basic> &lhs,
197  const RCP<const Basic> &rhs) const;
198  virtual RCP<const Boolean> logical_not() const;
199 };
200 
201 class Unequality : public Relational
202 {
204 public:
205  IMPLEMENT_TYPEID(SYMENGINE_UNEQUALITY)
206  Unequality(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
207  virtual RCP<const Basic> create(const RCP<const Basic> &lhs,
208  const RCP<const Basic> &rhs) const;
209  virtual RCP<const Boolean> logical_not() const;
210 };
211 
212 class LessThan : public Relational
213 {
215 public:
216  IMPLEMENT_TYPEID(SYMENGINE_LESSTHAN)
217  LessThan(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
218  virtual RCP<const Basic> create(const RCP<const Basic> &lhs,
219  const RCP<const Basic> &rhs) const;
220  virtual RCP<const Boolean> logical_not() const;
221 };
222 
224 {
226 public:
227  IMPLEMENT_TYPEID(SYMENGINE_STRICTLESSTHAN)
228  StrictLessThan(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
229  virtual RCP<const Basic> create(const RCP<const Basic> &lhs,
230  const RCP<const Basic> &rhs) const;
231  virtual RCP<const Boolean> logical_not() const;
232 };
233 
234 inline bool is_a_Relational(const Basic &b)
235 {
236  return (b.get_type_code() == SYMENGINE_EQUALITY
237  || b.get_type_code() == SYMENGINE_UNEQUALITY
238  || b.get_type_code() == SYMENGINE_LESSTHAN
239  || b.get_type_code() == SYMENGINE_STRICTLESSTHAN);
240 }
241 
242 inline bool is_a_Boolean(const Basic &b)
243 {
244  return (b.get_type_code() == SYMENGINE_BOOLEAN_ATOM
245  || b.get_type_code() == SYMENGINE_CONTAINS
246  || b.get_type_code() == SYMENGINE_AND
247  || b.get_type_code() == SYMENGINE_OR
248  || b.get_type_code() == SYMENGINE_NOT
249  || b.get_type_code() == SYMENGINE_XOR || is_a_Relational(b));
250 }
251 
253 RCP<const Boolean> Eq(const RCP<const Basic> &lhs);
255 RCP<const Boolean> Eq(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
257 RCP<const Boolean> Ne(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
259 RCP<const Boolean> Ge(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
261 RCP<const Boolean> Gt(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
263 RCP<const Boolean> Le(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
265 RCP<const Boolean> Lt(const RCP<const Basic> &lhs, const RCP<const Basic> &rhs);
266 
267 RCP<const Boolean> logical_and(const set_boolean &s);
268 RCP<const Boolean> logical_nand(const set_boolean &s);
269 RCP<const Boolean> logical_or(const set_boolean &s);
270 RCP<const Boolean> logical_not(const RCP<const Boolean> &s);
271 RCP<const Boolean> logical_nor(const set_boolean &s);
272 RCP<const Boolean> logical_xor(const vec_boolean &s);
273 RCP<const Boolean> logical_xnor(const vec_boolean &s);
274 } // namespace SymEngine
275 
276 #endif
The base class for SymEngine.
#define IMPLEMENT_TYPEID(SYMENGINE_ID)
Inline members and functions.
Definition: basic.h:340
virtual vec_basic get_args() const
Returns the list of arguments.
Definition: logic.cpp:173
virtual int compare(const Basic &o) const
Structural equality comparator.
Definition: logic.cpp:186
virtual bool __eq__(const Basic &o) const
Test equality.
Definition: logic.cpp:179
hash_t __hash__() const
Definition: logic.cpp:165
The lowest unit of symbolic representation.
Definition: basic.h:95
virtual int compare(const Basic &o) const
Structural equality comparator.
Definition: logic.cpp:37
virtual bool __eq__(const Basic &o) const
Test equality.
Definition: logic.cpp:31
hash_t __hash__() const
Definition: logic.cpp:14
virtual vec_basic get_args() const
Returns the list of arguments.
Definition: logic.cpp:26
virtual vec_basic get_args() const
Returns the list of arguments.
Definition: logic.cpp:77
virtual int compare(const Basic &o) const
Structural equality comparator.
Definition: logic.cpp:92
hash_t __hash__() const
Definition: logic.cpp:59
virtual bool __eq__(const Basic &o) const
Test equality.
Definition: logic.cpp:85
Contains(const RCP< const Basic > &expr, const RCP< const Set > &set)
Constructor.
Definition: logic.cpp:56
virtual RCP< const Basic > create(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const
Method to construct classes with canonicalization.
Definition: logic.cpp:581
virtual RCP< const Basic > create(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const
Method to construct classes with canonicalization.
Definition: logic.cpp:650
virtual bool __eq__(const Basic &o) const
Test equality.
Definition: logic.cpp:309
hash_t __hash__() const
Definition: logic.cpp:295
virtual int compare(const Basic &o) const
Structural equality comparator.
Definition: logic.cpp:314
virtual vec_basic get_args() const
Returns the list of arguments.
Definition: logic.cpp:302
hash_t __hash__() const
Definition: logic.cpp:233
virtual bool __eq__(const Basic &o) const
Test equality.
Definition: logic.cpp:247
virtual vec_basic get_args() const
Returns the list of arguments.
Definition: logic.cpp:241
virtual int compare(const Basic &o) const
Structural equality comparator.
Definition: logic.cpp:253
Piecewise(PiecewiseVec &&vec)
Constructor.
Definition: logic.cpp:118
virtual int compare(const Basic &o) const
Structural equality comparator.
Definition: logic.cpp:152
hash_t __hash__() const
Definition: logic.cpp:121
virtual bool __eq__(const Basic &o) const
Test equality.
Definition: logic.cpp:146
virtual vec_basic get_args() const
Returns the list of arguments.
Definition: logic.cpp:136
Relational(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Constructor.
Definition: logic.cpp:557
virtual bool is_canonical(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const
Definition: logic.cpp:562
virtual RCP< const Basic > create(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const
Method to construct classes with canonicalization.
Definition: logic.cpp:696
virtual RCP< const Basic > create(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs) const
Method to construct classes with canonicalization.
Definition: logic.cpp:621
virtual int compare(const Basic &o) const
Definition: logic.cpp:364
hash_t __hash__() const
Definition: logic.cpp:343
virtual vec_basic get_args() const
Returns the list of arguments.
Definition: logic.cpp:351
virtual bool __eq__(const Basic &o) const
Test equality.
Definition: logic.cpp:357
T move(T... args)
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:683
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:707
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:661
RCP< const Boolean > Eq(const RCP< const Basic > &lhs)
Returns the canonicalized Equality object from a single argument.
Definition: logic.cpp:592
RCP< const Boolean > Gt(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Convenience function returning StrictLessThan object.
Definition: logic.cpp:729
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:632