| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "telnov_strongin_algorithm/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "telnov_strongin_algorithm/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace telnov_strongin_algorithm { | ||
| 11 | |||
| 12 | 24 | TelnovStronginAlgorithmSEQ::TelnovStronginAlgorithmSEQ(const InType &in) { | |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | 24 | GetInput() = in; | |
| 15 | GetOutput() = 0; | ||
| 16 | 24 | } | |
| 17 | |||
| 18 | 24 | bool TelnovStronginAlgorithmSEQ::ValidationImpl() { | |
| 19 | const auto &in = GetInput(); | ||
| 20 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | return (in.eps > 0.0) && (in.b > in.a); |
| 21 | } | ||
| 22 | |||
| 23 | 24 | bool TelnovStronginAlgorithmSEQ::PreProcessingImpl() { | |
| 24 | 24 | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | 24 | bool TelnovStronginAlgorithmSEQ::RunImpl() { | |
| 28 | const auto &in = GetInput(); | ||
| 29 | 24 | const double a = in.a; | |
| 30 | 24 | const double b = in.b; | |
| 31 | 24 | const double eps = in.eps; | |
| 32 | |||
| 33 | 2424 | auto f = [](double x) { return ((x - 1.0) * (x - 1.0)) + 1.0; }; | |
| 34 | |||
| 35 | 24 | std::vector<double> x_vals{a, b}; | |
| 36 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<double> f_vals{f(a), f(b)}; |
| 37 | |||
| 38 | const int k_max_iters = 100; | ||
| 39 | int iter = 0; | ||
| 40 | |||
| 41 |
3/4✓ Branch 0 taken 2424 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 2400 times.
|
2424 | while ((x_vals.back() - x_vals.front()) > eps && iter < k_max_iters) { |
| 42 | 2400 | ++iter; | |
| 43 | |||
| 44 | 2400 | double m = 0.0; | |
| 45 |
2/2✓ Branch 0 taken 121200 times.
✓ Branch 1 taken 2400 times.
|
123600 | for (std::size_t i = 1; i < x_vals.size(); ++i) { |
| 46 | 121200 | m = std::max(m, std::abs(f_vals[i] - f_vals[i - 1]) / (x_vals[i] - x_vals[i - 1])); | |
| 47 | } | ||
| 48 | |||
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2400 times.
|
2400 | if (m == 0.0) { |
| 50 | ✗ | m = 1.0; | |
| 51 | } | ||
| 52 | |||
| 53 | const double r = 2.0; | ||
| 54 | double max_r = -1e9; | ||
| 55 | std::size_t best_idx = 1; | ||
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 121200 times.
✓ Branch 1 taken 2400 times.
|
123600 | for (std::size_t i = 1; i < x_vals.size(); ++i) { |
| 58 | 121200 | const double dx = x_vals[i] - x_vals[i - 1]; | |
| 59 | 121200 | const double df = f_vals[i] - f_vals[i - 1]; | |
| 60 | 121200 | const double r_val = (r * dx) + ((df * df) / (r * dx)) - (2.0 * (f_vals[i] + f_vals[i - 1])); | |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 31560 times.
✓ Branch 1 taken 89640 times.
|
121200 | if (r_val > max_r) { |
| 63 | max_r = r_val; | ||
| 64 | best_idx = i; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | double new_x = | ||
| 69 | 2400 | (0.5 * (x_vals[best_idx] + x_vals[best_idx - 1])) - ((f_vals[best_idx] - f_vals[best_idx - 1]) / (2.0 * m)); | |
| 70 | |||
| 71 |
3/4✓ Branch 0 taken 2400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 2376 times.
|
2400 | if (new_x <= x_vals[best_idx - 1] || new_x >= x_vals[best_idx]) { |
| 72 | 24 | new_x = 0.5 * (x_vals[best_idx] + x_vals[best_idx - 1]); | |
| 73 | } | ||
| 74 | |||
| 75 |
1/2✓ Branch 1 taken 2400 times.
✗ Branch 2 not taken.
|
2400 | x_vals.insert(x_vals.begin() + static_cast<std::ptrdiff_t>(best_idx), new_x); |
| 76 |
1/4✓ Branch 1 taken 2400 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
2400 | f_vals.insert(f_vals.begin() + static_cast<std::ptrdiff_t>(best_idx), f(new_x)); |
| 77 | } | ||
| 78 | |||
| 79 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | GetOutput() = *std::ranges::min_element(f_vals); |
| 80 | 24 | return true; | |
| 81 | } | ||
| 82 | |||
| 83 | 24 | bool TelnovStronginAlgorithmSEQ::PostProcessingImpl() { | |
| 84 | 24 | return true; | |
| 85 | } | ||
| 86 | |||
| 87 | } // namespace telnov_strongin_algorithm | ||
| 88 |