Loading...
Searching...
No Matches
SymEngine::Interval Class Reference
+ Inheritance diagram for SymEngine::Interval:
+ Collaboration diagram for SymEngine::Interval:

Public Member Functions

void accept (Visitor &v) const override
 
void accept (EvalRealDoubleVisitorFinal &v) const override
 
hash_t __hash__ () const override
 
bool __eq__ (const Basic &o) const override
 Test equality.
 
int compare (const Basic &o) const override
 
 Interval (const RCP< const Number > &start, const RCP< const Number > &end, const bool left_open=false, const bool right_open=false)
 
RCP< const Setopen () const
 
RCP< const Setclose () const
 
RCP< const SetLopen () const
 
RCP< const SetRopen () const
 
RCP< const Setset_union (const RCP< const Set > &o) const override
 
RCP< const Setset_intersection (const RCP< const Set > &o) const override
 
RCP< const Setset_complement (const RCP< const Set > &o) const override
 
RCP< const Booleancontains (const RCP< const Basic > &a) const override
 
vec_basic get_args () const override
 Returns the list of arguments.
 
const RCP< const Number > & get_start () const
 
const RCP< const Number > & get_end () const
 
const bool & get_left_open () const
 
const bool & get_right_open () const
 
- Public Member Functions inherited from SymEngine::Set
vec_basic get_args () const override=0
 Returns the list of arguments.
 
virtual RCP< const Setset_intersection (const RCP< const Set > &o) const =0
 
virtual RCP< const Setset_union (const RCP< const Set > &o) const =0
 
virtual RCP< const Setset_complement (const RCP< const Set > &o) const =0
 
virtual RCP< const Booleancontains (const RCP< const Basic > &a) const =0
 
bool is_subset (const RCP< const Set > &o) const
 
bool is_proper_subset (const RCP< const Set > &o) const
 
bool is_superset (const RCP< const Set > &o) const
 
bool is_proper_superset (const RCP< const Set > &o) const
 
- Public Member Functions inherited from SymEngine::Basic
TypeID get_type_code () const
 
 Basic ()
 Constructor.
 
 Basic (const Basic &)=delete
 Delete the copy constructor and assignment.
 
Basicoperator= (const Basic &)=delete
 Assignment operator in continuation with above.
 
 Basic (Basic &&)=delete
 Delete the move constructor and assignment.
 
Basicoperator= (Basic &&)=delete
 Assignment operator in continuation with above.
 
virtual hash_t __hash__ () const =0
 
hash_t hash () const
 
virtual bool __eq__ (const Basic &o) const =0
 Test equality.
 
bool __neq__ (const Basic &o) const
 true if this is not equal to o.
 
int __cmp__ (const Basic &o) const
 Comparison operator.
 
virtual int compare (const Basic &o) const =0
 
std::string __str__ () const
 
std::string dumps () const
 Returns a string of the instance serialized.
 
RCP< const Basicsubs (const map_basic_basic &subs_dict) const
 Substitutes 'subs_dict' into 'self'.
 
RCP< const Basicxreplace (const map_basic_basic &subs_dict) const
 
virtual RCP< const Basicexpand_as_exp () const
 expands the special function in terms of exp function
 
virtual vec_basic get_args () const =0
 Returns the list of arguments.
 
virtual void accept (Visitor &v) const =0
 
virtual void accept (EvalRealDoubleVisitorFinal &v) const =0
 
RCP< const Basicdiff (const RCP< const Symbol > &x, bool cache=true) const
 
- Public Member Functions inherited from SymEngine::EnableRCPFromThis< Basic >
RCP< Basicrcp_from_this ()
 Get RCP<T> pointer to self (it will cast the pointer to T)
 
RCP< const Basicrcp_from_this () const
 Get RCP<const T> pointer to self (it will cast the pointer to const T)
 
RCP< const T2 > rcp_from_this_cast () const
 Get RCP<T2> pointer to self (it will cast the pointer to T2)
 
