Theoretica
Scientific Computing
Loading...
Searching...
No Matches
real_analysis.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_COMMON_H
7#define THEORETICA_COMMON_H
8
9#include "./core_traits.h"
10#include "./constants.h"
11#include "./error.h"
12
13
14namespace theoretica {
15
16
19 return x;
20 }
21
22
24 template<typename Type, typename = std::enable_if<is_real_type<Type>::value>>
25 TH_CONSTEXPR inline Type conjugate(Type x) {
26 return x;
27 }
28
29
36 return x * x;
37 }
38
39
46 return x * x * x;
47 }
48
49
54 template<typename UnsignedIntType = uint64_t>
55 inline UnsignedIntType isqrt(UnsignedIntType n) {
56
57 // Upper bound
58 UnsignedIntType upper = n + 1;
59
60 // Lower bound
61 UnsignedIntType lower = 0;
62
63 // Carry for safe long division
64 UnsignedIntType c = 0;
65
66 while(lower != upper - 1) {
67
68 // Compute carry for long division by 2
69 c = ((lower % 2 != 0) && (upper % 2 != 0)) ? 1 : 0;
70
71 // Safer division by 2 for big numbers
72 const UnsignedIntType m = (lower >> 1) + (upper >> 1) + c;
73
74 // Using division instead of multiplication avoids
75 // overflows which would remove significant bits
76 const UnsignedIntType q = n / m;
77
78 if(m > q)
79 upper = m;
80 else if(m < q)
81 lower = m;
82 else
83 return m;
84 }
85
86 return lower;
87 }
88
89
94 template<typename UnsignedIntType = uint64_t>
95 inline UnsignedIntType icbrt(UnsignedIntType n) {
96
97 // Upper bound
98 UnsignedIntType upper = n + 1;
99
100 // Lower bound
101 UnsignedIntType lower = 0;
102
103 // Carry for safe long division
104 UnsignedIntType c = 0;
105
106 while(lower != upper - 1) {
107
108 // Compute carry for long division by 2
109 c = ((lower % 2 != 0) && (upper % 2 != 0)) ? 1 : 0;
110
111 // Safer division by 2 for big numbers
112 const UnsignedIntType m = (lower >> 1) + (upper >> 1) + c;
113
114 // Using division instead of multiplication avoids
115 // overflows which would remove significant bits
116 const UnsignedIntType q = (n / m) / m;
117
118 if(m > q)
119 upper = m;
120 else if(m < q)
121 lower = m;
122 else
123 return m;
124 }
125
126 return lower;
127 }
128
129
142 inline real sqrt(real x) {
143
144 if(x < 0) {
146 return nan();
147 }
148
149#ifdef THEORETICA_X86
150
151 #ifdef MSVC_ASM
152 __asm {
153 fld x
154 fsqrt
155 }
156 #else
157 asm("fsqrt" : "+t"(x));
158 #endif
159
160 return x;
161#else
162
163 if(x < 1) {
164
165 if(x == 0)
166 return 0;
167
168 // Approximate sqrt(x) between 0 and 1
169 // The root of the inverse is the inverse of the root
170 // !!! Possible precision problems with smaller numbers
171 return 1.0 / sqrt(1.0 / x);
172 }
173
174 // Approximate sqrt(x) using Newton-Raphson
175 real y = x;
176 unsigned int i = 0;
177
178 while((square(y) - x) > OPTIMIZATION_TOL && i < OPTIMIZATION_NEWTON_ITER) {
179 y = (y + x / y) / 2.0;
180 i++;
181 }
182
183 return y;
184#endif
185
186 }
187
188
199 inline real cbrt(real x) {
200
201 if(x < 1) {
202
203 if(x == 0)
204 return 0;
205
206 // cbrt(x) is odd
207 if(x < 0)
208 return -cbrt(-x);
209
210 // Approximate cbrt between 0 and 1
211 // The root of the inverse is the inverse of the root
212 // !!! Possible precision problems with smaller numbers
213 return 1.0 / cbrt(1.0 / x);
214 }
215
216 // Approximate cbrt(x) using Newton-Raphson
217 real y = x;
218 unsigned int i = 0;
219
220 while((cube(y) - x) > OPTIMIZATION_TOL && i < OPTIMIZATION_NEWTON_ITER) {
221 y = (y * 2.0 + x / (y * y)) / 3.0;
222 i++;
223 }
224
225 return y;
226 }
227
228
235 inline real abs(real x) {
236
237#ifdef THEORETICA_X86
238
239 #ifdef MSVC_ASM
240 __asm {
241 fld x
242 fabs
243 }
244 #else
245 asm("fabs" : "+t"(x));
246 #endif
247
248 return x;
249#else
250 return x >= 0 ? x : -x;
251#endif
252
253 }
254
255
259 inline int sgn(real x) {
260 return (x > 0) ? 1 : (x < 0 ? -1 : 0);
261 }
262
263
271 TH_CONSTEXPR inline int floor(real x) {
272
273 if(x < 0 && x > -1)
274 return -1;
275
276 // Compute the biggest integer number
277 // that is smaller than x
278 return x - (int(x) % 1);
279 }
280
281
288 inline real fract(real x) {
289 return abs(x - floor(x));
290 }
291
292
300 inline real max(real x, real y) {
301
302 #ifdef THEORETICA_BRANCHLESS
303 return (x + y + abs(x - y)) / 2.0;
304 #else
305 return x > y ? x : y;
306 #endif
307 }
308
315 template<typename T>
316 inline T max(T x, T y) {
317 return x > y ? x : y;
318 }
319
320
328 inline real min(real x, real y) {
329
330 #ifdef THEORETICA_BRANCHLESS
331 return (x + y - abs(x - y)) / 2.0;
332 #else
333 return x > y ? y : x;
334 #endif
335 }
336
343 template<typename T>
344 inline T min(T x, T y) {
345 return x > y ? y : x;
346 }
347
348
355 inline real clamp(real x, real a, real b) {
356 return x > b ? b : (x < a ? a : x);
357 }
358
359
368 template<typename T>
369 inline T clamp(T x, T a, T b) {
370 return x > b ? b : (x < a ? a : x);
371 }
372
373
374 // x86 instruction wrappers
375
376#ifdef THEORETICA_X86
377
379 inline real fyl2x(real x, real y) {
380
381 #ifdef MSVC_ASM
382
383 __asm {
384 fld y
385 fld x
386 fyl2x
387 }
388
389 #else
390 double r;
391 asm ("fyl2x" : "=t"(r) : "0"(x), "u"(y) : "st(1)");
392 return r;
393 #endif
394 }
395
396
401 inline real f2xm1(real x) {
402
403 #ifdef MSVC_ASM
404
405 __asm {
406 fld x
407 f2xm1
408 }
409
410 #else
411 asm("f2xm1" : "+t"(x));
412 return x;
413 #endif
414 }
415
416#endif
417
425 inline real log2(real x) {
426
427 if(x <= 0) {
428
429 if(x == 0) {
431 return -inf();
432 }
433
435 return nan();
436 }
437
438#ifdef THEORETICA_X86
439
440 // Approximate the binary logarithm of x by
441 // exploiting x86 Assembly instructions
442 return fyl2x(x, 1.0);
443#else
444
445 // Domain reduction to [1, +inf)
446 if (x < 1.0)
447 return -log2(1.0 / x);
448
449 if (x == inf())
450 return inf();
451
452 if (is_nan(x))
453 return nan();
454
455 // Compute the biggest power of 2 so that x <= 2^i
456 unsigned int exponent = 0;
457 while(x > (uint64_t(1) << exponent))
458 exponent++;
459
460 // If the exponent is bigger than 64, truncate the fractional part.
461 // On some architectures, the fractional computation may stall otherwise.
462 if (exponent > 64)
463 return real(exponent);
464
465 // Domain reduction to [1, 2]
466 x /= (uint64_t(1) << exponent);
467
468 // Use the Taylor expansion of the logarithm
469 // ln(1 + x) = \sum_k^n (-1)^(k+1) x^k / k
470 real log_z = 0;
471 const real z = x - 1;
472
473 // Exact powers of 2 don't need further computation
474 if(abs(z) > MACH_EPSILON) {
475
476 int sign = 1;
477 real pow_z = z;
478 log_z = z;
479
480 for (int i = 2; i <= 24; ++i) {
481
482 pow_z *= z;
483 sign *= -1;
484
485 log_z += sign * pow_z / i;
486 }
487 }
488
489 return exponent + (log_z / LN2);
490#endif
491 }
492
493
501 inline real log10(real x) {
502
503 if(x <= 0) {
504
505 if(x == 0) {
507 return -inf();
508 }
509
511 return nan();
512 }
513
514#ifdef THEORETICA_X86
515
516 // Approximate the binary logarithm of x by
517 // exploiting x86 Assembly instructions
518 return fyl2x(x, 1.0 / LOG210);
519#else
520 return log2(x) / LOG210;
521#endif
522 }
523
524
532 inline real ln(real x) {
533
534 if(x <= 0) {
535
536 if(x == 0) {
538 return -inf();
539 }
540
542 return nan();
543 }
544
545#ifdef THEORETICA_X86
546
547 // Approximate the binary logarithm of x by
548 // exploiting x86 Assembly instructions
549 return fyl2x(x, 1.0 / LOG2E);
550#else
551 return log2(x) / LOG2E;
552#endif
553 }
554
555
560 template<typename UnsignedIntType = uint64_t>
561 inline UnsignedIntType ilog2(UnsignedIntType x) {
562
563 if(x == 0) {
565 return std::numeric_limits<UnsignedIntType>::max();
566 }
567
568 UnsignedIntType bit = 0;
569
570 // Find the highest set bit
571 for (int i = (sizeof(UnsignedIntType) * 8 - 1); i > 0; --i) {
572 if(x & ((UnsignedIntType) 1 << i)) {
573 bit = i;
574 break;
575 }
576 }
577
578 return bit;
579 }
580
581
587 template<typename UnsignedIntType = uint64_t>
588 inline UnsignedIntType pad2(UnsignedIntType x) {
589
590 UnsignedIntType bit = 0;
591
592 for (int i = (sizeof(UnsignedIntType) * 8 - 1); i > 0; --i) {
593 if(x & ((UnsignedIntType) 1 << i)) {
594
595 // Find the highest set bit
596 bit = i;
597
598 // Check if x is a power of 2
599 for (int j = 0; j < i; ++j)
600 if(x & ((UnsignedIntType) 1 << j))
601 return (1 << (bit + 1));
602 }
603 }
604
605 return (1 << bit);
606 }
607
608
613 template<typename T = real>
614 TH_CONSTEXPR inline T pow(T x, int n) {
615
616 if(n > 0) {
617
618 T res = x;
619 T x_sqr = x * x;
620 int i = 1;
621
622 // Self-multiply up to biggest power of 2
623 for (; i < (n / 2); i *= 2)
624 res = res * res;
625
626 // Multiply by x^2 for remaining even powers
627 for (; i < (n - 1); i += 2)
628 res = res * x_sqr;
629
630 // Multiply for remaining powers
631 for (; i < n; ++i)
632 res = res * x;
633
634 return res;
635
636 } else if(n < 0) {
637 return T(1.0) / pow(x, -n);
638 } else {
639 return 1;
640 }
641 }
642
643
653 template<typename T = real>
654 TH_CONSTEXPR inline T ipow(T x, unsigned int n, T neutral_element = T(1.0)) {
655
656 if(n == 0)
657 return neutral_element;
658
659 T res = x;
660 T x_sqr = x * x;
661 unsigned int i = 1;
662
663 // Self-multiply up to biggest power of 2
664 for (; i <= (n / 2); i *= 2)
665 res = res * res;
666
667 // Multiply by x^2 for remaining even powers
668 for (; i < (n - 1); i += 2)
669 res = res * x_sqr;
670
671 // Multiply for remaining powers
672 for (; i < n; ++i)
673 res = res * x;
674
675 return res;
676 }
677
678
680 template<typename IntType = uint64_t>
681 TH_CONSTEXPR inline IntType fact(unsigned int n) {
682
683 IntType res = 1;
684 for (int i = n; i > 1; --i)
685 res *= i;
686
687 return res;
688 }
689
690
692 template<typename T = uint64_t>
693 TH_CONSTEXPR inline T falling_fact(T x, unsigned int n) {
694
695 T res = 1;
696 for (unsigned int i = 0; i < n; i++)
697 res *= (x - i);
698
699 return res;
700 }
701
702
704 template<typename T = uint64_t>
705 TH_CONSTEXPR inline T rising_fact(T x, unsigned int n) {
706
707 T res = 1;
708 for (unsigned int i = 0; i < n; i++)
709 res *= (x + i);
710
711 return res;
712 }
713
714
716 template<typename IntType = unsigned long long int>
717 TH_CONSTEXPR inline IntType double_fact(unsigned int n) {
718
719 IntType res = 1;
720
721 for (int i = n; i > 1; i -= 2)
722 res *= i;
723
724 return res;
725 }
726
727
728#ifdef THEORETICA_X86
729
732 inline real exp_x86_norm(real x) {
733
734 // e^x is computed as 2^(x / ln2)
735 return square(f2xm1(x / (2 * LN2)) + 1);
736 }
737
738#endif
739
740
749 inline real exp(real x) {
750
751 // Domain reduction to [0, +inf]
752 if(x < 0)
753 return 1.0 / exp(-x);
754
755 const real fract_x = fract(x);
756 const int floor_x = floor(x);
757
758 // Taylor series expansion
759 // Compute e^floor(x) * e^fract(x)
760
761 real res = 1;
762 real s_n = 1;
763
764 for (int i = 1; i <= CORE_TAYLOR_ORDER; ++i) {
765
766 // Recurrence formula to improve
767 // numerical stability and performance
768 s_n *= fract_x / (i * 4);
769 res += s_n;
770 }
771
772 // The fractional part is divided by 4 to improve convergence
773 const real sqr_r = res * res;
774 return pow(E, floor_x) * sqr_r * sqr_r;
775 }
776
777
784 inline real expm1(real x) {
785
786 if(abs(x) > 0.001)
787 return exp(x) - 1;
788
789 real res = 0;
790 real s_n = 1;
791
792 for (int i = 1; i <= 4; ++i) {
793 s_n *= x / i;
794 res += s_n;
795 }
796
797 return res;
798 }
799
800
806 inline real powf(real x, real a) {
807
808 if(a < 0)
809 return 1.0 / exp(-a * ln(abs(x)) * sgn(x));
810
811 // x^a = e^(a * ln(x))
812 return exp(a * ln(abs(x)) * sgn(x));
813 }
814
815
823 inline real root(real x, int n) {
824
825 if(((n % 2 == 0) && (x < 0)) || (n == 0)) {
827 return nan();
828 }
829
830 if(n < 0)
831 return 1.0 / root(x, -n);
832
833 // Trivial cases
834 if(n == 1)
835 return x;
836
837 if(n == 2)
838 return sqrt(x);
839
840 if(n == 3)
841 return cbrt(x);
842
843 if(x < 1) {
844
845 if(x == 0)
846 return 0;
847
848 // Approximate root between 0 and 1
849 // The root of the inverse is the inverse of the root
850 // !!! Possible precision problems with smaller numbers
851 return 1.0 / root(1.0 / x, n);
852 }
853
854 // Approximate n-th root using Newton-Raphson.
855 // If fast exponentials and logarithms are available,
856 // use a first calculation to speed up convergence.
857#ifdef THEORETICA_X86
858 real y = exp(ln(x) / n);
859#else
860 real y = x;
861#endif
862
863 unsigned int i = 0;
864
865 while(i < OPTIMIZATION_NEWTON_ITER) {
866
867 const real y_pow = pow(y, n - 1);
868
869 if((y_pow * y - x) < OPTIMIZATION_TOL)
870 break;
871
872 y = (y * (n - 1) + x / y_pow) / (real) n;
873 i++;
874 }
875
876 if(i >= OPTIMIZATION_NEWTON_ITER) {
878 return nan();
879 }
880
881 return y;
882 }
883
884
891 inline real sin(real x) {
892
893#ifdef THEORETICA_X86
894
895 #ifdef MSVC_ASM
896 __asm {
897 fld x
898 fsin
899 }
900 #else
901 asm("fsin" : "+t"(x));
902 #endif
903
904 return x;
905
906#else
907
908 // Clamp x between -2PI and 2PI
909 if(abs(x) >= TAU)
910 x -= floor(x / TAU) * TAU;
911
912 // Domain reduction to [-PI, PI]
913 if(x > PI)
914 x = PI - x;
915 else if(x < -PI)
916 x = -PI - x;
917
918 // Compute series with recurrence formula
919 real res = x;
920 real s = x;
921 const real sqr_x = x * x;
922
923 for (int i = 1; i < 16; ++i) {
924 s = s * -sqr_x / (4 * i * i + 2 * i);
925 res += s;
926 }
927
928 return res;
929#endif
930 }
931
932
939 inline real cos(real x) {
940
941#ifdef THEORETICA_X86
942
943 #ifdef MSVC_ASM
944 __asm {
945 fld x
946 fcos
947 }
948 #else
949 asm("fcos" : "+t"(x));
950 #endif
951
952 return x;
953
954#else
955 return sin(PI2 - x);
956#endif
957 }
958
959
966 inline real tan(real x) {
967
968#if defined(THEORETICA_X86) && !defined(MSVC_ASM)
969
970 real s, c;
971
972 asm ("fsincos" : "=t"(c), "=u"(s) : "0"(x));
973
974 if(abs(c) < MACH_EPSILON) {
976 return nan();
977 }
978
979 return s / c;
980#else
981
982 // Reflection
983 if(x < 0)
984 return -tan(-x);
985
986 // Domain reduction to [0, PI]
987 x -= floor(x / PI) * PI;
988
989 // Domain reduction to [0, PI / 4]
990 if(x > (PI / 4)) {
991 const real t = tan(x - PI / 4.0);
992 return (1 + t) / (1 - t);
993 }
994
995 const real s = sin(x);
996 const real c = cos(x);
997
998 if(abs(c) < MACH_EPSILON) {
1000 return nan();
1001 }
1002
1003 return s / c;
1004#endif
1005 }
1006
1007
1014 inline real cot(real x) {
1015
1016 real s, c;
1017
1018#if defined(THEORETICA_X86) && !defined(MSVC_ASM)
1019
1020 asm ("fsincos" : "=t"(c), "=u"(s) : "0"(x));
1021#else
1022 s = sin(x);
1023 c = cos(x);
1024#endif
1025
1026 if(abs(s) < MACH_EPSILON) {
1028 return nan();
1029 }
1030
1031 return c / s;
1032 }
1033
1034
1041 inline real atan(real x) {
1042
1043 // Odd symmetry: atan(-x) = -atan(x)
1044 const int sign = (x > 0) ? 1 : -1;
1045 x = abs(x);
1046
1047 // Reduce to [0, 1]
1048 if (x > 1.0)
1049 return sign * (PI2 - atan(1.0 / x));
1050
1051 // Around x = 0, atan(x) = x
1052 if (x < MACH_EPSILON)
1053 return sign * x;
1054
1055 // Domain reduction using the half-argument formula:
1056 // atan(x) = 2 * atan(x / (1 + sqrt(1 + x^2)))
1057 constexpr real TAN_PI12 = 0.26794919243112270647;
1058 constexpr int MAX_ITER = 5;
1059 int red = 0;
1060
1061 while (x > TAN_PI12 && red < MAX_ITER) {
1062
1063 // Half-argument formula
1064 x = x / (1.0 + sqrt(1.0 + x * x));
1065 red++;
1066 }
1067
1068 // Adaptive Taylor series with atan(x) = sum_n (-1)^n x^(2n+1) / (2n+1)
1069 const real x2 = x * x;
1070 real power = x;
1071 real sum = x;
1072
1073 // Use Taylor series up to 15 terms or until convergence
1074 for (int n = 1; n <= 15; ++n) {
1075
1076 // Update power
1077 power *= -x2;
1078
1079 // Current term with alternating sign built into power
1080 const real term = power / (2 * n + 1);
1081 sum += term;
1082
1083 // Convergence criterion
1084 if (abs(term) < MACH_EPSILON * abs(sum))
1085 break;
1086 }
1087
1088 // Undo argument reductions
1089 for (int i = 0; i < red; ++i)
1090 sum *= 2.0;
1091
1092 return sign * sum;
1093 }
1094
1095
1102 inline real asin(real x) {
1103
1104 if(abs(x) > 1) {
1106 return nan();
1107 }
1108
1109 return atan(x / sqrt(1 - x * x));
1110 }
1111
1112
1120 inline real acos(real x) {
1121
1122 if(abs(x) > 1) {
1124 return nan();
1125 }
1126
1127 if(x < 0)
1128 return atan(sqrt(1 - x * x) / x) + PI;
1129 else
1130 return atan(sqrt(1 - x * x) / x);
1131 }
1132
1133
1139 inline real atan2(real y, real x) {
1140
1141 if(abs(x) < MACH_EPSILON) {
1142
1143 if(abs(y) < MACH_EPSILON) {
1145 return nan();
1146 }
1147
1148 return y >= 0 ? PI2 : -PI2;
1149 }
1150
1151 if(x > 0)
1152 return atan(y / x);
1153 else
1154 return y >= 0 ? atan(y / x) + PI : atan(y / x) - PI;
1155 }
1156
1157
1163 inline real sinh(real x) {
1164 real exp_x = exp(x);
1165 return (exp_x - 1.0 / exp_x) / 2.0;
1166 }
1167
1168
1174 inline real cosh(real x) {
1175 real exp_x = exp(x);
1176 return (exp_x + 1.0 / exp_x) / 2.0;
1177 }
1178
1179
1183 inline real tanh(real x) {
1184 real exp_x = exp(x);
1185 return (exp_x - 1.0 / exp_x) / (exp_x + 1.0 / exp_x);
1186 }
1187
1188
1192 inline real coth(real x) {
1193 real exp_x = exp(x);
1194 return (exp_x + 1.0 / exp_x) / (exp_x - 1.0 / exp_x);
1195 }
1196
1197
1199 inline real asinh(real x) {
1200 return ln(x + sqrt(square(x) + 1));
1201 }
1202
1203
1205 inline real acosh(real x) {
1206
1207 if(x < 1) {
1209 return nan();
1210 }
1211
1212 return ln(x + sqrt(square(x) - 1));
1213 }
1214
1215
1217 inline real atanh(real x) {
1218
1219 if(x < -1 || x > 1) {
1221 return nan();
1222 }
1223
1224 return 0.5 * ln((x + 1) / (1 - x));
1225 }
1226
1227
1232 inline real sigmoid(real x) {
1233 return 1.0 / (1.0 + 1.0 / exp(x));
1234 }
1235
1236
1241 inline real sinc(real x) {
1242
1243 if(abs(x) <= MACH_EPSILON)
1244 return 1;
1245
1246 return sin(PI * x) / (PI * x);
1247 }
1248
1249
1254 inline real heaviside(real x) {
1255
1256 if(abs(x) < MACH_EPSILON)
1257 return 0.5;
1258
1259 return x > 0 ? 1 : 0;
1260 }
1261
1262
1268 template<typename IntType = unsigned long long int>
1269 TH_CONSTEXPR inline IntType binomial_coeff(unsigned int n, unsigned int m) {
1270
1271 if(n < m) {
1272 TH_MATH_ERROR("binomial_coeff", n, MathError::ImpossibleOperation);
1273 return 0;
1274 }
1275
1276 // TO-DO Check out of range
1277
1278 IntType res = 1;
1279
1280 for (unsigned int i = n; i > m; --i)
1281 res *= i;
1282
1283 return res / fact(n - m);
1284 }
1285
1286
1292 template <
1293 typename Integer1, typename Integer2,
1294 typename = std::enable_if_t<std::is_integral<Integer1>::value>,
1295 typename = std::enable_if_t<std::is_integral<Integer2>::value>
1296 >
1297 Integer1 gcd(Integer1 a, Integer2 b) {
1298
1299 if (b == 0)
1300 return a > 0 ? a : -a;
1301
1302 return gcd(b, a % b);
1303 }
1304
1305
1312 return degrees * DEG2RAD;
1313 }
1314
1315
1322 return radians * RAD2DEG;
1323 }
1324
1325
1330 template<typename T>
1331 TH_CONSTEXPR inline T kronecker_delta(T i, T j) {
1332 return i == j ? 1 : 0;
1333 }
1334
1335
1337 template<typename IntType = unsigned long long int>
1338 TH_CONSTEXPR inline IntType catalan(unsigned int n) {
1339 return binomial_coeff(2 * n, n) / (n + 1);
1340 }
1341
1342
1353 template <
1354 typename Type = real,
1355 disable_vector<Type> = true
1356 >
1357 TH_CONSTEXPR inline Type make_error() {
1358 return Type(nan());
1359 }
1360
1361}
1362
1363#endif
Mathematical constants and default algorithm parameters.
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition constants.h:170
Error handling with errno and exceptions.
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Fundamental type traits.
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
real heaviside(real x)
Compute the heaviside function.
Definition real_analysis.h:1254
real acosh(real x)
Compute the inverse hyperbolic cosine.
Definition real_analysis.h:1205
TH_CONSTEXPR IntType binomial_coeff(unsigned int n, unsigned int m)
Compute the binomial coefficient.
Definition real_analysis.h:1269
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition dataset.h:347
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition dual2_functions.h:54
TH_CONSTEXPR T rising_fact(T x, unsigned int n)
Compute the rising factorial of n.
Definition real_analysis.h:705
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition dual2_functions.h:195
real sinc(real x)
Compute the normalized sinc function.
Definition real_analysis.h:1241
bool is_nan(const T &x)
Check whether a generic variable is (equivalent to) a NaN number.
Definition error.h:90
constexpr real PI2
Half of Pi.
Definition constants.h:228
TH_CONSTEXPR real radians(real degrees)
Convert degrees to radians.
Definition real_analysis.h:1311
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
dual2 log2(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition dual2_functions.h:210
TH_CONSTEXPR IntType catalan(unsigned int n)
The n-th Catalan number.
Definition real_analysis.h:1338
TH_CONSTEXPR T kronecker_delta(T i, T j)
Kronecker delta, equals 1 if i is equal to j, 0 otherwise.
Definition real_analysis.h:1331
dual2 asin(dual2 x)
Compute the arcsine of a second order dual number.
Definition dual2_functions.h:248
double real
A real number, defined as a floating point type.
Definition constants.h:207
complex< T > identity(complex< T > z)
Complex identity.
Definition complex_analysis.h:19
dual2 exp(dual2 x)
Compute the exponential of a second order dual number.
Definition dual2_functions.h:138
constexpr real LOG210
The binary logarithm of 10.
Definition constants.h:252
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
Integer1 gcd(Integer1 a, Integer2 b)
Compute the greatest common divisor of two numbers using Euclid's algorithm.
Definition real_analysis.h:1297
UnsignedIntType ilog2(UnsignedIntType x)
Find the integer logarithm of x.
Definition real_analysis.h:561
dual2 log10(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition dual2_functions.h:226
real asinh(real x)
Compute the inverse hyperbolic sine.
Definition real_analysis.h:1199
auto sum(const Vector &X)
Compute the sum of a vector of real values using pairwise summation to reduce round-off error.
Definition dataset.h:215
TH_CONSTEXPR IntType double_fact(unsigned int n)
Compute the double factorial of n.
Definition real_analysis.h:717
constexpr real OPTIMIZATION_TOL
Approximation tolerance for root finding.
Definition constants.h:297
dual2 conjugate(dual2 x)
Return the conjugate of a second order dual number.
Definition dual2_functions.h:35
real cbrt(real x)
Compute the cubic root of x.
Definition real_analysis.h:199
UnsignedIntType isqrt(UnsignedIntType n)
Compute the integer square root of a positive integer.
Definition real_analysis.h:55
real atan2(real y, real x)
Compute the 2 argument arctangent.
Definition real_analysis.h:1139
constexpr real LN2
The natural logarithm of 2.
Definition constants.h:258
constexpr real LOG2E
The binary logarithm of e.
Definition constants.h:249
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition dataset.h:326
real sigmoid(real x)
Compute the sigmoid function.
Definition real_analysis.h:1232
dual2 tanh(dual2 x)
Compute the hyperbolic tangent of a second order dual number.
Definition dual2_functions.h:179
TH_CONSTEXPR real degrees(real radians)
Convert radians to degrees.
Definition real_analysis.h:1321
real fract(real x)
Compute the fractional part of a real number.
Definition real_analysis.h:288
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
dual2 sinh(dual2 x)
Compute the hyperbolic sine of a second order dual number.
Definition dual2_functions.h:151
TH_CONSTEXPR IntType fact(unsigned int n)
Compute the factorial of n.
Definition real_analysis.h:681
real coth(real x)
Compute the hyperbolic cotangent.
Definition real_analysis.h:1192
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
TH_CONSTEXPR T ipow(T x, unsigned int n, T neutral_element=T(1.0))
Compute the n-th positive power of x (where n is natural).
Definition real_analysis.h:654
dual2 cot(dual2 x)
Compute the cotangent of a second order dual number.
Definition dual2_functions.h:119
@ OutOfDomain
Argument out of domain.
Definition error.h:28
@ OutOfRange
Result out of range.
Definition error.h:29
@ ImpossibleOperation
Mathematically impossible operation.
Definition error.h:30
@ NoConvergence
Algorithm did not converge.
Definition error.h:31
@ DivByZero
Division by zero.
Definition error.h:27
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
constexpr real E
The Euler mathematical constant (e).
Definition constants.h:246
UnsignedIntType pad2(UnsignedIntType x)
Get the smallest power of 2 bigger than or equal to x.
Definition real_analysis.h:588
real atanh(real x)
Compute the inverse hyperbolic tangent.
Definition real_analysis.h:1217
constexpr int CORE_TAYLOR_ORDER
Order of Taylor series approximations.
Definition constants.h:288
dual2 tan(dual2 x)
Compute the tangent of a second order dual number.
Definition dual2_functions.h:100
constexpr unsigned int OPTIMIZATION_NEWTON_ITER
Maximum number of iterations for the Newton-Raphson algorithm.
Definition constants.h:309
dual2 cosh(dual2 x)
Compute the hyperbolic cosine of a second order dual number.
Definition dual2_functions.h:165
dual2 acos(dual2 x)
Compute the arcosine of a second order dual number.
Definition dual2_functions.h:267
real root(real x, int n)
Compute the n-th root of x.
Definition real_analysis.h:823
int sgn(real x)
Return the sign of x (1 if positive, -1 if negative, 0 if null).
Definition real_analysis.h:259
complex< T > powf(complex< T > z, real p)
Compute a complex number raised to a real power.
Definition complex_analysis.h:65
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition dual2_functions.h:72
UnsignedIntType icbrt(UnsignedIntType n)
Compute the integer cubic root of a positive integer.
Definition real_analysis.h:95
TH_CONSTEXPR real inf()
Get positive infinity in floating point representation.
Definition error.h:96
TH_CONSTEXPR T falling_fact(T x, unsigned int n)
Compute the falling factorial of n.
Definition real_analysis.h:693
constexpr real DEG2RAD
The scalar conversion factor from degrees to radians.
Definition constants.h:264
real expm1(real x)
Compute the exponential of x minus 1 more accurately for really small x.
Definition real_analysis.h:784
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23
constexpr real RAD2DEG
The scalar conversion factor from radians to degrees.
Definition constants.h:267
constexpr real PI
The Pi mathematical constant.
Definition constants.h:225
constexpr real TAU
The Tau mathematical constant (Pi times 2).
Definition constants.h:237
dual2 atan(dual2 x)
Compute the arctangent of a second order dual number.
Definition dual2_functions.h:286
real clamp(real x, real a, real b)
Clamp x between a and b.
Definition real_analysis.h:355
TH_CONSTEXPR int floor(real x)
Compute the floor of x, as the maximum integer number that is smaller than x.
Definition real_analysis.h:271
dual2 pow(dual2 x, int n)
Compute the n-th power of a second order dual number.
Definition dual2_functions.h:41
dual2 cube(dual2 x)
Return the cube of a second order dual number.
Definition dual2_functions.h:29