From 20f5f42f8acb1733e83d337ad7dcf88a30b118a0 Mon Sep 17 00:00:00 2001 From: Calabras <164652443+Calabras@users.noreply.github.com> Date: Tue, 17 Feb 2026 00:05:34 +0300 Subject: [PATCH 1/5] =?UTF-8?q?=D0=A8=D0=B8=D0=BB=D0=B8=D0=BD=20=D0=9D.?= =?UTF-8?q?=D0=94.=203823=D0=911=D0=9F=D0=A01.=20=D0=A2=D0=B5=D1=85=D0=BD?= =?UTF-8?q?=D0=BE=D0=BB=D0=BE=D0=B3=D0=B8=D1=8F=20SEQ.=20=D0=92=D1=8B?= =?UTF-8?q?=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=BD?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=D0=BC=D0=B5=D1=80=D0=BD=D1=8B=D1=85=20=D0=B8?= =?UTF-8?q?=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0=D0=BB=D0=BE=D0=B2=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=BC=20=D0=9C=D0=BE=D0=BD=D1=82?= =?UTF-8?q?=D0=B5-=D0=9A=D0=B0=D1=80=D0=BB=D0=BE.=20=D0=92=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=B0=D0=BD=D1=82=2012?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/include/common.hpp | 122 ++++++++++++++++++ .../info.json | 9 ++ .../seq/include/ops_seq.hpp | 27 ++++ .../seq/src/ops_seq.cpp | 90 +++++++++++++ .../settings.json | 6 + .../tests/functional/main.cpp | 92 +++++++++++++ .../tests/performance/main.cpp | 56 ++++++++ 7 files changed, 402 insertions(+) create mode 100644 tasks/shilin_n_monte_carlo_integration/common/include/common.hpp create mode 100644 tasks/shilin_n_monte_carlo_integration/info.json create mode 100644 tasks/shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp create mode 100644 tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp create mode 100644 tasks/shilin_n_monte_carlo_integration/settings.json create mode 100644 tasks/shilin_n_monte_carlo_integration/tests/functional/main.cpp create mode 100644 tasks/shilin_n_monte_carlo_integration/tests/performance/main.cpp diff --git a/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp b/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp new file mode 100644 index 00000000..f4b4d6b0 --- /dev/null +++ b/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp @@ -0,0 +1,122 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "task/include/task.hpp" + +namespace shilin_n_monte_carlo_integration { + +enum class FuncType : std::uint8_t { + kConstant = 0, + kLinear = 1, + kProduct = 2, + kSumSquares = 3, + kSinProduct = 4, +}; + +class IntegrandFunction { + public: + static double Evaluate(FuncType func_type, const std::vector &point) { + switch (func_type) { + case FuncType::kConstant: + return 1.0; + case FuncType::kLinear: { + double sum = 0.0; + for (double x : point) { + sum += x; + } + return sum; + } + case FuncType::kProduct: { + double prod = 1.0; + for (double x : point) { + prod *= x; + } + return prod; + } + case FuncType::kSumSquares: { + double sum = 0.0; + for (double x : point) { + sum += x * x; + } + return sum; + } + case FuncType::kSinProduct: { + double prod = 1.0; + for (double x : point) { + prod *= std::sin(x); + } + return prod; + } + default: + return 0.0; + } + } + + static double AnalyticalIntegral(FuncType func_type, const std::vector &lower, + const std::vector &upper) { + auto dim = static_cast(lower.size()); + switch (func_type) { + case FuncType::kConstant: { + double vol = 1.0; + for (int i = 0; i < dim; ++i) { + vol *= (upper[i] - lower[i]); + } + return vol; + } + case FuncType::kLinear: { + double result = 0.0; + for (int i = 0; i < dim; ++i) { + double term = (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0; + for (int j = 0; j < dim; ++j) { + if (j != i) { + term *= (upper[j] - lower[j]); + } + } + result += term; + } + return result; + } + case FuncType::kProduct: { + double prod = 1.0; + for (int i = 0; i < dim; ++i) { + prod *= (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0; + } + return prod; + } + case FuncType::kSumSquares: { + double result = 0.0; + for (int i = 0; i < dim; ++i) { + double term = (upper[i] * upper[i] * upper[i] - lower[i] * lower[i] * lower[i]) / 3.0; + for (int j = 0; j < dim; ++j) { + if (j != i) { + term *= (upper[j] - lower[j]); + } + } + result += term; + } + return result; + } + case FuncType::kSinProduct: { + double prod = 1.0; + for (int i = 0; i < dim; ++i) { + prod *= (-std::cos(upper[i]) + std::cos(lower[i])); + } + return prod; + } + default: + return 0.0; + } + } +}; + +using InType = std::tuple, std::vector, int, FuncType>; +using OutType = double; +using TestType = std::tuple; +using BaseTask = ppc::task::Task; + +} // namespace shilin_n_monte_carlo_integration diff --git a/tasks/shilin_n_monte_carlo_integration/info.json b/tasks/shilin_n_monte_carlo_integration/info.json new file mode 100644 index 00000000..ce2b5fa7 --- /dev/null +++ b/tasks/shilin_n_monte_carlo_integration/info.json @@ -0,0 +1,9 @@ +{ + "student": { + "first_name": "Никита", + "last_name": "Шилин", + "middle_name": "Дмитриевич", + "group_number": "3823Б1ПР1", + "task_number": "12" + } +} diff --git a/tasks/shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp b/tasks/shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp new file mode 100644 index 00000000..331be2e6 --- /dev/null +++ b/tasks/shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "shilin_n_monte_carlo_integration/common/include/common.hpp" +#include "task/include/task.hpp" + +namespace shilin_n_monte_carlo_integration { + +class ShilinNMonteCarloIntegrationSEQ : public BaseTask { + public: + static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() { + return ppc::task::TypeOfTask::kSEQ; + } + explicit ShilinNMonteCarloIntegrationSEQ(const InType &in); + + private: + bool ValidationImpl() override; + bool PreProcessingImpl() override; + bool RunImpl() override; + bool PostProcessingImpl() override; + + std::vector lower_bounds_; + std::vector upper_bounds_; + int num_points_{}; + FuncType func_type_{}; +}; + +} // namespace shilin_n_monte_carlo_integration diff --git a/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp b/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp new file mode 100644 index 00000000..bb468f04 --- /dev/null +++ b/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp @@ -0,0 +1,90 @@ +#include "shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp" + +#include +#include + +#include "shilin_n_monte_carlo_integration/common/include/common.hpp" + +namespace shilin_n_monte_carlo_integration { + +ShilinNMonteCarloIntegrationSEQ::ShilinNMonteCarloIntegrationSEQ(const InType &in) { + SetTypeOfTask(GetStaticTypeOfTask()); + GetInput() = in; + GetOutput() = 0.0; +} + +bool ShilinNMonteCarloIntegrationSEQ::ValidationImpl() { + const auto &[lower, upper, n, func_type] = GetInput(); + if (lower.size() != upper.size() || lower.empty()) { + return false; + } + if (n <= 0) { + return false; + } + for (size_t i = 0; i < lower.size(); ++i) { + if (lower[i] >= upper[i]) { + return false; + } + } + if (func_type < FuncType::kConstant || func_type > FuncType::kSinProduct) { + return false; + } + return true; +} + +bool ShilinNMonteCarloIntegrationSEQ::PreProcessingImpl() { + const auto &[lower, upper, n, func_type] = GetInput(); + lower_bounds_ = lower; + upper_bounds_ = upper; + num_points_ = n; + func_type_ = func_type; + return true; +} + +bool ShilinNMonteCarloIntegrationSEQ::RunImpl() { + auto dimensions = static_cast(lower_bounds_.size()); + + // Quasi-random Kronecker sequence: alpha_d = fractional part of sqrt(prime_d) + const std::vector kAlpha = { + 0.41421356237309504, // frac(sqrt(2)) + 0.73205080756887729, // frac(sqrt(3)) + 0.23606797749978969, // frac(sqrt(5)) + 0.64575131106459059, // frac(sqrt(7)) + 0.31662479035539984, // frac(sqrt(11)) + 0.60555127546398929, // frac(sqrt(13)) + 0.12310562561766059, // frac(sqrt(17)) + 0.35889894354067355, // frac(sqrt(19)) + 0.79583152331271838, // frac(sqrt(23)) + 0.38516480713450403 // frac(sqrt(29)) + }; + + auto alpha_size = static_cast(kAlpha.size()); + std::vector current(dimensions, 0.5); + std::vector point(dimensions); + double sum = 0.0; + + for (int i = 0; i < num_points_; ++i) { + for (int d = 0; d < dimensions; ++d) { + current[d] += kAlpha[d % alpha_size]; + if (current[d] >= 1.0) { + current[d] -= 1.0; + } + point[d] = lower_bounds_[d] + (upper_bounds_[d] - lower_bounds_[d]) * current[d]; + } + sum += IntegrandFunction::Evaluate(func_type_, point); + } + + double volume = 1.0; + for (int d = 0; d < dimensions; ++d) { + volume *= (upper_bounds_[d] - lower_bounds_[d]); + } + + GetOutput() = volume * sum / static_cast(num_points_); + return true; +} + +bool ShilinNMonteCarloIntegrationSEQ::PostProcessingImpl() { + return true; +} + +} // namespace shilin_n_monte_carlo_integration diff --git a/tasks/shilin_n_monte_carlo_integration/settings.json b/tasks/shilin_n_monte_carlo_integration/settings.json new file mode 100644 index 00000000..85bc37ad --- /dev/null +++ b/tasks/shilin_n_monte_carlo_integration/settings.json @@ -0,0 +1,6 @@ +{ + "tasks": { + "seq": "enabled" + }, + "tasks_type": "threads" +} diff --git a/tasks/shilin_n_monte_carlo_integration/tests/functional/main.cpp b/tasks/shilin_n_monte_carlo_integration/tests/functional/main.cpp new file mode 100644 index 00000000..00bfffc8 --- /dev/null +++ b/tasks/shilin_n_monte_carlo_integration/tests/functional/main.cpp @@ -0,0 +1,92 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "shilin_n_monte_carlo_integration/common/include/common.hpp" +#include "shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp" +#include "util/include/func_test_util.hpp" +#include "util/include/util.hpp" + +namespace shilin_n_monte_carlo_integration { + +class ShilinNRunFuncTestsThreads : public ppc::util::BaseRunFuncTests { + public: + static std::string PrintTestParam(const TestType &test_param) { + return std::get<1>(test_param); + } + + protected: + void SetUp() override { + TestType params = std::get(ppc::util::GTestParamIndex::kTestParams)>(GetParam()); + input_data_ = std::get<0>(params); + } + + bool CheckTestOutputData(OutType &output_data) final { + const auto &[lower, upper, n, func_type] = input_data_; + double expected = IntegrandFunction::AnalyticalIntegral(func_type, lower, upper); + + double volume = 1.0; + for (size_t i = 0; i < lower.size(); ++i) { + volume *= (upper[i] - lower[i]); + } + double epsilon = std::max(volume * 10.0 / std::sqrt(static_cast(n)), 1e-2); + return std::abs(output_data - expected) <= epsilon; + } + + InType GetTestInputData() final { + return input_data_; + } + + private: + InType input_data_; +}; + +namespace { + +TEST_P(ShilinNRunFuncTestsThreads, MonteCarloIntegration) { + ExecuteTest(GetParam()); +} + +const std::array kTestParam = {{ + std::make_tuple(std::make_tuple(std::vector{0.0}, std::vector{1.0}, 10000, FuncType::kLinear), + "kLinear_1D"), + std::make_tuple(std::make_tuple(std::vector{0.0}, std::vector{2.0}, 10000, FuncType::kSumSquares), + "kSumSquares_1D"), + std::make_tuple( + std::make_tuple(std::vector{0.0, 0.0}, std::vector{1.0, 1.0}, 50000, FuncType::kConstant), + "kConstant_2D"), + std::make_tuple( + std::make_tuple(std::vector{0.0, 0.0}, std::vector{1.0, 1.0}, 50000, FuncType::kLinear), + "kLinear_2D"), + std::make_tuple( + std::make_tuple(std::vector{0.0, 0.0}, std::vector{1.0, 1.0}, 50000, FuncType::kProduct), + "kProduct_2D"), + std::make_tuple( + std::make_tuple(std::vector{0.0, 0.0}, std::vector{1.0, 1.0}, 50000, FuncType::kSinProduct), + "kSinProduct_2D"), + std::make_tuple(std::make_tuple(std::vector{0.0, 0.0, 0.0}, std::vector{1.0, 1.0, 1.0}, 100000, + FuncType::kLinear), + "kLinear_3D"), + std::make_tuple(std::make_tuple(std::vector{0.0, 0.0, 0.0}, std::vector{1.0, 1.0, 1.0}, 100000, + FuncType::kProduct), + "kProduct_3D"), +}}; + +const auto kTestTasksList = ppc::util::AddFuncTask( + kTestParam, PPC_SETTINGS_shilin_n_monte_carlo_integration); + +const auto kGtestValues = ppc::util::ExpandToValues(kTestTasksList); + +const auto kPerfTestName = ShilinNRunFuncTestsThreads::PrintFuncTestName; + +INSTANTIATE_TEST_SUITE_P(MonteCarloTests, ShilinNRunFuncTestsThreads, kGtestValues, kPerfTestName); + +} // namespace + +} // namespace shilin_n_monte_carlo_integration diff --git a/tasks/shilin_n_monte_carlo_integration/tests/performance/main.cpp b/tasks/shilin_n_monte_carlo_integration/tests/performance/main.cpp new file mode 100644 index 00000000..e6dfc7e2 --- /dev/null +++ b/tasks/shilin_n_monte_carlo_integration/tests/performance/main.cpp @@ -0,0 +1,56 @@ +#include + +#include +#include +#include +#include + +#include "shilin_n_monte_carlo_integration/common/include/common.hpp" +#include "shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp" +#include "util/include/perf_test_util.hpp" + +namespace shilin_n_monte_carlo_integration { + +class ShilinNRunPerfTestThreads : public ppc::util::BaseRunPerfTests { + InType input_data_; + + void SetUp() override { + input_data_ = std::make_tuple(std::vector{0.0, 0.0, 0.0}, std::vector{1.0, 1.0, 1.0}, 10000000, + FuncType::kSumSquares); + } + + bool CheckTestOutputData(OutType &output_data) final { + const auto &[lower, upper, n, func_type] = input_data_; + double expected = IntegrandFunction::AnalyticalIntegral(func_type, lower, upper); + + double volume = 1.0; + for (size_t i = 0; i < lower.size(); ++i) { + volume *= (upper[i] - lower[i]); + } + double epsilon = std::max(volume * 10.0 / std::sqrt(static_cast(n)), 1e-2); + return std::abs(output_data - expected) <= epsilon; + } + + InType GetTestInputData() final { + return input_data_; + } +}; + +TEST_P(ShilinNRunPerfTestThreads, RunPerfModes) { + ExecuteTest(GetParam()); +} + +namespace { + +const auto kAllPerfTasks = + ppc::util::MakeAllPerfTasks(PPC_SETTINGS_shilin_n_monte_carlo_integration); + +const auto kGtestValues = ppc::util::TupleToGTestValues(kAllPerfTasks); + +const auto kPerfTestName = ShilinNRunPerfTestThreads::CustomPerfTestName; + +INSTANTIATE_TEST_SUITE_P(RunModeTests, ShilinNRunPerfTestThreads, kGtestValues, kPerfTestName); + +} // namespace + +} // namespace shilin_n_monte_carlo_integration From cad37155065c71df8ae973aa72f069e79f21be61 Mon Sep 17 00:00:00 2001 From: Calabras <164652443+Calabras@users.noreply.github.com> Date: Wed, 25 Feb 2026 13:18:48 +0300 Subject: [PATCH 2/5] Fix info.json key ordering for pretty-format-json hook --- tasks/shilin_n_monte_carlo_integration/info.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/shilin_n_monte_carlo_integration/info.json b/tasks/shilin_n_monte_carlo_integration/info.json index ce2b5fa7..1c80bfce 100644 --- a/tasks/shilin_n_monte_carlo_integration/info.json +++ b/tasks/shilin_n_monte_carlo_integration/info.json @@ -1,9 +1,9 @@ { "student": { "first_name": "Никита", + "group_number": "3823Б1ПР1", "last_name": "Шилин", "middle_name": "Дмитриевич", - "group_number": "3823Б1ПР1", - "task_number": "12" + "task_number": "1" } } From d07da1fc8d784cae6977f247050c7ca8276d2d5f Mon Sep 17 00:00:00 2001 From: Calabras <164652443+Calabras@users.noreply.github.com> Date: Wed, 25 Feb 2026 13:21:44 +0300 Subject: [PATCH 3/5] Limit max dimensions to 10 and remove modulo indexing for Kronecker alphas --- tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp b/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp index bb468f04..dcbb7836 100644 --- a/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp +++ b/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp @@ -29,6 +29,10 @@ bool ShilinNMonteCarloIntegrationSEQ::ValidationImpl() { if (func_type < FuncType::kConstant || func_type > FuncType::kSinProduct) { return false; } + constexpr size_t kMaxDimensions = 10; + if (lower.size() > kMaxDimensions) { + return false; + } return true; } @@ -58,14 +62,13 @@ bool ShilinNMonteCarloIntegrationSEQ::RunImpl() { 0.38516480713450403 // frac(sqrt(29)) }; - auto alpha_size = static_cast(kAlpha.size()); std::vector current(dimensions, 0.5); std::vector point(dimensions); double sum = 0.0; for (int i = 0; i < num_points_; ++i) { for (int d = 0; d < dimensions; ++d) { - current[d] += kAlpha[d % alpha_size]; + current[d] += kAlpha[d]; if (current[d] >= 1.0) { current[d] -= 1.0; } From f693afbb86d62fdd74f88006ad718ddd24fe0907 Mon Sep 17 00:00:00 2001 From: Calabras <164652443+Calabras@users.noreply.github.com> Date: Thu, 26 Feb 2026 12:19:08 +0300 Subject: [PATCH 4/5] Fix clang-tidy warnings: reduce cognitive complexity, fix naming, add missing includes --- .../common/include/common.hpp | 110 ++++++++++-------- .../seq/include/ops_seq.hpp | 2 + .../seq/src/ops_seq.cpp | 23 ++-- .../tests/performance/main.cpp | 1 + 4 files changed, 76 insertions(+), 60 deletions(-) diff --git a/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp b/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp index f4b4d6b0..df43e06c 100644 --- a/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp +++ b/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp @@ -61,57 +61,73 @@ class IntegrandFunction { const std::vector &upper) { auto dim = static_cast(lower.size()); switch (func_type) { - case FuncType::kConstant: { - double vol = 1.0; - for (int i = 0; i < dim; ++i) { - vol *= (upper[i] - lower[i]); - } - return vol; - } - case FuncType::kLinear: { - double result = 0.0; - for (int i = 0; i < dim; ++i) { - double term = (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0; - for (int j = 0; j < dim; ++j) { - if (j != i) { - term *= (upper[j] - lower[j]); - } - } - result += term; - } - return result; - } - case FuncType::kProduct: { - double prod = 1.0; - for (int i = 0; i < dim; ++i) { - prod *= (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0; - } - return prod; - } - case FuncType::kSumSquares: { - double result = 0.0; - for (int i = 0; i < dim; ++i) { - double term = (upper[i] * upper[i] * upper[i] - lower[i] * lower[i] * lower[i]) / 3.0; - for (int j = 0; j < dim; ++j) { - if (j != i) { - term *= (upper[j] - lower[j]); - } - } - result += term; - } - return result; - } - case FuncType::kSinProduct: { - double prod = 1.0; - for (int i = 0; i < dim; ++i) { - prod *= (-std::cos(upper[i]) + std::cos(lower[i])); - } - return prod; - } + case FuncType::kConstant: + return ComputeVolume(lower, upper); + case FuncType::kLinear: + return ComputeLinearIntegral(lower, upper, dim); + case FuncType::kProduct: + return ComputeProductIntegral(lower, upper, dim); + case FuncType::kSumSquares: + return ComputeSumSquaresIntegral(lower, upper, dim); + case FuncType::kSinProduct: + return ComputeSinProductIntegral(lower, upper, dim); default: return 0.0; } } + + private: + static double ComputeVolume(const std::vector &lower, const std::vector &upper) { + double vol = 1.0; + for (int i = 0; i < static_cast(lower.size()); ++i) { + vol *= (upper[i] - lower[i]); + } + return vol; + } + + static double VolumeExcludingDim(const std::vector &lower, const std::vector &upper, int exclude) { + double product = 1.0; + for (int j = 0; j < static_cast(lower.size()); ++j) { + if (j != exclude) { + product *= (upper[j] - lower[j]); + } + } + return product; + } + + static double ComputeLinearIntegral(const std::vector &lower, const std::vector &upper, int dim) { + double result = 0.0; + for (int i = 0; i < dim; ++i) { + double term = (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0; + result += term * VolumeExcludingDim(lower, upper, i); + } + return result; + } + + static double ComputeProductIntegral(const std::vector &lower, const std::vector &upper, int dim) { + double prod = 1.0; + for (int i = 0; i < dim; ++i) { + prod *= (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0; + } + return prod; + } + + static double ComputeSumSquaresIntegral(const std::vector &lower, const std::vector &upper, int dim) { + double result = 0.0; + for (int i = 0; i < dim; ++i) { + double term = (upper[i] * upper[i] * upper[i] - lower[i] * lower[i] * lower[i]) / 3.0; + result += term * VolumeExcludingDim(lower, upper, i); + } + return result; + } + + static double ComputeSinProductIntegral(const std::vector &lower, const std::vector &upper, int dim) { + double prod = 1.0; + for (int i = 0; i < dim; ++i) { + prod *= (-std::cos(upper[i]) + std::cos(lower[i])); + } + return prod; + } }; using InType = std::tuple, std::vector, int, FuncType>; diff --git a/tasks/shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp b/tasks/shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp index 331be2e6..f0895ed1 100644 --- a/tasks/shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp +++ b/tasks/shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "shilin_n_monte_carlo_integration/common/include/common.hpp" #include "task/include/task.hpp" diff --git a/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp b/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp index dcbb7836..e8d49c4a 100644 --- a/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp +++ b/tasks/shilin_n_monte_carlo_integration/seq/src/ops_seq.cpp @@ -1,6 +1,6 @@ #include "shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp" -#include +#include #include #include "shilin_n_monte_carlo_integration/common/include/common.hpp" @@ -30,10 +30,7 @@ bool ShilinNMonteCarloIntegrationSEQ::ValidationImpl() { return false; } constexpr size_t kMaxDimensions = 10; - if (lower.size() > kMaxDimensions) { - return false; - } - return true; + return lower.size() <= kMaxDimensions; } bool ShilinNMonteCarloIntegrationSEQ::PreProcessingImpl() { @@ -49,7 +46,7 @@ bool ShilinNMonteCarloIntegrationSEQ::RunImpl() { auto dimensions = static_cast(lower_bounds_.size()); // Quasi-random Kronecker sequence: alpha_d = fractional part of sqrt(prime_d) - const std::vector kAlpha = { + const std::vector alpha = { 0.41421356237309504, // frac(sqrt(2)) 0.73205080756887729, // frac(sqrt(3)) 0.23606797749978969, // frac(sqrt(5)) @@ -67,19 +64,19 @@ bool ShilinNMonteCarloIntegrationSEQ::RunImpl() { double sum = 0.0; for (int i = 0; i < num_points_; ++i) { - for (int d = 0; d < dimensions; ++d) { - current[d] += kAlpha[d]; - if (current[d] >= 1.0) { - current[d] -= 1.0; + for (int di = 0; di < dimensions; ++di) { + current[di] += alpha[di]; + if (current[di] >= 1.0) { + current[di] -= 1.0; } - point[d] = lower_bounds_[d] + (upper_bounds_[d] - lower_bounds_[d]) * current[d]; + point[di] = lower_bounds_[di] + ((upper_bounds_[di] - lower_bounds_[di]) * current[di]); } sum += IntegrandFunction::Evaluate(func_type_, point); } double volume = 1.0; - for (int d = 0; d < dimensions; ++d) { - volume *= (upper_bounds_[d] - lower_bounds_[d]); + for (int di = 0; di < dimensions; ++di) { + volume *= (upper_bounds_[di] - lower_bounds_[di]); } GetOutput() = volume * sum / static_cast(num_points_); diff --git a/tasks/shilin_n_monte_carlo_integration/tests/performance/main.cpp b/tasks/shilin_n_monte_carlo_integration/tests/performance/main.cpp index e6dfc7e2..1e01d36e 100644 --- a/tasks/shilin_n_monte_carlo_integration/tests/performance/main.cpp +++ b/tasks/shilin_n_monte_carlo_integration/tests/performance/main.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include From 46e1c7559afb70bf509bf6001ef2c3b3a1fc6bd4 Mon Sep 17 00:00:00 2001 From: Calabras <164652443+Calabras@users.noreply.github.com> Date: Thu, 26 Feb 2026 12:30:06 +0300 Subject: [PATCH 5/5] Fix signed/unsigned comparison warnings (modernize-use-integer-sign-comparison) --- .../common/include/common.hpp | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp b/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp index df43e06c..396e2587 100644 --- a/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp +++ b/tasks/shilin_n_monte_carlo_integration/common/include/common.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -59,7 +60,7 @@ class IntegrandFunction { static double AnalyticalIntegral(FuncType func_type, const std::vector &lower, const std::vector &upper) { - auto dim = static_cast(lower.size()); + size_t dim = lower.size(); switch (func_type) { case FuncType::kConstant: return ComputeVolume(lower, upper); @@ -79,15 +80,15 @@ class IntegrandFunction { private: static double ComputeVolume(const std::vector &lower, const std::vector &upper) { double vol = 1.0; - for (int i = 0; i < static_cast(lower.size()); ++i) { + for (size_t i = 0; i < lower.size(); ++i) { vol *= (upper[i] - lower[i]); } return vol; } - static double VolumeExcludingDim(const std::vector &lower, const std::vector &upper, int exclude) { + static double VolumeExcludingDim(const std::vector &lower, const std::vector &upper, size_t exclude) { double product = 1.0; - for (int j = 0; j < static_cast(lower.size()); ++j) { + for (size_t j = 0; j < lower.size(); ++j) { if (j != exclude) { product *= (upper[j] - lower[j]); } @@ -95,35 +96,37 @@ class IntegrandFunction { return product; } - static double ComputeLinearIntegral(const std::vector &lower, const std::vector &upper, int dim) { + static double ComputeLinearIntegral(const std::vector &lower, const std::vector &upper, size_t dim) { double result = 0.0; - for (int i = 0; i < dim; ++i) { + for (size_t i = 0; i < dim; ++i) { double term = (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0; result += term * VolumeExcludingDim(lower, upper, i); } return result; } - static double ComputeProductIntegral(const std::vector &lower, const std::vector &upper, int dim) { + static double ComputeProductIntegral(const std::vector &lower, const std::vector &upper, size_t dim) { double prod = 1.0; - for (int i = 0; i < dim; ++i) { + for (size_t i = 0; i < dim; ++i) { prod *= (upper[i] * upper[i] - lower[i] * lower[i]) / 2.0; } return prod; } - static double ComputeSumSquaresIntegral(const std::vector &lower, const std::vector &upper, int dim) { + static double ComputeSumSquaresIntegral(const std::vector &lower, const std::vector &upper, + size_t dim) { double result = 0.0; - for (int i = 0; i < dim; ++i) { + for (size_t i = 0; i < dim; ++i) { double term = (upper[i] * upper[i] * upper[i] - lower[i] * lower[i] * lower[i]) / 3.0; result += term * VolumeExcludingDim(lower, upper, i); } return result; } - static double ComputeSinProductIntegral(const std::vector &lower, const std::vector &upper, int dim) { + static double ComputeSinProductIntegral(const std::vector &lower, const std::vector &upper, + size_t dim) { double prod = 1.0; - for (int i = 0; i < dim; ++i) { + for (size_t i = 0; i < dim; ++i) { prod *= (-std::cos(upper[i]) + std::cos(lower[i])); } return prod;