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 <itpp/base/math/misc.h>
00041 #include <limits>
00042 
00043 
00049 #ifndef HAVE_LOG1P
00051 inline double log1p(double x) { return std::log(1.0 + x); }
00052 #endif
00053 
00054 #ifndef HAVE_LOG2
00055 #undef log2                     // This is required at least for Cygwin
00057 inline double log2(double x)
00058 {
00059   return (std::log(x) * 1.442695040888963387004650940070860087871551513671875);
00060 }
00061 #endif
00062 
00068 namespace itpp {
00069 
00072 
00073   // ----------------------------------------------------------------------
00074   // scalar functions
00075   // ----------------------------------------------------------------------
00076 
00078   inline double logb(double b, double x)
00079   {
00080     return (std::log(x) / std::log(b));
00081   }
00082 
00084   inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); }
00086   inline double pow2(double x) { return pow(2.0, x); }
00087 
00089   inline double pow10(double x) { return pow(10.0, x); }
00090 
00092   inline double dB(double x) { return 10.0 * log10(x); }
00094   inline double inv_dB(double x) { return pow(10.0, 0.1 * x); }
00095 
00097   inline int int2bits(int n)
00098   {
00099     it_assert(n >= 0, "int2bits(): Improper argument value");
00100 
00101     if (n == 0)
00102       return 1;
00103 
00104     int b = 0;
00105     while (n) {
00106       n >>= 1;
00107       ++b;
00108     }
00109     return b;
00110   }
00111 
00113   inline int levels2bits(int n)
00114   {
00115     it_assert(n > 0,"levels2bits(): Improper argument value");
00116     return int2bits(--n);
00117   }
00118 
00120   inline int needed_bits(int n)
00121   {
00122     it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead.");
00123     return int2bits(n);
00124   }
00125 
00127   const double log_double_max = std::log(std::numeric_limits<double>::max());
00129   const double log_double_min = std::log(std::numeric_limits<double>::min());
00130 
00143   inline double trunc_log(double x)
00144   {
00145     if (std::numeric_limits<double>::is_iec559) {
00146       if (x == std::numeric_limits<double>::infinity())
00147         return log_double_max;
00148       if (x <= 0)
00149         return log_double_min;
00150     }
00151     return std::log(x);
00152   }
00153 
00165   inline double trunc_exp(double x)
00166   {
00167     if (std::numeric_limits<double>::is_iec559
00168         && (x >= log_double_max))
00169       return std::numeric_limits<double>::max();
00170     return std::exp(x);
00171   }
00172 
00173 
00175   inline double log_add(double log_a, double log_b)
00176   {
00177     if (log_a < log_b) {
00178       double tmp = log_a;
00179       log_a = log_b;
00180       log_b = tmp;
00181     }
00182     double negdelta = log_b - log_a;
00183     if ((negdelta < log_double_min) || std::isnan(negdelta))
00184       return log_a;
00185     else
00186       return (log_a + log1p(std::exp(negdelta)));
00187   }
00188 
00189 
00190   // ----------------------------------------------------------------------
00191   // functions on vectors and matrices
00192   // ----------------------------------------------------------------------
00193 
00195   inline vec exp(const vec &x)
00196   {
00197     return apply_function<double>(std::exp, x);
00198   }
00200   inline cvec exp(const cvec &x)
00201   {
00202     return apply_function<std::complex<double> >(std::exp, x);
00203   }
00205   inline mat exp(const mat &m)
00206   {
00207     return apply_function<double>(std::exp, m);
00208   }
00210   inline cmat exp(const cmat &m)
00211   {
00212     return apply_function<std::complex<double> >(std::exp, m);
00213   }
00214 
00216   inline vec pow(const double x, const vec &y)
00217   {
00218     return apply_function<double>(std::pow, x, y);
00219   }
00221   inline mat pow(const double x, const mat &y)
00222   {
00223     return apply_function<double>(std::pow, x, y);
00224   }
00226   inline vec pow(const vec &x, const double y)
00227   {
00228     return apply_function<double>(std::pow, x, y);
00229   }
00231   inline mat pow(const mat &x, const double y)
00232   {
00233     return apply_function<double>(std::pow, x, y);
00234   }
00235 
00237   inline vec pow2(const vec &x)
00238   {
00239     return apply_function<double>(pow2, x);
00240   }
00242   inline mat pow2(const mat &x)
00243   {
00244     return apply_function<double>(pow2, x);
00245   }
00246 
00248   inline vec pow10(const vec &x)
00249   {
00250     return apply_function<double>(pow10, x);
00251   }
00253   inline mat pow10(const mat &x)
00254   {
00255     return apply_function<double>(pow10, x);
00256   }
00257 
00259   inline vec log(const vec &x)
00260   {
00261     return apply_function<double>(std::log, x);
00262   }
00264   inline mat log(const mat &x)
00265   {
00266     return apply_function<double>(std::log, x);
00267   }
00269   inline cvec log(const cvec &x)
00270   {
00271     return apply_function<std::complex<double> >(std::log, x);
00272   }
00274   inline cmat log(const cmat &x)
00275   {
00276     return apply_function<std::complex<double> >(std::log, x);
00277   }
00278 
00280   inline vec log2(const vec &x)
00281   {
00282     return apply_function<double>(::log2, x);
00283   }
00285   inline mat log2(const mat &x)
00286   {
00287     return apply_function<double>(::log2, x);
00288   }
00289 
00291   inline vec log10(const vec &x)
00292   {
00293     return apply_function<double>(std::log10, x);
00294   }
00296   inline mat log10(const mat &x)
00297   {
00298     return apply_function<double>(std::log10, x);
00299   }
00300 
00302   inline vec logb(double b, const vec &x)
00303   {
00304     return apply_function<double>(itpp::logb, b, x);
00305   }
00307   inline mat logb(double b, const mat &x)
00308   {
00309     return apply_function<double>(itpp::logb, b, x);
00310   }
00311 
00313   inline vec dB(const vec &x)
00314   {
00315     return apply_function<double>(dB, x);
00316   }
00318   inline mat dB(const mat &x)
00319   {
00320     return apply_function<double>(dB, x);
00321   }
00322 
00324   inline vec inv_dB(const vec &x)
00325   {
00326     return apply_function<double>(inv_dB, x);
00327   }
00329   inline mat inv_dB(const mat &x)
00330   {
00331     return apply_function<double>(inv_dB, x);
00332   }
00333 
00335   inline ivec needed_bits(const ivec& v)
00336   {
00337     it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead.");
00338     return apply_function<int>(int2bits, v);
00339   }
00340 
00342   inline ivec int2bits(const ivec& v)
00343   {
00344     return apply_function<int>(int2bits, v);
00345   }
00346 
00348   inline ivec levels2bits(const ivec& v)
00349   {
00350     return apply_function<int>(levels2bits, v);
00351   }
00352 
00354 
00355 } // namespace itpp
00356 
00357 #endif // #ifndef LOG_EXP_H
00358 
00359 
00360 
00361 
SourceForge Logo

Generated on Sat Apr 19 10:43:52 2008 for IT++ by Doxygen 1.5.5