From 023c9896f1170deb3fa573c135595e7e4e74347b Mon Sep 17 00:00:00 2001 From: Nathan Young Date: Thu, 7 Mar 2024 14:33:55 -0500 Subject: [PATCH] ENH: Initialize Data - Increased Variance in Random Generation for Floats (#880) Modified Initialize Data Algorithm to Increase Variance in Random Generation for Float32 and Float64 --- .../SimplnxCore/Filters/InitializeData.cpp | 60 ++++++++++++++----- .../SimplnxCore/test/InitializeDataTest.cpp | 4 +- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/InitializeData.cpp b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/InitializeData.cpp index 273a4156ab..a49d085795 100644 --- a/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/InitializeData.cpp +++ b/src/Plugins/SimplnxCore/src/SimplnxCore/Filters/InitializeData.cpp @@ -162,7 +162,7 @@ void IncrementalFill(DataArray& dataArray, const std::vector& st } } -template +template void RandomFill(std::vector& dist, DataArray& dataArray, const uint64 seed, const bool standardizeSeed) { usize numComp = dataArray.getNumberOfComponents(); // We checked that the values string is greater than max comps size so proceed check free @@ -180,11 +180,25 @@ void RandomFill(std::vector& dist, DataArray& dataArray, const { for(usize comp = 0; comp < numComp; comp++) { - if constexpr(!std::is_same_v) + if constexpr(std::is_floating_point_v) { - dataArray[tup * numComp + comp] = static_cast(dist[comp](generators[comp])); + if constexpr(Ranged) + { + dataArray[tup * numComp + comp] = static_cast(dist[comp](generators[comp])); + } + if constexpr(!Ranged) + { + if constexpr(std::is_signed_v) + { + dataArray[tup * numComp + comp] = static_cast(dist[comp](generators[comp]) * (std::numeric_limits::max() - 1) * (((rand() & 1) == 0) ? 1 : -1)); + } + if constexpr(!std::is_signed_v) + { + dataArray[tup * numComp + comp] = static_cast(dist[comp](generators[comp]) * std::numeric_limits::max()); + } + } } - if constexpr(std::is_same_v) + if constexpr(!std::is_floating_point_v) { dataArray[tup * numComp + comp] = static_cast(dist[comp](generators[comp])); } @@ -208,7 +222,7 @@ void FillIncForwarder(const StepType& stepType, ArgsT&&... args) } } -template +template void FillRandomForwarder(const std::vector& range, usize numComp, ArgsT&&... args) { if constexpr(std::is_same_v) @@ -218,25 +232,39 @@ void FillRandomForwarder(const std::vector& range, usize numComp, ArgsT&&... { dists.emplace_back((range.at(comp) ? 1 : 0), (range.at(comp + 1) ? 1 : 0)); } - ::RandomFill>(dists, std::forward(args)...); + ::RandomFill>(dists, std::forward(args)...); + return; } - if constexpr(std::is_floating_point_v) + if constexpr(!std::is_floating_point_v) { - std::vector> dists; + std::vector> dists; for(usize comp = 0; comp < numComp * 2; comp += 2) { dists.emplace_back(range.at(comp), range.at(comp + 1)); } - ::RandomFill>(dists, std::forward(args)...); + ::RandomFill>(dists, std::forward(args)...); } - if constexpr(!std::is_floating_point_v && !std::is_same_v) + if constexpr(std::is_floating_point_v) { - std::vector> dists; - for(usize comp = 0; comp < numComp * 2; comp += 2) + if constexpr(Ranged) { - dists.emplace_back(range.at(comp), range.at(comp + 1)); + + std::vector> dists; + for(usize comp = 0; comp < numComp * 2; comp += 2) + { + dists.emplace_back(range.at(comp), range.at(comp + 1)); + } + ::RandomFill>(dists, std::forward(args)...); + } + if constexpr(!Ranged) + { + std::vector> dists; + for(usize comp = 0; comp < numComp * 2; comp += 2) + { + dists.emplace_back(0, 1); + } + ::RandomFill>(dists, std::forward(args)...); } - ::RandomFill>(dists, std::forward(args)...); } } @@ -291,7 +319,7 @@ struct FillArrayFunctor range.push_back(true); } } - return ::FillRandomForwarder(range, numComp, dataArray, inputValues.seed, inputValues.standardizeSeed); + return ::FillRandomForwarder(range, numComp, dataArray, inputValues.seed, inputValues.standardizeSeed); } case InitializeType::RangedRandom: { auto randBegin = standardizeMultiComponent(numComp, inputValues.randBegin); @@ -305,7 +333,7 @@ struct FillArrayFunctor result = ConvertTo::convert(randEnd[comp]); range.push_back(result.value()); } - return ::FillRandomForwarder(range, numComp, dataArray, inputValues.seed, inputValues.standardizeSeed); + return ::FillRandomForwarder(range, numComp, dataArray, inputValues.seed, inputValues.standardizeSeed); } } } diff --git a/src/Plugins/SimplnxCore/test/InitializeDataTest.cpp b/src/Plugins/SimplnxCore/test/InitializeDataTest.cpp index a985b673c9..f1c9c3afcb 100644 --- a/src/Plugins/SimplnxCore/test/InitializeDataTest.cpp +++ b/src/Plugins/SimplnxCore/test/InitializeDataTest.cpp @@ -665,8 +665,8 @@ TEST_CASE("SimplnxCore::InitializeData 20: Multi Component Non-Standardized-Rand } ::BoundsCheck(dataStructure.getDataRefAs(::k_BaselinePath), - {std::numeric_limits::min(), std::numeric_limits::max(), std::numeric_limits::min(), std::numeric_limits::max(), - std::numeric_limits::min(), std::numeric_limits::max()}); + {(-1 * (std::numeric_limits::max() - 1)), std::numeric_limits::max(), (-1 * (std::numeric_limits::max() - 1)), + std::numeric_limits::max(), (-1 * (std::numeric_limits::max() - 1)), std::numeric_limits::max()}); } TEST_CASE("SimplnxCore::InitializeData 21: Boolean Single Component Fill Initialization", "[SimplnxCore][InitializeData]")