| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kutergin_a_multidim_trapezoid/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <utility> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "kutergin_a_multidim_trapezoid/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace kutergin_a_multidim_trapezoid { | ||
| 11 | namespace { | ||
| 12 | |||
| 13 | bool ValidateBorders(const std::vector<std::pair<double, double>> &borders) { | ||
| 14 | return std::ranges::all_of( | ||
| 15 | ✗ | borders, [](const auto &p) { return std::isfinite(p.first) && std::isfinite(p.second) && (p.first < p.second); }); | |
| 16 | } | ||
| 17 | |||
| 18 | bool NextIndex(std::vector<int> &idx, int dim, int max_index) { | ||
| 19 | ✗ | for (int pos = 0; pos < dim; ++pos) { | |
| 20 | ✗ | ++idx[pos]; | |
| 21 | ✗ | if (idx[pos] <= max_index) { | |
| 22 | return true; | ||
| 23 | } | ||
| 24 | ✗ | idx[pos] = 0; | |
| 25 | } | ||
| 26 | return false; | ||
| 27 | } | ||
| 28 | |||
| 29 | } // namespace | ||
| 30 | |||
| 31 | ✗ | KuterginAMultidimTrapezoidSEQ::KuterginAMultidimTrapezoidSEQ(const InType &in) { | |
| 32 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 33 | GetInput() = in; | ||
| 34 | ✗ | GetOutput() = 0.0; | |
| 35 | ✗ | } | |
| 36 | |||
| 37 | ✗ | bool KuterginAMultidimTrapezoidSEQ::ValidationImpl() { | |
| 38 | const auto &[func, borders, n] = GetInput(); | ||
| 39 | |||
| 40 | ✗ | if (!func) { | |
| 41 | return false; | ||
| 42 | } | ||
| 43 | ✗ | if (n <= 0) { | |
| 44 | return false; | ||
| 45 | } | ||
| 46 | ✗ | if (borders.empty()) { | |
| 47 | return false; | ||
| 48 | } | ||
| 49 | |||
| 50 | return ValidateBorders(borders); | ||
| 51 | } | ||
| 52 | |||
| 53 | ✗ | bool KuterginAMultidimTrapezoidSEQ::PreProcessingImpl() { | |
| 54 | ✗ | GetOutput() = 0.0; | |
| 55 | ✗ | return true; | |
| 56 | } | ||
| 57 | |||
| 58 | ✗ | bool KuterginAMultidimTrapezoidSEQ::RunImpl() { | |
| 59 | const auto &[func, borders, n] = GetInput(); | ||
| 60 | ✗ | const int dim = static_cast<int>(borders.size()); | |
| 61 | |||
| 62 | ✗ | std::vector<double> h(dim); | |
| 63 | double cell_volume = 1.0; | ||
| 64 | |||
| 65 | ✗ | for (int i = 0; i < dim; ++i) { | |
| 66 | ✗ | const double left = borders[i].first; | |
| 67 | ✗ | const double right = borders[i].second; | |
| 68 | ✗ | h[i] = (right - left) / n; | |
| 69 | ✗ | cell_volume *= h[i]; | |
| 70 | } | ||
| 71 | |||
| 72 | ✗ | const int max_index = n; | |
| 73 | |||
| 74 | ✗ | std::vector<int> idx(dim, 0); | |
| 75 | ✗ | std::vector<double> point(dim); | |
| 76 | |||
| 77 | double sum = 0.0; | ||
| 78 | |||
| 79 | while (true) { | ||
| 80 | double weight = 1.0; | ||
| 81 | |||
| 82 | ✗ | for (int i = 0; i < dim; ++i) { | |
| 83 | ✗ | point[i] = (borders[i].first + (idx[i] * h[i])); | |
| 84 | |||
| 85 | ✗ | if ((idx[i] == 0) || (idx[i] == n)) { | |
| 86 | ✗ | weight *= 0.5; | |
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | double f_val = func(point); | ||
| 91 | ✗ | if (!std::isfinite(f_val)) { | |
| 92 | return false; | ||
| 93 | } | ||
| 94 | |||
| 95 | ✗ | sum += weight * f_val; | |
| 96 | |||
| 97 | ✗ | if (!NextIndex(idx, dim, max_index)) { | |
| 98 | break; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | ✗ | GetOutput() = sum * cell_volume; | |
| 103 | ✗ | return std::isfinite(GetOutput()); | |
| 104 | } | ||
| 105 | |||
| 106 | ✗ | bool KuterginAMultidimTrapezoidSEQ::PostProcessingImpl() { | |
| 107 | ✗ | return std::isfinite(GetOutput()); | |
| 108 | } | ||
| 109 | |||
| 110 | } // namespace kutergin_a_multidim_trapezoid | ||
| 111 |