| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "dergachev_a_simple_iteration_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <vector> | ||
| 5 | |||
| 6 | #include "dergachev_a_simple_iteration_method/common/include/common.hpp" | ||
| 7 | |||
| 8 | namespace dergachev_a_simple_iteration_method { | ||
| 9 | |||
| 10 | 80 | DergachevASimpleIterationMethodSEQ::DergachevASimpleIterationMethodSEQ(const InType &in) { | |
| 11 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 12 | 80 | GetInput() = in; | |
| 13 | GetOutput() = 0; | ||
| 14 | 80 | } | |
| 15 | |||
| 16 | 80 | bool DergachevASimpleIterationMethodSEQ::ValidationImpl() { | |
| 17 |
2/4✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
|
80 | return (GetInput() > 0) && (GetOutput() == 0); |
| 18 | } | ||
| 19 | |||
| 20 | 80 | bool DergachevASimpleIterationMethodSEQ::PreProcessingImpl() { | |
| 21 | 80 | GetOutput() = 0; | |
| 22 | 80 | return true; | |
| 23 | } | ||
| 24 | |||
| 25 | 80 | bool DergachevASimpleIterationMethodSEQ::RunImpl() { | |
| 26 | 80 | int n = GetInput(); | |
| 27 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (n <= 0) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 | |||
| 31 | 80 | std::vector<double> x(n, 0.0); | |
| 32 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<double> x_new(n, 0.0); |
| 33 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<double> b(n, 1.0); |
| 34 |
2/6✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
80 | std::vector<std::vector<double>> a(n, std::vector<double>(n, 0.0)); |
| 35 | |||
| 36 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (int i = 0; i < n; i++) { |
| 37 | 1144 | a[i][i] = 1.0; | |
| 38 | } | ||
| 39 | |||
| 40 | const double tau = 0.5; | ||
| 41 | const double epsilon = 1e-6; | ||
| 42 | const int max_iterations = 1000; | ||
| 43 | int iteration = 0; | ||
| 44 | |||
| 45 |
1/2✓ Branch 0 taken 1752 times.
✗ Branch 1 not taken.
|
1752 | for (; iteration < max_iterations; iteration++) { |
| 46 |
2/2✓ Branch 0 taken 25912 times.
✓ Branch 1 taken 1752 times.
|
27664 | for (int i = 0; i < n; i++) { |
| 47 | double ax_i = 0.0; | ||
| 48 |
2/2✓ Branch 0 taken 771768 times.
✓ Branch 1 taken 25912 times.
|
797680 | for (int j = 0; j < n; j++) { |
| 49 | 771768 | ax_i += a[i][j] * x[j]; | |
| 50 | } | ||
| 51 | 25912 | x_new[i] = x[i] - (tau * (ax_i - b[i])); | |
| 52 | } | ||
| 53 | |||
| 54 | double diff = 0.0; | ||
| 55 |
2/2✓ Branch 0 taken 25912 times.
✓ Branch 1 taken 1752 times.
|
27664 | for (int i = 0; i < n; i++) { |
| 56 | 25912 | double d = x_new[i] - x[i]; | |
| 57 | 25912 | diff += d * d; | |
| 58 | } | ||
| 59 | 1752 | diff = std::sqrt(diff); | |
| 60 | |||
| 61 |
1/2✓ Branch 1 taken 1752 times.
✗ Branch 2 not taken.
|
1752 | x = x_new; |
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 1672 times.
✓ Branch 1 taken 80 times.
|
1752 | if (diff < epsilon) { |
| 64 | break; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | double sum = 0.0; | ||
| 69 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (int i = 0; i < n; i++) { |
| 70 | 1144 | sum += x[i]; | |
| 71 | } | ||
| 72 | |||
| 73 | 80 | GetOutput() = static_cast<int>(std::round(sum)); | |
| 74 | return true; | ||
| 75 | 80 | } | |
| 76 | |||
| 77 | 80 | bool DergachevASimpleIterationMethodSEQ::PostProcessingImpl() { | |
| 78 | 80 | return GetOutput() > 0; | |
| 79 | } | ||
| 80 | |||
| 81 | } // namespace dergachev_a_simple_iteration_method | ||
| 82 |