IT++ Logo

log_exp.h

Go to the documentation of this file.
00001 
00030 #ifndef LOG_EXP_H
00031 #define LOG_EXP_H
00032 
00033 #ifndef _MSC_VER
00034 #  include <itpp/config.h>
00035 #else
00036 #  include <itpp/config_msvc.h>
00037 #endif
00038 
00039 #include <itpp/base/help_functions.h>
00040 #include <limits>
00041 
00042 
00048 #ifndef HAVE_LOG1P
00050 inline double log1p(double x) { return std::log(1.0 + x); }
00051 #endif
00052 
00053 #ifndef HAVE_LOG2
00054 #undef log2                     // This is required at least for Cygwin
00056 inline double log2(double x)
00057 {
00058   return (std::log(x) * 1.442695040888963387004650940070860087871551513671875);
00059 }
00060 #endif
00061 
00067 namespace itpp {
00068 
00071 
00072   // ----------------------------------------------------------------------
00073   // scalar functions
00074   // ----------------------------------------------------------------------
00075 
00077   inline double logb(double b, double x)
00078   {
00079     return (std::log(x) / std::log(b));
00080   }
00081 
00083   inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); }
00085   inline double pow2(double x) { return pow(2.0, x); }
00086 
00088   inline double pow10(double x) { return pow(10.0, x); }
00089 
00091   inline double dB(double x) { return 10.0 * log10(x); }
00093   inline double inv_dB(double x) { return pow(10.0, 0.1 * x); }
00094 
00096   inline int int2bits(int n)
00097   {
00098     it_assert(n >= 0, "int2bits(): Improper argument value");
00099 
00100     if (n == 0)
00101       return 1;
00102 
00103     int b = 0;
00104     while (n) {
00105       n >>= 1;
00106       ++b;
00107     }
00108     return b;
00109   }
00110 
00112   inline int levels2bits(int n)
00113   {
00114     it_assert(n > 0,"levels2bits(): Improper argument value");
00115     return int2bits(--n);
00116   }
00117 
00119   inline int needed_bits(int n)
00120   {
00121     it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead.");
00122     return int2bits(n);
00123   }
00124 
00126   const double log_double_max = std::log(std::numeric_limits<double>::max());
00128   const double log_double_min = std::log(std::numeric_limits<double>::min());
00129 
00142   inline double trunc_log(double x)
00143   {
00144     if (std::numeric_limits<double>::is_iec559) {
00145       if (x == std::numeric_limits<double>::infinity())
00146   return log_double_max;
00147       if (x <= 0)
00148   return log_double_min;
00149     }
00150     return std::log(x);
00151   }
00152 
00164   inline double trunc_exp(double x)
00165   {
00166     if (std::numeric_limits<double>::is_iec559
00167   && (x >= log_double_max))
00168       return std::numeric_limits<double>::max();
00169     return std::exp(x);
00170   }
00171 
00172 
00174   inline double log_add(double log_a, double log_b)
00175   {
00176     if (log_a < log_b) {
00177       double tmp = log_a;
00178       log_a = log_b;
00179       log_b = tmp;
00180     }
00181     double negdelta = log_b - log_a;
00182     if (negdelta < log_double_min)
00183       return log_a;
00184     else
00185       return (log_a + log1p(std::exp(negdelta)));
00186   }
00187 
00188 
00189   // ----------------------------------------------------------------------
00190   // functions on vectors and matrices
00191   // ----------------------------------------------------------------------
00192 
00194   inline vec exp(const vec &x)
00195   {
00196     return apply_function<double>(std::exp, x);
00197   }
00199   inline cvec exp(const cvec &x)
00200   {
00201     return apply_function<std::complex<double> >(std::exp, x);
00202   }
00204   inline mat exp(const mat &m)
00205   {
00206     return apply_function<double>(std::exp, m);
00207   }
00209   inline cmat exp(const cmat &m)
00210   {
00211     return apply_function<std::complex<double> >(std::exp, m);
00212   }
00213 
00215   inline vec pow(const double x, const vec &y)
00216   {
00217     return apply_function<double>(std::pow, x, y);
00218   }
00220   inline mat pow(const double x, const mat &y)
00221   {
00222     return apply_function<double>(std::pow, x, y);
00223   }
00225   inline vec pow(const vec &x, const double y)
00226   {
00227     return apply_function<double>(std::pow, x, y);
00228   }
00230   inline mat pow(const mat &x, const double y)
00231   {
00232     return apply_function<double>(std::pow, x, y);
00233   }
00234 
00236   inline vec pow2(const vec &x)
00237   {
00238     return apply_function<double>(pow2, x);
00239   }
00241   inline mat pow2(const mat &x)
00242   {
00243     return apply_function<double>(pow2, x);
00244   }
00245 
00247   inline vec pow10(const vec &x)
00248   {
00249     return apply_function<double>(pow10, x);
00250   }
00252   inline mat pow10(const mat &x)
00253   {
00254     return apply_function<double>(pow10, x);
00255   }
00256 
00258   inline vec log(const vec &x)
00259   {
00260     return apply_function<double>(std::log, x);
00261   }
00263   inline mat log(const mat &x)
00264   {
00265     return apply_function<double>(std::log, x);
00266   }
00268   inline cvec log(const cvec &x)
00269   {
00270     return apply_function<std::complex<double> >(std::log, x);
00271   }
00273   inline cmat log(const cmat &x)
00274   {
00275     return apply_function<std::complex<double> >(std::log, x);
00276   }
00277 
00279   inline vec log2(const vec &x)
00280   {
00281     return apply_function<double>(::log2, x);
00282   }
00284   inline mat log2(const mat &x)
00285   {
00286     return apply_function<double>(::log2, x);
00287   }
00288 
00290   inline vec log10(const vec &x)
00291   {
00292     return apply_function<double>(std::log10, x);
00293   }
00295   inline mat log10(const mat &x)
00296   {
00297     return apply_function<double>(std::log10, x);
00298   }
00299 
00301   inline vec logb(double b, const vec &x)
00302   {
00303     return apply_function<double>(itpp::logb, b, x);
00304   }
00306   inline mat logb(double b, const mat &x)
00307   {
00308     return apply_function<double>(itpp::logb, b, x);
00309   }
00310 
00312   inline vec dB(const vec &x)
00313   {
00314     return apply_function<double>(dB, x);
00315   }
00317   inline mat dB(const mat &x)
00318   {
00319     return apply_function<double>(dB, x);
00320   }
00321 
00323   inline vec inv_dB(const vec &x)
00324   {
00325     return apply_function<double>(inv_dB, x);
00326   }
00328   inline mat inv_dB(const mat &x)
00329   {
00330     return apply_function<double>(inv_dB, x);
00331   }
00332 
00334   inline ivec needed_bits(const ivec& v)
00335   {
00336     it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead.");
00337     return apply_function<int>(int2bits, v);
00338   }
00339 
00341   inline ivec int2bits(const ivec& v)
00342   {
00343     return apply_function<int>(int2bits, v);
00344   }
00345 
00347   inline ivec levels2bits(const ivec& v)
00348   {
00349     return apply_function<int>(levels2bits, v);
00350   }
00351 
00353 
00354 } // namespace itpp
00355 
00356 #endif // #ifndef LOG_EXP_H
00357 
00358 
00359 
00360 
SourceForge Logo

Generated on Sun Dec 9 17:26:17 2007 for IT++ by Doxygen 1.5.4