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 { 00070 00073 00074 // ---------------------------------------------------------------------- 00075 // scalar functions 00076 // ---------------------------------------------------------------------- 00077 00079 inline double logb(double b, double x) 00080 { 00081 return (std::log(x) / std::log(b)); 00082 } 00083 00085 inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); } 00087 inline double pow2(double x) { return pow(2.0, x); } 00088 00090 inline double pow10(double x) { return pow(10.0, x); } 00091 00093 inline double dB(double x) { return 10.0 * log10(x); } 00095 inline double inv_dB(double x) { return pow(10.0, 0.1 * x); } 00096 00098 inline int int2bits(int n) 00099 { 00100 it_assert(n >= 0, "int2bits(): Improper argument value"); 00101 00102 if (n == 0) 00103 return 1; 00104 00105 int b = 0; 00106 while (n) { 00107 n >>= 1; 00108 ++b; 00109 } 00110 return b; 00111 } 00112 00114 inline int levels2bits(int n) 00115 { 00116 it_assert(n > 0, "levels2bits(): Improper argument value"); 00117 return int2bits(--n); 00118 } 00119 00121 inline int needed_bits(int n) 00122 { 00123 it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead."); 00124 return int2bits(n); 00125 } 00126 00128 const double log_double_max = std::log(std::numeric_limits<double>::max()); 00130 const double log_double_min = std::log(std::numeric_limits<double>::min()); 00131 00144 inline double trunc_log(double x) 00145 { 00146 if (std::numeric_limits<double>::is_iec559) { 00147 if (x == std::numeric_limits<double>::infinity()) 00148 return log_double_max; 00149 if (x <= 0) 00150 return log_double_min; 00151 } 00152 return std::log(x); 00153 } 00154 00166 inline double trunc_exp(double x) 00167 { 00168 if (std::numeric_limits<double>::is_iec559 00169 && (x >= log_double_max)) 00170 return std::numeric_limits<double>::max(); 00171 return std::exp(x); 00172 } 00173 00174 00176 inline double log_add(double log_a, double log_b) 00177 { 00178 if (log_a < log_b) { 00179 double tmp = log_a; 00180 log_a = log_b; 00181 log_b = tmp; 00182 } 00183 double negdelta = log_b - log_a; 00184 if ((negdelta < log_double_min) || std::isnan(negdelta)) 00185 return log_a; 00186 else 00187 return (log_a + log1p(std::exp(negdelta))); 00188 } 00189 00190 00191 // ---------------------------------------------------------------------- 00192 // functions on vectors and matrices 00193 // ---------------------------------------------------------------------- 00194 00196 inline vec exp(const vec &x) 00197 { 00198 return apply_function<double>(std::exp, x); 00199 } 00201 inline cvec exp(const cvec &x) 00202 { 00203 return apply_function<std::complex<double> >(std::exp, x); 00204 } 00206 inline mat exp(const mat &m) 00207 { 00208 return apply_function<double>(std::exp, m); 00209 } 00211 inline cmat exp(const cmat &m) 00212 { 00213 return apply_function<std::complex<double> >(std::exp, m); 00214 } 00215 00217 inline vec pow(const double x, const vec &y) 00218 { 00219 return apply_function<double>(std::pow, x, y); 00220 } 00222 inline mat pow(const double x, const mat &y) 00223 { 00224 return apply_function<double>(std::pow, x, y); 00225 } 00227 inline vec pow(const vec &x, const double y) 00228 { 00229 return apply_function<double>(std::pow, x, y); 00230 } 00232 inline mat pow(const mat &x, const double y) 00233 { 00234 return apply_function<double>(std::pow, x, y); 00235 } 00236 00238 inline vec pow2(const vec &x) 00239 { 00240 return apply_function<double>(pow2, x); 00241 } 00243 inline mat pow2(const mat &x) 00244 { 00245 return apply_function<double>(pow2, x); 00246 } 00247 00249 inline vec pow10(const vec &x) 00250 { 00251 return apply_function<double>(pow10, x); 00252 } 00254 inline mat pow10(const mat &x) 00255 { 00256 return apply_function<double>(pow10, x); 00257 } 00258 00260 inline vec log(const vec &x) 00261 { 00262 return apply_function<double>(std::log, x); 00263 } 00265 inline mat log(const mat &x) 00266 { 00267 return apply_function<double>(std::log, x); 00268 } 00270 inline cvec log(const cvec &x) 00271 { 00272 return apply_function<std::complex<double> >(std::log, x); 00273 } 00275 inline cmat log(const cmat &x) 00276 { 00277 return apply_function<std::complex<double> >(std::log, x); 00278 } 00279 00281 inline vec log2(const vec &x) 00282 { 00283 return apply_function<double>(::log2, x); 00284 } 00286 inline mat log2(const mat &x) 00287 { 00288 return apply_function<double>(::log2, x); 00289 } 00290 00292 inline vec log10(const vec &x) 00293 { 00294 return apply_function<double>(std::log10, x); 00295 } 00297 inline mat log10(const mat &x) 00298 { 00299 return apply_function<double>(std::log10, x); 00300 } 00301 00303 inline vec logb(double b, const vec &x) 00304 { 00305 return apply_function<double>(itpp::logb, b, x); 00306 } 00308 inline mat logb(double b, const mat &x) 00309 { 00310 return apply_function<double>(itpp::logb, b, x); 00311 } 00312 00314 inline vec dB(const vec &x) 00315 { 00316 return apply_function<double>(dB, x); 00317 } 00319 inline mat dB(const mat &x) 00320 { 00321 return apply_function<double>(dB, x); 00322 } 00323 00325 inline vec inv_dB(const vec &x) 00326 { 00327 return apply_function<double>(inv_dB, x); 00328 } 00330 inline mat inv_dB(const mat &x) 00331 { 00332 return apply_function<double>(inv_dB, x); 00333 } 00334 00336 inline ivec needed_bits(const ivec& v) 00337 { 00338 it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead."); 00339 return apply_function<int>(int2bits, v); 00340 } 00341 00343 inline ivec int2bits(const ivec& v) 00344 { 00345 return apply_function<int>(int2bits, v); 00346 } 00347 00349 inline ivec levels2bits(const ivec& v) 00350 { 00351 return apply_function<int>(levels2bits, v); 00352 } 00353 00355 00356 } // namespace itpp 00357 00358 #endif // #ifndef LOG_EXP_H 00359 00360 00361 00362
Generated on Wed Jan 20 23:03:04 2010 for IT++ by Doxygen 1.6.2