00001 00029 #ifndef ELEM_MATH_H 00030 #define ELEM_MATH_H 00031 00032 #include <itpp/base/help_functions.h> 00033 #include <itpp/base/converters.h> 00034 #include <cstdlib> // required by std::abs() 00035 00036 00037 namespace itpp 00038 { 00039 00042 00043 // -------------------- sqr function -------------------- 00044 00046 inline double sqr(double x) { return (x * x); } 00048 inline double sqr(const std::complex<double>& x) 00049 { 00050 return (x.real() * x.real() + x.imag() * x.imag()); 00051 } 00053 inline vec sqr(const vec &x) { return apply_function<double>(sqr, x); } 00055 inline mat sqr(const mat &x) { return apply_function<double>(sqr, x); } 00057 vec sqr(const cvec &x); 00059 mat sqr(const cmat &x); 00060 00061 00062 // -------------------- abs function -------------------- 00063 00065 inline vec abs(const vec &x) { return apply_function<double>(std::fabs, x); } 00067 inline mat abs(const mat &x) { return apply_function<double>(std::fabs, x); } 00069 inline ivec abs(const ivec &x) { return apply_function<int>(std::abs, x); } 00071 inline imat abs(const imat &x) { return apply_function<int>(std::abs, x); } 00073 vec abs(const cvec &x); 00075 mat abs(const cmat &x); 00076 00077 00078 // -------------------- sign/sgn functions -------------------- 00079 00081 inline double sign(double x) 00082 { 00083 return (x == 0.0 ? 0.0 : (x < 0.0 ? -1.0 : 1.0)); 00084 } 00086 inline vec sign(const vec &x) { return apply_function<double>(sign, x); } 00088 inline mat sign(const mat &x) { return apply_function<double>(sign, x); } 00089 00091 inline double sgn(double x) { return sign(x); } 00093 inline vec sgn(const vec &x) { return apply_function<double>(sign, x); } 00095 inline mat sgn(const mat &x) { return apply_function<double>(sign, x); } 00096 00098 inline int sign_i(int x) 00099 { 00100 return (x == 0 ? 0 : (x < 0 ? -1 : 1)); 00101 } 00103 inline ivec sign_i(const ivec &x) { return apply_function<int>(sign_i, x); } 00105 inline imat sign_i(const imat &x) { return apply_function<int>(sign_i, x); } 00106 00108 inline int sgn_i(int x) { return sign_i(x); } 00110 inline ivec sgn_i(const ivec &x) { return apply_function<int>(sign_i, x); } 00112 inline imat sgn_i(const imat &x) { return apply_function<int>(sign_i, x); } 00113 00115 inline int sign_i(double x) 00116 { 00117 return (x == 0.0 ? 0 : (x < 0.0 ? -1 : 1)); 00118 } 00119 00120 // -------------------- sqrt function -------------------- 00121 00123 inline vec sqrt(const vec &x) { return apply_function<double>(std::sqrt, x); } 00125 inline mat sqrt(const mat &x) { return apply_function<double>(std::sqrt, x); } 00126 00127 00128 // -------------------- gamma function -------------------- 00129 00131 double gamma(double x); 00133 vec gamma(const vec &x); 00135 mat gamma(const mat &x); 00136 00137 00138 // -------------------- rem function -------------------- 00139 00141 inline double rem(double x, double y) { return fmod(x, y); } 00143 inline vec rem(const vec &x, double y) 00144 { 00145 return apply_function<double>(rem, x, y); 00146 } 00148 inline vec rem(double x, const vec &y) 00149 { 00150 return apply_function<double>(rem, x, y); 00151 } 00153 inline mat rem(const mat &x, double y) 00154 { 00155 return apply_function<double>(rem, x, y); 00156 } 00158 inline mat rem(double x, const mat &y) 00159 { 00160 return apply_function<double>(rem, x, y); 00161 } 00162 00163 // -------------------- mod function -------------------- 00164 00166 inline int mod(int k, int n) 00167 { 00168 return (n == 0) ? k : (k - n * floor_i(static_cast<double>(k) / n)); 00169 } 00170 00171 00172 // -------------------- factorial coefficient function -------------------- 00173 00175 double fact(int index); 00176 00177 00178 // -------------------- binomial coefficient function -------------------- 00179 00181 double binom(int n, int k); 00182 00184 int binom_i(int n, int k); 00185 00187 double log_binom(int n, int k); 00188 00189 00190 // -------------------- greatest common divisor function -------------------- 00191 00199 int gcd(int a, int b); 00200 00201 00202 // -------------------- complex related functions -------------------- 00203 00205 vec real(const cvec &x); 00207 mat real(const cmat &x); 00209 vec imag(const cvec &x); 00211 mat imag(const cmat &x); 00212 00214 vec arg(const cvec &x); 00216 mat arg(const cmat &x); 00218 inline vec angle(const cvec &x) { return arg(x); } 00220 inline mat angle(const cmat &x) { return arg(x); } 00221 00222 // Added due to a failure in MSVC++ .NET 2005, which crashes on this 00223 // code. 00224 #ifndef _MSC_VER 00225 00226 inline cvec conj(const cvec &x) 00227 { 00228 return apply_function<std::complex<double> >(std::conj, x); 00229 } 00231 inline cmat conj(const cmat &x) 00232 { 00233 return apply_function<std::complex<double> >(std::conj, x); 00234 } 00235 #else 00236 00237 cvec conj(const cvec &x); 00238 00240 cmat conj(const cmat &x); 00241 #endif 00242 00244 00245 } // namespace itpp 00246 00247 #endif // #ifndef ELEM_MATH_H 00248 00249 00250 00251
Generated on Wed Jul 27 2011 16:27:04 for IT++ by Doxygen 1.7.4