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 00048 00049 double tgamma(double x); 00050 #endif 00051 00052 #if !defined(HAVE_LGAMMA) || (HAVE_DECL_SIGNGAM != 1) 00053 00054 double lgamma(double x); 00056 extern int signgam; 00057 #endif 00058 00059 #ifndef HAVE_CBRT 00060 00061 double cbrt(double x); 00062 #endif 00063 00065 00066 namespace itpp 00067 { 00068 00071 00072 // -------------------- sqr function -------------------- 00073 00075 inline double sqr(double x) { return (x * x); } 00077 inline double sqr(const std::complex<double>& x) 00078 { 00079 return (x.real() * x.real() + x.imag() * x.imag()); 00080 } 00082 inline vec sqr(const vec &x) { return apply_function<double>(sqr, x); } 00084 inline mat sqr(const mat &x) { return apply_function<double>(sqr, x); } 00086 vec sqr(const cvec &x); 00088 mat sqr(const cmat &x); 00089 00090 00091 // -------------------- abs function -------------------- 00092 00094 inline vec abs(const vec &x) { return apply_function<double>(std::fabs, x); } 00096 inline mat abs(const mat &x) { return apply_function<double>(std::fabs, x); } 00098 inline ivec abs(const ivec &x) { return apply_function<int>(std::abs, x); } 00100 inline imat abs(const imat &x) { return apply_function<int>(std::abs, x); } 00102 vec abs(const cvec &x); 00104 mat abs(const cmat &x); 00105 00106 00107 // -------------------- sign/sgn functions -------------------- 00108 00110 inline double sign(double x) 00111 { 00112 return (x == 0.0 ? 0.0 : (x < 0.0 ? -1.0 : 1.0)); 00113 } 00115 inline vec sign(const vec &x) { return apply_function<double>(sign, x); } 00117 inline mat sign(const mat &x) { return apply_function<double>(sign, x); } 00118 00120 inline double sgn(double x) { return sign(x); } 00122 inline vec sgn(const vec &x) { return apply_function<double>(sign, x); } 00124 inline mat sgn(const mat &x) { return apply_function<double>(sign, x); } 00125 00127 inline int sign_i(int x) 00128 { 00129 return (x == 0 ? 0 : (x < 0 ? -1 : 1)); 00130 } 00132 inline ivec sign_i(const ivec &x) { return apply_function<int>(sign_i, x); } 00134 inline imat sign_i(const imat &x) { return apply_function<int>(sign_i, x); } 00135 00137 inline int sgn_i(int x) { return sign_i(x); } 00139 inline ivec sgn_i(const ivec &x) { return apply_function<int>(sign_i, x); } 00141 inline imat sgn_i(const imat &x) { return apply_function<int>(sign_i, x); } 00142 00144 inline int sign_i(double x) 00145 { 00146 return (x == 0.0 ? 0 : (x < 0.0 ? -1 : 1)); 00147 } 00148 00149 // -------------------- sqrt function -------------------- 00150 00152 inline vec sqrt(const vec &x) { return apply_function<double>(std::sqrt, x); } 00154 inline mat sqrt(const mat &x) { return apply_function<double>(std::sqrt, x); } 00155 00156 00157 // -------------------- gamma function -------------------- 00158 00160 inline double gamma(double x) { return tgamma(x); } 00162 inline vec gamma(const vec &x) { return apply_function<double>(tgamma, x); } 00164 inline mat gamma(const mat &x) { return apply_function<double>(tgamma, x); } 00165 00166 00167 // -------------------- rem function -------------------- 00168 00170 inline double rem(double x, double y) { return fmod(x, y); } 00172 inline vec rem(const vec &x, double y) 00173 { 00174 return apply_function<double>(rem, x, y); 00175 } 00177 inline vec rem(double x, const vec &y) 00178 { 00179 return apply_function<double>(rem, x, y); 00180 } 00182 inline mat rem(const mat &x, double y) 00183 { 00184 return apply_function<double>(rem, x, y); 00185 } 00187 inline mat rem(double x, const mat &y) 00188 { 00189 return apply_function<double>(rem, x, y); 00190 } 00191 00192 // -------------------- mod function -------------------- 00193 00195 inline int mod(int k, int n) 00196 { 00197 return (n == 0) ? k : (k - n * floor_i(static_cast<double>(k) / n)); 00198 } 00199 00200 00201 // -------------------- factorial coefficient function -------------------- 00202 00204 double fact(int index); 00205 00206 00207 // -------------------- binomial coefficient function -------------------- 00208 00210 double binom(int n, int k); 00211 00213 int binom_i(int n, int k); 00214 00216 double log_binom(int n, int k); 00217 00218 00219 // -------------------- greatest common divisor function -------------------- 00220 00228 int gcd(int a, int b); 00229 00230 00231 // -------------------- complex related functions -------------------- 00232 00234 vec real(const cvec &x); 00236 mat real(const cmat &x); 00238 vec imag(const cvec &x); 00240 mat imag(const cmat &x); 00241 00243 vec arg(const cvec &x); 00245 mat arg(const cmat &x); 00247 inline vec angle(const cvec &x) { return arg(x); } 00249 inline mat angle(const cmat &x) { return arg(x); } 00250 00251 // Added due to a failure in MSVC++ .NET 2005, which crashes on this 00252 // code. 00253 #ifndef _MSC_VER 00254 00255 inline cvec conj(const cvec &x) 00256 { 00257 return apply_function<std::complex<double> >(std::conj, x); 00258 } 00260 inline cmat conj(const cmat &x) 00261 { 00262 return apply_function<std::complex<double> >(std::conj, x); 00263 } 00264 #else 00265 00266 cvec conj(const cvec &x); 00267 00269 cmat conj(const cmat &x); 00270 #endif 00271 00273 00274 } // namespace itpp 00275 00276 #endif // #ifndef ELEM_MATH_H 00277 00278 00279 00280
Generated on Tue Dec 6 2011 16:51:47 for IT++ by Doxygen 1.7.4