unsigned int use_count () const
 

Static Public Member Functions

static bool is_canonical (const RCP< const Number > &start, const RCP< const Number > &end, bool left_open, bool right_open)
 
- Static Public Member Functions inherited from SymEngine::Basic
static RCP< const Basicloads (const std::string &)
 Creates an instance of a serialized string.
 

Static Public Attributes

static const TypeID type_code_id = SYMENGINE_INTERVAL
 

Private Attributes

RCP< const Numberstart_
 
RCP< const Numberend_
 
bool left_open_
 
bool right_open_
 

Additional Inherited Members

- Data Fields inherited from SymEngine::Basic
TypeID type_code_
 

Detailed Description

Definition at line 145 of file sets.h.

Constructor & Destructor Documentation

◆ Interval()

SymEngine::Interval::Interval ( const RCP< const Number > &  start,
const RCP< const Number > &  end,
const bool  left_open = false,
const bool  right_open = false 
)

Definition at line 10 of file sets.cpp.

12 : start_(start), end_(end), left_open_(left_open), right_open_(right_open)
13{
14 SYMENGINE_ASSIGN_TYPEID()
15 SYMENGINE_ASSERT(
16 Interval::is_canonical(start_, end_, left_open_, right_open_));
17}

Member Function Documentation

◆ __eq__()

bool SymEngine::Interval::__eq__ ( const Basic o) const
overridevirtual

Test equality.

A virtual function for testing the equality of two Basic objects

Deprecated:
Use eq(const Basic &a, const Basic &b) non-member method
Parameters
oa constant reference to object to test against
Returns
True if this is equal to o

Implements SymEngine::Basic.

Definition at line 43 of file sets.cpp.

44{
45 if (is_a<Interval>(o)) {
46 const Interval &s = down_cast<const Interval &>(o);
47 return ((this->left_open_ == s.left_open_)
48 and (this->right_open_ == s.right_open_)
49 and eq(*this->start_, *s.start_) and eq(*this->end_, *s.end_));
50 }
51 return false;
52}
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
Definition: basic-inl.h:21

◆ __hash__()

hash_t SymEngine::Interval::__hash__ ( ) const
overridevirtual

Calculates the hash of the given SymEngine class. Use Basic.hash() which gives a cached version of the hash.

Returns
64-bit integer value for the hash

Implements SymEngine::Basic.

Definition at line 33 of file sets.cpp.

34{
35 hash_t seed = SYMENGINE_INTERVAL;
36 hash_combine<Basic>(seed, *start_);
37 hash_combine<Basic>(seed, *end_);
38 hash_combine<bool>(seed, left_open_);
39 hash_combine<bool>(seed, right_open_);
40 return seed;
41}

◆ accept() [1/2]

void SymEngine::Interval::accept ( EvalRealDoubleVisitorFinal v) const
overridevirtual

Implements SymEngine::Basic.

◆ accept() [2/2]

void SymEngine::Interval::accept ( Visitor v) const
overridevirtual

Implements SymEngine::Basic.

◆ close()

RCP< const Set > SymEngine::Interval::close ( ) const

Definition at line 92 of file sets.cpp.

93{
94 return interval(start_, end_, false, false);
95}
RCP< const Set > interval(const RCP< const Number > &start, const RCP< const Number > &end, const bool left_open=false, const bool right_open=false)
Definition: sets.h:611

◆ compare()

int SymEngine::Interval::compare ( const Basic o) const
overridevirtual

Returns -1, 0, 1 for this < o, this == o, this > o. This method is used when you want to sort things like x+y+z into canonical order. This function assumes that o is the same type as this. Use __cmp__ if you want general comparison.

Implements SymEngine::Basic.

Definition at line 54 of file sets.cpp.

