Library of Assembled Shared Sources
num/interval.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_INTERVAL_H
46#define LASS_GUARDIAN_OF_INCLUSION_NUM_INTERVAL_H
47
48#include "num_common.h"
49#include "num_traits.h"
50#include "../util/call_traits.h"
51#include <iostream>
52
53namespace lass
54{
55namespace num
56{
57
58template<typename C> class interval;
59
60template<typename C> void inpsqr(interval<C>& i);
61template<typename C> void inpsqrt(interval<C>& i);
62template<typename C> void inpexp(interval<C>& i);
63template<typename C> void inplog(interval<C>& i);
64template<typename C> void inpnorm(interval<C>& i);
65template<typename C> void inpinv(interval<C>& i);
66template<typename C, typename F> interval<C> applyFunction(const interval<C>& i, F func);
67
68/** Interval class. Class for interval arithmetic. The arithmetic is weak interval arithmetic: there is
69* no rounding code, to speed up things. It should however be useful except for numerical stability
70* studies on the bitlevel. If you wont strong interval arithmetic, look for the boost library.
71* @warning DO NOT derive a class from this one which uses dynamic memory management! destructor is not virtual.
72* This is to avoid the functionptrtable to be stored in the objects. Therefor none of the methods are virtual
73* @author Tom De Muer
74* @date 2003
75*/
76template<typename C>
77class interval
78{
79private:
80 C v[2];
81public:
82 typedef C baseType;
83
84 static const interval<C> empty;
85
86 inline interval() { v[0]=v[1]=C();};
87 inline ~interval() {};
88 inline explicit interval(typename util::CallTraits<baseType>::TParam iValue);
89 inline explicit interval(typename util::CallTraits<baseType>::TParam iInf,typename util::CallTraits<baseType>::TParam iSup);
90 inline interval& operator=(const interval<C>& i);
91
92 inline const interval& operator+() const;
93 inline interval operator-() const;
94
95 /** direct set of both infinum and supinum */
96 inline void set( typename util::CallTraits<C>::TParam iInf,typename util::CallTraits<C>::TParam iSup);
97 /** returns true for the empty interval */
98 inline bool isEmpty() const;
99 /** return true for degenerated intervals */
100 inline bool isSingleton() const;
101 /** returns true when ix is in the interval */
102 inline bool contains(typename util::CallTraits<baseType>::TParam ix) const;
103
104 /** indexed access, 0 = infinum, 1 = supinum */
105 inline baseType& operator[](int i);
106 /** indexed access, 0 = infinum, 1 = supinum */
107 inline baseType operator[](int i) const;
108 /** the infinum of the interval */
109 inline baseType inf() const;
110 /** the supinum of the interval */
111 inline baseType sup() const;
112 /** the infinum of the interval */
113 inline baseType& inf();
114 /** the supinum of the interval */
115 inline baseType& sup();
116 /** the middle of the interval */
117 inline baseType mid() const;
118 /** the difference between supinum and infinum */
119 inline baseType diam()const;
120 /** the ratio of the supinum over the infinum */
121 inline baseType ratio() const;
122
123 /** certainly greater than */
124 inline bool operator> (const interval<C>& i) const;
125 /** certainly less than */
126 inline bool operator< (const interval<C>& i) const;
127 /** certainly equal */
128 inline bool operator==(const interval<C>& i) const;
129 /** certainly not equal */
130 inline bool operator!=(const interval<C>& i) const;
131 /** certainly greater or equal than */
132 inline bool operator>=(const interval<C>& i) const;
133 /** certainly less or equal than */
134 inline bool operator<=(const interval<C>& i) const;
135
136 /** possibly equal */
137 inline bool pe(const interval<C>& i) const;
138 /** possibly not equal */
139 inline bool pne(const interval<C>& i) const;
140 /** possibly greater than */
141 inline bool pg(const interval<C>& i) const;
142 /** possibly greater or equal than */
143 inline bool pge(const interval<C>& i) const;
144 /** possibly less than */
145 inline bool pl(const interval<C>& i) const;
146 /** possibly less or equal than */
147 inline bool ple(const interval<C>& i) const;
148
149 /** fuzzy equal */
150 baseType fe(const interval<C>& i) const;
151 /** fuzzy not equal */
152 inline C fne(const interval<C>& i) const;
153 /** fuzzy greater than*/
154 baseType fg(const interval<C>& i) const;
155 /** fuzzy greater or equal than*/
156 baseType fge(const interval<C>& i) const;
157 /** fuzzy less than */
158 baseType fl(const interval<C>& i) const;
159 /** fuzzy less or equal than*/
160 baseType fle(const interval<C>& i) const;
161
162 inline interval& operator+=(const interval<C>& i);
163 inline interval& operator-=(const interval<C>& i);
164 inline interval& operator*=(const interval<C>& i);
165 interval& operator/=(const interval<C>& i);
166
167 inline interval& operator+=(typename util::CallTraits<baseType>::TParam s);
168 inline interval& operator-=(typename util::CallTraits<baseType>::TParam s);
169 inline interval& operator*=(typename util::CallTraits<baseType>::TParam s);
170 inline interval& operator/=(typename util::CallTraits<baseType>::TParam s);
172 friend void inpsqr<C>(interval<C>& i);
173 friend void inpsqrt<C>(interval<C>& i);
174 friend void inpexp<C>(interval<C>& i);
175 friend void inplog<C>(interval<C>& i);
176 friend void inpnorm<C>(interval<C>& i);
177 friend void inpinv<C>(interval<C>& i);
178
179 template<typename C2, typename F> friend interval<C2> applyFunction(const interval<C2>& iV, F func );
180
181};
182
183template<typename C> interval<C> operator+(const interval<C>& i1,const interval<C>& i2);
184template<typename C> interval<C> operator-(const interval<C>& i1,const interval<C>& i2);
185template<typename C> interval<C> operator*(const interval<C>& i1,const interval<C>& i2);
186template<typename C> interval<C> operator/(const interval<C>& i1,const interval<C>& i2);
187
188template<typename C> std::ostream& operator<<( std::ostream& os, const interval<C>& iV );
189template<typename C> std::string str( const interval<C>& iV );
190
191template<typename C> const interval<C> interval<C>::empty = interval<C>(NumTraits<C>::one, NumTraits<C>::zero);
192
193// implementation of basic_ops
194
195template<typename C> interval<C> sqr(const interval<C>& i);
196template<typename C> interval<C> sqrt(const interval<C>& i);
197template<typename C> interval<C> exp(const interval<C>& i);
198template<typename C> interval<C> log(const interval<C>& i);
199
200template<typename C> interval<C> set_union(const interval<C>& i1, const interval<C>& i2);
201template<typename C> interval<C> set_intersect(const interval<C>& i1, const interval<C>& i2);
202
203// num traits partial specialisation
204
205template< class C >
206struct NumTraits< interval<C> >
208 typedef interval<C> selfType;
209 typedef C baseType;
210 typedef C intervalType;
211
212 enum
214 isDistribution = 1,
215 isIntegral = 0,
216 isNative = 0,
217 isSigned = 1,
218 hasInfinity = 0,
219 };
220
221 static const size_t memorySize;
222 static const std::string name();
223
224 static const interval<C> one;
225 static const interval<C> zero;
226
227 static const interval<C> epsilon;
228 static const interval<C> min;
229 static const interval<C> max;
230 static const interval<C> minStrictPositive;
231 static const interval<C> pi;
232 static const interval<C> e;
233 static const interval<C> sqrt2;
234 static const interval<C> sqrtPi;
235};
236
237
238template<class C> const size_t NumTraits<interval<C> >::memorySize = sizeof(interval<C>);
239template<class C> const std::string NumTraits<interval<C> >::name() { return std::string("interval<")+NumTraits<baseType>::name()+ std::string(">"); }
240
241template<class C> const interval<C> NumTraits<interval<C> >::one = interval<C>( NumTraits<C>::one );
242template<class C> const interval<C> NumTraits<interval<C> >::zero = interval<C>( NumTraits<C>::zero );
243
244template<class C> const interval<C> NumTraits<interval<C> >::epsilon = interval<C>( NumTraits<C>::epsilon );
245template<class C> const interval<C> NumTraits<interval<C> >::min = interval<C>( NumTraits<C>::min );
246template<class C> const interval<C> NumTraits<interval<C> >::max = interval<C>( NumTraits<C>::max );
247template<class C> const interval<C> NumTraits<interval<C> >::minStrictPositive = interval<C>( NumTraits<C>::minStrictPositive );
248template<class C> const interval<C> NumTraits<interval<C> >::pi = interval<C>( NumTraits<C>::pi );
249template<class C> const interval<C> NumTraits<interval<C> >::e = interval<C>( NumTraits<C>::e );
250template<class C> const interval<C> NumTraits<interval<C> >::sqrt2 = interval<C>( NumTraits<C>::sqrt2 );
251template<class C> const interval<C> NumTraits<interval<C> >::sqrtPi = interval<C>( NumTraits<C>::sqrtPi );
252
253#include "interval.inl"
254
255}
256}
257
258
259#endif
Interval class.
bool contains(typename util::CallTraits< baseType >::TParam ix) const
returns true when ix is in the interval
bool pl(const interval< C > &i) const
possibly less than
bool operator!=(const interval< C > &i) const
certainly not equal
bool operator>=(const interval< C > &i) const
certainly greater or equal than
bool isSingleton() const
return true for degenerated intervals
bool pg(const interval< C > &i) const
possibly greater than
bool pe(const interval< C > &i) const
possibly equal
baseType sup() const
the supinum of the interval
bool ple(const interval< C > &i) const
possibly less or equal than
baseType & operator[](int i)
indexed access, 0 = infinum, 1 = supinum
baseType ratio() const
the ratio of the supinum over the infinum
baseType fle(const interval< C > &i) const
fuzzy less or equal than
baseType fge(const interval< C > &i) const
fuzzy greater or equal than
bool pge(const interval< C > &i) const
possibly greater or equal than
baseType fl(const interval< C > &i) const
fuzzy less than
bool operator==(const interval< C > &i) const
certainly equal
baseType diam() const
the difference between supinum and infinum
C fne(const interval< C > &i) const
fuzzy not equal
baseType inf() const
the infinum of the interval
baseType mid() const
the middle of the interval
bool pne(const interval< C > &i) const
possibly not equal
bool operator<=(const interval< C > &i) const
certainly less or equal than
void set(typename util::CallTraits< C >::TParam iInf, typename util::CallTraits< C >::TParam iSup)
direct set of both infinum and supinum
baseType fg(const interval< C > &i) const
fuzzy greater than
bool operator>(const interval< C > &i) const
certainly greater than
bool operator<(const interval< C > &i) const
certainly less than
bool isEmpty() const
returns true for the empty interval
baseType fe(const interval< C > &i) const
fuzzy equal
T sqr(const T &x)
return x * x
Definition basic_ops.h:162
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53