IT++ Logo

elem_math.h

Go to the documentation of this file.
00001 
00030 #ifndef ELEM_MATH_H
00031 #define ELEM_MATH_H
00032 
00033 #ifndef _MSC_VER
00034 #  include <itpp/config.h>
00035 #else
00036 #  include <itpp/config_msvc.h>
00037 #endif
00038 
00039 #include <itpp/base/help_functions.h>
00040 #include <itpp/base/converters.h>
00041 #include <cstdlib>
00042 
00043 
00046 
00047 #ifndef HAVE_TGAMMA
00049 double tgamma(double x);
00050 #endif
00051 
00052 #if !defined(HAVE_LGAMMA) || (HAVE_DECL_SIGNGAM != 1)
00054 double lgamma(double x);
00056 extern int signgam;
00057 #endif
00058 
00059 #ifndef HAVE_CBRT
00061 double cbrt(double x);
00062 #endif
00063 
00065 
00066 namespace itpp {
00067 
00070 
00071   // -------------------- sqr function --------------------
00072 
00074   inline double sqr(double x) { return (x * x); }
00076   inline double sqr(const std::complex<double>& x)
00077   {
00078     return (x.real() * x.real() + x.imag() * x.imag());
00079   }
00081   inline vec sqr(const vec &x) { return apply_function<double>(sqr, x); }
00083   inline mat sqr(const mat &x) { return apply_function<double>(sqr, x); }
00085   vec sqr(const cvec &x);
00087   mat sqr(const cmat &x);
00088 
00089 
00090   // -------------------- abs function --------------------
00091 
00093   inline vec abs(const vec &x) { return apply_function<double>(std::fabs, x); }
00095   inline mat abs(const mat &x) { return apply_function<double>(std::fabs, x); }
00097   inline ivec abs(const ivec &x) { return apply_function<int>(std::abs, x); }
00099   inline imat abs(const imat &x) { return apply_function<int>(std::abs, x); }
00101   vec abs(const cvec &x);
00103   mat abs(const cmat &x);
00104 
00105 
00106   // -------------------- sign/sgn functions --------------------
00107 
00109   inline double sign(double x)
00110   {
00111     return (x == 0.0 ? 0.0 : (x < 0.0 ? -1.0 : 1.0));
00112   }
00114   inline vec sign(const vec &x) { return apply_function<double>(sign, x); }
00116   inline mat sign(const mat &x) { return apply_function<double>(sign, x); }
00117 
00119   inline double sgn(double x) { return sign(x); }
00121   inline vec sgn(const vec &x) { return apply_function<double>(sign, x); }
00123   inline mat sgn(const mat &x) { return apply_function<double>(sign, x); }
00124 
00126   inline int sign_i(int x)
00127   {
00128     return (x == 0 ? 0 : (x < 0 ? -1 : 1));
00129   }
00131   inline ivec sign_i(const ivec &x) { return apply_function<int>(sign_i, x); }
00133   inline imat sign_i(const imat &x) { return apply_function<int>(sign_i, x); }
00134 
00136   inline int sgn_i(int x) { return sign_i(x); }
00138   inline ivec sgn_i(const ivec &x) { return apply_function<int>(sign_i, x); }
00140   inline imat sgn_i(const imat &x) { return apply_function<int>(sign_i, x); }
00141 
00143   inline int sign_i(double x)
00144   {
00145     return (x == 0.0 ? 0 : (x < 0.0 ? -1 : 1));
00146   }
00147 
00148   // -------------------- sqrt function --------------------
00149 
00151   inline vec sqrt(const vec &x) { return apply_function<double>(std::sqrt, x); }
00153   inline mat sqrt(const mat &x) { return apply_function<double>(std::sqrt, x); }
00154 
00155 
00156   // -------------------- gamma function --------------------
00157 
00159   inline double gamma(double x) { return tgamma(x); }
00161   inline vec gamma(const vec &x) { return apply_function<double>(gamma, x); }
00163   inline mat gamma(const mat &x) { return apply_function<double>(gamma, x); }
00164 
00165 
00166   // -------------------- rem function --------------------
00167 
00169   inline double rem(double x, double y) { return fmod(x, y); }
00171   inline vec rem(const vec &x, double y)
00172   {
00173     return apply_function<double>(rem, x, y);
00174   }
00176   inline vec rem(double x, const vec &y)
00177   {
00178     return apply_function<double>(rem, x, y);
00179   }
00181   inline mat rem(const mat &x, double y)
00182   {
00183     return apply_function<double>(rem, x, y);
00184   }
00186   inline mat rem(double x, const mat &y)
00187   {
00188     return apply_function<double>(rem, x, y);
00189   }
00190 
00191   // -------------------- mod function --------------------
00192 
00194   inline int mod(int k, int n)
00195   {
00196     return (n == 0) ? k : (k - n * floor_i(static_cast<double>(k) / n ));
00197   }
00198 
00199 
00200   // -------------------- factorial coefficient function --------------------
00201 
00203   double fact(int index);
00204 
00205 
00206   // -------------------- binomial coefficient function --------------------
00207 
00209   double binom(int n, int k);
00210 
00212   int binom_i(int n, int k);
00213 
00215   double log_binom(int n, int k);
00216 
00217 
00218   // -------------------- greatest common divisor function --------------------
00219 
00227   int gcd(int a, int b);
00228 
00229 
00230   // -------------------- complex related functions --------------------
00231 
00233   vec real(const cvec &x);
00235   mat real(const cmat &x);
00237   vec imag(const cvec &x);
00239   mat imag(const cmat &x);
00240 
00242   vec arg(const cvec &x);
00244   mat arg(const cmat &x);
00246   inline vec angle(const cvec &x) { return arg(x); }
00248   inline mat angle(const cmat &x) { return arg(x); }
00249 
00250   // Added due to a failure in MSVC++ .NET 2005, which crashes on this
00251   // code.
00252 #ifndef _MSC_VER
00254   inline cvec conj(const cvec &x)
00255   {
00256     return apply_function<std::complex<double> >(std::conj, x);
00257   }
00259   inline cmat conj(const cmat &x)
00260   {
00261     return apply_function<std::complex<double> >(std::conj, x);
00262   }
00263 #else
00265   cvec conj(const cvec &x);
00266 
00268   cmat conj(const cmat &x);
00269 #endif
00270 
00272 
00273 } // namespace itpp
00274 
00275 #endif // #ifndef ELEM_MATH_H
00276 
00277 
00278 
00279 
SourceForge Logo

Generated on Sun Dec 9 17:30:25 2007 for IT++ by Doxygen 1.5.4