Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
Namespaces | Macros | Functions | Variables
math.hpp File Reference

Scalar math helper utilities. More...

#include "detail/native_agm_math.hpp"
#include "detail/native_airy_math.hpp"
#include "detail/native_compound_math.hpp"
#include "detail/native_digamma_math.hpp"
#include "detail/native_dilog_math.hpp"
#include "detail/native_exponential_math.hpp"
#include "detail/native_expint_math.hpp"
#include "detail/native_math.hpp"
#include "detail/native_pi_math.hpp"
#include "detail/native_reciprocal_math.hpp"
#include "detail/native_upper_gamma_math.hpp"
#include "detail/native_special_math.hpp"
#include "factorial.hpp"
#include "math_constants.hpp"
#include "numeric_limits.hpp"
#include "runtime_precision.hpp"
#include "scalar_concepts.hpp"
#include <cmath>
#include <complex>
#include <numeric>
#include <type_traits>
#include <utility>
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.

Namespaces

namespace  uni20
 
namespace  uni20::math
 Shared scalar math with native overloads and class-specific ADL customization.
 

Macros

#define UNI20_SCALAR_MATH_ADL(NAME)
 
#define UNI20_SCALAR_MATH_DISPATCH(NAME)   UNI20_SCALAR_MATH_DISPATCH_POLICY(NAME, preserves_real_precision)
 
#define UNI20_SCALAR_MATH_DISPATCH_ADL_ONLY(NAME)
 
#define UNI20_SCALAR_MATH_DISPATCH_POLICY(NAME, CHECK)   UNI20_SCALAR_MATH_DISPATCH_TO(NAME, std, CHECK)
 
#define UNI20_SCALAR_MATH_DISPATCH_TO(NAME, NATIVE, RESULT_CHECK)
 

Functions

template<class T >
requires requires(T const& value) { math::abs(value) * math::abs(value); }
auto uni20::math::abs_squared (T const &value)
 Square the magnitude, evaluating the scalar absolute value once.
 
template<typename T >
uni20::complex< T > uni20::conj (detail::standard_complex< T > x)
 Returns the complex conjugate for complex-valued scalars.
 
template<Integer I>
constexpr I uni20::conj (I const &x)
 Returns the conjugate of an integer scalar.
 
template<Real R>
constexpr R uni20::conj (R const &x)
 Returns the conjugate of a real-valued scalar.
 
template<Scalar S>
constexpr auto uni20::herm (S x)
 Computes the Hermitian adjoint of a scalar value.
 
template<typename T >
constexpr T & uni20::imag (detail::standard_complex< T > &z) noexcept
 Provides mutable access to the imaginary component of a uni20::complex value.
 
template<class T >
constexpr bool uni20::is_exact (detail::standard_complex< T > const &x)
 A standard complex value is exact only when both components are exact.
 
template<Complex C>
constexpr bool uni20::isfinite (C const &z)
 Returns whether both components of a complex scalar are finite.
 
template<Integer I>
constexpr bool uni20::isfinite (I const &x) noexcept
 Returns whether an integer scalar is finite.
 
template<Real R>
constexpr bool uni20::isfinite (R const &x)
 Returns whether a real scalar is neither NaN nor positive/negative infinity.
 
template<typename T >
constexpr T & uni20::real (detail::standard_complex< T > &z) noexcept
 Provides mutable access to the real component of a uni20::complex value.
 
template<Scalar S, class Value >
requires (std::constructible_from<S, Value const&> || requires(S const& exemplar, Value const& value) { S(value, exemplar.precision()); })
constexpr auto uni20::scalar_like (S const &exemplar, Value const &value) -> S
 Construct a scalar using an exemplar's type and numerical precision.
 

Variables

constexpr detail::scalar_math::abs_fn uni20::math::abs {}
 Absolute value or complex magnitude, preserving the selected scalar implementation.
 
constexpr detail::scalar_math::acos_fn uni20::math::acos {}
 Inverse cosine in radians.
 
constexpr detail::scalar_math::acosh_fn uni20::math::acosh {}
 Inverse hyperbolic cosine.
 
constexpr detail::scalar_math::acospi_fn uni20::math::acospi {}
 Principal arccosine divided by pi.
 
constexpr detail::scalar_math::agm_fn uni20::math::agm {}
 Nonnegative arithmetic-geometric mean.
 
constexpr detail::scalar_math::airy_ai_fn uni20::math::airy_ai {}
 Real Airy Ai; native inputs retain their precision and full negative-axis phase.
 
constexpr detail::scalar_math::asin_fn uni20::math::asin {}
 Inverse sine in radians.
 
constexpr detail::scalar_math::asinh_fn uni20::math::asinh {}
 Inverse hyperbolic sine.
 
