codac 1.5.6
Loading...
Searching...
No Matches
codac2_Interval.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <list>
18#include <array>
19#include <3rd/gaol/gaol_interval.h>
20#include "codac2_Index.h"
21#include "codac2_Domain.h"
22#include "codac2_assert.h"
23#include "codac2_TypeInfo.h"
24
25namespace codac2
26{
27 const double oo = []() {
28
29 // (from IBEX lib, main author: Gilles Chabert)
30 // We use Gaol not in PRESERVE_ROUNDING mode, thus
31 // assuming the rounding mode is always set upward.
32 // Calling this function in the initialization of
33 // the 'oo' constant should be enough as this constant
34 // is initialized before the first Codac function call occurs.
35 gaol::round_upward();
36
37 return std::numeric_limits<double>::infinity();
38 }();
39
40 class Interval;
41
42 template<>
43 struct is_interval_based<Interval> : std::true_type {};
44
45 template<>
46 struct is_ctc<Interval> : std::false_type {};
47
48 template<>
49 struct is_sep<Interval> : std::false_type {};
50
61 class Interval : protected gaol::interval, public DomainInterface<Interval,double>
62 {
63 public:
64
65 using DegeneratedType = double;
66
70 Interval();
71
77 Interval(double a);
78
85 Interval(double a, double b);
86
92 Interval(const Interval& x);
93
99 explicit Interval(const std::array<double,1>& array);
100
106 explicit Interval(const std::array<double,2>& array);
107
113 Interval(std::initializer_list<double> l);
114
122 Interval& init();
123
132 Interval& init(const Interval& x);
133
142 Interval& init_from_list(const std::list<double>& l);
143
150 Interval& operator=(double x);
151
158 Interval& operator=(const Interval& x);
159
166 bool operator==(const Interval& x) const;
167
174 bool operator!=(const Interval& x) const;
175
181 double lb() const;
182
188 double ub() const;
189
205 double mid() const;
206
213 double mag() const;
214
224 double mig() const;
225
237 double rand() const;
238
244 double rad() const;
245
251 double diam() const;
252
260 double volume() const;
261
269 Index size() const;
270
274 void set_empty();
275
281 bool is_empty() const;
282
293 bool contains(const double& x) const;
294
301 bool interior_contains(const double& x) const;
302
310 bool is_unbounded() const;
311
320 bool is_degenerated() const;
321
328 bool is_integer() const;
329
336 bool intersects(const Interval &x) const;
337
344 bool is_disjoint(const Interval& x) const;
345
353 bool overlaps(const Interval &x) const;
354
363 bool is_subset(const Interval& x) const;
364
376 bool is_strict_subset(const Interval& x) const;
377
388 bool is_interior_subset(const Interval& x) const;
389
400 bool is_strict_interior_subset(const Interval& x) const;
401
410 bool is_superset(const Interval& x) const;
411
420 bool is_strict_superset(const Interval& x) const;
421
428 Interval& inflate(const double& rad);
429
437 bool is_bisectable() const;
438
447 std::pair<Interval,Interval> bisect(float ratio = 0.49) const;
448
455 std::vector<Interval> complementary(bool compactness = true) const;
456
464 std::vector<Interval> diff(const Interval& y, bool compactness = true) const;
465
472 Interval& operator|=(const Interval& x);
473
480 Interval& operator&=(const Interval& x);
481
488 Interval& operator+=(double x);
489
496 Interval& operator+=(const Interval& x);
497
503 Interval operator-() const;
504
511 Interval& operator-=(double x);
512
519 Interval& operator-=(const Interval& x);
520
527 Interval& operator*=(double x);
528
535 Interval& operator*=(const Interval& x);
536
543 Interval& operator/=(double x);
544
555 Interval& operator/=(const Interval& x);
556
562 static Interval empty();
563
569 static Interval zero();
570
576 static Interval one();
577
583 static Interval half_pi();
584
590 static Interval pi();
591
597 static Interval two_pi();
598
599 friend std::ostream& operator<<(std::ostream& os, const Interval& x);
600
601 // Getting 'inaccessible' operators from GAOL, required for pybind11
602
603 void* operator new(std::size_t size)
604 {
605 return ::operator new(size);
606 }
607
608 void operator delete(void* ptr)
609 {
610 ::operator delete(ptr);
611 }
612
613 protected:
614
615 Interval(const gaol::interval& x);
616
617 #define _dec_friend_interval2_arithm_op(f) \
618 friend Interval f(double, const Interval&); \
619 friend Interval f(const Interval&, double); \
620 friend Interval f(const Interval&, const Interval&); \
621
622 _dec_friend_interval2_arithm_op(operator&)
623 _dec_friend_interval2_arithm_op(operator|)
624 _dec_friend_interval2_arithm_op(operator+)
625 _dec_friend_interval2_arithm_op(operator-)
626 _dec_friend_interval2_arithm_op(operator*)
627 _dec_friend_interval2_arithm_op(operator/)
628
629 #define _dec_friend_interval2_unary_op(f) \
630 friend Interval f(const Interval&); \
631
632 _dec_friend_interval2_unary_op(sqr)
633 _dec_friend_interval2_unary_op(sqrt)
634 _dec_friend_interval2_unary_op(exp)
635 _dec_friend_interval2_unary_op(log)
636 _dec_friend_interval2_unary_op(cos)
637 _dec_friend_interval2_unary_op(sin)
638 _dec_friend_interval2_unary_op(tan)
639 _dec_friend_interval2_unary_op(acos)
640 _dec_friend_interval2_unary_op(asin)
641 _dec_friend_interval2_unary_op(atan)
642 _dec_friend_interval2_unary_op(cosh)
643 _dec_friend_interval2_unary_op(sinh)
644 _dec_friend_interval2_unary_op(tanh)
645 _dec_friend_interval2_unary_op(acosh)
646 _dec_friend_interval2_unary_op(asinh)
647 _dec_friend_interval2_unary_op(atanh)
648 _dec_friend_interval2_unary_op(abs)
649 _dec_friend_interval2_unary_op(sign)
650 _dec_friend_interval2_unary_op(integer)
651 _dec_friend_interval2_unary_op(floor)
652 _dec_friend_interval2_unary_op(ceil)
653
654 #define _dec_friend_interval2_binary_op(f) \
655 friend Interval f(const Interval&, const Interval&); \
656
657 _dec_friend_interval2_binary_op(max)
658 _dec_friend_interval2_binary_op(min)
659 _dec_friend_interval2_binary_op(atan2)
660
661 friend Interval pow(const Interval&, int);
662 friend Interval pow(const Interval&, double);
663
664 _dec_friend_interval2_binary_op(pow)
665
666 friend Interval root(const Interval&, int);
667 friend Interval chi(const Interval&, const Interval&, const Interval&);
668
669 friend struct AbsOp;
670 friend struct AcosOp;
671 friend struct AddOp;
672 friend struct ChiOp;
673 friend struct DivOp;
674 friend struct MulOp;
675 friend struct SubOp;
676 friend struct AsinOp;
677 friend struct AtanOp;
678 friend struct Atan2Op;
679 friend struct CosOp;
680 friend struct CoshOp;
681 friend struct DetOp;
682 friend struct ExpOp;
683 friend struct LogOp;
684 friend struct PowOp;
685 friend struct SinOp;
686 friend struct SinhOp;
687 friend struct SqrOp;
688 friend struct SqrtOp;
689 friend struct TanOp;
690 friend struct TanhOp;
691 };
692
700 std::ostream& operator<<(std::ostream& os, const Interval& x);
701
708 Interval operator""_i(long double x);
709
710 double previous_float(double x);
711 double next_float(double x);
712
722 Interval operator&(const Interval& x, const Interval& y);
723
733 Interval operator|(const Interval& x, double y);
734
744 Interval operator|(const Interval& x, const Interval& y);
745
754 const Interval& operator+(const Interval& x);
755
763 Interval operator+(const Interval& x, double y);
764
772 Interval operator+(double x, const Interval& y);
773
781 Interval operator+(const Interval& x, const Interval& y);
782
790 Interval operator-(const Interval& x, double y);
791
799 Interval operator-(double x, const Interval& y);
800
808 Interval operator-(const Interval& x, const Interval& y);
809
817 Interval operator*(const Interval& x, double y);
818
826 Interval operator*(double x, const Interval& y);
827
835 Interval operator*(const Interval& x, const Interval& y);
836
844 Interval operator/(const Interval& x, double y);
845
853 Interval operator/(double x, const Interval& y);
854
862 Interval operator/(const Interval& x, const Interval& y);
863}
864
865#include "codac2_Interval_impl.h"
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
friend Interval chi(const Interval &, const Interval &, const Interval &)
Return if , if , else.
Definition codac2_Interval_operations_impl.h:299
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
friend Interval pow(const Interval &, double)
Returns , .
Definition codac2_Interval_operations_impl.h:40
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
std::ostream & operator<<(std::ostream &os, const BoolInterval &x)
Streams out a BoolInterval.
Definition codac2_BoolInterval.h:45
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
Interval ceil(const Interval &x)
Returns ceil of .
Definition codac2_Interval_operations_impl.h:294
Interval atan(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:133
Interval max(const Interval &x, const Interval &y)
Returns .
Definition codac2_Interval_operations_impl.h:274
Interval asinh(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:237
Interval atanh(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:257
Interval sqrt(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:26
Interval cosh(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:205
Interval floor(const Interval &x)
Returns floor of .
Definition codac2_Interval_operations_impl.h:289
Interval sinh(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:216
Interval acos(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:119
Interval atan2(const Interval &y, const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:140
Interval log(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:85
Interval cos(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:98
Interval exp(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:78
Interval tanh(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:223
Interval sqr(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:21
Interval root(const Interval &x, int p)
Returns the p-th root: .
Definition codac2_Interval_operations_impl.h:60
Interval abs(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:264
Interval asin(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:126
Interval integer(const Interval &x)
Returns the largest integer interval included in .
Definition codac2_Interval_operations_impl.h:284
Interval min(const Interval &x, const Interval &y)
Returns .
Definition codac2_Interval_operations_impl.h:269
Interval acosh(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:230
Interval sign(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:279
Interval sin(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:105
Interval tan(const Interval &x)
Returns .
Definition codac2_Interval_operations_impl.h:112