root/win32/itpp-4.0.1/itpp/base/math/log_exp.h @ 44

Revision 35, 9.6 kB (checked in by mido, 16 years ago)

zasadni zmeny ve /win32

Line 
1/*!
2 * \file
3 * \brief Logarithmic and exponenential functions - header file
4 * \author Tony Ottosson, Adam Piatyszek and Conrad Sanderson
5 *
6 * -------------------------------------------------------------------------
7 *
8 * IT++ - C++ library of mathematical, signal processing, speech processing,
9 *        and communications classes and functions
10 *
11 * Copyright (C) 1995-2007  (see AUTHORS file for a list of contributors)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * -------------------------------------------------------------------------
28 */
29
30#ifndef LOG_EXP_H
31#define LOG_EXP_H
32
33#ifndef _MSC_VER
34#  include <itpp/config.h>
35#else
36#  include <itpp/config_msvc.h>
37#endif
38
39#include <itpp/base/help_functions.h>
40#include <limits>
41
42
43/*!
44 * \addtogroup logexpfunc
45 * @{
46 */
47
48#ifndef HAVE_LOG1P
49//! Lograrithm of an argument \c x plus one
50inline double log1p(double x) { return std::log(1.0 + x); }
51#endif
52
53#ifndef HAVE_LOG2
54#undef log2                     // This is required at least for Cygwin
55//! Base-2 logarithm
56inline double log2(double x)
57{
58  return (std::log(x) * 1.442695040888963387004650940070860087871551513671875);
59}
60#endif
61
62/*!
63 * @}
64 */
65
66
67namespace itpp {
68
69  //!\addtogroup logexpfunc
70  //!@{
71
72  // ----------------------------------------------------------------------
73  // scalar functions
74  // ----------------------------------------------------------------------
75
76  //! Base-b logarithm
77  inline double logb(double b, double x)
78  {
79    return (std::log(x) / std::log(b));
80  }
81
82  //! Calculate two to the power of x (2^x); x is integer
83  inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); }
84  //! Calculate two to the power of x (2^x)
85  inline double pow2(double x) { return pow(2.0, x); }
86
87  //! Calculate ten to the power of x (10^x)
88  inline double pow10(double x) { return pow(10.0, x); }
89
90  //! Decibel of x (10*log10(x))
91  inline double dB(double x) { return 10.0 * log10(x); }
92  //! Inverse of decibel of x
93  inline double inv_dB(double x) { return pow(10.0, 0.1 * x); }
94
95  //! Calculate the number of bits needed to represent an inteager \c n
96  inline int int2bits(int n)
97  {
98    it_assert(n >= 0, "int2bits(): Improper argument value");
99
100    if (n == 0)
101      return 1;
102
103    int b = 0;
104    while (n) {
105      n >>= 1;
106      ++b;
107    }
108    return b;
109  }
110
111  //! Calculate the number of bits needed to represent \c n different values (levels).
112  inline int levels2bits(int n)
113  {
114    it_assert(n > 0,"levels2bits(): Improper argument value");
115    return int2bits(--n);
116  }
117
118  //! Deprecated function. Please use int2bits() or levels2bits() instead.
119  inline int needed_bits(int n)
120  {
121    it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead.");
122    return int2bits(n);
123  }
124
125  //! Constant definition to speed up trunc_log() and trunc_exp()
126  const double log_double_max = std::log(std::numeric_limits<double>::max());
127  //! Constant definition to speed up trunc_log(), trunc_exp() and log_add()
128  const double log_double_min = std::log(std::numeric_limits<double>::min());
129
130  /*!
131    \brief Truncated natural logarithm function
132
133    This truncated function provides a solution in the cases when the
134    logarithm argument is less or equal to zero or infinity. The function
135    checks for such extreme values and use some kind of truncation
136    (saturation) before calculating the logarithm.
137
138    The truncated logarithm function can be used for calculation of
139    log-likelihood in soft demodulators, when numerical instability problem
140    might occur.
141  */
142  inline double trunc_log(double x)
143  {
144    if (std::numeric_limits<double>::is_iec559) {
145      if (x == std::numeric_limits<double>::infinity())
146        return log_double_max;
147      if (x <= 0)
148        return log_double_min;
149    }
150    return std::log(x);
151  }
152
153  /*!
154    \brief Truncated exponential function
155
156    This truncated function provides a solution in the case when the
157    exponent function results in infinity. The function checks for an
158    extreme value and use truncation (saturation) before calculating
159    the result.
160
161    The truncated exponential function can be used  when numerical
162    instability problem occurs for a standard exp function.
163  */
164  inline double trunc_exp(double x)
165  {
166    if (std::numeric_limits<double>::is_iec559
167        && (x >= log_double_max))
168      return std::numeric_limits<double>::max();
169    return std::exp(x);
170  }
171
172
173  //! Safe substitute for <tt>log(exp(log_a) + exp(log_b))</tt>
174  inline double log_add(double log_a, double log_b)
175  {
176    if (log_a < log_b) {
177      double tmp = log_a;
178      log_a = log_b;
179      log_b = tmp;
180    }
181    double negdelta = log_b - log_a;
182    if (negdelta < log_double_min)
183      return log_a;
184    else
185      return (log_a + log1p(std::exp(negdelta)));
186  }
187
188
189  // ----------------------------------------------------------------------
190  // functions on vectors and matrices
191  // ----------------------------------------------------------------------
192
193  //! Exp of the elements of a vector \c x
194  inline vec exp(const vec &x)
195  {
196    return apply_function<double>(std::exp, x);
197  }
198  //! Exp of the elements of a complex vector \c x
199  inline cvec exp(const cvec &x)
200  {
201    return apply_function<std::complex<double> >(std::exp, x);
202  }
203  //! Exp of the elements of a matrix \c m
204  inline mat exp(const mat &m)
205  {
206    return apply_function<double>(std::exp, m);
207  }
208  //! Exp of the elements of a complex matrix \c m
209  inline cmat exp(const cmat &m)
210  {
211    return apply_function<std::complex<double> >(std::exp, m);
212  }
213
214  //! Calculates x to the power of y (x^y)
215  inline vec pow(const double x, const vec &y)
216  {
217    return apply_function<double>(std::pow, x, y);
218  }
219  //! Calculates x to the power of y (x^y)
220  inline mat pow(const double x, const mat &y)
221  {
222    return apply_function<double>(std::pow, x, y);
223  }
224  //! Calculates x to the power of y (x^y)
225  inline vec pow(const vec &x, const double y)
226  {
227    return apply_function<double>(std::pow, x, y);
228  }
229  //! Calculates x to the power of y (x^y)
230  inline mat pow(const mat &x, const double y)
231  {
232    return apply_function<double>(std::pow, x, y);
233  }
234
235  //! Calculates two to the power of x (2^x)
236  inline vec pow2(const vec &x)
237  {
238    return apply_function<double>(pow2, x);
239  }
240  //! Calculates two to the power of x (2^x)
241  inline mat pow2(const mat &x)
242  {
243    return apply_function<double>(pow2, x);
244  }
245
246  //! Calculates ten to the power of x (10^x)
247  inline vec pow10(const vec &x)
248  {
249    return apply_function<double>(pow10, x);
250  }
251  //! Calculates ten to the power of x (10^x)
252  inline mat pow10(const mat &x)
253  {
254    return apply_function<double>(pow10, x);
255  }
256
257  //! The natural logarithm of the elements
258  inline vec log(const vec &x)
259  {
260    return apply_function<double>(std::log, x);
261  }
262  //! The natural logarithm of the elements
263  inline mat log(const mat &x)
264  {
265    return apply_function<double>(std::log, x);
266  }
267  //! The natural logarithm of the elements
268  inline cvec log(const cvec &x)
269  {
270    return apply_function<std::complex<double> >(std::log, x);
271  }
272  //! The natural logarithm of the elements
273  inline cmat log(const cmat &x)
274  {
275    return apply_function<std::complex<double> >(std::log, x);
276  }
277
278  //! log-2 of the elements
279  inline vec log2(const vec &x)
280  {
281    return apply_function<double>(::log2, x);
282  }
283  //! log-2 of the elements
284  inline mat log2(const mat &x)
285  {
286    return apply_function<double>(::log2, x);
287  }
288
289  //! log-10 of the elements
290  inline vec log10(const vec &x)
291  {
292    return apply_function<double>(std::log10, x);
293  }
294  //! log-10 of the elements
295  inline mat log10(const mat &x)
296  {
297    return apply_function<double>(std::log10, x);
298  }
299
300  //! log-b of \c x
301  inline vec logb(double b, const vec &x)
302  {
303    return apply_function<double>(itpp::logb, b, x);
304  }
305  //! log-b of \c x
306  inline mat logb(double b, const mat &x)
307  {
308    return apply_function<double>(itpp::logb, b, x);
309  }
310
311  //! Calculates 10*log10(x)
312  inline vec dB(const vec &x)
313  {
314    return apply_function<double>(dB, x);
315  }
316  //! Calculates 10*log10(x)
317  inline mat dB(const mat &x)
318  {
319    return apply_function<double>(dB, x);
320  }
321
322  //! Calulates the inverse of dB, 10^(x/10)
323  inline vec inv_dB(const vec &x)
324  {
325    return apply_function<double>(inv_dB, x);
326  }
327  //! Calculates the inverse of dB, 10^(x/10)
328  inline mat inv_dB(const mat &x)
329  {
330    return apply_function<double>(inv_dB, x);
331  }
332
333  //! Deprecated function. Please use int2bits() or levels2bits() instead.
334  inline ivec needed_bits(const ivec& v)
335  {
336    it_warning("needed_bits(): This function is depreceted. Depending on your needs, please use int2bits() or levels2bits() instead.");
337    return apply_function<int>(int2bits, v);
338  }
339
340  //! Calculate the number of bits needed to represent each inteager in a vector
341  inline ivec int2bits(const ivec& v)
342  {
343    return apply_function<int>(int2bits, v);
344  }
345
346  //! Calculate the number of bits needed to represent a numer of levels saved in a vector
347  inline ivec levels2bits(const ivec& v)
348  {
349    return apply_function<int>(levels2bits, v);
350  }
351
352  //!@}
353
354} // namespace itpp
355
356#endif // #ifndef LOG_EXP_H
357
358
359
Note: See TracBrowser for help on using the browser.