| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// UNSUPPORTED: c++03, c++11, c++14 |
| 10 |
|
| 11 |
// <cmath> |
| 12 |
|
| 13 |
// double hermite(unsigned n, double x); |
| 14 |
// float hermite(unsigned n, float x); |
| 15 |
// long double hermite(unsigned n, long double x); |
| 16 |
// float hermitef(unsigned n, float x); |
| 17 |
// long double hermitel(unsigned n, long double x); |
| 18 |
// template <class Integer> |
| 19 |
// double hermite(unsigned n, Integer x); |
| 20 |
|
| 21 |
#include <array> |
| 22 |
#include <cassert> |
| 23 |
#include <cmath> |
| 24 |
#include <limits> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "type_algorithms.h" |
| 28 |
|
| 29 |
inline constexpr unsigned g_max_n = 128; |
| 30 |
|
| 31 |
template <class T> |
| 32 |
std::array<T, 11> sample_points() { |
| 33 |
return {-12.34, -7.42, -1.0, -0.5, -0.1, 0.0, 0.1, 0.5, 1.0, 5.67, 15.67}; |
| 34 |
} |
| 35 |
|
| 36 |
template <class Real> |
| 37 |
class CompareFloatingValues { |
| 38 |
private: |
| 39 |
Real abs_tol; |
| 40 |
Real rel_tol; |
| 41 |
|
| 42 |
public: |
| 43 |
CompareFloatingValues() { |
| 44 |
abs_tol = []() -> Real { |
| 45 |
if (std::is_same_v<Real, float>) |
| 46 |
return 1e-5f; |
| 47 |
else if (std::is_same_v<Real, double>) |
| 48 |
return 1e-11; |
| 49 |
else |
| 50 |
return 1e-12l; |
| 51 |
}(); |
| 52 |
|
| 53 |
rel_tol = abs_tol; |
| 54 |
} |
| 55 |
|
| 56 |
bool operator()(Real result, Real expected) const { |
| 57 |
if (std::isinf(expected) && std::isinf(result)) |
| 58 |
return result == expected; |
| 59 |
|
| 60 |
if (std::isnan(expected) || std::isnan(result)) |
| 61 |
return false; |
| 62 |
|
| 63 |
Real tol = abs_tol + std::abs(expected) * rel_tol; |
| 64 |
return std::abs(result - expected) < tol; |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
// Roots are taken from |
| 69 |
// Salzer, Herbert E., Ruth Zucker, and Ruth Capuano. |
| 70 |
// Table of the zeros and weight factors of the first twenty Hermite |
| 71 |
// polynomials. US Government Printing Office, 1952. |
| 72 |
template <class T> |
| 73 |
std::vector<T> get_roots(unsigned n) { |
| 74 |
switch (n) { |
| 75 |
case 0: |
| 76 |
return {}; |
| 77 |
case 1: |
| 78 |
return {T(0)}; |
| 79 |
case 2: |
| 80 |
return {T(0.707106781186548)}; |
| 81 |
case 3: |
| 82 |
return {T(0), T(1.224744871391589)}; |
| 83 |
case 4: |
| 84 |
return {T(0.524647623275290), T(1.650680123885785)}; |
| 85 |
case 5: |
| 86 |
return {T(0), T(0.958572464613819), T(2.020182870456086)}; |
| 87 |
case 6: |
| 88 |
return {T(0.436077411927617), T(1.335849074013697), T(2.350604973674492)}; |
| 89 |
case 7: |
| 90 |
return {T(0), T(0.816287882858965), T(1.673551628767471), T(2.651961356835233)}; |
| 91 |
case 8: |
| 92 |
return {T(0.381186990207322), T(1.157193712446780), T(1.981656756695843), T(2.930637420257244)}; |
| 93 |
case 9: |
| 94 |
return {T(0), T(0.723551018752838), T(1.468553289216668), T(2.266580584531843), T(3.190993201781528)}; |
| 95 |
case 10: |
| 96 |
return { |
| 97 |
T(0.342901327223705), T(1.036610829789514), T(1.756683649299882), T(2.532731674232790), T(3.436159118837738)}; |
| 98 |
case 11: |
| 99 |
return {T(0), |
| 100 |
T(0.65680956682100), |
| 101 |
T(1.326557084494933), |
| 102 |
T(2.025948015825755), |
| 103 |
T(2.783290099781652), |
| 104 |
T(3.668470846559583)}; |
| 105 |
|
| 106 |
case 12: |
| 107 |
return {T(0.314240376254359), |
| 108 |
T(0.947788391240164), |
| 109 |
T(1.597682635152605), |
| 110 |
T(2.279507080501060), |
| 111 |
T(3.020637025120890), |
| 112 |
T(3.889724897869782)}; |
| 113 |
|
| 114 |
case 13: |
| 115 |
return {T(0), |
| 116 |
T(0.605763879171060), |
| 117 |
T(1.220055036590748), |
| 118 |
T(1.853107651601512), |
| 119 |
T(2.519735685678238), |
| 120 |
T(3.246608978372410), |
| 121 |
T(4.101337596178640)}; |
| 122 |
|
| 123 |
case 14: |
| 124 |
return {T(0.29174551067256), |
| 125 |
T(0.87871378732940), |
| 126 |
T(1.47668273114114), |
| 127 |
T(2.09518325850772), |
| 128 |
T(2.74847072498540), |
| 129 |
T(3.46265693360227), |
| 130 |
T(4.30444857047363)}; |
| 131 |
|
| 132 |
case 15: |
| 133 |
return {T(0.00000000000000), |
| 134 |
T(0.56506958325558), |
| 135 |
T(1.13611558521092), |
| 136 |
T(1.71999257518649), |
| 137 |
T(2.32573248617386), |
| 138 |
T(2.96716692790560), |
| 139 |
T(3.66995037340445), |
| 140 |
T(4.49999070730939)}; |
| 141 |
|
| 142 |
case 16: |
| 143 |
return {T(0.27348104613815), |
| 144 |
T(0.82295144914466), |
| 145 |
T(1.38025853919888), |
| 146 |
T(1.95178799091625), |
| 147 |
T(2.54620215784748), |
| 148 |
T(3.17699916197996), |
| 149 |
T(3.86944790486012), |
| 150 |
T(4.68873893930582)}; |
| 151 |
|
| 152 |
case 17: |
| 153 |
return {T(0), |
| 154 |
T(0.5316330013427), |
| 155 |
T(1.0676487257435), |
| 156 |
T(1.6129243142212), |
| 157 |
T(2.1735028266666), |
| 158 |
T(2.7577629157039), |
| 159 |
T(3.3789320911415), |
| 160 |
T(4.0619466758755), |
| 161 |
T(4.8713451936744)}; |
| 162 |
|
| 163 |
case 18: |
| 164 |
return {T(0.2582677505191), |
| 165 |
T(0.7766829192674), |
| 166 |
T(1.3009208583896), |
| 167 |
T(1.8355316042616), |
| 168 |
T(2.3862990891667), |
| 169 |
T(2.9613775055316), |
| 170 |
T(3.5737690684863), |
| 171 |
T(4.2481178735681), |
| 172 |
T(5.0483640088745)}; |
| 173 |
|
| 174 |
case 19: |
| 175 |
return {T(0), |
| 176 |
T(0.5035201634239), |
| 177 |
T(1.0103683871343), |
| 178 |
T(1.5241706193935), |
| 179 |
T(2.0492317098506), |
| 180 |
T(2.5911337897945), |
| 181 |
T(3.1578488183476), |
| 182 |
T(3.7621873519640), |
| 183 |
T(4.4285328066038), |
| 184 |
T(5.2202716905375)}; |
| 185 |
|
| 186 |
case 20: |
| 187 |
return {T(0.2453407083009), |
| 188 |
T(0.7374737285454), |
| 189 |
T(1.2340762153953), |
| 190 |
T(1.7385377121166), |
| 191 |
T(2.2549740020893), |
| 192 |
T(2.7888060584281), |
| 193 |
T(3.347854567332), |
| 194 |
T(3.9447640401156), |
| 195 |
T(4.6036824495507), |
| 196 |
T(5.3874808900112)}; |
| 197 |
|
| 198 |
default: // polynom degree n>20 is unsupported |
| 199 |
assert(false); |
| 200 |
return {T(-42)}; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
template <class Real> |
| 205 |
void test() { |
| 206 |
{ // checks if NaNs are reported correctly (i.e. output == input for input == NaN) |
| 207 |
using nl = std::numeric_limits<Real>; |
| 208 |
for (Real NaN : {nl::quiet_NaN(), nl::signaling_NaN()}) |
| 209 |
for (unsigned n = 0; n < g_max_n; ++n) |
| 210 |
assert(std::isnan(std::hermite(n, NaN))); |
| 211 |
} |
| 212 |
|
| 213 |
{ // simple sample points for n=0..127 should not produce NaNs. |
| 214 |
for (Real x : sample_points<Real>()) |
| 215 |
for (unsigned n = 0; n < g_max_n; ++n) |
| 216 |
assert(!std::isnan(std::hermite(n, x))); |
| 217 |
} |
| 218 |
|
| 219 |
{ // checks std::hermite(n, x) for n=0..5 against analytic polynoms |
| 220 |
const auto h0 = [](Real) -> Real { return 1; }; |
| 221 |
const auto h1 = [](Real y) -> Real { return 2 * y; }; |
| 222 |
const auto h2 = [](Real y) -> Real { return 4 * y * y - 2; }; |
| 223 |
const auto h3 = [](Real y) -> Real { return y * (8 * y * y - 12); }; |
| 224 |
const auto h4 = [](Real y) -> Real { return (16 * std::pow(y, 4) - 48 * y * y + 12); }; |
| 225 |
const auto h5 = [](Real y) -> Real { return y * (32 * std::pow(y, 4) - 160 * y * y + 120); }; |
| 226 |
|
| 227 |
for (Real x : sample_points<Real>()) { |
| 228 |
const CompareFloatingValues<Real> compare; |
| 229 |
assert(compare(std::hermite(0, x), h0(x))); |
| 230 |
assert(compare(std::hermite(1, x), h1(x))); |
| 231 |
assert(compare(std::hermite(2, x), h2(x))); |
| 232 |
assert(compare(std::hermite(3, x), h3(x))); |
| 233 |
assert(compare(std::hermite(4, x), h4(x))); |
| 234 |
assert(compare(std::hermite(5, x), h5(x))); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
{ // checks std::hermitef for bitwise equality with std::hermite(unsigned, float) |
| 239 |
if constexpr (std::is_same_v<Real, float>) |
| 240 |
for (unsigned n = 0; n < g_max_n; ++n) |
| 241 |
for (float x : sample_points<float>()) |
| 242 |
assert(std::hermite(n, x) == std::hermitef(n, x)); |
| 243 |
} |
| 244 |
|
| 245 |
{ // checks std::hermitel for bitwise equality with std::hermite(unsigned, long double) |
| 246 |
if constexpr (std::is_same_v<Real, long double>) |
| 247 |
for (unsigned n = 0; n < g_max_n; ++n) |
| 248 |
for (long double x : sample_points<long double>()) |
| 249 |
assert(std::hermite(n, x) == std::hermitel(n, x)); |
| 250 |
} |
| 251 |
|
| 252 |
{ // Checks if the characteristic recurrence relation holds: H_{n+1}(x) = 2x H_n(x) - 2n H_{n-1}(x) |
| 253 |
for (Real x : sample_points<Real>()) { |
| 254 |
for (unsigned n = 1; n < g_max_n - 1; ++n) { |
| 255 |
Real H_next = std::hermite(n + 1, x); |
| 256 |
Real H_next_recurrence = 2 * (x * std::hermite(n, x) - n * std::hermite(n - 1, x)); |
| 257 |
|
| 258 |
if (std::isinf(H_next)) |
| 259 |
break; |
| 260 |
const CompareFloatingValues<Real> compare; |
| 261 |
assert(compare(H_next, H_next_recurrence)); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
{ // sanity checks: hermite polynoms need to change signs at (simple) roots. checked upto order n<=20. |
| 267 |
|
| 268 |
// root tolerance: must be smaller than the smallest difference between adjacent roots |
| 269 |
Real tol = []() -> Real { |
| 270 |
if (std::is_same_v<Real, float>) |
| 271 |
return 1e-5f; |
| 272 |
else if (std::is_same_v<Real, double>) |
| 273 |
return 1e-9; |
| 274 |
else |
| 275 |
return 1e-10l; |
| 276 |
}(); |
| 277 |
|
| 278 |
const auto is_sign_change = [tol](unsigned n, Real x) -> bool { |
| 279 |
return std::hermite(n, x - tol) * std::hermite(n, x + tol) < 0; |
| 280 |
}; |
| 281 |
|
| 282 |
for (unsigned n = 0; n <= 20u; ++n) { |
| 283 |
for (Real x : get_roots<Real>(n)) { |
| 284 |
// the roots are symmetric: if x is a root, so is -x |
| 285 |
if (x > 0) |
| 286 |
assert(is_sign_change(n, -x)); |
| 287 |
assert(is_sign_change(n, x)); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
{ // check input infinity is handled correctly |
| 293 |
Real inf = std::numeric_limits<Real>::infinity(); |
| 294 |
for (unsigned n = 1; n < g_max_n; ++n) { |
| 295 |
assert(std::hermite(n, +inf) == inf); |
| 296 |
assert(std::hermite(n, -inf) == ((n & 1) ? -inf : inf)); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
{ // check: if overflow occurs that it is mapped to the correct infinity |
| 301 |
if constexpr (std::is_same_v<Real, double>) { |
| 302 |
// Q: Why only double? |
| 303 |
// A: The numeric values (e.g. overflow threshold `n`) below are different for other types. |
| 304 |
static_assert(sizeof(double) == 8); |
| 305 |
for (unsigned n = 0; n < g_max_n; ++n) { |
| 306 |
// Q: Why n=111 and x=300? |
| 307 |
// A: Both are chosen s.t. the first overlow occurs for some `n<g_max_n`. |
| 308 |
if (n < 111) { |
| 309 |
assert(std::isfinite(std::hermite(n, +300.0))); |
| 310 |
assert(std::isfinite(std::hermite(n, -300.0))); |
| 311 |
} else { |
| 312 |
double inf = std::numeric_limits<double>::infinity(); |
| 313 |
assert(std::hermite(n, +300.0) == inf); |
| 314 |
assert(std::hermite(n, -300.0) == ((n & 1) ? -inf : inf)); |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
struct TestFloat { |
| 322 |
template <class Real> |
| 323 |
void operator()() { |
| 324 |
test<Real>(); |
| 325 |
} |
| 326 |
}; |
| 327 |
|
| 328 |
struct TestInt { |
| 329 |
template <class Integer> |
| 330 |
void operator()() { |
| 331 |
// checks that std::hermite(unsigned, Integer) actually wraps std::hermite(unsigned, double) |
| 332 |
for (unsigned n = 0; n < g_max_n; ++n) |
| 333 |
for (Integer x : {-42, -7, -5, -1, 0, 1, 5, 7, 42}) |
| 334 |
assert(std::hermite(n, x) == std::hermite(n, static_cast<double>(x))); |
| 335 |
} |
| 336 |
}; |
| 337 |
|
| 338 |
int main() { |
| 339 |
types::for_each(types::floating_point_types(), TestFloat()); |
| 340 |
types::for_each(types::type_list<short, int, long, long long>(), TestInt()); |
| 341 |
} |