Developer documentation

Interval.hpp
Go to the documentation of this file.
1 #ifndef mmx_interval_hpp
2 #define mmx_interval_hpp
3 
4 #include <cassert>
5 #include <realroot/print.hpp>
6 #include <realroot/texp.hpp>
9 
10 namespace mmx {
11 
12  using namespace texp;
13 
40 namespace numerics { template<class T, int r> struct interval_base; }
41 
43 template<class T, int r = 3 >
44 struct Interval : numerics::interval_base<T,((unsigned)r)%4> {
45  typedef T value_type;
46  typedef T coeff_t;
47  typedef T boundary_type;
49  typedef typename base_t::rnd rnd_t;
51  struct extended { T a,b; extended( const T& c, const T& d ) { a = c; b = d; }; };
52  T m, M;
53  template<class S> Interval( const texp::template_expression<S>& e );
55  Interval();
57  Interval( int n );
59  Interval( unsigned n );
61  Interval( const T& x );
63  Interval( const T& a, const T& b );
65  Interval( const char * s );
66  //Interval& operator=( const char * s ){ let::assign(*this,s); return *this; }
67  Interval& operator=( const Interval & i )
68  {
69  m = i.m;
70  M = i.M;
71  return *this;
72  };
73 
74  template<class X>
75  Interval & operator=( const texp::template_expression<X> & x ) ;
76 
77  void define( const T& m, const T& M ) { this->m = m; this->M = M; };
78  void assign( const T& m, const T& M ) { define(m,M); };
79  void assign( const Interval& b ) { *this = b; };
80 
82  template<class X> bool operator==( const X& k) const { return m == M && M == k ; };
83  template<class X, int r_> bool operator==( const Interval<X,r_> & b ) const
84  { return b.m == m && b.M == M ; };
86  template<class X> inline bool operator>( const X& x ) const { return m > x; };
88  template<class X> inline bool operator>=( const X& x ) const { return m >= x; };
90  template<class X> inline bool operator<( const X& x ) const { return M < x; };
92  template<class X> inline bool operator<=( const X& x ) const { return M <= x; };
94  template<class X> inline bool operator!=(const X& x ) const { return !(*this == x); };
95  template<class X, int _r> bool operator<( const Interval<X,_r>& i ) const { return M < i.m; };
96  template<class X, int _r> bool operator>( const Interval<X,_r>& i ) const { return m > i.M; };
97 
98  const T& lower() const { return m; };
99  const T& upper() const { return M; };
100  T& lower() { return m; };
101  T& upper() { return M; };
102  const T& inf() const { return lower(); };
103  const T& sup() const { return upper(); };
104  T& inf() { return m; };
105  T& sup() { return M; };
106 
107  Interval& operator=( const T& x ) { m = M = x; return *this; };
108  template<class X, int R>
109  Interval& operator=( const Interval<X,R> & x );
110  // template<class X>
111  // Interval& operator+=( const X & x ) { *this = *this + x; return *this; };
112  // template<class X>
113  // Interval& operator-=( const X & x ) { *this = *this - x; return *this; };
114  // template<class X>
115  // Interval& operator*=( const X & x ) { *this = *this * x; return *this; };
116  // template<class X>
117  // Interval& operator/=( const X & x ) { *this = *this / x; return *this; };
118 
119  Interval& operator+=( const Interval & x ) { add(*this,x); return *this; };
120  Interval& operator-=( const Interval & x ) { sub(*this,x); return *this; };
121  Interval& operator*=( const Interval & x ) { mul(*this,x); return *this; };
122  Interval& operator/=( const Interval & x ) { div(*this,x); return *this; };
123 
125  T width() const { return M-m; };
127  T size() const { return width(); };
128  operator T() const {return (lower()+upper())/2; };
130  T center() const { return (M+m)/T(2); };
131  };
132 
133 
134 template<class OSTREAM, class T, int r> inline void
135 print(OSTREAM& os, const Interval<T,r>& a) {
136  //os<<"[";print(os,a.lower());os<<",";mmx::print(os,a.upper());os<<"]";
137 }
138 
139 template<class T, int r>
140 void hull( Interval<T,r>& v, const Interval<T,r>& a, const Interval<T,r>& b );
141 template<class T, int r> inline
142 bool intersect( Interval<T,r>& result,
143  const Interval<T,r>& a, const Interval<T,r>& b );
144 
145 } //end namespace mmx
146 
148 
149 namespace mmx {
150 
151 namespace texp {
152 
153  template<class X, int r> struct kernelof_< Interval<X,r> > {
154  typedef typename kernelof<X>::T T; };
155 }
156 
157 namespace let {
158 
159  template<class X, class Y, int r0, int r1> inline void
161  {
162  assign(a.m,b.m);
163  assign(a.M,b.M);
164  };
165 };
166 
167 template<class C,int R>
168 template<class X> inline
170  rnd_t rnd; // rounding mode verification
171  let::assign(*this,x); // evaluation of template expression
172  return *this;
173 }
174 
175 template<class C,int R>
176 template<class X> inline
178  rnd_t rnd; // rounding mode verification
179  std::cout<<"assign "<<x<<std::endl;
180  let::assign(*this,x); // evaluation of template expression
181 }
182 
183 template<class C, int mode>
184 template<class X, int R>
186 {
187  let::assign(*this,x);
188  return *this;
189 };
190 
191  namespace let {
192  template<class A,class B> void assign(A& a, const B&b);
193 
194  template<class C, class T> inline void
195  assign( Interval<C>& a, const T & b ) {
196  C mn,mx;
197  {
199  assign(mx,b);
200  }
201 
202  {
204  assign(mn,b);
205  }
206  if( mx < mn ) std::swap(mn,mx);
207  new (&a) Interval<C>(mn,mx);
208  };
209  }
210 
211 
212 
213 
214 
215 } //end namespace mmx
216 
217 #endif
Interval()
default constructor sets values to zero
Definition: Interval_fcts.hpp:72
Definition: Interval.hpp:40
void add(double &a, double b, double c)
Definition: texp_double.hpp:4
Interval & operator+=(const Interval &x)
Definition: Interval.hpp:119
Interval & operator/=(const Interval &x)
Definition: Interval.hpp:122
Definition: Interval_fcts.hpp:23
const C & b
Definition: Interval_glue.hpp:25
void div(double &a, double b, double c)
Definition: texp_double.hpp:7
TMPL X
Definition: polynomial_operators.hpp:148
Definition: Interval.hpp:51
structure defining a the empty list
Definition: texp_bool.hpp:11
void assign(const Interval &b)
Definition: Interval.hpp:79
void define(const T &m, const T &M)
Definition: Interval.hpp:77
bool operator<(const X &x) const
comparison with scalar value (<)
Definition: Interval.hpp:90
const T & upper() const
Definition: Interval.hpp:99
void sub(double &a, double b, double c)
Definition: texp_double.hpp:5
MP swap(const MP &P, int var_i, int var_j)
Definition: sparse_monomials.hpp:988
T upper(const Interval< T, r > &x)
Definition: Interval_fcts.hpp:89
bool operator>=(const X &x) const
comparison with scalar value (>=)
Definition: Interval.hpp:88
base_t::rnd rnd_t
Definition: Interval.hpp:49
kernelof< X >::T T
Definition: Interval.hpp:154
bool intersect(Interval< T, r > &result, const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:150
T value_type
Definition: Interval.hpp:45
T & inf()
Definition: Interval.hpp:104
Interval & operator-=(const Interval &x)
Definition: Interval.hpp:120
void mul(double &a, double b, double c)
Definition: texp_double.hpp:6
const T & sup() const
Definition: Interval.hpp:103
bool operator>(const X &x) const
comparison with scalar value (>)
Definition: Interval.hpp:86
Definition: rounding_mode.hpp:71
const T & lower() const
Definition: Interval.hpp:98
const T & inf() const
Definition: Interval.hpp:102
T boundary_type
Definition: Interval.hpp:47
void assign(const T &m, const T &M)
Definition: Interval.hpp:78
void hull(Interval< T, r > &v, const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:120
Definition: texp_expression.hpp:67
T coeff_t
Definition: Interval.hpp:46
Interval & operator=(const T &x)
Definition: Interval.hpp:107
T & sup()
Definition: Interval.hpp:105
#define Interval
Definition: Interval_glue.hpp:8
T lower(const Interval< T, r > &x)
Definition: Interval_fcts.hpp:87
T & upper()
Definition: Interval.hpp:101
T size() const
same as width
Definition: Interval.hpp:127
T width(const Interval< T, r > &x)
Definition: Interval_fcts.hpp:93
Interval & operator*=(const Interval &x)
Definition: Interval.hpp:121
numerics::interval_base< T,((unsigned) r)%4 > base_t
Definition: Interval.hpp:48
Generic class for intervals.
Definition: Interval.hpp:44
T m
Definition: Interval.hpp:52
const C & c
Definition: Interval_glue.hpp:45
bool operator!=(const X &x) const
comparison with scalar value (!=)
Definition: Interval.hpp:94
double C
Definition: solver_mv_fatarcs.cpp:16
bool operator>(const Interval< X, _r > &i) const
Definition: Interval.hpp:96
extended(const T &c, const T &d)
Definition: Interval.hpp:51
bool operator==(const X &k) const
true if the lower and upper bounds equals to k
Definition: Interval.hpp:82
void assign(A &a, const B &b)
Generic definition of the assignement function.
Definition: assign.hpp:97
Interval & operator=(const Interval &i)
Definition: Interval.hpp:67
T & lower()
Definition: Interval.hpp:100
bool operator==(const Interval< X, r_ > &b) const
Definition: Interval.hpp:83
T center() const
return the center of the interval
Definition: Interval.hpp:130
Definition: array.hpp:12
Interval< T, r > self_t
Definition: Interval.hpp:50
return the arithmetic kernel from which the unqualified type X comes from.
Definition: GMP.hpp:50
T width() const
return the width of the interval
Definition: Interval.hpp:125
bool operator<=(const X &x) const
comparison with scalar value (<=)
Definition: Interval.hpp:92
std::ostream & print(std::ostream &o)
Definition: texp_demangle.hpp:16
T M
Definition: Interval.hpp:52
Home