codac 1.5.6
Loading...
Searching...
No Matches
codac2_Interval_impl.h
Go to the documentation of this file.
1
14
15#pragma once
16
17// Inline functions
18
19namespace codac2
20{
22 : gaol::interval(-oo,oo)
23 { }
24
25 inline Interval::Interval(double a)
26 : gaol::interval(a)
27 {
28 if(a == -oo || a == oo)
29 set_empty();
30 }
31
32 inline Interval::Interval(double a, double b)
33 : gaol::interval(a,b)
34 { }
35
37 : gaol::interval(x)
38 { }
39
40 inline Interval::Interval(const std::array<double,1>& array)
41 : Interval(array[0])
42 { }
43
44 inline Interval::Interval(const std::array<double,2>& array)
45 : Interval(array[0],array[1])
46 { }
47
48 inline Interval::Interval(std::initializer_list<double> l)
49 : Interval()
50 {
52 }
53
55 {
56 *this = Interval(-oo,oo);
57 return *this;
58 }
59
61 {
62 *this = x;
63 return *this;
64 }
65
66 inline Interval& Interval::init_from_list(const std::list<double>& l)
67 {
68 if(l.size() == 1)
69 *this = Interval(*l.begin());
70
71 else if(l.size() == 2)
72 *this = Interval(*l.begin(),*std::prev(l.end()));
73
74 else
75 {
76 assert_release("'Interval' can only be defined by one or two 'double' values.");
77 }
78
79 return *this;
80 }
81
82 inline Interval& Interval::operator=(double x)
83 {
84 if(x == -oo || x == oo)
85 set_empty();
86 else
87 gaol::interval::operator=(x);
88
89 return *this;
90 }
91
93 {
94 gaol::interval::operator=(x);
95 return *this;
96 }
97
98 inline bool Interval::operator==(const Interval& x) const
99 {
100 return (is_empty() && x.is_empty()) || (lb() == x.lb() && ub() == x.ub());
101 }
102
103 inline bool Interval::operator!=(const Interval& x) const
104 {
105 return !(*this == x);
106 }
107
108 inline double Interval::lb() const
109 {
110 return gaol::interval::left();
111 }
112
113 inline double Interval::ub() const
114 {
115 return gaol::interval::right();
116 }
117
118 inline double Interval::mid() const
119 {
120 double m = gaol::interval::midpoint();
121 gaol::round_upward();
122 return m;
123 }
124
125 inline double Interval::mag() const
126 {
127 return gaol::interval::mag();
128 }
129
130 inline double Interval::mig() const
131 {
132 return gaol::interval::mig();
133 }
134
135 inline double Interval::rand() const
136 {
137 if(is_empty())
138 return std::numeric_limits<double>::quiet_NaN();
139
140 double a = std::max<double>(next_float(-oo),lb());
141 double b = std::min<double>(previous_float(oo),ub());
142 double r = a + (((double)std::rand())/(double)RAND_MAX)*(b-a);
143 // The above operation may result in a floating point outside the bounds,
144 // due to floating-point errors. Such possible error is corrected below:
145 return std::max<double>(lb(),std::min<double>(r,ub()));
146 }
147
148 inline double Interval::rad() const
149 {
150 if(is_empty())
151 return std::numeric_limits<double>::quiet_NaN();
152
153 else if(is_unbounded())
154 return oo;
155
156 else
157 {
158 double t = mid();
159 double t1 = (t-*this).ub();
160 double t2 = (*this-t).ub();
161 return (t1>t2) ? t1 : t2;
162 }
163 }
164
165 inline double Interval::diam() const
166 {
167 if(is_empty())
168 return std::numeric_limits<double>::quiet_NaN();
169
170 else
171 {
172 double d = gaol::interval::width();
173 gaol::round_upward();
174 return d;
175 }
176 }
177
178 inline double Interval::volume() const
179 {
180 return diam();
181 }
182
183 inline Index Interval::size() const
184 {
185 return 1;
186 }
187
189 {
190 *this = Interval::empty();
191 }
192
193 inline bool Interval::is_empty() const
194 {
195 return gaol::interval::is_empty();
196 }
197
198 inline bool Interval::contains(const double& x) const
199 {
200 return gaol::interval::set_contains(x);
201 }
202
203 inline bool Interval::interior_contains(const double& x) const
204 {
205 return !is_empty() && x > lb() && x < ub();
206 }
207
208 inline bool Interval::is_unbounded() const
209 {
210 return !gaol::interval::is_finite();
211 }
212
213 inline bool Interval::is_degenerated() const
214 {
215 return is_empty() || gaol::interval::is_a_double();
216 }
217
218 inline bool Interval::is_integer() const
219 {
220 return gaol::interval::is_an_int();
221 }
222
223 inline bool Interval::intersects(const Interval &x) const
224 {
225 return !is_empty() && !x.is_empty() && lb() <= x.ub() && ub() >= x.lb();
226 }
227
228 inline bool Interval::is_disjoint(const Interval& x) const
229 {
230 return is_empty() || x.is_empty() || lb() > x.ub() || ub() < x.lb();
231 }
232
233 inline bool Interval::overlaps(const Interval& x) const
234 {
235 return !is_empty() && !x.is_empty() && ub() > x.lb() && x.ub() > lb();
236 }
237
238 inline bool Interval::is_subset(const Interval& x) const
239 {
240 return is_empty() || (!x.is_empty() && x.lb() <= lb() && x.ub() >= ub());
241 }
242
243 inline bool Interval::is_strict_subset(const Interval& x) const
244 {
245 return !x.is_empty() && (is_empty() || (x.lb() < lb() && x.ub() >= ub()) || (x.ub() > ub() && x.lb() <= lb()));
246 }
247
248 inline bool Interval::is_interior_subset(const Interval& x) const
249 {
250 return is_empty() || (!x.is_empty() && (x.lb() == -oo || x.lb() < lb()) && (x.ub() == oo || x.ub() > ub()));
251 }
252
254 {
255 return !x.is_empty() && (is_empty() || (
256 (x.lb() < lb() && (x.ub() == oo || x.ub() > ub()))
257 || (x.ub() > ub() && (x.lb() == -oo || x.lb() < lb()))
258 ));
259 }
260
261 inline bool Interval::is_superset(const Interval& x) const
262 {
263 return x.is_subset(*this);
264 }
265
266 inline bool Interval::is_strict_superset(const Interval& x) const
267 {
268 return x.is_strict_subset(*this);
269 }
270
271 inline Interval& Interval::inflate(const double& rad)
272 {
273 (*this) += Interval(-rad,rad);
274 return *this;
275 }
276
277 inline bool Interval::is_bisectable() const
278 {
279 if(is_empty())
280 return false;
281 double m = mid();
282 return lb() < m && m < ub();
283 }
284
285 inline std::pair<Interval,Interval> Interval::bisect(float ratio) const
286 {
287 assert_release(is_bisectable());
288 assert_release(Interval(0,1).interior_contains(ratio));
289
290 if(lb() == -oo)
291 {
292 if(ub() == oo)
293 return { Interval(-oo,0), Interval(0,oo) };
294 else
295 return { Interval(-oo,-std::numeric_limits<double>::max()), Interval(-std::numeric_limits<double>::max(),ub()) };
296 }
297
298 else if(ub() == oo)
299 return { Interval(lb(),std::numeric_limits<double>::max()), Interval(std::numeric_limits<double>::max(),oo) };
300
301 else
302 {
303 double m;
304
305 if(ratio == 0.5)
306 m = mid();
307
308 else
309 {
310 m = lb() + ratio*diam();
311 if(m >= ub())
312 m = next_float(lb());
313
314 assert(m < ub());
315 }
316
317 return { Interval(lb(),m), Interval(m,ub()) };
318 }
319 }
320
321 inline std::vector<Interval> Interval::complementary(bool compactness) const
322 {
323 if(is_empty() || (compactness && is_degenerated()))
324 return { {-oo,oo} };
325
326 std::vector<Interval> l;
327
328 if(lb() > -oo)
329 l.push_back({-oo,lb()});
330
331 if(ub() < oo)
332 l.push_back({ub(),oo});
333
334 return l;
335 }
336
337 inline std::vector<Interval> Interval::diff(const Interval& y, bool compactness) const
338 {
339 if(compactness && is_degenerated())
340 {
341 if(is_empty() || y.contains(lb()))
342 return {};
343 else
344 return { *this };
345 }
346
347 std::vector<Interval> l;
348 for(const auto& li : y.complementary(compactness))
349 {
350 Interval inter = li & *this;
351 if(!inter.is_degenerated())
352 l.push_back(inter);
353 }
354
355 return l;
356 }
357
358 inline Interval operator&(const Interval& x, const Interval& y)
359 {
360 if(x.is_empty() || y.is_empty() || x.ub() < y.lb())
361 return Interval::empty();
362
363 else
364 return gaol::operator&(x,y);
365 }
366
367 inline Interval operator|(const Interval& x, double y)
368 {
369 return gaol::operator|(x,Interval(y));
370 }
371
372 inline Interval operator|(const Interval& x, const Interval& y)
373 {
374 return gaol::operator|(x,y);
375 }
376
377 inline const Interval& operator+(const Interval& x)
378 {
379 return x;
380 }
381
382 inline Interval operator+(const Interval& x, double y)
383 {
384 if(y == -oo || y == oo)
385 return Interval::empty();
386
387 else
388 return gaol::operator+(x,y);
389 }
390
391 inline Interval operator+(double x, const Interval& y)
392 {
393 if(x == -oo || x == oo)
394 return Interval::empty();
395
396 else
397 return gaol::operator+(x,y);
398 }
399
400 inline Interval operator+(const Interval& x, const Interval& y)
401 {
402 return gaol::operator+(x,y);
403 }
404
405 inline Interval operator-(const Interval& x, double y)
406 {
407 if(y == -oo || y == oo)
408 return Interval::empty();
409
410 else
411 return gaol::operator-(x, y);
412 }
413
414 inline Interval operator-(double x, const Interval& y)
415 {
416 if(x == -oo || x == oo)
417 return Interval::empty();
418
419 else
420 return gaol::operator-(x, y);
421 }
422
423 inline Interval operator-(const Interval& x, const Interval& y)
424 {
425 return gaol::operator-(x, y);
426 }
427
428 inline Interval operator*(const Interval& x, double y)
429 {
430 if(y == -oo || y == oo)
431 return Interval::empty();
432
433 else
434 return gaol::operator*(x,y);
435 }
436
437 inline Interval operator*(double x, const Interval& y)
438 {
439 if(x == -oo || x == oo)
440 return Interval::empty();
441
442 else
443 return gaol::operator*(x,y);
444 }
445
446 inline Interval operator*(const Interval& x, const Interval& y)
447 {
448 return gaol::operator*(x,y);
449 }
450
451 inline Interval operator/(const Interval& x, double y)
452 {
453 if(y == -oo || y == oo)
454 return Interval::empty();
455
456 else
457 return gaol::operator/(x,y);
458 }
459
460 inline Interval operator/(double x, const Interval& y)
461 {
462 if(x == -oo || x == oo)
463 return Interval::empty();
464
465 else
466 return gaol::operator/(x,y);
467 }
468
469 inline Interval operator/(const Interval& x, const Interval& y)
470 {
471 return gaol::operator/(x,y);
472 }
473
475 {
476 gaol::interval::operator|=(x);
477 return *this;
478 }
479
481 {
482 gaol::interval::operator&=(x);
483 return *this;
484 }
485
487 {
488 if(x == -oo || x == oo)
489 set_empty();
490 else
491 gaol::interval::operator+=(x);
492 return *this;
493 }
494
496 {
497 gaol::interval::operator+=(x);
498 return *this;
499 }
500
502 {
503 return 0.-*this;
504 }
505
507 {
508 if(x == -oo || x == oo)
509 set_empty();
510 else
511 gaol::interval::operator-=(x);
512 return *this;
513 }
514
516 {
517 gaol::interval::operator-=(x);
518 return *this;
519 }
520
522 {
523 if(x == -oo || x == oo)
524 set_empty();
525 else
526 gaol::interval::operator*=(x);
527 return *this;
528 }
529
531 {
532 gaol::interval::operator*=(x);
533 return *this;
534 }
535
537 {
538 if(x == -oo || x == oo)
539 set_empty();
540 else
541 gaol::interval::operator/=(x);
542 return *this;
543 }
544
546 {
547 gaol::interval::operator/=(x);
548 return *this;
549 }
550
552 {
553 return std::numeric_limits<double>::quiet_NaN();
554 }
555
557 {
558 return gaol::interval::zero();
559 }
560
562 {
563 return gaol::interval::one();
564 }
565
567 {
568 return gaol::interval::half_pi();
569 }
570
572 {
573 return gaol::interval::pi();
574 }
575
577 {
578 return gaol::interval::two_pi();
579 }
580
581 inline std::ostream& operator<<(std::ostream& os, const Interval& x)
582 {
583 gaol::interval::precision(os.precision());
584 gaol::operator<<(os,x);
585 return os;
586 }
587
588 inline Interval::Interval(const gaol::interval& x)
589 : gaol::interval(x)
590 { }
591
592 inline Interval operator""_i(long double x)
593 {
594 return Interval(x);
595 }
596
597 inline double previous_float(double x)
598 {
599 return gaol::previous_float(x);
600 }
601
602 inline double next_float(double x)
603 {
604 return gaol::next_float(x);
605 }
606}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
bool is_unbounded() const
Tests if one of the bounds of this is infinite.
Definition codac2_Interval_impl.h:208
Interval & operator*=(double x)
Self multiplication of this and a real x.
Definition codac2_Interval_impl.h:521
Interval & operator-=(double x)
Self substraction of this and a real x.
Definition codac2_Interval_impl.h:506
bool operator==(const Interval &x) const
Comparison (equality) between two intervals.
Definition codac2_Interval_impl.h:98
bool is_empty() const
Tests if this is empty.
Definition codac2_Interval_impl.h:193
double mig() const
Returns the mignitude of this.
Definition codac2_Interval_impl.h:130
bool is_bisectable() const
Tests if this can be bisected into two non-degenerated intervals.
Definition codac2_Interval_impl.h:277
Interval & inflate(const double &rad)
Adds [-rad,+rad] to this.
Definition codac2_Interval_impl.h:271
static Interval one()
Provides an interval for .
Definition codac2_Interval_impl.h:561
Interval & init_from_list(const std::list< double > &l)
Sets the bounds as the hull of a list of values.
Definition codac2_Interval_impl.h:66
double ub() const
Returns the upper bound of this.
Definition codac2_Interval_impl.h:113
bool intersects(const Interval &x) const
Tests if this and x intersect.
Definition codac2_Interval_impl.h:223
double volume() const
Returns the diameter of this.
Definition codac2_Interval_impl.h:178
bool interior_contains(const double &x) const
Tests if the interior of this contains x.
Definition codac2_Interval_impl.h:203
static Interval pi()
Provides an interval for .
Definition codac2_Interval_impl.h:571
double rand() const
Returns a random value inside the interval.
Definition codac2_Interval_impl.h:135
std::pair< Interval, Interval > bisect(float ratio=0.49) const
Bisects this into two subintervals.
Definition codac2_Interval_impl.h:285
bool is_integer() const
Tests if this is an integer, that is, in the form of where n is an integer.
Definition codac2_Interval_impl.h:218
friend std::ostream & operator<<(std::ostream &os, const Interval &x)
Streams out this.
Definition codac2_Interval_impl.h:581
std::vector< Interval > diff(const Interval &y, bool compactness=true) const
Computes the result of .
Definition codac2_Interval_impl.h:337
bool is_strict_subset(const Interval &x) const
Tests if this is a subset of x and not x itself.
Definition codac2_Interval_impl.h:243
bool is_interior_subset(const Interval &x) const
Tests if this is in the interior of x.
Definition codac2_Interval_impl.h:248
bool is_degenerated() const
Tests if this is degenerated, that is, in the form of .
Definition codac2_Interval_impl.h:213
std::vector< Interval > complementary(bool compactness=true) const
Computes the complementary of this.
Definition codac2_Interval_impl.h:321
Interval & operator/=(double x)
Self division of this and a real x.
Definition codac2_Interval_impl.h:536
double diam() const
Returns the diameter of this.
Definition codac2_Interval_impl.h:165
Interval & operator+=(double x)
Self addition of this and a real x.
Definition codac2_Interval_impl.h:486
bool is_disjoint(const Interval &x) const
Tests if this and x do not intersect.
Definition codac2_Interval_impl.h:228
Interval & operator&=(const Interval &x)
Self intersection of this and x.
Definition codac2_Interval_impl.h:480
static Interval zero()
Provides an interval for .
Definition codac2_Interval_impl.h:556
Index size() const
Returns the dimension of this (which is always )
Definition codac2_Interval_impl.h:183
bool is_superset(const Interval &x) const
Tests if this is a superset of x.
Definition codac2_Interval_impl.h:261
double rad() const
Returns the radius of this.
Definition codac2_Interval_impl.h:148
double mag() const
Returns the magnitude of this i.e. max(|lower bound|, |upper bound|).
Definition codac2_Interval_impl.h:125
bool overlaps(const Interval &x) const
Tests if this and x intersect and their intersection has a non-null volume.
Definition codac2_Interval_impl.h:233
void set_empty()
Sets this interval to the empty set.
Definition codac2_Interval_impl.h:188
bool is_subset(const Interval &x) const
Tests if this is a subset of x.
Definition codac2_Interval_impl.h:238
Interval & operator|=(const Interval &x)
Self union of this and x.
Definition codac2_Interval_impl.h:474
Interval & operator=(double x)
Sets this to x.
Definition codac2_Interval_impl.h:82
Interval()
Creates an interval .
Definition codac2_Interval_impl.h:21
bool contains(const double &x) const
Tests if this contains x.
Definition codac2_Interval_impl.h:198
bool operator!=(const Interval &x) const
Comparison (non equality) between two intervals.
Definition codac2_Interval_impl.h:103
double lb() const
Returns the lower bound of this.
Definition codac2_Interval_impl.h:108
static Interval two_pi()
Provides an interval for .
Definition codac2_Interval_impl.h:576
bool is_strict_interior_subset(const Interval &x) const
Tests if this is in the interior of x and different from x.
Definition codac2_Interval_impl.h:253
Interval & init()
Sets the value of this interval to [-oo,oo].
Definition codac2_Interval_impl.h:54
bool is_strict_superset(const Interval &x) const
Tests if this is a superset of x and different from x.
Definition codac2_Interval_impl.h:266
double mid() const
Returns the midpoint of this.
Definition codac2_Interval_impl.h:118
static Interval empty()
Provides an empty interval.
Definition codac2_Interval_impl.h:551
Interval operator-() const
Substraction of this.
Definition codac2_Interval_impl.h:501
static Interval half_pi()
Provides an interval for .
Definition codac2_Interval_impl.h:566
Ellipsoid operator+(const Ellipsoid &e1, const Ellipsoid &e2)
Compute the Minkowski sum of two ellipsoids.
Interval operator*(const Interval &x, double y)
Returns with .
Definition codac2_Interval_impl.h:428
Interval operator/(const Interval &x, double y)
Returns with .
Definition codac2_Interval_impl.h:451
Interval operator-(const Interval &x, double y)
Returns with .
Definition codac2_Interval_impl.h:405