| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstdint> | ||
| 5 | #include <tuple> | ||
| 6 | |||
| 7 | #include "task/include/task.hpp" | ||
| 8 | |||
| 9 | namespace dergynov_s_trapezoid_integration { | ||
| 10 | |||
| 11 | struct Input { | ||
| 12 | double a; | ||
| 13 | double b; | ||
| 14 | int n; | ||
| 15 | int func_id; | ||
| 16 | }; | ||
| 17 | |||
| 18 | using InType = Input; | ||
| 19 | using OutType = double; | ||
| 20 | using TestType = std::tuple<int, const char *>; | ||
| 21 | using BaseTask = ppc::task::Task<InType, OutType>; | ||
| 22 | |||
| 23 | enum class FunctionId : std::uint8_t { | ||
| 24 | kLinear = 0, | ||
| 25 | kQuadratic = 1, | ||
| 26 | kSin = 2, | ||
| 27 | }; | ||
| 28 | |||
| 29 | inline bool IsValidFunctionId(int id) { | ||
| 30 | ✗ | return id >= 0 && id <= 2; | |
| 31 | } | ||
| 32 | |||
| 33 | ✗ | inline double Function(double x, int id) { | |
| 34 | ✗ | if (!IsValidFunctionId(id)) { | |
| 35 | return 0.0; | ||
| 36 | } | ||
| 37 | |||
| 38 | ✗ | switch (static_cast<FunctionId>(id)) { | |
| 39 | case FunctionId::kLinear: | ||
| 40 | return x; | ||
| 41 | ✗ | case FunctionId::kQuadratic: | |
| 42 | ✗ | return x * x; | |
| 43 | ✗ | case FunctionId::kSin: | |
| 44 | ✗ | return std::sin(x); | |
| 45 | default: | ||
| 46 | return 0.0; | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | ✗ | inline double GetExactIntegral(const InType &in) { | |
| 51 | ✗ | if (!IsValidFunctionId(in.func_id)) { | |
| 52 | return 0.0; | ||
| 53 | } | ||
| 54 | |||
| 55 | ✗ | const double a = in.a; | |
| 56 | ✗ | const double b = in.b; | |
| 57 | |||
| 58 | ✗ | switch (static_cast<FunctionId>(in.func_id)) { | |
| 59 | ✗ | case FunctionId::kLinear: | |
| 60 | ✗ | return (b * b - a * a) / 2.0; | |
| 61 | ✗ | case FunctionId::kQuadratic: | |
| 62 | ✗ | return (b * b * b - a * a * a) / 3.0; | |
| 63 | ✗ | case FunctionId::kSin: | |
| 64 | ✗ | return std::cos(a) - std::cos(b); | |
| 65 | default: | ||
| 66 | return 0.0; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | } // namespace dergynov_s_trapezoid_integration | ||
| 71 |