AutoPas  3.0.0
Loading...
Searching...
No Matches
Classes | Namespaces | Typedefs | Functions | Variables
Math.h File Reference
#include <Eigen/Core>
#include <cmath>
#include <vector>
Include dependency graph for Math.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  autopas::utils::Math::internal::int_t_impl< FloatType >
 Helper struct to get the correspondingly sized integer for a given floating type. More...
 

Namespaces

namespace  autopas
 This is the main namespace of AutoPas.
 
namespace  autopas::utils
 In this namespace some helper classes and functions can be found used inside of AutoPas.
 

Typedefs

template<std::floating_point FloatType>
using autopas::utils::Math::int_t = typename internal::int_t_impl< FloatType >::type
 Returns the correspondingly sized integer type for a given float.
 

Functions

template<class Int_t , std::enable_if_t< std::is_integral_v< Int_t >, bool > = true>
Int_t autopas::utils::Math::safeAdd (const Int_t &a, const Int_t &b, const Int_t &valUnderflow=std::numeric_limits< Int_t >::min(), const Int_t &valOverflow=std::numeric_limits< Int_t >::max())
 Addition function for integer types that is safe against over and underflow.
 
template<class Float_t , std::enable_if_t< std::is_floating_point_v< Float_t >, bool > = true>
Float_t autopas::utils::Math::safeAdd (const Float_t &a, const Float_t &b, const Float_t &valUnderflow=-std::numeric_limits< Float_t >::max(), const Float_t &valOverflow=std::numeric_limits< Float_t >::max())
 Addition function for floating point types that is safe against over and underflow.
 
template<class Int_t , std::enable_if_t< std::is_integral_v< Int_t >, bool > = true>
Int_t autopas::utils::Math::safeSub (const Int_t &a, const Int_t &b, const Int_t &valUnderflow=std::numeric_limits< Int_t >::min(), const Int_t &valOverflow=std::numeric_limits< Int_t >::max())
 Subtraction function for integer types that is safe against over and underflow.
 
template<class Float_t , std::enable_if_t< std::is_floating_point_v< Float_t >, bool > = true>
Float_t autopas::utils::Math::safeSub (const Float_t &a, const Float_t &b, const Float_t &valUnderflow=-std::numeric_limits< Float_t >::max(), const Float_t &valOverflow=std::numeric_limits< Float_t >::max())
 Addition function for floating point types that is safe against over and underflow.
 
template<class Int_t , std::enable_if_t< std::is_integral_v< Int_t >, bool > = true>
Int_t autopas::utils::Math::safeMul (const Int_t &a, const Int_t &b, const Int_t &valUnderflow=std::numeric_limits< Int_t >::min(), const Int_t &valOverflow=std::numeric_limits< Int_t >::max())
 Multiplication function for integer types that is safe against over and underflow.
 
template<class Float_t , std::enable_if_t< std::is_floating_point_v< Float_t >, bool > = true>
Float_t autopas::utils::Math::safeMul (const Float_t &a, const Float_t &b, const Float_t &valUnderflow=-std::numeric_limits< Float_t >::max(), const Float_t &valOverflow=std::numeric_limits< Float_t >::max())
 Multiplication function for floating point types that is safe against over and underflow.
 
template<size_t exponent, class T >
autopas::utils::Math::pow (const T &base)
 No-overhead power function with exponent known at compile time.
 
double autopas::utils::Math::normalPDF (double x)
 Probability density function PDF of the standard normal distribution.
 
double autopas::utils::Math::normalCDF (double x)
 Cumulative distribution function CDF of the standard normal distribution.
 
double autopas::utils::Math::sigmoid (double x)
 Sigmoid logistic function.
 
template<std::floating_point FloatType>
bool autopas::utils::Math::isInUlp (FloatType lhs, FloatType rhs, unsigned int ulpDistance=MAX_ULP_DISTANCE)
 Function for comparing closeness of two floating point numbers using ULP (Units in the Last Place) method.
 
template<std::floating_point FloatType>
bool autopas::utils::Math::isNearRel (FloatType a, FloatType b, double maxRelativeDifference=EPSILON_RELATIVE_EQUALITY)
 Determines if two doubles are near each other.
 
template<std::floating_point FloatType>
bool autopas::utils::Math::isNearAbs (FloatType a, FloatType b, double maxAbsoluteDifference)
 Determines if two doubles are near each other.
 
double autopas::utils::Math::roundFixed (double d, int fixedPrecision)
 Round a floating point number to a given number of decimal digits.
 
double autopas::utils::Math::roundFloating (double d, int floatingPrecision)
 Round a floating point number to a given floating precision.
 
Eigen::VectorXd autopas::utils::Math::makeVectorXd (const std::vector< double > &elements)
 Create a vector of doubles from given elements.
 
Eigen::VectorXi autopas::utils::Math::makeVectorXi (const std::vector< int > &elements)
 Create a vector of ints from given elements.
 

