File: log_exp.h

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (361 lines) | stat: -rw-r--r-- 9,900 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*!
 * \file
 * \brief Logarithmic and exponenential functions - header file
 * \author Tony Ottosson, Adam Piatyszek and Conrad Sanderson
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#ifndef LOG_EXP_H
#define LOG_EXP_H

#ifndef _MSC_VER
#  include <itpp/config.h>
#else
#  include <itpp/config_msvc.h>
#endif

#include <itpp/base/help_functions.h>
#include <itpp/base/math/misc.h>
#include <limits>


/*!
 * \addtogroup logexpfunc
 * @{
 */

#ifndef HAVE_LOG1P
//! Lograrithm of an argument \c x plus one
inline double log1p(double x) { return std::log(1.0 + x); }
#endif

#ifndef HAVE_LOG2
#undef log2                     // This is required at least for Cygwin
//! Base-2 logarithm
inline double log2(double x)
{
  return (std::log(x) * 1.442695040888963387004650940070860087871551513671875);
}
#endif

/*!
 * @}
 */


namespace itpp {

  //!\addtogroup logexpfunc
  //!@{

  // ----------------------------------------------------------------------
  // scalar functions
  // ----------------------------------------------------------------------

  //! Base-b logarithm
  inline double logb(double b, double x)
  {
    return (std::log(x) / std::log(b));
  }

