Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
native_reciprocal_math.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "native_math.hpp"
5
6namespace uni20::detail::native_math
7{
8template <NativeReal R> R sec(R x) { return R(1) / std::cos(x); }
9template <NativeReal R> R csc(R x) { return R(1) / std::sin(x); }
10template <NativeReal R> R cot(R x) { return R(1) / std::tan(x); }
11template <NativeReal R> R coth(R x) { return R(1) / std::tanh(x); }
12
13template <bool Sine, NativeReal R> R reciprocal_hyperbolic(R x)
14{
15 using L = numeric_limits<R>;
16 R magnitude = std::abs(x);
17 if (magnitude <= R(L::digits())) return R(1) / (Sine ? std::sinh(x) : std::cosh(x));
18
19 // Beyond digits(), the relative denominator correction exp(-2*abs(x)) is
20 // smaller than epsilon squared. Evaluate the shared tail 2*exp(-abs(x)).
21 R result;
22 if (magnitude < R(-L::min_exponent) * log_two<R>)
23 result = R(2) * std::exp(-magnitude);
24 else
25 {
26 // Squaring an exponential at half the argument preserves representable
27 // final subnormals which exp(-abs(x)) might prematurely round to zero.
28 R half = std::exp(-magnitude / 2);
29 result = half * (R(2) * half);
30 }
31 if constexpr (Sine) return std::copysign(result, x);
32 return result;
33}
34template <NativeReal R> R sech(R x) { return reciprocal_hyperbolic<false>(x); }
35template <NativeReal R> R csch(R x) { return reciprocal_hyperbolic<true>(x); }
36} // namespace uni20::detail::native_math
Definition qnum.hpp:403
DiagnosticMagnitude< R > magnitude(R const &value)
Definition gmres_diagnostics.hpp:98
constexpr detail::scalar_math::csch_fn csch
Reciprocal hyperbolic sine, retaining the input type and precision.
Definition math.hpp:660
constexpr detail::scalar_math::sech_fn sech
Reciprocal hyperbolic cosine, retaining the input type and precision.
Definition math.hpp:658
constexpr detail::scalar_math::cot_fn cot
Reciprocal tangent, retaining the input type and precision.
Definition math.hpp:656
constexpr detail::scalar_math::coth_fn coth
Reciprocal hyperbolic tangent, retaining the input type and precision.
Definition math.hpp:662
constexpr detail::scalar_math::sec_fn sec
Reciprocal cosine, retaining the input type and precision.
Definition math.hpp:652
constexpr detail::scalar_math::csc_fn csc
Reciprocal sine, retaining the input type and precision.
Definition math.hpp:654
mpcomplex cosh(mpcomplex const &z)
Hyperbolic cosine at the input precision; exact zero returns exact one.
Definition mpcomplex.hpp:511