20 #ifndef __MATHEMATICS_H_
21 #define __MATHEMATICS_H_
31 #ifndef _USE_MATH_DEFINES
32 #define _USE_MATH_DEFINES
38 #include <sys/types.h>
53 #define cygwin_log2 log2
58 #define M_PI 3.14159265358979323846
67 #define isfinite _isfinite
72 #define RNG_SEED_SIZE 256
75 #define RADIX_STACK_SIZE 512
78 #define radix_push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
79 #define radix_pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
81 #ifndef DOXYGEN_SHOULD_SKIP_THIS
83 template <
class T>
struct radix_stack_t
96 template <
class T1,
class T2>
struct thread_qsort
106 int32_t* qsort_threads;
112 #endif // DOXYGEN_SHOULD_SKIP_THIS
114 #define COMPLEX128_ERROR_ONEARG(function) \
115 static inline complex128_t function(complex128_t a) \
117 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
119 return complex128_t(0.0, 0.0); \
122 #define COMPLEX128_STDMATH(function) \
123 static inline complex128_t function(complex128_t a) \
125 return std::function(a); \
155 static inline T
min(T a, T b)
157 return (a<=b) ? a : b;
162 static inline T
max(T a, T b)
164 return (a>=b) ? a : b;
169 static inline T
clamp(T value, T lb, T ub)
215 const T diff = CMath::abs<T>((a-b));
229 static inline bool fequals(
const T& a,
const T& b,
230 const float64_t eps,
bool tolerant=
false)
232 const T absA = CMath::abs<T>(a);
233 const T absB = CMath::abs<T>(b);
234 const T diff = CMath::abs<T>((a-b));
243 return CMath::fequals_abs<T>(a, b, eps);
256 else if ( (a==0) || (b==0) || (diff < comp) )
257 return (diff<(eps * comp));
262 T check = ((diff/(absA + absB)) > diff)?
263 (diff/(absA + absB)):diff;
264 return (check < eps);
290 return ::floor(d+0.5);
309 else return (a<0) ? (-1) : (+1);
314 static inline void swap(T &a,T &b)
324 static inline T
sq(T x)
371 tmp.i = 0x5f3759d5 - (tmp.i >> 1);
373 x = x*(1.5f - xhalf*x*x);
383 return ::powl((
long double) x, (
long double) n);
385 return ::pow((
double) x, (
double) n);
389 static inline int32_t
pow(
bool x, int32_t n)
394 static inline int32_t
pow(int32_t x, int32_t n)
415 return ::pow((
double)x, (
double)n);
420 return ::pow((
double) x, (
double) n);
426 return std::pow(x, n);
431 return std::pow(x, n);
436 return std::pow(x, n);
441 return std::pow(x, n);
446 return ::exp((
double) x);
455 return ::tan((
double) x);
464 return ::atan((
double) x);
473 return ::atan2((
double) x, (
double) y);
482 return ::tanh((
double) x);
490 return ::log(v)/
::log(10.0);
501 return ::log(v)/
::log(2.0);
516 for (i = 0; n != 0; i++)
578 for (
int i=1; i<len; i++)
579 area += 0.5*(xy[2*i]-xy[2*(i-1)])*(xy[2*i+1]+xy[2*(i-1)+1]);
583 for (
int i=1; i<len; i++)
584 area += 0.5*(xy[2*i+1]-xy[2*(i-1)+1])*(xy[2*i]+xy[2*(i-1)]);
597 for (
int i=2; i<=n; i++)
617 static inline uint64_t
random(uint64_t min_value, uint64_t max_value)
622 static inline int64_t
random(int64_t min_value, int64_t max_value)
627 static inline uint32_t
random(uint32_t min_value, uint32_t max_value)
632 static inline int32_t
random(int32_t min_value, int32_t max_value)
666 rand_s = rand_u*rand_u + rand_v*rand_v;
667 }
while ((rand_s == 0) || (rand_s >= 1));
670 ret = rand_u*
sqrt(-2.0*
log(rand_s)/rand_s);
671 ret = std_dev*ret + mean;
715 static inline int64_t
nchoosek(int32_t n, int32_t k)
719 for (int32_t i=n-k+1; i<=n; i++)
764 for (from_idx=0; from_idx<values.
vlen; ++from_idx)
766 if (from_idx!=min_index)
768 values_without_X0[to_idx]=
exp(values[from_idx]-X0);
791 static void sort(int32_t *a, int32_t cols, int32_t sort_col=0);
797 static void qsort(T* output, int32_t size)
804 if (output[0] > output [1])
809 T
split=output[size/2];
812 int32_t right=size-1;
816 while (output[left] < split)
818 while (output[right] > split)
830 qsort(output,right+1);
833 qsort(&output[left],size-left);
841 for (int32_t i=0; i<size-1; i++)
845 while (j >= 0 && output[j] > value)
847 output[j+1] = output[j];
867 static inline uint8_t
byte(T word, uint16_t p)
869 return (word >> (
sizeof(T)-p-1) * 8) & 0xff;
875 SG_SERROR(
"CMath::byte():: Not supported for complex128_t\n");
882 static size_t count[256], nc, cmin;
886 T *an, *aj, *pile[256];
901 for (ak = array; ak < an; ak++) {
929 for (cp = count + cmin; nc > 0; cp++) {
944 pile[cp - count] = ak += *cp;
968 for (r = *aj; aj < (ak = --pile[c =
byte(r, i)]);)
981 SG_SERROR(
"CMath::radix_sort_helper():: Not supported for complex128_t\n");
1001 if (*vector[0]>*vector[1])
1002 swap(vector[0],vector[1]);
1005 T*
split=vector[length/2];
1008 int32_t right=length-1;
1012 while (*vector[left]<*split)
1014 while (*vector[right]>*split)
1019 swap(vector[left],vector[right]);
1026 qsort(vector,right+1);
1029 qsort(&vector[left],length-left);
1035 SG_SERROR(
"CMath::qsort():: Not supported for complex128_t\n");
1039 template <
class T>
static void display_bits(T word, int32_t width=8*
sizeof(T))
1042 for (
int i=0; i<width; i++)
1044 T mask = ((T) 1)<<(
sizeof(T)*8-1);
1061 SG_SERROR(
"CMath::display_bits():: Not supported for complex128_t\n");
1069 template <
class T1,
class T2>
1070 static void qsort_index(T1* output, T2* index, uint32_t size);
1076 SG_SERROR(
"CMath::qsort_index():: Not supported for complex128_t\n");
1084 template <
class T1,
class T2>
1086 T1* output, T2* index, int32_t size);
1093 SG_SERROR(
"CMath::qsort_backword_index():: \
1094 Not supported for complex128_t\n");
1104 template <
class T1,
class T2>
1105 inline static void parallel_qsort_index(T1* output, T2* index, uint32_t size, int32_t n_threads, int32_t limit=262144)
1108 thread_qsort<T1,T2> t;
1114 t.num_threads=n_threads;
1115 parallel_qsort_index<T1,T2>(&t);
1121 uint32_t size, int32_t n_threads, int32_t limit=0)
1123 SG_SERROR(
"CMath::parallel_qsort_index():: Not supported for complex128_t\n");
1127 template <
class T1,
class T2>
1134 static void min(
float64_t* output, T* index, int32_t size);
1139 SG_SERROR(
"CMath::min():: Not supported for complex128_t\n");
1146 float64_t* output, T* index, int32_t size, int32_t n);
1150 int32_t size, int32_t n)
1152 SG_SERROR(
"CMath::nmin():: Not supported for complex128_t\n");
1170 int32_t middle=(start+end)/2;
1172 if (output[middle]>elem)
1174 else if (output[middle]<elem)
1186 SG_SERROR(
"CMath::binary_search_helper():: Not supported for complex128_t\n");
1196 if (ind >= 0 && output[ind] == elem)
1204 SG_SERROR(
"CMath::binary_search():: Not supported for complex128_t\n");
1221 int32_t end=length-1;
1228 int32_t middle=(start+end)/2;
1230 if (*vector[middle]>*elem)
1232 else if (*vector[middle]<*elem)
1241 if (start>=0&&*vector[start]==*elem)
1250 SG_SERROR(
"CMath::binary_search():: Not supported for complex128_t\n");
1256 T* output, int32_t size, T elem)
1260 if (output[ind]<=elem)
1262 if (ind>0 && output[ind-1] <= elem)
1271 SG_SERROR(
"CMath::binary_search_max_lower_equal():: \
1272 Not supported for complex128_t\n");
1279 char * seq1,
char* seq2, int32_t l1, int32_t l2,
float64_t gapCost);
1309 inline static uint32_t get_log_accuracy()
1312 return CMath::LOGACCURACY;
1323 static int is_nan(
double f);
1345 SG_SWARNING(
"INVALID second operand to logsum(%f,%f) expect undefined results\n", p, q)
1350 return diff >
LOGRANGE? p : p + logtable[(
int)(diff * LOGACCURACY)];
1351 return -diff >
LOGRANGE? q : q + logtable[(
int)(-diff * LOGACCURACY)];
1355 static
void init_log_table();
1358 static int32_t determine_logrange();
1361 static int32_t determine_logaccuracy(int32_t range);
1378 #ifdef USE_LOGSUMARRAY
1384 static inline float64_t logarithmic_sum_array(
1398 if (len%2==1) pp++ ;
1399 for (
register int32_t j=0; j < len>>1; j++)
1402 return logarithmic_sum_array(p,len%2 + (len>>1)) ;
1404 #endif //USE_LOGSUMARRAY
1408 virtual const char*
get_name()
const {
return "Math"; }
1451 static int32_t LOGACCURACY;
1459 template <
class T1,
class T2>
1462 struct thread_qsort<T1,T2>* ps=(thread_qsort<T1,T2>*) p;
1463 T1* output=ps->output;
1464 T2* index=ps->index;
1465 int32_t size=ps->size;
1466 int32_t* qsort_threads=ps->qsort_threads;
1467 int32_t sort_limit=ps->sort_limit;
1468 int32_t num_threads=ps->num_threads;
1477 if (output[0] > output [1])
1479 swap(output[0], output[1]);
1480 swap(index[0], index[1]);
1485 T1
split=output[size/2];
1488 int32_t right=size-1;
1492 while (output[left] < split)
1494 while (output[right] > split)
1499 swap(output[left], output[right]);
1500 swap(index[left], index[right]);
1505 bool lthread_start=
false;
1506 bool rthread_start=
false;
1509 struct thread_qsort<T1,T2> t1;
1510 struct thread_qsort<T1,T2> t2;
1512 if (right+1> 1 && (right+1< sort_limit || *qsort_threads >= num_threads-1))
1514 else if (right+1> 1)
1521 t1.qsort_threads=qsort_threads;
1522 t1.sort_limit=sort_limit;
1523 t1.num_threads=num_threads;
1524 if (pthread_create(<hread, NULL, parallel_qsort_index<T1,T2>, &t1) != 0)
1526 lthread_start=
false;
1533 if (size-left> 1 && (size-left< sort_limit || *qsort_threads >= num_threads-1))
1534 qsort_index(&output[left],&index[left], size-left);
1535 else if (size-left> 1)
1539 t2.output=&output[left];
1540 t2.index=&index[left];
1542 t2.qsort_threads=qsort_threads;
1543 t2.sort_limit=sort_limit;
1544 t2.num_threads=num_threads;
1545 if (pthread_create(&rthread, NULL, parallel_qsort_index<T1,T2>, &t2) != 0)
1547 rthread_start=
false;
1549 qsort_index(&output[left],&index[left], size-left);
1555 pthread_join(lthread, NULL);
1561 pthread_join(rthread, NULL);
1568 template <
class T1,
class T2>
1576 if (output[0] > output [1])
1578 swap(output[0],output[1]);
1579 swap(index[0],index[1]);
1584 T1
split=output[size/2];
1587 int32_t right=size-1;
1591 while (output[left] < split)
1593 while (output[right] > split)
1598 swap(output[left],output[right]);
1599 swap(index[left],index[right]);
1609 qsort_index(&output[left],&index[left], size-left);
1612 template <
class T1,
class T2>
1620 if (output[0] < output [1])
1622 swap(output[0],output[1]);
1623 swap(index[0],index[1]);
1629 T1
split=output[size/2];
1632 int32_t right=size-1;
1636 while (output[left] > split)
1638 while (output[right] < split)
1643 swap(output[left],output[right]);
1644 swap(index[left],index[right]);
1661 for (int32_t i=0; i<n; i++)
1662 min(&output[i], &index[i], size-i) ;
1674 int32_t min_index=0;
1675 for (int32_t i=1; i<size; i++)
1677 if (output[i]<min_elem)
1683 swap(output[0], output[min_index]);
1684 swap(index[0], index[min_index]);
1687 #define COMPLEX128_ERROR_ONEARG_T(function) \
1689 inline complex128_t CMath::function<complex128_t>(complex128_t a) \
1691 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1693 return complex128_t(0.0, 0.0); \
1696 #define COMPLEX128_ERROR_TWOARGS_T(function) \
1698 inline complex128_t CMath::function<complex128_t>(complex128_t a, complex128_t b) \
1700 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1702 return complex128_t(0.0, 0.0); \
1705 #define COMPLEX128_ERROR_THREEARGS_T(function) \
1707 inline complex128_t CMath::function<complex128_t>(complex128_t a, complex128_t b, complex128_t c) \
1709 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1711 return complex128_t(0.0, 0.0); \
1714 #define COMPLEX128_ERROR_SORT_T(function) \
1716 inline void CMath::function<complex128_t>(complex128_t* output, int32_t b) \
1718 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1744 #undef COMPLEX128_ERROR_ONEARG
1745 #undef COMPLEX128_ERROR_ONEARG_T
1746 #undef COMPLEX128_ERROR_TWOARGS_T
1747 #undef COMPLEX128_ERROR_THREEARGS_T
1748 #undef COMPLEX128_STDMATH
1749 #undef COMPLEX128_ERROR_SORT_T
static float64_t sin(float64_t x)
static float64_t normal_random(float64_t mean, float64_t std_dev)
static const float32_t F_MAX_VAL32
static const float64_t MACHINE_EPSILON
static bool strtof(const char *str, float32_t *float_result)
static int32_t binary_search(complex128_t *output, int32_t size, complex128_t elem)
binary_search not implemented for complex128_t
static void parallel_qsort_index(T1 *output, T2 *index, uint32_t size, int32_t n_threads, int32_t limit=262144)
static uint32_t seed
random generator seed
std::complex< float64_t > complex128_t
uint64_t random(uint64_t min_value, uint64_t max_value)
float64_t std_normal_distrib() const
static int is_finite(double f)
checks whether a float is finite
static float64_t Align(char *seq1, char *seq2, int32_t l1, int32_t l2, float64_t gapCost)
static float64_t sqrt(float64_t x)
x^0.5
static floatmax_t random(floatmax_t min_value, floatmax_t max_value)
static floatmax_t sqrt(floatmax_t x)
x^0.5
static void linspace(float64_t *output, float64_t start, float64_t end, int32_t n=100)
static float64_t ceil(float64_t d)
static bool strtod(const char *str, float64_t *double_result)
static complex128_t pow(complex128_t x, int32_t n)
x^n, x or n being a complex128_t
virtual ~CMath()
Destructor - frees logtable.
static const float64_t INFTY
infinity
static int32_t binary_search_helper(T *output, int32_t size, T elem)
#define COMPLEX128_ERROR_THREEARGS_T(function)
static void nmin(float64_t *output, T *index, int32_t size, int32_t n)
static void qsort_index(T1 *output, T2 *index, uint32_t size)
static void qsort_index(complex128_t *output, T *index, uint32_t size)
qsort_index not implemented for complex128_t
static float64_t log10(float64_t v)
tanh(x), x being a complex128_t
#define COMPLEX128_ERROR_TWOARGS_T(function)
static float32_t normal_random(float32_t mean, float32_t std_dev)
uint64_t random_64() const
static float64_t random(float64_t min_value, float64_t max_value)
static int32_t binary_search(T *output, int32_t size, T elem)
static uint32_t get_seed()
returns number generator seed
static float32_t randn_float()
static const float64_t MIN_REAL_NUMBER
static float64_t randn_double()
static int32_t binary_search_max_lower_equal(T *output, int32_t size, T elem)
static float64_t atan(float64_t x)
tan(x), x being a complex128_t
static const float64_t F_MAX_VAL64
static float64_t cosh(float64_t x)
acos(x), x being a complex128_t not implemented
static uint32_t get_log_range()
returns range of logtable
void split(v_array< ds_node< P > > &point_set, v_array< ds_node< P > > &far_set, int max_scale)
static float32_t random(float32_t min_value, float32_t max_value)
static const float32_t F_MIN_VAL32
static bool fequals_abs(const T &a, const T &b, const float64_t eps)
static float64_t get_abs_tolorance(float64_t true_value, float64_t rel_tolorance)
static float64_t imag(complex128_t c)
returns imag part of a complex128_t number
static float64_t floor(float64_t d)
static int32_t get_num_nonzero(complex128_t *vec, int32_t len)
static const float32_t F_MIN_NORM_VAL32
static float64_t real(complex128_t c)
returns real part of a complex128_t number
static uint8_t byte(complex128_t word, uint16_t p)
byte not implemented for complex128_t
static void qsort(T *output, int32_t size)
static int32_t LOGRANGE
range for logtable: log(1+exp(x)) -LOGRANGE <= x <= 0
static const float64_t ALMOST_NEG_INFTY
almost neg (log) infinity
static bool fequals(const T &a, const T &b, const float64_t eps, bool tolerant=false)
static complex128_t pow(complex128_t x, complex128_t n)
static float32_t invsqrt(float32_t x)
x^0.5, x being a complex128_t
static uint8_t byte(T word, uint16_t p)
float64_t normal_distrib(float64_t mu, float64_t sigma) const
static complex128_t pow(float64_t x, complex128_t n)
CMath()
Constructor - initializes log-table.
static float64_t pow(float64_t x, int32_t n)
Class SGObject is the base class of all shogun objects.
static int32_t random(int32_t min_value, int32_t max_value)
static float64_t pow(float64_t x, float64_t n)
static void qsort(T **vector, index_t length)
static void init_random(uint32_t initseed=0)
static int32_t pow(int32_t x, int32_t n)
#define radix_pop(a, n, i)
static void parallel_qsort_index(complex128_t *output, T *index, uint32_t size, int32_t n_threads, int32_t limit=0)
parallel_qsort_index not implemented for complex128_t
static void min(float64_t *output, complex128_t *index, int32_t size)
complex128_t cannot be used as index
#define COMPLEX128_ERROR_ONEARG(function)
static float64_t cos(float64_t x)
sinh(x), x being a complex128_t
static void qsort_backword_index(complex128_t *output, T *index, uint32_t size)
qsort_backword_index not implemented for complex128_t
static int32_t get_num_nonzero(T *vec, int32_t len)
static T log_sum_exp(SGVector< T > values)
static T max(T a, T b)
return the maximum of two integers
static float64_t area_under_curve(float64_t *xy, int32_t len, bool reversed)
cosh(x), x being a complex128_t
static void display_bits(T word, int32_t width=8 *sizeof(T))
display bits (useful for debugging)
static uint64_t random(uint64_t min_value, uint64_t max_value)
static int64_t factorial(int32_t n)
static void qsort(complex128_t **vector, index_t length)
qsort not implemented for complex128_t
static float64_t atan2(float64_t x, float64_t y)
atan(x), x being a complex128_t not implemented
static float64_t tan(float64_t x)
exp(x), x being a complex128_t
static int32_t binary_search_max_lower_equal(complex128_t *output, int32_t size, complex128_t elem)
binary_search_max_lower_equal not implemented for complex128_t
static float64_t sinh(float64_t x)
asin(x), x being a complex128_t not implemented
static int64_t nchoosek(int32_t n, int32_t k)
static int32_t binary_search(T **vector, index_t length, T *elem)
static int is_infinity(double f)
checks whether a float is infinity
void set_seed(uint32_t seed)
static float64_t acos(float64_t x)
cos(x), x being a complex128_t
static float64_t abs(complex128_t a)
return the absolute value of a complex number
static float64_t log2(float64_t v)
log10(x), x being a complex128_t
static void display_bits(complex128_t word, int32_t width=8 *sizeof(complex128_t))
disply_bits not implemented for complex128_t
#define COMPLEX128_STDMATH(function)
static void radix_sort_helper(T *array, int32_t size, uint16_t i)
static T sign(T a)
signum of type T variable a
static float64_t tanh(float64_t x)
atan2(x), x being a complex128_t not implemented
static int is_nan(double f)
checks whether a float is nan
virtual const char * get_name() const
static float64_t asin(float64_t x)
sin(x), x being a complex128_t
static T min(T a, T b)
return the minimum of two integers
static float64_t exp(float64_t x)
static const float64_t F_MIN_VAL64
static const float64_t F_MIN_NORM_VAL64
static float64_t log(float64_t v)
static void radix_sort_helper(complex128_t *array, int32_t size, uint16_t i)
radix_sort_helper not implemented for complex128_t
static const float64_t ALMOST_INFTY
Class which collects generic mathematical functions.
static T log_mean_exp(SGVector< T > values)
static void insertion_sort(T *output, int32_t size)
static void swap(T &a, T &b)
swap e.g. floats a and b
static complex128_t pow(complex128_t x, float64_t n)
static void sort(int32_t *a, int32_t cols, int32_t sort_col=0)
static int32_t binary_search_helper(complex128_t *output, int32_t size, complex128_t elem)
binary_search_helper not implemented for complex128_t
static float64_t round(float64_t d)
static float32_t sqrt(float32_t x)
x^0.5
static uint32_t generate_seed()
static index_t floor_log(index_t n)
log(x), x being a complex128_t
static void radix_sort(T *array, int32_t size)
static uint32_t random(uint32_t min_value, uint32_t max_value)
static floatmax_t powl(floatmax_t x, floatmax_t n)
x^n
static float64_t logarithmic_sum(float64_t p, float64_t q)
#define radix_push(a, n, i)
static T clamp(T value, T lb, T ub)
return the value clamped to interval [lb,ub]
static int32_t binary_search(complex128_t **vector, index_t length, complex128_t *elem)
binary_search not implemented for complex128_t
#define COMPLEX128_ERROR_SORT_T(function)
static bool strtold(const char *str, floatmax_t *long_double_result)
static int32_t pow(bool x, int32_t n)
static const float64_t NOT_A_NUMBER
not a number
static void qsort_backward_index(T1 *output, T2 *index, int32_t size)
static const float64_t MAX_REAL_NUMBER
static T abs(T a)
return the absolute value of a number
static void nmin(float64_t *output, complex128_t *index, int32_t size, int32_t n)
complex128_t cannot be used as index
static int64_t random(int64_t min_value, int64_t max_value)
static const float64_t PI