|
17 | 17 |
|
18 | 18 | #pragma once
|
19 | 19 |
|
| 20 | +#include <limits> |
| 21 | + |
20 | 22 | namespace sva
|
21 | 23 | {
|
22 | 24 |
|
| 25 | +namespace details |
| 26 | +{ |
| 27 | + |
| 28 | +template <typename T> |
| 29 | +T constexpr sqrtNewtonRaphson(T x, T curr, T prev) |
| 30 | +{ |
| 31 | + return curr == prev |
| 32 | + ? curr |
| 33 | + : sqrtNewtonRaphson(x, static_cast<T>(0.5) * (curr + x / curr), curr); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | +* Constexpr version of the square root |
| 38 | +* Return value: |
| 39 | +* - For a finite and non-negative value of "x", returns an approximation for the square root of "x" |
| 40 | +* - Otherwise, returns NaN |
| 41 | +* Copied from https://stackoverflow.com/a/34134071 |
| 42 | +*/ |
| 43 | +template <typename T> |
| 44 | +T constexpr sqrt(T x) |
| 45 | +{ |
| 46 | + return x >= static_cast<T>(0) && x < std::numeric_limits<T>::infinity() |
| 47 | + ? sqrtNewtonRaphson(x, x, static_cast<T>(0)) |
| 48 | + : std::numeric_limits<T>::quiet_NaN(); |
| 49 | +} |
| 50 | + |
| 51 | +} // namespace details |
| 52 | + |
| 53 | +/** sinus cardinal: sin(x)/x |
| 54 | + * Code adapted from boost::math::detail::sinc |
| 55 | + */ |
| 56 | +template<typename T> |
| 57 | +T sinc(const T x) |
| 58 | +{ |
| 59 | + constexpr T taylor_0_bound = std::numeric_limits<double>::epsilon(); |
| 60 | + constexpr T taylor_2_bound = details::sqrt(taylor_0_bound); |
| 61 | + constexpr T taylor_n_bound = details::sqrt(taylor_2_bound); |
| 62 | + |
| 63 | + if (std::abs(x) >= taylor_n_bound) |
| 64 | + { |
| 65 | + return(std::sin(x) / x); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + // approximation by taylor series in x at 0 up to order 0 |
| 70 | + T result = static_cast<T>(1); |
| 71 | + |
| 72 | + if (std::abs(x) >= taylor_0_bound) |
| 73 | + { |
| 74 | + T x2 = x*x; |
| 75 | + |
| 76 | + // approximation by taylor series in x at 0 up to order 2 |
| 77 | + result -= x2 / static_cast<T>(6); |
| 78 | + |
| 79 | + if (std::abs(x) >= taylor_2_bound) |
| 80 | + { |
| 81 | + // approximation by taylor series in x at 0 up to order 4 |
| 82 | + result += (x2*x2) / static_cast<T>(120); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return(result); |
| 87 | + } |
| 88 | +} |
| 89 | + |
23 | 90 | /**
|
24 | 91 | * Compute 1/sinc(x).
|
25 | 92 | * This code is inspired by boost/math/special_functions/sinc.hpp.
|
26 | 93 | */
|
27 | 94 | template<typename T>
|
28 | 95 | T sinc_inv(const T x)
|
29 | 96 | {
|
30 |
| - const T taylor_0_bound = std::numeric_limits<T>::epsilon(); |
31 |
| - const T taylor_2_bound = std::sqrt(taylor_0_bound); |
32 |
| - const T taylor_n_bound = std::sqrt(taylor_2_bound); |
| 97 | + constexpr T taylor_0_bound = std::numeric_limits<T>::epsilon(); |
| 98 | + constexpr T taylor_2_bound = details::sqrt(taylor_0_bound); |
| 99 | + constexpr T taylor_n_bound = details::sqrt(taylor_2_bound); |
33 | 100 |
|
34 | 101 | // We use the 4th order taylor series around 0 of x/sin(x) to compute
|
35 | 102 | // this function:
|
@@ -71,4 +138,4 @@ T sinc_inv(const T x)
|
71 | 138 | }
|
72 | 139 | }
|
73 | 140 |
|
74 |
| -} |
| 141 | +} // namespace sva |
0 commit comments