util.hh

Go to the documentation of this file.
00001 #ifndef UTIL_HH
00002 #define UTIL_HH
00003 
00004 #include <algorithm>
00005 #include <vector>
00006 #include <math.h>
00007 
00009 namespace util {
00010 
00012   template <typename T>
00013   T sqr(T a)
00014   {
00015     return a * a;
00016   }
00017 
00021   template <typename T>
00022   T
00023   median(std::vector<T> v)
00024   {
00025     std::sort(v.begin(), v.end());
00026     return v[v.size() / 2];
00027   }
00028 
00030   template <typename T>
00031   T
00032   abs(const T &value)
00033   {
00034     if (value < 0)
00035       return -value;
00036     return value;
00037   }
00038 
00040   template <typename T>
00041   T
00042   max(const T &a, const T &b)
00043   {
00044     if (a < b)
00045       return b;
00046     return a;
00047   }
00048 
00049   inline float
00050   log10add(float a, float b)
00051   {
00052     const float LOG10TOe = M_LN10;
00053     const float LOGeTO10 = 1.0 / M_LN10;
00054 
00055     a = a * LOG10TOe;
00056     b = b * LOG10TOe;
00057 
00058     float delta = a - b;
00059     if (delta > 64.0) {
00060       b += 64;
00061       delta = -delta;
00062     }
00063     return (b + log1pf(exp(delta))) * LOGeTO10;
00064   }
00065 
00066   inline float
00067   logadd(float a, float b)
00068   {
00069     float delta = a - b;
00070     if (delta > 64.0) {
00071       b += 64;
00072       delta = -delta;
00073     }
00074     return b + log1pf(exp(delta));
00075   }
00076 
00077 };
00078 
00079 #endif /* UTIL_HH */

Generated on Mon Jan 8 15:51:03 2007 for bit by  doxygen 1.4.6