00001 00029 #ifndef LOG_EXP_H 00030 #define LOG_EXP_H 00031 00032 #include <itpp/base/help_functions.h> 00033 00034 00035 namespace itpp 00036 { 00037 00040 00041 // ---------------------------------------------------------------------- 00042 // scalar functions 00043 // ---------------------------------------------------------------------- 00044 00046 inline double logb(double b, double x) 00047 { 00048 return (std::log(x) / std::log(b)); 00049 } 00050 00052 inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); } 00054 inline double pow2(double x) { return pow(2.0, x); } 00055 00057 inline double pow10(double x) { return pow(10.0, x); } 00058 00060 inline double dB(double x) { return 10.0 * log10(x); } 00062 inline double inv_dB(double x) { return pow(10.0, 0.1 * x); } 00063 00065 inline int int2bits(int n) 00066 { 00067 it_assert(n >= 0, "int2bits(): Improper argument value"); 00068 00069 if (n == 0) 00070 return 1; 00071 00072 int b = 0; 00073 while (n) { 00074 n >>= 1; 00075 ++b; 00076 } 00077 return b; 00078 } 00079 00081 inline int levels2bits(int n) 00082 { 00083 it_assert(n > 0, "levels2bits(): Improper argument value"); 00084 return int2bits(--n); 00085 } 00086 00088 const double log_double_max = std::log(std::numeric_limits<double>::max()); 00090 const double log_double_min = std::log(std::numeric_limits<double>::min()); 00091 00104 inline double trunc_log(double x) 00105 { 00106 if (std::numeric_limits<double>::is_iec559) { 00107 if (x == std::numeric_limits<double>::infinity()) 00108 return log_double_max; 00109 if (x <= 0) 00110 return log_double_min; 00111 } 00112 return std::log(x); 00113 } 00114 00126 inline double trunc_exp(double x) 00127 { 00128 if (std::numeric_limits<double>::is_iec559 00129 && (x >= log_double_max)) 00130 return std::numeric_limits<double>::max(); 00131 return std::exp(x); 00132 } 00133 00134 00136 double log_add(double log_a, double log_b); 00137 00138 00139 // ---------------------------------------------------------------------- 00140 // functions on vectors and matrices 00141 // ---------------------------------------------------------------------- 00142 00144 inline vec exp(const vec &x) 00145 { 00146 return apply_function<double>(std::exp, x); 00147 } 00149 inline cvec exp(const cvec &x) 00150 { 00151 return apply_function<std::complex<double> >(std::exp, x); 00152 } 00154 inline mat exp(const mat &m) 00155 { 00156 return apply_function<double>(std::exp, m); 00157 } 00159 inline cmat exp(const cmat &m) 00160 { 00161 return apply_function<std::complex<double> >(std::exp, m); 00162 } 00163 00165 inline vec pow(const double x, const vec &y) 00166 { 00167 return apply_function<double>(std::pow, x, y); 00168 } 00170 inline mat pow(const double x, const mat &y) 00171 { 00172 return apply_function<double>(std::pow, x, y); 00173 } 00175 inline vec pow(const vec &x, const double y) 00176 { 00177 return apply_function<double>(std::pow, x, y); 00178 } 00180 inline mat pow(const mat &x, const double y) 00181 { 00182 return apply_function<double>(std::pow, x, y); 00183 } 00184 00186 inline vec pow2(const vec &x) 00187 { 00188 return apply_function<double>(pow2, x); 00189 } 00191 inline mat pow2(const mat &x) 00192 { 00193 return apply_function<double>(pow2, x); 00194 } 00195 00197 inline vec pow10(const vec &x) 00198 { 00199 return apply_function<double>(pow10, x); 00200 } 00202 inline mat pow10(const mat &x) 00203 { 00204 return apply_function<double>(pow10, x); 00205 } 00206 00208 inline vec log(const vec &x) 00209 { 00210 return apply_function<double>(std::log, x); 00211 } 00213 inline mat log(const mat &x) 00214 { 00215 return apply_function<double>(std::log, x); 00216 } 00218 inline cvec log(const cvec &x) 00219 { 00220 return apply_function<std::complex<double> >(std::log, x); 00221 } 00223 inline cmat log(const cmat &x) 00224 { 00225 return apply_function<std::complex<double> >(std::log, x); 00226 } 00227 00228 // Cygwin defines log2 macro conflicting with IT++ functions 00229 #if defined(log2) 00230 # undef log2 00231 #endif 00232 00233 vec log2(const vec &x); 00235 mat log2(const mat &x); 00236 00238 inline vec log10(const vec &x) 00239 { 00240 return apply_function<double>(std::log10, x); 00241 } 00243 inline mat log10(const mat &x) 00244 { 00245 return apply_function<double>(std::log10, x); 00246 } 00247 00249 inline vec logb(double b, const vec &x) 00250 { 00251 return apply_function<double>(itpp::logb, b, x); 00252 } 00254 inline mat logb(double b, const mat &x) 00255 { 00256 return apply_function<double>(itpp::logb, b, x); 00257 } 00258 00260 inline vec dB(const vec &x) 00261 { 00262 return apply_function<double>(dB, x); 00263 } 00265 inline mat dB(const mat &x) 00266 { 00267 return apply_function<double>(dB, x); 00268 } 00269 00271 inline vec inv_dB(const vec &x) 00272 { 00273 return apply_function<double>(inv_dB, x); 00274 } 00276 inline mat inv_dB(const mat &x) 00277 { 00278 return apply_function<double>(inv_dB, x); 00279 } 00280 00282 inline ivec int2bits(const ivec& v) 00283 { 00284 return apply_function<int>(int2bits, v); 00285 } 00286 00288 inline ivec levels2bits(const ivec& v) 00289 { 00290 return apply_function<int>(levels2bits, v); 00291 } 00292 00294 00295 } // namespace itpp 00296 00297 #endif // #ifndef LOG_EXP_H 00298 00299 00300 00301
Generated on Wed Jul 27 2011 16:27:04 for IT++ by Doxygen 1.7.4