55{
56 // compares two interval based on their length
57 SYMENGINE_ASSERT(is_a<Interval>(s))
58 const Interval &o = down_cast<const Interval &>(s);
59 if (left_open_ and not o.left_open_) {
60 return -1;
61 } else if (not left_open_ and o.left_open_) {
62 return 1;
63 } else if (right_open_ and not o.right_open_) {
64 return 1;
65 } else if (not right_open_ and o.right_open_) {
66 return -1;
67 } else {
68 auto temp = start_->__cmp__(*(o.start_));
69 if (temp != 0) {
70 return temp;
71 } else {
72 return end_->__cmp__(*(o.end_));
73 }
74 }
75}

◆ contains()

RCP< const Boolean > SymEngine::Interval::contains ( const RCP< const Basic > &  a) const
overridevirtual

Implements SymEngine::Set.

Definition at line 97 of file sets.cpp.

98{
99 if (not is_a_Number(*a)) {
100 if (is_a_Set(*a)) {
101 return boolean(false);
102 } else {
103 return make_rcp<Contains>(a, rcp_from_this_cast<const Set>());
104 }
105 }
106 if (eq(*start_, *a))
107 return boolean(not left_open_);
108 if (eq(*end_, *a))
109 return boolean(not right_open_);
110 if (eq(*min({end_, a}), *end_) or eq(*max({start_, a}), *start_))
111 return boolean(false);
112 return boolean(true);
113}
bool is_a_Number(const Basic &b)
Definition: number.h:130
RCP< const Basic > max(const vec_basic &arg)
Canonicalize Max:
Definition: functions.cpp:3555
RCP< const Basic > min(const vec_basic &arg)
Canonicalize Min:
Definition: functions.cpp:3659

◆ get_args()

vec_basic SymEngine::Interval::get_args ( ) const
overridevirtual

Returns the list of arguments.

Implements SymEngine::Set.

Definition at line 275 of file sets.cpp.

276{
277 return {start_, end_, boolean(left_open_), boolean(right_open_)};
278}

◆ get_end()

const RCP< const Number > & SymEngine::Interval::get_end ( ) const
inline

Definition at line 180 of file sets.h.

181 {
182 return end_;
183 }

◆ get_left_open()

const bool & SymEngine::Interval::get_left_open ( ) const
inline

Definition at line 184 of file sets.h.

185 {
186 return this->left_open_;
187 }

◆ get_right_open()

const bool & SymEngine::Interval::get_right_open ( ) const
inline

Definition at line 188 of file sets.h.

189 {
190 return this->right_open_;
191 }

◆ get_start()

const RCP< const Number > & SymEngine::Interval::get_start ( ) const
inline

Definition at line 176 of file sets.h.

177 {
178 return start_;
179 }

◆ is_canonical()

bool SymEngine::Interval::is_canonical ( const RCP< const Number > &  start,
const RCP< const Number > &  end,
bool  left_open,
bool  right_open 
)
static

Definition at line 19 of file sets.cpp.

22{
23 if (is_a<Complex>(*s) or is_a<Complex>(*e))
24 throw NotImplementedError("Complex set not implemented");
25 if (eq(*e, *s)) {
26 return false;
27 } else if (eq(*min({s, e}), *e)) {
28 return false;
29 }
30 return true;
31}

◆ Lopen()

RCP< const Set > SymEngine::Interval::Lopen ( ) const

Definition at line 82 of file sets.cpp.

83{
84 return interval(start_, end_, true, false);
85}

◆ open()

RCP< const Set > SymEngine::Interval::open ( ) const

Definition at line 77 of file sets.cpp.

78{
79 return interval(start_, end_, true, true);
80}

◆ Ropen()

RCP< const Set > SymEngine::Interval::Ropen ( ) const

Definition at line 87 of file sets.cpp.

88{
89 return interval(start_, end_, false, true);
90}

◆ set_complement()

RCP< const Set > SymEngine::Interval::set_complement ( const RCP< const Set > &  o) const
overridevirtual

Implements SymEngine::Set.

