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 = x;
57 return *this;
58 }
59
60 inline Interval& Interval::init_from_list(const std::list<double>& l)
61 {
62 if(l.size() == 1)
63 *this = Interval(*l.begin());
64
65 else if(l.size() == 2)
66 *this = Interval(*l.begin(),*std::prev(l.end()));
67
68 else
69 {
70 assert_release("'Interval' can only be defined by one or two 'double' values.");
71 }
72
73 return *this;
74 }
75
76 inline Interval& Interval::operator=(double x)
77 {
78 if(x == -oo || x == oo)
79 set_empty();
80 else
81 gaol::interval::operator=(x);
82
83 return *this;
84 }
85
87 {
88 gaol::interval::operator=(x);
89 return *this;
90 }
91
92 inline bool Interval::operator==(const Interval& x) const
93 {
94 return (is_empty() && x.is_empty()) || (lb() == x.lb() && ub() == x.ub());
95 }
96
97 inline bool Interval::operator!=(const Interval& x) const
98 {
99 return !(*this == x);
100 }
101
102 inline double Interval::lb() const
103 {
104 return gaol::interval::left();
105 }
106
107 inline double Interval::ub() const
108 {
109 return gaol::interval::right();
110 }
111
112 inline double Interval::mid() const
113 {
114 double m = gaol::interval::midpoint();
115 gaol::round_upward();
116 return m;
117 }
118
119 inline double Interval::mag() const
120 {
121 return gaol::interval::mag();
122 }
123
124 inline double Interval::mig() const
125 {
126 return gaol::interval::mig();
127 }
128
129 inline double Interval::rand() const
130 {
131 if(is_empty())
132 return std::numeric_limits<double>::quiet_NaN();
133
134 double a = std::max<double>(next_float(-oo),lb());
135 double b = std::min<double>(previous_float(oo),ub());
136 double r = a + (((double)std::rand())/(double)RAND_MAX)*(b-a);
137 // The above operation may result in a floating point outside the bounds,
138 // due to floating-point errors. Such possible error is corrected below:
139 return std::max<double>(lb(),std::min<double>(r,ub()));
140 }
141
142 inline double Interval::rad() const
143 {
144 if(is_empty())
145 return std::numeric_limits<double>::quiet_NaN();
146
147 else if(is_unbounded())
148 return oo;
149
150 else
151 {
152 double t = mid();
153 double t1 = (t-*this).ub();
154 double t2 = (*this-t).ub();
155 return (t1>t2) ? t1 : t2;
156 }
157 }
158
159 inline double Interval::diam() const
160 {
161 if(is_empty())
162 return std::numeric_limits<double>::quiet_NaN();
163
164 else
165 {
166 double d = gaol::interval::width();
167 gaol::round_upward();
168 return d;
169 }
170 }
171
172 inline double Interval::volume() const
173 {
174 return diam();
175 }
176
177 inline Index Interval::size() const
178 {
179 return 1;
180 }
181
183 {
184 *this = Interval::empty();
185 }
186
187 inline bool Interval::is_empty() const
188 {
189 return gaol::interval::is_empty();
190 }
191
192 inline bool Interval::contains(const double& x) const
193 {
194 return gaol::interval::set_contains(x);
195 }
196
197 inline bool Interval::interior_contains(const double& x) const
198 {
199 return !is_empty() && x > lb() && x < ub();
200 }
201
202 inline bool Interval::is_unbounded() const
203 {
204 return !gaol::interval::is_finite();
205 }
206
207 inline bool Interval::is_degenerated() const
208 {
209 return is_empty() || gaol::interval::is_a_double();
210 }
211
212 inline bool Interval::intersects(const Interval &x) const
213 {
214 return !is_empty() && !x.is_empty() && lb() <= x.ub() && ub() >= x.lb();
215 }
216
217 inline bool Interval::is_disjoint(const Interval& x) const
218 {
219 return is_empty() || x.is_empty() || lb() > x.ub() || ub() < x.lb();
220 }
221
222 inline bool Interval::overlaps(const Interval& x) const
223 {
224 return !is_empty() && !x.is_empty() && ub() > x.lb() && x.ub() > lb();
225 }
226
227 inline bool Interval::is_subset(const Interval& x) const
228 {
229 return is_empty() || (!x.is_empty() && x.lb() <= lb() && x.ub() >= ub());
230 }
231
232 inline bool Interval::is_strict_subset(const Interval& x) const
233 {
234 return !x.is_empty() && (is_empty() || (x.lb() < lb() && x.ub() >= ub()) || (x.ub() > ub() && x.lb() <= lb()));
235 }
236
237 inline bool Interval::is_interior_subset(const Interval& x) const
238 {
239 return is_empty() || (!x.is_empty() && (x.lb() == -oo || x.lb() < lb()) && (x.ub() == oo || x.ub() > ub()));
240 }
241
243 {
244 return !x.is_empty() && (is_empty() || (
245 (x.lb() < lb() && (x.ub() == oo || x.ub() > ub()))
246 || (x.ub() > ub() && (x.lb() == -oo || x.lb() < lb()))
247 ));
248 }
249
250 inline bool Interval::is_superset(const Interval& x) const
251 {
252 return x.is_subset(*this);
253 }
254
255 inline bool Interval::is_strict_superset(const Interval& x) const
256 {
257 return x.is_strict_subset(*this);
258 }
259
260 inline Interval& Interval::inflate(const double& rad)
261 {
262 (*this) += Interval(-rad,rad);
263 return *this;
264 }
265
266 inline bool Interval::is_bisectable() const
267 {
268 if(is_empty())
269 return false;
270 double m = mid();
271 return lb() < m && m < ub();
272 }
273
274 inline std::pair<Interval,Interval> Interval::bisect(float ratio) const
275 {
276 assert_release(is_bisectable());
277 assert_release(Interval(0,1).interior_contains(ratio));
278
279 if(lb() == -oo)
280 {
281 if(ub() == oo)
282 return { Interval(-oo,0), Interval(0,oo) };
283 else
284 return { Interval(-oo,-std::numeric_limits<double>::max()), Interval(-std::numeric_limits<double>::max(),ub()) };
285 }
286
287 else if(ub() == oo)
288 return { Interval(lb(),std::numeric_limits<double>::max()), Interval(std::numeric_limits<double>::max(),oo) };
289
290 else
291 {
292 double m;
293
294 if(ratio == 0.5)
295 m = mid();
296
297 else
298 {
299 m = lb() + ratio*diam();
300 if(m >= ub())
301 m = next_float(lb());
302
303 assert(m < ub());
304 }
305
306 return { Interval(lb(),m), Interval(m,ub()) };
307 }
308 }
309
310 inline std::vector<Interval> Interval::complementary(bool compactness) const
311 {
312 if(is_empty() || (compactness && is_degenerated()))
313 return { {-oo,oo} };
314
315 std::vector<Interval> l;
316
317 if(lb() > -oo)
318 l.push_back({-oo,lb()});
319
320 if(ub() < oo)
321 l.push_back({ub(),oo});
322
323 return l;
324 }
325
326 inline std::vector<Interval> Interval::diff(const Interval& y, bool compactness) const
327 {
328 if(compactness && is_degenerated())
329 {
330 if(is_empty() || y.contains(lb()))
331 return {};
332 else
333 return { *this };
334 }
335
336 std::vector<Interval> l;
337 for(const auto& li : y.complementary(compactness))
338 {
339 Interval inter = li & *this;
340 if(!inter.is_degenerated())
341 l.push_back(inter);
342 }
343
344 return l;
345 }
346
347 inline Interval operator&(const Interval& x, const Interval& y)
348 {
349 if(x.is_empty() || y.is_empty() || x.ub() < y.lb())
350 return Interval::empty();
351
352 else
353 return gaol::operator&(x,y);
354 }
355
356 inline Interval operator|(const Interval& x, const Interval& y)
357 {
358 return gaol::operator|(x,y);
359 }
360
361 inline const Interval& operator+(const Interval& x)
362 {
363 return x;
364 }
365
366 inline Interval operator+(const Interval& x, double y)
367 {
368 if(y == -oo || y == oo)
369 return Interval::empty();
370
371 else
372 return gaol::operator+(x,y);
373 }
374
375 inline Interval operator+(double x, const Interval& y)
376 {
377 if(x == -oo || x == oo)
378 return Interval::empty();
379
380 else
381 return gaol::operator+(x,y);
382 }
383
384 inline Interval operator+(const Interval& x, const Interval& y)
385 {
386 return gaol::operator+(x,y);
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
459 {
460 gaol::interval::operator|=(x);
461 return *this;
462 }
463
465 {
466 gaol::interval::operator&=(x);
467 return *this;
468 }
469
471 {
472 if(x == -oo || x == oo)
473 set_empty();
474 else
475 gaol::interval::operator+=(x);
476 return *this;
477 }
478
480 {
481 gaol::interval::operator+=(x);
482 return *this;
483 }
484
486 {
487 return 0.-*this;
488 }
489
491 {
492 if(x == -oo || x == oo)
493 set_empty();
494 else
495 gaol::interval::operator-=(x);
496 return *this;
497 }
498
500 {
501 gaol::interval::operator-=(x);
502 return *this;
503 }
504
506 {
507 if(x == -oo || x == oo)
508 set_empty();
509 else
510 gaol::interval::operator*=(x);
511 return *this;
512 }
513
515 {
516 gaol::interval::operator*=(x);
517 return *this;
518 }
519
521 {
522 if(x == -oo || x == oo)
523 set_empty();
524 else
525 gaol::interval::operator/=(x);
526 return *this;
527 }
528
530 {
531 gaol::interval::operator/=(x);
532 return *this;
533 }
534
536 {
537 return std::numeric_limits<double>::quiet_NaN();
538 }
539
541 {
542 return gaol::interval::zero();
543 }
544
546 {
547 return gaol::interval::one();
548 }
549
551 {
552 return gaol::interval::half_pi();
553 }
554
556 {
557 return gaol::interval::pi();
558 }
559
561 {
562 return gaol::interval::two_pi();
563 }
564
565 inline std::ostream& operator<<(std::ostream& os, const Interval& x)
566 {
567 gaol::interval::precision(os.precision());
568 gaol::operator<<(os,x);
569 return os;
570 }
571
572 inline Interval::Interval(const gaol::interval& x)
573 : gaol::interval(x)
574 { }
575
576 inline Interval operator""_i(long double x)
577 {
578 return Interval(x);
579 }
580
581 inline double previous_float(double x)
582 {
583 return gaol::previous_float(x);
584 }
585
586 inline double next_float(double x)
587 {
588 return gaol::next_float(x);
589 }
590}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:62
Interval & init(const Interval &x)
Sets the value of this interval to x.
Definition codac2_Interval_impl.h:54
bool is_unbounded() const
Tests if one of the bounds of this is infinite.
Definition codac2_Interval_impl.h:202
Interval & operator*=(double x)
Self multiplication of this and a real x.
Definition codac2_Interval_impl.h:505
Interval & operator-=(double x)
Self substraction of this and a real x.
Definition codac2_Interval_impl.h:490
bool operator==(const Interval &x) const
Comparison (equality) between two intervals.
Definition codac2_Interval_impl.h:92
bool is_empty() const
Tests if this is empty.
Definition codac2_Interval_impl.h:187
double mig() const
Returns the mignitude of this.
Definition codac2_Interval_impl.h:124
bool is_bisectable() const
Tests if this can be bisected into two non-degenerated intervals.
Definition codac2_Interval_impl.h:266
Interval & inflate(const double &rad)
Adds [-rad,+rad] to this.
Definition codac2_Interval_impl.h:260
static Interval one()
Provides an interval for .
Definition codac2_Interval_impl.h:545
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:60
double ub() const
Returns the upper bound of this.
Definition codac2_Interval_impl.h:107
bool intersects(const Interval &x) const
Tests if this and x intersect.
Definition codac2_Interval_impl.h:212
double volume() const
Returns the diameter of this.
Definition codac2_Interval_impl.h:172
bool interior_contains(const double &x) const
Tests if the interior of this contains x.
Definition codac2_Interval_impl.h:197
static Interval pi()
Provides an interval for .
Definition codac2_Interval_impl.h:555
double rand() const
Returns a random value inside the interval.
Definition codac2_Interval_impl.h:129
std::pair< Interval, Interval > bisect(float ratio=0.49) const
Bisects this into two subintervals.
Definition codac2_Interval_impl.h:274
friend std::ostream & operator<<(std::ostream &os, const Interval &x)
Streams out this.
Definition codac2_Interval_impl.h:565
std::vector< Interval > diff(const Interval &y, bool compactness=true) const
Computes the result of .
Definition codac2_Interval_impl.h:326
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:232
bool is_interior_subset(const Interval &x) const
Tests if this is in the interior of x.
Definition codac2_Interval_impl.h:237
bool is_degenerated() const
Tests if this is degenerated, that is, in the form of .
Definition codac2_Interval_impl.h:207
std::vector< Interval > complementary(bool compactness=true) const
Computes the complementary of this.
Definition codac2_Interval_impl.h:310
Interval & operator/=(double x)
Self division of this and a real x.
Definition codac2_Interval_impl.h:520
double diam() const
Returns the diameter of this.
Definition codac2_Interval_impl.h:159
Interval & operator+=(double x)
Self addition of this and a real x.
Definition codac2_Interval_impl.h:470
bool is_disjoint(const Interval &x) const
Tests if this and x do not intersect.
Definition codac2_Interval_impl.h:217
Interval & operator&=(const Interval &x)
Self intersection of this and x.
Definition codac2_Interval_impl.h:464
static Interval zero()
Provides an interval for .
Definition codac2_Interval_impl.h:540
Index size() const
Returns the dimension of this (which is always )
Definition codac2_Interval_impl.h:177
bool is_superset(const Interval &x) const
Tests if this is a superset of x.
Definition codac2_Interval_impl.h:250
double rad() const
Returns the radius of this.
Definition codac2_Interval_impl.h:142
double mag() const
Returns the magnitude of this i.e. max(|lower bound|, |upper bound|).
Definition codac2_Interval_impl.h:119
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:222
void set_empty()
Sets this interval to the empty set.
Definition codac2_Interval_impl.h:182
bool is_subset(const Interval &x) const
Tests if this is a subset of x.
Definition codac2_Interval_impl.h:227
Interval & operator|=(const Interval &x)
Self union of this and x.
Definition codac2_Interval_impl.h:458
Interval & operator=(double x)
Sets this to x.
Definition codac2_Interval_impl.h:76
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:192
bool operator!=(const Interval &x) const
Comparison (non equality) between two intervals.
Definition codac2_Interval_impl.h:97
double lb() const
Returns the lower bound of this.
Definition codac2_Interval_impl.h:102
static Interval two_pi()
Provides an interval for .
Definition codac2_Interval_impl.h:560
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:242
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:255
double mid() const
Returns the midpoint of this.
Definition codac2_Interval_impl.h:112
static Interval empty()
Provides an empty interval.
Definition codac2_Interval_impl.h:535
Interval operator-() const
Substraction of this.
Definition codac2_Interval_impl.h:485
static Interval half_pi()
Provides an interval for .
Definition codac2_Interval_impl.h:550
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:412
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:389