Loading...
Searching...
No Matches
set_funcs.cpp
1#ifndef SYMENGINE_SET_FUNCS_H
2#define SYMENGINE_SET_FUNCS_H
3
4#include <symengine/visitor.h>
6
7namespace SymEngine
8{
9
10class SupVisitor : public BaseVisitor<SupVisitor>
11{
12private:
13 RCP<const Basic> sup_;
14
15public:
16 SupVisitor() {}
17
18 void bvisit(const Basic &x){};
19
20 void bvisit(const Set &x)
21 {
22 throw SymEngineException(
23 "Set not partially ordered: supremum undefined");
24 };
25
26 void bvisit(const Reals &x)
27 {
28 sup_ = infty(1);
29 };
30
31 void bvisit(const Rationals &x)
32 {
33 sup_ = infty(1);
34 };
35
36 void bvisit(const Integers &x)
37 {
38 sup_ = infty(1);
39 };
40
41 void bvisit(const Naturals &x)
42 {
43 sup_ = infty(1);
44 };
45
46 void bvisit(const Naturals0 &x)
47 {
48 sup_ = infty(1);
49 };
50
51 void bvisit(const Interval &x)
52 {
53 sup_ = x.get_end();
54 };
55
56 void bvisit(const FiniteSet &x)
57 {
58 const set_basic &container = x.get_container();
59 vec_basic v(container.begin(), container.end());
60 sup_ = max(v);
61 };
62
63 void bvisit(const Union &x)
64 {
66 for (auto &a : x.get_container()) {
67 a->accept(*this);
68 suprema.push_back(sup_);
69 }
70 sup_ = max(suprema);
71 };
72
73 void bvisit(const Complement &x)
74 {
75 throw NotImplementedError("sup for Complement not implemented");
76 };
77
78 void bvisit(const ImageSet &x)
79 {
80 throw NotImplementedError("sup for ImageSet not implemented");
81 };
82
83 RCP<const Basic> apply(const Set &s)
84 {
85 s.accept(*this);
86 return sup_;
87 };
88};
89
90class InfVisitor : public BaseVisitor<InfVisitor>
91{
92private:
93 RCP<const Basic> inf_;
94
95public:
96 InfVisitor() {}
97
98 void bvisit(const Basic &x){};
99
100 void bvisit(const Set &x)
101 {
102 throw SymEngineException(
103 "Set not partially ordered: infimum undefined");
104 };
105
106 void bvisit(const Reals &x)
107 {
108 inf_ = infty(-1);
109 };
110
111 void bvisit(const Rationals &x)
112 {
113 inf_ = infty(-1);
114 };
115
116 void bvisit(const Integers &x)
117 {
118 inf_ = infty(-1);
119 };
120
121 void bvisit(const Naturals &x)
122 {
123 inf_ = integer(1);
124 };
125
126 void bvisit(const Naturals0 &x)
127 {
128 inf_ = integer(0);
129 };
130
131 void bvisit(const Interval &x)
132 {
133 inf_ = x.get_start();
134 };
135
136 void bvisit(const FiniteSet &x)
137 {
138 const set_basic &container = x.get_container();
139 vec_basic v(container.begin(), container.end());
140 inf_ = min(v);
141 };
142
143 void bvisit(const Union &x)
144 {
146 for (auto &a : x.get_container()) {
147 a->accept(*this);
148 infima.push_back(inf_);
149 }
150 inf_ = min(infima);
151 };
152
153 void bvisit(const Complement &x)
154 {
155 throw NotImplementedError("inf for Complement not implemented");
156 };
157
158 void bvisit(const ImageSet &x)
159 {
160 throw NotImplementedError("inf for ImageSet not implemented");
161 };
162
163 RCP<const Basic> apply(const Set &s)
164 {
165 s.accept(*this);
166 return inf_;
167 };
168};
169
170RCP<const Basic> sup(const Set &s)
171{
173 return visitor.apply(s);
174}
175
176RCP<const Basic> inf(const Set &s)
177{
178 InfVisitor visitor;
179 return visitor.apply(s);
180}
181
182class BoundaryVisitor : public BaseVisitor<BoundaryVisitor>
183{
184private:
185 RCP<const Set> boundary_;
186
187public:
188 BoundaryVisitor() {}
189
190 void bvisit(const Basic &x){};
191
192 void bvisit(const EmptySet &x)
193 {
194 boundary_ = emptyset();
195 };
196
197 void bvisit(const UniversalSet &x)
198 {
199 boundary_ = emptyset();
200 };
201
202 void bvisit(const Complexes &x)
203 {
204 boundary_ = emptyset();
205 };
206
207 void bvisit(const Reals &x)
208 {
209 boundary_ = emptyset();
210 };
211
212 void bvisit(const Rationals &x)
213 {
214 boundary_ = reals();
215 };
216
217 void bvisit(const Integers &x)
218 {
219 boundary_ = integers();
220 };
221
222 void bvisit(const Naturals &x)
223 {
224 boundary_ = naturals();
225 };
226
227 void bvisit(const Naturals0 &x)
228 {
229 boundary_ = naturals0();
230 };
231
232 void bvisit(const Interval &x)
233 {
234 boundary_ = finiteset({x.get_start(), x.get_end()});
235 };
236
237 void bvisit(const FiniteSet &x)
238 {
240 };
241
242 void bvisit(const Union &x)
243 {
245 const set_set &sets = x.get_container();
246 for (auto it = sets.begin(); it != sets.end(); ++it) {
248 for (auto interit = sets.begin(); interit != sets.end();
249 ++interit) {
250 if (it != interit) {
251 interior_sets.insert(interior(**interit));
252 }
253 }
254 boundary_sets.insert(
255 set_complement(apply(**it), set_union(interior_sets)));
256 }
257 boundary_ = set_union(boundary_sets);
258 };
259
260 void bvisit(const Complement &x)
261 {
262 throw NotImplementedError("inf for Complement not implemented");
263 };
264
265 void bvisit(const ImageSet &x)
266 {
267 throw NotImplementedError("inf for ImageSet not implemented");
268 };
269
270 RCP<const Set> apply(const Set &s)
271 {
272 s.accept(*this);
273 return boundary_;
274 };
275};
276
277RCP<const Set> boundary(const Set &s)
278{
280 return visitor.apply(s);
281}
282
283RCP<const Set> interior(const Set &s)
284{
285 return set_complement(rcp_static_cast<const Set>(s.rcp_from_this()),
286 boundary(s));
287}
288
289RCP<const Set> closure(const Set &s)
290{
291 return s.set_union(boundary(s));
292}
293
294} // namespace SymEngine
295
296#endif // SYMENGINE_SET_FUNCS_H
The lowest unit of symbolic representation.
Definition basic.h:97
RCP< T > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)
T insert(T... args)
Main namespace for SymEngine package.
Definition add.cpp:19
RCP< const Reals > reals()
Definition sets.h:560
RCP< const EmptySet > emptyset()
Definition sets.h:590
RCP< const Set > finiteset(const set_basic &container)
Definition sets.h:602
RCP< const Basic > max(const vec_basic &arg)
Canonicalize Max:
RCP< const Naturals0 > naturals0()
Definition sets.h:584
RCP< const Integers > integers()
Definition sets.h:572
void hash_combine(hash_t &seed, const T &v)
Definition basic-inl.h:95
RCP< const Naturals > naturals()
Definition sets.h:578
RCP< const Basic > min(const vec_basic &arg)
Canonicalize Min:
std::enable_if< std::is_integral< T >::value, RCP< constInteger > >::type integer(T i)
Definition integer.h:197
T push_back(T... args)