Theoretica
A C++ numerical and automatic mathematical library
theoretica Namespace Reference

Main namespace of the library which contains all functions and objects. More...

Namespaces

 algebra
 Linear algebra routines.
 
 autodiff
 Differential operators with automatic differentiation.
 
 distribution
 Probability distribution functions.
 
 ode
 Numerical methods for ordinary differential equations.
 
 parallel
 Parallelized element-wise evaluation of functions.
 
 regression
 Regression to a model.
 
 signal
 Signal processing module.
 
 special
 Special functions.
 
 stats
 Statistical functions.
 
 tables
 Tabulated values.
 
 taylor
 Taylor series expansions.
 

Classes

class  mat_iterator
 A sequential iterator for matrices. More...
 
class  mat
 A generic matrix with a fixed number of rows and columns. More...
 
class  mat< Type, 0, 0 >
 A generic matrix with a variable number of rows and columns. More...
 
class  vec_iterator
 for vectors. More...
 
class  vec
 A statically allocated N-dimensional vector with elements of the given type. More...
 
class  vec< Type, 0 >
 
class  dual
 Dual number class. More...
 
class  dual2
 Second order dual number class. More...
 
class  multidual
 Multidual number algebra for functions of the form \(f: \mathbb{R}^n \rightarrow \mathbb{R}\). More...
 
class  complex
 Complex number in algebraic form \(a + ib\). More...
 
struct  is_complex_type
 Type trait to check whether the given type is a specialization of the complex number class or not, using the static boolean element is_complex_type<T>::value. More...
 
struct  is_complex_type< complex< T > >
 Type trait to check whether the given type is a specialization of the complex number class or not, using the static boolean element is_complex_type<T>::value. More...
 
class  phasor
 Complex number in exponential form \(\rho e^{i \theta}\). More...
 
class  quat
 Quaternion class in the form \(a + bi + cj + dk\). More...
 
struct  is_real_type
 Type trait to check whether a type represents a real number. More...
 
struct  is_real_type< real >
 Type trait to check whether a type represents a real number. More...
 
struct  is_orderable
 Check whether a structure is orderable, by checking that it has a comparison operator<(). More...
 
struct  is_orderable< Structure, _internal::void_t< decltype(std::declval< Structure >()< std::declval< Structure >())> >
 
struct  is_indexable
 Check whether a structure is indexable by a single integer index, by checking that it has the operator[](0). More...
 
struct  is_indexable< Structure, _internal::void_t< decltype(std::declval< Structure >()[0])> >
 
struct  is_iterable
 Check whether a structure is iterable, by checking that it has a method begin(). More...
 
struct  is_iterable< Structure, _internal::void_t< decltype(std::declval< Structure >().begin())> >
 
struct  is_vector
 Check whether a structure is considerable a vector, by checking that it has an operator[] and a size() method. More...
 
struct  is_vector< Structure, _internal::void_t< decltype(std::declval< Structure >()[0]), decltype(std::declval< Structure >().size())> >
 
struct  is_matrix
 Check whether a structure is considerable a matrix, by checking that it has an operator(), a rows() method and a cols() method. More...
 
struct  is_matrix< Structure, _internal::void_t< decltype(std::declval< Structure >()(0, 0)), decltype(std::declval< Structure >().rows()), decltype(std::declval< Structure >().cols())> >
 
struct  has_type_elements
 Type trait to check whether an indexable container has elements of the given type. More...
 
struct  extract_func_args
 Extract the type of the arguments of a function. More...
 
struct  extract_func_args< Function(Args...)>
 
class  ratio
 representing a ratio between two objects, like a fraction or a rational polynomial. More...
 
struct  spline_node
 A cubic splines node for a given x interval. More...
 
class  spline
 A natural cubic spline interpolation class. More...
 
class  polynomial
 A polynomial of arbitrary order. More...
 
class  PRNG
 A pseudorandom number generator. More...
 
struct  pdf_sampler
 A probability density function sampler which generates pseudorandom numbers following asymptotically a given distribution \(f(x; \vec \theta)\). More...
 
class  histogram
 Histogram class with running statistics, can be constructed from the parameters of the bins or from a dataset. More...
 

Typedefs

using mat2 = mat< real, 2, 2 >
 A 2x2 matrix with real entries.
 
using mat3 = mat< real, 3, 3 >
 A 3x3 matrix with real entries.
 
using mat4 = mat< real, 4, 4 >
 A 4x4 matrix with real entries.
 
using cmat = mat< complex<> >
 A variable size matrix with complex entries.
 
using cmat2 = mat< complex< real >, 2, 2 >
 A 2x2 matrix with real entries.
 
using cmat3 = mat< complex< real >, 3, 3 >
 A 3x3 matrix with real entries.
 
using cmat4 = mat< complex< real >, 4, 4 >
 A 4x4 matrix with real entries.
 
using vec2 = vec< real, 2 >
 A 2-dimensional vector with real elements.
 
using vec3 = vec< real, 3 >
 A 3-dimensional vector with real elements.
 
using vec4 = vec< real, 4 >
 A 4-dimensional vector with real elements.
 
using cvec = vec< complex<> >
 A variable size vector with complex elements.
 
using cvec2 = vec< complex<>, 2 >
 A 2-dimensional vector with complex elements.
 
using cvec3 = vec< complex<>, 3 >
 A 3-dimensional vector with complex elements.
 
using cvec4 = vec< complex<>, 4 >
 A 4-dimensional vector with complex elements.
 
template<typename Type = real>
using bicomplex = complex< complex< Type > >
 A bi-complex number.
 
template<typename Structure >
using has_complex_elements = is_complex_type< vector_element_t< Structure > >
 Type trait to check whether a container has complex elements.
 
template<typename Structure , typename T = bool>
using enable_complex = std::enable_if_t< is_complex_type< Structure >::value, T >
 Enable a function overload if the template typename is considerable a complex number. More...
 
using real = double
 A real number, defined as a floating point type. More...
 
template<typename Structure >
using vector_element_or_void_t = typename _internal::vector_element_or_void< Structure >::type
 Extract the type of a vector (or any indexable container) from its operator[], returning void if the type has no operator[].
 
template<typename Structure >
using vector_element_t = std::remove_reference_t< decltype(std::declval< Structure >()[0])>
 Extract the type of a vector (or any indexable container) from its operator[].
 
template<typename Structure >
using matrix_element_t = std::remove_reference_t< decltype(std::declval< Structure >()(0, 0))>
 Extract the type of a matrix (or any doubly indexable container) from its operator().
 
template<typename Structure >
using has_real_elements = is_real_type< vector_element_t< Structure > >
 Type trait to check whether an indexable container has real elements.
 
template<typename Structure , typename T = bool>
using enable_matrix = std::enable_if_t< is_matrix< Structure >::value, T >
 Enable a function overload if the template typename is considerable a matrix. More...
 
template<typename Structure , typename T = bool>
using enable_vector = std::enable_if_t< is_vector< Structure >::value, T >
 Enable a function overload if the template typename is considerable a vector. More...
 
template<typename Function >
using is_real_func = std::conditional_t< is_real_type< typename _internal::return_type_or_void< Function, real >::type >::value, std::true_type, std::false_type >
 Type trait to check whether the given function takes a real number as its first argument.
 
template<typename Function , typename T = bool>
using enable_real_func = typename std::enable_if_t< is_real_func< Function >::value, T >
 Enable a certain function overload if the given type is a function taking as first argument a real number.
 
template<typename Function >
using return_type_t = typename _internal::func_helper< Function >::return_type
 Extract the return type of a Callable object, such as a function pointer or lambda function.
 
using real_function = std::function< real(real)>
 Function pointer to a real function of real variable.
 
using complex_function = std::function< complex<>(complex<>)>
 Function pointer to a complex function of complex variable.
 
using stat_function = std::function< real(real, const vec< real > &)>
 Function pointer to a probability distribution function where the first argument is the variable and the second argument is a vector of the parameters of the distribution.
 
using polyn_recurr_formula = std::function< polynomial< real >(polynomial< real >, polynomial< real >, unsigned int)>
 Polynomial sequence recurrence formula type Used for computing orthogonal polynomial basis elements.
 
using pseudorandom_function = uint64_t(*)(uint64_t, std::vector< uint64_t > &)
 A function pointer which wraps a pseudorandom generator, taking as input the previous generated value (or seed) and the current state of the algorithm. More...
 
using pdf_sampling_function = real(*)(const std::vector< real > &, PRNG &)
 A p.d.f sampling function taking as input the parameters of the distribution and a pseudorandom number generator.
 

Enumerations

enum  MATH_ERRCODE {
  NO_ERROR = 0x00 , DIV_BY_ZERO = 0x01 , OUT_OF_DOMAIN = 0x02 , OUT_OF_RANGE = 0x04 ,
  IMPOSSIBLE_OPERATION = 0x08 , NO_ALGO_CONVERGENCE = 0x10 , INVALID_ARGUMENT = 0x20
}
 Math error enumeration.
 

Functions

template<typename ElementType , typename Type , typename ... Args>
void make_vec (vec< ElementType > &v, size_t index, Type last)
 
template<typename ElementType , typename Type , typename ... Args>
void make_vec (vec< ElementType > &v, size_t index, Type first, Args... elements)
 
template<typename Type , typename ... Args>
vec< Type > make_vec (Type first, Args... elements)
 Construct a dynamically allocated vector of type vec<Type> using variadic templates.
 
dual2 square (dual2 x)
 Return the square of a second order dual number.
 
dual2 cube (dual2 x)
 Return the cube of a second order dual number.
 
dual2 conjugate (dual2 x)
 Return the conjugate of a second order dual number.
 
dual2 pow (dual2 x, int n)
 Compute the n-th power of a second order dual number.
 
dual2 sqrt (dual2 x)
 Compute the square root of a second order dual number.
 
dual2 sin (dual2 x)
 Compute the sine of a second order dual number.
 
dual2 cos (dual2 x)
 Compute the cosine of a second order dual number.
 
dual2 tan (dual2 x)
 Compute the tangent of a second order dual number.
 
dual2 cot (dual2 x)
 Compute the cotangent of a second order dual number.
 
dual2 exp (dual2 x)
 Compute the exponential of a second order dual number.
 
dual2 ln (dual2 x)
 Compute the natural logarithm of a second order dual number.
 
dual2 log2 (dual2 x)
 Compute the natural logarithm of a second order dual number.
 
dual2 log10 (dual2 x)
 Compute the natural logarithm of a second order dual number.
 
dual2 abs (dual2 x)
 Compute the absolute value of a second order dual number.
 
dual2 asin (dual2 x)
 Compute the arcsine of a second order dual number.
 
dual2 acos (dual2 x)
 Compute the arcosine of a second order dual number.
 
dual2 atan (dual2 x)
 Compute the arctangent of a second order dual number.
 
dual square (dual x)
 Return the square of a dual number.
 
dual cube (dual x)
 Return the cube of a dual number.
 
dual conjugate (dual x)
 Return the conjugate of a dual number.
 
dual pow (dual x, int n)
 Compute the n-th power of a dual number.
 
dual sqrt (dual x)
 Compute the square root of a dual number.
 
dual sin (dual x)
 Compute the sine of a dual number.
 
dual cos (dual x)
 Compute the cosine of a dual number.
 
dual tan (dual x)
 Compute the tangent of a dual number.
 
dual cot (dual x)
 Compute the cotangent of a dual number.
 
dual exp (dual x)
 Compute the exponential of a dual number.
 
dual ln (dual x)
 Compute the natural logarithm of a dual number.
 
dual log2 (dual x)
 Compute the natural logarithm of a dual number.
 
dual log10 (dual x)
 Compute the natural logarithm of a dual number.
 
dual abs (dual x)
 Compute the absolute value of a dual number.
 
dual asin (dual x)
 Compute the arcsine of a dual number.
 
dual acos (dual x)
 Compute the arccosine of a dual number.
 
dual atan (dual x)
 Compute the arctangent of a dual number.
 
dual sinh (dual x)
 Compute the hyperbolic sine of a dual number.
 
dual cosh (dual x)
 Compute the hyperbolic cosine of a dual number.
 
dual tanh (dual x)
 Compute the hyperbolic tangent of a dual number.
 
template<unsigned int N>
multidual< N > square (multidual< N > x)
 Return the square of a multidual number.
 
template<unsigned int N>
multidual< N > cube (multidual< N > x)
 Return the cube of a multidual number.
 
template<unsigned int N>
multidual< N > conjugate (multidual< N > x)
 Return the conjugate of a multidual number.
 
template<unsigned int N>
multidual< N > pow (multidual< N > x, int n)
 Compute the n-th power of a multidual number.
 
template<unsigned int N>
multidual< N > sqrt (multidual< N > x)
 Compute the square root of a multidual number.
 
template<unsigned int N>
multidual< N > sin (multidual< N > x)
 Compute the sine of a multidual number.
 
template<unsigned int N>
multidual< N > cos (multidual< N > x)
 Compute the cosine of a multidual number.
 
template<unsigned int N>
multidual< N > tan (multidual< N > x)
 Compute the tangent of a multidual number.
 
template<unsigned int N>
multidual< N > cot (multidual< N > x)
 Compute the cotangent of a multidual number.
 
template<unsigned int N>
multidual< N > exp (multidual< N > x)
 Compute the exponential of a multidual number.
 
template<unsigned int N>
multidual< N > ln (multidual< N > x)
 Compute the natural logarithm of a multidual number.
 
template<unsigned int N>
multidual< N > log2 (multidual< N > x)
 Compute the natural logarithm of a multidual number.
 
template<unsigned int N>
multidual< N > log10 (multidual< N > x)
 Compute the natural logarithm of a multidual number.
 
template<unsigned int N>
multidual< N > abs (multidual< N > x)
 Compute the absolute value of a multidual number.
 
template<unsigned int N>
multidual< N > asin (multidual< N > x)
 Compute the arcsine of a multidual number.
 
template<unsigned int N>
multidual< N > acos (multidual< N > x)
 Compute the arccosine of a multidual number.
 
template<unsigned int N>
multidual< N > atan (multidual< N > x)
 Compute the arctangent of a multidual number.
 
template<unsigned int N>
multidual< N > sinh (multidual< N > x)
 Compute the hyperbolic sine of a multidual number.
 
template<unsigned int N>
multidual< N > cosh (multidual< N > x)
 Compute the hyperbolic cosine of a multidual number.
 
template<unsigned int N>
multidual< N > tanh (multidual< N > x)
 Compute the hyperbolic tangent of a multidual number.
 
template<typename Field = real>
polynomial< Field > deriv (const polynomial< Field > &p)
 Compute the exact derivative of a polynomial function. More...
 
