./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
33 / 33
100.0%
Branches:
33 / 40
82.5%
Functions:
5 / 5
100.0%

src/distribution/log_polynomial.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/distribution/log_polynomial.h"
4
5 #include "util/checks.h"
6
7 #include <algorithm>
8 #include <cmath>
9 #include <stdexcept>
10 #include <utility>
11
12 namespace terraingen {
13
14 793 LogPolynomialSfd::LogPolynomialSfd(std::vector<double> coefficients, SfdKind kind,
15 793 DiameterRange range, double diameterUnit, double areaUnit)
16 793 : SizeFrequencyDistribution(kind, range), m_coefficients(std::move(coefficients)),
17
2/2
✓ Branch 1 taken 792 times.
✓ Branch 2 taken 1 time.
793 m_diameterUnit(diameterUnit), m_areaUnit(areaUnit) {
18
4/4
✓ Branch 0 taken 792 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 791 times.
793 if (m_coefficients.empty() || !std::all_of(m_coefficients.begin(), m_coefficients.end(),
19
8/14
✗ Branch 0 not taken.
✓ Branch 1 taken 2340 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2340 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2340 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2340 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 11 times.
✓ Branch 12 taken 1 time.
✓ Branch 13 taken 11 times.
2365 [](double value) { return std::isfinite(value); })) {
20
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A log polynomial needs finite coefficients");
21 }
22
3/4
✓ Branch 0 taken 791 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 789 times.
✓ Branch 3 taken 1 time.
1581 if (!IsPositive(m_diameterUnit) || !IsPositive(m_areaUnit)) {
23
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("Diameter and area units must be positive and finite");
24 }
25
4/4
✓ Branch 0 taken 788 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 787 times.
✓ Branch 3 taken 1 time.
789 if (!(range.min > 0.0 && std::isfinite(range.max))) {
26 2 throw std::invalid_argument(
27
1/1
✓ Branch 2 taken 2 times.
2 "A log polynomial needs a finite range with a positive minimum");
28 }
29 787 constexpr int kSamples = 4096;
30 787 const double low = std::log10(range.min / m_diameterUnit);
31 787 const double high = std::log10(range.max / m_diameterUnit);
32
2/2
✓ Branch 0 taken 3216147 times.
✓ Branch 1 taken 785 times.
3216932 for (int i = 0; i <= kSamples; ++i) {
33
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3216145 times.
3216147 if (Derivative(low + (high - low) * i / kSamples) > 0.0) {
34
1/1
✓ Branch 2 taken 2 times.
2 throw std::invalid_argument("A cumulative distribution cannot increase with diameter");
35 }
36 }
37 793 }
38
39 2051221 double LogPolynomialSfd::Polynomial(double x) const {
40 2051221 double sum = 0.0;
41
2/2
✓ Branch 0 taken 24584665 times.
✓ Branch 1 taken 2051221 times.
26635886 for (auto it = m_coefficients.rbegin(); it != m_coefficients.rend(); ++it) {
42 24584665 sum = sum * x + *it;
43 }
44 2051221 return sum;
45 }
46
47 3389322 double LogPolynomialSfd::Derivative(double x) const {
48 3389322 double sum = 0.0;
49
2/2
✓ Branch 0 taken 37075862 times.
✓ Branch 1 taken 3389322 times.
40465184 for (std::size_t i = m_coefficients.size() - 1; i > 0; --i) {
50 37075862 sum = sum * x + static_cast<double>(i) * m_coefficients[i];
51 }
52 3389322 return sum;
53 }
54
55 2050479 double LogPolynomialSfd::CumulativeAt(double diameter) const {
56 2050479 return std::pow(10.0, Polynomial(std::log10(diameter / m_diameterUnit))) / m_areaUnit;
57 }
58
59 173132 double LogPolynomialSfd::IncrementalAt(double diameter) const {
60 173132 return -CumulativeAt(diameter) * Derivative(std::log10(diameter / m_diameterUnit)) / diameter;
61 }
62
63 } // namespace terraingen
64