| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "samoylenko_i_simple_iter_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "samoylenko_i_simple_iter_method/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace samoylenko_i_simple_iter_method { | ||
| 11 | |||
| 12 | 80 | SamoylenkoISimpleIterMethodSEQ::SamoylenkoISimpleIterMethodSEQ(const InType &in) { | |
| 13 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 14 | 80 | GetInput() = in; | |
| 15 | GetOutput().clear(); | ||
| 16 | 80 | } | |
| 17 | |||
| 18 | 80 | bool SamoylenkoISimpleIterMethodSEQ::ValidationImpl() { | |
| 19 |
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().empty(); |
| 20 | } | ||
| 21 | |||
| 22 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | bool SamoylenkoISimpleIterMethodSEQ::PreProcessingImpl() { |
| 23 | GetOutput().clear(); | ||
| 24 | 80 | return true; | |
| 25 | } | ||
| 26 | |||
| 27 | namespace { | ||
| 28 | |||
| 29 | 80 | std::vector<double> BuildMatrix(size_t size) { | |
| 30 | 80 | std::vector<double> matrix(size * size, 0.0); | |
| 31 |
2/2✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 80 times.
|
1224 | for (size_t i = 0; i < size; ++i) { |
| 32 |
2/2✓ Branch 0 taken 1064 times.
✓ Branch 1 taken 80 times.
|
1144 | matrix[(i * size) + i] = 4.0; |
| 33 |
2/2✓ Branch 0 taken 1064 times.
✓ Branch 1 taken 80 times.
|
1144 | if (i > 0) { |
| 34 | 1064 | matrix[(i * size) + (i - 1)] = 1.0; | |
| 35 | } | ||
| 36 |
2/2✓ Branch 0 taken 1064 times.
✓ Branch 1 taken 80 times.
|
1144 | if (i + 1 < size) { |
| 37 | 1064 | matrix[(i * size) + (i + 1)] = 1.0; | |
| 38 | } | ||
| 39 | } | ||
| 40 | 80 | return matrix; | |
| 41 | } | ||
| 42 | |||
| 43 | 80 | void PerformIterations(size_t size, const std::vector<double> &matrix, const std::vector<double> &vector, | |
| 44 | std::vector<double> &x_new, std::vector<double> &x_old) { | ||
| 45 | const double tau = 0.2; | ||
| 46 | const double eps = 1e-7; | ||
| 47 | const int iters = 2000; | ||
| 48 | |||
| 49 |
1/2✓ Branch 0 taken 1440 times.
✗ Branch 1 not taken.
|
1440 | for (int it = 0; it < iters; ++it) { |
| 50 |
2/2✓ Branch 0 taken 23608 times.
✓ Branch 1 taken 1440 times.
|
25048 | for (size_t i = 0; i < size; ++i) { |
| 51 | double ax_i = 0.0; | ||
| 52 |
2/2✓ Branch 0 taken 706744 times.
✓ Branch 1 taken 23608 times.
|
730352 | for (size_t j = 0; j < size; ++j) { |
| 53 | 706744 | ax_i += matrix[(i * size) + j] * x_old[j]; | |
| 54 | } | ||
| 55 | 23608 | x_new[i] = x_old[i] - (tau * (ax_i - vector[i])); | |
| 56 | } | ||
| 57 | |||
| 58 | 1440 | double max_diff = 0.0; | |
| 59 |
2/2✓ Branch 0 taken 23608 times.
✓ Branch 1 taken 1440 times.
|
25048 | for (size_t i = 0; i < size; ++i) { |
| 60 | 23608 | max_diff = std::max(max_diff, std::fabs(x_new[i] - x_old[i])); | |
| 61 | } | ||
| 62 | |||
| 63 | x_old.swap(x_new); | ||
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 80 times.
|
1440 | if (max_diff < eps) { |
| 66 | break; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | 80 | } | |
| 70 | |||
| 71 | } // namespace | ||
| 72 | |||
| 73 | 80 | bool SamoylenkoISimpleIterMethodSEQ::RunImpl() { | |
| 74 | 80 | const int n = GetInput(); | |
| 75 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (n <= 0) { |
| 76 | return false; | ||
| 77 | } | ||
| 78 | |||
| 79 | 80 | auto size = static_cast<size_t>(n); | |
| 80 | |||
| 81 | 80 | std::vector<double> matrix = BuildMatrix(size); | |
| 82 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<double> vector(size, 1.0); |
| 83 | |||
| 84 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<double> x_old(size, 0.0); |
| 85 |
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(size); |
| 86 | |||
| 87 | 80 | PerformIterations(size, matrix, vector, x_new, x_old); | |
| 88 | |||
| 89 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | GetOutput() = x_old; |
| 90 | return true; | ||
| 91 | } | ||
| 92 | |||
| 93 | 80 | bool SamoylenkoISimpleIterMethodSEQ::PostProcessingImpl() { | |
| 94 | 80 | return !GetOutput().empty(); | |
| 95 | } | ||
| 96 | |||
| 97 | } // namespace samoylenko_i_simple_iter_method | ||
| 98 |