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
71 #define NAN (strtod("NAN",NULL))
75 #define RNG_SEED_SIZE 256
78 #define RADIX_STACK_SIZE 512
81 #define radix_push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
82 #define radix_pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
84 #ifndef DOXYGEN_SHOULD_SKIP_THIS
86 template <
class T>
struct radix_stack_t
99 template <
class T1,
class T2>
struct thread_qsort
109 int32_t* qsort_threads;
115 #endif // DOXYGEN_SHOULD_SKIP_THIS
117 #define COMPLEX128_ERROR_ONEARG(function) \
118 static inline complex128_t function(complex128_t a) \
120 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
122 return complex128_t(0.0, 0.0); \
125 #define COMPLEX128_STDMATH(function) \
126 static inline complex128_t function(complex128_t a) \
128 return std::function(a); \
158 static inline T
min(T a, T b)
160 return (a<=b) ? a : b;
165 static inline T
max(T a, T b)
167 return (a>=b) ? a : b;
172 static inline T
clamp(T value, T lb, T ub)
210 return ::floor(d+0.5);
229 else return (a<0) ? (-1) : (+1);
234 static inline void swap(T &a,T &b)
244 static inline T
sq(T x)
291 tmp.i = 0x5f3759d5 - (tmp.i >> 1);
293 x = x*(1.5f - xhalf*x*x);
303 return ::powl((
long double) x, (
long double) n);
305 return ::pow((
double) x, (
double) n);
309 static inline int32_t
pow(
bool x, int32_t n)
314 static inline int32_t
pow(int32_t x, int32_t n)
335 return ::pow((
double)x, (
double)n);
340 return ::pow((
double) x, (
double) n);
346 return std::pow(x, n);
351 return std::pow(x, n);
356 return std::pow(x, n);
361 return std::pow(x, n);
366 return ::exp((
double) x);
375 return ::tan((
double) x);
384 return ::atan((
double) x);
393 return ::atan2((
double) x, (
double) y);
402 return ::tanh((
double) x);
410 return ::log(v)/
::log(10.0);
421 return ::log(v)/
::log(2.0);
436 for (i = 0; n != 0; i++)
498 for (
int i=1; i<len; i++)
499 area += 0.5*(xy[2*i]-xy[2*(i-1)])*(xy[2*i+1]+xy[2*(i-1)+1]);
503 for (
int i=1; i<len; i++)
504 area += 0.5*(xy[2*i+1]-xy[2*(i-1)+1])*(xy[2*i]+xy[2*(i-1)]);
513 for (
int i=2; i<=n; i++)
533 static inline uint64_t
random(uint64_t min_value, uint64_t max_value)
538 static inline int64_t
random(int64_t min_value, int64_t max_value)
543 static inline uint32_t
random(uint32_t min_value, uint32_t max_value)
548 static inline int32_t
random(int32_t min_value, int32_t max_value)
582 rand_s = rand_u*rand_u + rand_v*rand_v;
583 }
while ((rand_s == 0) || (rand_s >= 1));
586 ret = rand_u*
sqrt(-2.0*
log(rand_s)/rand_s);
587 ret = std_dev*ret + mean;
631 static inline int64_t
nchoosek(int32_t n, int32_t k)
635 for (int32_t i=n-k+1; i<=n; i++)
680 for (from_idx=0; from_idx<values.
vlen; ++from_idx)
682 if (from_idx!=min_index)
684 values_without_X0[to_idx]=
exp(values[from_idx]-X0);
707 static void sort(int32_t *a, int32_t cols, int32_t sort_col=0);
713 static void qsort(T* output, int32_t size)
720 if (output[0] > output [1])
725 T
split=output[size/2];
728 int32_t right=size-1;
732 while (output[left] < split)
734 while (output[right] > split)
746 qsort(output,right+1);
749 qsort(&output[left],size-left);
757 for (int32_t i=0; i<size-1; i++)
761 while (j >= 0 && output[j] > value)
763 output[j+1] = output[j];
783 static inline uint8_t
byte(T word, uint16_t p)
785 return (word >> (
sizeof(T)-p-1) * 8) & 0xff;
791 SG_SERROR(
"CMath::byte():: Not supported for complex128_t\n");
798 static size_t count[256], nc, cmin;
802 T *an, *aj, *pile[256];
817 for (ak = array; ak < an; ak++) {
845 for (cp = count + cmin; nc > 0; cp++) {
860 pile[cp - count] = ak += *cp;
884 for (r = *aj; aj < (ak = --pile[c =
byte(r, i)]);)
897 SG_SERROR(
"CMath::radix_sort_helper():: Not supported for complex128_t\n");
917 if (*vector[0]>*vector[1])
918 swap(vector[0],vector[1]);
921 T*
split=vector[length/2];
924 int32_t right=length-1;
928 while (*vector[left]<*split)
930 while (*vector[right]>*split)
935 swap(vector[left],vector[right]);
942 qsort(vector,right+1);
945 qsort(&vector[left],length-left);
951 SG_SERROR(
"CMath::qsort():: Not supported for complex128_t\n");
955 template <
class T>
static void display_bits(T word, int32_t width=8*
sizeof(T))
958 for (
int i=0; i<width; i++)
960 T mask = ((T) 1)<<(
sizeof(T)*8-1);
977 SG_SERROR(
"CMath::display_bits():: Not supported for complex128_t\n");
985 template <
class T1,
class T2>
986 static void qsort_index(T1* output, T2* index, uint32_t size);
992 SG_SERROR(
"CMath::qsort_index():: Not supported for complex128_t\n");
1000 template <
class T1,
class T2>
1002 T1* output, T2* index, int32_t size);
1009 SG_SERROR(
"CMath::qsort_backword_index():: \
1010 Not supported for complex128_t\n");
1020 template <
class T1,
class T2>
1021 inline static void parallel_qsort_index(T1* output, T2* index, uint32_t size, int32_t n_threads, int32_t limit=262144)
1024 thread_qsort<T1,T2> t;
1030 t.num_threads=n_threads;
1031 parallel_qsort_index<T1,T2>(&t);
1037 uint32_t size, int32_t n_threads, int32_t limit=0)
1039 SG_SERROR(
"CMath::parallel_qsort_index():: Not supported for complex128_t\n");
1043 template <
class T1,
class T2>
1050 static void min(
float64_t* output, T* index, int32_t size);
1055 SG_SERROR(
"CMath::min():: Not supported for complex128_t\n");
1062 float64_t* output, T* index, int32_t size, int32_t n);
1066 int32_t size, int32_t n)
1068 SG_SERROR(
"CMath::nmin():: Not supported for complex128_t\n");
1086 int32_t middle=(start+end)/2;
1088 if (output[middle]>elem)
1090 else if (output[middle]<elem)
1102 SG_SERROR(
"CMath::binary_search_helper():: Not supported for complex128_t\n");
1112 if (ind >= 0 && output[ind] == elem)
1120 SG_SERROR(
"CMath::binary_search():: Not supported for complex128_t\n");
1137 int32_t end=length-1;
1144 int32_t middle=(start+end)/2;
1146 if (*vector[middle]>*elem)
1148 else if (*vector[middle]<*elem)
1157 if (start>=0&&*vector[start]==*elem)
1166 SG_SERROR(
"CMath::binary_search():: Not supported for complex128_t\n");
1172 T* output, int32_t size, T elem)
1176 if (output[ind]<=elem)
1178 if (ind>0 && output[ind-1] <= elem)
1187 SG_SERROR(
"CMath::binary_search_max_lower_equal():: \
1188 Not supported for complex128_t\n");
1195 char * seq1,
char* seq2, int32_t l1, int32_t l2,
float64_t gapCost);
1225 inline static uint32_t get_log_accuracy()
1228 return CMath::LOGACCURACY;
1239 static int is_nan(
double f);
1261 SG_SWARNING(
"INVALID second operand to logsum(%f,%f) expect undefined results\n", p, q)
1266 return diff >
LOGRANGE? p : p + logtable[(
int)(diff * LOGACCURACY)];
1267 return -diff >
LOGRANGE? q : q + logtable[(
int)(-diff * LOGACCURACY)];
1271 static
void init_log_table();
1274 static int32_t determine_logrange();
1277 static int32_t determine_logaccuracy(int32_t range);
1294 #ifdef USE_LOGSUMARRAY
1300 static inline float64_t logarithmic_sum_array(
1314 if (len%2==1) pp++ ;
1315 for (
register int32_t j=0; j < len>>1; j++)
1318 return logarithmic_sum_array(p,len%2 + (len>>1)) ;
1320 #endif //USE_LOGSUMARRAY
1324 virtual const char*
get_name()
const {
return "Math"; }
1355 static int32_t LOGACCURACY;
1363 template <
class T1,
class T2>
1366 struct thread_qsort<T1,T2>* ps=(thread_qsort<T1,T2>*) p;
1367 T1* output=ps->output;
1368 T2* index=ps->index;
1369 int32_t size=ps->size;
1370 int32_t* qsort_threads=ps->qsort_threads;
1371 int32_t sort_limit=ps->sort_limit;
1372 int32_t num_threads=ps->num_threads;
1381 if (output[0] > output [1])
1383 swap(output[0], output[1]);
1384 swap(index[0], index[1]);
1389 T1
split=output[size/2];
1392 int32_t right=size-1;
1396 while (output[left] < split)
1398 while (output[right] > split)
1403 swap(output[left], output[right]);
1404 swap(index[left], index[right]);
1409 bool lthread_start=
false;
1410 bool rthread_start=
false;
1413 struct thread_qsort<T1,T2> t1;
1414 struct thread_qsort<T1,T2> t2;
1416 if (right+1> 1 && (right+1< sort_limit || *qsort_threads >= num_threads-1))
1418 else if (right+1> 1)
1425 t1.qsort_threads=qsort_threads;
1426 t1.sort_limit=sort_limit;
1427 t1.num_threads=num_threads;
1428 if (pthread_create(<hread, NULL, parallel_qsort_index<T1,T2>, &t1) != 0)
1430 lthread_start=
false;
1437 if (size-left> 1 && (size-left< sort_limit || *qsort_threads >= num_threads-1))
1438 qsort_index(&output[left],&index[left], size-left);
1439 else if (size-left> 1)
1443 t2.output=&output[left];
1444 t2.index=&index[left];
1446 t2.qsort_threads=qsort_threads;
1447 t2.sort_limit=sort_limit;
1448 t2.num_threads=num_threads;
1449 if (pthread_create(&rthread, NULL, parallel_qsort_index<T1,T2>, &t2) != 0)
1451 rthread_start=
false;
1453 qsort_index(&output[left],&index[left], size-left);
1459 pthread_join(lthread, NULL);
1465 pthread_join(rthread, NULL);
1472 template <
class T1,
class T2>
1480 if (output[0] > output [1])
1482 swap(output[0],output[1]);
1483 swap(index[0],index[1]);
1488 T1
split=output[size/2];
1491 int32_t right=size-1;
1495 while (output[left] < split)
1497 while (output[right] > split)
1502 swap(output[left],output[right]);
1503 swap(index[left],index[right]);
1513 qsort_index(&output[left],&index[left], size-left);
1516 template <
class T1,
class T2>
1524 if (output[0] < output [1])
1526 swap(output[0],output[1]);
1527 swap(index[0],index[1]);
1533 T1
split=output[size/2];
1536 int32_t right=size-1;
1540 while (output[left] > split)
1542 while (output[right] < split)
1547 swap(output[left],output[right]);
1548 swap(index[left],index[right]);
1565 for (int32_t i=0; i<n; i++)
1566 min(&output[i], &index[i], size-i) ;
1578 int32_t min_index=0;
1579 for (int32_t i=1; i<size; i++)
1581 if (output[i]<min_elem)
1587 swap(output[0], output[min_index]);
1588 swap(index[0], index[min_index]);
1591 #define COMPLEX128_ERROR_ONEARG_T(function) \
1593 inline complex128_t CMath::function<complex128_t>(complex128_t a) \
1595 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1597 return complex128_t(0.0, 0.0); \
1600 #define COMPLEX128_ERROR_TWOARGS_T(function) \
1602 inline complex128_t CMath::function<complex128_t>(complex128_t a, complex128_t b) \
1604 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1606 return complex128_t(0.0, 0.0); \
1609 #define COMPLEX128_ERROR_THREEARGS_T(function) \
1611 inline complex128_t CMath::function<complex128_t>(complex128_t a, complex128_t b, complex128_t c) \
1613 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1615 return complex128_t(0.0, 0.0); \
1618 #define COMPLEX128_ERROR_SORT_T(function) \
1620 inline void CMath::function<complex128_t>(complex128_t* output, int32_t b) \
1622 SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
1648 #undef COMPLEX128_ERROR_ONEARG
1649 #undef COMPLEX128_ERROR_ONEARG_T
1650 #undef COMPLEX128_ERROR_TWOARGS_T
1651 #undef COMPLEX128_ERROR_THREEARGS_T
1652 #undef COMPLEX128_STDMATH
1653 #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 float64_t MACHINE_EPSILON
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 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 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 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 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 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 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 int32_t pow(bool x, int32_t n)
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