20 #ifndef __MATHEMATICS_H_
21 #define __MATHEMATICS_H_
29 #ifndef _USE_MATH_DEFINES
30 #define _USE_MATH_DEFINES
36 #include <sys/types.h>
51 #define cygwin_log2 log2
56 #define M_PI 3.14159265358979323846
65 #define isfinite _isfinite
70 #define RNG_SEED_SIZE 256
73 #define RADIX_STACK_SIZE 512
76 #define radix_push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
77 #define radix_pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
79 #ifndef DOXYGEN_SHOULD_SKIP_THIS
81 template <
class T>
struct radix_stack_t
94 template <
class T1,
class T2>
struct thread_qsort
104 int32_t* qsort_threads;
110 #endif // DOXYGEN_SHOULD_SKIP_THIS
112 #define COMPLEX128_ERROR_ONEARG(function) \
113 static inline complex128_t function(complex128_t a) \
115 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
117 return complex128_t(0.0, 0.0); \
120 #define COMPLEX128_STDMATH(function) \
121 static inline complex128_t function(complex128_t a) \
123 return std::function(a); \
153 static inline T
min(T a, T b)
155 return (a<=b) ? a : b;
160 static inline T
max(T a, T b)
162 return (a>=b) ? a : b;
167 static inline T
clamp(T value, T lb, T ub)
213 const T diff = CMath::abs<T>((a-b));
227 static inline bool fequals(
const T& a,
const T& b,
228 const float64_t eps,
bool tolerant=
false)
230 const T absA = CMath::abs<T>(a);
231 const T absB = CMath::abs<T>(b);
232 const T diff = CMath::abs<T>((a-b));
241 return CMath::fequals_abs<T>(a, b, eps);
254 else if ( (a==0) || (b==0) || (diff < comp) )
255 return (diff<(eps * comp));
260 T check = ((diff/(absA + absB)) > diff)?
261 (diff/(absA + absB)):diff;
262 return (check < eps);
268 return ::floor(d+0.5);
287 else return (a<0) ? (-1) : (+1);
292 static inline void swap(T &a,T &b)
302 static inline T
sq(T x)
349 tmp.i = 0x5f3759d5 - (tmp.i >> 1);
351 x = x*(1.5f - xhalf*x*x);
361 return ::powl((
long double) x, (
long double) n);
363 return ::pow((
double) x, (
double) n);
367 static inline int32_t
pow(
bool x, int32_t n)
372 static inline int32_t
pow(int32_t x, int32_t n)
393 return ::pow((
double)x, (
double)n);
398 return ::pow((
double) x, (
double) n);
404 return std::pow(x, n);
409 return std::pow(x, n);
414 return std::pow(x, n);
419 return std::pow(x, n);
424 return ::exp((
double) x);
433 return ::tan((
double) x);
442 return ::atan((
double) x);
451 return ::atan2((
double) x, (
double) y);
460 return ::tanh((
double) x);
468 return ::log(v)/
::log(10.0);
479 return ::log(v)/
::log(2.0);
494 for (i = 0; n != 0; i++)
556 for (
int i=1; i<len; i++)
557 area += 0.5*(xy[2*i]-xy[2*(i-1)])*(xy[2*i+1]+xy[2*(i-1)+1]);
561 for (
int i=1; i<len; i++)
562 area += 0.5*(xy[2*i+1]-xy[2*(i-1)+1])*(xy[2*i]+xy[2*(i-1)]);
575 for (
int i=2; i<=n; i++)
595 static inline uint64_t
random(uint64_t min_value, uint64_t max_value)
600 static inline int64_t
random(int64_t min_value, int64_t max_value)
605 static inline uint32_t
random(uint32_t min_value, uint32_t max_value)
610 static inline int32_t
random(int32_t min_value, int32_t max_value)
644 rand_s = rand_u*rand_u + rand_v*rand_v;
645 }
while ((rand_s == 0) || (rand_s >= 1));
648 ret = rand_u*
sqrt(-2.0*
log(rand_s)/rand_s);
649 ret = std_dev*ret + mean;
693 static inline int64_t
nchoosek(int32_t n, int32_t k)
697 for (int32_t i=n-k+1; i<=n; i++)
742 for (from_idx=0; from_idx<values.
vlen; ++from_idx)
744 if (from_idx!=min_index)
746 values_without_X0[to_idx]=
exp(values[from_idx]-X0);
769 static void sort(int32_t *a, int32_t cols, int32_t sort_col=0);
775 static void qsort(T* output, int32_t size)
782 if (output[0] > output [1])
787 T
split=output[size/2];
790 int32_t right=size-1;
794 while (output[left] < split)
796 while (output[right] > split)
808 qsort(output,right+1);
811 qsort(&output[left],size-left);
819 for (int32_t i=0; i<size-1; i++)
823 while (j >= 0 && output[j] > value)
825 output[j+1] = output[j];
845 static inline uint8_t
byte(T word, uint16_t p)
847 return (word >> (
sizeof(T)-p-1) * 8) & 0xff;
853 SG_SERROR(
"CMath::byte():: Not supported for complex128_t\n");
860 static size_t count[256], nc, cmin;
864 T *an, *aj, *pile[256];
879 for (ak = array; ak < an; ak++) {
907 for (cp = count + cmin; nc > 0; cp++) {
922 pile[cp - count] = ak += *cp;
946 for (r = *aj; aj < (ak = --pile[c =
byte(r, i)]);)
959 SG_SERROR(
"CMath::radix_sort_helper():: Not supported for complex128_t\n");
979 if (*vector[0]>*vector[1])
980 swap(vector[0],vector[1]);
983 T*
split=vector[length/2];
986 int32_t right=length-1;
990 while (*vector[left]<*split)
992 while (*vector[right]>*split)
997 swap(vector[left],vector[right]);
1004 qsort(vector,right+1);
1007 qsort(&vector[left],length-left);
1013 SG_SERROR(
"CMath::qsort():: Not supported for complex128_t\n");
1017 template <
class T>
static void display_bits(T word, int32_t width=8*
sizeof(T))
1020 for (
int i=0; i<width; i++)
1022 T mask = ((T) 1)<<(
sizeof(T)*8-1);
1039 SG_SERROR(
"CMath::display_bits():: Not supported for complex128_t\n");
1047 template <
class T1,
class T2>
1048 static void qsort_index(T1* output, T2* index, uint32_t size);
1054 SG_SERROR(
"CMath::qsort_index():: Not supported for complex128_t\n");
1062 template <
class T1,
class T2>
1064 T1* output, T2* index, int32_t size);
1071 SG_SERROR(
"CMath::qsort_backword_index():: \
1072 Not supported for complex128_t\n");
1082 template <
class T1,
class T2>
1083 inline static void parallel_qsort_index(T1* output, T2* index, uint32_t size, int32_t n_threads, int32_t limit=262144)
1086 thread_qsort<T1,T2> t;
1092 t.num_threads=n_threads;
1093 parallel_qsort_index<T1,T2>(&t);
1099 uint32_t size, int32_t n_threads, int32_t limit=0)
1101 SG_SERROR(
"CMath::parallel_qsort_index():: Not supported for complex128_t\n");
1105 template <
class T1,
class T2>
1112 static void min(
float64_t* output, T* index, int32_t size);
1117 SG_SERROR(
"CMath::min():: Not supported for complex128_t\n");
1124 float64_t* output, T* index, int32_t size, int32_t n);
1128 int32_t size, int32_t n)
1130 SG_SERROR(
"CMath::nmin():: Not supported for complex128_t\n");
1148 int32_t middle=(start+end)/2;
1150 if (output[middle]>elem)
1152 else if (output[middle]<elem)
1164 SG_SERROR(
"CMath::binary_search_helper():: Not supported for complex128_t\n");
1174 if (ind >= 0 && output[ind] == elem)
1182 SG_SERROR(
"CMath::binary_search():: Not supported for complex128_t\n");
1199 int32_t end=length-1;
1206 int32_t middle=(start+end)/2;
1208 if (*vector[middle]>*elem)
1210 else if (*vector[middle]<*elem)
1219 if (start>=0&&*vector[start]==*elem)
1228 SG_SERROR(
"CMath::binary_search():: Not supported for complex128_t\n");
1234 T* output, int32_t size, T elem)
1238 if (output[ind]<=elem)
1240 if (ind>0 && output[ind-1] <= elem)
1249 SG_SERROR(
"CMath::binary_search_max_lower_equal():: \
1250 Not supported for complex128_t\n");
1257 char * seq1,
char* seq2, int32_t l1, int32_t l2,
float64_t gapCost);
1287 inline static uint32_t get_log_accuracy()
1290 return CMath::LOGACCURACY;
1301 static int is_nan(
double f);
1323 SG_SWARNING(
"INVALID second operand to logsum(%f,%f) expect undefined results\n", p, q)
1328 return diff >
LOGRANGE? p : p + logtable[(
int)(diff * LOGACCURACY)];
1329 return -diff >
LOGRANGE? q : q + logtable[(
int)(-diff * LOGACCURACY)];
1333 static
void init_log_table();
1336 static int32_t determine_logrange();
1339 static int32_t determine_logaccuracy(int32_t range);
1356 #ifdef USE_LOGSUMARRAY
1362 static inline float64_t logarithmic_sum_array(
1376 if (len%2==1) pp++ ;
1377 for (
register int32_t j=0; j < len>>1; j++)
1380 return logarithmic_sum_array(p,len%2 + (len>>1)) ;
1382 #endif //USE_LOGSUMARRAY
1386 virtual const char*
get_name()
const {
return "Math"; }
1429 static int32_t LOGACCURACY;
1437 template <
class T1,
class T2>
1440 struct thread_qsort<T1,T2>* ps=(thread_qsort<T1,T2>*) p;
1441 T1* output=ps->output;
1442 T2* index=ps->index;
1443 int32_t size=ps->size;
1444 int32_t* qsort_threads=ps->qsort_threads;
1445 int32_t sort_limit=ps->sort_limit;
1446 int32_t num_threads=ps->num_threads;
1455 if (output[0] > output [1])
1457 swap(output[0], output[1]);
1458 swap(index[0], index[1]);
1463 T1
split=output[size/2];
1466 int32_t right=size-1;
1470 while (output[left] < split)
1472 while (output[right] > split)
1477 swap(output[left], output[right]);
1478 swap(index[left], index[right]);
1483 bool lthread_start=
false;
1484 bool rthread_start=
false;
1487 struct thread_qsort<T1,T2> t1;
1488 struct thread_qsort<T1,T2> t2;
1490 if (right+1> 1 && (right+1< sort_limit || *qsort_threads >= num_threads-1))
1492 else if (right+1> 1)
1499 t1.qsort_threads=qsort_threads;
1500 t1.sort_limit=sort_limit;
1501 t1.num_threads=num_threads;
1502 if (pthread_create(<hread, NULL, parallel_qsort_index<T1,T2>, &t1) != 0)
1504 lthread_start=
false;
1511 if (size-left> 1 && (size-left< sort_limit || *qsort_threads >= num_threads-1))
1512 qsort_index(&output[left],&index[left], size-left);
1513 else if (size-left> 1)
1517 t2.output=&output[left];
1518 t2.index=&index[left];
1520 t2.qsort_threads=qsort_threads;
1521 t2.sort_limit=sort_limit;
1522 t2.num_threads=num_threads;
1523 if (pthread_create(&rthread, NULL, parallel_qsort_index<T1,T2>, &t2) != 0)
1525 rthread_start=
false;
1527 qsort_index(&output[left],&index[left], size-left);
1533 pthread_join(lthread, NULL);
1539 pthread_join(rthread, NULL);
1546 template <
class T1,
class T2>
1554 if (output[0] > output [1])
1556 swap(output[0],output[1]);
1557 swap(index[0],index[1]);
1562 T1
split=output[size/2];
1565 int32_t right=size-1;
1569 while (output[left] < split)
1571 while (output[right] > split)
1576 swap(output[left],output[right]);
1577 swap(index[left],index[right]);
1587 qsort_index(&output[left],&index[left], size-left);
1590 template <
class T1,
class T2>
1598 if (output[0] < output [1])
1600 swap(output[0],output[1]);
1601 swap(index[0],index[1]);
1607 T1
split=output[size/2];
1610 int32_t right=size-1;
1614 while (output[left] > split)
1616 while (output[right] < split)
1621 swap(output[left],output[right]);
1622 swap(index[left],index[right]);
1639 for (int32_t i=0; i<n; i++)
1640 min(&output[i], &index[i], size-i) ;
1652 int32_t min_index=0;
1653 for (int32_t i=1; i<size; i++)
1655 if (output[i]<min_elem)
1661 swap(output[0], output[min_index]);
1662 swap(index[0], index[min_index]);
1665 #define COMPLEX128_ERROR_ONEARG_T(function) \
1667 inline complex128_t CMath::function<complex128_t>(complex128_t a) \
1669 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1671 return complex128_t(0.0, 0.0); \
1674 #define COMPLEX128_ERROR_TWOARGS_T(function) \
1676 inline complex128_t CMath::function<complex128_t>(complex128_t a, complex128_t b) \
1678 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1680 return complex128_t(0.0, 0.0); \
1683 #define COMPLEX128_ERROR_THREEARGS_T(function) \
1685 inline complex128_t CMath::function<complex128_t>(complex128_t a, complex128_t b, complex128_t c) \
1687 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1689 return complex128_t(0.0, 0.0); \
1692 #define COMPLEX128_ERROR_SORT_T(function) \
1694 inline void CMath::function<complex128_t>(complex128_t* output, int32_t b) \
1696 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1722 #undef COMPLEX128_ERROR_ONEARG
1723 #undef COMPLEX128_ERROR_ONEARG_T
1724 #undef COMPLEX128_ERROR_TWOARGS_T
1725 #undef COMPLEX128_ERROR_THREEARGS_T
1726 #undef COMPLEX128_STDMATH
1727 #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 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