00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00028 #ifndef _INTERVAL_FILIB_H_
00029 #define _INTERVAL_FILIB_H_
00030
00031 #define FILIB_USE_MACROS 0
00032 #define FILIB_EXTENDED 1
00033
00034 #include <iostream>
00035
00036 #if FILIB_USE_MACROS
00037 #include <Interval.h>
00038 #else
00039 #include <interval/interval.hpp>
00040 #endif
00041
00042 namespace coco
00043 {
00044
00045 #define coconut_init_interval() filib::fp_traits<double>::setup()
00046
00047
00048 class interval;
00049
00051
00055 struct interval_st
00056 {
00057 double l, u;
00058
00059 interval_st& operator=(const interval& __i);
00060 };
00061
00063
00069 #if FILIB_USE_MACROS
00070 class interval : public Interval
00071 #else
00072 class interval : protected filib::interval<>
00073 #endif
00074 {
00075 private:
00076 #if FILIB_USE_MACROS
00077
00078 typedef Interval __Base;
00079 #else
00080
00081 typedef filib::interval<> __Base;
00082 #endif
00083
00084 public:
00086 interval(double lo, double up) : __Base(lo, up) {}
00088 interval(double d = 0) : __Base(d) {}
00090 interval(int d) : __Base((double) d) {}
00092 interval(unsigned d) : __Base((double) d) {}
00094 interval(long d) : __Base((double) d) {}
00096 interval(unsigned long d) : __Base((double) d) {}
00098 explicit interval(const __Base& x) : __Base(x) {}
00100 interval(const interval& x) : __Base(x) {}
00102 interval(const interval_st& x) : __Base(x.l, x.u) {}
00103
00105 void set(double lo, double up)
00106 {
00107 *this = interval(lo, up);
00108 }
00109
00111 double sup() const { return __Base::sup(); }
00113 double inf() const { return __Base::inf(); }
00114
00116 __Base base() const {return *this;}
00118 static interval EMPTY() {return interval(__Base::EMPTY());}
00119
00121 bool empty() const {return __Base::isEmpty();}
00123 bool is_empty() const {return __Base::isEmpty();}
00126 bool is_thin() const {return __Base::isPoint();}
00129 bool is_unbounded_below() const {return inf() == -COCO_INF;}
00132 bool is_unbounded_above() const {return sup() == COCO_INF;}
00134 bool is_entire() const {return inf() == -COCO_INF && sup() == COCO_INF;}
00136 bool is_bounded() const
00137 {return !is_unbounded_below() && !is_unbounded_above();}
00139 bool subset (const interval& x) const
00140 {return __Base::subset(x);}
00142 bool superset (const interval& x) const
00143 {return __Base::superset(x);}
00145 bool proper_subset (const interval& x) const
00146 {return __Base::proper_subset(x);}
00148 bool proper_superset (const interval& x) const
00149 {return __Base::proper_superset(x);}
00151 bool disjoint (const interval& x) const
00152 {return __Base::disjoint(x);}
00153
00154
00156 double rel_width() const;
00157
00159 interval& intersectwith(const interval& x)
00160 {
00161 return *this = __Base::intersect(x);
00162 }
00163
00165 interval& hullwith(const interval& x)
00166 {
00167 return *this = __Base::hull(x);
00168 }
00169
00172 interval& hulldiffwith(const interval& x)
00173 {
00174 if(subset(x))
00175 *this = EMPTY();
00176 else if(!superset(x))
00177 {
00178 if(x.contains(inf()))
00179 *this = interval(x.sup(), sup());
00180 else
00181 *this = interval(inf(), x.inf());
00182 }
00183 return *this;
00184 }
00185
00187 interval intersect(const interval& x) const
00188 {
00189 return interval(__Base::intersect(x));
00190 }
00191
00193 interval hull(const interval& x) const
00194 {
00195 return interval(__Base::hull(x));
00196 }
00197
00200 interval hulldiff(const interval& x) const
00201 {
00202 if(subset(x))
00203 return EMPTY();
00204 else if(!superset(x))
00205 {
00206 if(x.contains(inf()))
00207 return interval(x.sup(), sup());
00208 else
00209 return interval(inf(), x.inf());
00210 }
00211 return *this;
00212 }
00213
00215 bool contains(const interval& x) const
00216 {
00217 if(inf() <= x.inf() && sup() >= x.sup())
00218 return true;
00219 else
00220 return false;
00221 }
00222
00224 void setpair(double& lo, double& up) const {lo = inf(); up = sup();}
00226 void get_bounds(double& a, double& b) const {a = inf(); b = sup();}
00228 void set_bounds(double a, double b) {INF = a; SUP = b;}
00230 void set_lb(double d) {INF = d;}
00232 void set_ub(double d) {SUP = d;}
00233
00235
00236 interval& operator=(double d) {INF = SUP = d; return *this;}
00237 interval& operator=(int d) {INF = SUP = d; return *this;}
00238 interval& operator=(unsigned d) {INF = SUP = d; return *this;}
00239 interval& operator=(long d) {INF = SUP = d; return *this;}
00240 interval& operator=(unsigned long d) {INF = SUP = d; return *this;}
00242
00243 interval& operator=(const __Base& _x)
00244 {INF = _x.inf(); SUP = _x.sup(); return *this;}
00245
00247 interval operator-() const {return interval(__Base::operator-());}
00248
00250 interval& ipow(int n);
00252 interval& imin(const interval& x);
00254 interval& imax(const interval& x);
00255
00257 interval& round_to_integer();
00258
00261 interval& intersect_div(const interval& __i, const interval& __j);
00264 interval& intersect_power(const interval& __i, int __n);
00268 interval& intersect_invsqr_wc(const interval& __i, double __l, double __d);
00272 interval& intersect_invpower_wc(const interval& __i, double __l, int __n);
00276 interval& intersect_invsin_wc(const interval& __i, double __l, double __d);
00280 interval& intersect_invcos_wc(const interval& __i, double __l, double __d);
00284 interval& intersect_invgauss_wc(const interval& __i, double __l, double __m,
00285 double __s);
00286
00289 double project(double __d) const;
00290
00291
00292
00293
00294
00298 interval& operator+=(const interval& a);
00299
00303 interval& operator+=(double a);
00304
00308 interval& operator-=(const interval& a);
00309
00313 interval& operator-=(double a);
00314
00318 interval& operator*=(const interval& a);
00319
00323 interval& operator*=(double a);
00324
00328 interval& operator/=(const interval& a);
00329
00333 interval& operator/=(double a);
00334
00348 double mid() const;
00349
00351
00362 double diam() const;
00363 double width() const;
00365
00381 double relDiam() const;
00382
00394 double rad() const;
00395
00406 double mig() const;
00407
00419 double mag() const;
00420
00432 double dist(const interval& x) const;
00433
00447 double safeguarded_mid() const;
00448
00451 static int const & precision();
00453 static int precision(int const & p);
00454
00456 friend bool operator==(const interval& __i, const interval& __d);
00457 template <class _TC>
00458 friend bool operator==(const interval& __i, const _TC& __d);
00459 friend bool operator!=(const interval& __i, const interval& __d);
00460 template <class _TC>
00461 friend bool operator!=(const interval& __i, const _TC& __d);
00462 friend std::ostream& operator<<(std::ostream&, const interval&);
00463
00464 friend interval tanh(const interval& x);
00465 friend interval tan(const interval& x);
00466 friend interval sqrt(const interval& x);
00467 friend interval sqr(const interval& x);
00468 friend interval sinh(const interval& x);
00469 friend interval sin(const interval& x);
00470 friend interval pow(const interval& x, const interval& y);
00471 friend interval power(const interval& x, int n);
00472 friend interval log2(const interval& x);
00473 friend interval log1p(const interval& x);
00474 friend interval log10(const interval& x);
00475 friend interval log(const interval& x);
00476 friend interval expm1(const interval& x);
00477 friend interval exp2(const interval& x);
00478 friend interval exp10(const interval& x);
00479 friend interval exp(const interval& x);
00480 friend interval coth(const interval& x);
00481 friend interval cot(const interval& x);
00482 friend interval cosh(const interval& x);
00483 friend interval cos(const interval& x);
00484 friend interval atanh(const interval& x);
00485 friend interval atan(const interval& x);
00486 friend interval asinh(const interval& x);
00487 friend interval asin(const interval& x);
00488 friend interval acoth(const interval& x);
00489 friend interval acot(const interval& x);
00490 friend interval acosh(const interval& x);
00491 friend interval abs(const interval& x);
00492 friend interval acos(const interval& x);
00493 friend double dist(const interval& x, const interval& y);
00494 friend double mag(const interval&);
00495 friend double mig(const interval&);
00496 friend double rad(const interval&);
00497 friend double relDiam(const interval&);
00498 friend double width(const interval&);
00499 friend double diam(const interval&);
00500 friend double mid(const interval&);
00501 friend interval operator/(double a, const interval& b);
00502 friend interval operator/(const interval& a, double b);
00503 friend interval operator/(const interval& a, const interval& b);
00504 friend interval operator*(double a, const interval& b);
00505 friend interval operator*(const interval& a, double b);
00506 friend interval operator*(const interval& a, const interval& b);
00507 friend interval cancel(const interval& a, const interval& b);
00508 friend interval operator-(const interval& a, const interval& b);
00509 friend interval operator-(const interval& a, double b);
00510 friend interval operator-(double a, const interval& b);
00511 friend interval operator+(const interval& a, const interval& b);
00512 friend interval operator+(const interval& a, double b);
00513 friend interval operator+(double b, const interval& a);
00514 friend double upward_divides(const double& a, const double& b);
00515 friend double downward_divides(const double& a, const double& b);
00516 friend double upward_multiplies(const double& a, const double& b);
00517 friend double downward_multiplies(const double& a, const double& b);
00518 friend double upward_minus(const double& a, const double& b);
00519 friend double downward_minus(const double& a, const double& b);
00520 friend double upward_plus(const double& a, const double& b);
00521 friend double downward_plus(const double& a, const double& b);
00522 friend interval imin(const interval& x, const interval& y);
00523 friend interval imax(const interval& x, const interval& y);
00524 };
00525
00526
00527
00528
00529
00530
00531
00533 inline double safeguarded_mid(const interval& __i)
00534 {return __i.safeguarded_mid();}
00537 interval ipow(const interval& x, int n);
00539 interval gauss(const interval& x);
00542 interval atan2(const interval& y, const interval& x);
00544 double absmin(const interval& __i);
00547 double gainfactor(const interval& _old, const interval& _new);
00548
00550
00551 bool operator==(const interval& __i, const interval& __d);
00552 template <class _TC>
00553 bool operator==(const interval& __i, const _TC& __d);
00555
00557
00558 bool operator!=(const interval& __i, const interval& __d);
00559 template <class _TC>
00560 bool operator!=(const interval& __i, const _TC& __d);
00562
00563
00564
00565
00566
00567
00569 double downward_plus(const double& a, const double& b);
00571 double upward_plus(const double& a, const double& b);
00573 double downward_minus(const double& a, const double& b);
00575 double upward_minus(const double& a, const double& b);
00577 double downward_multiplies(const double& a, const double& b);
00579 double upward_multiplies(const double& a, const double& b);
00581 double downward_divides(const double& a, const double& b);
00583 double upward_divides(const double& a, const double& b);
00584
00586
00589 inline bool operator<(const interval& a, const interval& b){return a.sup() < b.inf();}
00590 inline bool operator<(const interval& a, double b){return a.sup() < b;}
00591 inline bool operator<(double a, const interval& b){return a < b.inf();}
00593
00595
00596 interval operator+(const interval& a, const interval& b);
00597 interval operator+(const interval& a, double b);
00598 interval operator+(double b, const interval& a);
00600
00601
00602 interval operator-(const interval& a, const interval& b);
00603 interval operator-(const interval& a, double b);
00604 interval operator-(double a, const interval& b);
00606 interval cancel(const interval& a, const interval& b);
00608
00609 interval operator*(const interval& a, const interval& b);
00610 interval operator*(const interval& a, double b);
00611 interval operator*(double a, const interval& b);
00613
00614
00615 interval operator/(const interval& a, const interval& b);
00616 interval operator/(const interval& a, double b);
00617 interval operator/(double a, const interval& b);
00619
00621
00636 interval division_part1(const interval& x, const interval& y, bool& b);
00637
00638 interval division_part2(const interval& x, const interval& y, bool b = true);
00640
00644 double mid(const interval&);
00648 double diam(const interval&);
00649 double width(const interval&);
00653 double relDiam(const interval&);
00657 double rad(const interval&);
00661 double mig(const interval&);
00665 double mag(const interval&);
00669 double dist(const interval& x, const interval& y);
00670
00674 interval round_to_integer(const interval& x);
00675
00679 interval acos(const interval& x);
00680
00684 interval abs(const interval& x);
00685
00690 interval acosh(const interval& x);
00691
00696 interval acot(const interval& x);
00697
00702 interval acoth(const interval& x);
00703
00707 interval asin(const interval& x);
00708
00713 interval asinh(const interval& x);
00714
00718 interval atan(const interval& x);
00719
00724 interval atanh(const interval& x);
00725
00729 interval cos(const interval& x);
00730
00734 interval cosh(const interval& x);
00735
00739 interval cot(const interval& x);
00740
00745 interval coth(const interval& x);
00746
00750 interval exp(const interval& x);
00751
00756 interval exp10(const interval& x);
00757
00762 interval exp2(const interval& x);
00763
00767 interval expm1(const interval& x);
00768
00773 interval log(const interval& x);
00774
00779 interval log10(const interval& x);
00780
00784 interval log1p(const interval& x);
00785
00790 interval log2(const interval& x);
00791
00795 interval power(const interval& x, int n);
00796
00800 interval pow(const interval& x, const interval& y);
00801
00805 interval sin(const interval& x);
00806
00810 interval sinh(const interval& x);
00811
00815 interval sqr(const interval& x);
00816
00820 interval sqrt(const interval& x);
00821
00825 interval tan(const interval& x);
00826
00831 interval tanh(const interval& x);
00832
00837 interval imax(const interval& x, const interval& y);
00838
00843 interval imin(const interval& x, const interval& y);
00844
00845
00846
00847
00848
00849
00850 inline
00851 interval_st& interval_st::operator=(const interval& x)
00852 {
00853 l = x.inf();
00854 u = x.sup();
00855 return *this;
00856 }
00857
00858
00859
00860 inline
00861 interval ipow(const interval& x, int n)
00862 {
00863 return power(x, n);
00864 }
00865
00866
00867 inline
00868 interval gauss(const interval& x)
00869 {
00870 return exp(-sqr(x));
00871 }
00872
00873
00874 inline
00875 interval atan2(const interval& y, const interval& x)
00876 {
00877 return atan(y/x);
00878 }
00879
00880
00881 inline
00882 double absmin(const interval& __i)
00883 {
00884 if(__i.contains(0.))
00885 return 0.;
00886 else if(__i.inf() > 0)
00887 return __i.inf();
00888 else
00889 return __i.sup();
00890 }
00891
00892 inline
00893 interval intersect(const interval& __x, const interval& __y)
00894 {
00895 return __x.intersect(__y);
00896 }
00897
00898 inline
00899 interval hull(const interval& __x, const interval& __y)
00900 {
00901 return __x.hull(__y);
00902 }
00903
00904 inline
00905 interval hulldiff(const interval& __x, const interval& __y)
00906 {
00907 return __x.hulldiff(__y);
00908 }
00909
00910 inline
00911 double gainfactor(const interval& _old, const interval& _new)
00912 {
00913 if(_old.is_thin())
00914 return 1.;
00915 if(_old.is_bounded() && _new.is_bounded())
00916 return _new.width()/_old.width();
00917 if((_new.sup() != COCO_INF && _old.sup() == COCO_INF) ||
00918 (_new.inf() != -COCO_INF && _old.inf() == -COCO_INF))
00919 return 0.;
00920 return 1.;
00921 }
00922
00923 inline
00924 bool operator==(const interval& __i, const interval& __d)
00925 {
00926 return filib::operator==(__i, __d);
00927 }
00928
00929 template <class _TC>
00930 inline
00931 bool operator==(const interval& __i, const _TC& __d)
00932 {
00933 return filib::operator==(__i, interval(__d));
00934 }
00935
00936 inline
00937 bool operator!=(const interval& __i, const interval& __d)
00938 {
00939 return filib::operator!=(__i, __d);
00940 }
00941
00942
00943 template <class _TC>
00944 inline
00945 bool operator!=(const interval& __i, const _TC& __d)
00946 {
00947 return filib::operator!=(__i, interval(__d));
00948 }
00949
00950
00951
00952 inline
00953 double interval::project(double __d) const
00954 {
00955 if(__d < inf())
00956 return inf();
00957 else if(__d > sup())
00958 return sup();
00959 else
00960 return __d;
00961 }
00962
00963 inline std::ostream& operator<<(std::ostream& o, const interval& i)
00964 {
00965 return o << filib::interval<>(i);
00966 }
00967
00968 }
00969
00970
00971 #endif // _COCONUT_INTERVAL_H_