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;
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;
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);
391 asm (
"fyl2x" :
"=t"(r) :
"0"(x),
"u"(y) :
"st(1)");
411 asm(
"f2xm1" :
"+t"(x));
442 return fyl2x(x, 1.0);
447 return -
log2(1.0 / x);
456 unsigned int exponent = 0;
457 while(x > (uint64_t(1) << exponent))
463 return real(exponent);
466 x /= (uint64_t(1) << exponent);
471 const real z = x - 1;
480 for (
int i = 2; i <= 24; ++i) {
485 log_z += sign * pow_z / i;
489 return exponent + (log_z /
LN2);
518 return fyl2x(x, 1.0 /
LOG210);
549 return fyl2x(x, 1.0 /
LOG2E);
560 template<
typename Un
signedIntType = u
int64_t>
561 inline UnsignedIntType
ilog2(UnsignedIntType x) {
565 return std::numeric_limits<UnsignedIntType>::max();
568 UnsignedIntType bit = 0;
571 for (
int i = (
sizeof(UnsignedIntType) * 8 - 1); i > 0; --i) {
572 if(x & ((UnsignedIntType) 1 << i)) {
587 template<
typename Un
signedIntType = u
int64_t>
588 inline UnsignedIntType
pad2(UnsignedIntType x) {
590 UnsignedIntType bit = 0;
592 for (
int i = (
sizeof(UnsignedIntType) * 8 - 1); i > 0; --i) {
593 if(x & ((UnsignedIntType) 1 << i)) {
599 for (
int j = 0; j < i; ++j)
600 if(x & ((UnsignedIntType) 1 << j))
601 return (1 << (bit + 1));
613 template<
typename T = real>
623 for (; i < (n / 2); i *= 2)
627 for (; i < (n - 1); i += 2)
637 return T(1.0) /
pow(x, -n);
653 template<
typename T = real>
657 return neutral_element;
664 for (; i <= (n / 2); i *= 2)
668 for (; i < (n - 1); i += 2)
680 template<
typename IntType = u
int64_t>
684 for (
int i = n; i > 1; --i)
692 template<
typename T = u
int64_t>
696 for (
unsigned int i = 0; i < n; i++)
704 template<
typename T = u
int64_t>
708 for (
unsigned int i = 0; i < n; i++)
716 template<
typename IntType =
unsigned long long int>
721 for (
int i = n; i > 1; i -= 2)
753 return 1.0 /
exp(-x);
756 const int floor_x =
floor(x);
768 s_n *= fract_x / (i * 4);
773 const real sqr_r = res * res;
774 return pow(
E, floor_x) * sqr_r * sqr_r;
792 for (
int i = 1; i <= 4; ++i) {
825 if(((n % 2 == 0) && (x < 0)) || (n == 0)) {
831 return 1.0 /
root(x, -n);
851 return 1.0 /
root(1.0 / x, n);
867 const real y_pow =
pow(y, n - 1);
872 y = (y * (n - 1) + x / y_pow) / (
real) n;
901 asm(
"fsin" :
"+t"(x));
921 const real sqr_x = x * x;
923 for (
int i = 1; i < 16; ++i) {
924 s = s * -sqr_x / (4 * i * i + 2 * i);
949 asm(
"fcos" :
"+t"(x));
968#if defined(THEORETICA_X86) && !defined(MSVC_ASM)
972 asm (
"fsincos" :
"=t"(c),
"=u"(s) :
"0"(x));
992 return (1 + t) / (1 - t);
1018#if defined(THEORETICA_X86) && !defined(MSVC_ASM)
1020 asm (
"fsincos" :
"=t"(c),
"=u"(s) :
"0"(x));
1044 const int sign = (x > 0) ? 1 : -1;
1049 return sign * (
PI2 -
atan(1.0 / x));
1057 constexpr real TAN_PI12 = 0.26794919243112270647;
1058 constexpr int MAX_ITER = 5;
1061 while (x > TAN_PI12 && red < MAX_ITER) {
1064 x = x / (1.0 +
sqrt(1.0 + x * x));
1069 const real x2 = x * x;
1074 for (
int n = 1; n <= 15; ++n) {
1080 const real term = power / (2 * n + 1);
1089 for (
int i = 0; i < red; ++i)
1148 return y >= 0 ?
PI2 : -
PI2;
1165 return (exp_x - 1.0 / exp_x) / 2.0;
1176 return (exp_x + 1.0 / exp_x) / 2.0;
1185 return (exp_x - 1.0 / exp_x) / (exp_x + 1.0 / exp_x);
1194 return (exp_x + 1.0 / exp_x) / (exp_x - 1.0 / exp_x);
1219 if(x < -1 || x > 1) {
1224 return 0.5 *
ln((x + 1) / (1 - x));
1233 return 1.0 / (1.0 + 1.0 /
exp(x));
1246 return sin(
PI * x) / (
PI * x);
1259 return x > 0 ? 1 : 0;
1268 template<
typename IntType =
unsigned long long int>
1280 for (
unsigned int i = n; i > m; --i)
1283 return res /
fact(n - m);
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>
1297 Integer1
gcd(Integer1 a, Integer2 b) {
1300 return a > 0 ? a : -a;
1302 return gcd(b, a % b);
1330 template<
typename T>
1332 return i == j ? 1 : 0;
1337 template<
typename IntType =
unsigned long long int>
1354 typename Type =
real,
1355 disable_vector<Type> =
true
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
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