IT++
4.3.1
Toggle main menu visibility
itpp
base
math
log_exp.h
Go to the documentation of this file.
1
28
29
#ifndef LOG_EXP_H
30
#define LOG_EXP_H
31
32
#include <cmath>
33
#include <
itpp/base/help_functions.h
>
34
#include <itpp/itexports.h>
35
36
namespace
itpp
37
{
38
41
42
// ----------------------------------------------------------------------
43
// scalar functions
44
// ----------------------------------------------------------------------
45
47
inline
double
logb
(
double
b,
double
x)
48
{
49
return
(std::log(x) / std::log(b));
50
}
51
53
inline
int
pow2i
(
int
x) {
return
((x < 0) ? 0 : (1 << x)); }
55
inline
double
pow2
(
double
x) {
return
std::pow(2.0, x); }
57
inline
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
68
inline
double
pow10
(
double
x) {
return
std::pow(10.0, x); }
69
71
inline
double
dB
(
double
x) {
return
10.0 * std::log10(x); }
73
inline
double
inv_dB
(
double
x) {
return
std::pow(10.0, 0.1 * x); }
74
76
inline
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
92
inline
int
levels2bits
(
int
n)
93
{
94
it_assert
(n > 0,
"levels2bits(): Improper argument value"
);
95
return
int2bits
(--n);
96
}
97
99
const
double
log_double_max
= std::log(std::numeric_limits<double>::max());
101
const
double
log_double_min
= std::log(std::numeric_limits<double>::min());
102
115
inline
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
137
inline
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
147
ITPP_EXPORT
double
log_add
(
double
log_a,
double
log_b);
148
149
150
// ----------------------------------------------------------------------
151
// functions on vectors and matrices
152
// ----------------------------------------------------------------------
153
155
inline
vec
exp
(
const
vec &x)
156
{
157
return
apply_function<double>
(std::exp, x);
158
}
159
160
inline
cvec
exp
(
const
cvec &x)
161
{
162
return
apply_function<std::complex<double>
>(std::exp, x);
163
}
164
165
inline
mat
exp
(
const
mat &m)
166
{
167
return
apply_function<double>
(std::exp, m);
168
}
169
170
inline
cmat
exp
(
const
cmat &m)
171
{
172
return
apply_function<std::complex<double>
>(std::exp, m);
173
}
174
176
inline
vec
pow
(
const
double
x,
const
vec &y)
177
{
178
return
apply_function<double>
(std::pow, x, y);
179
}
180
181
inline
mat
pow
(
const
double
x,
const
mat &y)
182
{
183
return
apply_function<double>
(std::pow, x, y);
184
}
185
186
inline
vec
pow
(
const
vec &x,
const
double
y)
187
{
188
return
apply_function<double>
(std::pow, x, y);
189
}
190
191
inline
mat
pow
(
const
mat &x,
const
double
y)
192
{
193
return
apply_function<double>
(std::pow, x, y);
194
}
195
197
inline
vec
pow2
(
const
vec &x)
198
{
199
return
apply_function<double>
(
pow2
, x);
200
}
201
203
inline
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
212
inline
mat
pow2
(
const
mat &x)
213
{
214
return
apply_function<double>
(
pow2
, x);
215
}
216
218
inline
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
230
inline
vec
pow10
(
const
vec &x)
231
{
232
return
apply_function<double>
(
pow10
, x);
233
}
234
235
inline
mat
pow10
(
const
mat &x)
236
{
237
return
apply_function<double>
(
pow10
, x);
238
}
239
241
inline
vec
log
(
const
vec &x)
242
{
243
return
apply_function<double>
(std::log, x);
244
}
245
246
inline
mat
log
(
const
mat &x)
247
{
248
return
apply_function<double>
(std::log, x);
249
}
250
251
inline
cvec
log
(
const
cvec &x)
252
{
253
return
apply_function<std::complex<double>
>(std::log, x);
254
}
255
256
inline
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
266
ITPP_EXPORT vec
log2
(
const
vec &x);
268
ITPP_EXPORT mat
log2
(
const
mat &x);
269
271
inline
vec
log10
(
const
vec &x)
272
{
273
return
apply_function<double>
(std::log10, x);
274
}
275
276
inline
mat
log10
(
const
mat &x)
277
{
278
return
apply_function<double>
(std::log10, x);
279
}
280
282
inline
vec
logb
(
double
b,
const
vec &x)
283
{
284
return
apply_function<double>
(
itpp::logb
, b, x);
285
}
286
287
inline
mat
logb
(
double
b,
const
mat &x)
288
{
289
return
apply_function<double>
(
itpp::logb
, b, x);
290
}
291
293
inline
vec
dB
(
const
vec &x)
294
{
295
return
apply_function<double>
(
dB
, x);
296
}
297
298
inline
mat
dB
(
const
mat &x)
299
{
300
return
apply_function<double>
(
dB
, x);
301
}
302
304
inline
vec
inv_dB
(
const
vec &x)
305
{
306
return
apply_function<double>
(
inv_dB
, x);
307
}
308
309
inline
mat
inv_dB
(
const
mat &x)
310
{
311
return
apply_function<double>
(
inv_dB
, x);
312
}
313
315
inline
ivec
int2bits
(
const
ivec& v)
316
{
317
return
apply_function<int>
(
int2bits
, v);
318
}
319
321
inline
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
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition
itassert.h:94
itpp::trunc_log
double trunc_log(double x)
Truncated natural logarithm function.
Definition
log_exp.h:115
itpp::pow
vec pow(const double x, const vec &y)
Calculates x to the power of y (x^y).
Definition
log_exp.h:176
itpp::log_add
double log_add(double log_a, double log_b)
Safe substitute for log(exp(log_a) + exp(log_b)).
Definition
log_exp.cpp:40
itpp::pow2i
int pow2i(int x)
Calculate two to the power of x (2^x); x is integer.
Definition
log_exp.h:53
itpp::logb
double logb(double b, double x)
Base-b logarithm.
Definition
log_exp.h:47
itpp::dB
double dB(double x)
Decibel of x (10*log10(x)).
Definition
log_exp.h:71
itpp::log
vec log(const vec &x)
The natural logarithm of the elements.
Definition
log_exp.h:241
itpp::int2bits
int int2bits(int n)
Calculate the number of bits needed to represent an integer n.
Definition
log_exp.h:76
itpp::log_double_min
const double log_double_min
Constant definition to speed up trunc_log(), trunc_exp() and log_add().
Definition
log_exp.h:101
itpp::pow2
double pow2(double x)
Calculate two to the power of x (2^x).
Definition
log_exp.h:55
itpp::log_double_max
const double log_double_max
Constant definition to speed up trunc_log() and trunc_exp().
Definition
log_exp.h:99
itpp::inv_dB
double inv_dB(double x)
Inverse of decibel of x.
Definition
log_exp.h:73
itpp::trunc_exp
double trunc_exp(double x)
Truncated exponential function.
Definition
log_exp.h:137
itpp::log2
vec log2(const vec &x)
log-2 of the elements
Definition
log_exp.cpp:36
itpp::exp
vec exp(const vec &x)
Exp of the elements of a vector x.
Definition
log_exp.h:155
itpp::pow10
double pow10(double x)
Calculate ten to the power of x (10^x).
Definition
log_exp.h:68
itpp::levels2bits
int levels2bits(int n)
Calculate the number of bits needed to represent n different values (levels).
Definition
log_exp.h:92
itpp::log10
vec log10(const vec &x)
log-10 of the elements
Definition
log_exp.h:271
itpp::apply_function
Vec< T > apply_function(T(*f)(T), const Vec< T > &v)
Help function to call for a function: Vec<T> function(Vec<T>).
Definition
help_functions.h:124
help_functions.h
Help functions to make functions with vec and mat as arguments.
itpp
itpp namespace
Definition
itmex.h:37
Generated by
1.17.0