src/util/special_functions.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "terraingen/util/special_functions.h" | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <limits> | ||
| 7 | #include <stdexcept> | ||
| 8 | |||
| 9 | namespace terraingen { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | /// The Euler-Mascheroni constant. | ||
| 14 | constexpr double kEulerGamma = 0.577215664901532860606512090082402431; | ||
| 15 | |||
| 16 | constexpr int kMaxIterations = 200; | ||
| 17 | constexpr double kEpsilon = std::numeric_limits<double>::epsilon(); | ||
| 18 | |||
| 19 | /// E_n(x) for x > 1 by its continued fraction, evaluated with Lentz's method. | ||
| 20 | 2219432 | double ExponentialIntegralFraction(int n, double x) { | |
| 21 | 2219432 | constexpr double kTiny = std::numeric_limits<double>::min() / kEpsilon; | |
| 22 | 2219432 | const int nm1 = n - 1; | |
| 23 | 2219432 | double b = x + n; | |
| 24 | 2219432 | double c = 1.0 / kTiny; | |
| 25 | 2219432 | double d = 1.0 / b; | |
| 26 | 2219432 | double h = d; | |
| 27 | 112978119 | for (int i = 1; i <= kMaxIterations; ++i) { | |
| 28 | 112978119 | const double a = -static_cast<double>(i) * (nm1 + i); | |
| 29 | 112978119 | b += 2.0; | |
| 30 | 112978119 | d = 1.0 / (a * d + b); | |
| 31 | 112978119 | c = b + a / c; | |
| 32 | 112978119 | const double delta = c * d; | |
| 33 | 112978119 | h *= delta; | |
| 34 | 112978119 | if (std::fabs(delta - 1.0) <= kEpsilon) { | |
| 35 | break; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | 2219432 | return h * std::exp(-x); | |
| 39 | } | ||
| 40 | |||
| 41 | /// E_n(x) for 0 < x <= 1 by its power series. | ||
| 42 | 1838664 | double ExponentialIntegralSeries(int n, double x) { | |
| 43 | 1838664 | const int nm1 = n - 1; | |
| 44 | 1838664 | double sum = nm1 != 0 ? 1.0 / nm1 : -std::log(x) - kEulerGamma; | |
| 45 | double factor = 1.0; | ||
| 46 | 28456624 | for (int i = 1; i <= kMaxIterations; ++i) { | |
| 47 | 28456624 | factor *= -x / i; | |
| 48 | 28456624 | double delta = 0.0; | |
| 49 | 28456624 | if (i != nm1) { | |
| 50 | 26617963 | delta = -factor / (i - nm1); | |
| 51 | } else { | ||
| 52 | double psi = -kEulerGamma; | ||
| 53 | 3677323 | for (int k = 1; k <= nm1; ++k) { | |
| 54 | 1838662 | psi += 1.0 / k; | |
| 55 | } | ||
| 56 | 1838661 | delta = factor * (-std::log(x) + psi); | |
| 57 | } | ||
| 58 | 28456624 | sum += delta; | |
| 59 | 28456624 | if (std::fabs(delta) <= std::fabs(sum) * kEpsilon) { | |
| 60 | break; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | 1838664 | return sum; | |
| 64 | } | ||
| 65 | |||
| 66 | } // namespace | ||
| 67 | |||
| 68 | 4058101 | double ExponentialIntegral(int n, double x) { | |
| 69 | 4058101 | if (n < 0 || !(std::isfinite(x)) || x < 0.0 || (x == 0.0 && n <= 1)) { | |
| 70 | 3 | throw std::domain_error("The exponential integral E_n(x) needs n >= 0 and x > 0"); | |
| 71 | } | ||
| 72 | 4058098 | if (n == 0) { | |
| 73 | 1 | return std::exp(-x) / x; | |
| 74 | } | ||
| 75 | 4058097 | if (x == 0.0) { | |
| 76 | 1 | return 1.0 / (n - 1); | |
| 77 | } | ||
| 78 | 4058096 | return x > 1.0 ? ExponentialIntegralFraction(n, x) : ExponentialIntegralSeries(n, x); | |
| 79 | } | ||
| 80 | |||
| 81 | } // namespace terraingen | ||
| 82 |