constexpr detail::scalar_math::asinpi_fn uni20::math::asinpi {}
 Principal arcsine divided by pi.
 
constexpr detail::scalar_math::atan_fn uni20::math::atan {}
 Inverse tangent in radians.
 
constexpr detail::scalar_math::atan2_fn uni20::math::atan2 {}
 Quadrant-aware inverse tangent of y/x in radians.
 
constexpr detail::scalar_math::atan2pi_fn uni20::math::atan2pi {}
 Quadrant-aware arctangent divided by pi.
 
constexpr detail::scalar_math::atanh_fn uni20::math::atanh {}
 Inverse hyperbolic tangent.
 
constexpr detail::scalar_math::atanpi_fn uni20::math::atanpi {}
 Principal arctangent divided by pi.
 
constexpr detail::scalar_math::bessel_j_fn uni20::math::bessel_j {}
 Integer-order Bessel function of the first kind, with order first.
 
constexpr detail::scalar_math::bessel_y_fn uni20::math::bessel_y {}
 Integer-order Bessel function of the second kind, with order first.
 
constexpr detail::scalar_math::beta_fn uni20::math::beta {}
 Real Beta function.
 
constexpr detail::scalar_math::cbrt_fn uni20::math::cbrt {}
 Real cube root, including negative arguments.
 
constexpr detail::scalar_math::ceil_fn uni20::math::ceil {}
 Least integral value not less than the argument, in the scalar's result type.
 
constexpr detail::scalar_math::compound_fn uni20::math::compound {}
 Integer power of one plus x, retaining the original increment.
 
constexpr detail::scalar_math::conj_fn uni20::math::conj {}
 Complex conjugation; real and integral values retain their type.
 
constexpr detail::scalar_math::copysign_fn uni20::math::copysign {}
 Copy the sign while retaining the magnitude operand type and precision.
 
constexpr detail::scalar_math::cos_fn uni20::math::cos {}
 Cosine with scalar-specific precision handling.
 
constexpr detail::scalar_math::cosh_fn uni20::math::cosh {}
 Hyperbolic cosine.
 
constexpr detail::scalar_math::cospi_fn uni20::math::cospi {}
 Cosine of pi times x, with argument reduction before multiplication by pi.
 
constexpr detail::scalar_math::cot_fn uni20::math::cot {}
 Reciprocal tangent, retaining the input type and precision.
 
constexpr detail::scalar_math::coth_fn uni20::math::coth {}
 Reciprocal hyperbolic tangent, retaining the input type and precision.
 
constexpr detail::scalar_math::csc_fn uni20::math::csc {}
 Reciprocal sine, retaining the input type and precision.
 
constexpr detail::scalar_math::csch_fn uni20::math::csch {}
 Reciprocal hyperbolic sine, retaining the input type and precision.
 
constexpr detail::scalar_math::digamma_fn uni20::math::digamma {}
 Logarithmic derivative of Gamma.
 
constexpr detail::scalar_math::dilog_real_fn uni20::math::dilog_real {}
 Real part of the dilogarithm.
 
constexpr detail::scalar_math::erf_fn uni20::math::erf {}
 Real error function.
 
constexpr detail::scalar_math::erfc_fn uni20::math::erfc {}
 Complementary error function without subtractive cancellation.
 
constexpr detail::scalar_math::exp_fn uni20::math::exp {}
 Natural exponential with scalar-specific precision handling.
 
constexpr detail::scalar_math::exp10_fn uni20::math::exp10 {}
 Base-ten exponential.
 
constexpr detail::scalar_math::exp10m1_fn uni20::math::exp10m1 {}
 Ten to the power x minus one, using the cancellation-safe provider.
 
constexpr detail::scalar_math::exp2_fn uni20::math::exp2 {}
 Base-two exponential.
 
constexpr detail::scalar_math::exp2m1_fn uni20::math::exp2m1 {}
 Two to the power x minus one, using the cancellation-safe provider.
 
constexpr detail::scalar_math::expint_fn uni20::math::expint {}
 Real principal-value exponential integral Ei.
 
constexpr detail::scalar_math::expm1_fn uni20::math::expm1 {}
 Natural exponential minus one, evaluated without subtractive cancellation.
 
constexpr detail::scalar_math::factorial_fn uni20::math::factorial {}
 MPFR factorial at an explicit arithmetic state; use uni20::factorial<Real> for a typed result.
 
constexpr detail::scalar_math::fdim_fn uni20::math::fdim {}
 Positive difference, preserving NaN propagation.
 
constexpr detail::scalar_math::floor_fn uni20::math::floor {}
 Round toward negative infinity.
 
