6#ifndef THEORETICA_COMMON_H
7#define THEORETICA_COMMON_H
54 template<
typename Un
signedIntType = u
int64_t>
69 c = ((
lower % 2 != 0) && (
upper % 2 != 0)) ? 1 : 0;
94 template<
typename Un
signedIntType = u
int64_t>
109 c = ((
lower % 2 != 0) && (
upper % 2 != 0)) ? 1 : 0;
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);
460 const real z = x - 1;
469 for (
int i = 2; i <= 24; ++i) {
549 template<
typename Un
signedIntType = u
int64_t>
554 return std::numeric_limits<UnsignedIntType>::max();
576 template<
typename Un
signedIntType = u
int64_t>
588 for (
int j = 0;
j < i; ++
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>
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)
742 return 1.0 /
exp(-x);
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);
890 asm(
"fsin" :
"+t"(x));
912 for (
int i = 1; i < 16; ++i) {
913 s =
s * -
sqr_x / (4 * i * i + 2 * i);
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));
1033 const int sign = (x > 0) ? 1 : -1;
1053 x = x / (1.0 +
sqrt(1.0 + x * x));
1063 for (
int n = 1;
n <= 15; ++
n) {
1078 for (
int i = 0; i <
red; ++i)
1213 return 0.5 *
ln((x + 1) / (1 - x));
1222 return 1.0 / (1.0 + 1.0 /
exp(x));
1235 return sin(
PI * x) / (
PI * x);
1248 return x > 0 ? 1 : 0;
1257 template<
typename IntType =
unsigned long long int>
1269 for (
unsigned int i =
n; i >
m; --i)
1283 typename = std::enable_if_t<std::is_integral<Integer1>::value>,
1284 typename = std::enable_if_t<std::is_integral<Integer2>::value>
1289 return a > 0 ? a : -a;
1291 return gcd(b, a % b);
1319 template<
typename T>
1321 return i ==
j ? 1 : 0;
1326 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:170
#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
Error handling for IO operations.
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:1243
double real
A real number, defined as a floating point type.
Definition constants.h:207
real acosh(real x)
Compute the inverse hyperbolic cosine.
Definition real_analysis.h:1194
TH_CONSTEXPR IntType binomial_coeff(unsigned int n, unsigned int m)
Compute the binomial coefficient.
Definition real_analysis.h:1258
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:694
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:1230
constexpr real PI2
Half of Pi.
Definition constants.h:228
TH_CONSTEXPR real radians(real degrees)
Convert degrees to radians.
Definition real_analysis.h:1300
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:1327
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:1320
dual2 asin(dual2 x)
Compute the arcsine of a second order dual number.
Definition dual2_functions.h:248
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:1286
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:226
real asinh(real x)
Compute the inverse hyperbolic sine.
Definition real_analysis.h:1188
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:706
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:1128
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:1221
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:1310
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:670
real coth(real x)
Compute the hyperbolic cotangent.
Definition real_analysis.h:1181
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:643
dual2 cot(dual2 x)
Compute the cotangent of a second order dual number.
Definition dual2_functions.h:119
@ OutOfDomain
Argument out of domain.
@ OutOfRange
Result out of range.
@ ImpossibleOperation
Mathematically impossible operation.
@ NoConvergence
Algorithm did not converge.
@ DivByZero
Division by zero.
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:577
real atanh(real x)
Compute the inverse hyperbolic tangent.
Definition real_analysis.h:1206
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:812
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:682
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: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: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