Definition at line 257 of file sets.cpp.

258{
259 if (is_a<Interval>(*o)) {
260 set_set cont;
261 const Interval &other = down_cast<const Interval &>(*o);
262 if (eq(*max({start_, other.start_}), *start_)) {
263 cont.insert(interval(other.get_start(), start_,
264 other.get_left_open(), not left_open_));
265 }
266 if (eq(*min({end_, other.end_}), *end_)) {
267 cont.insert(interval(end_, other.get_end(), not right_open_,
268 other.get_right_open()));
269 }
270 return SymEngine::set_union(cont);
271 }
272 return SymEngine::set_complement_helper(rcp_from_this_cast<const Set>(), o);
273}

◆ set_intersection()

RCP< const Set > SymEngine::Interval::set_intersection ( const RCP< const Set > &  o) const
overridevirtual

Implements SymEngine::Set.

Definition at line 131 of file sets.cpp.

132{
133 if (is_a<Interval>(*o)) {
134 const Interval &other = down_cast<const Interval &>(*o);
135 RCP<const Number> start, end;
136 bool left_open, right_open;
137 RCP<const Basic> start_end, end_start;
138 start_end = min({this->start_, other.end_});
139 end_start = min({this->end_, other.start_});
140
141 if (eq(*this->start_, *start_end) and eq(*other.start_, *end_start)) {
142 RCP<const Basic> start_start, end_end;
143 start_start = min({this->start_, other.start_});
144 end_end = min({this->end_, other.end_});
145 if (neq(*this->start_, *other.start_)) {
146 if (eq(*this->start_, *start_start)) {
147 start = other.start_;
148 left_open = other.left_open_;
149 } else {
150 start = this->start_;
151 left_open = this->left_open_;
152 }
153 } else {
154 start = this->start_;
155 left_open = this->left_open_ or other.left_open_;
156 }
157
158 if (neq(*this->end_, *other.end_)) {
159 if (eq(*this->end_, *end_end)) {
160 end = this->end_;
161 right_open = this->right_open_;
162 } else {
163 end = other.end_;
164 right_open = other.right_open_;
165 }
166 } else {
167 end = this->end_;
168 right_open = this->right_open_ or other.right_open_;
169 }
170 return interval(start, end, left_open, right_open);
171 } else {
172 return emptyset();
173 }
174 }
175 if (is_a<Integers>(*o) or is_a<Naturals>(*o) or is_a<Naturals0>(*o)) {
176 if (is_a_Number(*start_) and is_a_Number(*end_)) {
177 auto first = SymEngine::ceiling(start_);
178 auto last = SymEngine::floor(end_);
179 if (is_a<Naturals>(*o)
180 and not down_cast<const Integer &>(*first).is_positive()) {
181 first = integer(1);
182 } else if (is_a<Naturals0>(*o)
183 and down_cast<const Integer &>(*first).is_negative()) {
184 first = integer(0);
185 }
186 if (eq(*first, *start_) and left_open_) {
187 first = add(first, integer(1));
188 }
189 if (eq(*last, *end_) and right_open_) {
190 last = add(last, integer(-1));
191 }
192 if (eq(*Lt(last, first), *boolTrue)) {
193 return emptyset();
194 }
195 set_basic container;
196 while (eq(*Ge(last, first), *boolTrue)) {
197 container.insert(first);
198 first = add(first, integer(1));
199 }
200 return finiteset(container);
201 } else {
202 return SymEngine::set_intersection(
203 {rcp_from_this_cast<const Set>(), o});
204 }
205 }
206 if (is_a<UniversalSet>(*o) or is_a<EmptySet>(*o) or is_a<FiniteSet>(*o)
207 or is_a<Union>(*o) or is_a<Rationals>(*o) or is_a<Reals>(*o)
208 or is_a<Complexes>(*o)) {
209 return (*o).set_intersection(rcp_from_this_cast<const Set>());
210 }
211 return make_set_intersection({rcp_from_this_cast<const Set>(), o});
212}
T end(T... args)
T insert(T... args)
RCP< const Boolean > Ge(const RCP< const Basic > &lhs, const RCP< const Basic > &rhs)
Convenience function returning LessThan object.
Definition: logic.cpp:744
RCP< const EmptySet > emptyset()
Definition: sets.h:590
RCP< const Set > finiteset(const set_basic &container)
Definition: sets.h:602
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:768
RCP< const Basic > ceiling(const RCP< const Basic > &arg)
Canonicalize Ceiling:
Definition: functions.cpp:705
RCP< const Basic > floor(const RCP< const Basic > &arg)
Canonicalize Floor:
Definition: functions.cpp:611
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
Definition: add.cpp:425
bool neq(const Basic &a, const Basic &b)
Checks inequality for a and b
Definition: basic-inl.h:29
std::enable_if< std::is_integral< T >::value, RCP< constInteger > >::type integer(T i)
Definition: integer.h:197

