library of assembled shared sources

http://lass.cocamware.com

interval.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_INTERVAL_H
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_INTERVAL_H
00047 
00048 #include "num_common.h"
00049 #include "num_traits.h"
00050 #include "../util/call_traits.h"
00051 #include "../util/pyobject_plus.h"
00052 #include <iostream>
00053 
00054 namespace lass
00055 {
00056 namespace num
00057 {
00058 
00059 template<typename C> class interval;
00060 
00061 template<typename C> void inpsqr(interval<C>& i);
00062 template<typename C> void inpsqrt(interval<C>& i);
00063 template<typename C> void inpexp(interval<C>& i);
00064 template<typename C> void inplog(interval<C>& i);
00065 template<typename C> void inpnorm(interval<C>& i);
00066 template<typename C> void inpinv(interval<C>& i);
00067 template<typename C, typename F> interval<C> applyFunction(const interval<C>& i, F func);
00068 
00069 /** Interval class.  Class for interval arithmetic.  The arithmetic is weak interval arithmetic: there is
00070 *   no rounding code, to speed up things.  It should however be useful except for numerical stability
00071 *   studies on the bitlevel.  If you wont strong interval arithmetic, look for the boost library.
00072 * @warning DO NOT derive a class from this one which uses dynamic memory management! destructor is not virtual.
00073 * This is to avoid the functionptrtable to be stored in the objects.  Therefor none of the methods are virtual
00074 * @author Tom De Muer
00075 * @date   2003
00076 */
00077 template<typename C>
00078 class interval
00079 {
00080 private:
00081     C v[2];
00082 public:
00083     typedef C   baseType;
00084 
00085     static  const interval<C>   empty;
00086 
00087     inline interval() { v[0]=v[1]=C();};
00088     inline ~interval() {};
00089     inline explicit interval(typename util::CallTraits<baseType>::TParam iValue);
00090     inline explicit interval(typename util::CallTraits<baseType>::TParam iInf,typename util::CallTraits<baseType>::TParam iSup);
00091     inline interval& operator=(const interval<C>& i);
00092 
00093     inline const interval&  operator+() const;
00094     inline interval         operator-() const;
00095 
00096     /** direct set of both infinum and supinum */
00097     inline void set( typename util::CallTraits<C>::TParam iInf,typename util::CallTraits<C>::TParam iSup);
00098     /** returns true for the empty interval */
00099     inline bool isEmpty() const;
00100     /** return true for degenerated intervals */
00101     inline bool isSingleton() const;
00102     /** returns true when ix is in the interval */
00103     inline bool contains(typename util::CallTraits<baseType>::TParam ix) const;
00104 
00105     /** indexed access, 0 = infinum, 1 = supinum */
00106     inline baseType&    operator[](int i);
00107     /** indexed access, 0 = infinum, 1 = supinum */
00108     inline baseType operator[](int i) const;
00109     /** the infinum of the interval */
00110     inline baseType inf() const;
00111     /** the supinum of the interval */
00112     inline baseType sup() const;
00113     /** the infinum of the interval */
00114     inline baseType&    inf();
00115     /** the supinum of the interval */
00116     inline baseType&    sup();
00117     /** the middle of the interval */
00118     inline baseType mid() const;
00119     /** the difference between supinum and infinum */
00120     inline baseType diam()const;
00121     /** the ratio of the supinum over the infinum */
00122     inline baseType ratio() const;
00123 
00124     /** certainly greater than */
00125     inline bool operator> (const interval<C>& i) const;
00126     /** certainly less than */
00127     inline bool operator< (const interval<C>& i) const;
00128     /** certainly equal */
00129     inline bool operator==(const interval<C>& i) const;
00130     /** certainly not equal */
00131     inline bool operator!=(const interval<C>& i) const;
00132     /** certainly greater or equal than */
00133     inline bool operator>=(const interval<C>& i) const;
00134     /** certainly less or equal than */
00135     inline bool operator<=(const interval<C>& i) const;
00136 
00137     /** possibly equal */
00138     inline bool pe(const interval<C>& i) const;
00139     /** possibly not equal */
00140     inline bool pne(const interval<C>& i) const;
00141     /** possibly greater than */
00142     inline bool pg(const interval<C>& i) const;
00143     /** possibly greater or equal than */
00144     inline bool pge(const interval<C>& i) const;
00145     /** possibly less than */
00146     inline bool pl(const interval<C>& i) const;
00147     /** possibly less or equal than */
00148     inline bool ple(const interval<C>& i) const;
00149 
00150     /** fuzzy equal */
00151     baseType    fe(const interval<C>& i) const;
00152     /** fuzzy not equal */
00153     inline C    fne(const interval<C>& i) const;
00154     /** fuzzy greater than*/
00155     baseType    fg(const interval<C>& i) const;
00156     /** fuzzy greater or equal than*/
00157     baseType    fge(const interval<C>& i) const;
00158     /** fuzzy less than */
00159     baseType    fl(const interval<C>& i) const;
00160     /** fuzzy less or equal than*/
00161     baseType    fle(const interval<C>& i) const;
00162 
00163     inline interval&    operator+=(const interval<C>& i);
00164     inline interval&    operator-=(const interval<C>& i);
00165     inline interval&    operator*=(const interval<C>& i);
00166            interval&    operator/=(const interval<C>& i);
00167 
00168     inline interval&    operator+=(typename util::CallTraits<baseType>::TParam s);
00169     inline interval&    operator-=(typename util::CallTraits<baseType>::TParam s);
00170     inline interval&    operator*=(typename util::CallTraits<baseType>::TParam s);
00171     inline interval&    operator/=(typename util::CallTraits<baseType>::TParam s);
00172 
00173     friend void inpsqr<C>(interval<C>& i);
00174     friend void inpsqrt<C>(interval<C>& i);
00175     friend void inpexp<C>(interval<C>& i);
00176     friend void inplog<C>(interval<C>& i);
00177     friend void inpnorm<C>(interval<C>& i);
00178     friend void inpinv<C>(interval<C>& i);
00179 
00180     template<typename C2, typename F> friend interval<C2> applyFunction(const interval<C2>& iV, F func );
00181 
00182 };
00183 
00184 template<typename C> interval<C>    operator+(const interval<C>& i1,const interval<C>& i2);
00185 template<typename C> interval<C>    operator-(const interval<C>& i1,const interval<C>& i2);
00186 template<typename C> interval<C>    operator*(const interval<C>& i1,const interval<C>& i2);
00187 template<typename C> interval<C>    operator/(const interval<C>& i1,const interval<C>& i2);
00188 
00189 template<typename C> std::ostream& operator<<( std::ostream& os, const interval<C>& iV );
00190 template<typename C> std::string str( const interval<C>& iV );
00191 
00192 template<typename C> const interval<C> interval<C>::empty = interval<C>(NumTraits<C>::one, NumTraits<C>::zero);
00193 
00194 // implementation of basic_ops
00195 
00196 template<typename C> interval<C> sqr(const interval<C>& i);
00197 template<typename C> interval<C> sqrt(const interval<C>& i);
00198 template<typename C> interval<C> exp(const interval<C>& i);
00199 template<typename C> interval<C> log(const interval<C>& i);
00200 
00201 template<typename C>    interval<C> set_union(const interval<C>& i1, const interval<C>& i2);
00202 template<typename C>    interval<C> set_intersect(const interval<C>& i1, const interval<C>& i2);
00203 
00204 // num traits partial specialisation
00205 
00206 template< class C >
00207 struct NumTraits< interval<C> >
00208 {
00209     typedef interval<C> selfType;
00210     typedef C           baseType;
00211     typedef C           intervalType;
00212 
00213     enum
00214     {
00215         isDistribution = 1,
00216         isIntegral = 0,
00217         isNative = 0,
00218         isSigned = 1,
00219         hasInfinity = 0,
00220     };
00221 
00222     static const size_t memorySize;
00223     static const std::string name();
00224 
00225     static const interval<C>    one;
00226     static const interval<C>    zero;
00227 
00228     static const interval<C> epsilon;
00229     static const interval<C> min;
00230     static const interval<C> max;
00231     static const interval<C> minStrictPositive;
00232     static const interval<C> pi;
00233     static const interval<C> e;
00234     static const interval<C> sqrt2;
00235     static const interval<C> sqrtPi;
00236 };
00237 
00238 
00239 template<class C> const size_t NumTraits<interval<C> >::memorySize = sizeof(interval<C>);
00240 template<class C> const std::string NumTraits<interval<C> >::name() { return std::string("interval<")+NumTraits<baseType>::name()+ std::string(">"); }
00241 
00242 template<class C> const interval<C> NumTraits<interval<C> >::one = interval<C>( NumTraits<C>::one );
00243 template<class C> const interval<C> NumTraits<interval<C> >::zero = interval<C>( NumTraits<C>::zero );
00244 
00245 template<class C> const interval<C> NumTraits<interval<C> >::epsilon = interval<C>( NumTraits<C>::epsilon );
00246 template<class C> const interval<C> NumTraits<interval<C> >::min = interval<C>( NumTraits<C>::min );
00247 template<class C> const interval<C> NumTraits<interval<C> >::max = interval<C>( NumTraits<C>::max );
00248 template<class C> const interval<C> NumTraits<interval<C> >::minStrictPositive = interval<C>( NumTraits<C>::minStrictPositive );
00249 template<class C> const interval<C> NumTraits<interval<C> >::pi = interval<C>( NumTraits<C>::pi );
00250 template<class C> const interval<C> NumTraits<interval<C> >::e = interval<C>( NumTraits<C>::e );
00251 template<class C> const interval<C> NumTraits<interval<C> >::sqrt2 = interval<C>( NumTraits<C>::sqrt2 );
00252 template<class C> const interval<C> NumTraits<interval<C> >::sqrtPi = interval<C>( NumTraits<C>::sqrtPi );
00253 
00254 #include "interval.inl"
00255 
00256 }
00257 
00258 namespace python
00259 {
00260     template<class C> int pyGetSimpleObject_deprecated( PyObject* iValue, lass::num::interval<C>& oV )
00261     {
00262         std::pair<C,C> temp;
00263         int r=PyExportTraits<std::pair<C,C> >::get(iValue, temp);
00264         if (r==0)
00265         {
00266             oV.set(temp.first , temp.second);
00267         }
00268         return r;
00269     }
00270     template<class C> PyObject* pyBuildSimpleObject_deprecated( const lass::num::interval<C>& iV )
00271     {
00272         return PyExportTraits<std::pair<C,C> >::build( std::pair<C,C>(iV.inf(),iV.sup()) );
00273     }
00274     PYEXPORTTRAITS_USINGDEPRECATED_TEMPL( lass::num::interval )
00275 
00276 }
00277 
00278 }
00279 
00280 
00281 #endif

Generated on Mon Nov 10 14:20:05 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo