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