constexpr detail::scalar_math::fma_fn uni20::math::fma {}
 Fused multiplication and addition with one final rounding.
 
constexpr detail::scalar_math::fmax_fn uni20::math::fmax {}
 Maximum, preferring the numeric operand over a NaN.
 
constexpr detail::scalar_math::fmin_fn uni20::math::fmin {}
 Minimum, preferring the numeric operand over a NaN.
 
constexpr detail::scalar_math::fmod_fn uni20::math::fmod {}
 Remainder using a quotient truncated toward zero.
 
constexpr detail::scalar_math::frexp_fn uni20::math::frexp {}
 Binary fraction and exponent as an owning result.
 
template<typename T >
constexpr bool uni20::has_trivial_conj = Real<T> || Integer<T>
 Indicates whether the Uni20 conjugation helper is a no-op for the provided scalar type.
 
constexpr detail::scalar_math::hypot_fn uni20::math::hypot {}
 Euclidean length with scalar-specific overflow and underflow handling.
 
constexpr detail::scalar_math::ilogb_fn uni20::math::ilogb {}
 Binary exponent of a finite nonzero value; throws otherwise.
 
constexpr detail::scalar_math::imag_fn uni20::math::imag {}
 Read the imaginary component by value; real zero retains the input's type and precision.
 
constexpr detail::scalar_math::isfinite_fn uni20::math::isfinite {}
 Whether a real scalar is finite.
 
constexpr detail::scalar_math::isinf_fn uni20::math::isinf {}
 Whether a real scalar is infinite.
 
constexpr detail::scalar_math::isnan_fn uni20::math::isnan {}
 Whether a real scalar is NaN.
 
constexpr detail::scalar_math::ldexp_fn uni20::math::ldexp {}
 Multiply by a power of two using the scalar's exponent interface.
 
constexpr detail::scalar_math::lgamma_fn uni20::math::lgamma {}
 Logarithm of the absolute Gamma function; native providers are reentrant.
 
constexpr detail::scalar_math::lgamma_sign_fn uni20::math::lgamma_sign {}
 Log-absolute-Gamma and its sign as owning values.
 
constexpr detail::scalar_math::log_fn uni20::math::log {}
 Natural logarithm with scalar-specific precision handling.
 
constexpr detail::scalar_math::log10_fn uni20::math::log10 {}
 Base-ten logarithm.
 
constexpr detail::scalar_math::log10p1_fn uni20::math::log10p1 {}
 Base-ten logarithm of one plus x, without rounding the sum first.
 
constexpr detail::scalar_math::log1p_fn uni20::math::log1p {}
 Natural logarithm of one plus the argument, retaining small increments.
 
constexpr detail::scalar_math::log2_fn uni20::math::log2 {}
 Base-two logarithm where supplied by the scalar implementation.
 
constexpr detail::scalar_math::log2p1_fn uni20::math::log2p1 {}
 Base-two logarithm of one plus x, without rounding the sum first.
 
constexpr detail::scalar_math::modf_fn uni20::math::modf {}
 Signed fractional and integral parts as an owning result.
 
constexpr detail::scalar_math::next_down_fn uni20::math::next_down {}
 Adjacent value toward negative infinity.
 
constexpr detail::scalar_math::next_up_fn uni20::math::next_up {}
 Adjacent value toward positive infinity.
 
constexpr detail::scalar_math::nextafter_fn uni20::math::nextafter {}
 Adjacent value on the first operand grid toward the direction operand.
 
constexpr detail::scalar_math::pow_fn uni20::math::pow {}
 Power with the selected scalar's domain, exactness and precision rules.
 
constexpr detail::scalar_math::pown_fn uni20::math::pown {}
 Integer power with scalar-specific rounding.
 
constexpr detail::scalar_math::real_fn uni20::math::real {}
 Read the real component by value; real and integral scalars retain their type.
 
constexpr detail::scalar_math::remainder_fn uni20::math::remainder {}
 Remainder using the nearest-even integer quotient.
 
constexpr detail::scalar_math::remquo_fn uni20::math::remquo {}
 Nearest-even remainder and signed low three quotient bits.
 
constexpr detail::scalar_math::rootn_fn uni20::math::rootn {}
 Real integer-order root; negative orders give reciprocal roots.
 
constexpr detail::scalar_math::round_fn uni20::math::round {}
 Round to nearest integer with ties away from zero.
 
constexpr detail::scalar_math::round_even_fn uni20::math::round_even {}
 Nearest integer with ties to even.
 
constexpr detail::scalar_math::scalbn_fn uni20::math::scalbn {}
 Binary scaling with a signed 64-bit exponent.
 
