codac 2.0.5
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
111 {
112 if(is_empty() || x.is_empty())
113 return BoolInterval::EMPTY;
114 if(this->ub() < x.lb())
115 return BoolInterval::TRUE;
116 if(x.ub() <= this->lb())
117 return BoolInterval::FALSE;
119 }
120
122 {
123 return x < *this;
124 }
125
126 inline double Interval::lb() const
127 {
128 return gaol::interval::left();
129 }
130
131 inline double Interval::ub() const
132 {
133 return gaol::interval::right();
134 }
135
136 inline double Interval::mid() const
137 {
138 double m = gaol::interval::midpoint();
139 gaol::round_upward();
140 return m;
141 }
142
143 inline double Interval::mag() const
144 {
145 return gaol::interval::mag();
146 }
147
148 inline double Interval::mig() const
149 {
150 return gaol::interval::mig();
151 }
152
153 inline double Interval::smag() const
155 return (abs(lb()) > abs(ub())) ? lb() : ub();
156 }
157
158 inline double Interval::smig() const
159 {
160 return gaol::interval::smig();
161 }
162
163 inline double Interval::rand() const
164 {
165 if(is_empty())
166 return std::numeric_limits<double>::quiet_NaN();
167
168 double a = std::max<double>(next_float(-oo),lb());
169 double b = std::min<double>(prev_float(oo),ub());
170 double r = a + (((double)std::rand())/(double)RAND_MAX)*(b-a);
171 // The above operation may result in a floating point outside the bounds,
172 // due to floating-point errors. Such possible error is corrected below:
173 return std::max<double>(lb(),std::min<double>(r,ub()));
174 }
175
176 inline double Interval::rad() const
177 {
178 if(is_empty())
179 return std::numeric_limits<double>::quiet_NaN();
180
181 else if(is_unbounded())
182 return oo;
183
184 else
185 {
186 double t = mid();
187 double t1 = (t-*this).ub();
188 double t2 = (*this-t).ub();
189 return (t1>t2) ? t1 : t2;
190 }
191 }
192
193 inline double Interval::diam() const
194 {
195 if(is_empty())
196 return std::numeric_limits<double>::quiet_NaN();
197
198 else
199 {
200 double d = gaol::interval::width();
201 gaol::round_upward();
202 return d;
203 }
204 }
205
206 inline double Interval::volume() const
207 {
208 return diam();
209 }
210
211 inline Index Interval::size() const
212 {
213 return 1;
214 }
215
217 {
218 *this = Interval::empty();
219 }
220
221 inline bool Interval::is_empty() const
222 {
223 return gaol::interval::is_empty();
224 }
225
226 inline bool Interval::contains(const double& x) const
227 {
228 return gaol::interval::set_contains(x);
229 }
230
231 inline bool Interval::interior_contains(const double& x) const
232 {
233 return !is_empty() && x > lb() && x < ub();
234 }
235
236 inline bool Interval::is_unbounded() const
237 {
238 return !gaol::interval::is_finite();
239 }
240
241 inline bool Interval::is_degenerated() const
242 {
243 return is_empty() || gaol::interval::is_a_double();
244 }
245
246 inline bool Interval::is_integer() const
247 {
248 return gaol::interval::is_an_int();
249 }
250
252 {
253 return trunc(lb()) == lb() && trunc(ub()) == ub();
254 }
255
256 inline bool Interval::intersects(const Interval &x) const
257 {
258 return !is_empty() && !x.is_empty() && lb() <= x.ub() && ub() >= x.lb();
259 }
260
261 inline bool Interval::is_disjoint(const Interval& x) const
262 {
263 return is_empty() || x.is_empty() || lb() > x.ub() || ub() < x.lb();
264 }
265
266 inline bool Interval::overlaps(const Interval& x) const
267 {
268 return !is_empty() && !x.is_empty() && ub() > x.lb() && x.ub() > lb();
269 }
270
271 inline bool Interval::is_subset(const Interval& x) const
272 {
273 return is_empty() || (!x.is_empty() && x.lb() <= lb() && x.ub() >= ub());
274 }
275
276 inline bool Interval::is_strict_subset(const Interval& x) const
277 {
278 return !x.is_empty() && (is_empty() || (x.lb() < lb() && x.ub() >= ub()) || (x.ub() > ub() && x.lb() <= lb()));
279 }
280
281 inline bool Interval::is_interior_subset(const Interval& x) const
282 {
283 return is_empty() || (!x.is_empty() && (x.lb() == -oo || x.lb() < lb()) && (x.ub() == oo || x.ub() > ub()));
284 }
285
287 {
288 return !x.is_empty() && (is_empty() || (
289 (x.lb() < lb() && (x.ub() == oo || x.ub() > ub()))
290 || (x.ub() > ub() && (x.lb() == -oo || x.lb() < lb()))
291 ));
292 }
293
294 inline bool Interval::is_superset(const Interval& x) const
295 {
296 return x.is_subset(*this);
297 }
298
299 inline bool Interval::is_strict_superset(const Interval& x) const
300 {
301 return x.is_strict_subset(*this);
302 }
303
304 inline Interval& Interval::inflate(const double& rad)
305 {
306 (*this) += Interval(-rad,rad);
307 return *this;
308 }
309
310 inline bool Interval::is_bisectable() const
311 {
312 if(is_empty())
313 return false;
314 double m = mid();
315 return lb() < m && m < ub();
316 }
317
318 inline std::pair<Interval,Interval> Interval::bisect(float ratio) const
319 {
320 assert_release(is_bisectable());
321 assert_release(Interval(0,1).interior_contains(ratio));
322
323 if(lb() == -oo)
324 {
325 if(ub() == oo)
326 return { Interval(-oo,0), Interval(0,oo) };
327 else
328 return { Interval(-oo,-std::numeric_limits<double>::max()), Interval(-std::numeric_limits<double>::max(),ub()) };
329 }
330
331 else if(ub() == oo)
332 return { Interval(lb(),std::numeric_limits<double>::max()), Interval(std::numeric_limits<double>::max(),oo) };
333
334 else
335 {
336 double m;
337
338 if(ratio == 0.5)
339 m = mid();
340
341 else
342 {
343 m = lb() + ratio*diam();
344 if(m >= ub())
345 m = next_float(lb());
346
347 assert(m < ub());
348 }
349
350 return { Interval(lb(),m), Interval(m,ub()) };
351 }
352 }
353
354 inline std::vector<Interval> Interval::complementary(bool compactness) const
355 {
356 if(is_empty() || (compactness && is_degenerated()))
357 return { {-oo,oo} };
358
359 std::vector<Interval> l;
360
361 if(lb() > -oo)
362 l.push_back({-oo,lb()});
363
364 if(ub() < oo)
365 l.push_back({ub(),oo});
366
367 return l;
368 }
369
370 inline std::vector<Interval> Interval::diff(const Interval& y, bool compactness) const
371 {
372 if(compactness && is_degenerated())
373 {
374 if(is_empty() || y.contains(lb()))
375 return {};
376 else
377 return { *this };
378 }
379
380 std::vector<Interval> l;
381 for(const auto& li : y.complementary(compactness))
382 {
383 Interval inter = li & *this;
384 if(!inter.is_degenerated())
385 l.push_back(inter);
386 }
387
388 return l;
389 }
390
391 inline Interval operator&(const Interval& x, const Interval& y)
392 {
393 if(x.is_empty() || y.is_empty() || x.ub() < y.lb())
394 return Interval::empty();
395
396 else
397 return gaol::operator&(x,y);
398 }
399
400 inline Interval operator|(const Interval& x, double y)
401 {
402 return gaol::operator|(x,Interval(y));
403 }
404
405 inline Interval operator|(const Interval& x, const Interval& y)
406 {
407 return gaol::operator|(x,y);
408 }
409
410 inline const Interval& operator+(const Interval& x)
411 {
412 return x;
413 }
414
415 inline Interval operator+(const Interval& x, double y)
416 {
417 if(y == -oo || y == oo)
418 return Interval::empty();
419
420 else
421 return gaol::operator+(x,y);
422 }
423
424 inline Interval operator+(double x, const Interval& y)
425 {
426 if(x == -oo || x == oo)
427 return Interval::empty();
428
429 else
430 return gaol::operator+(x,y);
431 }
432
433 inline Interval operator+(const Interval& x, const Interval& y)
434 {
435 return gaol::operator+(x,y);
436 }
437
438 inline Interval operator-(const Interval& x, double y)
439 {
440 if(y == -oo || y == oo)
441 return Interval::empty();
442
443 else
444 return gaol::operator-(x, y);
445 }
446
447 inline Interval operator-(double x, const Interval& y)
448 {
449 if(x == -oo || x == oo)
450 return Interval::empty();
451
452 else
453 return gaol::operator-(x, y);
454 }
455
456 inline Interval operator-(const Interval& x, const Interval& y)
457 {
458 return gaol::operator-(x, y);
459 }
460
461 inline Interval operator*(const Interval& x, double y)
462 {
463 if(y == -oo || y == oo)
464 return Interval::empty();
465
466 else
467 return gaol::operator*(x,y);
468 }
469
470 inline Interval operator*(double x, const Interval& y)
471 {
472 if(x == -oo || x == oo)
473 return Interval::empty();
474
475 else
476 return gaol::operator*(x,y);
477 }
478
479 inline Interval operator*(const Interval& x, const Interval& y)
480 {
481 return gaol::operator*(x,y);
482 }
483
484 inline Interval operator/(const Interval& x, double y)
485 {
486 if(y == -oo || y == oo)
487 return Interval::empty();
488
489 else
490 return gaol::operator/(x,y);
491 }
492
493 inline Interval operator/(double x, const Interval& y)
494 {
495 if(x == -oo || x == oo)
496 return Interval::empty();
497
498 else
499 return gaol::operator/(x,y);
500 }
501
502 inline Interval operator/(const Interval& x, const Interval& y)
503 {
504 return gaol::operator/(x,y);
505 }
506
508 {
509 gaol::interval::operator|=(x);
510 return *this;
511 }
512
514 {
515 gaol::interval::operator&=(x);
516 return *this;
517 }
518
520 {
521 if(x == -oo || x == oo)
522 set_empty();
523 else
524 gaol::interval::operator+=(x);
525 return *this;
526 }
527
529 {
530 gaol::interval::operator+=(x);
531 return *this;
532 }
533
535 {
536 return 0.-*this;
537 }
538
540 {
541 if(x == -oo || x == oo)
542 set_empty();
543 else
544 gaol::interval::operator-=(x);
545 return *this;
546 }
547
549 {
550 gaol::interval::operator-=(x);
551 return *this;
552 }
553
555 {
556 if(x == -oo || x == oo)
557 set_empty();
558 else
559 gaol::interval::operator*=(x);
560 return *this;
561 }
562
564 {
565 gaol::interval::operator*=(x);
566 return *this;
567 }
568
570 {
571 if(x == -oo || x == oo)
572 set_empty();
573 else
574 gaol::interval::operator/=(x);
575 return *this;
576 }
577
579 {
580 gaol::interval::operator/=(x);
581 return *this;
582 }
583
585 {
586 return std::numeric_limits<double>::quiet_NaN();
587 }
588
590 {
591 return gaol::interval::zero();
592 }
593
595 {
596 return gaol::interval::one();
597 }
598
600 {
601 return gaol::interval::half_pi();
602 }
603
605 {
606 return gaol::interval::pi();
607 }
608
610 {
611 return gaol::interval::two_pi();
612 }
613
614 inline std::ostream& operator<<(std::ostream& os, const Interval& x)
615 {
616 gaol::interval::precision(os.precision());
617 gaol::operator<<(os,x);
618 return os;
619 }
620
621 inline Interval::Interval(const gaol::interval& x)
622 : gaol::interval(x)
623 { }
624
625 inline Interval operator""_i(long double x)
626 {
627 return Interval(x);
628 }
629
630 inline double prev_float(double x)
631 {
632 return gaol::previous_float(x);
633 }
634
635 inline double next_float(double x)
636 {
637 return gaol::next_float(x);
638 }
639}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:50
double smig() const
Returns the signed mignitude of this.
Definition codac2_Interval_impl.h:158
bool is_unbounded() const
Tests if one of the bounds of this is infinite.
Definition codac2_Interval_impl.h:236
Interval & operator*=(double x)
Self multiplication of this and a real x.
Definition codac2_Interval_impl.h:554
Interval & operator-=(double x)
Self substraction of this and a real x.
Definition codac2_Interval_impl.h:539
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:221
double mig() const
Returns the mignitude of this.
Definition codac2_Interval_impl.h:148
bool is_bisectable() const
Tests if this can be bisected into two non-degenerated intervals.
Definition codac2_Interval_impl.h:310
Interval & inflate(const double &rad)
Adds [-rad,+rad] to this.
Definition codac2_Interval_impl.h:304
static Interval one()
Provides an interval for .
Definition codac2_Interval_impl.h:594
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:131
bool intersects(const Interval &x) const
Tests if this and x intersect.
Definition codac2_Interval_impl.h:256
double volume() const
Returns the diameter of this.
Definition codac2_Interval_impl.h:206
bool interior_contains(const double &x) const
Tests if the interior of this contains x.
Definition codac2_Interval_impl.h:231
static Interval pi()
Provides an interval for .
Definition codac2_Interval_impl.h:604
double rand() const
Returns a random value inside the interval.
Definition codac2_Interval_impl.h:163
std::pair< Interval, Interval > bisect(float ratio=0.49) const
Bisects this into two subintervals.
Definition codac2_Interval_impl.h:318
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:246
std::vector< Interval > diff(const Interval &y, bool compactness=true) const
Computes the result of .
Definition codac2_Interval_impl.h:370
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:276
bool is_interior_subset(const Interval &x) const
Tests if this is in the interior of x.
Definition codac2_Interval_impl.h:281
bool is_degenerated() const
Tests if this is degenerated, that is, in the form of .
Definition codac2_Interval_impl.h:241
std::vector< Interval > complementary(bool compactness=true) const
Computes the complementary of this.
Definition codac2_Interval_impl.h:354
Interval & operator/=(double x)
Self division of this and a real x.
Definition codac2_Interval_impl.h:569
double diam() const
Returns the diameter of this.
Definition codac2_Interval_impl.h:193
BoolInterval operator<(const Interval &x) const
Comparison (strict less-than) between this and x.
Definition codac2_Interval_impl.h:110
Interval & operator+=(double x)
Self addition of this and a real x.
Definition codac2_Interval_impl.h:519
bool is_disjoint(const Interval &x) const
Tests if this and x do not intersect.
Definition codac2_Interval_impl.h:261
Interval & operator&=(const Interval &x)
Self intersection of this and x.
Definition codac2_Interval_impl.h:513
static Interval zero()
Provides an interval for .
Definition codac2_Interval_impl.h:589
Index size() const
Returns the dimension of this (which is always ).
Definition codac2_Interval_impl.h:211
bool is_superset(const Interval &x) const
Tests if this is a superset of x.
Definition codac2_Interval_impl.h:294
double rad() const
Returns the radius of this.
Definition codac2_Interval_impl.h:176
double mag() const
Returns the magnitude of this i.e. max(|lower bound|, |upper bound|).
Definition codac2_Interval_impl.h:143
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:266
void set_empty()
Sets this interval to the empty set.
Definition codac2_Interval_impl.h:216
bool is_subset(const Interval &x) const
Tests if this is a subset of x.
Definition codac2_Interval_impl.h:271
Interval & operator|=(const Interval &x)
Self union of this and x.
Definition codac2_Interval_impl.h:507
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:226
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:126
bool has_integer_bounds() const
Checks whether the interval has integer lower and upper bounds.
Definition codac2_Interval_impl.h:251
static Interval two_pi()
Provides an interval for .
Definition codac2_Interval_impl.h:609
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:286
Interval & init()
Sets the value of this interval to [-oo,oo].
Definition codac2_Interval_impl.h:56
BoolInterval operator>(const Interval &x) const
Comparison (strict greater-than) between this and x.
Definition codac2_Interval_impl.h:121
double smag() const
Returns the signed magnitude of this i.e. lower bound if |lower bound|>|upper bound|,...
Definition codac2_Interval_impl.h:153
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:299
double mid() const
Returns the midpoint of this.
Definition codac2_Interval_impl.h:136
static Interval empty()
Provides an empty interval.
Definition codac2_Interval_impl.h:584
Interval operator-() const
Substraction of this.
Definition codac2_Interval_impl.h:534
static Interval half_pi()
Provides an interval for .
Definition codac2_Interval_impl.h:599
Definition codac2_OctaSym.h:21
double prev_float(double x)
Returns the previous representable double-precision floating-point value before x.
Definition codac2_Interval_impl.h:630
Interval operator*(const Interval &x, double y)
Returns with .
Definition codac2_Interval_impl.h:461
Interval operator/(const Interval &x, double y)
Returns with .
Definition codac2_Interval_impl.h:484
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:131
CtcInterType< typenameC1::ContractedTypes >::Ctc operator&(const C1 &c1, const C2 &c2)
Builds an intersection contractor from two contractors.
Definition codac2_CtcInter.h:229
Interval operator-(const Interval &x, double y)
Returns with .
Definition codac2_Interval_impl.h:438
BoolInterval
Enumeration representing a boolean interval.
Definition codac2_BoolInterval.h:26
@ UNKNOWN
Definition codac2_BoolInterval.h:32
@ EMPTY
Definition codac2_BoolInterval.h:30
double next_float(double x)
Returns the next representable double-precision floating-point value after x.
Definition codac2_Interval_impl.h:635
Interval abs(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:264