log_exp.h
Go to the documentation of this file.
#ifndef LOG_EXP_H
#define LOG_EXP_H
#include <cmath>
#include <itpp/base/help_functions.h>
#include <itpp/itexports.h>
namespace itpp
{
// ----------------------------------------------------------------------
// scalar functions
// ----------------------------------------------------------------------
inline double logb(double b, double x)
{
return (std::log(x) / std::log(b));
}
inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); }
inline double pow2(double x) { return std::pow(2.0, x); }
inline double pow2(int x)
{
#ifdef _MSC_VER
//use pow since it seems to be faster on MSVC
return std::pow(2.0, x);
#else
return std::ldexp(1.0,x);
#endif
}
inline double pow10(double x) { return std::pow(10.0, x); }
inline double dB(double x) { return 10.0 * std::log10(x); }
inline double inv_dB(double x) { return std::pow(10.0, 0.1 * x); }
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;
}
inline int levels2bits(int n)
{
it_assert(n > 0, "levels2bits(): Improper argument value");
return int2bits(--n);
}
const double log_double_max = std::log(std::numeric_limits<double>::max());
const double log_double_min = std::log(std::numeric_limits<double>::min());
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);
}
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);
}
ITPP_EXPORT double log_add(double log_a, double log_b);
// ----------------------------------------------------------------------
// functions on vectors and matrices
// ----------------------------------------------------------------------
inline vec exp(const vec &x)
{
return apply_function<double>(std::exp, x);
}
inline cvec exp(const cvec &x)
{
return apply_function<std::complex<double> >(std::exp, x);
}
inline mat exp(const mat &m)
{
return apply_function<double>(std::exp, m);
}
inline cmat exp(const cmat &m)
{
return apply_function<std::complex<double> >(std::exp, m);
}
inline vec pow(const double x, const vec &y)
{
return apply_function<double>(std::pow, x, y);
}
inline mat pow(const double x, const mat &y)
{
return apply_function<double>(std::pow, x, y);
}
inline vec pow(const vec &x, const double y)
{
return apply_function<double>(std::pow, x, y);
}
inline mat pow(const mat &x, const double y)
{
return apply_function<double>(std::pow, x, y);
}
inline vec pow2(const vec &x)
{
return apply_function<double>(pow2, x);
}
inline vec pow2(const ivec &x)
{
vec out(x.length());
for(int i = 0; i < x.length(); i++)
out(i) = pow2(x(i));
return out;
}
inline mat pow2(const mat &x)
{
return apply_function<double>(pow2, x);
}
inline mat pow2(const imat &x)
{
mat out(x.rows(), x.cols());
for(int i = 0; i < x.rows(); i++) {
for(int j = 0; j < x.cols(); j++) {
out(i, j) = pow2(x(i, j));
}
}
return out;
}
inline vec pow10(const vec &x)
{
return apply_function<double>(pow10, x);
}
inline mat pow10(const mat &x)
{
return apply_function<double>(pow10, x);
}
inline vec log(const vec &x)
{
return apply_function<double>(std::log, x);
}
inline mat log(const mat &x)
{
return apply_function<double>(std::log, x);
}
inline cvec log(const cvec &x)
{
return apply_function<std::complex<double> >(std::log, x);
}
inline cmat log(const cmat &x)
{
return apply_function<std::complex<double> >(std::log, x);
}
// Cygwin defines log2 macro conflicting with IT++ functions
#if defined(log2)
# undef log2
#endif
ITPP_EXPORT vec log2(const vec &x);
ITPP_EXPORT mat log2(const mat &x);
inline vec log10(const vec &x)
{
return apply_function<double>(std::log10, x);
}
inline mat log10(const mat &x)
{
return apply_function<double>(std::log10, x);
}
inline vec logb(double b, const vec &x)
{
return apply_function<double>(itpp::logb, b, x);
}
inline mat logb(double b, const mat &x)
{
return apply_function<double>(itpp::logb, b, x);
}
inline vec dB(const vec &x)
{
return apply_function<double>(dB, x);
}
inline mat dB(const mat &x)
{
return apply_function<double>(dB, x);
}
inline vec inv_dB(const vec &x)
{
return apply_function<double>(inv_dB, x);
}
inline mat inv_dB(const mat &x)
{
return apply_function<double>(inv_dB, x);
}
inline ivec int2bits(const ivec& v)
{
return apply_function<int>(int2bits, v);
}
inline ivec levels2bits(const ivec& v)
{
return apply_function<int>(levels2bits, v);
}
} // namespace itpp
#endif // #ifndef LOG_EXP_H
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
cmat log(const cmat &x)
The natural logarithm of the elements.
Definition: log_exp.h:256
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::pow
mat pow(const mat &x, const double y)
Calculates x to the power of y (x^y)
Definition: log_exp.h:191
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::exp
cmat exp(const cmat &m)
Exp of the elements of a complex matrix m.
Definition: log_exp.h:170
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::log10
mat log10(const mat &x)
log-10 of the elements
Definition: log_exp.h:276
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::min
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
itpp::max
T max(const Vec< T > &v)
Maximum value of vector.
Definition: min_max.h:45
help_functions.h
Help functions to make functions with vec and mat as arguments.
itpp
itpp namespace
Definition: itmex.h:37

Generated on Tue Aug 17 2021 10:59:15 for IT++ by Doxygen 1.9.1