constexpr detail::scalar_math::sec_fn uni20::math::sec {}
 Reciprocal cosine, retaining the input type and precision.
 
constexpr detail::scalar_math::sech_fn uni20::math::sech {}
 Reciprocal hyperbolic cosine, retaining the input type and precision.
 
constexpr detail::scalar_math::signbit_fn uni20::math::signbit {}
 Inspect the sign, including approximate negative zero.
 
constexpr detail::scalar_math::sin_fn uni20::math::sin {}
 Sine with scalar-specific precision handling.
 
constexpr detail::scalar_math::sincos_fn uni20::math::sincos {}
 Sine and cosine at one input.
 
constexpr detail::scalar_math::sinh_fn uni20::math::sinh {}
 Hyperbolic sine.
 
constexpr detail::scalar_math::sinhcosh_fn uni20::math::sinhcosh {}
 Hyperbolic sine and cosine at one input.
 
constexpr detail::scalar_math::sinpi_fn uni20::math::sinpi {}
 Sine of pi times x, with argument reduction before multiplication by pi.
 
constexpr detail::scalar_math::sqrt_fn uni20::math::sqrt {}
 Square root; exact inputs requiring approximation need an explicit finite precision.
 
constexpr detail::scalar_math::tan_fn uni20::math::tan {}
 Tangent in radians.
 
constexpr detail::scalar_math::tanh_fn uni20::math::tanh {}
 Hyperbolic tangent.
 
constexpr detail::scalar_math::tanpi_fn uni20::math::tanpi {}
 Tangent of pi times x, with argument reduction before multiplication by pi.
 
constexpr detail::scalar_math::tgamma_fn uni20::math::tgamma {}
 Real Gamma function.
 
constexpr detail::scalar_math::trunc_fn uni20::math::trunc {}
 Round toward zero.
 
constexpr detail::scalar_math::upper_gamma_fn uni20::math::upper_gamma {}
 Upper unregularized incomplete Gamma, with shape and lower integration endpoint.
 
constexpr detail::scalar_math::zeta_fn uni20::math::zeta {}
 Real Riemann zeta with native Euler-Maclaurin evaluation and reflection.
 

Detailed Description

Scalar math helper utilities.

Macro Definition Documentation

◆ UNI20_SCALAR_MATH_ADL

#define UNI20_SCALAR_MATH_ADL (   NAME)
Value:
namespace NAME##_adl \
{ \
template <class... Args> void NAME(Args const&...) = delete; \
template <class... Args> \
constexpr auto call(Args const&... args) noexcept(noexcept(NAME(args...))) -> decltype(NAME(args...)) \
{ \
return NAME(args...); \
} \
}

◆ UNI20_SCALAR_MATH_DISPATCH

#define UNI20_SCALAR_MATH_DISPATCH (   NAME)    UNI20_SCALAR_MATH_DISPATCH_POLICY(NAME, preserves_real_precision)

◆ UNI20_SCALAR_MATH_DISPATCH_ADL_ONLY

#define UNI20_SCALAR_MATH_DISPATCH_ADL_ONLY (   NAME)
Value:
struct NAME##_fn \
{ \
template <class... Args> \
constexpr auto operator()(Args const&... args) const \
noexcept(noexcept(NAME##_adl::call(args...))) -> decltype(NAME##_adl::call(args...)) \
{ \
return NAME##_adl::call(args...); \
} \
};
#define UNI20_SCALAR_MATH_ADL(NAME)
Definition math.hpp:229

◆ UNI20_SCALAR_MATH_DISPATCH_POLICY

#define UNI20_SCALAR_MATH_DISPATCH_POLICY (   NAME,
  CHECK 
)    UNI20_SCALAR_MATH_DISPATCH_TO(NAME, std, CHECK)

◆ UNI20_SCALAR_MATH_DISPATCH_TO

#define UNI20_SCALAR_MATH_DISPATCH_TO (   NAME,
  NATIVE,
  RESULT_CHECK 
)
Value:
struct NAME##_fn \
{ \
template <class... Args> \
constexpr auto operator()(Args const&... args) const \
noexcept(noexcept(NAME##_adl::call(args...))) -> decltype(NAME##_adl::call(args...)) \
{ \
return NAME##_adl::call(args...); \
} \
template <class... Args> \
requires((native_argument<Args> && ...) && !requires(Args const&... args) { NAME##_adl::call(args...); }) \
constexpr auto operator()(Args const&... args) const \
noexcept(noexcept(NATIVE::NAME(args...))) -> decltype(NATIVE::NAME(args...)) \
requires((RESULT_CHECK<decltype(NATIVE::NAME(args...)), Args>()) && ...) \
{ \
return NATIVE::NAME(args...); \
} \
};