-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Math.h
2294 lines (1982 loc) · 54 KB
/
Math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Thoralf Klein
* Written (W) 2013 Soumyajit De
* Written (W) 2012 Fernando Jose Iglesias Garcia
* Written (W) 2011 Siddharth Kherada
* Written (W) 2011 Justin Patera
* Written (W) 2011 Alesis Novik
* Written (W) 2011-2013 Heiko Strathmann
* Written (W) 1999-2009 Soeren Sonnenburg
* Written (W) 1999-2008 Gunnar Raetsch
* Written (W) 2007 Konrad Rieck
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef __MATHEMATICS_H_
#define __MATHEMATICS_H_
#include <algorithm>
#include <shogun/base/Parallel.h>
#include <shogun/base/init.h>
#include <shogun/lib/SGVector.h>
#include <shogun/lib/common.h>
#include <shogun/lib/config.h>
#include <shogun/mathematics/Random.h>
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#include <cfloat>
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#ifdef SUNOS
#include <ieeefp.h>
#endif
/// workaround for log2 being a define on cygwin
#ifdef log2
#define cygwin_log2 log2
#undef log2
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifdef _WIN32
#ifndef isnan
#define isnan _isnan
#endif
#ifndef isfinite
#define isfinite _isfinite
#endif
#endif //_WIN32
/* Size of RNG seed */
#define RNG_SEED_SIZE 256
/* Maximum stack size */
#define RADIX_STACK_SIZE 512
/* Stack macros */
#define radix_push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
#define radix_pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
#ifndef DOXYGEN_SHOULD_SKIP_THIS
/** Stack structure */
template <class T> struct radix_stack_t
{
/** Pointer to pile */
T *sa;
/** Number of grams in pile */
size_t sn;
/** Byte in current focus */
uint16_t si;
};
/** thread qsort */
template <class T1, class T2> struct thread_qsort
{
/** output */
T1* output;
/** index */
T2* index;
/** size */
uint32_t size;
/** qsort threads */
int32_t* qsort_threads;
/** sort limit */
int32_t sort_limit;
/** number of threads */
int32_t num_threads;
};
#endif // DOXYGEN_SHOULD_SKIP_THIS
#define COMPLEX128_ERROR_ONEARG(function) \
static inline complex128_t function(complex128_t a) \
{ \
SG_SERROR("CMath::%s():: Not supported for complex128_t\n",\
#function);\
return complex128_t(0.0, 0.0); \
}
#define COMPLEX128_STDMATH(function) \
static inline complex128_t function(complex128_t a) \
{ \
return std::function(a); \
}
namespace shogun
{
/** @brief Class which collects generic mathematical functions
*/
class CMath : public CSGObject
{
public:
/**@name Constructor/Destructor.
*/
//@{
///Constructor - initializes log-table
CMath();
///Destructor - frees logtable
virtual ~CMath();
//@}
/**@name min/max/abs functions.
*/
//@{
/** Returns the smallest element amongst two input values
* @param a first value
* @param b second value
* @return minimum value amongst a and b
*/
template <class T>
static inline T min(T a, T b)
{
return (a<=b) ? a : b;
}
/** Returns the greatest element amongst two input values
* @param a first value
* @param b second value
* @return maximum value amongst a and b
*/
template <class T>
static inline T max(T a, T b)
{
return (a>=b) ? a : b;
}
/** Returns the absolute value of a number, that is
* if a>0, output is a; if a<0 ,output is -a
* @param a complex number
* @return the corresponding absolute value
*/
template <class T>
static inline T abs(T a)
{
// can't be a>=0?(a):(-a), because compiler complains about
// 'comparison always true' when T is unsigned
if (a==0)
return 0;
else if (a>0)
return a;
else
return -a;
}
/** Returns the absolute value of a complex number
* @param a complex number
* @return the corresponding absolute value
*/
static inline float64_t abs(complex128_t a)
{
float64_t a_real=a.real();
float64_t a_imag=a.imag();
return (CMath::sqrt(a_real*a_real+a_imag*a_imag));
}
//@}
/** Returns the smallest element in the vector
* @param vec input vector
* @param len length of vector
* @return minimum value in the vector
*/
template <class T>
static T min(T* vec, int32_t len)
{
ASSERT(len>0)
T minv=vec[0];
for (int32_t i=1; i<len; i++)
minv=min(vec[i], minv);
return minv;
}
/** Returns the greatest element in the vector
* @param vec input vector
* @param len length of vector
* @return maximum value in the vector
*/
template <class T>
static T max(T* vec, int32_t len)
{
ASSERT(len>0)
T maxv=vec[0];
for (int32_t i=1; i<len; i++)
maxv=max(vec[i], maxv);
return maxv;
}
/** Returns the value clamped to interval [lb,ub]
* @param value input value
* @param lb lower bound
* @param ub upper bound
* @return the corresponding clamped value
*/
template <class T>
static inline T clamp(T value, T lb, T ub)
{
if (value<=lb)
return lb;
else if (value>=ub)
return ub;
else
return value;
}
/** Returns the index of the maximum value
* @param vec input vector
* @param inc increment factor
* @param len length of the input vector
* @param maxv_ptr pointer to store the maximum value
* @return index of the maximum value
*/
template <class T>
static int32_t arg_max(T * vec, int32_t inc, int32_t len, T * maxv_ptr = NULL)
{
ASSERT(len > 0 || inc > 0)
T maxv = vec[0];
int32_t maxIdx = 0;
for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
{
if (vec[j] > maxv)
maxv = vec[j], maxIdx = i;
}
if (maxv_ptr != NULL)
*maxv_ptr = maxv;
return maxIdx;
}
/** Returns the index of the minimum value
* @param vec input vector
* @param inc increment factor
* @param len length of the input vector
* @param minv_ptr pointer to store the minimum value
* @return index of the minimum value
*/
template <class T>
static int32_t arg_min(T * vec, int32_t inc, int32_t len, T * minv_ptr = NULL)
{
ASSERT(len > 0 || inc > 0)
T minv = vec[0];
int32_t minIdx = 0;
for (int32_t i = 1, j = inc ; i < len ; i++, j += inc)
{
if (vec[j] < minv)
minv = vec[j], minIdx = i;
}
if (minv_ptr != NULL)
*minv_ptr = minv;
return minIdx;
}
/**@name misc functions */
//@{
/** Compares the value of two floats based on eps only
* @param a first value to compare
* @param b second value to compare
* @param eps threshold for values to be equal/different
* @return true if values are equal within eps accuracy, false if not.
*/
template <class T>
static inline bool fequals_abs(const T& a, const T& b,
const float64_t eps)
{
const T diff = CMath::abs<T>((a-b));
return (diff < eps);
}
/** Compares the value of two floats (handles special cases, such as NaN, Inf etc.)
* Note: returns true if a == b == NAN
* Implementation inspired by http://floating-point-gui.de/errors/comparison/
* @param a first value to compare
* @param b second value to compare
* @param eps threshold for values to be equal/different
* @param tolerant allows linient check on float equality (within accuracy)
* @return true if values are equal within eps accuracy, false if not.
*/
template <class T>
static inline bool fequals(const T& a, const T& b,
const float64_t eps, bool tolerant=false)
{
const T absA = CMath::abs<T>(a);
const T absB = CMath::abs<T>(b);
const T diff = CMath::abs<T>((a-b));
T comp;
// Handle this separately since NAN is unordered
if (CMath::is_nan((float64_t)a) && CMath::is_nan((float64_t)b))
return true;
// Required for JSON Serialization Tests
if (tolerant)
return CMath::fequals_abs<T>(a, b, eps);
// handles float32_t and float64_t separately
if (sizeof(T) == 4)
comp = CMath::F_MIN_NORM_VAL32;
else
comp = CMath::F_MIN_NORM_VAL64;
if (a == b)
return true;
// both a and b are 0 and relative error is less meaningful
else if ((a == 0) || (b == 0) || (diff < comp))
return (diff < (eps * comp));
// use max(relative error, diff) to handle large eps
else
{
T check = ((diff/(absA + absB)) > diff)?
(diff/(absA + absB)):diff;
return (check < eps);
}
}
/* Get the corresponding absolute tolerance for unit test given a relative tolerance
*
* Note that a unit test will be passed only when
* \f[
* |v_\text{true} - v_\text{predict}| \leq tol_\text{relative} * |v_\text{true}|
* \f] which is equivalent to
* \f[
* |v_\text{true} - v_\text{predict}| \leq tol_\text{absolute}
* \f] where
* \f[
* tol_\text{absolute} = tol_\text{relative} * |v_\text{true}|
* \f]
*
* @param true_value true value should be finite (neither NAN nor INF)
* @param rel_tolerance relative tolerance should be positive and less than 1.0
*
* @return the corresponding absolute tolerance
*/
static float64_t get_abs_tolerance(float64_t true_value, float64_t rel_tolerance);
/** Rounds off the input value to it's nearest integer (as a floating-point value)
* @param d input decimal value
* @return rounded off value
*/
static inline float64_t round(float64_t d)
{
return ::floor(d+0.5);
}
/** The value of x rounded downward (as a floating-point value)
* @param d input decimal value
* @return rounded off value
*/
static inline float64_t floor(float64_t d)
{
return ::floor(d);
}
/** The value of x rounded upward (as a floating-point value)
* @param d input decimal value
* @return rounded off value
*/
static inline float64_t ceil(float64_t d)
{
return ::ceil(d);
}
/** Signum of input value
* @param a input value
* @return 1 if a>0, -1 if a<0
*/
template <class T>
static inline T sign(T a)
{
if (a==0)
return 0;
else return (a<0) ? (-1) : (+1);
}
/** Swaps a and b
* @param a first input value
* @param b second input value
*/
template <class T>
static inline void swap(T &a,T &b)
{
T c=a;
a=b;
b=c;
}
/** Computes square of the input
* @param x input value
* @return x*x (x^2)
*/
template <class T>
static inline T sq(T x)
{
return x*x;
}
/** Computes square-root of the input
* @param x input value
* @return x^0.5
*/
static inline float32_t sqrt(float32_t x)
{
return ::sqrtf(x);
}
/** Computes square-root of the input
* @param x input value
* @return x^0.5
*/
static inline float64_t sqrt(float64_t x)
{
return ::sqrt(x);
}
/** Computes square-root of the input
* @param x input value
* @return x^0.5
*/
static inline floatmax_t sqrt(floatmax_t x)
{
//fall back to double precision sqrt if sqrtl is not
//available
#ifdef HAVE_SQRTL
return ::sqrtl(x);
#else
return ::sqrt(x);
#endif
}
/// x^0.5, x being a complex128_t
COMPLEX128_STDMATH(sqrt)
/** Computes inverse square-root of the input
* @param x input value
* @return x^-0.5
*/
static inline float32_t invsqrt(float32_t x)
{
union float_to_int
{
float32_t f;
int32_t i;
};
float_to_int tmp;
tmp.f=x;
float32_t xhalf = 0.5f * x;
// store floating-point bits in integer tmp.i
// and do initial guess for Newton's method
tmp.i = 0x5f3759d5 - (tmp.i >> 1);
x = tmp.f; // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}
/**
* @name Exponential methods (x^n)
*/
//@{
/** Computes x raised to the power n
* @param x base
* @param n exponent
* @return x^n
*/
static inline floatmax_t powl(floatmax_t x, floatmax_t n)
{
//fall back to double precision pow if powl is not
//available
#ifdef HAVE_POWL
return ::powl((long double) x, (long double) n);
#else
return ::pow((double) x, (double) n);
#endif
}
static inline int32_t pow(bool x, int32_t n)
{
return 0;
}
/**
* @param x base (integer)
* @param n exponent (integer)
*/
static inline int32_t pow(int32_t x, int32_t n)
{
ASSERT(n>=0)
int32_t result=1;
while (n--)
result*=x;
return result;
}
/**
* @param x base (decimal)
* @param n exponent (integer)
*/
static inline float64_t pow(float64_t x, int32_t n)
{
if (n>=0)
{
float64_t result=1;
while (n--)
result*=x;
return result;
}
else
return ::pow((double)x, (double)n);
}
/**
* @param x base (decimal)
* @param n exponent (decimal)
*/
static inline float64_t pow(float64_t x, float64_t n)
{
return ::pow((double) x, (double) n);
}
/**
* @param x base (complex)
* @param n exponent (integer)
*/
static inline complex128_t pow(complex128_t x, int32_t n)
{
return std::pow(x, n);
}
/**
* @param x base (complex)
* @param n exponent (complex)
*/
static inline complex128_t pow(complex128_t x, complex128_t n)
{
return std::pow(x, n);
}
/**
* @param x base (complex)
* @param n exponent (decimal)
*/
static inline complex128_t pow(complex128_t x, float64_t n)
{
return std::pow(x, n);
}
/**
* @param x base (decimal)
* @param n exponent (complex)
*/
static inline complex128_t pow(float64_t x, complex128_t n)
{
return std::pow(x, n);
}
//@}
/** Computes e^x where e=2.71828 approx.
* @param x exponent
*/
static inline float64_t exp(float64_t x)
{
return ::exp((double) x);
}
/// Compute dot product between v1 and v2 (blas optimized)
static inline float64_t dot(const bool* v1, const bool* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((v1[i]) ? 1 : 0) * ((v2[i]) ? 1 : 0);
return r;
}
/// Compute dot product between v1 and v2 (blas optimized)
static inline floatmax_t dot(const floatmax_t* v1, const floatmax_t* v2, int32_t n)
{
floatmax_t r=0;
for (int32_t i=0; i<n; i++)
r+=v1[i]*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (blas optimized)
static float64_t dot(const float64_t* v1, const float64_t* v2, int32_t n);
/// Compute dot product between v1 and v2 (blas optimized)
static float32_t dot(const float32_t* v1, const float32_t* v2, int32_t n);
/// compute dot product between v1 and v2 (for 64bit unsigned ints)
static inline float64_t dot(
const uint64_t* v1, const uint64_t* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (for 64bit ints)
static inline float64_t dot(
const int64_t* v1, const int64_t* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (for 32bit ints)
static inline float64_t dot(
const int32_t* v1, const int32_t* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (for 32bit unsigned ints)
static inline float64_t dot(
const uint32_t* v1, const uint32_t* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (for 16bit unsigned ints)
static inline float64_t dot(
const uint16_t* v1, const uint16_t* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (for 16bit unsigned ints)
static inline float64_t dot(
const int16_t* v1, const int16_t* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (for 8bit (un)signed ints)
static inline float64_t dot(
const char* v1, const char* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (for 8bit (un)signed ints)
static inline float64_t dot(
const uint8_t* v1, const uint8_t* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2 (for 8bit (un)signed ints)
static inline float64_t dot(
const int8_t* v1, const int8_t* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// Compute dot product between v1 and v2
static inline float64_t dot(
const float64_t* v1, const char* v2, int32_t n)
{
float64_t r=0;
for (int32_t i=0; i<n; i++)
r+=((float64_t) v1[i])*v2[i];
return r;
}
/// exp(x), x being a complex128_t
COMPLEX128_STDMATH(exp)
/**
* @name Trignometric and Hyperbolic Functions
*/
//@{
/** Computes tangent of input
* @param x input
* @return tan(x)
*/
static inline float64_t tan(float64_t x)
{
return ::tan((double) x);
}
/// tan(x), x being a complex128_t
COMPLEX128_STDMATH(tan)
/** Computes arc tangent of input
* @param x input
* @return arctan(x)
*/
static inline float64_t atan(float64_t x)
{
return ::atan((double) x);
}
/// atan(x), x being a complex128_t not implemented
COMPLEX128_ERROR_ONEARG(atan)
/** Computes arc tangent with 2 parameters
* @param y input(numerator)
* @param x input(denominator)
* @return arctan(y/x)
*/
static inline float64_t atan2(float64_t y, float64_t x)
{
return ::atan2((double) y, (double) x);
}
/// atan2(x), x being a complex128_t not implemented
COMPLEX128_ERROR_ONEARG(atan2)
/** Computes hyperbolic tangent of input
* @param x input
* @return tanh(x)
*/
static inline float64_t tanh(float64_t x)
{
return ::tanh((double) x);
}
/// tanh(x), x being a complex128_t
COMPLEX128_STDMATH(tanh)
/** Computes sine of input
* @param x input
* @return sin(x)
*/
static inline float64_t sin(float64_t x)
{
return ::sin(x);
}
/// sin(x), x being a complex128_t
COMPLEX128_STDMATH(sin)
/** Computes arc sine of input
* @param x input
* @return arcsin(x)
*/
static inline float64_t asin(float64_t x)
{
return ::asin(x);
}
/// asin(x), x being a complex128_t not implemented
COMPLEX128_ERROR_ONEARG(asin)
/** Computes hyperbolic sine of input
* @param x input
* @return sinh(x)
*/
static inline float64_t sinh(float64_t x)
{
return ::sinh(x);
}
/// sinh(x), x being a complex128_t
COMPLEX128_STDMATH(sinh)
/** Computes cosine of input
* @param x input
* @return cos(x)
*/
static inline float64_t cos(float64_t x)
{
return ::cos(x);
}
/// cos(x), x being a complex128_t
COMPLEX128_STDMATH(cos)
/** Computes arc cosine of input
* @param x input
* @return arccos(x)
*/
static inline float64_t acos(float64_t x)
{
return ::acos(x);
}
/// acos(x), x being a complex128_t not implemented
COMPLEX128_ERROR_ONEARG(acos)
/** Computes hyperbolic cosine of input
* @param x input
* @return cosh(x)
*/
static inline float64_t cosh(float64_t x)
{
return ::cosh(x);
}
/// cosh(x), x being a complex128_t
COMPLEX128_STDMATH(cosh)
//@}
/**
* @name Logarithmic functions
*/
//@{
/** Computes logarithm base 10 of input
* @param v input
* @return log base 10 of v
*/
static inline float64_t log10(float64_t v)
{
return ::log(v)/::log(10.0);
}
/// log10(x), x being a complex128_t
COMPLEX128_STDMATH(log10)
/** Computes logarithm base 2 of input
* @param v input
* @return log base 2 of v
*/
static inline float64_t log2(float64_t v)
{
#ifdef HAVE_LOG2
return ::log2(v);
#else
return ::log(v)/::log(2.0);
#endif //HAVE_LOG2
}
/** Computes natural logarithm input
* @param v input
* @return log base e of v or ln(v)
*/
static inline float64_t log(float64_t v)
{
return ::log(v);
}
/// log(x), x being a complex128_t
COMPLEX128_STDMATH(log)
static inline index_t floor_log(index_t n)
{
index_t i;
for (i = 0; n != 0; i++)
n >>= 1;
return i;
}
//@}
/** Computes area under the curve
* @param xy
* @param len length
* @param reversed boolean
* @return area
*/
static float64_t area_under_curve(float64_t* xy, int32_t len, bool reversed)
{
ASSERT(len>0 && xy)
float64_t area = 0.0;
if (!reversed)
{
for (int i=1; i<len; i++)
area += 0.5*(xy[2*i]-xy[2*(i-1)])*(xy[2*i+1]+xy[2*(i-1)+1]);
}
else
{
for (int i=1; i<len; i++)
area += 0.5*(xy[2*i+1]-xy[2*(i-1)+1])*(xy[2*i]+xy[2*(i-1)]);
}
return area;
}
/** Converts string to float
* @param str input string
* @param float_result float pointer
* @return returns true if successful, else returns false
*/
static bool strtof(const char* str, float32_t* float_result);
/** Converts string to double
* @param str input string
* @param double_result double pointer
* @return returns true if successful, else returns false
*/
static bool strtod(const char* str, float64_t* double_result);
/** Converts string to long double
* @param str input string
* @param long_double_result long double pointer
* @return returns true if successful, else returns false
*/
static bool strtold(const char* str, floatmax_t* long_double_result);
/** Computes factorial of input
* @param n input
* @return factorial of n (n!)
*/
static inline int64_t factorial(int32_t n)
{
int64_t res=1;
for (int i=2; i<=n; i++)
res*=i ;
return res ;
}
//@}
/** Implements the greatest common divisor (gcd) via modulo operations.
* Requires that either a>0 and b>=0 or vice versa.
*
* @param a first number
* @param b second number
* @return gcd between the two numbers
*/
static int32_t gcd(int32_t a, int32_t b)