IT++ Logo
log_exp.h
Go to the documentation of this file.
1
29#ifndef LOG_EXP_H
30#define LOG_EXP_H
31
32#include <cmath>
34#include <itpp/itexports.h>
35
36namespace itpp
37{
38
41
42// ----------------------------------------------------------------------
43// scalar functions
44// ----------------------------------------------------------------------
45
47inline double logb(double b, double x)
48{
49 return (std::log(x) / std::log(b));
50}
51
53inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); }
55inline double pow2(double x) { return std::pow(2.0, x); }
57inline double pow2(int x)
58{
59#ifdef _MSC_VER
60 //use pow since it seems to be faster on MSVC
61 return std::pow(2.0, x);
62#else
63 return std::ldexp(1.0,x);
64#endif
65}
66
68inline double pow10(double x) { return std::pow(10.0, x); }
69
71inline double dB(double x) { return 10.0 * std::log10(x); }
73inline double inv_dB(double x) { return std::pow(10.0, 0.1 * x); }
74
76inline int int2bits(int n)
77{
78 it_assert(n >= 0, "int2bits(): Improper argument value");
79
80 if (n == 0)
81 return 1;
82
83 int b = 0;
84 while (n) {
85 n >>= 1;
86 ++b;
87 }
88 return b;
89}
90
92inline int levels2bits(int n)
93{
94 it_assert(n > 0, "levels2bits(): Improper argument value");
95 return int2bits(--n);
96}
97
99const double log_double_max = std::log(std::numeric_limits<double>::max());
101const double log_double_min = std::log(std::numeric_limits<double>::min());
102
115inline double trunc_log(double x)
116{
117 if (std::numeric_limits<double>::is_iec559) {
118 if (x == std::numeric_limits<double>::infinity())
119 return log_double_max;
120 if (x <= 0)
121 return log_double_min;
122 }
123 return std::log(x);
124}
125
137inline double trunc_exp(double x)
138{
139 if (std::numeric_limits<double>::is_iec559
140 && (x >= log_double_max))
141 return std::numeric_limits<double>::max();
142 return std::exp(x);
143}
144
145
147ITPP_EXPORT double log_add(double log_a, double log_b);
148
149
150// ----------------------------------------------------------------------
151// functions on vectors and matrices
152// ----------------------------------------------------------------------
153
155inline vec exp(const vec &x)
156{
157 return apply_function<double>(std::exp, x);
158}
160inline cvec exp(const cvec &x)
161{
162 return apply_function<std::complex<double> >(std::exp, x);
163}
165inline mat exp(const mat &m)
166{
167 return apply_function<double>(std::exp, m);
168}
170inline cmat exp(const cmat &m)
171{
172 return apply_function<std::complex<double> >(std::exp, m);
173}
174
176inline vec pow(const double x, const vec &y)
177{
178 return apply_function<double>(std::pow, x, y);
179}
181inline mat pow(const double x, const mat &y)
182{
183 return apply_function<double>(std::pow, x, y);
184}
186inline vec pow(const vec &x, const double y)
187{
188 return apply_function<double>(std::pow, x, y);
189}
191inline mat pow(const mat &x, const double y)
192{
193 return apply_function<double>(std::pow, x, y);
194}
195
197inline vec pow2(const vec &x)
198{
199 return apply_function<double>(pow2, x);
200}
201
203inline vec pow2(const ivec &x)
204{
205 vec out(x.length());
206 for(int i = 0; i < x.length(); i++)
207 out(i) = pow2(x(i));
208 return out;
209}
210
212inline mat pow2(const mat &x)
213{
214 return apply_function<double>(pow2, x);
215}
216
218inline mat pow2(const imat &x)
219{
220 mat out(x.rows(), x.cols());
221 for(int i = 0; i < x.rows(); i++) {
222 for(int j = 0; j < x.cols(); j++) {
223 out(i, j) = pow2(x(i, j));
224 }
225 }
226 return out;
227}
228
230inline vec pow10(const vec &x)
231{
232 return apply_function<double>(pow10, x);
233}
235inline mat pow10(const mat &x)
236{
237 return apply_function<double>(pow10, x);
238}
239
241inline vec log(const vec &x)
242{
243 return apply_function<double>(std::log, x);
244}
246inline mat log(const mat &x)
247{
248 return apply_function<double>(std::log, x);
249}
251inline cvec log(const cvec &x)
252{
253 return apply_function<std::complex<double> >(std::log, x);
254}
256inline cmat log(const cmat &x)
257{
258 return apply_function<std::complex<double> >(std::log, x);
259}
260
261// Cygwin defines log2 macro conflicting with IT++ functions
262#if defined(log2)
263# undef log2
264#endif
266ITPP_EXPORT vec log2(const vec &x);
268ITPP_EXPORT mat log2(const mat &x);
269
271inline vec log10(const vec &x)
272{
273 return apply_function<double>(std::log10, x);
274}
276inline mat log10(const mat &x)
277{
278 return apply_function<double>(std::log10, x);
279}
280
282inline vec logb(double b, const vec &x)
283{
284 return apply_function<double>(itpp::logb, b, x);
285}
287inline mat logb(double b, const mat &x)
288{
289 return apply_function<double>(itpp::logb, b, x);
290}
291
293inline vec dB(const vec &x)
294{
295 return apply_function<double>(dB, x);
296}
298inline mat dB(const mat &x)
299{
300 return apply_function<double>(dB, x);
301}
302
304inline vec inv_dB(const vec &x)
305{
306 return apply_function<double>(inv_dB, x);
307}
309inline mat inv_dB(const mat &x)
310{
311 return apply_function<double>(inv_dB, x);
312}
313
315inline ivec int2bits(const ivec& v)
316{
317 return apply_function<int>(int2bits, v);
318}
319
321inline ivec levels2bits(const ivec& v)
322{
323 return apply_function<int>(levels2bits, v);
324}
325
327
328} // namespace itpp
329
330#endif // #ifndef LOG_EXP_H
331
332
333
334
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
double trunc_log(double x)
Truncated natural logarithm function.
Definition: log_exp.h:115
vec pow(const double x, const vec &y)
Calculates x to the power of y (x^y)
Definition: log_exp.h:176
double log_add(double log_a, double log_b)
Safe substitute for log(exp(log_a) + exp(log_b))
Definition: log_exp.cpp:40
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
Definition: log_exp.h:53
double logb(double b, double x)
Base-b logarithm.
Definition: log_exp.h:47
double dB(double x)
Decibel of x (10*log10(x))
Definition: log_exp.h:71
vec log(const vec &x)
The natural logarithm of the elements.
Definition: log_exp.h:241
int int2bits(int n)
Calculate the number of bits needed to represent an integer n.
Definition: log_exp.h:76
const double log_double_min
Constant definition to speed up trunc_log(), trunc_exp() and log_add()
Definition: log_exp.h:101
double pow2(double x)
Calculate two to the power of x (2^x)
Definition: log_exp.h:55
const double log_double_max
Constant definition to speed up trunc_log() and trunc_exp()
Definition: log_exp.h:99
double inv_dB(double x)
Inverse of decibel of x.
Definition: log_exp.h:73
double trunc_exp(double x)
Truncated exponential function.
Definition: log_exp.h:137
vec log2(const vec &x)
log-2 of the elements
Definition: log_exp.cpp:36
vec exp(const vec &x)
Exp of the elements of a vector x.
Definition: log_exp.h:155
double pow10(double x)
Calculate ten to the power of x (10^x)
Definition: log_exp.h:68
int levels2bits(int n)
Calculate the number of bits needed to represent n different values (levels).
Definition: log_exp.h:92
vec log10(const vec &x)
log-10 of the elements
Definition: log_exp.h:271
Help functions to make functions with vec and mat as arguments.
itpp namespace
Definition: itmex.h:37
SourceForge Logo

Generated on Tue Jan 24 2023 00:00:00 for IT++ by Doxygen 1.9.5