00001 #ifndef JMATH_MISC_HPP
00002 #define JMATH_MISC_HPP
00003
00004 #include <cmath>
00005 #include <string>
00006 #include <sstream>
00007 #include "jmath/jblas.hpp"
00008
00009 namespace jafar {
00010 namespace jmath {
00011
00012 #ifdef THALES_TAROT
00013 #undef max
00014 #undef min
00015 #endif
00016
00017
00018
00019 template<typename T>
00020 static inline T abs(const T x) { return (x>=0 ? x : -x); }
00021
00022 template<typename T>
00023 static inline T sqr(const T x) { return x*x; }
00024
00025 template<typename T>
00026 static inline T sum_sqr(const T x, const T y) { return sqr(x) + sqr(y); }
00027 template<typename T>
00028 static inline T sum_sqr(const T x, const T y, const T z) { return sqr(x) + sqr(y) + sqr(z); }
00029
00030 template <typename T>
00031 static inline T max(const T a, const T b) { return std::max(a,b); }
00032
00033
00034 template <typename T>
00035 static inline T max(const T a, const T b, const T c) { return std::max(std::max(a,b),c); }
00036
00037 template <typename T>
00038 static inline T min(const T a, const T b) { return std::min(a,b); }
00039
00040
00041 template <typename T>
00042 static inline T min(const T a, const T b, const T c) { return std::min(std::min(a,b),c); }
00043
00044 template <typename T>
00045 static inline T sign(const T x) { return (x < 0 ? -1 : +1); }
00046
00047 template <typename T>
00048 static inline T mod(const T x, const T n) { return x-n*std::floor(x/n); }
00049
00050 template <typename T>
00051 static inline T mod2(const T x, const T a, const T b) { return mod(x-a,b-a)+a; }
00052
00053 template <typename T>
00054 static inline T round(const T x) { return (x >= 0 ? std::floor(x+(T)0.5) : std::ceil(x-(T)0.5)); }
00055
00056 template <typename T>
00057 static inline std::string toStr(T a) { std::ostringstream s; s << a; return s.str(); }
00058
00059 static inline std::string intToStr(int i) {
00060 std::ostringstream name;
00061 name << "" << i;
00062 return name.str();
00063 }
00064
00065 #define M_SQRTPI 1.7724538509055160
00066
00067 inline double evalGaussian(double sigma, double x)
00068 {
00069 return 1.0/(sigma*M_SQRT2*M_SQRTPI) * exp(-(x*x)/(2.0*sigma*sigma));
00070 }
00071
00072 inline double evalGaussian(double sigma, double x, double y)
00073 {
00074 double sigma22 = 2.0*sigma*sigma;
00075 return 1.0/(M_PI*sigma22)*exp(-(x*x+y*y)/sigma22);
00076 }
00077
00078 inline double evalGaussian(double sigmax, double sigmay, double x, double y)
00079 {
00080 return 1.0/(2.0*M_PI*sigmax*sigmay)*exp(-(x*x)/(2.0*sigmax*sigmax) - (y*y)/(2.0*sigmay*sigmay));
00081 }
00082
00083 template <typename SYM_MAT>
00084 inline jblas::vec stdevFromCov(SYM_MAT &P)
00085 {
00086 jblas::vec x = ublas::matrix_vector_range<SYM_MAT>(P, ublas::range(0, P.size1()), ublas::range (0, P.size2()));
00087 jblas::vec res(x.size());
00088 for(size_t i = 0; i < x.size(); ++i) res(i) = sqrt(x(i));
00089 return res;
00090 }
00091
00092
00093 }
00094 }
00095
00096 #endif
00097