00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00024 #ifndef __scalfunc_h
00025 #define __scalfunc_h
00026
00027 #include <cmath>
00028 #include <algorithm>
00029 #include <cstdlib>
00030
00031 namespace SPUC {
00037 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00038
00039 #ifndef M_PI
00040 # define M_PI 3.14159265358979323846
00041 #endif
00042
00043 #ifndef PI
00044 const double PI=M_PI;
00045 #endif
00046
00047 const float maxfloat=1.0E30F;
00048
00049 #ifndef M_LOG2E
00050 # define M_LOG2E 1.4426950408889634074
00051 #endif
00052 #ifndef M_SQRT2
00053 # define M_SQRT2 1.41421356237309504880
00054 #endif
00055
00056 #endif //DOXYGEN_SHOULD_SKIP_THIS
00057
00058
00059
00060
00061 #ifdef _MSC_VER
00062
00067 double erf(double x);
00068
00073 double erfc(double x);
00074 #endif
00075
00080 double Qfunc(double x);
00081
00086 double erfinv(double x);
00087
00092 double gamma(double x);
00093
00094
00095 #ifdef _MSC_VER
00096 double lgamma(double x);
00097 extern int signgam;
00098 double cbrt(double x);
00099 #endif
00100
00101 #ifndef __CYGWIN__
00102
00110 inline double log2(double x) { return M_LOG2E*log(x); }
00111 #endif
00112
00117 inline double logb(double b, double x) { return log(x)/log(b); }
00118
00123 inline double sinc(double x) { return ( (x==0) ? 1 : (sin(PI*x)/PI/x) ); }
00124
00126
00127
00128 #ifdef _MSC_VER
00129
00130 inline double rint(double x) { return floor(x+0.5); }
00131 #endif
00132
00134 inline int round_i(double x) { return int(rint(x)); }
00136 inline int ceil_i(double x) { return int(ceil(x)); }
00138 inline int floor_i(double x) { return int(floor(x)); }
00140 inline double round(double x) { return rint(x); }
00141
00143 inline bool is_int(double x) { double dummy; return (modf(x, &dummy) == 0.0); }
00144
00146 inline bool is_even(int x) { return ((x&1) == 0); }
00148
00150
00151
00153 inline int needed_bits(int n)
00154 {
00155 int b=0;
00156
00157 n--; while (n) { n>>=1; b++; }
00158 return b;
00159 }
00160
00162 inline int needed_bits(double n) {
00163
00164 return int(ceil(log2(n))); }
00165
00167 #define pow2i(x) ((x)<0 ? 0 : (1<<(x)))
00168
00169 inline int pow2(int x) { return pow2i(x); }
00170
00172 inline double pow2(double x) { return pow(2.0, x); }
00174 inline double pow10(double x) { return pow(10.0, x); }
00175
00177 inline double dB(double x) { return 10.0 * log10(x); }
00179 inline double inv_dB(double x) { return pow(10.0, x/10.0); }
00181
00183
00184
00186 inline int gray_code(int x) { return x^(x>>1); }
00187
00189 double binom(int n, int k);
00190
00192 int binom_i(int n, int k);
00193
00195 double log_binom(int n, int k);
00196
00198 inline double rad_to_deg(double x) { return 180.0 / M_PI * x; }
00200 inline double deg_to_rad(double x) { return M_PI / 180.0 * x; }
00202
00203 #if !defined(__GLIBC__) || __GLIBC__ < 2
00204
00206
00207
00209 double asinh(double x);
00211 double acosh(double x);
00213 double atanh(double x);
00215
00216 #endif
00217
00219
00220
00222 inline double sqr(double x) { return x*x; }
00224 inline double rem(double x, double y) {return fmod(x,y);}
00226 inline double sign(double x) { return x==0.0 ? 0.0 : (x<0.0 ? -1.0 : 1.0); }
00228 inline double sgn(double x) { return x==0.0 ? 0.0 : (x<0.0 ? -1.0 : 1.0); }
00229
00231 inline signed char abs(signed char x) { return x>0 ? x : -x; }
00233 inline short abs(short x) { return x>0 ? x : -x; }
00234 #ifdef _MSC_VER // Included in cmath for gcc
00235
00236 inline double abs(double x) { return fabs(x); }
00237 #endif
00238
00239
00240
00242 double fact(int index);
00243
00245 long mod(long k, long n);
00246
00253 long gcd(long a, long b);
00254
00256 double integrate(double (*f)(double), double A, double B, double tol=0.001);
00257
00259 }
00260 #endif // __scalfunc_h