| 1 |
/* dipy math functions |
| 2 |
* |
| 3 |
* To give some platform independence for simple math functions |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <math.h> |
| 7 |
#include "numpy/npy_math.h" |
| 8 |
|
| 9 |
#define DPY_PI NPY_PI |
| 10 |
|
| 11 |
/* From numpy npy_math.c.src commit b2f6792d284b0e9383093c30d51ec3a82e8312fd*/ |
| 12 |
double dpy_log2(double x) |
| 13 |
{ |
| 14 |
#ifdef HAVE_LOG2 |
| 15 |
return log2(x); |
| 16 |
#else |
| 17 |
return NPY_LOG2E*log(x); |
| 18 |
#endif |
| 19 |
} |
| 20 |
|
| 21 |
#if (defined(_WIN32) || defined(_WIN64)) && !defined(__GNUC__) |
| 22 |
#define fmin min |
| 23 |
#endif |
| 24 |
|
| 25 |
#define dpy_floor(x) floor((double)(x)) |
| 26 |
|
| 27 |
double dpy_rint(double x) |
| 28 |
{ |
| 29 |
#ifdef HAVE_RINT |
| 30 |
return rint(x); |
| 31 |
#else |
| 32 |
double y, r; |
| 33 |
|
| 34 |
y = dpy_floor(x); |
| 35 |
r = x - y; |
| 36 |
|
| 37 |
if (r > 0.5) { |
| 38 |
y += 1.0; |
| 39 |
} |
| 40 |
|
| 41 |
/* Round to nearest even */ |
| 42 |
if (r == 0.5) { |
| 43 |
r = y - 2.0*dpy_floor(0.5*y); |
| 44 |
if (r == 1.0) { |
| 45 |
y += 1.0; |
| 46 |
} |
| 47 |
} |
| 48 |
return y; |
| 49 |
#endif |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
int dpy_signbit(double x) |
| 54 |
{ |
| 55 |
#ifdef signbit |
| 56 |
return signbit(x); |
| 57 |
#else |
| 58 |
union |
| 59 |
{ |
| 60 |
double d; |
| 61 |
short s[4]; |
| 62 |
int i[2]; |
| 63 |
} u; |
| 64 |
|
| 65 |
u.d = x; |
| 66 |
|
| 67 |
#if NPY_SIZEOF_INT == 4 |
| 68 |
|
| 69 |
#ifdef WORDS_BIGENDIAN /* defined in pyconfig.h */ |
| 70 |
return u.i[0] < 0; |
| 71 |
#else |
| 72 |
return u.i[1] < 0; |
| 73 |
#endif |
| 74 |
|
| 75 |
#else /* NPY_SIZEOF_INT != 4 */ |
| 76 |
|
| 77 |
#ifdef WORDS_BIGENDIAN |
| 78 |
return u.s[0] < 0; |
| 79 |
#else |
| 80 |
return u.s[3] < 0; |
| 81 |
#endif |
| 82 |
|
| 83 |
#endif /* NPY_SIZEOF_INT */ |
| 84 |
|
| 85 |
#endif /*NPY_HAVE_DECL_SIGNBIT*/ |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
#ifndef NPY_HAVE_DECL_ISNAN |
| 90 |
#define dpy_isnan(x) ((x) != (x)) |
| 91 |
#else |
| 92 |
#ifdef _MSC_VER |
| 93 |
#define dpy_isnan(x) _isnan((x)) |
| 94 |
#else |
| 95 |
#define dpy_isnan(x) isnan(x) |
| 96 |
#endif |
| 97 |
#endif |
| 98 |
|
| 99 |
|
| 100 |
#ifndef NPY_HAVE_DECL_ISFINITE |
| 101 |
#ifdef _MSC_VER |
| 102 |
#define dpy_isfinite(x) _finite((x)) |
| 103 |
#else |
| 104 |
#define dpy_isfinite(x) !npy_isnan((x) + (-x)) |
| 105 |
#endif |
| 106 |
#else |
| 107 |
#define dpy_isfinite(x) isfinite((x)) |
| 108 |
#endif |
| 109 |
|
| 110 |
|
| 111 |
#ifndef NPY_HAVE_DECL_ISINF |
| 112 |
#define dpy_isinf(x) (!dpy_isfinite(x) && !dpy_isnan(x)) |
| 113 |
#else |
| 114 |
#ifdef _MSC_VER |
| 115 |
#define dpy_isinf(x) (!_finite((x)) && !_isnan((x))) |
| 116 |
#else |
| 117 |
#define dpy_isinf(x) isinf((x)) |
| 118 |
#endif |
| 119 |
#endif |