Variables

const double autopas::utils::Math::normalScale = 1. / std::sqrt(2 * M_PI)
 Factor of PDF of standard normal distribution.
 
constexpr double autopas::utils::Math::EPSILON_RELATIVE_EQUALITY = 1e-9
 This default relative EPSILON used for the isNearRel function.
 
constexpr unsigned int autopas::utils::Math::MAX_ULP_DISTANCE = 4
 The default maximal allowed ULP (Units in the Last Place) distance utilized for FloatingPoint comparisons.
 
template<typename T >
constexpr size_t autopas::utils::Math::internal::bit_size_v = sizeof(T) * 8
 Helper to find out the bit size of a given type.
 

Detailed Description

Author
Jan Nguyen
Date
18.08.19

Typedef Documentation

◆ int_t

template<std::floating_point FloatType>
using autopas::utils::Math::int_t = typedef typename internal::int_t_impl<FloatType>::type

Returns the correspondingly sized integer type for a given float.

E.g. int_t of float would be int32_t.

Function Documentation

◆ isInUlp()

template<std::floating_point FloatType>
bool autopas::utils::Math::isInUlp ( FloatType  lhs,
FloatType  rhs,
unsigned int  ulpDistance = MAX_ULP_DISTANCE 
)

Function for comparing closeness of two floating point numbers using ULP (Units in the Last Place) method.

Template Parameters
FloatTypemust be either double or float
Parameters
lhsThe left hand side floating point number to compare.
rhsThe right hand side floating point number to compare.
ulpDistanceThe maximum acceptable ULP distance between the two floating points for which they would be considered near each other, defaults to autopas::utils::Math::MAX_ULP_DISTANCE
Returns
true if the ULP distance between lhs and rhs is less than or equal to the provided ulpDistance value, otherwise, false. Returns true if both numbers are exactly the same. Returns false if the signs do not match.
Note
The ULP distance between 3.0 and std::nextafter(3.0, INFINITY) would be 1, the ULP distance of 3.0 and std::nextafter(std::nextafter(3.0, INFINITY), INFINITY) would be 2, etc.
See also
https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

◆ isNearAbs()

template<std::floating_point FloatType>
bool autopas::utils::Math::isNearAbs ( FloatType  a,
FloatType  b,
double  maxAbsoluteDifference 
)

Determines if two doubles are near each other.

This function should be preferred to comparing with ==.

Parameters
a
b
maxAbsoluteDifferenceinclusive
Returns

◆ isNearRel()

template<std::floating_point FloatType>
bool autopas::utils::Math::isNearRel ( FloatType  a,
FloatType  b,
double  maxRelativeDifference = EPSILON_RELATIVE_EQUALITY 
)

Determines if two doubles are near each other.

This function should be preferred to comparing with ==.

Parameters
a
b
maxRelativeDifferenceinclusive, relative to max(|a|, |b|), defaults to autopas::utils::Math::EPSILON_RELATIVE_EQUALITY
Returns

◆ makeVectorXd()

Eigen::VectorXd autopas::utils::Math::makeVectorXd ( const std::vector< double > &  elements)

Create a vector of doubles from given elements.

Parameters
elements
Returns

◆ makeVectorXi()

Eigen::VectorXi autopas::utils::Math::makeVectorXi ( const std::vector< int > &  elements)

Create a vector of ints from given elements.

Parameters
elements
Returns

◆ normalCDF()

double autopas::utils::Math::normalCDF ( double  x)

Cumulative distribution function CDF of the standard normal distribution.

Parameters
x
Returns
CDF(x)

◆ normalPDF()

double autopas::utils::Math::normalPDF ( double  x)

Probability density function PDF of the standard normal distribution.

Parameters
x
Returns
PDF(x)

◆ pow()

template<size_t exponent, class T >
T autopas::utils::Math::pow ( const T &  base)

No-overhead power function with exponent known at compile time.

Template Parameters
exponent
T
Parameters
base
Returns

◆ roundFixed()

double autopas::utils::Math::roundFixed ( double  d,
int  fixedPrecision 
)

Round a floating point number to a given number of decimal digits.

Parameters
dNumber to round.
fixedPrecisionNumber of decimal digits. Negative values lead to rounding of digits left of the decimal.
Returns
d rounded to the given number of digits.

◆ roundFloating()

double autopas::utils::Math::roundFloating ( double  d,
int  floatingPrecision 
)

Round a floating point number to a given floating precision.

Parameters
dNumber to round.
floatingPrecisionNumber of significant digits. Values <0 will return in 0.
Returns
d rounded to the given number of digits.

◆ safeAdd() [1/2]

template<class Float_t , std::enable_if_t< std::is_floating_point_v< Float_t >, bool > = true>
Float_t autopas::utils::Math::safeAdd ( const Float_t &  a,
const Float_t &  b,
const Float_t &  valUnderflow = -std::numeric_limits<Float_t>::max(),
const Float_t &  valOverflow = std::numeric_limits<Float_t>::max() 
)

