./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
38 / 38
100.0%
Branches:
26 / 29
89.7%
Functions:
3 / 3
100.0%

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
1/2
✓ Branch 0 taken 112978119 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 110758687 times.
✓ Branch 1 taken 2219432 times.
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
2/2
✓ Branch 0 taken 1838661 times.
✓ Branch 1 taken 3 times.
1838664 double sum = nm1 != 0 ? 1.0 / nm1 : -std::log(x) - kEulerGamma;
45 double factor = 1.0;
46
1/2
✓ Branch 0 taken 28456624 times.
✗ Branch 1 not taken.
28456624 for (int i = 1; i <= kMaxIterations; ++i) {
47 28456624 factor *= -x / i;
48 28456624 double delta = 0.0;
49
2/2
✓ Branch 0 taken 1838661 times.
✓ Branch 1 taken 26617963 times.
28456624 if (i != nm1) {
50 26617963 delta = -factor / (i - nm1);
51 } else {
52 double psi = -kEulerGamma;
53
2/2
✓ Branch 0 taken 1838662 times.
✓ Branch 1 taken 1838661 times.
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
2/2
✓ Branch 0 taken 26617960 times.
✓ Branch 1 taken 1838664 times.
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
7/8
✓ Branch 0 taken 4058100 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 4058100 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4058099 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 4058098 times.
4058101 if (n < 0 || !(std::isfinite(x)) || x < 0.0 || (x == 0.0 && n <= 1)) {
70
1/1
✓ Branch 2 taken 3 times.
3 throw std::domain_error("The exponential integral E_n(x) needs n >= 0 and x > 0");
71 }
72
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4058097 times.
4058098 if (n == 0) {
73 1 return std::exp(-x) / x;
74 }
75
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4058096 times.
4058097 if (x == 0.0) {
76 1 return 1.0 / (n - 1);
77 }
78
2/2
✓ Branch 0 taken 2219432 times.
✓ Branch 1 taken 1838664 times.
4058096 return x > 1.0 ? ExponentialIntegralFraction(n, x) : ExponentialIntegralSeries(n, x);
79 }
80
81 } // namespace terraingen
82