Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members

plMath.h

00001 /*=============================================================================
00002  * Product        : OpenPL 
00003  * File           : plMath.h
00004  * Author         : Kamel Mekhnacha
00005  * Creation       : August 07 2001
00006  *
00007  *=============================================================================
00008  *     (c) Copyright 2000, Centre National de la Recherche Scientifique,
00009  *                              all rights reserved
00010  *=============================================================================
00011  *
00012  *------------------------- Description ---------------------------------------
00013  *    
00014  *   
00015  *-----------------------------------------------------------------------------
00016  */
00017 
00018 #ifndef _plMath_h_
00019 #define _plMath_h_
00020 
00021 //=============================================================================
00022 //                 STL INCLUDES
00023 //=============================================================================
00024 
00025 #define PL_USE_NAMESPACE
00026 
00027 
00028 #include <algorithm>
00029 #include <bitset>
00030 
00031 #include <list>
00032 #include <map>
00033 #include <set>
00034 #include <string>
00035 #include <vector>
00036 #include <fstream>
00037 
00038 #ifdef PL_USE_NAMESPACE
00039 using namespace std;
00040 
00041 #else
00042 
00043 #define bitset std::bitset
00044 #define list std::list
00045 #define map std::map
00046 #define string std::string
00047 #define vector std::vector
00048 #define pair std::pair
00049 
00050 #define ostream std::ostream
00051 #define istream std::istream
00052 #define ofstream std::ofstream
00053 #define ifstream std::ifstream
00054 
00055 #define make_pair (std::make_pair)
00056 #define remove_if (std::remove_if)
00057 #define back_inserter (std::back_inserter)
00058 /*  #define find (std::find) */
00059 #define find_if (std::find_if)
00060 #define set_union (std::set_union)
00061 #define set_intersection (std::set_intersection)
00062 #define set_difference (std::set_difference)
00063 #define includes (std::includes)
00064 
00065 #define cout (std::cout)
00066 #define cin (std::cin)
00067 #define cerr (std::cerr)
00068 #define endl (std::endl)
00069 
00070 #endif
00071 
00072 
00073 //=============================================================================
00074 //                 STANDARD C INCLUDES
00075 //=============================================================================
00076 
00077 #include <math.h>
00078 #include <stdlib.h>
00079 #include <limits.h>
00080 #include <stdio.h>
00081 #include <float.h>
00082 
00083 //=============================================================================
00084 //                 COMPILATION VARIABLES
00085 //=============================================================================
00086 
00087 
00088 // Comment out the following line to use double  or single precision floating numbers.
00089 #define PL_LONG_DOUBLE_PRECISION
00090 
00091 
00092 // There is no long double floating numbers on macintosh. Long double is just a double!
00093 #ifdef macintosh
00094 #undef PL_LONG_DOUBLE_PRECISION
00095 #endif
00096 
00097 
00098 // Comment out the following line to use single precision floating numbers 
00099 #define PL_DOUBLE_PRECISION
00100 
00101 
00102 // Uncomment  the following line to use our own arithmetics on probabilities.
00103 // otherwise, machine floating numbers are used.
00104 //#define PL_USE_PLPROBA
00105 
00106 // Uncomment the following line if floating numbers representation is not IEEE754.
00107 // This is only used when PL_USE_PLPROBA is defined (we are using plProbability type).
00108 //#define PL_NOT_IEEE754
00109 
00110 
00111 // Uncomment this line to use our own tabulated ERF function
00112 //#define PL_USE_TABLE_FOR_ERF
00113 
00114 // The ERF function is not defined in Windows machines!!
00115 #if defined(WIN32) || defined(_WIN32)
00116 #define PL_USE_TABLE_FOR_ERF
00117 #endif
00118 
00119 //=============================================================================
00120 //                 FLOATING NUMBERS RELATED CONSTANTS
00121 //=============================================================================
00122 
00123 #ifdef PL_LONG_DOUBLE_PRECISION
00124 typedef long double plFloat;
00125 
00126 #define PL_LOG_OF_2     0.69314718055994530942l
00127 #define PL_PI_2         1.57079632679489661923l
00128 #define PL_PI           3.14159265358979323846l
00129 #define PL_2PI          6.28318530717958647692l
00130 #define PL_INV_SQRT_2PI 0.398942280401433l
00131 #define PL_SQRT_PI      1.77245385090551602729816748334l
00132 #define PL_LOG_ROOT_2_PI  0.9189385332046727418l
00133 #define PL_ZERO         0.0l
00134 #define PL_HALF         0.5l
00135 #define PL_ONE          1.0l
00136 #define PL_TWO          2.0l
00137 #define PL_SQRT_2       1.414213562373095145475l
00138 #define PL_E            2.7182818284590452354l  /* e */
00139 #define PL_EPSILON      LDBL_EPSILON // smallest number such that 1+Eps != 1
00140 #define PL_FLOAT_MAX    LDBL_MAX
00141 #define PL_MIN_EXP      -745.0l
00142 #define PL_LOG_OF_10_BASE2 3.321928094887362660l
00143 
00144 #define PL_MINUS_ONE -1.0l
00145 #else
00146 
00147 #ifdef PL_DOUBLE_PRECISION
00148 //Use double precision floating numbers
00149 typedef double plFloat;
00150 
00151 
00152 #define PL_LOG_OF_2     0.69314718055994530942
00153 #define PL_PI_2         1.57079632679489661923
00154 #define PL_PI           3.14159265358979323846
00155 #define PL_2PI          6.28318530717958647692
00156 #define PL_INV_SQRT_2PI 0.398942280401433
00157 #define PL_SQRT_PI      1.77245385090551602729816748334
00158 #define PL_LOG_ROOT_2_PI  0.9189385332046727418
00159 #define PL_ZERO         0.0
00160 #define PL_HALF         0.5
00161 #define PL_ONE          1.0
00162 #define PL_TWO          2.0
00163 #define PL_SQRT_2       1.414213562373095145475
00164 #define PL_E            2.7182818284590452354   /* e */
00165 #define PL_EPSILON      DBL_EPSILON // smallest number such that 1+Eps != 1
00166 #define PL_FLOAT_MAX    DBL_MAX
00167 #define PL_MIN_EXP      -745.0
00168 #define PL_LOG_OF_10_BASE2 3.321928094887362660
00169 
00170 #define PL_MINUS_ONE -1.0
00171 #else
00172 //Use single precision floating numbers
00173 typedef float plFloat;
00174 
00175 
00176 #define PL_LOG_OF_2     0.69314718055994530942f
00177 #define PL_PI_2         1.57079632679489661923f
00178 #define PL_PI           3.14159265358979323846f
00179 #define PL_2PI          6.28318530717958647692f
00180 #define PL_INV_SQRT_2PI 0.398942280401433f
00181 #define PL_SQRT_PI      1.77245385090551602729816748334f
00182 #define PL_LOG_ROOT_2_PI  0.9189385332046727418f
00183 #define PL_ZERO         0.0f
00184 #define PL_HALF         0.5f
00185 #define PL_ONE          1.0f
00186 #define PL_TWO          2.0f
00187 #define PL_SQRT_2       1.414213562373095145475f
00188 #define PL_E            2.7182818284590452354f  /* e */
00189 #define PL_EPSILON      FLT_EPSILON // smallest number such that 1+Eps != 1
00190 #define PL_FLOAT_MAX    FLT_MAX
00191 #define PL_MIN_EXP      -745.0f
00192 #define PL_LOG_OF_10_BASE2 3.321928094887362660f
00193 
00194 #define PL_MINUS_ONE -1.0f
00195 #endif
00196 
00197 #endif
00198 
00199 
00200 //=============================================================================
00201 //                 OTHER CONSTANTS AND TOOLS
00202 //=============================================================================
00203 
00204 //=============================================================================
00205 inline plFloat plLogBase2(plFloat v) 
00206 { 
00207   return log(v)/PL_LOG_OF_2; 
00208 }
00209 
00210 static const unsigned int PL_NUMBER_OF_BITS_BY_CHAR = sizeof(unsigned char)*8;
00211 
00212 //=============================================================================
00213 inline int plNbBits(int _int) 
00214 { 
00215   int i=0;
00216   _int = _int -1;
00217   while( _int != 0){
00218     _int = _int >> 1;
00219     i++;
00220   }
00221   return i;
00222 }
00223 
00224 #define PL_MAX_ULONG ULONG_MAX
00225 
00226 //=============================================================================
00227 inline int plRound( plFloat x )
00228 {
00229   plFloat tmp1 = floor( x );
00230   plFloat tmp2 = ceil( x );
00231   return (int) ( ( x - tmp1 < tmp2 - x ) ? tmp1 : tmp2);
00232 }
00233 
00234 
00235 //=============================================================================
00236 inline plFloat plFabs( plFloat x )
00237 {
00238   return (x < PL_ZERO) ? -x : x;
00239 }
00240 
00241 
00242 //=============================================================================
00243 //                 TIMING TOOLS
00244 //=============================================================================
00245 
00246 #include <plTimer.h>
00247 
00248 // Returns execution time in seconds of the program "prg" in the double floating 
00249 // number "time_in_seconds".
00250 #define PL_TIME_SEC(prg, time_in_seconds)\
00251 plTimer::timer.start();\
00252 prg;\
00253 plTimer::timer.stop();\
00254 time_in_seconds = plTimer::timer.get_time_sec();
00255 
00256 // Returns execution time in milliseconds of the program "prg" in the double floating 
00257 // number "time_in_milliseconds".
00258 #define PL_TIME_MSEC(prg, time_in_milliseconds)\
00259 plTimer::timer.start();\
00260 prg;\
00261 plTimer::timer.stop();\
00262 time_in_milliseconds = plTimer::timer.get_time_msec();
00263 
00264 #define PL_TIME PL_TIME_SEC
00265 
00266 //=============================================================================
00267 //                 DEFINE A TYPE TO BE USED FOR PROBABILITIES
00268 //=============================================================================
00269 
00270 #ifndef PL_USE_PLPROBA
00271 //We are using standard arithmetics using machine's floating numbers.
00272 
00279 typedef plFloat plProbValue;
00280 #define PL_ZERO_PROB PL_ZERO
00281 #define PL_HALF_PROB PL_HALF
00282 #define PL_ONE_PROB PL_ONE
00283 #define PL_TWO_PROB PL_TWO
00284 
00285 #define PL_INFINITY_PROB HUGE_VAL
00286 #define PL_INVALID_PROB -1.0
00287 #else
00288 
00289 //  We are using our own arithmetics.
00290 #include <plProbability32.h>
00291 
00298 typedef plProbability plProbValue;
00299 #define PL_ZERO_PROB plProbability::plZeroProbaConstant
00300 #define PL_HALF_PROB plProbability::plHalfProbaConstant
00301 #define PL_ONE_PROB plProbability::plOneProbaConstant
00302 #define PL_TWO_PROB plProbability::plTwoProbaConstant
00303 
00304 #define PL_INFINITY_PROB plProbability::plInfinityProbaConstant
00305 #define PL_INVALID_PROB plProbability::plNaNProbaConstant
00306 #endif
00307 
00308 //=============================================================================
00309 //                 RANDOM SAMPLING TOOLS
00310 //=============================================================================
00311 #include <plRandom.h>
00312 
00313 
00314 
00315 //==============================================================================
00316 #endif 

Generated on Fri Apr 1 10:58:14 2005 for ProBT by  doxygen 1.4.1