template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real deriv_central (RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
 Approximate the first derivative of a real function using the central method. More...
 
template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real deriv_forward (RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
 Approximate the first derivative of a real function using the forward method. More...
 
template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real deriv_backward (RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
 Approximate the first derivative of a real function using the backward method. More...
 
template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real deriv_ridders2 (RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
 Approximate the first derivative of a real function using Ridder's method of second degree. More...
 
template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real deriv_ridders (RealFunction f, real x, real h=0.01, unsigned int degree=3)
 Approximate the first derivative of a real function using Ridder's method of arbitrary degree. More...
 
template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real deriv (RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
 Approximate the first derivative of a real function using the best available algorithm. More...
 
template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real deriv2 (RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
 Approximate the second derivative of a real function using the best available algorithm. More...
 
template<typename T = real>
polynomial< T > integral (const polynomial< T > &p)
 Compute the indefinite integral of a polynomial. More...
 
template<typename RealFunction >
real integral_midpoint (RealFunction f, real a, real b, unsigned int steps=CALCULUS_INTEGRAL_STEPS)
 Approximate the definite integral of an arbitrary function using the midpoint method. More...
 
template<typename RealFunction >
real integral_trapezoid (RealFunction f, real a, real b, unsigned int steps=CALCULUS_INTEGRAL_STEPS)
 Approximate the definite integral of an arbitrary function using the trapezoid method. More...
 
template<typename RealFunction >
real integral_simpson (RealFunction f, real a, real b, unsigned int steps=CALCULUS_INTEGRAL_STEPS)
 Approximate the definite integral of an arbitrary function using Simpson's method. More...
 
template<typename RealFunction >
real integral_romberg (RealFunction f, real a, real b, unsigned int iter=8)
 Approximate the definite integral of an arbitrary function using Romberg's method accurate to the given order. More...
 
template<typename RealFunction >
real integral_romberg_tol (RealFunction f, real a, real b, real tolerance=CALCULUS_INTEGRAL_TOL)
 Approximate the definite integral of an arbitrary function using Romberg's method to the given tolerance. More...
 
template<typename RealFunction >
real integral_gauss (RealFunction f, const std::vector< real > &x, const std::vector< real > &w)
 Use Gaussian quadrature using the given points and weights. More...
 
template<typename RealFunction >
real integral_gauss (RealFunction f, real *x, real *w, unsigned int n)
 Use Gaussian quadrature using the given points and weights. More...
 
template<typename RealFunction >
real integral_gauss (RealFunction f, real *x, real *w, unsigned int n, real_function Winv)
 Use Gaussian quadrature using the given points and weights. More...
 
template<typename RealFunction >
real integral_legendre (RealFunction f, real a, real b, real *x, real *w, unsigned int n)
 Use Gauss-Legendre quadrature of arbitrary degree to approximate a definite integral providing the roots of the n degree Legendre polynomial. More...
 
template<typename RealFunction >
real integral_legendre (RealFunction f, real a, real b, const std::vector< real > &x, const std::vector< real > &w)
 Use Gauss-Legendre quadrature of arbitrary degree to approximate a definite integral providing the roots of the n degree Legendre polynomial. More...
 
template<typename RealFunction >
real integral_legendre (RealFunction f, real a, real b, const std::vector< real > &x)
 Use Gauss-Legendre quadrature of arbitrary degree to approximate a definite integral providing the roots of the n degree Legendre polynomial. More...
 
template<typename RealFunction >
real integral_legendre (RealFunction f, real a, real b, unsigned int n=16)
 Use Gauss-Legendre quadrature of degree 2, 4, 8 or 16, using pre-computed values, to approximate an integral over [a, b]. More...
 
template<typename RealFunction >
real integral_laguerre (RealFunction f, const std::vector< real > &x)
 Use Gauss-Laguerre quadrature of arbitrary degree to approximate an integral over [0, +inf) providing the roots of the n degree Legendre polynomial. More...
 
template<typename RealFunction >
real integral_laguerre (RealFunction f, real a, real b, const std::vector< real > &x)
 Use Gauss-Laguerre quadrature of arbitrary degree to approximate an integral over [a, b] providing the roots of the n degree Legendre polynomial. More...
 
template<typename RealFunction >
real integral_laguerre (RealFunction f, unsigned int n=16)
 Use Gauss-Laguerre quadrature of degree 2, 4, 8 or 16, using pre-computed values, to approximate an integral over [0, +inf). More...
 
template<typename RealFunction >
real integral_hermite (RealFunction f, const std::vector< real > &x)
 Use Gauss-Hermite quadrature of arbitrary degree to approximate an integral over (-inf, +inf) providing the roots of the n degree Hermite polynomial. More...
 
template<typename RealFunction >
real integral_hermite (RealFunction f, unsigned int n=16)
 Use Gauss-Hermite quadrature of degree 2, 4, 8 or 16, using pre-computed values, to approximate an integral over (-inf, +inf). More...
 
real integral_inf_riemann (real_function f, real a, real step_sz=1, real tol=CALCULUS_INTEGRAL_TOL, unsigned int max_iter=100)
 Integrate a function from a point up to infinity by integrating it by steps, stopping execution when the variation of the integral is small enough or the number of steps reaches a maximum value.
 
template<typename RealFunction >
real integral (RealFunction f, real a, real b)
 Use the best available algorithm to approximate the definite integral of a real function. More...
 
template<typename RealFunction >
real integral (RealFunction f, real a, real b, real tol)
 Use the best available algorithm to approximate the definite integral of a real function to a given tolerance. More...
 
template<typename T >
complex< T > identity (complex< T > z)
 Complex identity. More...
 
template<typename T >
complex< T > conjugate (complex< T > z)
 Compute the conjugate of a complex number. More...
 
template<typename T >
complex< T > inverse (complex< T > z)
 Compute the conjugate of a complex number. More...
 
template<typename T >
complex< T > square (complex< T > z)
 Compute the square of a complex number. More...
 
template<typename T >
complex< T > cube (complex< T > z)
 Compute the cube of a complex number. More...
 
template<typename T >
complex< T > exp (complex< T > z)
 Compute the complex exponential. More...
 
template<typename T >
real abs (complex< T > z)
 Return the modulus of a complex number. More...
 
template<typename T >
complex< T > sin (complex< T > z)
 Computer the complex sine. More...
 
template<typename T >
complex< T > cos (complex< T > z)
 Compute the complex cosine. More...
 
template<typename T >
complex< T > tan (complex< T > z)
 Compute the complex tangent. More...
 
template<typename T >
complex< T > sqrt (complex< T > z)
 Compute the complex square root. More...
 
template<typename T >
complex< T > ln (complex< T > z)
 Compute the complex logarithm. More...
 
template<typename T >
complex< T > asin (complex< T > z)
 Compute the complex arcsine. More...
 
template<typename T >
complex< T > acos (complex< T > z)
 Compute the complex arccosine. More...
 
template<typename T >
complex< T > atan (complex< T > z)
 Compute the complex arctangent. More...
 
void mul_uint128 (uint64_t a, uint64_t b, uint64_t &c_low, uint64_t &c_high)
 Multiply two 64-bit unsigned integers and store the result in two 64-bit variables, keeping 128 bits of the result. More...
 
uint64_t mix_mum (uint64_t a, uint64_t b)
 MUM bit mixing function, computes the 128-bit product of a and b and the XOR of their high and low 64-bit parts. More...
 
template<typename UnsignedIntType >
TH_CONSTEXPR UnsignedIntType bit_rotate (UnsignedIntType x, unsigned int i)
 Bit rotation of unsigned integer types using shifts. More...
 
template<typename Vector , enable_vector< Vector > = true>
void swap_bit_reverse (Vector &x, unsigned int m)
 Swap the elements of a vector pair-wise, by exchanging elements with indices related by bit reversion (e.g. More...
 
template<typename Vector , enable_vector< Vector > = true>
auto product (const Vector &X)
 Compute the product of a set of values.
 
template<typename Vector >
auto product_sum (const Vector &X, const Vector &Y)
 Sum the products of two sets of values.
 
template<typename Vector >
auto product_sum_squares (const Vector &X, const Vector &Y)
 Sum the products of the squares of two sets of data.
 
template<typename Vector >
auto product_sum (const Vector &X, const Vector &Y, const Vector &Z)
 Sum the products of three sets of values.
 
template<typename Vector >
auto quotient_sum (const Vector &X, const Vector &Y)
 Sum the quotients of two sets of values.
 
template<typename Vector >
auto sum_squares (const Vector &X)
 Sum the squares of a set of values.
 
template<typename Vector >
real sum_compensated (const Vector &X)
 Compute the sum of a set of values using the compensated Neumaier-Kahan-Babushka summation algorithm to reduce round-off error. More...
 
template<typename Vector >
real sum_pairwise (const Vector &X, size_t begin=0, size_t end=0, size_t base_size=128)
 Compute the sum of a set of values using pairwise summation to reduce round-off error. More...
 
template<typename Vector , std::enable_if_t< has_real_elements< Vector >::value > = true>
auto sum (const Vector &X)
 Compute the sum of a vector of real values using pairwise summation to reduce round-off error. More...
 
template<typename Vector >
auto sum (const Vector &X)
 Compute the sum of a set of values. More...
 
template<typename Vector , typename Function >
Vector & apply (Function f, Vector &X)
 Apply a function to a set of values element-wise. More...
 
template<typename Vector1 , typename Vector2 = Vector1, typename Function >
Vector2 & map (Function f, const Vector1 &src, Vector2 &dest)
 Get a new vector obtained by applying the function element-wise. More...
 
template<typename Vector2 , typename Vector1 , typename Function >
Vector2 map (Function f, const Vector1 &X)
 Get a new vector obtained by applying the function element-wise. More...
 
template<typename Vector , typename Function >
Vector map (Function f, const Vector &X)
 Get a new vector obtained by applying the function element-wise. More...
 
template<typename Vector1 , typename Vector2 , typename Vector3 = Vector1>
Vector3 concatenate (const Vector1 &v1, const Vector2 &v2)
 Concatenate two datasets to form a single one.
 
template<typename Vector >
auto max (const Vector &X)
 Finds the maximum value inside a dataset.
 
template<typename Vector >
auto min (const Vector &X)
 Finds the minimum value inside a dataset.
 
template<typename Dataset >
real arithmetic_mean (const Dataset &data)
 Compute the arithmetic mean of a set of values.
 
template<typename Dataset >
real harmonic_mean (const Dataset &data)
 Compute the harmonic mean of a set of values.
 
template<typename Dataset >
real geometric_mean (const Dataset &data)
 Compute the geometric mean of a set of values as \(\sqrt[n]{\Pi_i x_i}\).
 
template<typename Dataset1 , typename Dataset2 >
real weighted_mean (const Dataset1 &data, const Dataset2 &weights)
 Compute the weighted mean of a set of values <data> and <weights> must have the same size.
 
template<typename Dataset >
real quadratic_mean (const Dataset &data)
 Compute the quadratic mean (Root Mean Square) of a set of values \(m_q = \sqrt{x1^2 + x2^2 + ...}\).
 
int th_errcode_to_errno (MATH_ERRCODE err)
 Convert a MATH_ERRCODE to errno error codes.
 
real nan ()
 Return a quiet NaN number in floating point representation.
 
template<typename T >
bool is_nan (const T &x)
 Check whether a generic variable is (equivalent to) a NaN number. More...
 
real inf ()
 Return positive infinity in floating point representation.
 
TH_CONSTEXPR real identity (real x)
 Identity.
 
template<typename Type , typename = std::enable_if<is_real_type<Type>::value>>
TH_CONSTEXPR Type conjugate (Type x)
 Complex conjugate of a real number (identity)
 
TH_CONSTEXPR real square (real x)
 Compute the square of a real number. More...
 
TH_CONSTEXPR real cube (real x)
 Compute the cube of a real number. More...
 
template<typename UnsignedIntType = uint64_t>
UnsignedIntType isqrt (UnsignedIntType n)
 Compute the integer square root of a positive integer. More...
 
template<typename UnsignedIntType = uint64_t>
UnsignedIntType icbrt (UnsignedIntType n)
 Compute the integer cubic root of a positive integer. More...
 
real sqrt (real x)
 Compute the square root of a real number. More...
 
real cbrt (real x)
 Compute the cubic root of x. More...
 
real abs (real x)
 Compute the absolute value of a real number. More...
 
int sgn (real x)
 Return the sign of x (1 if positive, -1 if negative, 0 if null) More...
 
TH_CONSTEXPR int floor (real x)
 Compute the floor of x Computes the maximum integer number that is smaller than x. More...
 
real fract (real x)
 Compute the fractional part of a real number. More...
 
real max (real x, real y)
 Return the greatest number between two real numbers. More...
 
template<typename T >
max (T x, T y)
 Compare two objects and return the greatest. More...
 
real min (real x, real y)
 Return the smallest number between two real numbers. More...
 
template<typename T >
min (T x, T y)
 Compare two objects and return the greatest. More...
 
real clamp (real x, real a, real b)
 Clamp x between a and b. More...
 
template<typename T >
clamp (T x, T a, T b)
 Clamp a value between two other values. More...
 
real log2 (real x)
 Compute the binary logarithm of a real number. More...
 
real log10 (real x)
 Compute the base-10 logarithm of x. More...
 
real ln (real x)
 Compute the natural logarithm of x. More...
 
template<typename UnsignedIntType = uint64_t>
UnsignedIntType ilog2 (UnsignedIntType x)
 Find the integer logarithm of x. More...
 
template<typename UnsignedIntType = uint64_t>
UnsignedIntType pad2 (UnsignedIntType x)
 Get the smallest power of 2 bigger than or equal to x. More...
 
template<typename T = real>
TH_CONSTEXPRpow (T x, int n)
 Compute the n-th power of x (where n is natural) More...
 
template<typename T = real>
TH_CONSTEXPRipow (T x, unsigned int n, T neutral_element=T(1))
 Compute the n-th positive power of x (where n is natural) More...
 
template<typename IntType = uint64_t>
TH_CONSTEXPR IntType fact (unsigned int n)
 Compute the factorial of n.
 
template<typename T = uint64_t>
TH_CONSTEXPRfalling_fact (T x, unsigned int n)
 Compute the falling factorial of n.
 
template<typename T = uint64_t>
TH_CONSTEXPRrising_fact (T x, unsigned int n)
 Compute the rising factorial of n.
 
template<typename IntType = unsigned long long int>
TH_CONSTEXPR IntType double_fact (unsigned int n)
 Compute the double factorial of n.
 
real exp (real x)
 Compute the real exponential. More...
 
real expm1 (real x)
 Compute the exponential of x minus 1 more accurately for really small x. More...
 
real powf (real x, real a)
 Approximate x elevated to a real exponent. More...
 
real root (real x, int n)
 Compute the n-th root of x. More...
 
real sin (real x)
 Compute the sine of a real number. More...
 
real cos (real x)
 Compute the cosine of a real number. More...
 
real tan (real x)
 Compute the tangent of x. More...
 
real cot (real x)
 Compute the cotangent of x. More...
 
real atan (real x)
 Compute the arctangent. More...
 
real asin (real x)
 Compute the arcsine. More...
 
real acos (real x)
 Compute the arccosine. More...
 
real atan2 (real y, real x)
 Compute the 2 argument arctangent. More...
 
real sinh (real x)
 Compute the hyperbolic sine. More...
 
real cosh (real x)
 Compute the hyperbolic cosine. More...
 
real tanh (real x)
 Compute the hyperbolic tangent. More...
 
real coth (real x)
 Compute the hyperbolic cotangent. More...
 
real asinh (real x)
 Compute the inverse hyperbolic sine.
 
real acosh (real x)
 Compute the inverse hyperbolic cosine.
 
real atanh (real x)
 Compute the inverse hyperbolic tangent.
 
real sigmoid (real x)
 Compute the sigmoid function. More...
 
real sinc (real x)
 Compute the normalized sinc function. More...
 
real heaviside (real x)
 Compute the heaviside function. More...
 
template<typename IntType = unsigned long long int>
TH_CONSTEXPR IntType binomial_coeff (unsigned int n, unsigned int m)
 Compute the binomial coefficient. More...
 
TH_CONSTEXPR real radians (real degrees)
 Convert degrees to radians. More...
 
TH_CONSTEXPR real degrees (real radians)
 Convert radians to degrees. More...
 
template<typename T >
TH_CONSTEXPRkronecker_delta (T i, T j)
 Kronecker delta, equals 1 if i is equal to j, 0 otherwise. More...
 
template<typename IntType = unsigned long long int>
TH_CONSTEXPR IntType catalan (unsigned int n)
 The n-th Catalan number.
 
template<typename T = real>
polynomial< T > lagrange_polynomial (const std::vector< vec< T, 2 >> &points)
 Compute the Lagrange polynomial interpolating a set of points. More...
 
template<typename VectorType = std::vector<real>>
VectorType chebyshev_nodes (real a, real b, unsigned int n)
 Compute the n Chebyshev nodes on a given interval. More...
 
polynomial< realinterpolate_grid (real_function f, real a, real b, unsigned int order)
 Compute the interpolating polynomial of a real function on an equidistant point sample. More...
 
polynomial< realinterpolate_chebyshev (real_function f, real a, real b, unsigned int order)
 Compute the interpolating polynomial of a real function using Chebyshev nodes as sampling points. More...
 
real lerp (real x1, real x2, real interp)
 Linear interpolation.
 
template<unsigned int N>
vec< real, N > lerp (const vec< real, N > &P1, const vec< real, N > &P2, real interp)
 Linear interpolation.
 
real invlerp (real x1, real x2, real value)
 Inverse linear interpolation.
 
template<unsigned int N>
vec< real, N > invlerp (const vec< real, N > &P1, const vec< real, N > &P2, real value)
 Inverse linear interpolation.
 
real remap (real iFrom, real iTo, real oFrom, real oTo, real value)
 Remap a value from one range to another.
 
template<unsigned int N>
vec< real, N > remap (const vec< real, N > &iFrom, const vec< real, N > &iTo, const vec< real, N > &oFrom, const vec< real, N > &oTo, real value)
 Remap a vector value from one range to another.
 
template<unsigned int N>
vec< real, N > nlerp (const vec< real, N > &P1, const vec< real, N > &P2, real interp)
 Normalized linear interpolation.
 
template<unsigned int N>
vec< real, N > slerp (const vec< real, N > &P1, const vec< real, N > &P2, real t)
 Spherical interpolation.
 
real smoothstep (real x1, real x2, real interp)
 Smoothstep interpolation.
 
real smootherstep (real x1, real x2, real interp)
 Smootherstep interpolation.
 
template<unsigned int N>
vec< real, N > quadratic_bezier (const vec< real, N > &P0, const vec< real, N > &P1, const vec< real, N > &P2, real t)
 Quadratic Bezier curve.
 
template<unsigned int N>
vec< real, N > cubic_bezier (const vec< real, N > &P0, const vec< real, N > &P1, const vec< real, N > &P2, vec< real, N > P3, real t)
 Cubic Bezier curve.
 
template<unsigned int N>
vec< real, N > bezier (const std::vector< vec< real, N >> &points, real t)
 Generic Bezier curve in N dimensions. More...
 
template<typename DataPoints = std::vector<vec2>>
std::vector< spline_nodecubic_splines (DataPoints p)
 Compute the cubic splines interpolation of a set of data points. More...
 
template<typename Dataset1 , typename Dataset2 >
std::vector< spline_nodecubic_splines (const Dataset1 &x, const Dataset2 &y)
 Compute the cubic splines interpolation of the sets of X and Y data points. More...
 
template<typename RealFunction >
real maximize_goldensection (RealFunction f, real a, real b)
 Approximate a function maximum using the Golden Section search algorithm. More...
 
template<typename RealFunction >
real minimize_goldensection (RealFunction f, real a, real b)
 Approximate a function minimum using the Golden Section search algorithm. More...
 
template<typename RealFunction >
real maximize_newton (RealFunction f, RealFunction Df, RealFunction D2f, real guess=0)
 Approximate a function maximum given the function and the first two derivatives using Newton-Raphson's method to find a root of the derivative. More...
 
template<typename RealFunction >
real minimize_newton (RealFunction f, RealFunction Df, RealFunction D2f, real guess=0)
 Approximate a function minimum given the function and the first two derivatives using Newton-Raphson's method to find a root of the derivative. More...
 
template<typename RealFunction >
real maximize_bisection (RealFunction f, RealFunction Df, real a, real b)
 Approximate a function maximum inside an interval given the function and its first derivative using bisection on the derivative. More...
 
template<typename RealFunction >
real minimize_bisection (RealFunction f, RealFunction Df, real a, real b)
 Approximate a function minimum inside an interval given the function and its first derivative using bisection on the derivative. More...
 
template<unsigned int N>
vec< real, N > multi_minimize_grad (multidual< N >(*f)(vec< multidual< N >, N >), vec< real, N > guess=vec< real, N >(0), real gamma=OPTIMIZATION_MINGRAD_GAMMA, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
 Find a local minimum of the given multivariate function using fixed-step gradient descent. More...
 
template<unsigned int N>
vec< real, N > multi_maximize_grad (multidual< N >(*f)(vec< multidual< N >, N >), vec< real, N > guess=vec< real, N >(0), real gamma=OPTIMIZATION_MINGRAD_GAMMA, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
 Find a local maximum of the given multivariate function using fixed-step gradient descent. More...
 
template<unsigned int N>
vec< real, N > multi_minimize_lingrad (multidual< N >(*f)(vec< multidual< N >, N >), vec< real, N > guess=vec< real, N >(0), real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
 Find a local minimum of the given multivariate function using gradient descent with linear search. More...
 
template<unsigned int N>
vec< real, N > multi_maximize_lingrad (multidual< N >(*f)(vec< multidual< N >, N >), vec< real, N > guess=vec< real, N >(0), real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
 Find a local maximum of the given multivariate function using gradient descent with linear search. More...
 
template<unsigned int N>
vec< real, N > multi_minimize (multidual< N >(*f)(vec< multidual< N >, N >), vec< real, N > guess=vec< real, N >(0), real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE)
 Use the best available algorithm to find a local minimum of the given multivariate function. More...
 
template<unsigned int N>
vec< real, N > multi_maximize (multidual< N >(*f)(vec< multidual< N >, N >), vec< real, N > guess=vec< real, N >(0), real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE)
 Use the best available algorithm to find a local maximum of the given multivariate function. More...
 
template<unsigned int N>
vec< real, N > multiroot_newton (autodiff::dvec_t< N >(*f)(autodiff::dvec_t< N >), vec< real, N > guess=vec< real, N >(0), real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
 Approximate the root of a multivariate function using Newton's method with pure Jacobian. More...
 
template<typename RealFunction >
std::vector< vec2find_root_intervals (RealFunction f, real a, real b, unsigned int steps=10)
 Find candidate intervals for root finding. More...
 
template<typename RealFunction >
real root_bisection (RealFunction f, real a, real b, real tolerance=OPTIMIZATION_TOL)
 Approximate a root of an arbitrary function using bisection inside a compact interval [a, b] where f(a) * f(b) < 0. More...
 
template<typename RealFunction >
real root_newton (RealFunction f, RealFunction Df, real guess=0)
 Approximate a root of an arbitrary function using Newton's method. More...
 
real root_newton (dual(*f)(dual), real guess=0)
 Approximate a root of an arbitrary function using Newton's method, computing the derivative using automatic differentiation. More...
 
real root_newton (const polynomial< real > &p, real guess=0)
 Approximate a root of a polynomial using Newton's method. More...
 
complex root_newton (complex<>(*f)(complex<>), complex<>(*df)(complex<>), complex<> guess=complex<>(0, 0), real tolerance=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_TOL)
 Approximate a root of an arbitrary complex function using Newton's method,. More...
 
template<typename RealFunction >
real root_halley (RealFunction f, RealFunction Df, RealFunction D2f, real guess=0)
 Approximate a root of an arbitrary function using Halley's method. More...
 
real root_halley (dual2(*f)(dual2), real guess=0)
 Approximate a root of an arbitrary function using Halley's method, leveraging automatic differentiation to compute the first and second derivatives of the function. More...
 
real root_halley (const polynomial< real > &p, real guess=0)
 Approximate a root of a polynomial using Halley's method. More...
 
template<typename RealFunction >
real root_steffensen (RealFunction f, real guess=0)
 Approximate a root of an arbitrary function using Steffensen's method. More...
 
real root_steffensen (const polynomial< real > &p, real guess=0)
 Approximate a root of a polynomial using Steffensen's method. More...
 
template<typename RealFunction >
real root_chebyshev (RealFunction f, RealFunction Df, RealFunction D2f, real guess=0)
 Approximate a root of an arbitrary function using Chebyshev's method. More...
 
real root_chebyshev (dual2(*f)(dual2), real guess=0)
 Approximate a root of an arbitrary function using Chebyshev's method, by computing the first and second derivatives using automatic differentiation. More...
 
real root_chebyshev (const polynomial< real > &p, real guess=0)
 Approximate a root of a polynomial using Chebyshev's method. More...
 
template<typename RealFunction >
std::vector< realroots (RealFunction f, real a, real b, real tolerance=OPTIMIZATION_TOL, real steps=10)
 Find the roots of a function inside a given interval. More...
 
template<typename Field >
std::vector< Field > roots (const polynomial< Field > &p, real tolerance=OPTIMIZATION_TOL, unsigned int steps=0)
 Find all the roots of a polynomial. More...
 
polynomial< realgen_polyn_recurr (polynomial< real > P0, polynomial< real > P1, polyn_recurr_formula f, unsigned int n)
 Generate a polynomial basis using a recursion formula. More...
 
polynomial< reallegendre_polyn_recurr (polynomial< real > P0, polynomial< real > P1, unsigned int l)
 Recursion formula for Legendre polynomials.
 
polynomial< reallegendre_polynomial (unsigned int n)
 Compute the nth Legendre polynomial. More...
 
real legendre_polyn_normalization (unsigned int n)
 Normalization constant for the nth Legendre polynomial.
 
std::function< real(real)> assoc_legendre_polynomial (unsigned int l, int m)
 
polynomial< realassoc_legendre_polynomial_even (unsigned int l, int m)
 
polynomial< reallaguerre_polyn_recurr (polynomial< real > L0, polynomial< real > L1, unsigned int i)
 Recursion formula for Laguerre polynomials.
 
polynomial< reallaguerre_polynomial (unsigned int n)
 Compute the nth Laguerre polynomial.
 
polynomial< realgeneral_laguerre_polyn_recurr (polynomial< real > L0, polynomial< real > L1, real alpha, unsigned int i)
 Recursion formula for Generalized Laguerre polynomials.
 
polynomial< realgeneral_laguerre_polynomial (real alpha, unsigned int n)
 Compute the nth Laguerre polynomial.
 
polynomial< realhermite_polyn_recurr (polynomial< real > H0, polynomial< real > H1, unsigned int i)
 Recursion formula for Hermite polynomials.
 
polynomial< realhermite_polynomial (unsigned int n)
 Compute the nth Hermite polynomial. More...
 
real hermite_polyn_normalization (unsigned int n)
 Normalization constant for the nth Hermite polynomial.
 
polynomial< realchebyshev_polyn_recurr (polynomial< real > T0, polynomial< real > T1, unsigned int i)
 Recursion formula for Chebyshev polynomials The formula is the same for first and second kind polynomials.
 
polynomial< realchebyshev1_polynomial (unsigned int n)
 Compute the nth Chebyshev polynomial of the first kind. More...
 
polynomial< realchebyshev2_polynomial (unsigned int n)
 Compute the nth Chebyshev polynomial of the second kind. More...
 
std::vector< reallegendre_roots (unsigned int n)
 Roots of the n-th Legendre polynomial. More...
 
std::vector< reallegendre_weights (const std::vector< real > &roots)
 Legendre weights for Gauss-Legendre quadrature of n-th order. More...
 
std::vector< reallaguerre_weights (const std::vector< real > &roots)
 Laguerre weights for Gauss-Laguerre quadrature of n-th order. More...
 
std::vector< realhermite_weights (const std::vector< real > &roots)
 Hermite weights for Gauss-Hermite quadrature of n-th order. More...
 
real integral_crude (real_function f, real a, real b, PRNG &g, unsigned int N=1000)
 Approximate an integral by using Crude Monte Carlo integration. More...
 
template<unsigned int S>
real integral_crude (real(*f)(vec< real, S >), vec< vec2, S > extremes, PRNG &g, unsigned int N=1000)
 Approximate an integral by using Crude Monte Carlo integration. More...
 
real integral_quasi_crude (real_function f, real a, real b, unsigned int N=1000)
 Approximate an integral by using Crude Quasi-Monte Carlo integration by sampling from the Weyl sequence. More...
 
template<unsigned int S>
real integral_quasi_crude (real(*f)(vec< real, S >), vec< vec2, S > extremes, unsigned int N, vec< real, S > alpha)
 Approximate an integral by using Crude Quasi-Monte Carlo integration by sampling from the Weyl sequence. More...
 
template<unsigned int S>
real integral_quasi_crude (real(*f)(vec< real, S >), vec< vec2, S > extremes, unsigned int N=1000, real alpha=0)
 Approximate an integral by using Crude Quasi-Monte Carlo integration by sampling from the Weyl sequence. More...
 
real integral_hom (real_function f, real a, real b, real c, real d, PRNG &g, unsigned int N=1000)
 Approximate an integral by using Hit-or-miss Monte Carlo integration. More...
 
real integral_hom (real_function f, real a, real b, real f_max, PRNG &g, unsigned int N=1000)
 Approximate an integral by using Hit-or-miss Monte Carlo integration. More...
 
real integral_quasi_hom (real_function f, real a, real b, real c, real d, unsigned int N=1000)
 Approximate an integral by using Hit-or-miss Quasi-Monte Carlo integration, sampling points from the Weyl bi-dimensional sequence. More...
 
real integral_quasi_hom (real_function f, real a, real b, real f_max, unsigned int N=1000)
 Approximate an integral by using Hit-or-miss Quasi-Monte Carlo integration, sampling points from the Weyl bi-dimensional sequence. More...
 
real integral_hom_2d (real(*f)(real, real), real a, real b, real c, real d, real f_max, PRNG &g, unsigned int N=1000)
 Use the Hit-or-Miss Monte Carlo method to approximate a double integral. More...
 
real integral_impsamp (real_function f, real_function g, real_function Ginv, PRNG &gen, unsigned int N=1000)
 Approximate an integral by using Crude Monte Carlo integration with importance sampling. More...
 
real integral_quasi_impsamp (real_function f, real_function g, real_function Ginv, unsigned int N=1000)
 Approximate an integral by using Crude Quasi-Monte Carlo integration with importance sampling, using the Weyl sequence. More...
 
template<typename Vector = std::vector<real>, typename Function = std::function<real(vec<real>)>>
Vector sample_mc (Function f, std::vector< pdf_sampler > &rv, unsigned int N)
 Generate a Monte Carlo sample of values of a given function of arbitrary variables following the given distributions. More...
 
template<typename Vector >
void shuffle (Vector &v, PRNG &g, unsigned int rounds=0)
 Shuffle a set by exchanging random couples of elements. More...
 
uint64_t rand_congruential (uint64_t x, uint64_t a=48271, uint64_t c=0, uint64_t m=((uint64_t) 1<< 31) - 1)
 Generate a pseudorandom number using the congruential pseudorandom number generation algorithm. More...
 
uint64_t rand_congruential (uint64_t x, std::vector< uint64_t > &state)
 Generate a pseudorandom number using the congruential pseudorandom number generation algorithm (wrapper) More...
 
uint64_t rand_xoshiro (uint64_t &a, uint64_t &b, uint64_t &c, uint64_t &d)
 Generate a pseudorandom number using the xoshiro256++ pseudorandom number generation algorithm. More...
 
uint64_t rand_xoshiro (uint64_t x, std::vector< uint64_t > &state)
 Generate a pseudorandom number using the xoshiro256++ pseudorandom number generation algorithm (wrapper) More...
 
uint64_t rand_splitmix64 (uint64_t x)
 Generate a pseudorandom number using the SplitMix64 pseudorandom number generation algorithm. More...
 
uint64_t rand_splitmix64 (uint64_t x, std::vector< uint64_t > &p)
 Generate a pseudorandom number using the SplitMix64 pseudorandom number generation algorithm. More...
 
uint64_t rand_wyrand (uint64_t &seed, uint64_t p1, uint64_t p2)
 Generate a pseudorandom number using the Wyrand pseudorandom number generation, as invented by Yi Wang. More...
 
uint64_t rand_wyrand (uint64_t x, std::vector< uint64_t > &p)
 Generate a pseudorandom number using the Wyrand pseudorandom number generation, as invented by Yi Wang (wrapper) More...
 
uint64_t rand_middlesquare (uint64_t seed, uint64_t offset=765872292751861)
 Generate a pseudorandom number using the middle-square pseudorandom number generation algorithm. More...
 
uint64_t rand_middlesquare (uint64_t x, std::vector< uint64_t > &p)
 Generate a pseudorandom number using the middle-square pseudorandom number generation algorithm (wrapper) More...
 
real qrand_weyl (unsigned int n, real alpha=INVPHI)
 Weyl quasi-random sequence. More...
 
real qrand_weyl_recurr (real prev=0, real alpha=INVPHI)
 Weyl quasi-random sequence (computed with recurrence relation) More...
 
template<unsigned int N>
vec< real, N > qrand_weyl_multi (unsigned int n, real alpha)
 Weyl quasi-random sequence in N dimensions. More...
 
vec2 qrand_weyl2 (unsigned int n, real alpha=0.7548776662466927)
 Weyl quasi-random sequence in 2 dimensions. More...
 
real rand_uniform (real a, real b, PRNG &g, uint64_t prec=STATISTICS_RAND_PREC)
 Generate a pseudorandom real number in [a, b] using a preexisting generator. More...
 
real rand_uniform (const std::vector< real > &theta, PRNG &g)
 Wrapper for rand_uniform(real, real, PRNG) More...
 
real rand_cointoss (PRNG &g)
 Coin toss random generator. More...
 
real rand_cointoss (const std::vector< real > &theta, PRNG &g)
 Wrapper for rand_cointoss(PRNG) More...
 
real rand_diceroll (unsigned int faces, PRNG &g)
 Dice roll random generator. More...
 
real rand_diceroll (const std::vector< real > &theta, PRNG &g)
 Wrapper for rand_diceroll(PRNG) More...
 
real rand_trycatch (stat_function f, const vec< real > &theta, real x1, real x2, real y1, real y2, PRNG &g, unsigned int max_iter=STATISTICS_TRYANDCATCH_ITER)
 Generate a pseudorandom value following any probability distribution function using the Try-and-Catch (rejection) algorithm. More...
 
real rand_rejectsamp (stat_function f, const vec< real > &theta, real_function p, real_function Pinv, PRNG &g, unsigned int max_tries=100)
 Generate a random number following any given distribution using rejection sampling. More...
 
real rand_gaussian_polar (real mean, real sigma, PRNG &g)
 Generate a random number following a Gaussian distribution using Marsaglia's polar method. More...
 
real rand_gaussian_boxmuller (real mean, real sigma, PRNG &g)
 Generate a random number following a Gaussian distribution using the Box-Muller method. More...
 
real rand_gaussian_clt (real mean, real sigma, PRNG &g)
 Generate a random number in a range following a Gaussian distribution by exploiting the Central Limit Theorem. More...
 
real rand_gaussian_clt (real mean, real sigma, PRNG &g, unsigned int N)
 Generate a random number in a range following a Gaussian distribution by exploiting the Central Limit Theorem. More...
 
real rand_gaussian (real mean, real sigma, PRNG &g)
 Generate a random number following a Gaussian distribution using the best available algorithm.
 
real rand_gaussian (const std::vector< real > &theta, PRNG &g)
 Wrapper for rand_gaussian(real, real, PRNG) More...
 
real rand_exponential (real lambda, PRNG &g)
 Generate a random number following an exponential distribution using the quantile (inverse) function method.
 
real rand_exponential (const std::vector< real > &theta, PRNG &g)
 Wrapper for rand_exponential(real, PRNG) More...
 
real rand_rayleigh (real sigma, PRNG &g)
 Generate a random number following a Rayleigh distribution using the quantile (inverse) function method.
 
real rand_rayleigh (const std::vector< real > &theta, PRNG &g)
 Wrapper for rand_rayleigh(real, PRNG) More...
 
real rand_cauchy (real mu, real alpha, PRNG &g)
 Generate a random number following a Cauchy distribution using the quantile (inverse) function method.
 
real rand_cauchy (const std::vector< real > &theta, PRNG &g)
 Wrapper for rand_cauchy(real, real, PRNG) More...
 
real rand_laplace (real mu, real b, PRNG &g)
 Generate a random number following a Laplace distribution using the quantile (inverse) function method.
 
real rand_laplace (const std::vector< real > &theta, PRNG &g)
 Generate a random number following a Laplace distribution using the quantile (inverse) function method.
 
real rand_pareto (real x_m, real alpha, PRNG &g)
 Generate a random number following a Pareto distribution using the quantile (inverse) function method.
 
real rand_pareto (const std::vector< real > &theta, PRNG &g)
 Wrapper for rand_pareto(real, real, PRNG) More...
 
real metropolis (real_function pdf, pdf_sampler &g, real x0, PRNG &rnd, unsigned int depth=STATISTICS_METROPOLIS_DEPTH)
 Metropolis algorithm for distribution sampling using a symmetric proposal distribution. More...
 
real metropolis (real_function pdf, pdf_sampler &g, real x0, unsigned int depth=STATISTICS_METROPOLIS_DEPTH)
 Metropolis algorithm for distribution sampling using a symmetric proposal distribution. More...
 
real max (const histogram &h)
 Compute the maximum value of the elements of a histogram.
 
real min (const histogram &h)
 Compute the minimum value of the elements of a histogram.
 
template<typename Type >
void print (const Type &curr)
 Print the given argument to standard output.
 
template<typename Type , typename ... Args>
void print (const Type &curr, Args... args)
 Print the given arguments to standard output separated by a space.
 
template<typename Type >
void fprint (std::ostream &out, const Type &last)
 Print the given argument to a stream.
 
template<typename Type , typename ... Args>
void fprint (std::ostream &out, const Type &curr, Args... args)
 Print the given arguments to standard output separated by a space.
 
void println ()
 Print a newline to standard output.
 
template<typename Type >
void println (const Type &curr)
 Print the given argument to standard output followed by a newline.
 
template<typename Type , typename ... Args>
void println (const Type &curr, Args... args)
 Print the given arguments to standard output separated by a space and followed by a newline.
 
template<typename Type >
void fprintln (std::ostream &out, const Type &curr)
 Print the given argument to an output stream followed by a newline.
 
template<typename Type , typename ... Args>
void fprintln (std::ostream &out, const Type &curr, Args... args)
 Print the given arguments to an output stream separated by a space and followed by a newline.
 
template<typename Type = real>
vec< Type > readln (std::istream &in, const std::string &terminator, std::function< Type(std::string)> parse)
 Insert a data set of the given type from a stream, reading line by line until a line is equal to the terminator and parsing each line using the given function, returning the list of values.
 
vec< realreadln (std::istream &in, const std::string &terminator="")
 Insert a data set of the given type from a stream, reading line by line until a line is equal to the terminator and parsing each line as a real value, returning the list of values.
 
vec< realreadln (const std::string &terminator="")
 Insert a data set of the given type from standard input, reading line by line until a line is equal to the terminator and parsing each line as a real value, returning the list of values.
 

Variables

constexpr real MACH_EPSILON = std::numeric_limits<real>::epsilon()
 Machine epsilon for the real type.
 
constexpr real PHI = 1.6180339887498948482045868
 The Phi (Golden Section) mathematical constant.
 
constexpr real INVPHI = 0.6180339887498948482045868
 The inverse of the Golden Section mathematical constant.
 
constexpr real PI = 3.141592653589793238462643
 The Pi mathematical constant.
 
constexpr real PI2 = 1.57079632679489655799898
 Half of Pi.
 
constexpr real PI4 = PI / 4.0
 A quarter of Pi.
 
constexpr real PIDOUBLE = PI * 2
 Pi multiplied by 2.
 
constexpr real TAU = PI * 2
 The Tau mathematical constant (Pi times 2)
 
constexpr real INVPI = 1.0 / PI
 The inverse of Pi.
 
constexpr real SQRTPI = 1.7724538509055159927
 The square root of Pi.
 
constexpr real E = 2.718281828459045235360287
 The Euler mathematical constant (e)
 
constexpr real LOG2E = 1.44269504088896338700465094
 The binary logarithm of e.
 
constexpr real LOG210 = 3.32192809488736218170856773213
 The binary logarithm of 10.
 
constexpr real LOG10E = 0.434294481903
 The base-10 logarithm of e.
 
constexpr real LN2 = 0.69314718056
 The natural logarithm of 2.
 
constexpr real LN10 = 2.30258509299
 The natural logarithm of 10.
 
constexpr real DEG2RAD = 0.017453292519943295474371680598
 The scalar conversion factor from degrees to radians.
 
constexpr real RAD2DEG = 57.2957795130823228646477218717
 The scalar conversion factor from radians to degrees.
 
constexpr real SQRT2 = 1.4142135623730950488
 The square root of 2.
 
constexpr real INVSQR2 = 0.7071067811865475
 The inverse of the square root of 2.
 
constexpr real SQRT3 = 1.732050807568877
 The square root of 3.
 
constexpr real ALGEBRA_ELEMENT_TOL = THEORETICA_ALGEBRA_ELEMENT_TOL
 Tolerance for the elements of matrices.
 
constexpr real ALGEBRA_EIGEN_TOL = THEORETICA_ALGEBRA_EIGEN_TOL
 Tolerance for eigensolvers.
 
constexpr real ALGEBRA_EIGEN_ITER = THEORETICA_ALGEBRA_EIGEN_ITER
 Maximum number of iterations for eigensolvers.
 
constexpr int CORE_TAYLOR_ORDER = THEORETICA_CORE_TAYLOR_ORDER
 Order of Taylor series approximations.
 
constexpr int CALCULUS_INTEGRAL_STEPS = THEORETICA_CALCULUS_INTEGRAL_STEPS
 Default number of steps for integral approximation.
 
constexpr real CALCULUS_INTEGRAL_TOL = THEORETICA_CALCULUS_INTEGRAL_TOL
 
constexpr real OPTIMIZATION_TOL = THEORETICA_OPTIMIZATION_TOL
 Approximation tolerance for root finding.
 
constexpr unsigned int OPTIMIZATION_BISECTION_ITER = THEORETICA_OPTIMIZATION_BISECTION_ITER
 Maximum number of iterations for the bisection algorithm.
 
constexpr unsigned int OPTIMIZATION_GOLDENSECTION_ITER = THEORETICA_OPTIMIZATION_GOLDENSECTION_ITER
 Maximum number of iterations for the golden section search algorithm.
 
constexpr unsigned int OPTIMIZATION_HALLEY_ITER = THEORETICA_OPTIMIZATION_HALLEY_ITER
 Maximum number of iterations for Halley's method.
 
constexpr unsigned int OPTIMIZATION_NEWTON_ITER = THEORETICA_OPTIMIZATION_NEWTON_ITER
 Maximum number of iterations for the Newton-Raphson algorithm.
 
constexpr unsigned int OPTIMIZATION_STEFFENSEN_ITER = THEORETICA_OPTIMIZATION_STEFFENSEN_ITER
 Maximum number of iterations for the Steffensen algorithm.
 
constexpr unsigned int OPTIMIZATION_CHEBYSHEV_ITER = THEORETICA_OPTIMIZATION_CHEBYSHEV_ITER
 Maximum number of iterations for the Chebyshev algorithm.
 
constexpr unsigned int STATISTICS_TRYANDCATCH_ITER = THEORETICA_STATISTICS_TRYANDCATCH_ITER
 Maximum number of failed iterations for the Try-and-Catch algorithm.
 
constexpr real CALCULUS_DERIV_STEP = THEORETICA_CALCULUS_DERIV_STEP
 Default variation for derivative approximation.
 
constexpr real OPTIMIZATION_MINGRAD_GAMMA = THEORETICA_OPTIMIZATION_MINGRAD_GAMMA
 Default step size for gradient descent minimization.
 
constexpr real OPTIMIZATION_MINGRAD_TOLERANCE = THEORETICA_OPTIMIZATION_MINGRAD_TOLERANCE
 Default tolerance for gradient descent minimization.
 
constexpr unsigned int OPTIMIZATION_MINGRAD_ITER = THEORETICA_OPTIMIZATION_MINGRAD_ITER
 Maximum number of iterations for gradient descent minimization.
 
constexpr uint64_t STATISTICS_RAND_PREC = THEORETICA_STATISTICS_RAND_PREC
 Default precision for random number generation using rand_uniform()
 
constexpr unsigned int STATISTICS_METROPOLIS_DEPTH = THEORETICA_STATISTICS_METROPOLIS_DEPTH
 Default depth of the Metropolis algorithm.
 

Detailed Description

Main namespace of the library which contains all functions and objects.

Typedef Documentation

◆ enable_complex

template<typename Structure , typename T = bool>
using theoretica::enable_complex = typedef std::enable_if_t<is_complex_type<Structure>::value, T>

Enable a function overload if the template typename is considerable a complex number.

The std::enable_if structure is used, with type T which defaults to bool.

◆ enable_matrix

template<typename Structure , typename T = bool>
using theoretica::enable_matrix = typedef std::enable_if_t<is_matrix<Structure>::value, T>

Enable a function overload if the template typename is considerable a matrix.

The std::enable_if structure is used, with type T which defaults to bool.

◆ enable_vector

template<typename Structure , typename T = bool>
using theoretica::enable_vector = typedef std::enable_if_t<is_vector<Structure>::value, T>

Enable a function overload if the template typename is considerable a vector.

The std::enable_if structure is used, with type T which defaults to bool.

◆ pseudorandom_function

using theoretica::pseudorandom_function = typedef uint64_t (*)(uint64_t, std::vector<uint64_t>&)

A function pointer which wraps a pseudorandom generator, taking as input the previous generated value (or seed) and the current state of the algorithm.

Such functions may be passed to the PRNG class to simplify the usage of generators.

◆ real

using theoretica::real = typedef double

A real number, defined as a floating point type.

The underlying type is determined by the defined macros: By default, real will be defined as the double type. If THEORETICA_FLOAT_PREC is defined, real will be defined as a float, if THEORETICA_LONG_DOUBLE_PREC is defined, real will be defined as a long double

Note
The THEORETICA_ARBITRARY_PREC option is currently unsupported

Function Documentation

◆ abs() [1/2]

template<typename T >
real theoretica::abs ( complex< T >  z)
inline

Return the modulus of a complex number.

Parameters
zA complex number

◆ abs() [2/2]

real theoretica::abs ( real  x)
inline

Compute the absolute value of a real number.

Parameters
xA real number
Returns
The absolute value of x

On x86 architectures, if THEORETICA_X86 is defined, the fabs instruction will be used.

◆ acos() [1/2]

template<typename T >
complex<T> theoretica::acos ( complex< T >  z)
inline

Compute the complex arccosine.

Parameters
zA complex number

◆ acos() [2/2]

real theoretica::acos ( real  x)
inline

Compute the arccosine.

Parameters
xA real number
Returns
The arccosine of x

Domain: [-1, 1]. The identities \(acos(x) = atan(\frac{sqrt{1 - x^2}}{x})\) and \(acos(x) = atan(\frac{\sqrt{1 - x^2}}{x}) + \pi\) are used.

◆ apply()

template<typename Vector , typename Function >
Vector& theoretica::apply ( Function  f,
Vector &  X 
)
inline

Apply a function to a set of values element-wise.

Note
Unlike functions in the parallel namespace, this routine is not parallelized.
Parameters
fThe function to apply
XThe vector to modify the elements of
Returns
A reference to the modified vector

◆ asin() [1/2]

template<typename T >
complex<T> theoretica::asin ( complex< T >  z)
inline

Compute the complex arcsine.

Parameters
zA complex number

◆ asin() [2/2]

real theoretica::asin ( real  x)
inline

Compute the arcsine.

Parameters
xA real number
Returns
The arcsine of x

Domain: [-1, 1]. The identity \(asin(x) = atan(\frac{x}{\sqrt{1 - x^2}})\) is used.

◆ atan() [1/2]

template<typename T >
complex<T> theoretica::atan ( complex< T >  z)
inline

Compute the complex arctangent.

Parameters
zA complex number

◆ atan() [2/2]

real theoretica::atan ( real  x)
inline

Compute the arctangent.

Parameters
xAn angle in radians
Returns
The arctangent of x

A degree 9 interpolating polynomial through Chebyshev nodes is used to approximate \(atan(x)\). Domain reduction to [-1, 1] is performed.

◆ atan2()

real theoretica::atan2 ( real  y,
real  x 
)
inline

Compute the 2 argument arctangent.

Parameters
yThe y coordinate in Cartesian space
xThe x coordinate in Cartesian space
Returns
The counterclockwise angle between the vector described by x and y and the x axis.

Computed using identities on atan(x).

◆ bezier()

template<unsigned int N>
vec<real, N> theoretica::bezier ( const std::vector< vec< real, N >> &  points,
real  t 
)
inline

Generic Bezier curve in N dimensions.

Parameters
pointsThe control points
tThe curve parameter between 0 and 1

The generic Bezier curve is computed by successive linear interpolations. For cubic and quadratic Bezier curves the related functions should be preferred.

See also
quadratic_bezier
cubic_bezier

◆ binomial_coeff()

template<typename IntType = unsigned long long int>
TH_CONSTEXPR IntType theoretica::binomial_coeff ( unsigned int  n,
unsigned int  m 
)
inline

Compute the binomial coefficient.

Parameters
nA natural number
mA natural number smaller than n
Returns
The binomial coefficient computed on (n, m) as \(\frac{n!}{m!(n - m)!}\)

◆ bit_rotate()

template<typename UnsignedIntType >
TH_CONSTEXPR UnsignedIntType theoretica::bit_rotate ( UnsignedIntType  x,
unsigned int  i 
)
inline

Bit rotation of unsigned integer types using shifts.

Parameters
xThe unsigned integer to rotate the bits of
iThe index of the rotated bits
Returns
The unsigned integer with the given bits rotated

◆ cbrt()

real theoretica::cbrt ( real  x)
inline

Compute the cubic root of x.

Parameters
xA real number
Returns
The cubic root of x

Domain: [-inf, +inf]
The Newton-Raphson algorithm, optimized for the cubic root and limited by the THEORETICA_OPTIMIZATION_NEWTON_ITER macro constant, is used. Domain reduction to [0, 1] is applied to ensure convergence of the algorithm.

◆ chebyshev1_polynomial()

polynomial<real> theoretica::chebyshev1_polynomial ( unsigned int  n)
inline

Compute the nth Chebyshev polynomial of the first kind.

Note
The result is not normalized

◆ chebyshev2_polynomial()

polynomial<real> theoretica::chebyshev2_polynomial ( unsigned int  n)
inline

Compute the nth Chebyshev polynomial of the second kind.

Note
The result is not normalized

◆ chebyshev_nodes()

template<typename VectorType = std::vector<real>>
VectorType theoretica::chebyshev_nodes ( real  a,
real  b,
unsigned int  n 
)
inline

Compute the n Chebyshev nodes on a given interval.

Parameters
aThe lower bound of the interval
bThe upper bound of the interval
nThe number of points to evaluate

◆ clamp() [1/2]

real theoretica::clamp ( real  x,
real  a,
real  b 
)
inline

Clamp x between a and b.

Parameters
xThe real number to clamp
aThe lower bound
bThe upper bound
Returns
Returns x if x is between a and b, a if x is less than a, b if x is bigger than b

◆ clamp() [2/2]

template<typename T >
T theoretica::clamp ( x,
a,
b 
)
inline

Clamp a value between two other values.

Parameters
xThe value to clamp
aThe lower bound
bThe upper bound
Returns
Returns x if x is between a and b, a if x is less than a, b if x is bigger than b

The templated T type must have comparison operators.

◆ conjugate()

template<typename T >
complex<T> theoretica::conjugate ( complex< T >  z)
inline

Compute the conjugate of a complex number.

Parameters
zA complex number

◆ cos() [1/2]

template<typename T >
complex<T> theoretica::cos ( complex< T >  z)
inline

Compute the complex cosine.

Parameters
zA complex number

◆ cos() [2/2]

real theoretica::cos ( real  x)
inline

Compute the cosine of a real number.

Parameters
xAn angle in radians
Returns
The cosine of x

On x86 architectures, if THEORETICA_X86 is defined, the fcos instruction will be used.

◆ cosh()

real theoretica::cosh ( real  x)
inline

Compute the hyperbolic cosine.

Parameters
xA real number
Returns
The hyperbolic cosine of x

\(cosh = \frac{e^x + e^{-x}}{2}\)

◆ cot()

real theoretica::cot ( real  x)
inline

Compute the cotangent of x.

Parameters
xAn angle in radians
Returns
The cotangent of x

On x86 architectures, if THEORETICA_X86 is defined, the fsincos instruction will be used if supported by the compiler.

◆ coth()

real theoretica::coth ( real  x)
inline

Compute the hyperbolic cotangent.

Parameters
xA real number
Returns
The hyperbolic cotangent of x

◆ cube() [1/2]

template<typename T >
complex<T> theoretica::cube ( complex< T >  z)
inline

Compute the cube of a complex number.

Parameters
zA complex number

◆ cube() [2/2]

TH_CONSTEXPR real theoretica::cube ( real  x)
inline

Compute the cube of a real number.

Parameters
xA real number
Returns
The cube of x

Domain: [-inf, +inf]

◆ cubic_splines() [1/2]

template<typename Dataset1 , typename Dataset2 >
std::vector<spline_node> theoretica::cubic_splines ( const Dataset1 &  x,
const Dataset2 &  y 
)
inline

Compute the cubic splines interpolation of the sets of X and Y data points.

The X values should be strictly increasing.

◆ cubic_splines() [2/2]

template<typename DataPoints = std::vector<vec2>>
std::vector<spline_node> theoretica::cubic_splines ( DataPoints  p)
inline

Compute the cubic splines interpolation of a set of data points.

The X values should be strictly increasing.

◆ degrees()

TH_CONSTEXPR real theoretica::degrees ( real  radians)
inline

Convert radians to degrees.

Parameters
radiansAn angle in radians
Returns
The converted angle in degrees

The RAD2DEG scalar factor is used.

◆ deriv() [1/2]

template<typename Field = real>
polynomial<Field> theoretica::deriv ( const polynomial< Field > &  p)
inline

Compute the exact derivative of a polynomial function.

Parameters
pThe polynomial to differentiate
Returns
The derivative polynomial

◆ deriv() [2/2]

template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real theoretica::deriv ( RealFunction  f,
real  x,
real  h = CALCULUS_DERIV_STEP 
)
inline

Approximate the first derivative of a real function using the best available algorithm.

Parameters
fThe function to approximate the derivative of
xThe real value to approximate at
hThe step size to use in the finite differences method
Returns
The approximated value of the derivative

◆ deriv2()

template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real theoretica::deriv2 ( RealFunction  f,
real  x,
real  h = CALCULUS_DERIV_STEP 
)
inline

Approximate the second derivative of a real function using the best available algorithm.

Parameters
fThe function to approximate the second derivative of
xThe real value to approximate at
hThe step size to use in the finite differences method
Returns
The approximated value of the second derivative

◆ deriv_backward()

template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real theoretica::deriv_backward ( RealFunction  f,
real  x,
real  h = CALCULUS_DERIV_STEP 
)
inline

Approximate the first derivative of a real function using the backward method.

Parameters
fThe function to approximate the derivative of
xThe real value to approximate at
hThe step size to use in the finite differences method
Returns
The approximated value of the derivative

◆ deriv_central()

template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real theoretica::deriv_central ( RealFunction  f,
real  x,
real  h = CALCULUS_DERIV_STEP 
)
inline

Approximate the first derivative of a real function using the central method.

Parameters
fThe function to approximate the derivative of
xThe real value to approximate at
hThe step size to use in the finite differences method
Returns
The approximated value of the derivative

◆ deriv_forward()

template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real theoretica::deriv_forward ( RealFunction  f,
real  x,
real  h = CALCULUS_DERIV_STEP 
)
inline

Approximate the first derivative of a real function using the forward method.

Parameters
fThe function to approximate the derivative of
xThe real value to approximate at
hThe step size to use in the finite differences method
Returns
The approximated value of the derivative

◆ deriv_ridders()

template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real theoretica::deriv_ridders ( RealFunction  f,
real  x,
real  h = 0.01,
unsigned int  degree = 3 
)
inline

Approximate the first derivative of a real function using Ridder's method of arbitrary degree.

Parameters
fThe function to approximate the derivative of
xThe real value to approximate at
degreeThe degree of the algorithm
hThe step size to use in the finite differences method
Returns
The approximated value of the derivative

◆ deriv_ridders2()

template<typename RealFunction = std::function<real(real)>, enable_real_func< RealFunction > = true>
real theoretica::deriv_ridders2 ( RealFunction  f,
real  x,
real  h = CALCULUS_DERIV_STEP 
)
inline

Approximate the first derivative of a real function using Ridder's method of second degree.

Parameters
fThe function to approximate the derivative of
xThe real value to approximate at
hThe step size to use in the finite differences method
Returns
The approximated value of the derivative

◆ exp() [1/2]

template<typename T >
complex<T> theoretica::exp ( complex< T >  z)
inline

Compute the complex exponential.

Parameters
zA complex number

◆ exp() [2/2]

real theoretica::exp ( real  x)
inline

Compute the real exponential.

Parameters
xA real number
Returns
The exponential of x

The exponential is computed as \(e^{floor(x)} \cdot e^{fract(x)}\), where \(e^{floor(x)} = pow(e, floor(x))\) and \(e^{fract(x)}\) is approximated using Taylor series on [0, 0.25]

◆ expm1()

real theoretica::expm1 ( real  x)
inline

Compute the exponential of x minus 1 more accurately for really small x.

For |x| > 0.001, th::exp is used.

Parameters
xA real number.
Returns
The exponential of x minus 1.

◆ find_root_intervals()

template<typename RealFunction >
std::vector<vec2> theoretica::find_root_intervals ( RealFunction  f,
real  a,
real  b,
unsigned int  steps = 10 
)
inline

Find candidate intervals for root finding.

Parameters
fA function of real variable
aThe lower extreme of the region of interest
bThe upper extreme of the region of interest
stepsThe number of subintervals to check (optional)

◆ floor()

TH_CONSTEXPR int theoretica::floor ( real  x)
inline

Compute the floor of x Computes the maximum integer number that is smaller than x.

Parameters
xA real number
Returns
The floor of x

e.g. floor(1.6) = 1 e.g. floor(-0.3) = -1

◆ fract()

real theoretica::fract ( real  x)
inline

Compute the fractional part of a real number.

Parameters
xA real number
Returns
The fractional part of x

e.g. fract(2.5) = 0.5 e.g. fract(-0.2) = 0.2

◆ gen_polyn_recurr()

polynomial<real> theoretica::gen_polyn_recurr ( polynomial< real P0,
polynomial< real P1,
polyn_recurr_formula  f,
unsigned int  n 
)
inline

Generate a polynomial basis using a recursion formula.

Parameters
P0First polynomial of the sequence
P1Second polynomial of the sequence
fRecursion formula
nDegree of the final polynomial
Returns
The resulting polynomial

◆ heaviside()

real theoretica::heaviside ( real  x)
inline

Compute the heaviside function.

Parameters
xA real number
Returns
The heaviside function for x, equal to 1 if x > 0, 0 if x < 0 and 1/2 if x = 0

◆ hermite_polynomial()

polynomial<real> theoretica::hermite_polynomial ( unsigned int  n)
inline

Compute the nth Hermite polynomial.

Note
The result is not normalized

◆ hermite_weights()

std::vector<real> theoretica::hermite_weights ( const std::vector< real > &  roots)
inline

Hermite weights for Gauss-Hermite quadrature of n-th order.

Parameters
rootsThe n roots of the n-th Hermite polynomial
Returns
The Hermite weights for Gauss-Hermite quadrature

◆ icbrt()

template<typename UnsignedIntType = uint64_t>
UnsignedIntType theoretica::icbrt ( UnsignedIntType  n)
inline

Compute the integer cubic root of a positive integer.

Parameters
nA positive integer number
Returns
The rounded down cubic root of n A binary search algorithm is used.

◆ identity()

template<typename T >
complex<T> theoretica::identity ( complex< T >  z)
inline

Complex identity.

Parameters
zA complex number

◆ ilog2()

template<typename UnsignedIntType = uint64_t>
UnsignedIntType theoretica::ilog2 ( UnsignedIntType  x)
inline

Find the integer logarithm of x.

Defined as the biggest n so that 2^n is smaller than x.

Parameters
xAn integer value
Returns
The integer logarithm of x

◆ integral() [1/3]

template<typename T = real>
polynomial<T> theoretica::integral ( const polynomial< T > &  p)
inline

Compute the indefinite integral of a polynomial.

Parameters
pThe polynomial to integrate
Returns
The indefinite polynomial integral

◆ integral() [2/3]

template<typename RealFunction >
real theoretica::integral ( RealFunction  f,
real  a,
real  b 
)
inline

Use the best available algorithm to approximate the definite integral of a real function.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
Returns
An approximation of the integral of f

◆ integral() [3/3]

template<typename RealFunction >
real theoretica::integral ( RealFunction  f,
real  a,
real  b,
real  tol 
)
inline

Use the best available algorithm to approximate the definite integral of a real function to a given tolerance.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
tolTolerance
Returns
An approximation of the integral of f

◆ integral_crude() [1/2]

template<unsigned int S>
real theoretica::integral_crude ( real(*)(vec< real, S >)  f,
vec< vec2, S >  extremes,
PRNG g,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Crude Monte Carlo integration.

Parameters
fThe function to integrate
extremesA vector of the extremes of integration
gAn already initialized PRNG
NThe number of points to generate

◆ integral_crude() [2/2]

real theoretica::integral_crude ( real_function  f,
real  a,
real  b,
PRNG g,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Crude Monte Carlo integration.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
gAn already initialized PRNG
NThe number of points to generate

◆ integral_gauss() [1/3]

template<typename RealFunction >
real theoretica::integral_gauss ( RealFunction  f,
const std::vector< real > &  x,
const std::vector< real > &  w 
)
inline

Use Gaussian quadrature using the given points and weights.

Parameters
fThe function to integrate
xThe points of evaluation
wThe weights of the linear combination

◆ integral_gauss() [2/3]

template<typename RealFunction >
real theoretica::integral_gauss ( RealFunction  f,
real x,
real w,
unsigned int  n 
)
inline

Use Gaussian quadrature using the given points and weights.

Parameters
fThe function to integrate
xThe points of evaluation
wThe weights of the linear combination
nThe number of points used

◆ integral_gauss() [3/3]

template<typename RealFunction >
real theoretica::integral_gauss ( RealFunction  f,
real x,
real w,
unsigned int  n,
real_function  Winv 
)
inline

Use Gaussian quadrature using the given points and weights.

Parameters
fThe function to integrate
xThe points of evaluation
wThe weights of the linear combination
nThe number of points used
WinvThe inverse of the weight function

◆ integral_hermite() [1/2]

template<typename RealFunction >
real theoretica::integral_hermite ( RealFunction  f,
const std::vector< real > &  x 
)
inline

Use Gauss-Hermite quadrature of arbitrary degree to approximate an integral over (-inf, +inf) providing the roots of the n degree Hermite polynomial.

Parameters
fThe function to integrate
xThe roots of the n degree Hermite polynomial
Returns
The Gauss-Hermite quadrature of the given function

◆ integral_hermite() [2/2]

template<typename RealFunction >
real theoretica::integral_hermite ( RealFunction  f,
unsigned int  n = 16 
)
inline

Use Gauss-Hermite quadrature of degree 2, 4, 8 or 16, using pre-computed values, to approximate an integral over (-inf, +inf).

Parameters
fThe function to integrate
nThe order of the polynomial (available values are 2, 4, 8 or 16).
Returns
The Gauss-Hermite quadrature of the given function

◆ integral_hom() [1/2]

real theoretica::integral_hom ( real_function  f,
real  a,
real  b,
real  c,
real  d,
PRNG g,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Hit-or-miss Monte Carlo integration.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
cThe function minimum in the domain [a, b]
dThe function maximum in the domain [a, b]
gAn already initialized PRNG
NThe number of points to generate

◆ integral_hom() [2/2]

real theoretica::integral_hom ( real_function  f,
real  a,
real  b,
real  f_max,
PRNG g,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Hit-or-miss Monte Carlo integration.

Note
This implementation considers only the portion of the function over zero (useful for distributions for example), if you need to consider all of the values of the function in the domain of integration, use the other implementation of integral_hom
Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
f_maxThe function maximum in the [a, b] interval
gAn already initialized PRNG
NThe number of points to generate

◆ integral_hom_2d()

real theoretica::integral_hom_2d ( real(*)(real, real f,
real  a,
real  b,
real  c,
real  d,
real  f_max,
PRNG g,
unsigned int  N = 1000 
)
inline

Use the Hit-or-Miss Monte Carlo method to approximate a double integral.

Parameters
fThe multivariate function to integrate
aThe lower extreme of integration on x
bThe upper extreme of integration on x
cThe lower extreme of integration on y
dThe upper extreme of integration on y
f_maxThe function maximum in the [a, b]x[c, d] interval
gAn already initialized PRNG
NThe number of points to generate

◆ integral_impsamp()

real theoretica::integral_impsamp ( real_function  f,
real_function  g,
real_function  Ginv,
PRNG gen,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Crude Monte Carlo integration with importance sampling.

Parameters
fThe function to integrate
gThe importance function (normalized)
GinvThe inverse of the primitive of g, with domain [0, 1]
genAn already initialized PRNG
NThe number of points to generate

◆ integral_laguerre() [1/3]

template<typename RealFunction >
real theoretica::integral_laguerre ( RealFunction  f,
const std::vector< real > &  x 
)
inline

Use Gauss-Laguerre quadrature of arbitrary degree to approximate an integral over [0, +inf) providing the roots of the n degree Legendre polynomial.

Parameters
fThe function to integrate
xThe roots of the n degree Laguerre polynomial
Returns
The Gauss-Laguerre quadrature of the given function

◆ integral_laguerre() [2/3]

template<typename RealFunction >
real theoretica::integral_laguerre ( RealFunction  f,
real  a,
real  b,
const std::vector< real > &  x 
)
inline

Use Gauss-Laguerre quadrature of arbitrary degree to approximate an integral over [a, b] providing the roots of the n degree Legendre polynomial.

Parameters
fThe function to integrate
xThe roots of the n degree Laguerre polynomial
aThe lower extreme of integration
bThe upper extreme of integration
Returns
The Gauss-Laguerre quadrature of the given function

◆ integral_laguerre() [3/3]

template<typename RealFunction >
real theoretica::integral_laguerre ( RealFunction  f,
unsigned int  n = 16 
)
inline

Use Gauss-Laguerre quadrature of degree 2, 4, 8 or 16, using pre-computed values, to approximate an integral over [0, +inf).

Parameters
fThe function to integrate
nThe order of the polynomial (available values are 2, 4, 8 or 16).
Returns
The Gauss-Legendre quadrature of the given function

◆ integral_legendre() [1/4]

template<typename RealFunction >
real theoretica::integral_legendre ( RealFunction  f,
real  a,
real  b,
const std::vector< real > &  x 
)
inline

Use Gauss-Legendre quadrature of arbitrary degree to approximate a definite integral providing the roots of the n degree Legendre polynomial.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
xThe roots of the n degree Legendre polynomial
Returns
The Gauss-Legendre quadrature of the given function

◆ integral_legendre() [2/4]

template<typename RealFunction >
real theoretica::integral_legendre ( RealFunction  f,
real  a,
real  b,
const std::vector< real > &  x,
const std::vector< real > &  w 
)
inline

Use Gauss-Legendre quadrature of arbitrary degree to approximate a definite integral providing the roots of the n degree Legendre polynomial.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
xThe roots of the n degree Legendre polynomial
wThe weights computed for the n-th order quadrature
Returns
The Gauss-Legendre quadrature of the given function

◆ integral_legendre() [3/4]

template<typename RealFunction >
real theoretica::integral_legendre ( RealFunction  f,
real  a,
real  b,
real x,
real w,
unsigned int  n 
)
inline

Use Gauss-Legendre quadrature of arbitrary degree to approximate a definite integral providing the roots of the n degree Legendre polynomial.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
xThe roots of the n degree Legendre polynomial
wThe weights computed for the n-th order quadrature
Returns
The Gauss-Legendre quadrature of the given function

◆ integral_legendre() [4/4]

template<typename RealFunction >
real theoretica::integral_legendre ( RealFunction  f,
real  a,
real  b,
unsigned int  n = 16 
)
inline

Use Gauss-Legendre quadrature of degree 2, 4, 8 or 16, using pre-computed values, to approximate an integral over [a, b].

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
nThe order of the polynomial (available values are 2, 4, 8, 16 or 32).
Returns
The Gauss-Legendre quadrature of the given function

◆ integral_midpoint()

template<typename RealFunction >
real theoretica::integral_midpoint ( RealFunction  f,
real  a,
real  b,
unsigned int  steps = CALCULUS_INTEGRAL_STEPS 
)
inline

Approximate the definite integral of an arbitrary function using the midpoint method.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
stepsThe number of steps
Returns
An approximation of the integral of f

◆ integral_quasi_crude() [1/3]

template<unsigned int S>
real theoretica::integral_quasi_crude ( real(*)(vec< real, S >)  f,
vec< vec2, S >  extremes,
unsigned int  N,
vec< real, S >  alpha 
)
inline

Approximate an integral by using Crude Quasi-Monte Carlo integration by sampling from the Weyl sequence.

Parameters
fThe function to integrate
extremesA vector of the extremes of integration
alphaA vector of the irrational numbers to use for the Weyl sequence
NThe number of points to generate

◆ integral_quasi_crude() [2/3]

template<unsigned int S>
real theoretica::integral_quasi_crude ( real(*)(vec< real, S >)  f,
vec< vec2, S >  extremes,
unsigned int  N = 1000,
real  alpha = 0 
)
inline

Approximate an integral by using Crude Quasi-Monte Carlo integration by sampling from the Weyl sequence.

Parameters
fThe function to integrate
extremesA vector of the extremes of integration
alphaAn irrational number
NThe number of points to generate

◆ integral_quasi_crude() [3/3]

real theoretica::integral_quasi_crude ( real_function  f,
real  a,
real  b,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Crude Quasi-Monte Carlo integration by sampling from the Weyl sequence.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
NThe number of points to generate

◆ integral_quasi_hom() [1/2]

real theoretica::integral_quasi_hom ( real_function  f,
real  a,
real  b,
real  c,
real  d,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Hit-or-miss Quasi-Monte Carlo integration, sampling points from the Weyl bi-dimensional sequence.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
f_maxThe function maximum in the [a, b] interval
NThe number of points to generate

◆ integral_quasi_hom() [2/2]

real theoretica::integral_quasi_hom ( real_function  f,
real  a,
real  b,
real  f_max,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Hit-or-miss Quasi-Monte Carlo integration, sampling points from the Weyl bi-dimensional sequence.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
f_maxThe function maximum in the [a, b] interval
NThe number of points to generate

◆ integral_quasi_impsamp()

real theoretica::integral_quasi_impsamp ( real_function  f,
real_function  g,
real_function  Ginv,
unsigned int  N = 1000 
)
inline

Approximate an integral by using Crude Quasi-Monte Carlo integration with importance sampling, using the Weyl sequence.

Parameters
fThe function to integrate
gThe importance function (normalized)
GinvThe inverse of the primitive of g, with domain [0, 1]
genAn already initialized PRNG
NThe number of points to generate

◆ integral_romberg()

template<typename RealFunction >
real theoretica::integral_romberg ( RealFunction  f,
real  a,
real  b,
unsigned int  iter = 8 
)
inline

Approximate the definite integral of an arbitrary function using Romberg's method accurate to the given order.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
orderThe order of accuracy
Returns
An approximation of the integral of f

◆ integral_romberg_tol()

template<typename RealFunction >
real theoretica::integral_romberg_tol ( RealFunction  f,
real  a,
real  b,
real  tolerance = CALCULUS_INTEGRAL_TOL 
)
inline

Approximate the definite integral of an arbitrary function using Romberg's method to the given tolerance.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
toleranceConvergence tolerance for the algorithm
Returns
An approximation of the integral of f

◆ integral_simpson()

template<typename RealFunction >
real theoretica::integral_simpson ( RealFunction  f,
real  a,
real  b,
unsigned int  steps = CALCULUS_INTEGRAL_STEPS 
)
inline

Approximate the definite integral of an arbitrary function using Simpson's method.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
stepsThe number of steps
Returns
An approximation of the integral of f

◆ integral_trapezoid()

template<typename RealFunction >
real theoretica::integral_trapezoid ( RealFunction  f,
real  a,
real  b,
unsigned int  steps = CALCULUS_INTEGRAL_STEPS 
)
inline

Approximate the definite integral of an arbitrary function using the trapezoid method.

Parameters
fThe function to integrate
aThe lower extreme of integration
bThe upper extreme of integration
stepsThe number of steps
Returns
An approximation of the integral of f

◆ interpolate_chebyshev()

polynomial<real> theoretica::interpolate_chebyshev ( real_function  f,
real  a,
real  b,
unsigned int  order 
)
inline

Compute the interpolating polynomial of a real function using Chebyshev nodes as sampling points.

Parameters
fThe function to interpolate
aLower bound of the interval
bUpper bound of the interval
orderOrder of the resulting polynomial
Returns
A polynomial of (n - 1) degree interpolating the function through the Chebyshev nodes.
See also
chebyshev_nodes
lagrange_polynomial

◆ interpolate_grid()

polynomial<real> theoretica::interpolate_grid ( real_function  f,
real  a,
real  b,
unsigned int  order 
)
inline

Compute the interpolating polynomial of a real function on an equidistant point sample.

Parameters
fThe function to interpolate
aLower bound of the interval
bUpper bound of the interval
orderOrder of the resulting polynomial
Returns
A polynomial of (n - 1) degree interpolating the function

◆ inverse()

template<typename T >
complex<T> theoretica::inverse ( complex< T >  z)
inline

Compute the conjugate of a complex number.

Parameters
zA complex number

◆ ipow()

template<typename T = real>
TH_CONSTEXPR T theoretica::ipow ( x,
unsigned int  n,
neutral_element = T(1) 
)
inline

Compute the n-th positive power of x (where n is natural)

Parameters
xAny element of a multiplicative algebra
nThe integer exponent
neutral_elementThe neutral element of the given type T
Returns
x to the power n
Note
This function should be preferred when computing the non-negative power of objects which are not strictly numbers but have a multiplication operation.

◆ is_nan()

template<typename T >
bool theoretica::is_nan ( const T &  x)
inline

Check whether a generic variable is (equivalent to) a NaN number.

NaN numbers are the only variables which do not compare equal to themselves in floating point operations. This is valid for real types but also for any mathematical structure, as NaNs are used to report failure inside the library.

Parameters
xThe mathematical structure to test for being a NaN or NaN-equivalent structure.

◆ isqrt()

template<typename UnsignedIntType = uint64_t>
UnsignedIntType theoretica::isqrt ( UnsignedIntType  n)
inline

Compute the integer square root of a positive integer.

Parameters
nA positive integer number
Returns
The rounded down square root of n A binary search algorithm is used.

◆ kronecker_delta()

template<typename T >
TH_CONSTEXPR T theoretica::kronecker_delta ( i,
j 
)
inline

Kronecker delta, equals 1 if i is equal to j, 0 otherwise.

Parameters
iThe first value to compare
jThe second value to compare
Returns
1 if i is equal to j, 0 otherwise

◆ lagrange_polynomial()

template<typename T = real>
polynomial<T> theoretica::lagrange_polynomial ( const std::vector< vec< T, 2 >> &  points)
inline

Compute the Lagrange polynomial interpolating a set of points.

Parameters
pointsThe set of n points to interpolate
Returns
A polynomial of (n - 1) degree interpolating the points

◆ laguerre_weights()

std::vector<real> theoretica::laguerre_weights ( const std::vector< real > &  roots)
inline

Laguerre weights for Gauss-Laguerre quadrature of n-th order.

Parameters
rootsThe n roots of the n-th Laguerre polynomial
Returns
The Laguerre weights for Gauss-Laguerre quadrature

◆ legendre_polynomial()

polynomial<real> theoretica::legendre_polynomial ( unsigned int  n)
inline

Compute the nth Legendre polynomial.

Note
The result is not normalized

◆ legendre_roots()

std::vector<real> theoretica::legendre_roots ( unsigned int  n)
inline

Roots of the n-th Legendre polynomial.

Parameters
nThe degree of the polynomial
Returns
A list of the n roots of the Legendre polynomial

◆ legendre_weights()

std::vector<real> theoretica::legendre_weights ( const std::vector< real > &  roots)
inline

Legendre weights for Gauss-Legendre quadrature of n-th order.

Parameters
rootsThe n roots of the n-th Legendre polynomial
Returns
The Legendre weights for Gauss-Legendre quadrature

◆ ln() [1/2]

template<typename T >
complex<T> theoretica::ln ( complex< T >  z)
inline

Compute the complex logarithm.

Parameters
zA complex number

◆ ln() [2/2]

real theoretica::ln ( real  x)
inline

Compute the natural logarithm of x.

Parameters
xA real number bigger than 0
Returns
The natural logarithm of x

Domain: (0, +inf]
On x86 architectures, if THEORETICA_X86 is defined, the fyl2x instruction will be used.

◆ log10()

real theoretica::log10 ( real  x)
inline

Compute the base-10 logarithm of x.

Parameters
xA real number bigger than 0
Returns
The base-10 logarithm of x

Domain: (0, +inf]
On x86 architectures, if THEORETICA_X86 is defined, the fyl2x instruction will be used.

◆ log2()

real theoretica::log2 ( real  x)
inline

Compute the binary logarithm of a real number.

Parameters
xA real number bigger than 0
Returns
The binary logarithm of x

Domain: (0, +inf]
On x86 architectures, if THEORETICA_X86 is defined, the fyl2x instruction will be used.

◆ map() [1/3]

template<typename Vector , typename Function >
Vector theoretica::map ( Function  f,
const Vector &  X 
)
inline

Get a new vector obtained by applying the function element-wise.

Parameters
fThe function to apply
XThe input vector
Returns
The modified output vector

◆ map() [2/3]

template<typename Vector1 , typename Vector2 = Vector1, typename Function >
Vector2& theoretica::map ( Function  f,
const Vector1 &  src,
Vector2 &  dest 
)
inline

Get a new vector obtained by applying the function element-wise.

Parameters
fThe function to apply
srcThe input vector
destThe output vector
Returns
A reference to the modified output vector

◆ map() [3/3]

template<typename Vector2 , typename Vector1 , typename Function >
Vector2 theoretica::map ( Function  f,
const Vector1 &  X 
)
inline

Get a new vector obtained by applying the function element-wise.

Parameters
fThe function to apply
XThe input vector
Returns
The modified output vector

◆ max() [1/2]

real theoretica::max ( real  x,
real  y 
)
inline

Return the greatest number between two real numbers.

Parameters
xA real number
yA real number
Returns
The greatest number between x and y

If THEORETICA_BRANCHLESS is defined, a branchless implementation will be used

◆ max() [2/2]

template<typename T >
T theoretica::max ( x,
y 
)
inline

Compare two objects and return the greatest.

Parameters
xThe first object to compare
yThe second object to compare
Returns
The greatest between the objects

The templated T type must have comparison operators.

◆ maximize_bisection()

template<typename RealFunction >
real theoretica::maximize_bisection ( RealFunction  f,
RealFunction  Df,
real  a,
real  b 
)
inline

Approximate a function maximum inside an interval given the function and its first derivative using bisection on the derivative.

Parameters
fThe function to search a local minimum of.
DfThe derivative of the function.
aThe lower extreme of the search interval.
bThe upper extreme of the search interval.
Returns
The coordinate of the local maximum.

◆ maximize_goldensection()

template<typename RealFunction >
real theoretica::maximize_goldensection ( RealFunction  f,
real  a,
real  b 
)
inline

Approximate a function maximum using the Golden Section search algorithm.

Parameters
fThe function to search a local maximum of.
aThe lower extreme of the search interval.
bThe upper extreme of the search interval.
Returns
The coordinate of the local maximum.

◆ maximize_newton()

template<typename RealFunction >
real theoretica::maximize_newton ( RealFunction  f,
RealFunction  Df,
RealFunction  D2f,
real  guess = 0 
)
inline

Approximate a function maximum given the function and the first two derivatives using Newton-Raphson's method to find a root of the derivative.

Parameters
fThe function to search a local maximum of.
DfThe derivative of the function.
D2fThe second derivative of the function.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the local maximum.

◆ metropolis() [1/2]

real theoretica::metropolis ( real_function  pdf,
pdf_sampler g,
real  x0,
PRNG rnd,
unsigned int  depth = STATISTICS_METROPOLIS_DEPTH 
)
inline

Metropolis algorithm for distribution sampling using a symmetric proposal distribution.

Parameters
pdfThe target distribution
gA pdf_sampler already initialized to sample from the proposal distribution
rndAn already initialized PRNG
depthThe number of iterations of the algorithm (defaults to STATISTICS_METROPOLIS_DEPTH)

◆ metropolis() [2/2]

real theoretica::metropolis ( real_function  pdf,
pdf_sampler g,
real  x0,
unsigned int  depth = STATISTICS_METROPOLIS_DEPTH 
)
inline

Metropolis algorithm for distribution sampling using a symmetric proposal distribution.

This function uses the same PRNG as the proposal distribution sampler to generate uniform samples.

Parameters
pdfThe target distribution
gA pdf_sampler already initialized to sample from the proposal distribution
depthThe number of iterations of the algorithm (defaults to STATISTICS_METROPOLIS_DEPTH)

◆ min() [1/2]

real theoretica::min ( real  x,
real  y 
)
inline

Return the smallest number between two real numbers.

Parameters
xA real number
yA real number
Returns
The smallest number between x and y

If THEORETICA_BRANCHLESS is defined, a branchless implementation will be used

◆ min() [2/2]

template<typename T >
T theoretica::min ( x,
y 
)
inline

Compare two objects and return the greatest.

Parameters
xThe first object to compare
yThe second object to compare
Returns
The smallest between the objects

The templated T type must have comparison operators.

◆ minimize_bisection()

template<typename RealFunction >
real theoretica::minimize_bisection ( RealFunction  f,
RealFunction  Df,
real  a,
real  b 
)
inline

Approximate a function minimum inside an interval given the function and its first derivative using bisection on the derivative.

Parameters
fThe function to search a local minimum of.
DfThe derivative of the function.
aThe lower extreme of the search interval.
bThe upper extreme of the search interval.
Returns
The coordinate of the local minimum.

◆ minimize_goldensection()

template<typename RealFunction >
real theoretica::minimize_goldensection ( RealFunction  f,
real  a,
real  b 
)
inline

Approximate a function minimum using the Golden Section search algorithm.

Parameters
fThe function to search a local minimum of.
aThe lower extreme of the search interval.
bThe upper extreme of the search interval.
Returns
The coordinate of the local minimum.

◆ minimize_newton()

template<typename RealFunction >
real theoretica::minimize_newton ( RealFunction  f,
RealFunction  Df,
RealFunction  D2f,
real  guess = 0 
)
inline

Approximate a function minimum given the function and the first two derivatives using Newton-Raphson's method to find a root of the derivative.

Parameters
fThe function to search a local minimum of.
DfThe derivative of the function.
D2fThe second derivative of the function.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the local minimum.

◆ mix_mum()

uint64_t theoretica::mix_mum ( uint64_t  a,
uint64_t  b 
)
inline

MUM bit mixing function, computes the 128-bit product of a and b and the XOR of their high and low 64-bit parts.

Parameters
aThe first operand
bThe second operand
Returns
The XOR of the high and low bits of the 128-bit product of a and b.

◆ mul_uint128()

void theoretica::mul_uint128 ( uint64_t  a,
uint64_t  b,
uint64_t &  c_low,
uint64_t &  c_high 
)
inline

Multiply two 64-bit unsigned integers and store the result in two 64-bit variables, keeping 128 bits of the result.

Parameters
aThe first number to multiply
bThe second number to multiply
c_lowThe variable to store the lowest 64 bits of the result.
c_highThe variable to store the highest 64 bits of the result.

◆ multi_maximize()

template<unsigned int N>
vec<real, N> theoretica::multi_maximize ( multidual< N >(*)(vec< multidual< N >, N >)  f,
vec< real, N >  guess = vec<real, N>(0),
real  tolerance = OPTIMIZATION_MINGRAD_TOLERANCE 
)
inline

Use the best available algorithm to find a local maximum of the given multivariate function.

Parameters
fThe function to multi_maximize
guessThe initial guess
toleranceThe minimum magnitude of the gradient to stop the algorithm at, defaults to OPTIMIZATION_MINGRAD_TOLERANCE.
Returns
The coordinates of the local maximum, NaN if the algorithm did not converge.

◆ multi_maximize_grad()

template<unsigned int N>
vec<real, N> theoretica::multi_maximize_grad ( multidual< N >(*)(vec< multidual< N >, N >)  f,
vec< real, N >  guess = vec<real, N>(0),
real  gamma = OPTIMIZATION_MINGRAD_GAMMA,
real  tolerance = OPTIMIZATION_MINGRAD_TOLERANCE,
unsigned int  max_iter = OPTIMIZATION_MINGRAD_ITER 
)
inline

Find a local maximum of the given multivariate function using fixed-step gradient descent.

Parameters
fThe function to multi_maximize
guessThe initial guess, defaults to the origin
gammaThe fixed step size, defaults to OPTIMIZATION_MINGRAD_GAMMA
toleranceThe maximum magnitude of the gradient to stop the algorithm at, defaults to OPTIMIZATION_MINGRAD_TOLERANCE.
max_iterThe maximum number of iterations to perform before stopping execution of the routine.
Returns
The coordinates of the local maximum, NaN if the algorithm did not converge.

◆ multi_maximize_lingrad()

template<unsigned int N>
vec<real, N> theoretica::multi_maximize_lingrad ( multidual< N >(*)(vec< multidual< N >, N >)  f,
vec< real, N >  guess = vec<real, N>(0),
real  tolerance = OPTIMIZATION_MINGRAD_TOLERANCE,
unsigned int  max_iter = OPTIMIZATION_MINGRAD_ITER 
)
inline

Find a local maximum of the given multivariate function using gradient descent with linear search.

Parameters
fThe function to multi_maximize
guessThe initial guess, defaults to the origin
toleranceThe minimum magnitude of the gradient to stop the algorithm at, defaults to OPTIMIZATION_MINGRAD_TOLERANCE.
max_iterThe maximum number of iterations to perform before stopping execution of the routine.
Returns
The coordinates of the local maximum, NaN if the algorithm did not converge.

◆ multi_minimize()

template<unsigned int N>
vec<real, N> theoretica::multi_minimize ( multidual< N >(*)(vec< multidual< N >, N >)  f,
vec< real, N >  guess = vec<real, N>(0),
real  tolerance = OPTIMIZATION_MINGRAD_TOLERANCE 
)
inline

Use the best available algorithm to find a local minimum of the given multivariate function.

Parameters
fThe function to multi_minimize
guessThe initial guess
toleranceThe minimum magnitude of the gradient to stop the algorithm at, defaults to OPTIMIZATION_MINGRAD_TOLERANCE.
Returns
The coordinates of the local minimum, NaN if the algorithm did not converge.

◆ multi_minimize_grad()

template<unsigned int N>
vec<real, N> theoretica::multi_minimize_grad ( multidual< N >(*)(vec< multidual< N >, N >)  f,
vec< real, N >  guess = vec<real, N>(0),
real  gamma = OPTIMIZATION_MINGRAD_GAMMA,
real  tolerance = OPTIMIZATION_MINGRAD_TOLERANCE,
unsigned int  max_iter = OPTIMIZATION_MINGRAD_ITER 
)
inline

Find a local minimum of the given multivariate function using fixed-step gradient descent.

Parameters
fThe function to multi_minimize
guessThe initial guess, defaults to the origin
gammaThe fixed step size, defaults to OPTIMIZATION_MINGRAD_GAMMA
toleranceThe maximum magnitude of the gradient to stop the algorithm at, defaults to OPTIMIZATION_MINGRAD_TOLERANCE.
max_iterThe maximum number of iterations to perform before stopping execution of the routine.
Returns
The coordinates of the local minimum, NaN if the algorithm did not converge.

◆ multi_minimize_lingrad()

template<unsigned int N>
vec<real, N> theoretica::multi_minimize_lingrad ( multidual< N >(*)(vec< multidual< N >, N >)  f,
vec< real, N >  guess = vec<real, N>(0),
real  tolerance = OPTIMIZATION_MINGRAD_TOLERANCE,
unsigned int  max_iter = OPTIMIZATION_MINGRAD_ITER 
)
inline

Find a local minimum of the given multivariate function using gradient descent with linear search.

Parameters
fThe function to multi_minimize
guessThe initial guess, defaults to the origin
toleranceThe maximum magnitude of the gradient to stop the algorithm at, defaults to OPTIMIZATION_MINGRAD_TOLERANCE.
max_iterThe maximum number of iterations to perform before stopping execution of the routine.
Returns
The coordinates of the local minimum, NaN if the algorithm did not converge.

◆ multiroot_newton()

template<unsigned int N>
vec<real, N> theoretica::multiroot_newton ( autodiff::dvec_t< N >(*)(autodiff::dvec_t< N >)  f,
vec< real, N >  guess = vec<real, N>(0),
real  tolerance = OPTIMIZATION_MINGRAD_TOLERANCE,
unsigned int  max_iter = OPTIMIZATION_MINGRAD_ITER 
)
inline

Approximate the root of a multivariate function using Newton's method with pure Jacobian.

Parameters
fThe function to find the root of
guessThe first guess (defaults to the origin)
toleranceThe tolerance over the final result (defaults to OPTIMIZATION_MINGRAD_TOLERANCE)
max_iterThe maximum number of iterations before stopping the algorithm
Returns
The computed vector at which f is approximately zero

◆ pad2()

template<typename UnsignedIntType = uint64_t>
UnsignedIntType theoretica::pad2 ( UnsignedIntType  x)
inline

Get the smallest power of 2 bigger than or equal to x.

This function is useful to add padding to vectors and matrices to apply recursive algorithms such as the FFT.

Parameters
xAn integer number
Returns
The smallest power of 2 bigger or equal to x

◆ pow()

template<typename T = real>
TH_CONSTEXPR T theoretica::pow ( x,
int  n 
)
inline

Compute the n-th power of x (where n is natural)

Parameters
xAny element of a multiplicative algebra
nThe integer exponent
Returns
x to the power n

◆ powf()

real theoretica::powf ( real  x,
real  a 
)
inline

Approximate x elevated to a real exponent.

Parameters
xA real number
aA real exponent

Approximated as \(e^{a ln(|x|) sgn(x)}\)

◆ qrand_weyl()

real theoretica::qrand_weyl ( unsigned int  n,
real  alpha = INVPHI 
)
inline

Weyl quasi-random sequence.

Parameters
nThe index of the element in the sequence
alphaThe base of the sequence, defaults to the inverse of the Golden Section

The Weyl sequence is defined as \(x_n = \{n \alpha\}\), where \(\{ \}\) is the fractional part.

Note
The alpha argument should be an irrational number.

◆ qrand_weyl2()

vec2 theoretica::qrand_weyl2 ( unsigned int  n,
real  alpha = 0.7548776662466927 
)
inline

Weyl quasi-random sequence in 2 dimensions.

Parameters
nThe index of the element in the sequence
alphaThe base of the sequence
Note
The alpha argument should be an irrational number.

◆ qrand_weyl_multi()

template<unsigned int N>
vec<real, N> theoretica::qrand_weyl_multi ( unsigned int  n,
real  alpha 
)
inline

Weyl quasi-random sequence in N dimensions.

Parameters
nThe index of the element in the sequence
alphaThe base of the sequence
Note
The alpha argument should be an irrational number.

◆ qrand_weyl_recurr()

real theoretica::qrand_weyl_recurr ( real  prev = 0,
real  alpha = INVPHI 
)
inline

Weyl quasi-random sequence (computed with recurrence relation)

Parameters
prevThe previously computed value
alphaThe base of the sequence, defaults to the inverse of the Golden Section

If no arguments are provided or prev is zero, the function computes the first element of the Weyl sequence associated to the parameter alpha.

See also
qrand_weyl

◆ radians()

TH_CONSTEXPR real theoretica::radians ( real  degrees)
inline

Convert degrees to radians.

Parameters
degreesAn angle in degrees
Returns
The converted angle in radians

The DEG2RAD scalar factor is used.

◆ rand_cauchy()

real theoretica::rand_cauchy ( const std::vector< real > &  theta,
PRNG g 
)
inline

Wrapper for rand_cauchy(real, real, PRNG)

Parameters
thetaThe parameters of the distribution
gAn already initialized PRNG

◆ rand_cointoss() [1/2]

real theoretica::rand_cointoss ( const std::vector< real > &  theta,
PRNG g 
)
inline

Wrapper for rand_cointoss(PRNG)

Parameters
thetaThe parameters of the distribution
gAn already initialized PRNG

◆ rand_cointoss() [2/2]

real theoretica::rand_cointoss ( PRNG g)
inline

Coin toss random generator.

Extracts 1 or -1 with equal probability.

Parameters
gAn already initialized PRNG

◆ rand_congruential() [1/2]

uint64_t theoretica::rand_congruential ( uint64_t  x,
std::vector< uint64_t > &  state 
)
inline

Generate a pseudorandom number using the congruential pseudorandom number generation algorithm (wrapper)

Parameters
xThe current recurrence value of the algorithm ( \(x_n\))
stateA vector containing the state of the algorithm (a, c, m in this order)
Returns
The next generated pseudorandom number
See also
rand_congruential

◆ rand_congruential() [2/2]

uint64_t theoretica::rand_congruential ( uint64_t  x,
uint64_t  a = 48271,
uint64_t  c = 0,
uint64_t  m = ((uint64_t) 1 << 31) - 1 
)
inline

Generate a pseudorandom number using the congruential pseudorandom number generation algorithm.

Parameters
xThe current recurrence value of the algorithm (x_n)
aThe multiplier term
cThe increment term
mThe modulus term
Returns
The next generated pseudorandom number

The congruential generator is defined by the recurrence formula \(x_{n+1} = (a x_n + c) mod m\)
If no parameters are passed, the generator defaults to a = 48271, c = 0, m = (1 << 31) - 1.

◆ rand_diceroll() [1/2]

real theoretica::rand_diceroll ( const std::vector< real > &  theta,
PRNG g 
)
inline

Wrapper for rand_diceroll(PRNG)

Parameters
thetaThe parameters of the distribution
gAn already initialized PRNG

◆ rand_diceroll() [2/2]

real theoretica::rand_diceroll ( unsigned int  faces,
PRNG g 
)
inline

Dice roll random generator.

Extracts a random natural number in [1, faces] with equal probability.

Parameters
facesThe number of faces of the dice

◆ rand_exponential()

real theoretica::rand_exponential ( const std::vector< real > &  theta,
PRNG g 
)
inline

Wrapper for rand_exponential(real, PRNG)

Parameters
thetaThe parameters of the distribution
gAn already initialized PRNG

◆ rand_gaussian()

real theoretica::rand_gaussian ( const std::vector< real > &  theta,
PRNG g 
)
inline

Wrapper for rand_gaussian(real, real, PRNG)

Parameters
thetaThe parameters of the distribution
gAn already initialized PRNG

◆ rand_gaussian_boxmuller()

real theoretica::rand_gaussian_boxmuller ( real  mean,
real  sigma,
PRNG g 
)
inline

Generate a random number following a Gaussian distribution using the Box-Muller method.

Note
This function may not be thread-safe as it uses static variables to keep track of spare generated values.

◆ rand_gaussian_clt() [1/2]

real theoretica::rand_gaussian_clt ( real  mean,
real  sigma,
PRNG g 
)
inline

Generate a random number in a range following a Gaussian distribution by exploiting the Central Limit Theorem.

Parameters
meanThe mean of the target distribution
sigmaThe sigma of the target distribution
gAn already initialized PRNG

Exactly 12 real numbers in a range are generated and the mean is computed to get a single real number following (asymptotically) a Gaussian distribution.

◆ rand_gaussian_clt() [2/2]

real theoretica::rand_gaussian_clt ( real  mean,
real  sigma,
PRNG g,
unsigned int  N 
)
inline

Generate a random number in a range following a Gaussian distribution by exploiting the Central Limit Theorem.

Parameters
meanThe mean of the target distribution
sigmaThe sigma of the target distribution
gAn already initialized PRNG
NThe number of random numbers to generate

Many real numbers in a range are generated and the mean is computed to get a single real number following (asymptotically) a Gaussian distribution.

Note
This function uses a square root (th::sqrt) to rescale the output for variable N, the constant N implementation has better performance.

◆ rand_gaussian_polar()

real theoretica::rand_gaussian_polar ( real  mean,
real  sigma,
PRNG g 
)
inline

Generate a random number following a Gaussian distribution using Marsaglia's polar method.

Note
This function may not be thread-safe as it uses static variables to keep track of spare generated values.

◆ rand_middlesquare() [1/2]

uint64_t theoretica::rand_middlesquare ( uint64_t  seed,
uint64_t  offset = 765872292751861 
)
inline

Generate a pseudorandom number using the middle-square pseudorandom number generation algorithm.

Parameters
seedThe (changing) seed of the algorithm
Returns
A 64-bit pseudorandom number

An offset is added to the 64-bit seed and the result is squared, taking the middle 64-bit of the 128-bit result.

◆ rand_middlesquare() [2/2]

uint64_t theoretica::rand_middlesquare ( uint64_t  x,
std::vector< uint64_t > &  p 
)
inline

Generate a pseudorandom number using the middle-square pseudorandom number generation algorithm (wrapper)

Parameters
xThe seed of the algorithm
pAlgorithm parameters, p[0] is the offset
Returns
A 64-bit pseudorandom number

◆ rand_pareto()

real theoretica::rand_pareto ( const std::vector< real > &  theta,
PRNG g 
)
inline

Wrapper for rand_pareto(real, real, PRNG)

Parameters
thetaThe parameters of the distribution
gAn already initialized PRNG

◆ rand_rayleigh()

real theoretica::rand_rayleigh ( const std::vector< real > &  theta,
PRNG g 
)
inline

Wrapper for rand_rayleigh(real, PRNG)

Parameters
thetaThe parameters of the distribution
gAn already initialized PRNG

◆ rand_rejectsamp()

real theoretica::rand_rejectsamp ( stat_function  f,
const vec< real > &  theta,
real_function  p,
real_function  Pinv,
PRNG g,
unsigned int  max_tries = 100 
)
inline

Generate a random number following any given distribution using rejection sampling.

Parameters
fTarget distribution
thetaThe parameters of the target distribution
pProposal distribution
PinvInverse cumulative function of the proposal distribution
gAn already initialized PRNG
max_triesMaximum number of tries before stopping execution.

◆ rand_splitmix64() [1/2]

uint64_t theoretica::rand_splitmix64 ( uint64_t  x)
inline

Generate a pseudorandom number using the SplitMix64 pseudorandom number generation algorithm.

Parameters
xThe 64-bit state of the algorithm
Returns
A 64-bit pseudorandom number

Adapted from the reference implementation by Sebastiano Vigna.

◆ rand_splitmix64() [2/2]

uint64_t theoretica::rand_splitmix64 ( uint64_t  x,
std::vector< uint64_t > &  p 
)
inline

Generate a pseudorandom number using the SplitMix64 pseudorandom number generation algorithm.

Parameters
xThe 64-bit state of the algorithm
pDummy variable (needed for function signature)
Returns
A 64-bit pseudorandom number

◆ rand_trycatch()

real theoretica::rand_trycatch ( stat_function  f,
const vec< real > &  theta,
real  x1,
real  x2,
real  y1,
real  y2,
PRNG g,
unsigned int  max_iter = STATISTICS_TRYANDCATCH_ITER 
)
inline

Generate a pseudorandom value following any probability distribution function using the Try-and-Catch (rejection) algorithm.

Parameters
fA probability distribution function
thetaThe parameters of the pdf
x1The left extreme of the rectangle
x2The right extreme of the rectangle
y1The lower extreme of the rectangle
y2The upper extreme of the rectangle
gAn already initialized PRNG to use for number generation
max_iterThe maximum number of failed generations before stopping execution (defaults to STATISTICS_TRYANDCATCH_ITER)
Returns
A real number following the given pdf

Random real numbers are generated inside a rectangle defined by x1, x2, y1 and y2 following a uniform distribution. Only numbers below the pdf are returned.

◆ rand_uniform() [1/2]

real theoretica::rand_uniform ( const std::vector< real > &  theta,
PRNG g 
)
inline

Wrapper for rand_uniform(real, real, PRNG)

Parameters
thetaThe parameters of the distribution
gAn already initialized PRNG

◆ rand_uniform() [2/2]

real theoretica::rand_uniform ( real  a,
real  b,
PRNG g,
uint64_t  prec = STATISTICS_RAND_PREC 
)
inline

Generate a pseudorandom real number in [a, b] using a preexisting generator.

Parameters
aThe lower extreme of the interval
bThe higher extreme of the interval
gAn already initialized pseudorandom number generator
precPrecision parameters for the normalization, defaults to STATISTICS_RAND_PREC.

The algorithm generates a random integer number, computes its modulus and divides it by prec: \(x = \frac{(n mod p)}{2^p}\), where n is the random integer and p is the prec parameter

◆ rand_wyrand() [1/2]

uint64_t theoretica::rand_wyrand ( uint64_t &  seed,
uint64_t  p1,
uint64_t  p2 
)
inline

Generate a pseudorandom number using the Wyrand pseudorandom number generation, as invented by Yi Wang.

Parameters
seedThe (changing) seed of the algorithm
p0Additive constant (ideally a large prime number)
p1Mask for the algorithm
Returns
A 64-bit pseudorandom number

◆ rand_wyrand() [2/2]

uint64_t theoretica::rand_wyrand ( uint64_t  x,
std::vector< uint64_t > &  p 
)
inline

Generate a pseudorandom number using the Wyrand pseudorandom number generation, as invented by Yi Wang (wrapper)

Parameters
xDummy variable
pAlgorithm parameters
Returns
A 64-bit pseudorandom number

p[0] is the initial seed, p[1] a large prime number and p[2] is the bit mask.

◆ rand_xoshiro() [1/2]

uint64_t theoretica::rand_xoshiro ( uint64_t &  a,
uint64_t &  b,
uint64_t &  c,
uint64_t &  d 
)
inline

Generate a pseudorandom number using the xoshiro256++ pseudorandom number generation algorithm.

Parameters
xDummy parameter (needed for function signature)
stateThe four 64-bit integer state of the algorithm which will be updated during the iteration.
Returns
A 64-bit pseudorandom number

Adapted from the reference implementation by Sebastiano Vigna.

◆ rand_xoshiro() [2/2]

uint64_t theoretica::rand_xoshiro ( uint64_t  x,
std::vector< uint64_t > &  state 
)
inline

Generate a pseudorandom number using the xoshiro256++ pseudorandom number generation algorithm (wrapper)

Parameters
xDummy parameter (needed for function signature)
stateThe four 64-bit integer state of the algorithm which will be updated during the iteration.
Returns
A 64-bit pseudorandom number

◆ root()

real theoretica::root ( real  x,
int  n 
)
inline

Compute the n-th root of x.

Parameters
xA real number
nThe root number
Returns
The n-th real root of x

The Newton-Raphson method is used, limited by the THEORETICA_OPTIMIZATION_NEWTON_ITER macro constant.

◆ root_bisection()

template<typename RealFunction >
real theoretica::root_bisection ( RealFunction  f,
real  a,
real  b,
real  tolerance = OPTIMIZATION_TOL 
)
inline

Approximate a root of an arbitrary function using bisection inside a compact interval [a, b] where f(a) * f(b) < 0.

Parameters
fThe real function to search the root of.
aThe lower extreme of the interval.
bThe upper extreme of the interval.
toleranceThe minimum size of the bound to stop the algorithm.
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_chebyshev() [1/3]

real theoretica::root_chebyshev ( const polynomial< real > &  p,
real  guess = 0 
)
inline

Approximate a root of a polynomial using Chebyshev's method.

Parameters
pThe polynomial to search the root of.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_chebyshev() [2/3]

real theoretica::root_chebyshev ( dual2(*)(dual2 f,
real  guess = 0 
)
inline

Approximate a root of an arbitrary function using Chebyshev's method, by computing the first and second derivatives using automatic differentiation.

Parameters
fThe real function to search the root of, with dual2 argument and return value.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_chebyshev() [3/3]

template<typename RealFunction >
real theoretica::root_chebyshev ( RealFunction  f,
RealFunction  Df,
RealFunction  D2f,
real  guess = 0 
)
inline

Approximate a root of an arbitrary function using Chebyshev's method.

Parameters
fThe real function to search the root of.
DfThe first derivative of the function.
D2fThe second derivative of the function.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_halley() [1/3]

real theoretica::root_halley ( const polynomial< real > &  p,
real  guess = 0 
)
inline

Approximate a root of a polynomial using Halley's method.

Parameters
pThe polynomial to search the root of. @guess The initial guess (defaults to 0).
Returns
The coordinate of the root of the polynomial, or NaN if the algorithm did not converge.

◆ root_halley() [2/3]

real theoretica::root_halley ( dual2(*)(dual2 f,
real  guess = 0 
)
inline

Approximate a root of an arbitrary function using Halley's method, leveraging automatic differentiation to compute the first and second derivatives of the function.

Parameters
fThe real function to search the root of, with dual2 argument and return value.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_halley() [3/3]

template<typename RealFunction >
real theoretica::root_halley ( RealFunction  f,
RealFunction  Df,
RealFunction  D2f,
real  guess = 0 
)
inline

Approximate a root of an arbitrary function using Halley's method.

Parameters
fThe real function to search the root of.
DfThe first derivative of the function.
D2fThe second derivative of the function.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_newton() [1/4]

complex theoretica::root_newton ( complex<>(*)(complex<>)  f,
complex<>(*)(complex<>)  df,
complex<>  guess = complex<>(0, 0),
real  tolerance = OPTIMIZATION_TOL,
unsigned int  max_iter = OPTIMIZATION_TOL 
)
inline

Approximate a root of an arbitrary complex function using Newton's method,.

Parameters
fThe complex function to search the root of
dfThe derivative of the function
guessThe initial guess (defaults to 0).
toleranceThe minimum tolerance to stop the algorithm when reached.
max_iterThe maximum number of iterations before stopping the algorithm (defaults to OPTIMIZATION_TOL).
Returns
The coordinate of the root of the function, or a complex NaN if the algorithm did not converge.

◆ root_newton() [2/4]

real theoretica::root_newton ( const polynomial< real > &  p,
real  guess = 0 
)
inline

Approximate a root of a polynomial using Newton's method.

Parameters
pThe polynomial to search the root of.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the polynomial, or NaN if the algorithm did not converge.

◆ root_newton() [3/4]

real theoretica::root_newton ( dual(*)(dual f,
real  guess = 0 
)
inline

Approximate a root of an arbitrary function using Newton's method, computing the derivative using automatic differentiation.

Parameters
fThe real function to search the root of, with dual argument and return value.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_newton() [4/4]

template<typename RealFunction >
real theoretica::root_newton ( RealFunction  f,
RealFunction  Df,
real  guess = 0 
)
inline

Approximate a root of an arbitrary function using Newton's method.

Parameters
fThe real function to search the root of.
DfThe derivative of the function.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_steffensen() [1/2]

real theoretica::root_steffensen ( const polynomial< real > &  p,
real  guess = 0 
)
inline

Approximate a root of a polynomial using Steffensen's method.

Parameters
pThe polynomial to search the root of.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ root_steffensen() [2/2]

template<typename RealFunction >
real theoretica::root_steffensen ( RealFunction  f,
real  guess = 0 
)
inline

Approximate a root of an arbitrary function using Steffensen's method.

Parameters
fThe real function to search the root of.
guessThe initial guess (defaults to 0).
Returns
The coordinate of the root of the function, or NaN if the algorithm did not converge.

◆ roots() [1/2]

template<typename Field >
std::vector<Field> theoretica::roots ( const polynomial< Field > &  p,
real  tolerance = OPTIMIZATION_TOL,
unsigned int  steps = 0 
)
inline

Find all the roots of a polynomial.

An interval bound on the roots is found using Cauchy's theorem.

Parameters
pThe polynomial to search the roots of.
stepsThe number of steps to use (defaults to twice the polynomial's order).
Returns
A vector of the roots of the polynomial that were found.

◆ roots() [2/2]

template<typename RealFunction >
std::vector<real> theoretica::roots ( RealFunction  f,
real  a,
real  b,
real  tolerance = OPTIMIZATION_TOL,
real  steps = 10 
)
inline

Find the roots of a function inside a given interval.

Parameters
fThe real function to search the root of.
aThe lower extreme of the search interval.
bThe upper extreme of the search interval.
stepsThe number of sub-intervals to check for alternating sign (optional).
Returns
A vector of the roots of the function that were found.
Note
If the number of roots inside the interval is completely unknown, using many more steps should be preferred, to ensure all roots are found.

◆ sample_mc()

template<typename Vector = std::vector<real>, typename Function = std::function<real(vec<real>)>>
Vector theoretica::sample_mc ( Function  f,
std::vector< pdf_sampler > &  rv,
unsigned int  N 
)

Generate a Monte Carlo sample of values of a given function of arbitrary variables following the given distributions.

Parameters
fThe function with argument vec<real>
rvA vector of distribution samplers from the distributions of the random variables
NThe size of the sample
Returns
The sampled values

◆ sgn()

int theoretica::sgn ( real  x)
inline

Return the sign of x (1 if positive, -1 if negative, 0 if null)

Parameters
xA real number
Returns
The sign of x

◆ shuffle()

template<typename Vector >
void theoretica::shuffle ( Vector &  v,
PRNG g,
unsigned int  rounds = 0 
)
inline

Shuffle a set by exchanging random couples of elements.

Parameters
vThe set to shuffle (as a Vector with [] and .size() methods)
gAn already initialized PRNG
roundsThe number of pairs to exchange (defaults to (N - 1)^2)

◆ sigmoid()

real theoretica::sigmoid ( real  x)
inline

Compute the sigmoid function.

Parameters
xA real number
Returns
The sigmoid function for x defined as \(\frac{1}{1 + e^{-x}}\)

◆ sin() [1/2]

template<typename T >
complex<T> theoretica::sin ( complex< T >  z)
inline

Computer the complex sine.

Parameters
zA complex number

◆ sin() [2/2]

real theoretica::sin ( real  x)
inline

Compute the sine of a real number.

Parameters
xAn angle in radians
Returns
The sine of x

On x86 architectures, if THEORETICA_X86 is defined, the fsin instruction will be used.

◆ sinc()

real theoretica::sinc ( real  x)
inline

Compute the normalized sinc function.

Parameters
xA real number
Returns
The normalized sinc function for x defined as \(\frac{sin(\pi x)}{\pi x}\)

◆ sinh()

real theoretica::sinh ( real  x)
inline

Compute the hyperbolic sine.

Parameters
xA real number
Returns
The hyperbolic sine of x

\(sinh = \frac{e^x - e^{-x}}{2}\)

◆ sqrt() [1/2]

template<typename T >
complex<T> theoretica::sqrt ( complex< T >  z)
inline

Compute the complex square root.

Parameters
zA complex number

◆ sqrt() [2/2]

real theoretica::sqrt ( real  x)
inline

Compute the square root of a real number.

Parameters
xA real number
Returns
The square root of x

Domain: [0, +inf]
The Newton-Raphson algorithm, optimized for the square root and limited by the THEORETICA_OPTIMIZATION_NEWTON_ITER macro constant, is used. Domain reduction to [0, 1] is applied to ensure convergence of the algorithm. On x86 architectures, if THEORETICA_X86 is defined, the fsqrt instruction will be used.

◆ square() [1/2]

template<typename T >
complex<T> theoretica::square ( complex< T >  z)
inline

Compute the square of a complex number.

Parameters
zA complex number

◆ square() [2/2]

TH_CONSTEXPR real theoretica::square ( real  x)
inline

Compute the square of a real number.

Parameters
xA real number
Returns
The square of x

Domain: [-inf, +inf]

◆ sum() [1/2]

template<typename Vector , std::enable_if_t< has_real_elements< Vector >::value > = true>
auto theoretica::sum ( const Vector &  X)
inline

Compute the sum of a vector of real values using pairwise summation to reduce round-off error.

Parameters
XThe vector of values to sum

◆ sum() [2/2]

template<typename Vector >
auto theoretica::sum ( const Vector &  X)
inline

Compute the sum of a set of values.

Parameters
XThe vector of values to sum

◆ sum_compensated()

template<typename Vector >
real theoretica::sum_compensated ( const Vector &  X)
inline

Compute the sum of a set of values using the compensated Neumaier-Kahan-Babushka summation algorithm to reduce round-off error.

Parameters
XThe vector of real values to sum

◆ sum_pairwise()

template<typename Vector >
real theoretica::sum_pairwise ( const Vector &  X,
size_t  begin = 0,
size_t  end = 0,
size_t  base_size = 128 
)
inline

Compute the sum of a set of values using pairwise summation to reduce round-off error.

The function does not check for validity of begin and end indices.

Parameters
XThe vector of real values to sum
beginThe starting index of the sum, defaults to 0
endOne plus the last index of the sum, defaults to X.size()
base_sizeThe size of the base case, defaults to 128

◆ swap_bit_reverse()

template<typename Vector , enable_vector< Vector > = true>
void theoretica::swap_bit_reverse ( Vector &  x,
unsigned int  m 
)
inline

Swap the elements of a vector pair-wise, by exchanging elements with indices related by bit reversion (e.g.

\(110_2\) and \(011_2\)).

Parameters
xThe vector of elements to swap in-place
mThe maximum bit to consider when computing bit reversion

◆ tan() [1/2]

template<typename T >
complex<T> theoretica::tan ( complex< T >  z)
inline

Compute the complex tangent.

Parameters
zA complex number

◆ tan() [2/2]

real theoretica::tan ( real  x)
inline

Compute the tangent of x.

Parameters
xAn angle in radians
Returns
The tangent of x

On x86 architectures, if THEORETICA_X86 is defined, the fsincos instruction will be used if supported by the compiler.

◆ tanh()

real theoretica::tanh ( real  x)
inline

Compute the hyperbolic tangent.

Parameters
xA real number
Returns
The hyperbolic tangent of x