6 #ifndef THEORETICA_COMMON_H
7 #define THEORETICA_COMMON_H
24 template<typename Type, typename = std::enable_if<is_real_type<Type>::value>>
54 template<
typename Un
signedIntType = u
int64_t>
55 inline UnsignedIntType
isqrt(UnsignedIntType n) {
58 UnsignedIntType upper = n + 1;
61 UnsignedIntType lower = 0;
64 UnsignedIntType c = 0;
66 while(lower != upper - 1) {
69 c = ((lower % 2 != 0) && (upper % 2 != 0)) ? 1 : 0;
72 const UnsignedIntType m = (lower >> 1) + (upper >> 1) + c;
76 const UnsignedIntType q = n / m;
94 template<
typename Un
signedIntType = u
int64_t>
95 inline UnsignedIntType
icbrt(UnsignedIntType n) {
98 UnsignedIntType upper = n + 1;
101 UnsignedIntType lower = 0;
104 UnsignedIntType c = 0;
106 while(lower != upper - 1) {
109 c = ((lower % 2 != 0) && (upper % 2 != 0)) ? 1 : 0;
112 const UnsignedIntType m = (lower >> 1) + (upper >> 1) + c;
116 const UnsignedIntType q = (n / m) / m;
149 #ifdef THEORETICA_X86
157 asm(
"fsqrt" :
"+t"(x));
171 return 1.0 /
sqrt(1.0 / x);
179 y = (y + x / y) / 2.0;
213 return 1.0 /
cbrt(1.0 / x);
221 y = (y * 2.0 + x / (y * y)) / 3.0;
237 #ifdef THEORETICA_X86
245 asm(
"fabs" :
"+t"(x));
250 return x >= 0 ? x : -x;
260 return (x > 0) ? 1 : (x < 0 ? -1 : 0);
278 return x - (int(x) % 1);
302 #ifdef THEORETICA_BRANCHLESS
303 return (x + y +
abs(x - y)) / 2.0;
305 return x > y ? x : y;
317 return x > y ? x : y;
330 #ifdef THEORETICA_BRANCHLESS
331 return (x + y -
abs(x - y)) / 2.0;
333 return x > y ? y : x;
345 return x > y ? y : x;
356 return x > b ? b : (x < a ? a : x);
370 return x > b ? b : (x < a ? a : x);
376 #ifdef THEORETICA_X86
391 asm (
"fyl2x" :
"=t"(r) :
"0"(x),
"u"(y) :
"st(1)");
411 asm(
"f2xm1" :
"+t"(x));
438 #ifdef THEORETICA_X86
442 return fyl2x(x, 1.0);
447 return -
log2(1.0 / x);
451 while(x > (uint64_t(1) << i))
455 x /= (uint64_t(1) << i);
460 const real z = x - 1;
469 for (
int i = 2; i <= 24; ++i) {
474 log_z += sign * pow_z / i;
478 return i + (log_z /
LN2);
503 #ifdef THEORETICA_X86
507 return fyl2x(x, 1.0 /
LOG210);
534 #ifdef THEORETICA_X86
538 return fyl2x(x, 1.0 /
LOG2E);
549 template<
typename Un
signedIntType = u
int64_t>
550 inline UnsignedIntType
ilog2(UnsignedIntType x) {
557 UnsignedIntType bit = 0;
560 for (
int i = (
sizeof(UnsignedIntType) * 8 - 1); i > 0; --i) {
561 if(x & ((UnsignedIntType) 1 << i)) {
576 template<
typename Un
signedIntType = u
int64_t>
577 inline UnsignedIntType
pad2(UnsignedIntType x) {
579 UnsignedIntType bit = 0;
581 for (
int i = (
sizeof(UnsignedIntType) * 8 - 1); i > 0; --i) {
582 if(x & ((UnsignedIntType) 1 << i)) {
588 for (
int j = 0; j < i; ++j)
589 if(x & ((UnsignedIntType) 1 << j))
590 return (1 << (bit + 1));
602 template<
typename T = real>
612 for (; i < (n / 2); i *= 2)
616 for (; i < (n - 1); i += 2)
626 return T(1.0) /
pow(x, -n);
642 template<
typename T = real>
646 return neutral_element;
653 for (; i <= (n / 2); i *= 2)
657 for (; i < (n - 1); i += 2)
669 template<
typename IntType = u
int64_t>
673 for (
int i = n; i > 1; --i)
681 template<
typename T = u
int64_t>
685 for (
unsigned int i = 0; i < n; i++)
693 template<
typename T = u
int64_t>
697 for (
unsigned int i = 0; i < n; i++)
705 template<
typename IntType =
unsigned long long int>
710 for (
int i = n; i > 1; i -= 2)
717 #ifdef THEORETICA_X86
742 return 1.0 /
exp(-x);
745 const int floor_x =
floor(x);
757 s_n *= fract_x / (i * 4);
762 const real sqr_r = res * res;
763 return pow(
E, floor_x) * sqr_r * sqr_r;
781 for (
int i = 1; i <= 4; ++i) {
814 if(((n % 2 == 0) && (x < 0)) || (n == 0)) {
820 return 1.0 /
root(x, -n);
840 return 1.0 /
root(1.0 / x, n);
846 #ifdef THEORETICA_X86
856 const real y_pow =
pow(y, n - 1);
861 y = (y * (n - 1) + x / y_pow) / (
real) n;
882 #ifdef THEORETICA_X86
890 asm(
"fsin" :
"+t"(x));
910 const real sqr_x = x * x;
912 for (
int i = 1; i < 16; ++i) {
913 s = s * -sqr_x / (4 * i * i + 2 * i);
930 #ifdef THEORETICA_X86
938 asm(
"fcos" :
"+t"(x));
957 #if defined(THEORETICA_X86) && !defined(MSVC_ASM)
961 asm (
"fsincos" :
"=t"(c),
"=u"(s) :
"0"(x));
981 return (1 + t) / (1 - t);
1007 #if defined(THEORETICA_X86) && !defined(MSVC_ASM)
1009 asm (
"fsincos" :
"=t"(c),
"=u"(s) :
"0"(x));
1037 const real x2 = x * x;
1041 return x * (0.999999981788655
1042 + x2 * (-0.3333303670928597
1043 + x2 * (0.1999187202864565
1044 + x2 * (-0.1419779780241299
1045 + x2 * (0.1061837062890163
1046 + x2 * (-0.07456854814404323
1047 + x2 * (0.04213762366862284
1048 + x2 * (-0.0157312490955519
1049 + x2 * 0.002766283502978695))))))));
1125 return (exp_x - 1.0 / exp_x) / 2.0;
1136 return (exp_x + 1.0 / exp_x) / 2.0;
1145 return (exp_x - 1.0 / exp_x) / (exp_x + 1.0 / exp_x);
1154 return (exp_x + 1.0 / exp_x) / (exp_x - 1.0 / exp_x);
1179 if(x < -1 || x > 1) {
1184 return 0.5 *
ln((x + 1) / (1 - x));
1193 return 1.0 / (1.0 + 1.0 /
exp(x));
1206 return sin(
PI * x) / (
PI * x);
1219 return x > 0 ? 1 : 0;
1228 template<
typename IntType =
unsigned long long int>
1240 for (
unsigned int i = n; i > m; --i)
1243 return res /
fact(n - m);
1271 template<
typename T>
1273 return i == j ? 1 : 0;
1278 template<
typename IntType =
unsigned long long int>
Mathematical constants and default algorithm parameters.
#define TH_CONSTEXPR
Enable constexpr in function declarations if C++14 is supported.
Definition: constants.h:161
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
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:1214
double real
A real number, defined as a floating point type.
Definition: constants.h:198
real acosh(real x)
Compute the inverse hyperbolic cosine.
Definition: real_analysis.h:1165
TH_CONSTEXPR IntType binomial_coeff(unsigned int n, unsigned int m)
Compute the binomial coefficient.
Definition: real_analysis.h:1229
auto min(const Vector &X)
Finds the minimum value inside a dataset.
Definition: dataset.h:351
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition: dual2_functions.h:54
dual cosh(dual x)
Compute the hyperbolic cosine of a dual number.
Definition: dual_functions.h:205
TH_CONSTEXPR T rising_fact(T x, unsigned int n)
Compute the rising factorial of n.
Definition: real_analysis.h:694
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:151
real sinc(real x)
Compute the normalized sinc function.
Definition: real_analysis.h:1201
dual sinh(dual x)
Compute the hyperbolic sine of a dual number.
Definition: dual_functions.h:194
real inf()
Return positive infinity in floating point representation.
Definition: error.h:76
constexpr real PI2
Half of Pi.
Definition: constants.h:219
TH_CONSTEXPR real radians(real degrees)
Convert degrees to radians.
Definition: real_analysis.h:1252
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:198
dual2 log2(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:166
TH_CONSTEXPR IntType catalan(unsigned int n)
The n-th Catalan number.
Definition: real_analysis.h:1279
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:1272
dual2 asin(dual2 x)
Compute the arcsine of a second order dual number.
Definition: dual2_functions.h:204
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:243
UnsignedIntType ilog2(UnsignedIntType x)
Find the integer logarithm of x.
Definition: real_analysis.h:550
dual2 log10(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:182
TH_CONSTEXPR T ipow(T x, unsigned int n, T neutral_element=T(1))
Compute the n-th positive power of x (where n is natural)
Definition: real_analysis.h:643
real asinh(real x)
Compute the inverse hyperbolic sine.
Definition: real_analysis.h:1159
TH_CONSTEXPR IntType double_fact(unsigned int n)
Compute the double factorial of n.
Definition: real_analysis.h:706
constexpr real OPTIMIZATION_TOL
Approximation tolerance for root finding.
Definition: constants.h:288
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:1098
real powf(real x, real a)
Approximate x elevated to a real exponent.
Definition: real_analysis.h:795
constexpr real LN2
The natural logarithm of 2.
Definition: constants.h:249
constexpr real LOG2E
The binary logarithm of e.
Definition: constants.h:240
auto max(const Vector &X)
Finds the maximum value inside a dataset.
Definition: dataset.h:330
real sigmoid(real x)
Compute the sigmoid function.
Definition: real_analysis.h:1192
TH_CONSTEXPR real degrees(real radians)
Convert radians to degrees.
Definition: real_analysis.h:1262
real fract(real x)
Compute the fractional part of a real number.
Definition: real_analysis.h:288
TH_CONSTEXPR IntType fact(unsigned int n)
Compute the factorial of n.
Definition: real_analysis.h:670
real coth(real x)
Compute the hyperbolic cotangent.
Definition: real_analysis.h:1152
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition: dual2_functions.h:86
dual2 cot(dual2 x)
Compute the cotangent of a second order dual number.
Definition: dual2_functions.h:119
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:207
constexpr real E
The Euler mathematical constant (e)
Definition: constants.h:237
UnsignedIntType pad2(UnsignedIntType x)
Get the smallest power of 2 bigger than or equal to x.
Definition: real_analysis.h:577
real atanh(real x)
Compute the inverse hyperbolic tangent.
Definition: real_analysis.h:1177
constexpr int CORE_TAYLOR_ORDER
Order of Taylor series approximations.
Definition: constants.h:279
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:300
dual2 acos(dual2 x)
Compute the arcosine of a second order dual number.
Definition: dual2_functions.h:223
real root(real x, int n)
Compute the n-th root of x.
Definition: real_analysis.h:812
int sgn(real x)
Return the sign of x (1 if positive, -1 if negative, 0 if null)
Definition: real_analysis.h:259
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 T falling_fact(T x, unsigned int n)
Compute the falling factorial of n.
Definition: real_analysis.h:682
complex< T > identity(complex< T > z)
Complex identity.
Definition: complex_analysis.h:19
constexpr real DEG2RAD
The scalar conversion factor from degrees to radians.
Definition: constants.h:255
real expm1(real x)
Compute the exponential of x minus 1 more accurately for really small x.
Definition: real_analysis.h:773
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:258
constexpr real PI
The Pi mathematical constant.
Definition: constants.h:216
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
constexpr real TAU
The Tau mathematical constant (Pi times 2)
Definition: constants.h:228
dual2 atan(dual2 x)
Compute the arctangent of a second order dual number.
Definition: dual2_functions.h:242
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 Computes the maximum integer number that is smaller than x.
Definition: real_analysis.h:271
dual tanh(dual x)
Compute the hyperbolic tangent of a dual number.
Definition: dual_functions.h:216
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