| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstdint> | ||
| 5 | #include <string> | ||
| 6 | #include <tuple> | ||
| 7 | |||
| 8 | #include "task/include/task.hpp" | ||
| 9 | |||
| 10 | namespace galkin_d_trapezoid_method { | ||
| 11 | |||
| 12 | struct Input { | ||
| 13 | double a; // левая граница интегрирования | ||
| 14 | double b; // правая граница | ||
| 15 | int n; // число разбиений (трапеций) | ||
| 16 | int func_id; // id функции для интегрирования | ||
| 17 | }; | ||
| 18 | |||
| 19 | using InType = Input; | ||
| 20 | using OutType = double; | ||
| 21 | using TestType = std::tuple<int, std::string>; | ||
| 22 | using BaseTask = ppc::task::Task<InType, OutType>; | ||
| 23 | |||
| 24 | enum class FunctionId : std::uint8_t { | ||
| 25 | kLinear = 0, // f(x) = x | ||
| 26 | kQuadratic = 1, // f(x) = x^2 | ||
| 27 | kSin = 2 // f(x) = sin(x) | ||
| 28 | }; | ||
| 29 | |||
| 30 | inline bool IsValidFunctionId(int func_id) { | ||
| 31 | 75072 | return func_id >= static_cast<int>(FunctionId::kLinear) && func_id <= static_cast<int>(FunctionId::kSin); | |
| 32 | } | ||
| 33 | |||
| 34 | 75032 | inline double Function(double x, int func_id) { | |
| 35 |
1/2✓ Branch 0 taken 75032 times.
✗ Branch 1 not taken.
|
75032 | if (!IsValidFunctionId(func_id)) { |
| 36 | return 0.0; | ||
| 37 | } | ||
| 38 | |||
| 39 | 75032 | const auto id = static_cast<FunctionId>(func_id); | |
| 40 |
3/3✓ Branch 0 taken 20008 times.
✓ Branch 1 taken 30008 times.
✓ Branch 2 taken 25016 times.
|
75032 | switch (id) { |
| 41 | case FunctionId::kLinear: | ||
| 42 | return x; // f(x) = x | ||
| 43 | 20008 | case FunctionId::kQuadratic: | |
| 44 | 20008 | return x * x; // f(x) = x^2 | |
| 45 | 30008 | case FunctionId::kSin: | |
| 46 | 30008 | return std::sin(x); // f(x) = sin(x) | |
| 47 | default: | ||
| 48 | return 0.0; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | 40 | inline double GetExactIntegral(const InType &in) { | |
| 53 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | if (!IsValidFunctionId(in.func_id)) { |
| 54 | return 0.0; | ||
| 55 | } | ||
| 56 | |||
| 57 | 40 | const double a = in.a; | |
| 58 | 40 | const double b = in.b; | |
| 59 | 40 | const auto id = static_cast<FunctionId>(in.func_id); | |
| 60 | |||
| 61 |
3/3✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
|
40 | switch (id) { |
| 62 | 20 | case FunctionId::kLinear: | |
| 63 | 20 | return (b * b - a * a) / 2.0; | |
| 64 | 10 | case FunctionId::kQuadratic: | |
| 65 | 10 | return (b * b * b - a * a * a) / 3.0; | |
| 66 | 10 | case FunctionId::kSin: | |
| 67 | 10 | return std::cos(a) - std::cos(b); | |
| 68 | default: | ||
| 69 | return 0.0; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | } // namespace galkin_d_trapezoid_method | ||
| 74 |