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);
461 const real z = x - 1;
470 for (
int i = 2; i <= 24; ++i) {
475 log_z += sign * pow_z / i;
479 return i + (log_z /
LN2);
504 #ifdef THEORETICA_X86
508 return fyl2x(x, 1.0 /
LOG210);
535 #ifdef THEORETICA_X86
539 return fyl2x(x, 1.0 /
LOG2E);
550 template<
typename Un
signedIntType = u
int64_t>
551 inline UnsignedIntType
ilog2(UnsignedIntType x) {
558 UnsignedIntType bit = 0;
561 for (
int i = (
sizeof(UnsignedIntType) * 8 - 1); i > 0; --i) {
562 if(x & ((UnsignedIntType) 1 << i)) {
577 template<
typename Un
signedIntType = u
int64_t>
578 inline UnsignedIntType
pad2(UnsignedIntType x) {
580 UnsignedIntType bit = 0;
582 for (
int i = (
sizeof(UnsignedIntType) * 8 - 1); i > 0; --i) {
583 if(x & ((UnsignedIntType) 1 << i)) {
589 for (
int j = 0; j < i; ++j)
590 if(x & ((UnsignedIntType) 1 << j))
591 return (1 << (bit + 1));
603 template<
typename T = real>
613 for (; i < (n / 2); i *= 2)
617 for (; i < (n - 1); i += 2)
627 return T(1.0) /
pow(x, -n);
643 template<
typename T = real>
647 return neutral_element;
654 for (; i <= (n / 2); i *= 2)
658 for (; i < (n - 1); i += 2)
670 template<
typename IntType = u
int64_t>
674 for (
int i = n; i > 1; --i)
682 template<
typename T = u
int64_t>
686 for (
unsigned int i = 0; i < n; i++)
694 template<
typename T = u
int64_t>
698 for (
unsigned int i = 0; i < n; i++)
706 template<
typename IntType =
unsigned long long int>
711 for (
int i = n; i > 1; i -= 2)
718 #ifdef THEORETICA_X86
743 return 1.0 /
exp(-x);
746 const int floor_x =
floor(x);
758 s_n *= fract_x / (i * 4);
763 const real sqr_r = res * res;
764 return pow(
E, floor_x) * sqr_r * sqr_r;
782 for (
int i = 1; i <= 4; ++i) {
815 if(((n % 2 == 0) && (x < 0)) || (n == 0)) {
821 return 1.0 /
root(x, -n);
841 return 1.0 /
root(1.0 / x, n);
847 #ifdef THEORETICA_X86
857 const real y_pow =
pow(y, n - 1);
862 y = (y * (n - 1) + x / y_pow) / (
real) n;
883 #ifdef THEORETICA_X86
891 asm(
"fsin" :
"+t"(x));
911 const real sqr_x = x * x;
913 for (
int i = 1; i < 16; ++i) {
914 s = s * -sqr_x / (4 * i * i + 2 * i);
931 #ifdef THEORETICA_X86
939 asm(
"fcos" :
"+t"(x));
958 #if defined(THEORETICA_X86) && !defined(MSVC_ASM)
962 asm (
"fsincos" :
"=t"(c),
"=u"(s) :
"0"(x));
982 return (1 + t) / (1 - t);
1008 #if defined(THEORETICA_X86) && !defined(MSVC_ASM)
1010 asm (
"fsincos" :
"=t"(c),
"=u"(s) :
"0"(x));
1038 const real x2 = x * x;
1042 return x * (0.999999981788655
1043 + x2 * (-0.3333303670928597
1044 + x2 * (0.1999187202864565
1045 + x2 * (-0.1419779780241299
1046 + x2 * (0.1061837062890163
1047 + x2 * (-0.07456854814404323
1048 + x2 * (0.04213762366862284
1049 + x2 * (-0.0157312490955519
1050 + x2 * 0.002766283502978695))))))));
1126 return (exp_x - 1.0 / exp_x) / 2.0;
1137 return (exp_x + 1.0 / exp_x) / 2.0;
1146 return (exp_x - 1.0 / exp_x) / (exp_x + 1.0 / exp_x);
1155 return (exp_x + 1.0 / exp_x) / (exp_x - 1.0 / exp_x);
1180 if(x < -1 || x > 1) {
1185 return 0.5 *
ln((x + 1) / (1 - x));
1194 return 1.0 / (1.0 + 1.0 /
exp(x));
1207 return sin(
PI * x) / (
PI * x);
1220 return x > 0 ? 1 : 0;
1229 template<
typename IntType =
unsigned long long int>
1241 for (
unsigned int i = n; i > m; --i)
1244 return res /
fact(n - m);
1272 template<
typename T>
1274 return i == j ? 1 : 0;
1279 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:151
#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:1215
double real
A real number, defined as a floating point type.
Definition: constants.h:188
real acosh(real x)
Compute the inverse hyperbolic cosine.
Definition: real_analysis.h:1166
TH_CONSTEXPR IntType binomial_coeff(unsigned int n, unsigned int m)
Compute the binomial coefficient.
Definition: real_analysis.h:1230
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:194
TH_CONSTEXPR T rising_fact(T x, unsigned int n)
Compute the rising factorial of n.
Definition: real_analysis.h:695
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:139
real sinc(real x)
Compute the normalized sinc function.
Definition: real_analysis.h:1202
dual sinh(dual x)
Compute the hyperbolic sine of a dual number.
Definition: dual_functions.h:186
real inf()
Return positive infinity in floating point representation.
Definition: error.h:76
constexpr real PI2
Half of Pi.
Definition: constants.h:209
TH_CONSTEXPR real radians(real degrees)
Convert degrees to radians.
Definition: real_analysis.h:1253
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
dual2 log2(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:153
TH_CONSTEXPR IntType catalan(unsigned int n)
The n-th Catalan number.
Definition: real_analysis.h:1280
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:1273
dual2 asin(dual2 x)
Compute the arcsine of a second order dual number.
Definition: dual2_functions.h:189
dual2 exp(dual2 x)
Compute the exponential of a second order dual number.
Definition: dual2_functions.h:130
constexpr real LOG210
The binary logarithm of 10.
Definition: constants.h:233
UnsignedIntType ilog2(UnsignedIntType x)
Find the integer logarithm of x.
Definition: real_analysis.h:551
dual2 log10(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:168
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:644
real asinh(real x)
Compute the inverse hyperbolic sine.
Definition: real_analysis.h:1160
TH_CONSTEXPR IntType double_fact(unsigned int n)
Compute the double factorial of n.
Definition: real_analysis.h:707
constexpr real OPTIMIZATION_TOL
Approximation tolerance for root finding.
Definition: constants.h:278
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:1099
real powf(real x, real a)
Approximate x elevated to a real exponent.
Definition: real_analysis.h:796
constexpr real LN2
The natural logarithm of 2.
Definition: constants.h:239
constexpr real LOG2E
The binary logarithm of e.
Definition: constants.h:230
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:1193
TH_CONSTEXPR real degrees(real radians)
Convert radians to degrees.
Definition: real_analysis.h:1263
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:671
real coth(real x)
Compute the hyperbolic cotangent.
Definition: real_analysis.h:1153
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition: dual2_functions.h:82
dual2 cot(dual2 x)
Compute the cotangent of a second order dual number.
Definition: dual2_functions.h:112
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
constexpr real E
The Euler mathematical constant (e)
Definition: constants.h:227
UnsignedIntType pad2(UnsignedIntType x)
Get the smallest power of 2 bigger than or equal to x.
Definition: real_analysis.h:578
real atanh(real x)
Compute the inverse hyperbolic tangent.
Definition: real_analysis.h:1178
constexpr int CORE_TAYLOR_ORDER
Order of Taylor series approximations.
Definition: constants.h:269
dual2 tan(dual2 x)
Compute the tangent of a second order dual number.
Definition: dual2_functions.h:94
constexpr unsigned int OPTIMIZATION_NEWTON_ITER
Maximum number of iterations for the Newton-Raphson algorithm.
Definition: constants.h:290
dual2 acos(dual2 x)
Compute the arcosine of a second order dual number.
Definition: dual2_functions.h:206
real root(real x, int n)
Compute the n-th root of x.
Definition: real_analysis.h:813
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:70
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:683
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:245
real expm1(real x)
Compute the exponential of x minus 1 more accurately for really small x.
Definition: real_analysis.h:774
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:248
constexpr real PI
The Pi mathematical constant.
Definition: constants.h:206
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:218
dual2 atan(dual2 x)
Compute the arctangent of a second order dual number.
Definition: dual2_functions.h:223
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:202
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