  //! Calculate two to the power of x (2^x); x is integer
  inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); }
  //! Calculate two to the power of x (2^x)
  inline double pow2(double x) { return pow(2.0, x); }

  //! Calculate ten to the power of x (10^x)
  inline double pow10(double x) { return pow(10.0, x); }

  //! Decibel of x (10*log10(x))
  inline double dB(double x) { return 10.0 * log10(x); }
  //! Inverse of decibel of x
  inline double inv_dB(double x) { return pow(10.0, 0.1 * x); }

  //! Calculate the number of bits needed to represent an inteager \c n
  inline int int2bits(int n)
  {
    it_assert(n >= 0, "int2bits(): Improper argument value");

    if (n == 0)
      return 1;

    int b = 0;
    while (n) {
      n >>= 1;
      ++b;
    }
    return b;
  }

  //! Calculate the number of bits needed to represent \c n different values (levels).
  inline int levels2bits(int n)
  {
    it_assert(n > 0,"levels2bits(): Improper argument value");
    return int2bits(--n);
  }

  //! Deprecated function. Please use int2bits() or levels2bits() instead.
  inline int needed_bits(int n)
  {
    it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead.");
    return int2bits(n);
  }

  //! Constant definition to speed up trunc_log() and trunc_exp()
  const double log_double_max = std::log(std::numeric_limits<double>::max());
  //! Constant definition to speed up trunc_log(), trunc_exp() and log_add()
  const double log_double_min = std::log(std::numeric_limits<double>::min());

  /*!
    \brief Truncated natural logarithm function

    This truncated function provides a solution in the cases when the
    logarithm argument is less or equal to zero or infinity. The function
    checks for such extreme values and use some kind of truncation
    (saturation) before calculating the logarithm.

    The truncated logarithm function can be used for calculation of
    log-likelihood in soft demodulators, when numerical instability problem
    might occur.
  */
  inline double trunc_log(double x)
  {
    if (std::numeric_limits<double>::is_iec559) {
      if (x == std::numeric_limits<double>::infinity())
	return log_double_max;
      if (x <= 0)
	return log_double_min;
    }
    return std::log(x);
  }

  /*!
    \brief Truncated exponential function

    This truncated function provides a solution in the case when the
    exponent function results in infinity. The function checks for an
    extreme value and use truncation (saturation) before calculating
    the result.

    The truncated exponential function can be used  when numerical
    instability problem occurs for a standard exp function.
  */
  inline double trunc_exp(double x)
  {
    if (std::numeric_limits<double>::is_iec559
	&& (x >= log_double_max))
      return std::numeric_limits<double>::max();
    return std::exp(x);
  }


  //! Safe substitute for <tt>log(exp(log_a) + exp(log_b))</tt>
  inline double log_add(double log_a, double log_b)
  {
    if (log_a < log_b) {
      double tmp = log_a;
      log_a = log_b;
      log_b = tmp;
    }
    double negdelta = log_b - log_a;
    if ((negdelta < log_double_min) || std::isnan(negdelta))
      return log_a;
    else
      return (log_a + log1p(std::exp(negdelta)));
  }


  // ----------------------------------------------------------------------
  // functions on vectors and matrices
  // ----------------------------------------------------------------------

  //! Exp of the elements of a vector \c x
  inline vec exp(const vec &x)
  {
    return apply_function<double>(std::exp, x);
  }
  //! Exp of the elements of a complex vector \c x
  inline cvec exp(const cvec &x)
  {
    return apply_function<std::complex<double> >(std::exp, x);
  }
  //! Exp of the elements of a matrix \c m
  inline mat exp(const mat &m)
  {
    return apply_function<double>(std::exp, m);
  }
  //! Exp of the elements of a complex matrix \c m
  inline cmat exp(const cmat &m)
  {
    return apply_function<std::complex<double> >(std::exp, m);
  }

  //! Calculates x to the power of y (x^y)
  inline vec pow(const double x, const vec &y)
  {
    return apply_function<double>(std::pow, x, y);
  }
  //! Calculates x to the power of y (x^y)
  inline mat pow(const double x, const mat &y)
  {
    return apply_function<double>(std::pow, x, y);
  }
  //! Calculates x to the power of y (x^y)
  inline vec pow(const vec &x, const double y)
  {
    return apply_function<double>(std::pow, x, y);
  }
  //! Calculates x to the power of y (x^y)
  inline mat pow(const mat &x, const double y)
  {
    return apply_function<double>(std::pow, x, y);
  }

  //! Calculates two to the power of x (2^x)
  inline vec pow2(const vec &x)
  {
    return apply_function<double>(pow2, x);
  }
  //! Calculates two to the power of x (2^x)
  inline mat pow2(const mat &x)
  {
    return apply_function<double>(pow2, x);
  }

  //! Calculates ten to the power of x (10^x)
  inline vec pow10(const vec &x)
  {
    return apply_function<double>(pow10, x);
  }
  //! Calculates ten to the power of x (10^x)
  inline mat pow10(const mat &x)
  {
    return apply_function<double>(pow10, x);
  }

  //! The natural logarithm of the elements
  inline vec log(const vec &x)
  {
    return apply_function<double>(std::log, x);
  }
  //! The natural logarithm of the elements
  inline mat log(const mat &x)
  {
    return apply_function<double>(std::log, x);
  }
  //! The natural logarithm of the elements
  inline cvec log(const cvec &x)
  {
    return apply_function<std::complex<double> >(std::log, x);
  }
  //! The natural logarithm of the elements
  inline cmat log(const cmat &x)
  {
    return apply_function<std::complex<double> >(std::log, x);
  }

  //! log-2 of the elements
  inline vec log2(const vec &x)
  {
    return apply_function<double>(::log2, x);
  }
  //! log-2 of the elements
  inline mat log2(const mat &x)
  {
    return apply_function<double>(::log2, x);
  }

  //! log-10 of the elements
  inline vec log10(const vec &x)
  {
    return apply_function<double>(std::log10, x);
  }
  //! log-10 of the elements
  inline mat log10(const mat &x)
  {
    return apply_function<double>(std::log10, x);
  }

  //! log-b of \c x
  inline vec logb(double b, const vec &x)
  {
    return apply_function<double>(itpp::logb, b, x);
  }
  //! log-b of \c x
  inline mat logb(double b, const mat &x)
  {
    return apply_function<double>(itpp::logb, b, x);
  }

  //! Calculates 10*log10(x)
  inline vec dB(const vec &x)
  {
    return apply_function<double>(dB, x);
  }
  //! Calculates 10*log10(x)
  inline mat dB(const mat &x)
  {
    return apply_function<double>(dB, x);
  }

  //! Calulates the inverse of dB, 10^(x/10)
  inline vec inv_dB(const vec &x)
  {
    return apply_function<double>(inv_dB, x);
  }
  //! Calculates the inverse of dB, 10^(x/10)
  inline mat inv_dB(const mat &x)
  {
    return apply_function<double>(inv_dB, x);
  }

  //! Deprecated function. Please use int2bits() or levels2bits() instead.
  inline ivec needed_bits(const ivec& v)
  {
    it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead.");
    return apply_function<int>(int2bits, v);
  }

  //! Calculate the number of bits needed to represent each inteager in a vector
  inline ivec int2bits(const ivec& v)
  {
    return apply_function<int>(int2bits, v);
  }

  //! Calculate the number of bits needed to represent a numer of levels saved in a vector
  inline ivec levels2bits(const ivec& v)
  {
    return apply_function<int>(levels2bits, v);
  }

  //!@}

} // namespace itpp

#endif // #ifndef LOG_EXP_H