| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gonozov_l_simple_iteration_method/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <tuple> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "gonozov_l_simple_iteration_method/common/include/common.hpp" | ||
| 8 | // #include "util/include/util.hpp" | ||
| 9 | |||
| 10 | namespace gonozov_l_simple_iteration_method { | ||
| 11 | |||
| 12 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GonozovLSimpleIterationMethodSEQ::GonozovLSimpleIterationMethodSEQ(const InType &in) |
| 13 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | : number_unknowns_(static_cast<int>(std::get<0>(in))) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 |
2/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
|
24 | std::vector g_outp(number_unknowns_, 0.0); |
| 17 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetOutput() = g_outp; |
| 18 | 24 | } | |
| 19 | |||
| 20 | 24 | bool GonozovLSimpleIterationMethodSEQ::ValidationImpl() { | |
| 21 | // д.б. |a11| > |a12|+|a13|, |a22| > |a21|+|a23|, |a33| > |a31|+|a32| | ||
| 22 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
|
120 | for (int i = 0; i < number_unknowns_; i++) { |
| 23 | double sum = 0.0; | ||
| 24 |
2/2✓ Branch 0 taken 400 times.
✓ Branch 1 taken 96 times.
|
496 | for (int j = 0; j < number_unknowns_; j++) { |
| 25 |
2/2✓ Branch 0 taken 304 times.
✓ Branch 1 taken 96 times.
|
400 | if (j != i) { |
| 26 | 304 | sum += std::get<1>(GetInput())[(i * number_unknowns_) + j]; | |
| 27 | } | ||
| 28 | } | ||
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 | if (std::get<1>(GetInput())[(i * number_unknowns_) + i] < sum) { |
| 30 | return false; | ||
| 31 | } | ||
| 32 | } | ||
| 33 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | return (static_cast<int>(std::get<0>(GetInput())) > 0) && (static_cast<int>(std::get<1>(GetInput()).size()) > 0) && |
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | (static_cast<int>(std::get<2>(GetInput()).size()) > 0); |
| 35 | } | ||
| 36 | |||
| 37 | 24 | bool GonozovLSimpleIterationMethodSEQ::PreProcessingImpl() { | |
| 38 | 24 | return true; | |
| 39 | } | ||
| 40 | |||
| 41 | namespace { | ||
| 42 | 1312 | void CalculatingNewApproximations(std::vector<double> &matrix, std::vector<double> &previous_approximations, | |
| 43 | std::vector<double> ¤t_approximations, int number_unknowns, | ||
| 44 | std::vector<double> &b) { | ||
| 45 |
2/2✓ Branch 0 taken 5144 times.
✓ Branch 1 taken 1312 times.
|
6456 | for (int i = 0; i < number_unknowns; i++) { |
| 46 | double sum = 0.0; | ||
| 47 | // Суммируем все недиагональные элементы | ||
| 48 |
2/2✓ Branch 0 taken 20968 times.
✓ Branch 1 taken 5144 times.
|
26112 | for (int j = 0; j < number_unknowns; j++) { |
| 49 |
2/2✓ Branch 0 taken 15824 times.
✓ Branch 1 taken 5144 times.
|
20968 | if (j != i) { |
| 50 | 15824 | sum += matrix[(i * number_unknowns) + j] * previous_approximations[j]; | |
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | // Строим новое приближение | ||
| 55 | 5144 | current_approximations[i] = (b[i] - sum) / matrix[(i * number_unknowns) + i]; | |
| 56 | } | ||
| 57 | 1312 | } | |
| 58 | |||
| 59 | int ConvergenceCheck(std::vector<double> ¤t_approximations, std::vector<double> &previous_approximations, | ||
| 60 | int number_unknowns) { | ||
| 61 | int converged = 0; | ||
| 62 |
2/2✓ Branch 0 taken 5144 times.
✓ Branch 1 taken 1312 times.
|
6456 | for (int i = 0; i < number_unknowns; i++) { |
| 63 |
2/2✓ Branch 0 taken 544 times.
✓ Branch 1 taken 4600 times.
|
5144 | double diff = fabs(current_approximations[i] - previous_approximations[i]); |
| 64 | 5144 | double norm = fabs(current_approximations[i]); | |
| 65 |
2/2✓ Branch 0 taken 544 times.
✓ Branch 1 taken 4600 times.
|
5144 | if (diff < 0.00001 * (norm + 1e-10)) { |
| 66 | 544 | converged++; | |
| 67 | } | ||
| 68 | } | ||
| 69 | return converged; | ||
| 70 | } | ||
| 71 | } // namespace | ||
| 72 | 24 | bool GonozovLSimpleIterationMethodSEQ::RunImpl() { | |
| 73 | int max_number_iteration = 10000; | ||
| 74 | |||
| 75 | 24 | std::vector<double> matrix = std::get<1>(GetInput()); | |
| 76 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<double> b = std::get<2>(GetInput()); |
| 77 | |||
| 78 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<double> previous_approximations(number_unknowns_, 0.0); |
| 79 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<double> current_approximations(number_unknowns_, 0.0); |
| 80 | |||
| 81 | // Нулевое приближение | ||
| 82 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
|
120 | for (int i = 0; i < number_unknowns_; i++) { |
| 83 | 96 | previous_approximations[i] = b[i] / matrix[(i * number_unknowns_) + i]; | |
| 84 | } | ||
| 85 | |||
| 86 | // Основной цикл | ||
| 87 |
1/2✓ Branch 0 taken 1312 times.
✗ Branch 1 not taken.
|
1312 | for (int iter = 0; iter < max_number_iteration; iter++) { |
| 88 | // Для каждой переменной вычисляем новое приближение | ||
| 89 | 1312 | CalculatingNewApproximations(matrix, previous_approximations, current_approximations, number_unknowns_, b); | |
| 90 | |||
| 91 | // Проверка сходимости | ||
| 92 | int converged = ConvergenceCheck(current_approximations, previous_approximations, number_unknowns_); | ||
| 93 | |||
| 94 | // Если все переменные сошлись | ||
| 95 |
2/2✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 24 times.
|
1312 | if (converged == number_unknowns_) { |
| 96 | break; | ||
| 97 | } | ||
| 98 | |||
| 99 |
1/2✓ Branch 1 taken 1288 times.
✗ Branch 2 not taken.
|
1288 | previous_approximations = current_approximations; |
| 100 | } | ||
| 101 | |||
| 102 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
|
120 | for (int i = 0; i < number_unknowns_; i++) { |
| 103 | 96 | GetOutput()[i] = current_approximations[i]; | |
| 104 | } | ||
| 105 | |||
| 106 | 24 | return true; | |
| 107 | } | ||
| 108 | |||
| 109 | 24 | bool GonozovLSimpleIterationMethodSEQ::PostProcessingImpl() { | |
| 110 | 24 | return !GetOutput().empty(); | |
| 111 | } | ||
| 112 | |||
| 113 | } // namespace gonozov_l_simple_iteration_method | ||
| 114 |