LvArray
Classes | Namespaces | Functions
math.hpp File Reference

Contains some portable math functions. More...

#include "LvArrayConfig.hpp"
#include "Macros.hpp"
#include <cmath>
#include <type_traits>
Include dependency graph for math.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  LvArray::math::internal::SingleType< T >
 The type of a single value of type T. More...
 

Namespaces

 LvArray
 The top level namespace.
 
 LvArray::math
 Contains protable wrappers around cmath functions and some cuda specific functions.
 

Functions

template<typename T , typename U >
LVARRAY_HOST_DEVICE constexpr T LvArray::math::internal::convert (T const, U const u)
 Convert u to type. More...
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr int LvArray::math::internal::numValues (T const)
 Return the number of values stored in. More...
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr SingleType< T > LvArray::math::internal::getFirst (T const x)
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr SingleType< T > LvArray::math::internal::getSecond (T const x)
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr T LvArray::math::internal::lessThan (T const x, T const y)
 
template<typename T >
LVARRAY_DEVICELvArray::math::internal::asinImpl (T const x)
 
template<typename T >
LVARRAY_DEVICELvArray::math::internal::acosImpl (T const x)
 
template<typename T >
LVARRAY_DEVICELvArray::math::internal::atan2Impl (T const y, T const x)
 
Square root and inverse square root.
LVARRAY_HOST_DEVICE float LvArray::math::sqrt (float const x)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::sqrt (T const x)
 
LVARRAY_HOST_DEVICE float LvArray::math::invSqrt (float const x)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::invSqrt (T const x)
 
Trigonometric functions
LVARRAY_HOST_DEVICE float LvArray::math::sin (float const theta)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::sin (T const theta)
 
LVARRAY_HOST_DEVICE float LvArray::math::cos (float const theta)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::cos (T const theta)
 
LVARRAY_HOST_DEVICE void LvArray::math::sincos (float const theta, float &sinTheta, float &cosTheta)
 Compute the sine and cosine of theta. More...
 
template<typename T >
LVARRAY_HOST_DEVICE void LvArray::math::sincos (double const theta, double &sinTheta, double &cosTheta)
 Compute the sine and cosine of theta. More...
 
template<typename T >
LVARRAY_HOST_DEVICE void LvArray::math::sincos (T const theta, double &sinTheta, double &cosTheta)
 Compute the sine and cosine of theta. More...
 
LVARRAY_HOST_DEVICE float LvArray::math::tan (float const theta)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::tan (T const theta)
 
Inverse trigonometric functions
LVARRAY_HOST_DEVICE float LvArray::math::asin (float const x)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::asin (T const x)
 
LVARRAY_HOST_DEVICE float LvArray::math::acos (float const x)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::acos (T const x)
 
LVARRAY_HOST_DEVICE float LvArray::math::atan2 (float const y, float const x)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::atan2 (T const y, T const x)
 
Exponential functions
LVARRAY_HOST_DEVICE float LvArray::math::exp (float const x)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::exp (T const x)
 
LVARRAY_HOST_DEVICE float LvArray::math::log (float const x)
 
template<typename T >
LVARRAY_HOST_DEVICE double LvArray::math::log (T const x)
 

General purpose functions

template<typename T >
using LvArray::math::SingleType = typename internal::SingleType< T >::type
 The type of a single value of type T. More...
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr int LvArray::math::numValues ()
 Return the number of values stored in type. More...
 
template<typename T , typename U >
LVARRAY_HOST_DEVICE constexpr T LvArray::math::convert (U const u)
 Convert u to type. More...
 
template<typename T , typename U , typename V >
LVARRAY_HOST_DEVICE constexpr T LvArray::math::convert (U const u, V const v)
 Convert u and v to a dual type. More...
 
template<typename T >
LVARRAY_DEVICE SingleType< T > LvArray::math::getFirst (T const x)
 
template<typename T >
LVARRAY_DEVICE SingleType< T > LvArray::math::getSecond (T const x)
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > LvArray::math::max (T const a, T const b)
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr std::enable_if_t< std::is_arithmetic< T >::value, T > LvArray::math::min (T const a, T const b)
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr T LvArray::math::abs (T const x)
 
template<typename T >
LVARRAY_HOST_DEVICE constexpr T LvArray::math::square (T const x)
 

Detailed Description

Contains some portable math functions.

Function Documentation

◆ acosImpl()

template<typename T >
LVARRAY_DEVICE T LvArray::math::internal::acosImpl ( T const  x)
inline
Returns
The arcsine of x.
Parameters
xThe value to get the arcsine of, must be in [-1, 1].

Has a max error of 2.64e-3 and max relative error of 1.37e-3 for half precision.

Note
Modified from https://developer.download.nvidia.com/cg/acos.html

◆ asinImpl()

template<typename T >
LVARRAY_DEVICE T LvArray::math::internal::asinImpl ( T const  x)
inline
Returns
The arcsine of x.
Parameters
xThe value to get the arcsine of, must be in [-1, 1].

Has a max error of 1.36e-3 and max relative error of 6.95e-3 for half precision. The largeish relative error occurs where the algorithm transitions away from the small value approximation. The original algorithm didn't the small value approximation and it had huge (~3.0x) relative error, I believe due to the poor cancellation when subtracting from pi/2. acos doesn't have this problem and calculating asin as

pi/2 - acos

led to the the same error. To improve precision you could always add in another term in the taylor series (x^3 / 6) but I don't think it's worth it.

Note
Modified from https://developer.download.nvidia.com/cg/asin.html

◆ atan2Impl()

template<typename T >
LVARRAY_DEVICE T LvArray::math::internal::atan2Impl ( T const  y,
T const  x 
)
inline
Returns
The arctangent of the point x, y.
Parameters
yThe y coordinate.
xThe x coordinate.

Has a max error of 2.29e-3 and max relative error of 1.51e-3 for half precision.

Note
Modified from https://developer.download.nvidia.com/cg/atan2.html

◆ convert()

template<typename T , typename U >
LVARRAY_HOST_DEVICE constexpr T LvArray::math::internal::convert ( const,
U const  u 
)
inline

Convert u to type.

Template Parameters
T.
TThe type to convert to.
UThe type to convert from.
Parameters
uThe value to convert.
Note
No checking is done to ensure the conversion is safe. Ie you could convert -1 to uint.
Returns
u converted to
Template Parameters
T.

◆ getFirst()

template<typename T >
LVARRAY_HOST_DEVICE constexpr SingleType< T > LvArray::math::internal::getFirst ( T const  x)
inline
Returns
x.
Template Parameters
TThe type of x.
Parameters
xThe value to return.

◆ getSecond()

template<typename T >
LVARRAY_HOST_DEVICE constexpr SingleType< T > LvArray::math::internal::getSecond ( T const  x)
inline
Returns
x.
Template Parameters
TThe type of x.
Parameters
xThe value to return.

◆ lessThan()

template<typename T >
LVARRAY_HOST_DEVICE constexpr T LvArray::math::internal::lessThan ( T const  x,
T const  y 
)
inline
Returns
1 if x is less than y, else 0.
Template Parameters
TThe type of x and y.
Parameters
xThe first value.
yThe second value.

◆ numValues()

template<typename T >
LVARRAY_HOST_DEVICE constexpr int LvArray::math::internal::numValues ( const)
inline

Return the number of values stored in.

Template Parameters
T,bydefault this is 1.
TThe type to query.
Returns
The number of values stored in
Template Parameters
T,bydefault this is 1.