Addition function for floating point types that is safe against over and underflow.

If over or underflow is detected, the function returns the specified values.

Note
Underflow here refers to a value more negative than representable and not the underflow gap around zero.
Template Parameters
Float_t
Parameters
a
b
valUnderflowReturn value in case of underflow.
valOverflowReturn value in case of overflow.
Returns
Sum or valUnderflow or valOverflow.

◆ safeAdd() [2/2]

template<class Int_t , std::enable_if_t< std::is_integral_v< Int_t >, bool > = true>
Int_t autopas::utils::Math::safeAdd ( const Int_t &  a,
const Int_t &  b,
const Int_t &  valUnderflow = std::numeric_limits<Int_t>::min(),
const Int_t &  valOverflow = std::numeric_limits<Int_t>::max() 
)

Addition function for integer types that is safe against over and underflow.

If over or underflow is detected, the function returns the specified values.

Template Parameters
Int_t
Parameters
a
b
valUnderflowReturn value in case of underflow.
valOverflowReturn value in case of overflow.
Returns
Sum or valOverflow or valUnderflow.

◆ safeMul() [1/2]

template<class Float_t , std::enable_if_t< std::is_floating_point_v< Float_t >, bool > = true>
Float_t autopas::utils::Math::safeMul ( const Float_t &  a,
const Float_t &  b,
const Float_t &  valUnderflow = -std::numeric_limits<Float_t>::max(),
const Float_t &  valOverflow = std::numeric_limits<Float_t>::max() 
)

Multiplication function for floating point types that is safe against over and underflow.

If over or underflow is detected, the function returns the specified values.

Note
Underflow here refers to a value more negative than representable and not the underflow gap around zero.
Template Parameters
Float_t
Parameters
a
b
valUnderflowReturn value in case of underflow.
valOverflowReturn value in case of overflow.
Returns
Product or valUnderflow or valOverflow.

◆ safeMul() [2/2]

template<class Int_t , std::enable_if_t< std::is_integral_v< Int_t >, bool > = true>
Int_t autopas::utils::Math::safeMul ( const Int_t &  a,
const Int_t &  b,
const Int_t &  valUnderflow = std::numeric_limits<Int_t>::min(),
const Int_t &  valOverflow = std::numeric_limits<Int_t>::max() 
)

Multiplication function for integer types that is safe against over and underflow.

If over or underflow is detected, the function returns the specified values.

Template Parameters
Int_t
Parameters
a
b
valUnderflowReturn value in case of underflow.
valOverflowReturn value in case of overflow.
Returns
Product or valUnderflow or valOverflow.

◆ safeSub() [1/2]

template<class Float_t , std::enable_if_t< std::is_floating_point_v< Float_t >, bool > = true>
Float_t autopas::utils::Math::safeSub ( const Float_t &  a,
const Float_t &  b,
const Float_t &  valUnderflow = -std::numeric_limits<Float_t>::max(),
const Float_t &  valOverflow = std::numeric_limits<Float_t>::max() 
)

Addition function for floating point types that is safe against over and underflow.

If over or underflow is detected, the function returns the specified values.

Note
Underflow here refers to a value more negative than representable and not the underflow gap around zero.
Template Parameters
Float_t
Parameters
a
b
valUnderflowReturn value in case of underflow.
valOverflowReturn value in case of overflow.
Returns
Sum or valUnderflow or valOverflow.

◆ safeSub() [2/2]

template<class Int_t , std::enable_if_t< std::is_integral_v< Int_t >, bool > = true>
Int_t autopas::utils::Math::safeSub ( const Int_t &  a,
const Int_t &  b,
const Int_t &  valUnderflow = std::numeric_limits<Int_t>::min(),
const Int_t &  valOverflow = std::numeric_limits<Int_t>::max() 
)

Subtraction function for integer types that is safe against over and underflow.

If over or underflow is detected, the function returns the specified values.

Template Parameters
Int_t
Parameters
a
b
valUnderflowReturn value in case of underflow.
valOverflowReturn value in case of overflow.
Returns
Difference or valOverflow or valUnderflow.

◆ sigmoid()

double autopas::utils::Math::sigmoid ( double  x)

Sigmoid logistic function.

Parameters
x
Returns
S(x)

Variable Documentation

◆ bit_size_v

template<typename T >
constexpr size_t autopas::utils::Math::internal::bit_size_v = sizeof(T) * 8
constexpr

Helper to find out the bit size of a given type.

Template Parameters
Tany type T

◆ MAX_ULP_DISTANCE

constexpr unsigned int autopas::utils::Math::MAX_ULP_DISTANCE = 4
constexpr

The default maximal allowed ULP (Units in the Last Place) distance utilized for FloatingPoint comparisons.

This is also the value utilized in GoogleTest for their DoubleEq() and FloatEq() Matchers.

See also
https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/