| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "../include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "../../common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace shekhirev_v_cg_method_seq { | ||
| 10 | |||
| 11 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | ConjugateGradientSeq::ConjugateGradientSeq(const shekhirev_v_cg_method::InType &in) { |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 | GetInput() = in; | ||
| 14 | 24 | } | |
| 15 | |||
| 16 | 24 | bool ConjugateGradientSeq::ValidationImpl() { | |
| 17 | 24 | const int n = GetInput().n; | |
| 18 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | return n > 0 && GetInput().A.size() == static_cast<size_t>(n) * n && GetInput().b.size() == static_cast<size_t>(n); |
| 19 | } | ||
| 20 | |||
| 21 | 24 | bool ConjugateGradientSeq::PreProcessingImpl() { | |
| 22 | 24 | return true; | |
| 23 | } | ||
| 24 | |||
| 25 | 48 | std::vector<double> ConjugateGradientSeq::MultiplyMatrixVector(const std::vector<double> &matrix, | |
| 26 | const std::vector<double> &vec, int n) { | ||
| 27 | 48 | std::vector<double> result(n, 0.0); | |
| 28 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (int i = 0; i < n; ++i) { |
| 29 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 120 times.
|
432 | for (int j = 0; j < n; ++j) { |
| 30 | 312 | size_t index = (static_cast<size_t>(i) * n) + j; | |
| 31 | 312 | result[i] += matrix[index] * vec[j]; | |
| 32 | } | ||
| 33 | } | ||
| 34 | 48 | return result; | |
| 35 | } | ||
| 36 | |||
| 37 | ✗ | double ConjugateGradientSeq::DotProduct(const std::vector<double> &a, const std::vector<double> &b) { | |
| 38 | double res = 0.0; | ||
| 39 |
6/8✓ Branch 0 taken 56 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 120 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 120 times.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
416 | for (size_t i = 0; i < a.size(); ++i) { |
| 40 | 296 | res += (a[i] * b[i]); | |
| 41 | } | ||
| 42 | ✗ | return res; | |
| 43 | } | ||
| 44 | |||
| 45 | 24 | bool ConjugateGradientSeq::RunImpl() { | |
| 46 | 24 | const auto &a_mat = GetInput().A; | |
| 47 | 24 | const auto &b_vec = GetInput().b; | |
| 48 | 24 | const int n = GetInput().n; | |
| 49 | |||
| 50 | 24 | std::vector<double> x(n, 0.0); | |
| 51 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<double> r = b_vec; |
| 52 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<double> p = r; |
| 53 | |||
| 54 | double rs_old = DotProduct(r, r); | ||
| 55 | |||
| 56 | 24 | const int max_iter = n * 2; | |
| 57 | const double epsilon = 1e-10; | ||
| 58 | |||
| 59 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | for (int k = 0; k < max_iter; ++k) { |
| 60 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<double> ap = MultiplyMatrixVector(a_mat, p, n); |
| 61 | double p_ap = DotProduct(p, ap); | ||
| 62 | |||
| 63 | 48 | const double alpha = rs_old / p_ap; | |
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (int i = 0; i < n; ++i) { |
| 66 | 120 | x[i] += (alpha * p[i]); | |
| 67 | 120 | r[i] -= (alpha * ap[i]); | |
| 68 | } | ||
| 69 | |||
| 70 | double rs_new = DotProduct(r, r); | ||
| 71 | |||
| 72 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (std::sqrt(rs_new) < epsilon) { |
| 73 | break; | ||
| 74 | } | ||
| 75 | |||
| 76 | 24 | const double beta = rs_new / rs_old; | |
| 77 | |||
| 78 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 24 times.
|
88 | for (int i = 0; i < n; ++i) { |
| 79 | 64 | p[i] = r[i] + (beta * p[i]); | |
| 80 | } | ||
| 81 | |||
| 82 | rs_old = rs_new; | ||
| 83 | } | ||
| 84 | |||
| 85 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetOutput() = x; |
| 86 | 24 | return true; | |
| 87 | } | ||
| 88 | |||
| 89 | 24 | bool ConjugateGradientSeq::PostProcessingImpl() { | |
| 90 | 24 | return true; | |
| 91 | } | ||
| 92 | |||
| 93 | } // namespace shekhirev_v_cg_method_seq | ||
| 94 |