| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <functional> | ||
| 6 | #include <optional> | ||
| 7 | #include <span> | ||
| 8 | |||
| 9 | namespace gutyansky_a_monte_carlo_multi_dimension { | ||
| 10 | |||
| 11 | class FunctionRegistry final { | ||
| 12 | public: | ||
| 13 | using IntegralFunction = std::function<double(std::span<double>)>; | ||
| 14 | |||
| 15 | 152 | struct FunctionDescription { | |
| 16 | size_t n_dims; | ||
| 17 | IntegralFunction func; | ||
| 18 | }; | ||
| 19 | |||
| 20 | 152 | static std::optional<FunctionDescription> GetIntegralFunction(size_t func_id) { | |
| 21 | auto f_const = [](std::span<double> /* not used */) { return 1.0; }; | ||
| 22 | auto f_linear = [](std::span<double> x) { | ||
| 23 | double res = 0.0; | ||
| 24 | |||
| 25 |
2/2✓ Branch 0 taken 13500000 times.
✓ Branch 1 taken 4500000 times.
|
18000000 | for (auto val : x) { |
| 26 | 13500000 | res += val; | |
| 27 | } | ||
| 28 | |||
| 29 | return res; | ||
| 30 | }; | ||
| 31 | auto f_product = [](std::span<double> x) { | ||
| 32 | double res = 1.0; | ||
| 33 | |||
| 34 |
2/2✓ Branch 0 taken 27000000 times.
✓ Branch 1 taken 4500000 times.
|
31500000 | for (auto val : x) { |
| 35 | 27000000 | res *= val; | |
| 36 | } | ||
| 37 | |||
| 38 | return res; | ||
| 39 | }; | ||
| 40 | auto f_quadric = [](std::span<double> x) { | ||
| 41 | double res = 0.0; | ||
| 42 | |||
| 43 |
2/2✓ Branch 0 taken 22500000 times.
✓ Branch 1 taken 9000000 times.
|
31500000 | for (auto val : x) { |
| 44 | 22500000 | res += val * val; | |
| 45 | } | ||
| 46 | |||
| 47 | return res; | ||
| 48 | }; | ||
| 49 | 4500000 | auto f_gauss = [](std::span<double> x) { | |
| 50 | double sum = 0.0; | ||
| 51 | |||
| 52 |
2/2✓ Branch 0 taken 9000000 times.
✓ Branch 1 taken 4500000 times.
|
13500000 | for (auto val : x) { |
| 53 | 9000000 | sum += val * val; | |
| 54 | } | ||
| 55 | |||
| 56 | 4500000 | return std::exp(-sum); | |
| 57 | }; | ||
| 58 | 4500000 | auto f_trig = [](std::span<double> x) { return std::sin(x[0]) * std::cos(x[1]); }; | |
| 59 | auto f_sphere = [](std::span<double> x) { | ||
| 60 | double r2 = 0.0; | ||
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 9000000 times.
✓ Branch 1 taken 4500000 times.
|
13500000 | for (auto val : x) { |
| 63 | 9000000 | r2 += val * val; | |
| 64 | } | ||
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 965759 times.
✓ Branch 1 taken 3534241 times.
|
4500000 | return (r2 <= 1.0) ? 1.0 : 0.0; |
| 67 | }; | ||
| 68 | |||
| 69 |
7/8✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 19 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 19 times.
✓ Branch 6 taken 19 times.
✗ Branch 7 not taken.
|
152 | switch (func_id) { |
| 70 | 19 | case 0: | |
| 71 | 19 | return FunctionDescription{.n_dims = 0, .func = f_const}; | |
| 72 | 19 | case 1: | |
| 73 | 19 | return FunctionDescription{.n_dims = 0, .func = f_linear}; | |
| 74 | 19 | case 2: | |
| 75 | 19 | return FunctionDescription{.n_dims = 0, .func = f_product}; | |
| 76 | 38 | case 3: | |
| 77 | 38 | return FunctionDescription{.n_dims = 0, .func = f_quadric}; | |
| 78 | 19 | case 4: | |
| 79 | 19 | return FunctionDescription{.n_dims = 0, .func = f_gauss}; | |
| 80 | 19 | case 5: | |
| 81 | 19 | return FunctionDescription{.n_dims = 2, .func = f_trig}; | |
| 82 | 19 | case 6: | |
| 83 | 19 | return FunctionDescription{.n_dims = 0, .func = f_sphere}; | |
| 84 | default: | ||
| 85 | ✗ | return std::nullopt; | |
| 86 | } | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | } // namespace gutyansky_a_monte_carlo_multi_dimension | ||
| 91 |