◆ set_union()

RCP< const Set > SymEngine::Interval::set_union ( const RCP< const Set > &  o) const
overridevirtual

Implements SymEngine::Set.

Definition at line 214 of file sets.cpp.

215{
216 if (is_a<Interval>(*o)) {
217 const Interval &other = down_cast<const Interval &>(*o);
218 RCP<const Basic> start_start, end_end, m;
219 RCP<const Number> start, end;
220 bool left_open, right_open;
221 start_start = max({this->start_, other.start_});
222 end_end = min({this->end_, other.end_});
223 m = min({start_start, end_end});
224 if ((eq(*end_end, *start_start) and eq(*end_end, *m)
225 and ((eq(*end_end, *this->end_) and this->right_open_)
226 or (eq(*end_end, *other.end_) and other.right_open_)))
227 or (eq(*end_end, *m) and not eq(*end_end, *start_start))) {
228 return SymEngine::make_set_union(
229 {rcp_from_this_cast<const Set>(), o});
230 } else {
231 if (eq(*min({this->start_, other.start_}), *this->start_)) {
232 start = this->start_;
233 } else {
234 start = other.start_;
235 }
236 if (eq(*max({this->end_, other.end_}), *this->end_)) {
237 end = this->end_;
238 } else {
239 end = other.end_;
240 }
241 left_open = ((neq(*this->start_, *start) or this->left_open_)
242 and (neq(*other.start_, *start) or other.left_open_));
243 right_open = ((neq(*this->end_, *end) or this->right_open_)
244 and (neq(*other.end_, *end) or other.right_open_));
245 return interval(start, end, left_open, right_open);
246 }
247 }
248 if (is_a<UniversalSet>(*o) or is_a<EmptySet>(*o) or is_a<FiniteSet>(*o)
249 or is_a<Union>(*o) or is_a<Complexes>(*o) or is_a<Reals>(*o)
250 or is_a<Rationals>(*o) or is_a<Integers>(*o) or is_a<Naturals>(*o)
251 or is_a<Naturals0>(*o)) {
252 return (*o).set_union(rcp_from_this_cast<const Set>());
253 }
254 return SymEngine::make_set_union({rcp_from_this_cast<const Set>(), o});
255}

Field Documentation

◆ end_

RCP<const Number> SymEngine::Interval::end_
private

Definition at line 149 of file sets.h.

◆ left_open_

bool SymEngine::Interval::left_open_
private

Definition at line 150 of file sets.h.

◆ right_open_

bool SymEngine::Interval::right_open_
private

Definition at line 150 of file sets.h.

◆ start_

RCP<const Number> SymEngine::Interval::start_
private

Definition at line 148 of file sets.h.

◆ type_code_id

const TypeID SymEngine::Interval::type_code_id = SYMENGINE_INTERVAL
static

Type_code_id shared by all instances

Definition at line 153 of file sets.h.


The documentation for this class was generated from the following files: