IT++ Logo Newcom Logo

scalfunc.h

Go to the documentation of this file.
00001 
00033 #ifndef SCALFUNC_H
00034 #define SCALFUNC_H
00035 
00036 #ifndef _MSC_VER
00037 #  include <itpp/config.h>
00038 #else
00039 #  include <itpp/config_msvc.h>
00040 #endif
00041 
00042 #include <itpp/base/itassert.h>
00043 #include <complex>
00044 #include <limits>
00045 
00046 
00048 
00049 
00050 #ifndef HAVE_ASINH
00052   double asinh(double x);
00053 #endif
00054 
00055 #ifndef HAVE_ACOSH
00057   double acosh(double x);
00058 #endif
00059 
00060 #ifndef HAVE_ATANH
00062   double atanh(double x);
00063 #endif
00065 
00066 
00067 #ifdef _MSC_VER
00068 // These functions are part of C99. But apparently not in Visual C++.
00073 double erf(double x);
00078 double erfc(double x);
00079 #endif
00080 
00081 #ifndef HAVE_TGAMMA
00082 
00086 double tgamma(double x);
00087 #endif
00088 
00089 #if !defined(HAVE_LGAMMA) || (HAVE_DECL_SIGNGAM != 1)
00090 
00094 double lgamma(double x);
00095 extern int signgam;
00096 #endif
00097 
00098 #ifndef HAVE_CBRT
00099 /*
00100   \brief Cubic root
00101   \ingroup miscfunc
00102 */
00103 double cbrt(double x);
00104 #endif
00105 
00106 
00107 // Fix log2 for some platforms, that have it defined as a macro
00108 #if defined (log2)
00109 #undef log2
00110 #endif
00111 
00112 namespace itpp {
00113 
00115   const double pi = 3.14159265358979323846;
00116 
00118   const double m_2pi = 2 * pi;
00119 
00121   const double eps = std::numeric_limits<double>::epsilon();
00122 
00124   const double log_double_max = std::log(std::numeric_limits<double>::max());
00126   const double log_double_min = std::log(std::numeric_limits<double>::min());
00127 
00140   inline double trunc_log(double x)
00141   {
00142     if (std::numeric_limits<double>::is_iec559) {
00143       if (x == std::numeric_limits<double>::infinity())
00144         return log_double_max;
00145       if (x <= 0)
00146         return log_double_min;
00147     }
00148     return std::log(x);
00149   }
00150 
00162   inline double trunc_exp(double x)
00163   {
00164     if (std::numeric_limits<double>::is_iec559
00165         && (x >= log_double_max))
00166       return std::numeric_limits<double>::max();
00167     return std::exp(x);
00168   }
00169 
00174   double gamma(double x);
00175 
00180   double Qfunc(double x);
00181 
00186   double erfinv(double x);
00187 
00208   std::complex<double> erf(const std::complex<double>& z);
00209 
00214   inline double log2(double x) { return (std::log(x)/0.693147180559945309417); }
00215 
00220   inline double logb(double b, double x) { return std::log(x)/std::log(b); }
00221 
00226   inline double sinc(double x) { return ( (x==0) ? 1 : (sin(itpp::pi*x)/itpp::pi/x) ); }
00227 
00229 
00230 
00231 #ifdef _MSC_VER
00233   inline double rint(double x) { return floor(x+0.5); }
00234 #endif
00235 
00237   inline int round_i(double x) { return int(rint(x)); }
00239   inline int ceil_i(double x) { return int(ceil(x)); }
00241   inline int floor_i(double x) { return int(floor(x)); }
00242 
00244   inline double round(double x) { return rint(x); }
00245 
00247   inline bool is_int(double x) { double dummy; return (modf(x, &dummy) == 0.0); }
00248 
00250   inline bool is_even(int x) { return ((x&1) == 0); }
00252 
00254 
00255 
00257   inline int int2bits(int n)
00258   {
00259     it_assert(n >= 0, "int2bits(): Improper argument value");
00260 
00261     if (n == 0)
00262       return 1;
00263 
00264     int b = 0;
00265     while (n) { 
00266       n >>= 1; 
00267       ++b; 
00268     }
00269     return b;
00270   }
00271   
00273   inline int levels2bits(int n)
00274   {
00275     it_assert(n > 0,"levels2bits(): Improper argument value");
00276     return int2bits(--n);
00277   }
00278 
00280   inline int needed_bits(int n)
00281   {
00282     it_warning("needed_bits(): This function is depreceted. Might be removed in future releases. Depending on your needs, please use int2bits() or levels2bits() instead.");
00283     return int2bits(n);
00284   }
00285 
00287   inline int needed_bits(double n) 
00288   { 
00289     it_warning("needed_bits(): This function is depreceted. Might be removed in future releases. Depending on your needs, please use int2bits() or levels2bits() instead.");
00290     it_assert(n > 0, "needed_bits(): Improper argument value"); 
00291     return ceil_i(log2(n));
00292   }
00293 
00295   inline int pow2i(int x) { return ((x < 0) ? 0 : (1 << x)); }
00297   inline int pow2(int x) { return pow2i(x); }
00298 
00300   inline double pow2(double x) { return pow(2.0, x); }
00302   inline double pow10(double x) { return pow(10.0, x); }
00303 
00305   inline double dB(double x) { return 10.0 * log10(x); }
00307   inline double inv_dB(double x) { return pow(10.0, x/10.0); }
00309 
00311 
00312 
00314   inline int gray_code(int x) { return x^(x>>1); }
00315 
00317   double binom(int n, int k);
00318 
00320   int binom_i(int n, int k);
00321 
00323   double log_binom(int n, int k);
00324 
00326   inline double rad_to_deg(double x) { return 180.0 / itpp::pi * x; }
00328   inline double deg_to_rad(double x) { return itpp::pi / 180.0 * x; }
00330 
00331 
00333 
00334 
00336   inline double sqr(double x) { return x*x; }
00338   inline double sqr(std::complex<double> x) { return (x.real()*x.real()+x.imag()*x.imag()); }
00340   inline double rem(double x, double y) {return fmod(x,y);}
00342   inline double sign(double x) { return x==0.0 ? 0.0 : (x<0.0 ? -1.0 : 1.0); }
00344   inline double sgn(double x) { return x==0.0 ? 0.0 : (x<0.0 ? -1.0 : 1.0); }
00345 
00347   inline signed char abs(signed char x) { return x>0 ? x : -x; }
00349   inline short abs(short x) { return x>0 ? x : -x; }
00351   inline int abs(int x) { return x>0 ? x : -x; }
00352 
00353   //double sigmoid(double x) { return 1.0/(1.0+exp(-x)); }
00354 
00356   double fact(int index);
00357 
00359   long mod(long k, long n);
00360 
00367   long gcd(long a, long b);
00368 
00370   inline double round_to_zero(double x, double threshold = 1e-14) {
00371     return (std::abs(x) < threshold) ? 0.0 : x;
00372   }
00373   
00375   std::complex<double> round_to_zero(const std::complex<double>& x, 
00376                                      double threshold = 1e-14);
00377 
00379 
00380 } // namespace itpp
00381 
00382 #endif // #ifndef SCALFUNC_H
SourceForge Logo

Generated on Wed Apr 18 11:23:31 2007 for IT++ by Doxygen 1.5.2